static int ProcessMessage(char *Buffer, char *Response) { char *Cmd = strtok(Buffer, " "); if (Cmd == NULL) { sprintf(Response, "no command specified"); return -1; } if (strcmp(Cmd, "add") == 0) { char *Token = strtok(NULL, " "); unsigned FilterID; char *FileName; if (Token == NULL) { sprintf(Response, "wrong format. expect: add <filter ID> <file name>"); return -1; } FilterID = atoi(Token); FileName = strtok(NULL, " "); if (FileName == NULL) { sprintf(Response, "wrong format. expect: add <filter ID> <file name>"); return -1; } if (AddFilter(FilterID, FileName) == -1) { sprintf(Response, "failed to add the filter"); return -1; } sprintf(Response, "filter %u is successfully added", FilterID); } else if (strcmp(Cmd, "del") == 0) { char *Token = strtok(NULL, " "); unsigned FilterID; if (Token == NULL) { sprintf(Response, "wrong format. expect: del <filter ID>"); return -1; } FilterID = atoi(Token); if (DeleteFilter(FilterID) == -1) { sprintf(Response, "failed to delete the filter"); return -1; } sprintf(Response, "filter %u is successfully deleted", FilterID); } else if (strcmp(Cmd, "ls") == 0) { unsigned FilterIDs[MaxNumFilters]; unsigned NumFilters = ListFilters(FilterIDs, MaxNumFilters); unsigned i; unsigned Printed = sprintf(Response, "filter IDs:"); for (i = 0; i < NumFilters; ++i) { Printed += sprintf(Response + Printed, " %u", FilterIDs[i]); } } else { sprintf(Response, "unknown command"); return -1; } return 0; }
static void ParseOptions( _In_ PACE_FILTER_OPTIONS opt, _In_ int argc, _In_ TCHAR * argv[] ) { const static struct option long_options[] = { { _T("list-plugins"), no_argument, 0, _T('L') }, { _T("list-importers"), no_argument, 0, _T('I') }, { _T("list-filters"), no_argument, 0, _T('F') }, { _T("list-writers"), no_argument, 0, _T('W') }, { _T("importers"), required_argument, 0, _T('i') }, { _T("filters"), required_argument, 0, _T('f') }, { _T("writers"), required_argument, 0, _T('w') }, { _T("invert-filters"), required_argument, 0, _T('r') }, { _T("progression"), required_argument, 0, _T('p') }, { _T("logfile"), required_argument, 0, _T('l') }, { _T("dbglvl"), required_argument, 0, _T('d') }, { _T("loglvl"), required_argument, 0, _T('d') }, { _T("help"), no_argument, 0, _T('h') }, { _T("usage"), no_argument, 0, _T('h') }, { 0, 0, 0, 0 } }; int curropt = 0; BOOL bExitAfterOpt = FALSE; LPTSTR filter = NULL; LPTSTR ctx = NULL; // // Default options // SetLogLevel(DEFAULT_OPT_DEBUGLEVEL); opt->misc.progression = DEFAULT_OPT_PROGRESSION; // // Parsing // while ((curropt = getopt_long_only(argc, argv, EMPTY_STR, long_options, NULL)) != -1) { switch (curropt) { // // List // case _T('L'): SetLogLevel(_T("NONE")); ListImporters(); ListFilters(); ListWriters(); ExitProcess(EXIT_SUCCESS); break; case _T('I'): SetLogLevel(_T("NONE")); ListImporters(); bExitAfterOpt = TRUE; break; case _T('F'): SetLogLevel(_T("NONE")); ListFilters(); bExitAfterOpt = TRUE; break; case _T('W'): SetLogLevel(_T("NONE")); ListWriters(); bExitAfterOpt = TRUE; break; // // Plugins // case _T('i'): opt->names.importers = optarg; opt->plugins.numberOfImporters = PluginLoadImporters(opt->names.importers, opt->plugins.importers, PLUGIN_MAX_IMPORTERS); break; case _T('f'): opt->names.filters = optarg; opt->plugins.numberOfFilters = PluginLoadFilters(opt->names.filters, opt->plugins.filters, PLUGIN_MAX_FILTERS); break; case _T('w'): opt->names.writers = optarg; opt->plugins.numberOfWriters = PluginLoadWriters(opt->names.writers, opt->plugins.writers, PLUGIN_MAX_WRITERS); break; case _T('r'): opt->names.inverted = optarg; DWORD i = 0; filter = NULL; ctx = NULL; // TODO : invert after filters (numOfF == 0) while (StrNextToken(opt->names.inverted, ",", &ctx, &filter)) { for (i = 0; i < PLUGIN_MAX_FILTERS; i++) { if (PLUGIN_IS_LOADED(&opt->plugins.filters[i])) { if (STR_EQ(filter, PLUGIN_GET_NAME(&opt->plugins.filters[i]))) { LOG(Info, SUB_LOG(_T("Inverting filter <%s>")), filter); opt->plugins.filters[i].inverted = TRUE; break; } } } if (i == PLUGIN_MAX_FILTERS) { FATAL(_T("Trying to invert a non-loaded filter : <%s>"), filter); } } break; // // Misc // case _T('p'): opt->misc.progression = _tstoi(optarg); LOG(Info, SUB_LOG(_T("Printing progression every <%u> ACE")), opt->misc.progression); break; case _T('l'): opt->misc.logfile = optarg; SetLogFile(opt->misc.logfile); break; case _T('d'): opt->misc.loglvl = optarg; SetLogLevel(opt->misc.loglvl); break; case _T('h'): opt->misc.showHelp = TRUE; break; default: FATAL(_T("Unknown option")); break; } } LPTSTR optname = NULL; LPTSTR optval = NULL; int i = 0; for (i = optind; i < argc; i++) { ctx = NULL; StrNextToken(argv[i], "=", &ctx, &optname); optval = ctx; //We do not use StrNextToken here because optval can contain an equal sign. if (!optname || !optval) { FATAL(_T("Cannot parse plugin option <%s> (must be in format name=value)"), argv[i]); } else { LOG(Dbg, _T("Adding plugin option <%s : %s>"), optname, optval); AddStrPair(&gs_PluginOptions.end, optname, optval); if (!gs_PluginOptions.head) { gs_PluginOptions.head = gs_PluginOptions.end; } } } if (bExitAfterOpt) { ExitProcess(EXIT_SUCCESS); } }