Accommodate more xstrtol changes.
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 11 Aug 2007 06:30:43 +0000 (08:30 +0200)
committerJim Meyering <jim@meyering.net>
Sat, 11 Aug 2007 06:32:50 +0000 (08:32 +0200)
* src/df.c (long_options): Don't bother prepending "--" to long
options that OPT_STR might decode, as that hack is no longer needed.
(main): Invoke xstrtol_fatal rather than STRTOL_FATAL_ERROR.
* src/du.c (long_options, main): Likewise.
* src/ls.c (decode_switches): Likewise.
* src/od.c (long_options, main): Likewise.
* src/pr.c (first_last_page, main): Likewise.
* src/sort.c (long_options, specify_sort_size): Likewise.
* src/pr.c (first_last_page): Accept option index and option char
instead of an assembled option string.  All callers changed.
* src/sort.c (specify_sort_size): Likewise.
* src/system.h (OPT_STR, LONG_OPT_STR, short_opt_str, OPT_STR_INIT):
Remove.

ChangeLog
lib/.cvsignore
lib/.gitignore
src/df.c
src/du.c
src/ls.c
src/od.c
src/pr.c
src/sort.c
src/system.h

index df5ed6e..1cd7390 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2007-08-10  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Accommodate more xstrtol changes.
+       * src/df.c (long_options): Don't bother prepending "--" to long
+       options that OPT_STR might decode, as that hack is no longer needed.
+       (main): Invoke xstrtol_fatal rather than STRTOL_FATAL_ERROR.
+       * src/du.c (long_options, main): Likewise.
+       * src/ls.c (decode_switches): Likewise.
+       * src/od.c (long_options, main): Likewise.
+       * src/pr.c (first_last_page, main): Likewise.
+       * src/sort.c (long_options, specify_sort_size): Likewise.
+       * src/pr.c (first_last_page): Accept option index and option char
+       instead of an assembled option string.  All callers changed.
+       * src/sort.c (specify_sort_size): Likewise.
+       * src/system.h (OPT_STR, LONG_OPT_STR, short_opt_str, OPT_STR_INIT):
+       Remove.
+
 2007-08-05  Jim Meyering  <jim@meyering.net>
 
        Encapsulate a static variable.
index b4694ec..d16b937 100644 (file)
@@ -439,6 +439,7 @@ xstrndup.h
 xstrtod.c
 xstrtod.h
 xstrtoimax.c
+xstrtol-error.c
 xstrtol.c
 xstrtol.h
 xstrtold.c
index 67dd922..b81fee7 100644 (file)
@@ -430,6 +430,7 @@ xstrndup.h
 xstrtod.c
 xstrtod.h
 xstrtoimax.c
+xstrtol-error.c
 xstrtol.c
 xstrtol.h
 xstrtold.c
index c2d1180..94cf330 100644 (file)
--- a/src/df.c
+++ b/src/df.c
@@ -121,7 +121,7 @@ enum
 static struct option const long_options[] =
 {
   {"all", no_argument, NULL, 'a'},
-  {OPT_STR_INIT ("block-size"), required_argument, NULL, 'B'},
+  {"block-size", required_argument, NULL, 'B'},
   {"inodes", no_argument, NULL, 'i'},
   {"human-readable", no_argument, NULL, 'h'},
   {"si", no_argument, NULL, 'H'},
@@ -815,7 +815,7 @@ main (int argc, char **argv)
            enum strtol_error e = human_options (optarg, &human_output_opts,
                                                 &output_block_size);
            if (e != LONGINT_OK)
-             STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, e);
+             xstrtol_fatal (e, oi, c, long_options, optarg);
          }
          break;
        case 'i':
index caacbb0..6681079 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -204,7 +204,7 @@ static struct option const long_options[] =
 {
   {"all", no_argument, NULL, 'a'},
   {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
-  {OPT_STR_INIT ("block-size"), required_argument, NULL, 'B'},
+  {"block-size", required_argument, NULL, 'B'},
   {"bytes", no_argument, NULL, 'b'},
   {"count-links", no_argument, NULL, 'l'},
   {"dereference", no_argument, NULL, 'L'},
@@ -796,7 +796,7 @@ main (int argc, char **argv)
            enum strtol_error e = human_options (optarg, &human_output_opts,
                                                 &output_block_size);
            if (e != LONGINT_OK)
-             STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, e);
+             xstrtol_fatal (e, oi, c, long_options, optarg);
          }
          break;
 
