static void clear_args (struct gengetopt_args_info *args_info) { args_info->lanes_conf_arg = gengetopt_strdup ("Lanes.conf"); args_info->lanes_conf_orig = NULL; args_info->stoplines_conf_arg = gengetopt_strdup ("StopLines.conf"); args_info->stoplines_conf_orig = NULL; args_info->no_stoplines_flag = 1; args_info->no_lanes_flag = 0; args_info->camera_conf_arg = gengetopt_strdup ("CameraInfo.conf"); args_info->camera_conf_orig = NULL; args_info->list_file_arg = NULL; args_info->list_file_orig = NULL; args_info->list_path_arg = gengetopt_strdup (""); args_info->list_path_orig = NULL; args_info->image_file_arg = NULL; args_info->image_file_orig = NULL; args_info->wait_arg = 0; args_info->wait_orig = NULL; args_info->show_flag = 0; args_info->step_flag = 0; args_info->show_lane_numbers_flag = 0; args_info->output_suffix_arg = gengetopt_strdup ("_results"); args_info->output_suffix_orig = NULL; args_info->save_images_flag = 0; args_info->save_lanes_flag = 0; args_info->debug_flag = 0; }
static void clear_args (struct gengetopt_args_info *args_info) { args_info->port_arg = 1213; args_info->port_orig = NULL; args_info->front_arg = gengetopt_strdup ("SuggestFront"); args_info->front_orig = NULL; args_info->quit_key_arg = gengetopt_strdup ("alt+shift+0"); args_info->quit_key_orig = NULL; args_info->launch_key_arg = gengetopt_strdup ("alt+shift+p"); args_info->launch_key_orig = NULL; }
static void clear_args (struct gengetopt_args_info *args_info) { args_info->fg_flag = 0; args_info->debug_flag = 0; args_info->conf_arg = gengetopt_strdup ("/etc/ggsn.conf"); args_info->conf_orig = NULL; args_info->pidfile_arg = gengetopt_strdup ("/var/run/ggsn.pid"); args_info->pidfile_orig = NULL; args_info->statedir_arg = gengetopt_strdup ("/var/lib/ggsn/"); args_info->statedir_orig = NULL; args_info->listen_arg = NULL; args_info->listen_orig = NULL; args_info->net_arg = gengetopt_strdup ("192.168.0.0/24"); args_info->net_orig = NULL; args_info->ipup_arg = NULL; args_info->ipup_orig = NULL; args_info->ipdown_arg = NULL; args_info->ipdown_orig = NULL; args_info->dynip_arg = NULL; args_info->dynip_orig = NULL; args_info->statip_arg = NULL; args_info->statip_orig = NULL; args_info->pcodns1_arg = gengetopt_strdup ("0.0.0.0"); args_info->pcodns1_orig = NULL; args_info->pcodns2_arg = gengetopt_strdup ("0.0.0.0"); args_info->pcodns2_orig = NULL; args_info->timelimit_arg = 0; args_info->timelimit_orig = NULL; args_info->apn_arg = gengetopt_strdup ("internet"); args_info->apn_orig = NULL; args_info->qos_arg = 0x0b921f; args_info->qos_orig = NULL; }
int cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) { int c; /* Character of the parsed option. */ int missing_required_options = 0; args_info->help_given = 0 ; args_info->version_given = 0 ; args_info->auto_given = 0 ; args_info->coordinated_given = 0 ; args_info->delay_given = 0 ; args_info->federation_given = 0 ; args_info->logfile_given = 0 ; args_info->name_given = 0 ; args_info->demo_given = 0 ; args_info->timer_given = 0 ; args_info->verbose_given = 0 ; args_info->xoffset_given = 0 ; args_info->yoffset_given = 0 ; args_info->initx_given = 0 ; args_info->inity_given = 0 ; args_info->filename_given = 0 ; args_info->notimestamp_given = 0 ; #define clear_args() { \ args_info->coordinated_flag = 1;\ args_info->federation_arg = NULL; \ args_info->logfile_arg = NULL; \ args_info->name_arg = NULL; \ args_info->demo_arg = NULL; \ args_info->verbose_flag = 0;\ args_info->filename_arg = NULL;\ args_info->notimestamp_flag = 1;\ } clear_args(); optarg = 0; optind = 1; opterr = 1; optopt = '?'; while (1) { int option_index = 0; char *stop_char; static struct option long_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { "auto", 1, NULL, 'a' }, { "coordinated", 0, NULL, 'c' }, { "delay", 1, NULL, 'd' }, { "notimestamp", 0, NULL, 'e' }, { "federation", 1, NULL, 'f' }, { "logfile", 1, NULL, 'l' }, { "name", 1, NULL, 'n' }, { "demo", 1, NULL, 'o' }, { "timer", 1, NULL, 't' }, { "verbose", 0, NULL, 'v' }, { "xoffset", 1, NULL, 'x' }, { "yoffset", 1, NULL, 'y' }, { "initx", 1, NULL, 'X' }, { "inity", 1, NULL, 'Y' }, { "filename", 1, NULL, 'F' }, { NULL, 0, NULL, 0 } }; stop_char = 0; c = getopt_long (argc, argv, "hVa:cd:f:l:n:o:t:vx:y:X:Y:F:e", long_options, &option_index); if (c == -1) break; /* Exit from `while (1)' loop. */ switch (c) { case 'h': /* Print help and exit. */ clear_args (); cmdline_parser_print_help (); exit (EXIT_SUCCESS); case 'V': /* Print version and exit. */ clear_args (); cmdline_parser_print_version (); exit (EXIT_SUCCESS); case 'a': /* auto start. */ if (args_info->auto_given) { fprintf (stderr, "%s: `--auto' (`-a') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->auto_given = 1; args_info->auto_arg = strtol (optarg,&stop_char,0); break; case 'c': /* coordinated time. */ if (args_info->coordinated_given) { fprintf (stderr, "%s: `--coordinated' (`-c') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->coordinated_given = 1; args_info->coordinated_flag = !(args_info->coordinated_flag); break; case 'd': /* delay before 1st step. */ if (args_info->delay_given) { fprintf (stderr, "%s: `--delay' (`-d') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->delay_given = 1; args_info->delay_arg = strtol (optarg,&stop_char,0); break; case 'e': /* no time stamp. */ if (args_info->notimestamp_given) { fprintf (stderr, "%s: `--notimestamp' (`-e') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->notimestamp_given = 1; args_info->notimestamp_flag = !(args_info->notimestamp_flag); break; case 'f': /* federation name. */ if (args_info->federation_given) { fprintf (stderr, "%s: `--federation' (`-f') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->federation_given = 1; args_info->federation_arg = gengetopt_strdup (optarg); break; case 'l': /* file to log events. */ if (args_info->logfile_given) { fprintf (stderr, "%s: `--logfile' (`-l') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->logfile_given = 1; args_info->logfile_arg = gengetopt_strdup (optarg); break; case 'n': /* federate name. */ if (args_info->name_given) { fprintf (stderr, "%s: `--name' (`-n') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->name_given = 1; args_info->name_arg = gengetopt_strdup (optarg); break; case 'o': /* select demo (static-ddm, dynamic-ddm). */ if (args_info->demo_given) { fprintf (stderr, "%s: `--demo' (`-o') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->demo_given = 1; args_info->demo_arg = gengetopt_strdup (optarg); break; case 't': /* timer. */ if (args_info->timer_given) { fprintf (stderr, "%s: `--timer' (`-t') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->timer_given = 1; args_info->timer_arg = strtol (optarg,&stop_char,0); break; case 'v': /* verbose mode. */ if (args_info->verbose_given) { fprintf (stderr, "%s: `--verbose' (`-v') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->verbose_given = 1; args_info->verbose_flag = !(args_info->verbose_flag); break; case 'x': /* X offset (X11). */ if (args_info->xoffset_given) { fprintf (stderr, "%s: `--xoffset' (`-x') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->xoffset_given = 1; args_info->xoffset_arg = strtol (optarg,&stop_char,0); break; case 'y': /* Y offset (X11). */ if (args_info->yoffset_given) { fprintf (stderr, "%s: `--yoffset' (`-y') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->yoffset_given = 1; args_info->yoffset_arg = strtol (optarg,&stop_char,0); break; case 'X': /* ball initial X value. */ if (args_info->initx_given) { fprintf (stderr, "%s: `--initx' (`-X') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->initx_given = 1; args_info->initx_arg = strtol (optarg,&stop_char,0); break; case 'Y': /* ball initial Y value. */ if (args_info->inity_given) { fprintf (stderr, "%s: `--inity' (`-Y') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->inity_given = 1; args_info->inity_arg = strtol (optarg,&stop_char,0); break; case 'F': /* FED file name */ if (args_info->filename_given) { fprintf (stderr, "%s: `--filename' (`-F') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->filename_given = 1; args_info->filename_arg = gengetopt_strdup (optarg); break; case 0: /* Long option with no short option */ case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ exit (EXIT_FAILURE); default: /* bug: option not considered. */ fprintf (stderr, "%s: option unknown: %c\n", CMDLINE_PARSER_PACKAGE, c); abort (); } /* switch */ } /* while */ if (! args_info->federation_given) { fprintf (stderr, "%s: '--federation' ('-f') option required\n", CMDLINE_PARSER_PACKAGE); missing_required_options = 1; } if (! args_info->name_given) { fprintf (stderr, "%s: '--name' ('-n') option required\n", CMDLINE_PARSER_PACKAGE); missing_required_options = 1; } if (! args_info->filename_given) { fprintf (stderr, "%s: '--filename' ('-F') option required\n", CMDLINE_PARSER_PACKAGE); missing_required_options = 1; } if ( missing_required_options ) exit (EXIT_FAILURE); return 0; }
int cmdline_parser( int argc, char * const *argv, struct gengetopt_args_info *args_info ) { /* Character of the parsed option. */ int c; /* Tmd retval */ int r; int missing_required_options = 0; args_info->help_given = 0; args_info->version_given = 0; args_info->user_given = 0; args_info->pass_given = 0; args_info->remuser_given = 0; args_info->rempass_given = 0; args_info->proxy_given = 0; args_info->proxyauth_given = 0; args_info->proxyhost_given = 0; args_info->proxyport_given = 0; args_info->dest_given = 0; args_info->remproxy_given = 0; args_info->remproxyauth_given = 0; args_info->verbose_given = 0; args_info->ntlm_given = 0; args_info->inetd_given = 0; args_info->quiet_given = 0; args_info->header_given = 0; args_info->domain_given = 0; args_info->encrypt_given = 0; args_info->encryptproxy_given = 0; args_info->encryptremproxy_given = 0; args_info->proctitle_given = 0; args_info->host_given = 0; /* No... we can't make this a function... -- Maniac */ #define clear_args() \ { \ args_info->user_arg = NULL; \ args_info->pass_arg = NULL; \ args_info->remuser_arg = NULL; \ args_info->rempass_arg = NULL; \ args_info->domain_arg = NULL; \ args_info->proxy_arg = NULL; \ args_info->proxyauth_arg = NULL; \ args_info->proxyhost_arg = NULL; \ args_info->dest_arg = NULL; \ args_info->remproxy_arg = NULL; \ args_info->remproxyauth_arg = NULL; \ args_info->header_arg[0] = '\0'; \ args_info->verbose_flag = 0; \ args_info->ntlm_flag = 0; \ args_info->inetd_flag = 0; \ args_info->quiet_flag = 0; \ args_info->standalone_arg = 0; \ args_info->encrypt_flag = 0; \ args_info->encryptproxy_flag = 0; \ args_info->encryptremproxy_flag = 0; \ args_info->proctitle_arg = NULL; \ args_info->host_arg = NULL; \ } clear_args(); optarg = 0; #ifdef HAVE_GETOPT_LONG optind = 1; opterr = 1; optopt = '?'; #endif while (1) { #ifdef HAVE_GETOPT_LONG int option_index = 0; /* Struct option: Name, Has_arg, Flag, Value */ static struct option long_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { "user", 1, NULL, 'u' }, { "pass", 1, NULL, 's' }, { "domain", 1, NULL, 't' }, // { "uservar", 1, NULL, 'U' }, // { "passvar", 1, NULL, 'S' }, { "passfile", 1, NULL, 'F' }, { "proxy", 1, NULL, 'p' }, { "proxyauth", 1, NULL, 'P' }, { "dest", 1, NULL, 'd' }, { "remproxy", 1, NULL, 'r' }, { "remproxyauth", 1, NULL, 'R' }, { "proctitle", 1, NULL, 'x' }, { "host", 1, NULL, 'o' }, { "header", 1, NULL, 'H' }, { "verbose", 0, NULL, 'v' }, { "ntlm", 0, NULL, 'N' }, { "inetd", 0, NULL, 'i' }, { "standalone", 1, NULL, 'a' }, { "quiet", 0, NULL, 'q' }, { "encrypt", 0, NULL, 'e' }, { "encrypt-proxy", 0, NULL, 'E' }, { "encrypt-remproxy",0,NULL, 'X' }, { NULL, 0, NULL, 0 } }; c = getopt_long (argc, argv, "hVia:u:s:t:F:p:P:r:R:d:H:x:nvNeEXqo:", long_options, &option_index); #else c = getopt( argc, argv, "hVia:u:s:t:F:p:P:r:R:d:H:x:nvNeEXqo:" ); #endif if (c == -1) break; /* Exit from `while (1)' loop. */ switch (c) { case 'h': /* Print help and exit. */ clear_args (); cmdline_parser_print_help (); exit(0); #ifdef USE_SSL case 'e': /* Turn on SSL encryption */ args_info->encrypt_flag = !(args_info->encrypt_flag); if( args_info->verbose_flag ) message("SSL enabled\n"); break; case 'E': /* Turn on client to proxy SSL encryption */ args_info->encryptproxy_flag = !(args_info->encryptproxy_flag); if( args_info->verbose_flag ) message("SSL client to proxy enabled\n"); break; #endif case 'i': /* Run from inetd. */ if ( args_info->standalone_arg > 0 ) { fprintf( stderr, "%s: '--inetd' ('-i') conflicts with '--standalone' ('-a')\n", PACKAGE ); clear_args(); exit( 1 ); } args_info->inetd_flag = !(args_info->inetd_flag); break; case 'a': /* Run as standalone daemon */ if ( args_info->inetd_flag ) { fprintf( stderr, "%s: `--standalone' (`-a') conflicts with `--inetd' (`-i')\n", PACKAGE ); clear_args(); exit(1); } if ( ( args_info->standalone_arg = atoi( optarg ) ) < 1 ) { fprintf( stderr, "%s: Illegal port value for `--standalone' (`-a')\n", PACKAGE); clear_args(); exit(1); } break; case 'V': /* Print version and exit. */ clear_args (); cmdline_parser_print_version (); exit(0); case 'x': args_info->proctitle_given = 1; message( "Proctitle override enabled\n" ); args_info->proctitle_arg = gengetopt_strdup (optarg); break; case 'o': args_info->host_given = 1; /* message( "Host-header override enabled\n" ); */ args_info->host_arg = gengetopt_strdup (optarg); break; case 'u': /* Username to send to HTTPS proxy for authentication. */ if (args_info->user_given) { fprintf (stderr, "%s: `--user' (`-u'), `--proxyauth' (`-P') or `--passfile' (`-F') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->user_given = 1; args_info->user_arg = gengetopt_strdup (optarg); message ("Option -u/--user is deprecated, please use -P/--proxyauth user:pass\n"); break; case 's': /* Password to send to HTTPS proxy for authentication. */ if (args_info->pass_given) { fprintf (stderr, "%s: `--pass' (`-s') or `--passfile' (`-F') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->pass_given = 1; args_info->pass_arg = gengetopt_strdup (optarg); message ("Option -s/--pass is deprecated, please use -P/--proxyauth user:pass\n"); break; case 't': /* Env Var with NTLM DOMAIN (when overriding). */ if (args_info->domain_given) { fprintf (stderr, "%s: `--domain' (`-t') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->domain_given = 1; args_info->domain_arg = gengetopt_strdup (optarg); break; case 'F': /* File containing Username & Password to send to HTTPS proxy for authentication. */ if (args_info->user_given) { fprintf (stderr, "%s: `--user' (`-u') or `--passfile' (`-F') option given more than once\n", PACKAGE); clear_args (); exit(1); } if (args_info->pass_given) { fprintf (stderr, "%s: `--pass' (`-s') or `--passfile' (`-F') option given more than once\n", PACKAGE); clear_args (); exit(1); } char *result = getCredentialsFromFile(optarg, &(args_info->user_arg), &(args_info->pass_arg), &(args_info->remuser_arg), &(args_info->rempass_arg) ); if ( args_info->user_arg != NULL ) args_info->user_given = 1; if ( args_info->pass_arg != NULL ) args_info->pass_given = 1; if ( args_info->remuser_arg != NULL ) args_info->remuser_given = 1; if ( args_info->rempass_arg != NULL ) args_info->rempass_given = 1; if( result != NULL ) { fprintf( stderr, "%s: Bad password file for `--passfile' (`-F')\n%s\n", PACKAGE, result); clear_args(); exit(1); } break; case 'p': /* HTTPS Proxy host:port to connect to. */ if (args_info->proxy_given) { fprintf (stderr, "%s: `--proxy' (`-p') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->proxy_given = 1; args_info->proxy_arg = gengetopt_strdup (optarg); break; case 'P': /* HTTPS Proxy auth user:pass for local proxy */ if (args_info->proxyauth_given) { fprintf (stderr, "%s: `--proxyauth' (`-P') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->proxyauth_given = 1; args_info->proxyauth_arg = gengetopt_strdup (optarg); break; case 'r': /* Use a remote proxy */ if (args_info->remproxy_given) { fprintf (stderr, "%s: `--remproxy' (`-r') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->remproxy_given = 1; args_info->remproxy_arg = gengetopt_strdup (optarg); break; case 'R': /* HTTPS Proxy auth user:pass for remote proxy */ if (args_info->remproxyauth_given) { fprintf (stderr, "%s: `--remproxyauth' (`-P') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->remproxyauth_given = 1; args_info->remproxyauth_arg = gengetopt_strdup (optarg); break; case 'X': /* Turn on local to remote proxy SSL encryption */ args_info->encryptremproxy_flag = !(args_info->encryptremproxy_flag); if( args_info->verbose_flag ) message("SSL local to remote proxy enabled\n"); break; case 'd': /* Destination host to built the tunnel to. */ if (args_info->dest_given) { fprintf (stderr, "%s: `--dest' (`-d') option given more than once\n", PACKAGE); clear_args (); exit(1); } args_info->dest_given = 1; args_info->dest_arg = gengetopt_strdup (optarg); break; case 'H': /* Extra headers to send to HTTPS proxy. */ args_info->header_given++; strzcat( args_info->header_arg, "%s\r\n", optarg); break; case 'v': /* Turn on verbosity. */ if (args_info->quiet_flag) { /* -q also on cmd line */ fprintf (stderr, "-v and -q are mutually exclusive\n"); clear_args(); exit(1); } args_info->verbose_flag = !(args_info->verbose_flag); break; case 'N': /* Turn on NTLM. */ args_info->ntlm_flag = !(args_info->ntlm_flag); break; case 'q': /* Suppress messages -- Quiet mode */ args_info->quiet_flag = !(args_info->quiet_flag); break; case 0: /* Long option with no short option */ case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ clear_args(); exit(1); default: /* bug: option not considered. */ fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c); clear_args(); abort(); } /* switch */ } /* while */ /* For Windows quiet is the default output. -- Dag */ #ifdef CYGWIN if (! args_info->verbose_flag ) { args_info->quiet_flag = 1; } #endif /* Get credentials from environment. -- Dag */ char *tmp = NULL; if ( args_info->user_arg == NULL ) { if ( (tmp = getenv("PROXYUSER")) != NULL) { args_info->user_given = 1; args_info->user_arg = gengetopt_strdup (tmp); if( args_info->verbose_flag ) message( "Found user '%s' in env variable PROXYUSER.\n", args_info->user_arg); } } if ( args_info->pass_arg == NULL ) { if ( (tmp = getenv("PROXYPASS")) != NULL ) { args_info->pass_given = 1; args_info->pass_arg = gengetopt_strdup (tmp); if( args_info->verbose_flag ) message( "Found password in env variable PROXYPASS.\n", args_info->pass_arg); } } if ( args_info->remuser_arg == NULL ) { if ( (tmp = getenv("REMPROXYUSER")) != NULL ) { args_info->remuser_given = 1; args_info->user_arg = gengetopt_strdup (tmp); if( args_info->verbose_flag ) message( "Found remote user '%s' in env variable REMPROXYPASS.\n", args_info->remuser_arg); } } if ( args_info->rempass_arg == NULL ) { if ( (tmp = getenv("REMPROXYPASS")) != NULL ) { args_info->rempass_given = 1; args_info->user_arg = gengetopt_strdup (tmp); if( args_info->verbose_flag ) message( "Found remote password in env variable REMPROXYPASS.\n" ); } } if ( args_info->proxy_arg == NULL ) { if ( ((tmp = getenv("http_proxy")) != NULL) || ((tmp = getenv("HTTP_PROXY")) != NULL) ) { int r; char * temp; temp = malloc( 56+1 ); r = sscanf( tmp, "http://%56[^/]/", temp ); // message( "r = '%d'\ntemp = '%s'\n", r, temp); args_info->proxy_given = 1; args_info->proxy_arg = gengetopt_strdup (temp); if( args_info->verbose_flag ) message( "Proxy info found using env variable HTTP_PROXY (%s).\n", args_info->proxy_arg); } } if (! args_info->proxy_given && ! args_info->dest_given ) { clear_args (); // cmdline_parser_print_help (); message( "No proxy or destination given, exiting\nUse '--help' flag for usage info\n" ); exit(1); } if (args_info->proxy_given ) { char * phost; int pport; phost = malloc( 50+1 ); r = sscanf( args_info->proxy_arg, "%50[^:]:%5u", phost, &pport ); if ( r == 2 ) { args_info->proxyhost_arg = phost; args_info->proxyport_arg = pport; args_info->proxyhost_given = 1; args_info->proxyport_given = 1; } else { message( "parse_cmdline: could not find your proxy hostname/ip (%s)\n", args_info->proxy_arg ); missing_required_options++; } } /* Parse -P/--proxyauth information */ if (args_info->proxyauth_given ) { char *puser = NULL; char *ppass = NULL; puser = malloc( 24+1 ); ppass = malloc( 24+1 ); r = sscanf( args_info->proxyauth_arg, "%24[^:]:%24s", puser, ppass ); if ( r == 2 ) { args_info->user_arg = puser; args_info->pass_arg = ppass; args_info->user_given = 1; args_info->pass_given = 1; } else if ( r == 1 ) { args_info->user_arg = args_info->proxyauth_arg; args_info->user_given = 1; } else { message( "parse_cmdline: could not find your proxy auth user/pass\n" ); missing_required_options++; } } /* Parse -R/--remproxyauth information */ if (args_info->remproxyauth_given ) { char *ruser = NULL; char *rpass = NULL; ruser = malloc( 24+1 ); rpass = malloc( 24+1 ); r = sscanf( args_info->remproxyauth_arg, "%24[^:]:%24s", ruser, rpass ); if ( r == 2 ) { args_info->remuser_arg = ruser; args_info->rempass_arg = rpass; args_info->remuser_given = 1; args_info->rempass_given = 1; } else if ( r == 1 ) { args_info->remuser_arg = args_info->remproxyauth_arg; args_info->remuser_given = 1; } else { message( "parse_cmdline: could not find your proxy auth user/pass\n" ); missing_required_options++; } } if ( missing_required_options ) exit(1); return 0; }
int cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) { int c; /* Character of the parsed option. */ int missing_required_options = 0; args_info->help_given = 0 ; args_info->version_given = 0 ; args_info->from_given = 0 ; args_info->to_given = 0 ; args_info->output_given = 0 ; args_info->algorithm_given = 0 ; args_info->char_given = 0 ; args_info->sort_given = 0 ; args_info->time_given = 0 ; args_info->freq_given = 0 ; args_info->m1_given = 0 ; #define clear_args() { \ args_info->from_arg = gengetopt_strdup("UTF-8") ;\ args_info->to_arg = gengetopt_strdup("UTF-8") ;\ args_info->output_arg = NULL; \ args_info->algorithm_arg = 2 ;\ args_info->char_flag = 0;\ args_info->sort_flag = 0;\ args_info->time_flag = 0;\ args_info->freq_arg = 1 ;\ args_info->m1_arg = 1 ;\ } clear_args(); args_info->inputs = NULL; args_info->inputs_num = 0; optarg = 0; optind = 1; opterr = 1; optopt = '?'; while (1) { int option_index = 0; char *stop_char; static struct option long_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { "from", 1, NULL, 'F' }, { "to", 1, NULL, 'T' }, { "output", 1, NULL, 'o' }, { "algorithm", 1, NULL, 'a' }, { "char", 0, NULL, 'c' }, { "sort", 0, NULL, 's' }, { "time", 0, NULL, 't' }, { "freq", 1, NULL, 'f' }, { "m1", 1, NULL, 'm' }, { NULL, 0, NULL, 0 } }; stop_char = 0; c = getopt_long (argc, argv, "hVF:T:o:a:cstf:m:", long_options, &option_index); if (c == -1) break; /* Exit from `while (1)' loop. */ switch (c) { case 'h': /* Print help and exit. */ clear_args (); cmdline_parser_print_help (); exit (EXIT_SUCCESS); case 'V': /* Print version and exit. */ clear_args (); cmdline_parser_print_version (); exit (EXIT_SUCCESS); case 'F': /* input stream encoding (for character ngram only). */ if (args_info->from_given) { fprintf (stderr, "%s: `--from' (`-F') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->from_given = 1; args_info->from_arg = gengetopt_strdup (optarg); break; case 'T': /* output stream encoding (for character ngram only). */ if (args_info->to_given) { fprintf (stderr, "%s: `--to' (`-T') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->to_given = 1; args_info->to_arg = gengetopt_strdup (optarg); break; case 'o': /* write output to file,use stdout if omitted. */ if (args_info->output_given) { fprintf (stderr, "%s: `--output' (`-o') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->output_given = 1; args_info->output_arg = gengetopt_strdup (optarg); break; case 'a': /* using Nth reduction algorithm. */ if (args_info->algorithm_given) { fprintf (stderr, "%s: `--algorithm' (`-a') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->algorithm_given = 1; args_info->algorithm_arg = strtol (optarg,&stop_char,0); break; case 'c': /* enter char gram mode, default is word ngram model. */ if (args_info->char_given) { fprintf (stderr, "%s: `--char' (`-c') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->char_given = 1; args_info->char_flag = !(args_info->char_flag); break; case 's': /* sort result. */ if (args_info->sort_given) { fprintf (stderr, "%s: `--sort' (`-s') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->sort_given = 1; args_info->sort_flag = !(args_info->sort_flag); break; case 't': /* show time cost by reduction algorithm on stderr (not including I/O). */ if (args_info->time_given) { fprintf (stderr, "%s: `--time' (`-t') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->time_given = 1; args_info->time_flag = !(args_info->time_flag); break; case 'f': /* frequence threshold needed in algorithm 1,2,4. */ if (args_info->freq_given) { fprintf (stderr, "%s: `--freq' (`-f') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->freq_given = 1; args_info->freq_arg = strtol (optarg,&stop_char,0); break; case 'm': /* minimum n-gram need in algorithm 4. */ if (args_info->m1_given) { fprintf (stderr, "%s: `--m1' (`-m') option given more than once\n", CMDLINE_PARSER_PACKAGE); clear_args (); exit (EXIT_FAILURE); } args_info->m1_given = 1; args_info->m1_arg = strtol (optarg,&stop_char,0); break; case 0: /* Long option with no short option */ case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ exit (EXIT_FAILURE); default: /* bug: option not considered. */ fprintf (stderr, "%s: option unknown: %c\n", CMDLINE_PARSER_PACKAGE, c); abort (); } /* switch */ } /* while */ if ( missing_required_options ) exit (EXIT_FAILURE); if (optind < argc) { int i = 0 ; args_info->inputs_num = argc - optind ; args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; while (optind < argc) args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ; } return 0; }