m4-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

xstrtol upstream change


From: Eric Blake
Subject: xstrtol upstream change
Date: Tue, 7 Aug 2007 20:16:33 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

In an effort to make xstrtol strings easier to translate, gnulib changed its 
interface.  Here's the patch that I'm applying to head to accomodate (the 
branch does not use xstrtol), borrowing ideas from coreutils.  I found Paul's 
idea of OPT_STR_INIT rather slick, once I figured out how it worked.

2007-08-07  Eric Blake  <address@hidden>

        Adjust to gnulib xstrtol changes.
        * src/main.c (size_opt): Adjust signature, so that error strings
        are easier to translate.
        (OPT_STR, LONG_OPT_STR, short_opt_str, OPT_STR_INIT): New macros
        for tracking long options.
        (main): Track which long option was used.
        * tests/options.at (--debuglen, --nesting-limit): Adjust to change
        in error message.

Index: src/main.c
===================================================================
RCS file: /sources/m4/m4/src/main.c,v
retrieving revision 1.114
diff -u -p -b -r1.114 main.c
--- src/main.c  7 Aug 2007 03:15:31 -0000       1.114
+++ src/main.c  7 Aug 2007 20:07:35 -0000
@@ -216,12 +216,37 @@ enum
   VERSION_OPTION                       /* no short opt */
 };
 
