int main( int argc, char *argv[] ) { int ch, i; while( (ch = getopt( argc, argv, optStr )) != -1 ) { if( ch == '?' ) { Quit( usageMsg, NULL ); } doOption( ch ); } displayBanner(); if( argc < 2 ) { Quit( usageMsg, "No files specified\n" ); } if( quietFlag ) { VerboseFlag = false; } for( i = 1; i < argc; i++ ) { if( argv[i][0] == '@' ) { processOptionFile( &argv[i][1] ); } else { processFileList( argv[i] ); } } if( appendFlag ) { if( VerboseFlag ) { printf( "Generated %u tags.\n", TagCount ); } ReadExtraTags( fileName ); } GenerateTagsFile( fileName ); return( 0 ); } /* main */
HXTStatus hxtParseOptions(const int argc, char* argv[]) { int dashdash = 0; int trailing = getNextTrailingOption(0); for (int i=1; i<argc; i++) { const char *arg = NULL; HXTOption* opt = NULL; if(dashdash || argv[i][0]!='-' || (argv[i][0]=='-' && argv[i][1]=='\0')){ if(trailing==-1 || optionList[trailing].valuePtr==NULL) return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "Additional argument \"%s\" does not correspond to any option", argv[i]); else HXT_CHECK( doOption(&optionList[trailing], argv[i], optionList[trailing].description) ); trailing = getNextTrailingOption(trailing); } else if(argv[i][1]=='-') { /* long opt */ if(argv[i][2]=='\0') /* -- terminate argument parsing, everything from now on is a trailing option */ { dashdash = 1; continue; } char* equalSign = strchr(argv[i]+2,'='); if(equalSign!=NULL){ *equalSign = '\0'; arg = equalSign + 1; } int num = searchLongOption(argv[i]+2); if(num<0){ return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "option \"%s\" not found", argv[i]); } opt = &optionList[num]; if(equalSign!=NULL){ *equalSign = '='; if(opt->valueType==HXT_FLAG || opt->valueType==HXT_NO_FLAG){ return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "option \"%s\" takes no argument", argv[i]); } } else{ if(num==0) { // it's the help option return HXT_STATUS_INTERNAL; } if(opt->valueType!=HXT_FLAG && opt->valueType!=HXT_NO_FLAG) { if(argc<=i+1) { return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "options \"%s\" requires an argument", argv[i]); } else { arg = argv[i+1]; i++; } } } HXT_CHECK( doOption(opt, arg, argv[i]) ); } else{ /* short option */ int cond = argv[i][1]; for(int j=1; cond; j++){ int num = searchShortOption(argv[i][j]); if(num<0){ return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "option \'%c\' in \"%s\" not found", argv[i][j], argv[i]); } char optName[] = "- "; optName[1] = argv[i][j]; cond = argv[i][j+1]; if(num==0){ return HXT_STATUS_INTERNAL; } opt = &optionList[num]; if(opt->valueType!=HXT_FLAG && opt->valueType!=HXT_NO_FLAG){ if(cond){ arg = argv[i] + j+1; cond = 0; // stop the loop } else if(argc<=i+1) { return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "options \'%c\' in \"%s\" requires an argument", argv[i][j], argv[i]); } else { arg = argv[i+1]; i++; } } HXT_CHECK( doOption(opt, arg, optName) ); } } } if(trailing!=-1) { if(getNextTrailingOption(trailing)!=-1) return HXT_ERROR_MSG(HXT_STATUS_FORMAT_ERROR, "No \"%s\" given", optionList[trailing].description); } HXT_CHECK( hxtFree(&optionList) ); return HXT_STATUS_OK; }
bool HttpServlet::doRun(session& session, socket_stream* stream /* = NULL */) { socket_stream* in; socket_stream* out; bool cgi_mode; if (stream == NULL) { // 数据流为空,则当 CGI 模式处理,将标准输入输出 // 作为数据流 stream = NEW socket_stream(); (void) stream->open(ACL_VSTREAM_IN); in = stream; stream = NEW socket_stream(); (void) stream->open(ACL_VSTREAM_OUT); out = stream; cgi_mode = true; } else { in = out = stream; cgi_mode = false; } // req/res 采用栈变量,减少内存分配次数 HttpServletResponse res(*out); HttpServletRequest req(res, session, *in, local_charset_, parse_body_enable_, parse_body_limit_); if (rw_timeout_ >= 0) req.setRwTimeout(rw_timeout_); res.setCgiMode(cgi_mode); bool ret; http_method_t method = req.getMethod(); switch (method) { case HTTP_METHOD_GET: ret = doGet(req, res); break; case HTTP_METHOD_POST: ret = doPost(req, res); break; case HTTP_METHOD_PUT: ret = doPut(req, res); break; case HTTP_METHOD_CONNECT: ret = doConnect(req, res); break; case HTTP_METHOD_PURGE: ret = doPurge(req, res); break; case HTTP_METHOD_DELETE: ret = doDelete(req, res); break; case HTTP_METHOD_HEAD: ret = doHead(req, res); break; case HTTP_METHOD_OPTION: ret = doOption(req, res); break; default: ret = false; // 有可能是IO失败或未知方法 if (req.getLastError() == HTTP_REQ_ERR_METHOD) doUnknown(req, res); else doError(req, res); break; } if (in != out) { // 如果是标准输入输出流,则需要先将数据流与标准输入输出解绑, // 然后才能释放数据流对象,数据流内部会自动判断流句柄合法性 // 这样可以保证与客户端保持长连接 in->unbind(); out->unbind(); delete in; delete out; } return ret; }
/* * processOptionFile - process an option file */ static void processOptionFile( const char *fname ) { FILE *optfile; char option[MAX_STR]; char *ptr; char *cmd, *arg; int ch; optfile = fopen( fname, "r" ); if( optfile == NULL ) { printf( "Could not open option file %s\n", fname ); return; } while( (ptr = fgets( option, sizeof( option ), optfile )) != NULL ) { while( isspace( *ptr ) ) { ptr++; } if( *ptr == '#' || *ptr == '\0' ) { continue; } cmd = ptr; while( !isspace( *ptr ) && *ptr != '\0' ) { ptr++; } if( *ptr == '\0' ) { continue; } *ptr = '\0'; ptr++; while( isspace( *ptr ) ) { ptr++; } if( *ptr == '\0' ) { continue; } if( !stricmp( cmd, "file" ) ) { for( ;; ) { arg = ptr; while( !isspace( *ptr ) && *ptr != ',' && *ptr != '\0' ) { ptr++; } ch = *ptr; *ptr = '\0'; processFileList( arg ); if( ch == '\0' ) { break; } ptr++; while( isspace( *ptr ) || *ptr == ',' ) { ptr++; } if( *ptr == '\0' ) { break; } } } else if( !stricmp( cmd, "option" ) ) { WantTypedefs = false; WantMacros = false; WantAllDefines = false; WantUSE = false; while( *ptr != '\0' ) { if( *ptr == 'f' ) { ptr++; while( isspace( *ptr ) ) { ptr++; } if( *ptr == '\0' ) { break; } strcpy( tmpFileName, ptr ); ptr = tmpFileName; while( !isspace( *ptr ) ) { ptr++; } *ptr = '\0'; optarg = tmpFileName; doOption( 'f' ); break; } doOption( *ptr ); ptr++; } } } fclose( optfile ); } /* processOptionFile */
int main(int argc, const char **argv) { #ifdef _WIN32 _setmode( _fileno( stdout ), _O_BINARY ); _setmode( _fileno( stdin ), _O_BINARY ); #endif Owned<IProperties> globals = createProperties("dumpkey.ini", true); for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') doOption(argv[i]); else if (strchr(argv[i], '=')) globals->loadProp(argv[i]); else globals->setProp("keyfile", argv[i]); } try { StringBuffer logname("dumpkey."); logname.append(GetCachedHostName()).append("."); StringBuffer lf; openLogFile(lf, logname.append("log").str()); Owned <IKeyIndex> index; const char * keyName = globals->queryProp("keyfile"); if (keyName) index.setown(createKeyIndex(keyName, 0, false, false)); else usage(); Owned <IKeyCursor> cursor = index->getCursor(NULL); size32_t key_size = index->keySize(); Owned<IFile> in = createIFile(keyName); Owned<IFileIO> io = in->open(IFOread); if (!io) throw MakeStringException(999, "Failed to open file %s", keyName); Owned<CKeyHdr> header = new CKeyHdr; MemoryAttr block(sizeof(KeyHdr)); io->read(0, sizeof(KeyHdr), (void *)block.get()); header->load(*(KeyHdr*)block.get()); unsigned nodeSize = header->getNodeSize(); if (!optRaw) { printf("Key '%s'\nkeySize=%d NumParts=%x, Top=%d\n", keyName, key_size, index->numParts(), index->isTopLevelKey()); printf("File size = %"I64F"d, nodes = %"I64F"d\n", in->size(), in->size() / nodeSize - 1); printf("rootoffset=%"I64F"d[%"I64F"d]\n", header->getRootFPos(), header->getRootFPos()/nodeSize); } char *buffer = (char*)alloca(key_size); if (globals->hasProp("node")) { if (stricmp(globals->queryProp("node"), "all")==0) { } else { int node = globals->getPropInt("node"); if (node != 0) index->dumpNode(stdout, node * nodeSize, globals->getPropInt("recs", 0), optRaw); } } else if (globals->hasProp("fpos")) { index->dumpNode(stdout, globals->getPropInt("fpos"), globals->getPropInt("recs", 0), optRaw); } else { bool backwards=false; bool ok; if (globals->hasProp("end")) { memset(buffer, 0, key_size); strcpy(buffer, globals->queryProp("end")); ok = cursor->ltEqual(buffer, buffer); backwards = true; } else if (globals->hasProp("start")) { memset(buffer, 0, key_size); strcpy(buffer, globals->queryProp("start")); ok = cursor->gtEqual(buffer, buffer); } else ok = cursor->first(buffer); unsigned count = globals->getPropInt("recs", 1); while (ok && count--) { offset_t pos = cursor->getFPos(); unsigned __int64 seq = cursor->getSequence(); size32_t size = cursor->getSize(); if (optRaw) { fwrite(buffer, 1, size, stdout); } else if (optHex) { for (unsigned i = 0; i < size; i++) printf("%02x", ((unsigned char) buffer[i]) & 0xff); printf(" :%"I64F"u:%012"I64F"x\n", seq, pos); } else printf("%.*s :%"I64F"u:%012"I64F"x\n", size, buffer, seq, pos); if (backwards) ok = cursor->prev(buffer); else ok = cursor->next(buffer); } } } catch (IException *E) { StringBuffer msg; E->errorMessage(msg); E->Release(); fatal("%s", msg.str()); } releaseAtoms(); ExitModuleObjects(); return 0; }