]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Adapt to new human and xstrtol API.
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 4 Aug 2007 11:36:59 +0000 (13:36 +0200)
committerJim Meyering <jim@meyering.net>
Sat, 4 Aug 2007 11:36:59 +0000 (13:36 +0200)
* src/df.c (long_options): Prepend "--" to long options that
OPT_STR might decode.
* src/du.c (long_options): Likewise.
* src/od.c (long_options): Likewise.
* src/sort.c (long_options): Likewise.
* src/df.c (main): Adjust to new human and xstrtol API.
* src/du.c (main): Likewise.
* src/ls.c (decode_switches): Likewise.
* src/od.c (main): Likewise.
* src/pr.c (first_last_page): Likewise.  New argument OPTION.
All callers changed.
* src/sort.c (specify_sort_size): New arg OPTION.  All callers
changed.  Adjust to new xstrtol API.
* src/system.h (opt_str_storage): New static var.
(OPT_STR, LONG_OPT_STR, OPT_STR_INIT): New macros.

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

index ee5220e4d56a30946b03bda6f44d3248d8f51b1e..6c406e0bdc743aa2b790ec18106995cbb75b408d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2007-08-04  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Adapt to new human and xstrtol API.
+       * src/df.c (long_options): Prepend "--" to long options that
+       OPT_STR might decode.
+       * src/du.c (long_options): Likewise.
+       * src/od.c (long_options): Likewise.
+       * src/sort.c (long_options): Likewise.
+       * src/df.c (main): Adjust to new human and xstrtol API.
+       * src/du.c (main): Likewise.
+       * src/ls.c (decode_switches): Likewise.
+       * src/od.c (main): Likewise.
+       * src/pr.c (first_last_page): Likewise.  New argument OPTION.
+       All callers changed.
+       * src/sort.c (specify_sort_size): New arg OPTION.  All callers
+       changed.  Adjust to new xstrtol API.
+       * src/system.h (opt_str_storage): New static var.
+       (OPT_STR, LONG_OPT_STR, OPT_STR_INIT): New macros.
+
 2007-08-02  Jim Meyering  <jim@meyering.net>
 
        Adjust one more test to accommodate the recent fts change.
index 41bda87052906f5436d061376b35c93efd809e20..c2d1180b7a2503954374b5fc77448f8e8b6c1a02 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'},
-  {"block-size", required_argument, NULL, 'B'},
+  {OPT_STR_INIT ("block-size"), required_argument, NULL, 'B'},
   {"inodes", no_argument, NULL, 'i'},
   {"human-readable", no_argument, NULL, 'h'},
   {"si", no_argument, NULL, 'H'},
@@ -776,7 +776,6 @@ kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
 int
 main (int argc, char **argv)
 {
-  int c;
   struct stat *stats IF_LINT (= 0);
 
   initialize_main (&argc, &argv);
@@ -798,16 +797,26 @@ main (int argc, char **argv)
   posix_format = false;
   exit_status = EXIT_SUCCESS;
 
-  while ((c = getopt_long (argc, argv, "aB:iF:hHklmPTt:vx:", long_options, NULL))
-        != -1)
+  for (;;)
     {
+      int oi = -1;
+      int c = getopt_long (argc, argv, "aB:iF:hHklmPTt:vx:", long_options,
+                          &oi);
+      if (c == -1)
+       break;
+
       switch (c)
        {
        case 'a':
          show_all_fs = true;
          break;
        case 'B':
-         human_output_opts = human_options (optarg, true, &output_block_size);
+         {
+           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);
+         }
          break;
        case 'i':
          inode_format = true;
@@ -873,8 +882,8 @@ main (int argc, char **argv)
          output_block_size = (getenv ("POSIXLY_CORRECT") ? 512 : 1024);
        }
       else
-       human_output_opts = human_options (getenv ("DF_BLOCK_SIZE"), false,
-                                          &output_block_size);
+       human_options (getenv ("DF_BLOCK_SIZE"),
+                      &human_output_opts, &output_block_size);
     }
 
   /* Fail if the same file system type was both selected and excluded.  */
index f9bd2e3647281afe5ba1ec520b87398d4aa1e70d..caacbb0bd35016510ef1a5ac0a68923a0316e608 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},
-  {"block-size", required_argument, NULL, 'B'},
+  {OPT_STR_INIT ("block-size"), required_argument, NULL, 'B'},
   {"bytes", no_argument, NULL, 'b'},
   {"count-links", no_argument, NULL, 'l'},
   {"dereference", no_argument, NULL, 'L'},
