diff options
-rw-r--r-- | libshouldbeinlibc/argp-parse.c | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/libshouldbeinlibc/argp-parse.c b/libshouldbeinlibc/argp-parse.c index 40cfc87f..f6051d37 100644 --- a/libshouldbeinlibc/argp-parse.c +++ b/libshouldbeinlibc/argp-parse.c @@ -568,7 +568,7 @@ parser_finalize (struct parser *parser, /* Suppress errors generated by unparsed arguments. */ err = 0; - if (!err) + if (! err) if (parser->state.next == parser->state.argc) /* We successfully parsed all arguments! Call all the parsers again, just a few more times... */ @@ -605,6 +605,8 @@ parser_finalize (struct parser *parser, { /* Maybe print an error message. */ if (err == EBADKEY) + /* An appropriate message describing what the error was should have + been printed earlier. */ __argp_state_help (&parser->state, parser->state.err_stream, ARGP_HELP_STD_ERR); @@ -634,10 +636,10 @@ parser_finalize (struct parser *parser, return err; } -/* Parse the non-option argument ARG, at the current position. Returns - any error, and sets ARG_EBADKEY to true if return EBADKEY. */ +/* Call the user parsers to parse the non-option argument VAL, at the current + position, returning any error. */ static error_t -parser_parse_arg (struct parser *parser, char *val, int *arg_ebadkey) +parser_parse_arg (struct parser *parser, char *val) { int index = parser->state.next; error_t err = EBADKEY; @@ -658,56 +660,50 @@ parser_parse_arg (struct parser *parser, char *val, int *arg_ebadkey) /* The user wants to reparse some args, give getopt another try. */ parser->try_getopt = 1; - if (err == EBADKEY) - *arg_ebadkey = 1; - return err; } -/* Parse the option OPT (with argument ARG), at the current position. - Returns any error, and sets ARG_EBADKEY to true if it was actually an - argument and the parser returned EBADKEY. */ +/* Call the user parsers to parse the option OPT, with argument VAL, at the + current position, returning any error. */ static error_t -parser_parse_opt (struct parser *parser, int opt, char *val, int *arg_ebadkey) +parser_parse_opt (struct parser *parser, int opt, char *val) { /* The group key encoded in the high bits; 0 for short opts or group_number + 1 for long opts. */ int group_key = opt >> USER_BITS; - error_t err = EBADKEY; /* until otherwise asserted */ - struct group *group; if (group_key == 0) - /* A short option. */ + /* A short option. By comparing OPT's position in SHORT_OPTS to the + various starting positions in each group's SHORT_END field, we can + determine which group OPT came from. */ { - /* By comparing OPT's position in SHORT_OPTS to the various - starting positions in each group's SHORT_END field, we can - determine which group OPT came from. */ + struct group *group; char *short_index = strchr (parser->short_opts, opt); + if (short_index) for (group = parser->groups; group < parser->egroup; group++) if (group->short_end > short_index) - { - err = group_parse (group, &parser->state, opt, optarg); - break; - } - else - err = EBADKEY; + return group_parse (group, &parser->state, opt, optarg); + + return EBADKEY; /* until otherwise asserted */ } else /* A long option. We use shifts instead of masking for extracting the user value in order to preserve the sign. */ - err = + return group_parse (&parser->groups[group_key - 1], &parser->state, (opt << GROUP_BITS) >> GROUP_BITS, optarg); - - return err; } -/* Parse the next argument in PARSER (as indicated by PARSER->state.next. */ +/* Parse the next argument in PARSER (as indicated by PARSER->state.next). + Any error from the parsers is returned, and *ARGP_EBADKEY indicates + whether a value of EBADKEY is due to an unrecognized argument (which is + generally not fatal). */ static error_t parser_parse_next (struct parser *parser, int *arg_ebadkey) { int opt; + error_t err = 0; if (parser->state.quoted && parser->state.next < parser->state.quoted) /* The next argument pointer has been moved to before the quoted @@ -766,20 +762,21 @@ parser_parse_next (struct parser *parser, int *arg_ebadkey) } else /* A non-option arg. */ - return - parser_parse_arg (parser, - parser->state.argv[parser->state.next++], - arg_ebadkey); + err = + parser_parse_arg (parser, parser->state.argv[parser->state.next++]); else if (opt == KEY_ARG) /* A non-option argument; try each parser in turn. */ - return parser_parse_arg (parser, optarg, arg_ebadkey); + err = parser_parse_arg (parser, optarg); else - return parser_parse_opt (parser, opt, optarg, arg_ebadkey); + err = parser_parse_opt (parser, opt, optarg); -#if 0 - if (err == EBADKEY && arg_ebadkey) - state.next--; /* Put back the unused argument. */ -#endif + if (err == EBADKEY) + { + *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG); + parser->state.next--; /* Put back the unused argument. */ + } + + return err; } /* Parse the options strings in ARGC & ARGV according to the argp in ARGP. |