]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Merge with gnulib.
authorBruno Haible <bruno@clisp.org>
Fri, 22 Nov 2002 14:43:45 +0000 (14:43 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:08:52 +0000 (12:08 +0200)
lib/ChangeLog
lib/argmatch.c
lib/argmatch.h

index 93d4b2167119cd0fb7bd4ccc6f8291125b8d4f48..75d34168666cf04b5ff6fe7bd24162836ce46b52 100644 (file)
@@ -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.
index 0ff6ef861f6809178fd784cdae2bdfe181ad9e40..7951f3ed6854e9eb4ed37349f88f71981256cf7b 100644 (file)
 /* 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
index fb87a6f5f2a744eacd24ab886013c795fa12f56b..deb908fb9d1b03427caa9805a5379a52985d6670 100644 (file)
 #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_ */