+/* Use OPT_IDX to decide whether to return either a short option
+   string "-C", or a long option string derived from LONG_OPTIONS.
+   OPT_IDX is -1 if the short option C was used; otherwise it is an
+   index into LONG_OPTIONS, which should have a name preceded by two
+   '-' characters.  */
+#define OPT_STR(opt_idx, c, long_options)      \
+  ((opt_idx) < 0                               \
+   ? short_opt_str (c)                         \
+   : LONG_OPT_STR (opt_idx, long_options))
+
+/* Likewise, but assume OPT_IDX is nonnegative.  */
+#define LONG_OPT_STR(opt_idx, long_options) ((long_options)[opt_idx].name - 2)
+
+/* Given the byte, C, return the string "-C" in static storage.  */
+static inline char *
+short_opt_str (char c)
+{
+  static char opt_str_storage[3] = {'-', 0, 0};
+  opt_str_storage[1] = c;
+  return opt_str_storage;
+}
+
+/* Define an option string that will be used with OPT_STR or LONG_OPT_STR.  */
+#define OPT_STR_INIT(name) ("--" name + 2)
+
 /* Decode options and launch execution.  */
 static const struct option long_options[] =
 {
   {"batch", no_argument, NULL, 'b'},
   {"debug", optional_argument, NULL, 'd'},
-  {"debuglen", required_argument, NULL, 'l'},
+  {OPT_STR_INIT ("debuglen"), required_argument, NULL, 'l'},
   {"debugmode", optional_argument, NULL, 'd'},
   {"define", required_argument, NULL, 'D'},
   {"discard-comments", no_argument, NULL, 'c'},
@@ -232,7 +257,7 @@ static const struct option long_options[
   {"interactive", no_argument, NULL, 'i'},
   {"load-module", required_argument, NULL, 'm'},
   {"module-directory", required_argument, NULL, 'M'},
-  {"nesting-limit", required_argument, NULL, 'L'},
+  {OPT_STR_INIT ("nesting-limit"), required_argument, NULL, 'L'},
   {"posix", no_argument, NULL, 'G'},
   {"prefix-builtins", no_argument, NULL, 'P'},
   {"pushdef", required_argument, NULL, 'p'},
@@ -247,7 +272,7 @@ static const struct option long_options[
   {"undefine", required_argument, NULL, 'U'},
   {"warnings", no_argument, NULL, 'W'},
 
-  {"arglength", required_argument, NULL, ARGLENGTH_OPTION},
+  {OPT_STR_INIT ("arglength"), required_argument, NULL, ARGLENGTH_OPTION},
   {"debugfile", required_argument, NULL, DEBUGFILE_OPTION},
   {"diversions", required_argument, NULL, DIVERSIONS_OPTION},
   {"hashsize", required_argument, NULL, HASHSIZE_OPTION},
@@ -282,17 +307,17 @@ enum interactive_choice
   INTERACTIVE_NO       /* -b specified last */
 };
 
-/* Convert OPT to size_t, reporting an error using MSGID if it does
-   not fit.  */
+/* Convert OPT to size_t, reporting an error using long option index
+   OI or short option character OPTCHAR if it does not fit.  */
 static size_t
-size_opt (char const *opt, char const *msgid)
+size_opt (char const *opt, int oi, int optchar)
 {
   unsigned long int size;
   strtol_error status = xstrtoul (opt, NULL, 10, &size, "kKmMgGtTPEZY0");
   if (SIZE_MAX < size && status == LONGINT_OK)
     status = LONGINT_OVERFLOW;
   if (status != LONGINT_OK)
-    STRTOL_FATAL_ERROR (opt, _(msgid), status);
+    STRTOL_FATAL_ERROR (OPT_STR (oi, optchar, long_options), opt, status);
   return size;
 }
 
@@ -326,7 +351,6 @@ main (int argc, char *const *argv, char 
   deferred *head = NULL;       /* head of deferred argument list */
   deferred *tail = NULL;
   deferred *defn;
-  int optchar;                 /* option character */
   size_t size;                 /* for parsing numeric option arguments */
 
   bool import_environment = false; /* true to import environment */
@@ -368,8 +392,14 @@ main (int argc, char *const *argv, char 
   /* First, we decode the arguments, to size up tables and stuff.
      Avoid lasting side effects; for example 'm4 --debugfile=oops
      --help' must not create the file `oops'.  */
-  while ((optchar = getopt_long (argc, (char **) argv, OPTSTRING,
-                                long_options, NULL)) != -1)
+  while (1)
+    {
+      int oi = -1;
+      int optchar = getopt_long (argc, (char **) argv, OPTSTRING,
+                                long_options, &oi);
+      if (optchar == -1)
+       break;
+
     switch (optchar)
       {
       default:
@@ -476,7 +506,7 @@ main (int argc, char *const *argv, char 
        break;
 
       case 'L':
-       size = size_opt (optarg, N_("nesting limit"));
+         size = size_opt (optarg, oi, optchar);
        m4_set_nesting_limit_opt (context, size);
        break;
 
@@ -552,8 +582,7 @@ main (int argc, char *const *argv, char 
               "--arglength", "--debuglen");
        /* fall through */
       case 'l':
-       size = size_opt (optarg,
-                        N_("debug argument length"));
+         size = size_opt (optarg, oi, optchar);
        m4_set_max_debug_arg_length_opt (context, size);
        break;
 
@@ -590,6 +619,7 @@ main (int argc, char *const *argv, char 
        usage (EXIT_SUCCESS);
        break;
       }
+    }
 
   /* Interactive if specified, or if no input files and stdin and
      stderr are terminals, to match sh behavior.  Interactive mode
Index: tests/options.at
===================================================================
RCS file: /sources/m4/m4/tests/options.at,v
retrieving revision 1.28
diff -u -p -b -r1.28 options.at
--- tests/options.at    7 Aug 2007 03:15:32 -0000       1.28
+++ tests/options.at    7 Aug 2007 20:07:35 -0000
@@ -310,15 +310,15 @@ echo(`long string')
 ]])
 
 AT_CHECK_M4([--debuglen=-1 in], [1], [],
-[[m4: invalid debug argument length `-1'
+[[m4: invalid --debuglen argument `-1'
 ]])
 
 AT_CHECK_M4([--debuglen oops in], [1], [],
-[[m4: invalid debug argument length `oops'
+[[m4: invalid --debuglen argument `oops'
 ]])
 
 AT_CHECK_M4([-l 10oops in], [1], [],
-[[m4: invalid character following debug argument length in `10oops'
+[[m4: invalid suffix in -l argument `10oops'
 ]])
 
 dnl MiB is the suffix to implict 1, resulting in 1048576
@@ -328,7 +328,15 @@ AT_CHECK_M4([-lMiB in], [0], [[long stri
 
 dnl this assumes size_t is no bigger than 64 bits
 AT_CHECK_M4([-l 123456789012345678901234567890 in], [1], [],
-[[m4: debug argument length `123456789012345678901234567890' too large
+[[m4: -l argument `123456789012345678901234567890' too large
+]])
+AT_CHECK_M4([--debugl 123456789012345678901234567890 in], [1], [],
+[[m4: --debuglen argument `123456789012345678901234567890' too large
+]])
+
+dnl per POSIX guidelines, this is a decimal number 10, not octal 8
+AT_CHECK_M4([-l 010 in], [0], [[long string
+]], [[m4trace: -1- echo(`long strin...') -> ``long stri...'
 ]])
 
 AT_CHECK_M4([-l 3 in], [0], [[long string
@@ -570,15 +578,15 @@ echo(echo(echo(echo(echo(echo(echo(echo(
 ]])
 
 AT_CHECK_M4([--nesting-limit=-1 in], [1], [],
-[[m4: invalid nesting limit `-1'
+[[m4: invalid --nesting-limit argument `-1'
 ]])
 
 AT_CHECK_M4([--nesting-limit oops in], [1], [],
-[[m4: invalid nesting limit `oops'
+[[m4: invalid --nesting-limit argument `oops'
 ]])
 
 AT_CHECK_M4([-L 10oops in], [1], [],
-[[m4: invalid character following nesting limit in `10oops'
+[[m4: invalid suffix in -L argument `10oops'
 ]])
 
 dnl MiB is the suffix to implict 1, resulting in 1048576
@@ -588,7 +596,10 @@ nested string
 
 dnl this assumes size_t is no bigger than 64 bits
 AT_CHECK_M4([-L 123456789012345678901234567890 in], [1], [],
-[[m4: nesting limit `123456789012345678901234567890' too large
+[[m4: -L argument `123456789012345678901234567890' too large
+]])
+AT_CHECK_M4([--nest 123456789012345678901234567890 in], [1], [],
+[[m4: --nesting-limit argument `123456789012345678901234567890' too large
 ]])
 
 AT_CHECK_M4([-L 5 in], [1], [[nested string






reply via email to

[Prev in Thread] Current Thread [Next in Thread]