diff options
author | Bruno Haible <bruno@clisp.org> | 2001-10-20 13:11:19 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2001-10-20 13:11:19 +0000 |
commit | e8bde484c69801c2aa848c03005022eee062dcac (patch) | |
tree | 4daa5486ab6b67089284dd1800e9f0030feb3d53 /lib | |
parent | 924e7ac34e7bfbf1c9d1b0138006270b6a69dc66 (diff) | |
download | external_gettext-e8bde484c69801c2aa848c03005022eee062dcac.zip external_gettext-e8bde484c69801c2aa848c03005022eee062dcac.tar.gz external_gettext-e8bde484c69801c2aa848c03005022eee062dcac.tar.bz2 |
Subroutines for recognizing command line arguments with a finite set of
possibilities.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ChangeLog | 7 | ||||
-rw-r--r-- | lib/Makefile.am | 21 | ||||
-rw-r--r-- | lib/argmatch.c | 220 | ||||
-rw-r--r-- | lib/argmatch.h | 117 |
4 files changed, 355 insertions, 10 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog index ca6b838..bab7212 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,12 @@ 2001-09-17 Bruno Haible <haible@clisp.cons.org> + * argmatch.h: New file, from fileutils-4.1 with simplifications. + * argmatch.c: New file, from fileutils-4.1 with simplifications. + * Makefile.am (libnlsut_a_SOURCES): Add argmatch.c. + (libnlsut_a_HEADER): Add argmatch.h. + +2001-09-17 Bruno Haible <haible@clisp.cons.org> + * copy-file.h: New file. * copy-file.c: New file. * Makefile.am (libnlsut_a_SOURCES): Add copy-file.c. diff --git a/lib/Makefile.am b/lib/Makefile.am index c7cc946..079fa14 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -23,16 +23,17 @@ noinst_LIBRARIES = libnlsut.a # Sources that are compiled on all platforms. -libnlsut_a_SOURCES = basename.c c-ctype.c concatpath.c execute.c findprog.c \ -fstrcmp.c full-write.c gcd.c getopt.c getopt1.c hash.c javacomp.c javaexec.c \ -linebreak.c localcharset.c mbswidth.c obstack.c pipe-bidi.c pipe-in.c \ -pipe-out.c progname.c sh-quote.c tmpdir.c wait-process.c xerror.c xgetcwd.c \ -xmalloc.c xstrdup.c - -libnlsut_a_HEADER = basename.h c-ctype.h execute.h findprog.h fstrcmp.h \ -full-write.h gcd.h getopt.h hash.h javacomp.h javaexec.h lbrkprop.h \ -linebreak.h mbswidth.h obstack.h pathmax.h pipe.h progname.h sh-quote.h \ -system.h tmpdir.h utf8-ucs4.h utf16-ucs4.h wait-process.h xerror.h xmalloc.h +libnlsut_a_SOURCES = argmatch.c basename.c c-ctype.c concatpath.c copy-file.c \ +execute.c findprog.c fstrcmp.c full-write.c gcd.c getopt.c getopt1.c hash.c \ +javacomp.c javaexec.c linebreak.c localcharset.c mbswidth.c obstack.c \ +pipe-bidi.c pipe-in.c pipe-out.c progname.c sh-quote.c tmpdir.c \ +wait-process.c xerror.c xgetcwd.c xmalloc.c xstrdup.c + +libnlsut_a_HEADER = argmatch.h basename.h c-ctype.h copy-file.h execute.h \ +findprog.h fstrcmp.h full-write.h gcd.h getopt.h hash.h javacomp.h javaexec.h \ +lbrkprop.h linebreak.h mbswidth.h obstack.h pathmax.h pipe.h progname.h \ +sh-quote.h system.h tmpdir.h utf8-ucs4.h utf16-ucs4.h wait-process.h xerror.h \ +xmalloc.h # Sources that are compiled only on platforms that lack the functions. diff --git a/lib/argmatch.c b/lib/argmatch.c new file mode 100644 index 0000000..2ed60cb --- /dev/null +++ b/lib/argmatch.c @@ -0,0 +1,220 @@ +/* argmatch.c -- find a match for a string in an array + Copyright (C) 1990, 1998, 1999, 2001 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* Written by David MacKenzie <djm@ai.mit.edu> + Modified by Akim Demaille <demaille@inf.enst.fr> */ + +#include "argmatch.h" + +#include <stdio.h> +#ifdef STDC_HEADERS +# include <string.h> +#endif + +#include <locale.h> + +#if ENABLE_NLS +# include <libintl.h> +# define _(Text) gettext (Text) +#else +# define _(Text) Text +#endif + +#include "error.h" + +/* The following test is to work around the gross typo in + systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE + is defined to 0, not 1. */ +#if !EXIT_FAILURE +# undef EXIT_FAILURE +# define EXIT_FAILURE 1 +#endif + +/* Non failing version of argmatch call this function after failing. */ +#ifndef ARGMATCH_DIE +# define ARGMATCH_DIE exit (EXIT_FAILURE) +#endif + +#ifdef ARGMATCH_DIE_DECL +ARGMATCH_DIE_DECL; +#endif + + +/* Prototypes for local functions. Needed to ensure compiler checking of + function argument counts despite of K&R C function definition syntax. */ +static void __argmatch_die PARAMS ((void)); + + +static void +__argmatch_die () +{ + ARGMATCH_DIE; +} + +/* Used by XARGMATCH and XARGCASEMATCH. See description in argmatch.h. + Default to __argmatch_die, but allow caller to change this at run-time. */ +argmatch_exit_fn argmatch_die = __argmatch_die; + + +/* If ARG is an unambiguous match for an element of the + null-terminated array ARGLIST, return the index in ARGLIST + of the matched element, else -1 if it does not match any element + or -2 if it is ambiguous (is a prefix of more than one element). + + If VALLIST is none null, use it to resolve ambiguities limited to + synonyms, i.e., for + "yes", "yop" -> 0 + "no", "nope" -> 1 + "y" is a valid argument, for `0', and "n" for `1'. */ + +int +argmatch (arg, arglist, vallist, valsize) + const char *arg; + const char *const *arglist; + const char *vallist; + size_t valsize; +{ + int i; /* Temporary index in ARGLIST. */ + size_t arglen; /* Length of ARG. */ + int matchind = -1; /* Index of first nonexact match. */ + int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */ + + arglen = strlen (arg); + + /* Test all elements for either exact match or abbreviated matches. */ + for (i = 0; arglist[i]; i++) + { + if (!strncmp (arglist[i], arg, arglen)) + { + if (strlen (arglist[i]) == arglen) + /* Exact match found. */ + return i; + else if (matchind == -1) + /* First nonexact match found. */ + matchind = i; + else + { + /* Second nonexact match found. */ + if (vallist == NULL + || memcmp (vallist + valsize * matchind, + vallist + valsize * i, valsize)) + { + /* There is a real ambiguity, or we could not + disambiguate. */ + ambiguous = 1; + } + } + } + } + if (ambiguous) + return -2; + else + return matchind; +} + +/* Error reporting for argmatch. + CONTEXT is a description of the type of entity that was being matched. + VALUE is the invalid value that was given. + PROBLEM is the return value from argmatch. */ + +void +argmatch_invalid (context, value, problem) + const char *context; + const char *value; + int problem; +{ + char const *format = (problem == -1 + ? _("invalid argument `%s' for `%s'") + : _("ambiguous argument `%s' for `%s'")); + + error (0, 0, format, value, context); +} + +/* List the valid arguments for argmatch. + ARGLIST is the same as in argmatch. + VALLIST is a pointer to an array of values. + VALSIZE is the size of the elements of VALLIST */ +void +argmatch_valid (arglist, vallist, valsize) + const char *const *arglist; + const char *vallist; + size_t valsize; +{ + int i; + const char *last_val = NULL; + + /* We try to put synonyms on the same line. The assumption is that + synonyms follow each other */ + fprintf (stderr, _("Valid arguments are:")); + for (i = 0; arglist[i]; i++) + if ((i == 0) + || memcmp (last_val, vallist + valsize * i, valsize)) + { + fprintf (stderr, "\n - `%s'", arglist[i]); + last_val = vallist + valsize * i; + } + else + { + fprintf (stderr, ", `%s'", arglist[i]); + } + putc ('\n', stderr); +} + +/* Never failing versions of the previous functions. + + CONTEXT is the context for which argmatch is called (e.g., + "--version-control", or "$VERSION_CONTROL" etc.). Upon failure, + calls the (supposed never to return) function EXIT_FN. */ + +int +__xargmatch_internal (context, arg, arglist, vallist, valsize, exit_fn) + const char *context; + const char *arg; + const char *const *arglist; + const char *vallist; + size_t valsize; + argmatch_exit_fn exit_fn; +{ + int res = argmatch (arg, arglist, vallist, valsize); + if (res >= 0) + /* Success. */ + return res; + + /* We failed. Explain why. */ + argmatch_invalid (context, arg, res); + argmatch_valid (arglist, vallist, valsize); + (*exit_fn) (); + + return -1; /* To please the compilers. */ +} + +/* Look for VALUE in VALLIST, an array of objects of size VALSIZE and + return the first corresponding argument in ARGLIST */ +const char * +argmatch_to_argument (value, arglist, vallist, valsize) + const char *value; + const char *const *arglist; + const char *vallist; + size_t valsize; +{ + int i; + + for (i = 0; arglist[i]; i++) + if (!memcmp (value, vallist + valsize * i, valsize)) + return arglist[i]; + return NULL; +} diff --git a/lib/argmatch.h b/lib/argmatch.h new file mode 100644 index 0000000..32b318b --- /dev/null +++ b/lib/argmatch.h @@ -0,0 +1,117 @@ +/* argmatch.h -- definitions and prototypes for argmatch.c + Copyright (C) 1990, 1998, 1999, 2001 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* Written by David MacKenzie <djm@ai.mit.edu> + Modified by Akim Demaille <demaille@inf.enst.fr> */ + +#ifndef ARGMATCH_H_ +# define ARGMATCH_H_ 1 + +# if HAVE_CONFIG_H +# include <config.h> +# endif + +# include <sys/types.h> + +# ifndef PARAMS +# if PROTOTYPES || (defined (__STDC__) && __STDC__) +# define PARAMS(args) args +# else +# define PARAMS(args) () +# endif /* GCC. */ +# endif /* Not PARAMS. */ + +/* Assert there are as many real arguments as there are values + (argument list ends with a NULL guard). There is no execution + cost, since it will be statically evalauted to `assert (0)' or + `assert (1)'. Unfortunately there is no -Wassert-0. */ + +# undef ARRAY_CARDINALITY +# define ARRAY_CARDINALITY(Array) (sizeof ((Array)) / sizeof (*(Array))) + +# define ARGMATCH_ASSERT(Arglist, Vallist) \ + assert (ARRAY_CARDINALITY ((Arglist)) == ARRAY_CARDINALITY ((Vallist)) + 1) + +/* Return the index of the element of ARGLIST (NULL terminated) that + matches with ARG. If VALLIST is not NULL, then use it to resolve + false ambiguities (i.e., different matches of ARG but corresponding + to the same values in VALLIST). */ + +int argmatch + PARAMS ((const char *arg, const char *const *arglist, + const char *vallist, size_t valsize)); + +# define ARGMATCH(Arg, Arglist, Vallist) \ + argmatch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist))) + +/* xargmatch calls this function when it fails. This function should not + return. By default, this is a function that calls ARGMATCH_DIE which + in turn defaults to `exit (EXIT_FAILURE)'. */ +typedef void (*argmatch_exit_fn) PARAMS ((void)); +extern argmatch_exit_fn argmatch_die; + +/* Report on stderr why argmatch failed. Report correct values. */ + +void argmatch_invalid + PARAMS ((const char *context, const char *value, int problem)); + +/* Left for compatibility with the old name invalid_arg */ + +# define invalid_arg(Context, Value, Problem) \ + argmatch_invalid ((Context), (Value), (Problem)) + + + +/* Report on stderr the list of possible arguments. */ + +void argmatch_valid + PARAMS ((const char *const *arglist, + const char *vallist, size_t valsize)); + +# define ARGMATCH_VALID(Arglist, Vallist) \ + argmatch_valid (Arglist, (const char *) Vallist, sizeof (*(Vallist))) + + + +/* Same as argmatch, but upon failure, reports a explanation on the + failure, and exits using the function EXIT_FN. */ + +int __xargmatch_internal + PARAMS ((const char *context, + const char *arg, const char *const *arglist, + const char *vallist, size_t valsize, + argmatch_exit_fn exit_fn)); + +/* Programmer friendly interface to __xargmatch_internal. */ + +# define XARGMATCH(Context, Arg, Arglist, Vallist) \ + (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \ + (const char *) (Vallist), \ + sizeof (*(Vallist)), \ + argmatch_die)]) + +/* Convert a value into a corresponding argument. */ + +const char *argmatch_to_argument + PARAMS ((char const *value, const char *const *arglist, + const char *vallist, size_t valsize)); + +# define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \ + argmatch_to_argument ((char const *) &(Value), (Arglist), \ + (const char *) (Vallist), sizeof (*(Vallist))) + +#endif /* ARGMATCH_H_ */ |