char * Clp_Shift(Clp_Parser *clp, int allow_dashes) /* Returns the next argument from the argument list without parsing it. If there are no more arguments, returns 0. */ { Clp_ParserState clpsave; Clp_SaveParser(clp, &clpsave); next_argument(clp, allow_dashes ? 2 : 1); if (!clp->have_arg) Clp_RestoreParser(clp, &clpsave); return clp->arg; }
status_t __parse_invoke_line(char *invoker, char ***_newArgs, char * const **_oldArgs, int32 *_argCount, const char *arg0) { int32 i, count = 0; char *arg = invoker; char **newArgs; // count arguments in the line while (next_argument(&arg, false)) { count++; } // this is a shell script and requires special treatment newArgs = (char**)malloc((*_argCount + count + 1) * sizeof(void *)); if (newArgs == NULL) return B_NO_MEMORY; // copy invoker and old arguments and to newArgs for (i = 0; (arg = next_argument(&invoker, true)) != NULL; i++) { newArgs[i] = arg; } for (i = 0; i < *_argCount; i++) { if (i == 0) newArgs[i + count] = (char*)arg0; else newArgs[i + count] = (char *)(*_oldArgs)[i]; } newArgs[i + count] = NULL; *_newArgs = newArgs; *_oldArgs = (char * const *)newArgs; *_argCount += count; return B_OK; }
Environment::FunInfoPtr Environment::create_fun(Type* ret_type, ListArg* args) { Environment::FunInfoPtr new_function(new Environment::fun_info); new_function->ret_type = ret_type; new_function->is_extern = false; new_function->position = -1; for(ListArg::iterator it = args->begin(); it != args->end();it++){ Environment::VarInfoPtr next_argument(new Environment::var_info); Argument *argument = dynamic_cast<Argument*>(*it); if(argument == 0) { if (debug) std::cerr << "Cannot cast Arg to Argument. Did you changed grammar?"; throw "Did you changed grammar?!"; } next_argument->type = argument->type_; new_function->arguments.push_back(next_argument); } return new_function; }
static int process_line() { const char *config_segment = NULL; char *shell = NULL; if (current_argument(&config_segment) < 0 || !config_segment) return 0; if (strcmp(config_segment, "execute") == 0) { if (next_argument(&config_segment) < 0) return -1; shell = strdup(config_segment); int outcome = add_execute_respawn(shell); free(shell); if (outcome < 0) return -1; } else if (strcmp(config_segment, "execute_critical") == 0) { if (next_argument(&config_segment) < 0) return -1; shell = strdup(config_segment); int outcome = add_execute_critical_respawn(shell); free(shell); if (outcome < 0) return -1; } else if (strcmp(config_segment, "system") == 0) { if (next_argument(&config_segment) < 0) return -1; shell = strdup(config_segment); int outcome = add_system_respawn(shell); free(shell); if (outcome < 0) return -1; } else if (strcmp(config_segment, "system_critical") == 0) { if (next_argument(&config_segment) < 0) return -1; shell = strdup(config_segment); int outcome = add_system_critical_respawn(shell); free(shell); if (outcome < 0) return -1; } else if (strcmp(config_segment, "limit") == 0) { if (remaining_line(&config_segment) < 0 || !config_segment) return -1; int new_limit = 0; if (!parse_integer10(config_segment, &new_limit) || new_limit < 0) return -1; set_limit(new_limit); } else if (strcmp(config_segment, "security") == 0) { if (remaining_line(&config_segment) < 0 || !config_segment) return -1; permission_mask new_permissions = 0; if (!parse_permissions(config_segment, &new_permissions)) return -1; set_permissions(new_permissions); } else if (strcmp(config_segment, "priority") == 0) { if (remaining_line(&config_segment) < 0 || !config_segment) return -1; int new_priority = 0; if ( !parse_integer10(config_segment, &new_priority) || new_priority < 0 || new_priority > 255 ) return -1; set_priority(new_priority); } else if (strcmp(config_segment, "log_mode") == 0) { if (remaining_line(&config_segment) < 0 || !config_segment) return -1; logging_mode mode_mask = logging_none; if (!parse_logging_mode(config_segment, &mode_mask)) return -1; set_logging_mode(mode_mask); } else return -1; return 0; }
int Clp_Next(Clp_Parser *clp) /* Gets and parses the next argument from the argument list. If there are no more arguments, returns Clp_Done. If the next argument isn't an option, returns Clp_NotOption; the argument is stored in clp->arg. If the next argument is an option, returns that option's option_id. If the next argument is an unrecognizable or ambiguous option, an error message is given and Clp_BadOption is returned. If an option has an argument, that argument is stored in clp->arg and clp->have_arg is set to 1. Furthermore, that argument's parsed value (according to its type) is stored in the clp->val union. If an option needs an argument but isn't given one; if it doesn't need an argument but IS given one; or if the argument is the wrong type, an error message is given and Clp_BadOption is returned. */ { Clp_Internal *cli = clp->internal; Clp_Option *opt; Clp_ParserState clpsave; int complain; /** Set up clp **/ cli->current_option = 0; cli->ambiguous = 0; /** Get the next argument or option **/ if (!next_argument(clp, cli->option_processing ? 0 : 2)) return clp->have_arg ? Clp_NotOption : Clp_Done; clp->negated = cli->whole_negated; if (cli->is_short) opt = find_short(clp, cli->text[0]); else opt = find_long(clp, cli->text); /** If there's ambiguity between long & short options, and we couldn't find a long option, look for a short option **/ if (!opt && cli->could_be_short) { switch_to_short_argument(clp); opt = find_short(clp, cli->text[0]); } /** If we didn't find an option... **/ if (!opt || (clp->negated && !TEST(opt, Clp_Negate))) { /* default processing for the "--" option: turn off option processing and return the next argument */ if (strcmp(cli->argv[0], "--") == 0) { Clp_SetOptionProcessing(clp, 0); return Clp_Next(clp); } /* otherwise, report some error or other */ if (cli->ambiguous) ambiguity_error(clp, cli->ambiguous, cli->ambiguous_values, cli->opt, cli->option_chars, "option `%s%s' is ambiguous", cli->option_chars, cli->text); else if (cli->is_short && !cli->could_be_short) Clp_OptionError(clp, "unrecognized option `%s%c'", cli->option_chars, cli->text[0]); else Clp_OptionError(clp, "unrecognized option `%s%s'", cli->option_chars, cli->text); return Clp_BadOption; } /** Set the current option **/ cli->current_option = opt; cli->current_short = cli->is_short; cli->negated_by_no = clp->negated && !cli->whole_negated; /** The no-argument (or should-have-no-argument) case **/ if (clp->negated || !TEST(opt, Clp_AnyArgument)) { if (clp->have_arg) { Clp_OptionError(clp, "`%O' can't take an argument"); return Clp_BadOption; } else return opt->option_id; } /** Get an argument if we need one, or if it's optional **/ /* Sanity-check the argument type. */ if (opt->arg_type <= 0 || opt->arg_type >= cli->nargtype || cli->argtype[ opt->arg_type ].func == 0) return Clp_Error; /* complain == 1 only if the argument was explicitly given, or it is mandatory. */ complain = (clp->have_arg != 0) || TEST(opt, Clp_Mandatory); Clp_SaveParser(clp, &clpsave); if (TEST(opt, Clp_Mandatory) && !clp->have_arg) { /* Mandatory argument case */ /* Allow arguments to options to start with a dash, but only if the argument type allows it by not setting Clp_DisallowOptions */ int disallow = TEST(&cli->argtype[opt->arg_type], Clp_DisallowOptions); next_argument(clp, disallow ? 1 : 2); if (!clp->have_arg) { int got_option = cli->text != 0; Clp_RestoreParser(clp, &clpsave); if (got_option) Clp_OptionError(clp, "`%O' requires a non-option argument"); else Clp_OptionError(clp, "`%O' requires an argument"); return Clp_BadOption; } } else if (cli->is_short && !clp->have_arg && cli->text[1] != 0) /* The -[option]argument case: Assume that the rest of the current string is the argument. */ next_argument(clp, 1); /** Parse the argument **/ if (clp->have_arg) { Clp_ArgType *atr = &cli->argtype[ opt->arg_type ]; if (atr->func(clp, clp->arg, complain, atr->thunk) <= 0) { /* parser failed */ clp->have_arg = 0; if (TEST(opt, Clp_Mandatory)) return Clp_BadOption; else Clp_RestoreParser(clp, &clpsave); } } return opt->option_id; }
struct pb_Parameters * pb_ReadParameters(int *_argc, char **argv) { char *err_message; struct argparse ap; struct pb_Parameters *ret = (struct pb_Parameters *)malloc(sizeof(struct pb_Parameters)); /* Initialize the parameters structure */ ret->outFile = NULL; ret->inpFiles = (char **)malloc(sizeof(char *)); ret->inpFiles[0] = NULL; /* Each argument */ initialize_argparse(&ap, *_argc, argv); while(!is_end_of_arguments(&ap)) { char *arg = get_argument(&ap); /* Single-character flag */ if ((arg[0] == '-') && (arg[1] != 0) && (arg[2] == 0)) { delete_argument(&ap); /* This argument is consumed here */ switch(arg[1]) { case 'o': /* Output file name */ if (is_end_of_arguments(&ap)) { err_message = "Expecting file name after '-o'\n"; goto error; } free(ret->outFile); ret->outFile = strdup(consume_argument(&ap)); break; case 'i': /* Input file name */ if (is_end_of_arguments(&ap)) { err_message = "Expecting file name after '-i'\n"; goto error; } ret->inpFiles = read_string_array(consume_argument(&ap)); break; case '-': /* End of options */ goto end_of_options; default: err_message = "Unexpected command-line parameter\n"; goto error; } } else { /* Other parameters are ignored */ next_argument(&ap); } } /* end for each argument */ end_of_options: *_argc = ap.argc; /* Save the modified argc value */ finalize_argparse(&ap); return ret; error: fputs(err_message, stderr); pb_FreeParameters(ret); return NULL; }