//========================================================================== // ArgInit // Initializes the command line arguments list. //========================================================================== void ArgInit(const char *cmdline) { // Clear the names register. num_names = 0; memset(names, 0, sizeof(names)); // First count the number of arguments. num_args = ArgParse(PM_COUNT, cmdline); // Allocate memory for the list. args = calloc(num_args, sizeof(char *)); // Then create the actual list of arguments. ArgParse(PM_NORMAL, cmdline); }
int __cdecl main(int argc, const char **argv) { DWORD Mask = 0x1; int IsAdd = -1; PCSTR devName = NULL; HRESULT hr; PARG_BASE *Options = GenSupportedOptions(); hr = ArgParse(Options, TOTAL_OPT, argc - 1, argv + 1); do { if (FAILED(hr)) { ArgsHelp(Options, TOTAL_OPT); break; } if (((PARG_LITERAL)Options[ADD_FILTER_OPT])->Count == 1) { IsAdd = 1; if (((PARG_INT)Options[AFFINITY_OPT])->Count == 1) { Mask = ((PARG_INT)Options[AFFINITY_OPT])->Values[0]; } } else { if (((PARG_LITERAL)Options[RM_FILTER_OPT])->Count == 1) { IsAdd = 0; } } if (((PARG_STR)Options[DEVNAME_OPT])->Count == 1) { devName = ((PARG_STR)Options[DEVNAME_OPT])->Values[0]; } } while (FALSE); FreeSupportedOptions(Options); if (IsAdd != -1) AddRmKeyForAllDevice(Mask, IsAdd, devName); return 1; }
int main(int argc, char *argv[]) { #ifdef WIN32 WSADATA wdata; #endif #ifndef NODOWNLOAD #ifdef WIN32 if(WSAStartup(MAKEWORD(2, 2), &wdata) != 0) return -1; #else #ifdef DOWNLOAD_LIBCURL curl_global_init(CURL_GLOBAL_ALL); #endif /* DOWNLOAD_LIBCURL */ #endif /* WIN32 */ #endif /* NODOWNLOAD */ #ifdef WIN32 SetConsoleTitle("dnsforwarder"); #endif /* WIN32 */ ArgParse(argc, argv); if( ConfigFile == NULL ) { ConfigFile = malloc(320); if( ConfigFile == NULL ) { return -1; } GetDefaultConfigureFile(ConfigFile, 320); } PRINTM("DNSforwarder by holmium. Version "VERSION__" . License : GPL v3.\nTime of compilation : %s %s.\n\n", __DATE__, __TIME__); #ifndef WIN32 PRINTM("Please run `dnsforwarder -p' if something wrong.\n\n") #endif PRINTM("Configure File : %s\n\n", ConfigFile); if( DeamonMode == TRUE ) { if( DaemonInit() == 0 ) { ShowMassages = FALSE; ErrorMessages = FALSE; } else { printf("Daemon init failed, continuing on non-daemon mode.\n"); } } if( QueryDNSInterfaceInit(ConfigFile) != 0 ) goto JustEnd; putchar('\n'); if( QueryDNSInterfaceStart() != 0 ) goto JustEnd; QueryDNSInterfaceWait(); JustEnd: #ifdef WIN32 WSACleanup(); #endif return 0; }
int main(int argc, char * argv[]) { HDBC hdbc; HENV henv; HSTMT hstmt; RETCODE rc; UCHAR uid[UID_LEN]; UCHAR pwd[PWD_LEN]; UCHAR file[OPT1_LEN]; UCHAR dsn[DSN_LEN]; uid[0] = (UCHAR)NULL; file[0] = (UCHAR)NULL; dsn[0] = (UCHAR)NULL; pwd[0] = (UCHAR)NULL; if (argc > 1) { int argIndex; /* Input argument index */ int argCount; /* Running count of arguments */ for (argCount=argc-1, argIndex=1; argCount > 0; argIndex++, argCount--) { switch (ArgParse(argv[argIndex])) { case HELP_ARG: printf(USAGE_MSG1, argv[0]); return(1); case PWD_ARG: argIndex++; if (argCount <= 1) { printf(PWD_ERR1); printf(USAGE_MSG1, argv[0]); return(1); } if (strlen(argv[argIndex]) > PWD_LEN) { printf(PWD_MSG1, PWD_LEN); return(1); } strcpy((char*)pwd, argv[argIndex]); argCount--; break; case UID_ARG: argIndex++; if (argCount <= 1) { printf(UID_ERR1); printf(USAGE_MSG1, argv[0]); return(1); } if (strlen(argv[argIndex]) > UID_LEN) { printf(UID_MSG1, UID_LEN); return(1); } strcpy((char*)uid, argv[argIndex]); argCount--; break; case DSN_ARG: argIndex++; if (argCount <= 1) { printf(DSN_ERR1); printf(USAGE_MSG1, argv[0]); return(1); } if (strlen(argv[argIndex]) > DSN_LEN) { printf(DSN_MSG1, DSN_LEN); return(1); } strcpy((char*)dsn, argv[argIndex]); argCount--; break; case FILE_ARG: argIndex++; if (argCount <= 1) { printf(FILE_ERR1); printf(USAGE_MSG1, argv[0]); return(1); } if (strlen(argv[argIndex]) > OPT1_LEN) { printf(FILE_MSG1, OPT1_LEN); return(1); } strcpy((char*)file, argv[argIndex]); argCount--; break; } } } else { printf(USAGE_MSG1, argv[0]); return(1); } #if !defined (__cplusplus) && defined (hppa) /* ** C programs must call the HP C++ Object initializer function. */ _main (); #endif rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) { printf("Failed to initialize ODBC environmment...\n"); exit(255); /* Exit with failure */ } ODBC_initialize ( &henv ); ODBC_prepareCon ( henv, &hdbc ); rc = ODBC_connect(henv, hdbc, dsn, uid, pwd); if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) exit(255); /* Exit with failure */ /* ** Allocate a HSTMT to communicate with ODBC DB Driver. */ rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) { printf ("Unable to Allocate a statement handle.\n"); ODBC_getErrorInfo (henv, hdbc, hstmt); ODBC_releaseEnv(henv, hdbc); exit (255); } if(file[0] == (UCHAR) NULL) { printf("\nPlease enter SQL script file: "); scanf("%s", (char*)file); } ODBC_fileRead ((char*)file, henv, hdbc, hstmt); SQLFreeHandle (SQL_HANDLE_STMT, hstmt); ODBC_releaseEnv (henv, hdbc); return(0); }
HRESULT GetCmd( OUT PCMD_INFO Cmd, int argc, char **argv) { HRESULT hr; PARG_BASE *Options; int i; Options = SupportedOptions(); hr = ArgParse(Options, ARG_TABLE_END, argc, argv); if (SUCCEEDED(hr)) { hr = S_OK; if (((PARG_LITERAL)Options[READ_REGS_OPT])->Count) { Cmd->IoCtrlCode = IOCTL_DISPLAY_DEBUG_REGS; } if (((PARG_LITERAL)Options[DUMP_OPT])->Count) { Cmd->IoCtrlCode = IOCTL_SET_RX_DUMP; } if (((PARG_LITERAL)Options[INFO_OPT])->Count) { Cmd->IoCtrlCode = IOCTL_GET_INFO; } if (((PARG_STR)Options[UPDATE_MAC_ADDR_OPT])->Count) { hr = ParseMACAddr(Cmd, ((PARG_STR)Options[UPDATE_MAC_ADDR_OPT])->Values[0]); } if (((PARG_INT)Options[PREAMBLE_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[PREAMBLE_OPT], PreambleTypeCheck, IOCTL_SET_PREAMBLE_TYPE); } if (((PARG_INT)Options[CHANNEL_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[CHANNEL_OPT], CentralFreqCheck, IOCTL_SET_CHANNEL); } if (((PARG_INT)Options[FREQ_OFFSET_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[FREQ_OFFSET_OPT], NULL, IOCTL_SET_FREQ_OFFSET); } if (((PARG_INT)Options[RXGAIN_PRESET0])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[RXGAIN_PRESET0], NULL, IOCTL_SET_RX_GAIN_PRESET0); } if (((PARG_INT)Options[RXGAIN_PRESET1])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[RXGAIN_PRESET1], NULL, IOCTL_SET_RX_GAIN_PRESET1); } if (((PARG_INT)Options[RXGAIN_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[RXGAIN_OPT], NULL, IOCTL_SET_RX_GAIN); } if (((PARG_INT)Options[RXPA_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[RXPA_OPT], NULL, IOCTL_SET_RX_PA); } if (((PARG_INT)Options[TXGAIN_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[TXGAIN_OPT], NULL, IOCTL_SET_TX_GAIN); } if (((PARG_INT)Options[DATA_RATE_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[DATA_RATE_OPT], DataRateCheck, IOCTL_SET_DATA_RATE); } if (((PARG_INT)Options[SPD_MAX_BLOCK])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[SPD_MAX_BLOCK], NULL, IOCTL_SET_SPD_MAX_BLOCK); } if (((PARG_INT)Options[SPD_POWER_THRESHOLD])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[SPD_POWER_THRESHOLD], NULL, IOCTL_SET_SPD_POWER_THRESHOLD); } if (((PARG_INT)Options[SPD_POWER_THRESHOLD_LH])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[SPD_POWER_THRESHOLD_LH], NULL, IOCTL_SET_SPD_POWER_THRESHOLD_LH); } if (((PARG_INT)Options[SPD_POWER_THRESHOLD_HL])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[SPD_POWER_THRESHOLD_HL], NULL, IOCTL_SET_SPD_POWER_THRESHOLD_HL); } if (((PARG_INT)Options[SAMPLE_RATE_OPT])->Count) { hr = IntArg2IoCode(Cmd, (PARG_INT)Options[SAMPLE_RATE_OPT], NULL, IOCTL_SET_SAMPLE_RATE); } } if (FAILED(hr)) { AboutMe(); ArgsHelp(Options, ARG_TABLE_END); } SupportedOptionsDtor(Options); return hr; }
//========================================================================== // ArgParse // Parses the given command line. //========================================================================== int ArgParse(int mode, const char *cmdline) { char word[512], *ptr, *response, *cmd = (char *) cmdline; int i; int count = 0; FILE *file; boolean quote = false; boolean is_response; // Init normal mode? if(mode == PM_NORMAL) arg_pos = 0; for(; *cmd;) { // Must allow: -cmd "echo ""this is a command""" // But also: @"\Program Files\test.rsp\" // And also: @\"Program Files"\test.rsp // Hello" My"Friend means "Hello MyFriend" // Skip initial whitespace. cmd = M_SkipWhite(cmd); // Check for response files. if(*cmd == '@') { is_response = true; cmd = M_SkipWhite(cmd + 1); } else { is_response = false; } for(ptr = word; *cmd && (quote || !isspace(*cmd)); cmd++) { if(!quote) { // We're not inside quotes. if(*cmd == '\"') // Quote begins. { quote = true; continue; } } else { // We're inside quotes. if(*cmd == '\"') // Quote ends. { if(cmd[1] == '\"') // Doubles? { // Normal processing, but output only one quote. cmd++; } else { quote = false; continue; } } } *ptr++ = *cmd; } // Append NULL to the word. *ptr++ = 0; // Word has been extracted, examine it. if(is_response) // Response file? { // Try to open it. if((file = fopen(word, "rt")) != NULL) { // How long is it? fseek(file, 0, SEEK_END); i = ftell(file); rewind(file); response = calloc(i + 1, 1); fread(response, 1, i, file); fclose(file); count += ArgParse(mode == PM_COUNT ? PM_COUNT : PM_NORMAL_REC, response); free(response); } } else if(!strcmp(word, "--")) // End of arguments. { return count; } else if(word[0]) // Make sure there *is* a word. { // Increase argument count. count++; if(mode != PM_COUNT) { // Allocate memory for the argument and copy it to the list. args[arg_pos] = malloc(strlen(word) + 1); strcpy(args[arg_pos++], word); } } } return count; }