@@ -661,7 +661,6 @@ du_files (char **files, int bit_flags)
 int
 main (int argc, char **argv)
 {
-  int c;
   char *cwd_only[2];
   bool max_depth_specified = false;
   char **files;
@@ -692,12 +691,17 @@ main (int argc, char **argv)
 
   exclude = new_exclude ();
 
-  human_output_opts = human_options (getenv ("DU_BLOCK_SIZE"), false,
-                                    &output_block_size);
+  human_options (getenv ("DU_BLOCK_SIZE"),
+                &human_output_opts, &output_block_size);
 
-  while ((c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
-                          long_options, NULL)) != -1)
+  for (;;)
     {
+      int oi = -1;
+      int c = getopt_long (argc, argv, DEBUG_OPT "0abchHklmsxB:DLPSX:",
+                          long_options, &oi);
+      if (c == -1)
+       break;
+
       switch (c)
        {
 #if DU_DEBUG
@@ -788,7 +792,12 @@ main (int argc, char **argv)
          break;
 
        case 'B':
-         human_output_opts = human_options (optarg, true, &output_block_size);
+         {
+           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);
+         }
          break;
 
        case 'D': /* This will eventually be 'H' (-H), too.  */
index ee736227e14e51f13809535e5d657e140b460599..064a51f743e46174d0a752c40e0f04944e64408a 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -1450,8 +1450,8 @@ decode_switches (int argc, char **argv)
 
   {
     char const *ls_block_size = getenv ("LS_BLOCK_SIZE");
-    human_output_opts = human_options (ls_block_size, false,
-                                      &output_block_size);
+    human_options (ls_block_size,
+                  &human_output_opts, &output_block_size);
     if (ls_block_size || getenv ("BLOCK_SIZE"))
       file_output_block_size = output_block_size;
   }
@@ -1793,8 +1793,13 @@ decode_switches (int argc, char **argv)
          break;
 
        case BLOCK_SIZE_OPTION:
-         human_output_opts = human_options (optarg, true, &output_block_size);
-         file_output_block_size = output_block_size;
+         {
+           enum strtol_error e = human_options (optarg, &human_output_opts,
+                                                &output_block_size);
+           if (e != LONGINT_OK)
+             STRTOL_FATAL_ERROR ("--block-size", optarg, e);
+           file_output_block_size = output_block_size;
+         }
          break;
 
        case SI_OPTION:
index 3f2159ee8d73704e2a08b722d8e07a8210075a81..472c513e3cfbf96cc58380a66402918479cf7290 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -1,5 +1,5 @@
 /* od -- dump files in octal and other formats
-   Copyright (C) 92, 1995-2006 Free Software Foundation, Inc.
+   Copyright (C) 92, 1995-2007 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -281,14 +281,14 @@ enum
 
 static struct option const long_options[] =
 {
-  {"skip-bytes", required_argument, NULL, 'j'},
+  {OPT_STR_INIT ("skip-bytes"), required_argument, NULL, 'j'},
   {"address-radix", required_argument, NULL, 'A'},
-  {"read-bytes", required_argument, NULL, 'N'},
+  {OPT_STR_INIT ("read-bytes"), required_argument, NULL, 'N'},
   {"format", required_argument, NULL, 't'},
   {"output-duplicates", no_argument, NULL, 'v'},
-  {"strings", optional_argument, NULL, 'S'},
+  {OPT_STR_INIT ("strings"), optional_argument, NULL, 'S'},
   {"traditional", no_argument, NULL, TRADITIONAL_OPTION},
-  {"width", optional_argument, NULL, 'w'},
+  {OPT_STR_INIT ("width"), optional_argument, NULL, 'w'},
 
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
@@ -1555,7 +1555,6 @@ dump_strings (void)
 int
 main (int argc, char **argv)
 {
-  int c;
   int n_files;
   size_t i;
   int l_c_m;
@@ -1609,11 +1608,14 @@ main (int argc, char **argv)
   address_pad_len = 7;
   flag_dump_strings = false;
 
-  while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
-        != -1)
+  for (;;)
     {
       uintmax_t tmp;
       enum strtol_error s_err;
+      int oi = -1;
+      int c = getopt_long (argc, argv, short_options, long_options, &oi);
+      if (c == -1)
+        break;
 
       switch (c)
        {
@@ -1653,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 (optarg, _("skip argument"), s_err);
+           STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, s_err);
          break;
 
        case 'N':
@@ -1663,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 (optarg, _("limit argument"), s_err);
+           STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg, s_err);
          break;
 
        case 'S':
@@ -1674,7 +1676,8 @@ it must be one character from [doxn]"),
            {
              s_err = xstrtoumax (optarg, NULL, 0, &tmp, multipliers);
              if (s_err != LONGINT_OK)
-               STRTOL_FATAL_ERROR (optarg, _("minimum string length"), s_err);
+               STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg,
+                                   s_err);
 
              /* The minimum string length may be no larger than SIZE_MAX,
                 since we may allocate a buffer of this size.  */
@@ -1746,7 +1749,8 @@ 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 (optarg, _("width specification"), s_err);
+               STRTOL_FATAL_ERROR (OPT_STR (oi, c, long_options), optarg,
+                                   s_err);
              if (SIZE_MAX < w_tmp)
                error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
              desired_width = w_tmp;
index f7ae015121fb4b11fc7a96c57ae757884400ab59..329183b995b635b7616f381fc6f5ab4b8f3e70b3 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 *pages)
+first_last_page (char const *option, 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_ERROR (EXIT_FAILURE, pages, _("page range"), err);
+    STRTOL_FATAL_ERROR (option, pages, err);
 
   if (p == pages || !first)
     return false;
@@ -813,7 +813,7 @@ first_last_page (char const *pages)
       char const *p1 = p + 1;
       err = xstrtoumax (p1, &p, 10, &last, "");
       if (err != LONGINT_OK)
-       _STRTOL_ERROR (EXIT_FAILURE, pages, _("page range"), err);
+       STRTOL_FATAL_ERROR (option, pages, err);
       if (p1 == p || last < first)
        return false;
     }
