static void parse_script_options(struct config *config, struct option_list *option_list) { struct option_list *opt = option_list; while (opt != NULL) { int i; int c = 0; for (i = 0; options[i].name != NULL; i++) { if (strcmp(options[i].name, opt->name) == 0) { c = options[i].val; break; } } if (c != 0) { process_option(options[i].val, opt->value, config, config->script_path); } else { die("%s: option '%s' unknown in file: %s\n", config->script_path, opt->name, config->script_path); } opt = opt->next; } }
static int process(int argc, char *argv[]) { int arg; arg = 1; while(arg < argc) { if(argv[arg][0] == '-') { if(process_option(argv[arg]) < 0) { fprintf(stderr, "invalid option %s\n", argv[arg]); return -EINVAL; } arg++; continue; } /* found a filename */ break; } if(arg >= argc) { fprintf(stderr, "no files to encode\n"); return -EINVAL; } while(arg < argc) { /* ordinary image */ encode(argv[arg]); arg++; } return 0; }
int cfg_parse_enum(void* param, cfg_parser_t* st, unsigned int flags) { int ret; cfg_token_t t; cfg_option_t* values, *val; values = (cfg_option_t*)param; ret = cfg_get_token(&t, st, flags); if (ret != 0) return ret; if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) { ERR("%s:%d:%d: Invalid enum value '%.*s'\n", st->file, t.start.line, t.start.col, STR_FMT(&t.val)); return -1; } if (values) { if ((val = cfg_lookup_token(values, &t.val)) == NULL) { ERR("%s:%d:%d Unsupported enum value '%.*s'\n", st->file, t.start.line, t.start.col, STR_FMT(&t.val)); return -1; } return process_option(st, val); } else { return 0; } }
int main() { int n = 0; do { print_menu(n);; } while(process_option(get_option(), &n) != 0); return EXIT_SUCCESS; }
/** * Process the command line arguments, splitting them into options, the * target action, and any arguments to that action. The results are pushed * into the session for later use. I could have done this in the scripts, * but I need the value of the /scripts option to find them. * \returns OKAY if successful. */ int process_arguments(lua_State* L, int argc, const char** argv) { int i; /* Create empty lists for Options and Args */ lua_newtable(L); lua_newtable(L); for (i = 1; i < argc; ++i) { /* Options start with '/' or '--'. The first argument that isn't an option * is the action. Anything after that is an argument to the action */ if (argv[i][0] == '/') { process_option(L, argv[i] + 1); } else if (argv[i][0] == '-' && argv[i][1] == '-') { process_option(L, argv[i] + 2); } else { /* not an option, is the action */ lua_pushstring(L, argv[i++]); lua_setglobal(L, "_ACTION"); /* everything else is an argument */ while (i < argc) { lua_pushstring(L, argv[i++]); lua_rawseti(L, -2, luaL_getn(L, -2) + 1); } } } /* push the Options and Args lists */ lua_setglobal(L, "_ARGS"); lua_setglobal(L, "_OPTIONS"); return OKAY; }
void impl::app::process_options(void) { assert(inited()); std::string optstr; #if defined(HAVE_GNU_GETOPT) optstr += '+'; // Turn on POSIX behavior. #endif optstr += ':'; { options_set opts = options(); for (options_set::const_iterator iter = opts.begin(); iter != opts.end(); iter++) { const option& opt = (*iter); optstr += opt.m_character; if (!opt.m_argument.empty()) optstr += ':'; } } int ch; const int old_opterr = ::opterr; ::opterr = 0; while ((ch = ::getopt(m_argc, m_argv, optstr.c_str())) != -1) { switch (ch) { case 'h': m_hflag = true; break; case ':': throw usage_error("Option -%c requires an argument.", ::optopt); case '?': throw usage_error("Unknown option -%c.", ::optopt); default: process_option(ch, ::optarg); } } m_argc -= ::optind; m_argv += ::optind; // Clear getopt state just in case the test wants to use it. opterr = old_opterr; optind = 1; #if defined(HAVE_OPTRESET) optreset = 1; #endif }
int main(int argc, char **argv) { int ret = 0; if (argc==1){ print_help(); return 0; } ret = c_rpc_qcsapi_init(); if (ret < 0) { return -1; } process_option(argc, argv); return 0; }
int sr_cfg_parse(cfg_parser_t* st) { int ret; cfg_token_t t; cfg_option_t* opt; while(1) { ret = cfg_get_token(&t, st, 0); if (ret < 0) return ret; if (ret > 0) break; switch(t.type) { case CFG_TOKEN_ALPHA: /* Lookup the option name */ if ((opt = cfg_lookup_token(st->options, &t.val)) == NULL) { ERR("%s:%d:%d: Unsupported option '%.*s'\n", st->file, t.start.line, t.start.col, STR_FMT(&t.val)); return -1; } st->cur_opt = &t; if (process_option(st, opt) < 0) { st->cur_opt = 0; return -1; } st->cur_opt = 0; break; case '[': if (st->section.parser == NULL) { ERR("%s:%d:%d: Syntax error\n", st->file, t.start.line, t.start.col); return -1; } if (st->section.parser(st->section.param, st, 0) < 0) return -1; break; /* Empty line */ case '\n': continue; default: ERR("%s:%d:%d: Syntax error\n", st->file, t.start.line, t.start.col); return -1; } } return 0; }
int set_options(const char args[]) { int err = 0; if(*args == '\0') { print_changed_options(); return 0; } while(*args != '\0') { char buf[1024]; args = extract_option(args, buf, 1); if(args == NULL) return -1; err += process_option(buf); } return err; }
static void process_arg (char *s) { switch (s[0]) { case '-': /* normal option switch */ process_option (s+1); break; // case '@': /* resume file name */ // strcpy (resume_file, s+1); break; case '&': /* more switches from environment */ process_env_string (s+1); break; case '#': /* jump to line number */ if (active_view) view_gotoline (atoi(s+1)-1); else { redbox (redbox_locus, 200, eh_continue); } break; default: /* assume it is a file to load */ DBG_MEM(memout<<" file " <<__FILE__<<" line "<<__LINE__<<checkmem2<<endl); locate_view (s, 1); /* load if not found */ DBG_MEM(memout<<" file " <<__FILE__<<" line "<<__LINE__<<checkmem2<<endl); break; } }
void ProgOptions::parseCommandLine( int argc, char* argv[] ){ const char* name = strrchr(argv[0],'/'); if (name) this->progname = ++name; else this->progname = argv[0]; std::vector<const char*> args; std::list<ProgOpt*> expected_vals; bool no_more_flags = false; // Loop over all command line arguments for( int i = 1; i < argc; ++i ) { std::string arg(argv[i]); if (arg.empty()) continue; if (!expected_vals.empty()) { ProgOpt* opt = expected_vals.front(); expected_vals.pop_front(); assert(opt->type != FLAG); opt->args.push_back( arg ); evaluate( *opt, opt->storage, arg ); } else if (!no_more_flags && arg[0] == '-') { if (arg.length() > 2 && arg[1] == '-') { // long opt size_t eq = arg.find_first_of('='); if (eq != std::string::npos) { ProgOpt* opt = lookup( long_names, arg.substr( 2, eq-2 ) ); process_option( opt, arg, arg.substr( eq+1 ).c_str() ); } else { ProgOpt* opt = lookup( long_names, arg.substr( 2 ) ); if (process_option( opt, arg )) expected_vals.push_back( opt ); } } else if (arg == "--") { // -- no_more_flags = true; } else for (size_t f = 1; f < arg.length(); ++f) { // for each short opt ProgOpt* opt = lookup( short_names, std::string(1,arg[f]) ); if (opt->flags & int_flag) { const char val[] = { arg[f], 0 }; process_option( opt, std::string(1,arg[f]), val ); } else if (process_option( opt, std::string(1,arg[f]) )) expected_vals.push_back( opt ); } } else{ /* arguments */ args.push_back(argv[i]); } }/* End loop over inputs */ // Print error if any missing values if (!expected_vals.empty()) { error( "Missing value for option: -" + expected_vals.front()->shortname + ",--" + expected_vals.front()->longname ); } // Process non-option arguments std::vector<help_line>::iterator arg_help_pos = arg_help_strings.begin(); std::vector<const char*>::iterator arg_val_pos = args.begin(); std::vector<help_line>::iterator opt_args_pos = arg_help_strings.end(); size_t min_required_args = required_args.size(); size_t max_required_args = required_args.size(); if (expect_optional_args) { min_required_args--; if (max_optional_args) max_required_args += max_optional_args; else max_required_args = std::numeric_limits<int>::max(); opt_args_pos = arg_help_pos + optional_args_position; } // check valid number of non-flag arguments if (args.size() < min_required_args) { size_t missing_pos = args.size(); if (expect_optional_args && missing_pos >= optional_args_position) ++missing_pos; const std::string& missed_arg = arg_help_strings[missing_pos].first->longname; error("Did not find required positional argument: " + missed_arg ); } else if (args.size() > max_required_args) { error( "Unexpected argument: " + std::string(args[max_required_args]) ); } // proccess arguments up to the first optional argument // (or all arguments if no optional args) while (arg_help_pos != opt_args_pos) { ProgOpt* opt = arg_help_pos->first; ++arg_help_pos; opt->args.push_back( *arg_val_pos ); evaluate( *opt, opt->storage, *arg_val_pos ); ++arg_val_pos; } // process any optional args if (arg_help_pos != arg_help_strings.end()) { assert( arg_help_pos == opt_args_pos ); size_t num_opt_args = args.size() + 1 - required_args.size(); ProgOpt* opt = arg_help_pos->first; ++arg_help_pos; while (num_opt_args--) { opt->args.push_back( *arg_val_pos ); evaluate( *opt, opt->storage, *arg_val_pos ); ++arg_val_pos; } } // process any remaining args while (arg_help_pos != arg_help_strings.end()) { assert(arg_val_pos != args.end()); ProgOpt* opt = arg_help_pos->first; ++arg_help_pos; opt->args.push_back( *arg_val_pos ); evaluate( *opt, opt->storage, *arg_val_pos ); ++arg_val_pos; } assert(arg_val_pos == args.end()); }
int main(int argc, char *argv[]) { static const struct option long_opts[] = { /* commands */ {"inject", required_argument, NULL, c_INJECT}, {"remove", required_argument, NULL, c_REMOVE}, {"hexdump", required_argument, NULL, c_HEXDUMP}, /* options */ {"output", required_argument, NULL, o_OUTPUT}, /* flags */ {"force", no_argument, NULL, f_FORCE}, {"p8", no_argument, NULL, f_P8}, {"verbose", no_argument, NULL, f_VERBOSE}, {"help", no_argument, NULL, f_HELP}, {0, 0, 0, 0} }; static const char *short_opts = "I:R:H:o:fpvh"; int rc = EXIT_FAILURE; if (argc == 1) usage(args.short_name, false), exit(rc); int opt = 0, idx = 0; while ((opt = getopt_long(argc, argv, short_opts, long_opts, &idx)) != -1) if (process_argument(&args, opt, optarg) < 0) goto error; /* getopt_long doesn't know what to do with orphans, */ /* so we'll scoop them up here, and deal with them later */ while (optind < argc) if (process_option(&args, argv[optind++]) < 0) goto error; if (args.verbose == f_VERBOSE) args_dump(&args); if (validate_args(&args) < 0) goto error; if (process_args(&args) < 0) goto error; rc = EXIT_SUCCESS; if (false) { err_t *err; error: err = err_get(); assert(err != NULL); fprintf(stderr, "%s: %s : %s(%d) : (code=%d) %.*s\n", program_invocation_short_name, err_type_name(err), err_file(err), err_line(err), err_code(err), err_size(err), (char *)err_data(err)); } return rc; }
/* process a command line, internal version that tracks `-config' depth */ static void __opt_process_options(struct opt_odb_t *odb, /* option data base */ int argc, /* number of arguments */ char **argv, /* argument array */ int depth) /* `-config' option depth */ { int index, do_dumpconfig; char *dumpconfig_name; index = 0; do_dumpconfig = FALSE; dumpconfig_name = NULL; /* visit all command line arguments */ while (index < argc) { /* process any encountered orphans */ while (index < argc && argv[index][0] != '-') { if (depth > 0) { /* orphans are not allowed during config file processing */ fatal("orphan `%s' encountered during config file processing", argv[index]); } /* else, call the user-stalled orphan handler */ if (odb->orphan_fn) { if (!odb->orphan_fn(index+1, argc, argv)) { /* done processing command line */ goto done_processing; } } else { /* no one claimed this option */ fatal("orphan argument `%s' was unclaimed", argv[index]); } /* go to next option */ } /* done with command line? */ if (index == argc) { /* done processing command line */ goto done_processing; } /* when finished, argv[index] is an option to parse */ /* process builtin options */ if (!strcmp(argv[index], "-config")) { /* handle `-config' builtin option */ index++; if (index >= argc || argv[index][0] == '-') { /* no arguments available */ fatal("option `-config' requires an argument"); } process_file(odb, argv[index], depth); index++; } else if (!strcmp(argv[index], "-dumpconfig")) { /* this is performed *last* */ do_dumpconfig = TRUE; /* handle `-dumpconfig' builtin option */ index++; if (index >= argc || (argv[index][0] == '-' && argv[index][1] != '\0')) { /* no arguments available */ fatal("option `-dumpconfig' requires an argument"); } dumpconfig_name = argv[index]; index++; } else { /* process user-installed option */ index = process_option(odb, index, argc, argv); } } done_processing: if (do_dumpconfig) dump_config(odb, dumpconfig_name); }
/** * trace_util_add_option - add an option/val pair to set plugin options * @name: The name of the option (format: <plugin>:<option> or just <option>) * @val: (optiona) the value for the option * * Modify a plugin option. If @val is given than the value of the option * is set (note, some options just take a boolean, so @val must be either * "1" or "0" or "true" or "false"). */ int trace_util_add_option(const char *name, const char *val) { struct trace_plugin_options *op; char *option_str; char *plugin; option_str = strdup(name); if (!option_str) return -ENOMEM; parse_option_name(&option_str, &plugin); /* If the option exists, update the val */ for (op = trace_plugin_options; op; op = op->next) { /* Both must be NULL or not NULL */ if ((!plugin || !op->plugin) && plugin != op->plugin) continue; if (plugin && strcmp(plugin, op->plugin) != 0) continue; if (strcmp(op->option, option_str) != 0) continue; /* update option */ free(op->value); if (val) { op->value = strdup(val); if (!op->value) goto out_free; } else op->value = NULL; /* plugin and option_str don't get freed at the end */ free(plugin); free(option_str); plugin = op->plugin; option_str = op->option; break; } /* If not found, create */ if (!op) { op = malloc(sizeof(*op)); if (!op) return -ENOMEM; memset(op, 0, sizeof(*op)); op->next = trace_plugin_options; trace_plugin_options = op; op->plugin = plugin; op->option = option_str; if (val) { op->value = strdup(val); if (!op->value) goto out_free; } } return process_option(plugin, option_str, val); out_free: free(option_str); return -ENOMEM; }
/* * This program is used to generate and display WAN boot encryption and * hash keys. The paths to the keystores are predetermined. That is, the * master keystore (used to store a master HMAC SHA1 key) will always * reside in the default location, MASTER_KEY_FILE. The client keystores * will always reside in default locations that are computed using their * network number and cid values. * * Note: * The master keystore can store client keys too. This program * cannot be used to insert client keys into the master keystore. * However, it must not corrupt any client keystore inserted into * the file by other means (keymgmt). * * We do not do any file locking scheme. This means that if two * keygen commands are run concurrently, results can be disastrous. * * Returns: * KEYGEN_SUCCESS or KEYGEN_ERROR. */ int main(int argc, char **argv) { char filename[PATH_MAX]; char *filenamep; int c; boolean_t is_client = B_FALSE; boolean_t is_master = B_FALSE; boolean_t display = B_FALSE; char *net = NULL; char *cid = NULL; wbku_key_attr_t ka; wbku_retcode_t ret; /* * Do the necessary magic for localization support. */ (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) #define TEXT_DOMAIN "SYS_TEST" #endif (void) textdomain(TEXT_DOMAIN); /* * Initialize program name for use by wbku_printerr(). */ wbku_errinit(argv[0]); /* * At the very least, we'll need one arg. */ if (argc < 2) { usage(argv[0]); return (KEYGEN_ERROR); } /* * Parse the options. */ ka.ka_type = WBKU_KEY_UNKNOWN; while ((c = getopt(argc, argv, "dcmo:")) != EOF) { switch (c) { case 'd': /* * Display a key. */ display = B_TRUE; break; case 'o': /* * Suboptions. */ if (process_option(optarg, &net, &cid, &ka) != 0) { usage(argv[0]); return (KEYGEN_ERROR); } break; case 'c': is_client = B_TRUE; break; case 'm': is_master = B_TRUE; break; default: usage(argv[0]); return (KEYGEN_ERROR); } } /* * Must be operating on a master or client key and if * it's a client key, then type must have been given. */ if ((is_client == is_master) || (is_client && ka.ka_type == WBKU_KEY_UNKNOWN)) { usage(argv[0]); return (KEYGEN_ERROR); } /* * If operating on the master key, then it is an HMAC SHA1 * key. Build the correct 'ka'. If we're working on a client * key, the 'ka' was already built as part of option parsing. */ if (is_master) { ret = wbku_str_to_keyattr(WBKU_KW_HMAC_SHA1, &ka, WBKU_HASH_KEY); if (ret != WBKU_SUCCESS) { wbku_printerr("Internal error\n"); return (KEYGEN_ERROR); } filenamep = MASTER_KEY_FILE; } else { /* * Build the path to the client keystore. */ if (create_client_filename(filename, sizeof (filename), net, cid, !display) != KEYGEN_SUCCESS) { return (KEYGEN_ERROR); } filenamep = filename; } /* * If display chosen, go do it. */ if (display) { return (display_key(filenamep, &ka, is_master)); } /* * Can't generate RSA key here. */ if (ka.ka_type == WBKU_KEY_RSA) { wbku_printerr("keygen cannot create RSA key\n"); return (KEYGEN_ERROR); } /* * If generating a master key, go do it. */ if (is_master) { return (master_gen_key(&ka)); } /* * Must be generating a client key, go do it. */ if (net == NULL) { net = default_net; } if (cid == NULL) { cid = default_cid; } if (client_gen_key(filename, &ka, net, cid) != KEYGEN_SUCCESS) { return (KEYGEN_ERROR); } return (KEYGEN_SUCCESS); }
static void process_client(const char *node, const char *port, int fd) { char **temp_files; char buf[BUFSIZ]; char *option; int *port_array; int *pid_array; int pagesize; int start_port; int udp_port; int options; int size; int cpus; int cpu; int pid; int ofd; int n, s, t, i; /* Let the client know what we are */ write(fd, "tracecmd", 8); /* read back the CPU count */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; cpus = atoi(buf); plog("cpus=%d\n", cpus); if (cpus < 0) return; /* next read the page size */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; pagesize = atoi(buf); plog("pagesize=%d\n", pagesize); if (pagesize <= 0) return; /* Now the number of options */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; options = atoi(buf); for (i = 0; i < options; i++) { /* next is the size of the options */ n = read_string(fd, buf, BUFSIZ); if (n == BUFSIZ) /** ERROR **/ return; size = atoi(buf); /* prevent a client from killing us */ if (size > MAX_OPTION_SIZE) return; option = malloc_or_die(size); do { t = size; s = 0; s = read(fd, option+s, t); if (s <= 0) return; t -= s; s = size - t; } while (t); s = process_option(option); free(option); /* do we understand this option? */ if (!s) return; } if (use_tcp) plog("Using TCP for live connection\n"); /* Create the client file */ snprintf(buf, BUFSIZ, "%s.%s:%s.dat", output_file, node, port); ofd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644); if (ofd < 0) pdie("Can not create file %s", buf); port_array = malloc_or_die(sizeof(int) * cpus); pid_array = malloc_or_die(sizeof(int) * cpus); memset(pid_array, 0, sizeof(int) * cpus); start_port = START_PORT_SEARCH; /* Now create a UDP port for each CPU */ for (cpu = 0; cpu < cpus; cpu++) { udp_port = open_udp(node, port, &pid, cpu, pagesize, start_port); if (udp_port < 0) goto out_free; port_array[cpu] = udp_port; pid_array[cpu] = pid; /* due to some bugging finding ports, force search after last port */ start_port = udp_port+1; } /* send the client a comma deliminated set of port numbers */ for (cpu = 0; cpu < cpus; cpu++) { snprintf(buf, BUFSIZ, "%s%d", cpu ? "," : "", port_array[cpu]); write(fd, buf, strlen(buf)); } /* end with null terminator */ write(fd, "\0", 1); /* Now we are ready to start reading data from the client */ do { n = read(fd, buf, BUFSIZ); if (n < 0) { if (errno == EINTR) continue; pdie("reading client"); } t = n; s = 0; do { s = write(ofd, buf+s, t); if (s < 0) { if (errno == EINTR) break; pdie("writing to file"); } t -= s; s = n - t; } while (t); } while (n > 0 && !done); /* wait a little to let our readers finish reading */ sleep(1); /* stop our readers */ for (cpu = 0; cpu < cpus; cpu++) { if (pid_array[cpu] > 0) kill(pid_array[cpu], SIGUSR1); } /* wait a little to have the readers clean up */ sleep(1); /* Now put together the file */ temp_files = malloc_or_die(sizeof(*temp_files) * cpus); for (cpu = 0; cpu < cpus; cpu++) temp_files[cpu] = get_temp_file(node, port, cpu); tracecmd_attach_cpu_data_fd(ofd, cpus, temp_files); out_free: for (cpu = 0; cpu < cpus; cpu++) { if (pid_array[cpu] > 0) { kill(pid_array[cpu], SIGKILL); delete_temp_file(node, port, cpu); pid_array[cpu] = 0; } } }