summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2002-11-22 14:43:45 +0000
committerBruno Haible <bruno@clisp.org>2009-06-23 12:08:52 +0200
commit02df1aa0c0a75d4a019766c4fc9f68fff2cc3320 (patch)
treeffc7159224de9827239f5b4b4bed55144c9590b4
parentf97f30dda22adcf975fe6842d933b2951646a283 (diff)
downloadexternal_gettext-02df1aa0c0a75d4a019766c4fc9f68fff2cc3320.zip
external_gettext-02df1aa0c0a75d4a019766c4fc9f68fff2cc3320.tar.gz
external_gettext-02df1aa0c0a75d4a019766c4fc9f68fff2cc3320.tar.bz2
Merge with gnulib.
-rw-r--r--lib/ChangeLog5
-rw-r--r--lib/argmatch.c85
-rw-r--r--lib/argmatch.h68
3 files changed, 121 insertions, 37 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 93d4b21..75d3416 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2002-11-22 Bruno Haible <bruno@clisp.org>
+
+ * argmatch.h: Update from current gnulib version.
+ * argmatch.c: Update from current gnulib version, with simplifications.
+
2002-11-19 Bruno Haible <bruno@clisp.org>
* pathmax.h: Update from current gnulib version.
diff --git a/lib/argmatch.c b/lib/argmatch.c
index 0ff6ef8..7951f3e 100644
--- a/lib/argmatch.c
+++ b/lib/argmatch.c
@@ -18,12 +18,16 @@
/* Written by David MacKenzie <djm@ai.mit.edu>
Modified by Akim Demaille <demaille@inf.enst.fr> */
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification. */
#include "argmatch.h"
#include <stdio.h>
-#ifdef STDC_HEADERS
-# include <string.h>
-#endif
+#include <stdlib.h>
+#include <string.h>
#include "gettext.h"
#define _(msgid) gettext (msgid)
@@ -40,9 +44,8 @@
ARGMATCH_DIE_DECL;
#endif
-
static void
-__argmatch_die ()
+__argmatch_die (void)
{
ARGMATCH_DIE;
}
@@ -187,3 +190,75 @@ argmatch_to_argument (const char *value,
return arglist[i];
return NULL;
}
+
+#ifdef TEST
+/*
+ * Based on "getversion.c" by David MacKenzie <djm@gnu.ai.mit.edu>
+ */
+char *program_name;
+extern const char *getenv ();
+
+/* When to make backup files. */
+enum backup_type
+{
+ /* Never make backups. */
+ none,
+
+ /* Make simple backups of every file. */
+ simple,
+
+ /* Make numbered backups of files that already have numbered backups,
+ and simple backups of the others. */
+ numbered_existing,
+
+ /* Make numbered backups of every file. */
+ numbered
+};
+
+/* Two tables describing arguments (keys) and their corresponding
+ values */
+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 = XARGMATCH ("$VERSION_CONTROL", cp,
+ backup_args, backup_vals);
+
+ if (argc == 2)
+ backup_type = XARGMATCH (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
diff --git a/lib/argmatch.h b/lib/argmatch.h
index fb87a6f..deb908f 100644
--- a/lib/argmatch.h
+++ b/lib/argmatch.h
@@ -21,33 +21,37 @@
#ifndef ARGMATCH_H_
# define ARGMATCH_H_ 1
-# if HAVE_CONFIG_H
-# include <config.h>
-# endif
+# include <stddef.h>
-# include <sys/types.h>
+# define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
+
+# define ARGMATCH_CONSTRAINT(Arglist, Vallist) \
+ (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
/* 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. */
+ (argument list ends with a NULL guard). ARGMATCH_VERIFY is
+ preferred, since it is guaranteed to be checked at compile-time.
+ ARGMATCH_ASSERT is for backward compatibility only. */
-# undef ARRAY_CARDINALITY
-# define ARRAY_CARDINALITY(Array) (sizeof ((Array)) / sizeof (*(Array)))
+# define ARGMATCH_VERIFY(Arglist, Vallist) \
+ struct argmatch_verify \
+ { \
+ char argmatch_verify[ARGMATCH_CONSTRAINT(Arglist, Vallist) ? 1 : -1]; \
+ }
-# define ARGMATCH_ASSERT(Arglist, Vallist) \
- assert (ARRAY_CARDINALITY ((Arglist)) == ARRAY_CARDINALITY ((Vallist)) + 1)
+# define ARGMATCH_ASSERT(Arglist, Vallist) \
+ assert (ARGMATCH_CONSTRAINT (Arglist, Vallist))
/* 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 (const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize);
+int argmatch (char const *arg, char const *const *arglist,
+ char const *vallist, size_t valsize);
# define ARGMATCH(Arg, Arglist, Vallist) \
- argmatch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist)))
+ argmatch (Arg, Arglist, (char const *) (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
@@ -57,49 +61,49 @@ extern argmatch_exit_fn argmatch_die;
/* Report on stderr why argmatch failed. Report correct values. */
-void argmatch_invalid (const char *context, const char *value, int problem);
+void argmatch_invalid (char const *context, char const *value, int problem);
/* Left for compatibility with the old name invalid_arg */
# define invalid_arg(Context, Value, Problem) \
- argmatch_invalid ((Context), (Value), (Problem))
+ argmatch_invalid (Context, Value, Problem)
/* Report on stderr the list of possible arguments. */
-void argmatch_valid (const char *const *arglist,
- const char *vallist, size_t valsize);
+void argmatch_valid (char const *const *arglist,
+ char const *vallist, size_t valsize);
# define ARGMATCH_VALID(Arglist, Vallist) \
- argmatch_valid (Arglist, (const char *) Vallist, sizeof (*(Vallist)))
+ argmatch_valid (Arglist, (char const *) (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 (const char *context,
- const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize,
+int __xargmatch_internal (char const *context,
+ char const *arg, char const *const *arglist,
+ char const *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)])
+# define XARGMATCH(Context, Arg, Arglist, Vallist) \
+ ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
+ (char const *) (Vallist), \
+ sizeof *(Vallist), \
+ argmatch_die)])
/* Convert a value into a corresponding argument. */
-const char *argmatch_to_argument (char const *value,
- const char *const *arglist,
- const char *vallist, size_t valsize);
+char const *argmatch_to_argument (char const *value,
+ char const *const *arglist,
+ char const *vallist, size_t valsize);
# define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
- argmatch_to_argument ((char const *) &(Value), (Arglist), \
- (const char *) (Vallist), sizeof (*(Vallist)))
+ argmatch_to_argument (Value, Arglist, \
+ (char const *) (Vallist), sizeof *(Vallist))
#endif /* ARGMATCH_H_ */