@@ -902,7 +902,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 ("+", optarg + 1)))
            file_names[n_files++] = optarg;
          break;
 
@@ -911,7 +911,7 @@ main (int argc, char **argv)
            if (! optarg)
              error (EXIT_FAILURE, 0,
                     _("`--pages=FIRST_PAGE[:LAST_PAGE]' missing argument"));
-           else if (! first_last_page (optarg))
+           else if (! first_last_page ("--pages", optarg))
              error (EXIT_FAILURE, 0, _("Invalid page range %s"),
                     quote (optarg));
            break;
index af23e844fe6a0d1fd5b73494a5c70fdce2d47477..dc7874a9efbe74a5f7051bcf5dfee3a29e55cd00 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'},
-  {"buffer-size", required_argument, NULL, 'S'},
+  {OPT_STR_INIT ("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 *s)
+specify_sort_size (char const *option, char const *s)
 {
   uintmax_t n;
   char *suffix;
@@ -1088,7 +1088,7 @@ specify_sort_size (char const *s)
       e = LONGINT_OVERFLOW;
     }
 
-  STRTOL_FATAL_ERROR (s, _("sort size"), e);
+  STRTOL_FATAL_ERROR (option, s, e);
 }
 
 /* Return the default sort size.  */
@@ -2842,6 +2842,7 @@ main (int argc, char **argv)
         pedantic and a file was seen, unless the POSIX version
         predates 1003.1-2001 and -c was not seen and the operand is
         "-o FILE" or "-oFILE".  */
+      int oi = -1;
 
       if (c == -1
          || (posixly_correct && nfiles != 0
@@ -2851,7 +2852,7 @@ main (int argc, char **argv)
                    && argv[optind][0] == '-' && argv[optind][1] == 'o'
                    && (argv[optind][2] || optind + 1 != argc)))
          || ((c = getopt_long (argc, argv, short_options,
-                               long_options, NULL))
+                               long_options, &oi))
              == -1))
        {
          if (argc <= optind)
@@ -3011,7 +3012,7 @@ main (int argc, char **argv)
          break;
 
        case 'S':
-         specify_sort_size (optarg);
+         specify_sort_size (OPT_STR (oi, c, long_options), optarg);
          break;
 
        case 't':
index 3c7f49d1010bcb01fced409eff2b129529ecc7f7..cc97f2fec1e63f49171cd25e5608b917b8ef086c 100644 (file)
@@ -592,3 +592,20 @@ 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_OPTION.
+   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.  */
+static char opt_str_storage[3] = {'-', 0, 0};
+#define OPT_STR(opt_idx, c, long_options)      \
+  ((opt_idx) < 0                               \
+   ? (opt_str_storage[1] = c, opt_str_storage) \
+   : 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)
+
+/* Define an option string that will be used with OPT_STR or LONG_OPT_STR.  */
+#define OPT_STR_INIT(name) ("--" name + 2)