index 064a51f..c6a15f6 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -1375,7 +1375,6 @@ main (int argc, char **argv)
 static int
 decode_switches (int argc, char **argv)
 {
-  int c;
   char *time_style_option = NULL;
 
   /* Record whether there is an option specifying sort type.  */
@@ -1506,10 +1505,15 @@ decode_switches (int argc, char **argv)
       }
   }
 
-  while ((c = getopt_long (argc, argv,
-                          "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1",
-                          long_options, NULL)) != -1)
+  for (;;)
     {
+      int oi = -1;
+      int c = getopt_long (argc, argv,
+                          "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1",
+                          long_options, &oi);
+      if (c == -1)
+       break;
+
       switch (c)
        {
        case 'a':
@@ -1797,7 +1801,7 @@ decode_switches (int argc, char **argv)
            enum strtol_error e = human_options (optarg, &human_output_opts,
                                                 &output_block_size);
            if (e != LONGINT_OK)
-             STRTOL_FATAL_ERROR ("--block-size", optarg, e);
+             xstrtol_fatal (e, oi, 0, long_options, optarg);
            file_output_block_size = output_block_size;
          }
          break;
index 472c513..1e77f92 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -281,14 +281,14 @@ enum
 
 static struct option const long_options[] =
 {
-  {OPT_STR_INIT ("skip-bytes"), required_argument, NULL, 'j'},
+  {"skip-bytes", required_argument, NULL, 'j'},
   {"address-radix", required_argument, NULL, 'A'},
-  {OPT_STR_INIT ("read-bytes"), required_argument, NULL, 'N'},
+  {"read-bytes", required_argument, NULL, 'N'},
   {"format", required_argument, NULL, 't'},
   {"output-duplicates", no_argument, NULL, 'v'},
-  {OPT_STR_INIT ("strings"), optional_argument, NULL, 'S'},
+  {"strings", optional_argument, NULL, 'S'},
   {"traditional", no_argument, NULL, TRADITIONAL_OPTION},
-  {OPT_STR_INIT ("width"), optional_argument, NULL, 'w'},
+  {"width", optional_argument, NULL, 'w'},
 
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
@@ -1655,7 +1655,7 @@ it must be one character from [doxn]"),
          modern = true;
          s_err = xstrtoumax (optarg, NULL, 0, &n_bytes_to_skip, multipliers);
          if (s_err != LONGINT_OK)
-           STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, s_err);
+           xstrtol_fatal (s_err, oi, c, long_options, optarg);
          break;
 
        case 'N':
@@ -1665,7 +1665,7 @@ it must be one character from [doxn]"),
          s_err = xstrtoumax (optarg, NULL, 0, &max_bytes_to_format,
                              multipliers);
          if (s_err != LONGINT_OK)
-           STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, s_err);
+           xstrtol_fatal (s_err, oi, c, long_options, optarg);
          break;
 
        case 'S':
@@ -1676,8 +1676,7 @@ it must be one character from [doxn]"),
            {
              s_err = xstrtoumax (optarg, NULL, 0, &tmp, multipliers);
              if (s_err != LONGINT_OK)
-               STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg,
-                                   s_err);
+               xstrtol_fatal (s_err, oi, c, long_options, optarg);
 
              /* The minimum string length may be no larger than SIZE_MAX,
                 since we may allocate a buffer of this size.  */
@@ -1749,8 +1748,7 @@ it must be one character from [doxn]"),
              uintmax_t w_tmp;
              s_err = xstrtoumax (optarg, NULL, 10, &w_tmp, "");
              if (s_err != LONGINT_OK)
-               STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg,
-                                   s_err);
+               xstrtol_fatal (s_err, oi, c, long_options, optarg);
              if (SIZE_MAX < w_tmp)
                error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
              desired_width = w_tmp;
index 329183b..1ed79d6 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
@@ -796,14 +796,14 @@ cols_ready_to_print (void)
    using option +FIRST_PAGE:LAST_PAGE */
 
 static bool
