diff options
author | Tom Tromey <tromey@src.gnome.org> | 1998-03-04 07:59:28 +0000 |
---|---|---|
committer | Tom Tromey <tromey@src.gnome.org> | 1998-03-04 07:59:28 +0000 |
commit | ef86ac226d7eb2d81a30107490061ac05056c9ba (patch) | |
tree | cf84b8a04fc18269fd851e256084fffa29b8675a | |
parent | 3d7219a67cd78133b25938636adc2a0f67ba5bda (diff) | |
download | shared-mime-info-ef86ac226d7eb2d81a30107490061ac05056c9ba.tar.gz |
Last import was from an older glibc, sigh.
- tromey@cygnus.com
svn path=/branches/GNU/; revision=101
-rw-r--r-- | support/argp-fmtstream.h | 6 | ||||
-rw-r--r-- | support/argp-help.c | 29 | ||||
-rw-r--r-- | support/argp-parse.c | 128 | ||||
-rw-r--r-- | support/argp.h | 56 | ||||
-rw-r--r-- | support/strndup.c | 63 |
5 files changed, 181 insertions, 101 deletions
diff --git a/support/argp-fmtstream.h b/support/argp-fmtstream.h index d1a262a7..ebc68380 100644 --- a/support/argp-fmtstream.h +++ b/support/argp-fmtstream.h @@ -23,8 +23,8 @@ that. This header file is only used internally while compiling argp, and shouldn't be installed. */ -#ifndef __ARGP_FMTSTREAM_H__ -#define __ARGP_FMTSTREAM_H__ +#ifndef _ARGP_FMTSTREAM_H +#define _ARGP_FMTSTREAM_H #ifdef HAVE_CONFIG_H #include <config.h> @@ -294,4 +294,4 @@ __argp_fmtstream_point (argp_fmtstream_t __fs) #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */ -#endif /* __ARGP_FMTSTREAM_H__ */ +#endif /* argp-fmtstream.h */ diff --git a/support/argp-help.c b/support/argp-help.c index 2120ab15..8faacd53 100644 --- a/support/argp-help.c +++ b/support/argp-help.c @@ -778,7 +778,7 @@ hol_append (struct hol *hol, struct hol *more) more->num_entries = 0; /* Mark MORE's fields as invalid. */ } else - /* append the entries in MORE to those in HOL, taking care to only add + /* Append the entries in MORE to those in HOL, taking care to only add non-shadowed SHORT_OPTIONS values. */ { unsigned left; @@ -791,12 +791,12 @@ hol_append (struct hol *hol, struct hol *more) char *short_options = malloc (hol_so_len + strlen (more->short_options) + 1); - memcpy (entries, hol->entries, - hol->num_entries * sizeof (struct hol_entry)); - memcpy (entries + hol->num_entries, more->entries, - more->num_entries * sizeof (struct hol_entry)); + __mempcpy (__mempcpy (entries, hol->entries, + hol->num_entries * sizeof (struct hol_entry)), + more->entries, + more->num_entries * sizeof (struct hol_entry)); - memcpy (short_options, hol->short_options, hol_so_len); + __mempcpy (short_options, hol->short_options, hol_so_len); /* Fix up the short options pointers from HOL. */ for (e = entries, left = hol->num_entries; left > 0; e++, left--) @@ -922,7 +922,7 @@ filter_doc (const char *doc, int key, const struct argp *argp, } else /* No filter. */ - return (char *)doc; + return doc; } /* Prints STR as a header line, with the margin lines set appropiately, and @@ -1319,7 +1319,8 @@ argp_args_usage (const struct argp *argp, const struct argp_state *state, if (fdoc) { - nl = strchr (fdoc, '\n'); + const char *cp = fdoc; + nl = strchr (cp, '\n'); if (nl) /* This is a `multi-level' args doc; advance to the correct position as determined by our state in LEVELS, and update LEVELS. */ @@ -1327,17 +1328,17 @@ argp_args_usage (const struct argp *argp, const struct argp_state *state, int i; multiple = 1; for (i = 0; i < *our_level; i++) - fdoc = nl + 1, nl = strchr (fdoc, '\n'); + cp = nl + 1, nl = strchr (cp, '\n'); (*levels)++; } if (! nl) - nl = fdoc + strlen (fdoc); + nl = cp + strlen (cp); /* Manually do line wrapping so that it (probably) won't get wrapped at any embedded spaces. */ - space (stream, 1 + nl - fdoc); + space (stream, 1 + nl - cp); - __argp_fmtstream_write (stream, fdoc, nl - fdoc); + __argp_fmtstream_write (stream, cp, nl - cp); } if (fdoc && fdoc != tdoc) free ((char *)fdoc); /* Free user's modified doc string. */ @@ -1499,7 +1500,7 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream, do { int old_lm; - int old_wm = __argp_fmtstream_set_wmargin (fs, USAGE_INDENT); + int old_wm = __argp_fmtstream_set_wmargin (fs, uparams.usage_indent); char *levels = pattern_levels; __argp_fmtstream_printf (fs, "%s %s", @@ -1508,7 +1509,7 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream, /* We set the lmargin as well as the wmargin, because hol_usage manually wraps options with newline to avoid annoying breaks. */ - old_lm = __argp_fmtstream_set_lmargin (fs, USAGE_INDENT); + old_lm = __argp_fmtstream_set_lmargin (fs, uparams.usage_indent); if (flags & ARGP_HELP_SHORT_USAGE) /* Just show where the options go. */ diff --git a/support/argp-parse.c b/support/argp-parse.c index edc55168..553dd885 100644 --- a/support/argp-parse.c +++ b/support/argp-parse.c @@ -38,10 +38,11 @@ # define _(msgid) (msgid) # define gettext(msgid) (msgid) #endif +#define N_(msgid) (msgid) #endif #if _LIBC - 0 -#include <libc-lock.h> +#include <bits/libc-lock.h> #else #ifdef HAVE_CTHREADS_H #include <cthreads.h> @@ -84,11 +85,11 @@ volatile int _argp_hang = 0; static const struct argp_option argp_default_options[] = { - {"help", '?', 0, 0, "Give this help list", -1}, - {"usage", OPT_USAGE, 0, 0, "Give a short usage message"}, - {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, "Set the program name"}, + {"help", '?', 0, 0, N_("Give this help list"), -1}, + {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message")}, + {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, N_("Set the program name")}, {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN, - "Hang for SECS seconds (default 3600)"}, + N_("Hang for SECS seconds (default 3600)")}, {0, 0} }; @@ -145,7 +146,7 @@ static const struct argp argp_default_argp = static const struct argp_option argp_version_options[] = { - {"version", 'V', 0, 0, "Print program version", -1}, + {"version", 'V', 0, 0, N_("Print program version"), -1}, {0, 0} }; @@ -160,7 +161,7 @@ argp_version_parser (int key, char *arg, struct argp_state *state) else if (argp_program_version) fprintf (state->out_stream, "%s\n", argp_program_version); else - __argp_error (state, _("No version known!?")); + __argp_error (state, _("(PROGRAM ERROR) No version known!?")); if (! (state->flags & ARGP_NO_EXIT)) exit (0); break; @@ -637,8 +638,8 @@ parser_finalize (struct parser *parser, err = EBADKEY; } - /* Okay, we're all done, with either an error or success. We only call the - parsers once more, to indicate which one. */ + /* Okay, we're all done, with either an error or success; call the parsers + to indicate which one. */ if (err) { @@ -654,7 +655,7 @@ parser_finalize (struct parser *parser, group_parse (group, &parser->state, ARGP_KEY_ERROR, 0); } else - /* Do final cleanup, including propagating back values from parsers. */ + /* Notify parsers of success, and propagate back values from parsers. */ { /* We pass over the groups in reverse order so that child groups are given a chance to do there processing before passing back a value to @@ -667,6 +668,10 @@ parser_finalize (struct parser *parser, err = 0; /* Some parser didn't understand. */ } + /* Call parsers once more, to do any final cleanup. Errors are ignored. */ + for (group = parser->egroup - 1; group >= parser->groups; group--) + group_parse (group, &parser->state, ARGP_KEY_FINI, 0); + if (err == EBADKEY) err = EINVAL; @@ -676,28 +681,55 @@ parser_finalize (struct parser *parser, } /* Call the user parsers to parse the non-option argument VAL, at the current - position, returning any error. */ + position, returning any error. The state NEXT pointer is assumed to have + been adjusted (by getopt) to point after this argument; this function will + adjust it correctly to reflect however many args actually end up being + consumed. */ static error_t parser_parse_arg (struct parser *parser, char *val) { - int index = parser->state.next; + /* Save the starting value of NEXT, first adjusting it so that the arg + we're parsing is again the front of the arg vector. */ + int index = --parser->state.next; error_t err = EBADKEY; struct group *group; + int key = 0; /* Which of ARGP_KEY_ARG[S] we used. */ + /* Try to parse the argument in each parser. */ for (group = parser->groups ; group < parser->egroup && err == EBADKEY ; group++) - err = group_parse (group, &parser->state, ARGP_KEY_ARG, val); - - if (!err) - if (parser->state.next >= index) - /* Remember that we successfully processed a non-option - argument -- but only if the user hasn't gotten tricky and set - the clock back. */ - (--group)->args_processed++; - else - /* The user wants to reparse some args, give getopt another try. */ - parser->try_getopt = 1; + { + parser->state.next++; /* For ARGP_KEY_ARG, consume the arg. */ + key = ARGP_KEY_ARG; + err = group_parse (group, &parser->state, key, val); + + if (err == EBADKEY) + /* This parser doesn't like ARGP_KEY_ARG; try ARGP_KEY_ARGS instead. */ + { + parser->state.next--; /* For ARGP_KEY_ARGS, put back the arg. */ + key = ARGP_KEY_ARGS; + err = group_parse (group, &parser->state, key, 0); + } + } + + if (! err) + { + if (key == ARGP_KEY_ARGS) + /* The default for ARGP_KEY_ARGS is to assume that if NEXT isn't + changed by the user, *all* arguments should be considered + consumed. */ + parser->state.next = parser->state.argc; + + if (parser->state.next > index) + /* Remember that we successfully processed a non-option + argument -- but only if the user hasn't gotten tricky and set + the clock back. */ + (--group)->args_processed += (parser->state.next - index); + else + /* The user wants to reparse some args, give getopt another try. */ + parser->try_getopt = 1; + } return err; } @@ -710,6 +742,7 @@ 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; if (group_key == 0) /* A short option. By comparing OPT's position in SHORT_OPTS to the @@ -722,16 +755,39 @@ parser_parse_opt (struct parser *parser, int opt, char *val) if (short_index) for (group = parser->groups; group < parser->egroup; group++) if (group->short_end > short_index) - return group_parse (group, &parser->state, opt, optarg); - - return EBADKEY; /* until otherwise asserted */ + { + err = group_parse (group, &parser->state, opt, optarg); + break; + } } else /* A long option. We use shifts instead of masking for extracting the user value in order to preserve the sign. */ - return + err = group_parse (&parser->groups[group_key - 1], &parser->state, (opt << GROUP_BITS) >> GROUP_BITS, optarg); + + if (err == EBADKEY) + /* At least currently, an option not recognized is an error in the + parser, because we pre-compute which parser is supposed to deal + with each option. */ + { + static const char bad_key_err[] = + N_("(PROGRAM ERROR) Option should have been recognized!?"); + if (group_key == 0) + __argp_error (&parser->state, "-%c: %s", opt, _(bad_key_err)); + else + { + struct option *long_opt = parser->long_opts; + while (long_opt->val != opt && long_opt->name) + long_opt++; + __argp_error (&parser->state, "--%s: %s", + long_opt->name ? long_opt->name : "???", + _(bad_key_err)); + } + } + + return err; } /* Parse the next argument in PARSER (as indicated by PARSER->state.next). @@ -800,20 +856,20 @@ parser_parse_next (struct parser *parser, int *arg_ebadkey) return EBADKEY; } else - /* A non-option arg. */ - err = - parser_parse_arg (parser, parser->state.argv[parser->state.next++]); - else if (opt == KEY_ARG) + /* A non-option arg; simulate what getopt might have done. */ + { + opt = KEY_ARG; + optarg = parser->state.argv[parser->state.next++]; + } + + if (opt == KEY_ARG) /* A non-option argument; try each parser in turn. */ err = parser_parse_arg (parser, optarg); else err = parser_parse_opt (parser, opt, optarg); - + if (err == EBADKEY) - { - *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG); - parser->state.next--; /* Put back the unused argument. */ - } + *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG); return err; } diff --git a/support/argp.h b/support/argp.h index 886742a3..55024cac 100644 --- a/support/argp.h +++ b/support/argp.h @@ -18,8 +18,8 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#ifndef __ARGP_H__ -#define __ARGP_H__ +#ifndef _ARGP_H +#define _ARGP_H #include <stdio.h> #include <ctype.h> @@ -29,16 +29,16 @@ #include <errno.h> #ifndef __const -#define __const const +# define __const const #endif #ifndef __error_t_defined typedef int error_t; -#define __error_t_defined +# define __error_t_defined #endif #ifndef __P -# if (defined (__STDC__) && __STDC__) || defined (__cplusplus) +# if (defined __STDC__ && __STDC__) || defined __cplusplus # define __P(args) args # else # define __P(args) () @@ -160,6 +160,12 @@ typedef error_t (*argp_parser_t)(int key, char *arg, struct argp_state *state); actually modify the argument (perhaps into an option), and have it processed again. */ #define ARGP_KEY_ARG 0 +/* There are remaining arguments not parsed by any parser, which may be found + starting at (STATE->argv + STATE->next). If success is returned, but + STATE->next left untouched, it's assumed that all arguments were consume, + otherwise, the parser should adjust STATE->next to reflect any arguments + consumed. */ +#define ARGP_KEY_ARGS 0x1000006 /* There are no more command line arguments at all. */ #define ARGP_KEY_END 0x1000001 /* Because it's common to want to do some special processing if there aren't @@ -172,11 +178,12 @@ typedef error_t (*argp_parser_t)(int key, char *arg, struct argp_state *state); element of the CHILD_INPUT field, if any, in the state structure is copied to each child's state to be the initial value of the INPUT field. */ #define ARGP_KEY_INIT 0x1000003 +/* Use after all other keys, including SUCCESS & END. */ +#define ARGP_KEY_FINI 0x1000007 /* Passed in when parsing has successfully been completed (even if there are still arguments remaining). */ #define ARGP_KEY_SUCCESS 0x1000004 -/* Passed in if an error occurs (in which case a call with ARGP_KEY_SUCCESS is - never made, so any cleanup must be done here). */ +/* Passed in if an error occurs. */ #define ARGP_KEY_ERROR 0x1000005 /* An argp structure contains a set of options declarations, a function to @@ -396,7 +403,7 @@ extern void (*argp_program_version_hook) __P ((FILE *__stream, argp_help if the ARGP_HELP_BUG_ADDR flag is set (as it is by various standard help messages), embedded in a sentence that says something like `Report bugs to ADDR.'. */ -__const extern char *argp_program_bug_address; +extern __const char *argp_program_bug_address; /* The exit status that argp will use when exiting due to a parsing error. If not defined or set by the user program, this defaults to EX_USAGE from @@ -503,16 +510,16 @@ extern void *__argp_input __P ((__const struct argp *argp, #ifdef __OPTIMIZE__ -#if !_LIBC -# define __argp_usage argp_usage -# define __argp_state_help argp_state_help -# define __option_is_short _option_is_short -# define __option_is_end _option_is_end -#endif +# if !_LIBC +# define __argp_usage argp_usage +# define __argp_state_help argp_state_help +# define __option_is_short _option_is_short +# define __option_is_end _option_is_end +# endif -#ifndef ARGP_EI -# define ARGP_EI extern inline -#endif +# ifndef ARGP_EI +# define ARGP_EI extern inline +# endif ARGP_EI void __argp_usage (__const struct argp_state *__state) @@ -538,17 +545,16 @@ __option_is_end (__const struct argp_option *__opt) return !__opt->key && !__opt->name && !__opt->doc && !__opt->group; } -#if !_LIBC -# undef __argp_usage -# undef __argp_state_help -# undef __option_is_short -# undef __option_is_end -#endif - +# if !_LIBC +# undef __argp_usage +# undef __argp_state_help +# undef __option_is_short +# undef __option_is_end +# endif #endif /* __OPTIMIZE__ */ #ifdef __cplusplus } #endif -#endif /* __ARGP_H__ */ +#endif /* argp.h */ diff --git a/support/strndup.c b/support/strndup.c index 72635bb9..cd971e1c 100644 --- a/support/strndup.c +++ b/support/strndup.c @@ -1,28 +1,43 @@ -/* Copyright (C) 1996 Free Software Foundation, Inc. -This file is part of the GNU C Library. - -The GNU C Library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. - -The GNU C Library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ - -#include <stddef.h> -#include <stdlib.h> -#include <string.h> - +/* Copyright (C) 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include <stdio.h> +#include <sys/types.h> + +#if defined _LIBC || defined STDC_HEADERS +# include <stdlib.h> +# include <string.h> +#else +char *malloc (); +#endif + +#ifndef weak_alias +# define __strndup strndup +#endif char * -__strndup (const char *s, size_t n) +__strndup (s, n) + const char *s; + size_t n; { size_t len = strnlen (s, n); char *new = malloc (len + 1); @@ -33,4 +48,6 @@ __strndup (const char *s, size_t n) new[len] = '\0'; return memcpy (new, s, len); } +#ifdef weak_alias weak_alias (__strndup, strndup) +#endif |