123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #include "argmatch.h"
- #include <stdio.h>
- #ifdef STDC_HEADERS
- # include <string.h>
- #endif
- #if HAVE_LOCALE_H
- # include <locale.h>
- #endif
- #if ENABLE_NLS
- # include <libintl.h>
- # define _(Text) gettext (Text)
- #else
- # define _(Text) Text
- #endif
- #include "error.h"
- #include "quotearg.h"
- #ifndef ARGMATCH_QUOTING_STYLE
- # define ARGMATCH_QUOTING_STYLE escape_quoting_style
- #endif
- #if !EXIT_FAILURE
- # undef EXIT_FAILURE
- # define EXIT_FAILURE 1
- #endif
- #ifndef ARGMATCH_DIE
- # define ARGMATCH_DIE exit (EXIT_FAILURE)
- #endif
- #ifdef ARGMATCH_DIE_DECL
- ARGMATCH_DIE_DECL;
- #endif
- static void
- __argmatch_die (void)
- {
- ARGMATCH_DIE;
- }
- argmatch_exit_fn argmatch_die = __argmatch_die;
- static int
- __argmatch_internal (const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize,
- int case_sensitive)
- {
- int i;
- size_t arglen;
- int matchind = -1;
- int ambiguous = 0;
- arglen = strlen (arg);
-
- for (i = 0; arglist[i]; i++)
- {
- if (case_sensitive
- ? !strncmp (arglist[i], arg, arglen)
- : !strncasecmp (arglist[i], arg, arglen))
- {
- if (strlen (arglist[i]) == arglen)
-
- return i;
- else if (matchind == -1)
-
- matchind = i;
- else
- {
-
- if (vallist == NULL
- || memcmp (vallist + valsize * matchind,
- vallist + valsize * i, valsize))
- {
-
- ambiguous = 1;
- }
- }
- }
- }
- if (ambiguous)
- return -2;
- else
- return matchind;
- }
- int
- argmatch (const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize)
- {
- return __argmatch_internal (arg, arglist, vallist, valsize, 1);
- }
- int
- argcasematch (const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize)
- {
- return __argmatch_internal (arg, arglist, vallist, valsize, 0);
- }
- void
- argmatch_invalid (const char *context, const char *value, int problem)
- {
- enum quoting_style saved_quoting_style;
- char const *format;
-
- saved_quoting_style = get_quoting_style (NULL);
- set_quoting_style (NULL, ARGMATCH_QUOTING_STYLE);
- format = (problem == -1
- ? _("invalid argument `%s' for `%s'")
- : _("ambiguous argument `%s' for `%s'"));
- error (0, 0, format, quotearg (value), context);
- set_quoting_style (NULL, saved_quoting_style);
- }
- void
- argmatch_valid (const char *const *arglist,
- const char *vallist, size_t valsize)
- {
- int i;
- const char *last_val = NULL;
-
- 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);
- }
- int
- __xargmatch_internal (const char *context,
- const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize,
- int case_sensitive,
- argmatch_exit_fn exit_fn)
- {
- int res = __argmatch_internal (arg, arglist,
- vallist, valsize,
- case_sensitive);
- if (res >= 0)
-
- return res;
-
- argmatch_invalid (context, arg, res);
- argmatch_valid (arglist, vallist, valsize);
- (*exit_fn) ();
- return -1;
- }
- const char *
- argmatch_to_argument (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;
- }
- #ifdef TEST
- char *program_name;
- extern const char *getenv ();
- enum backup_type
- {
-
- none,
-
- simple,
-
- numbered_existing,
-
- numbered
- };
- static const char *const backup_args[] =
- {
- "no", "none", "off",
- "simple", "never",
- "existing", "nil",
- "numbered", "t",
- 0
- };
- static const enum backup_type backup_vals[] =
- {
- none, none, none,
- simple, simple,
- numbered_existing, numbered_existing,
- numbered, numbered
- };
- int
- main (int argc, const char *const *argv)
- {
- const char *cp;
- enum backup_type backup_type = none;
- program_name = (char *) argv[0];
- if (argc > 2)
- {
- fprintf (stderr, "Usage: %s [VERSION_CONTROL]\n", program_name);
- exit (1);
- }
- if ((cp = getenv ("VERSION_CONTROL")))
- backup_type = XARGCASEMATCH ("$VERSION_CONTROL", cp,
- backup_args, backup_vals);
- if (argc == 2)
- backup_type = XARGCASEMATCH (program_name, argv[1],
- backup_args, backup_vals);
- printf ("The version control is `%s'\n",
- ARGMATCH_TO_ARGUMENT (backup_type, backup_args, backup_vals));
- return 0;
- }
- #endif
|