-first_last_page (char const *option, char const *pages)
+first_last_page (int oi, char c, char const *pages)
 {
   char *p;
   uintmax_t first;
   uintmax_t last = UINTMAX_MAX;
   strtol_error err = xstrtoumax (pages, &p, 10, &first, "");
   if (err != LONGINT_OK && err != LONGINT_INVALID_SUFFIX_CHAR)
-    STRTOL_FATAL_ERROR (option, pages, err);
+    xstrtol_fatal (err, oi, c, long_options, pages);
 
   if (p == pages || !first)
     return false;
@@ -813,7 +813,7 @@ first_last_page (char const *option, char const *pages)
       char const *p1 = p + 1;
       err = xstrtoumax (p1, &p, 10, &last, "");
       if (err != LONGINT_OK)
-       STRTOL_FATAL_ERROR (option, pages, err);
+       xstrtol_fatal (err, oi, c, long_options, pages);
       if (p1 == p || last < first)
        return false;
     }
@@ -856,7 +856,6 @@ separator_string (const char *optarg_S)
 int
 main (int argc, char **argv)
 {
-  int c;
   int n_files;
   bool old_options = false;
   bool old_w = false;
@@ -881,9 +880,13 @@ main (int argc, char **argv)
                ? xmalloc ((argc - 1) * sizeof (char *))
                : NULL);
 
-  while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
-        != -1)
+  for (;;)
     {
+      int oi = -1;
+      int c = getopt_long (argc, argv, short_options, long_options, &oi);
+      if (c == -1)
+       break;
+
       if (ISDIGIT (c))
        {
          /* Accumulate column-count digits specified via old-style options. */
@@ -902,7 +905,7 @@ main (int argc, char **argv)
        case 1:                 /* Non-option argument. */
          /* long option --page dominates old `+FIRST_PAGE ...'.  */
          if (! (first_page_number == 0
-                && *optarg == '+' && first_last_page ("+", optarg + 1)))
+                && *optarg == '+' && first_last_page (-2, '+', optarg + 1)))
            file_names[n_files++] = optarg;
          break;
 
@@ -911,7 +914,7 @@ main (int argc, char **argv)
            if (! optarg)
              error (EXIT_FAILURE, 0,
                     _("`--pages=FIRST_PAGE[:LAST_PAGE]' missing argument"));
-           else if (! first_last_page ("--pages", optarg))
+           else if (! first_last_page (oi, 0, optarg))
              error (EXIT_FAILURE, 0, _("Invalid page range %s"),
                     quote (optarg));
            break;
index dc7874a..620a4bc 100644 (file)
@@ -414,7 +414,7 @@ static struct option const long_options[] =
   {"output", required_argument, NULL, 'o'},
   {"reverse", no_argument, NULL, 'r'},
   {"stable", no_argument, NULL, 's'},
-  {OPT_STR_INIT ("buffer-size"), required_argument, NULL, 'S'},
+  {"buffer-size", required_argument, NULL, 'S'},
   {"field-separator", required_argument, NULL, 't'},
   {"temporary-directory", required_argument, NULL, 'T'},
   {"unique", no_argument, NULL, 'u'},
@@ -1032,7 +1032,7 @@ inittables (void)
 
 /* Specify the amount of main memory to use when sorting.  */
 static void
-specify_sort_size (char const *option, char const *s)
+specify_sort_size (int oi, char c, char const *s)
 {
   uintmax_t n;
   char *suffix;
@@ -1088,7 +1088,7 @@ specify_sort_size (char const *option, char const *s)
       e = LONGINT_OVERFLOW;
     }
 
-  STRTOL_FATAL_ERROR (option, s, e);
+  xstrtol_fatal (e, oi, c, long_options, s);
 }
 
 /* Return the default sort size.  */
@@ -3012,7 +3012,7 @@ main (int argc, char **argv)
          break;
 
        case 'S':
-         specify_sort_size (OPT_STR (oi, c, long_options), optarg);
+         specify_sort_size (oi, c, optarg);
          break;
 
        case 't':
index dcb13ba..3c7f49d 100644 (file)
@@ -592,28 +592,3 @@ emit_bug_reporting_address (void)
      bugs (typically your translation team's web or email address).  */
   printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
 }
-
-/* 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)