#include "argmatch.h"
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# 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"
#include "quote.h"
#include "unlocked-io.h"
#ifndef ARGMATCH_QUOTING_STYLE
# define ARGMATCH_QUOTING_STYLE locale_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)
{
char const *format = (problem == -1
? _("invalid argument %s for %s")
: _("ambiguous argument %s for %s"));
error (0, 0, format, quotearg_n_style (0, ARGMATCH_QUOTING_STYLE, value),
quote_n (1, context));
}
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