bug-gnulib
[Top][All Lists]
Advanced

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

Re: xstrtol.h


From: Jim Meyering
Subject: Re: xstrtol.h
Date: Sun, 22 Jul 2007 19:24:32 +0200

Jim Meyering <address@hidden> wrote:
> Jim Meyering <address@hidden> wrote:
>> Here's the xstrtol.h change, then the ones for coreutils:

Here's a better patch:

I have to initialize getopt_long's final parameter each and every time.
Otherwise, I'd get invalid results like this:

    $ ./od --skip 1 -N -1
    ./od: invalid --skip-bytes argument `-1'


---
 ChangeLog    |   13 +++++++++++++
 src/od.c     |   21 +++++++++++++--------
 src/pr.c     |    4 ++--
 src/sort.c   |    9 +++++----
 src/system.h |   23 +++++++++++++++++++++++
 5 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3b144b3..b9449ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2007-07-22  Jim Meyering  <address@hidden>
+
+       Make some invalid-integer diagnostics easier to translate properly.
+       * src/system.h (OPT_STR): Define.
+       (opt_str): New function.
+       * src/od.c (main): Declare new local, oi.
+       Set it via the call to getopt_long.
+       Use its value to build the option string we pass to STRTOL_FATAL_ERROR.
+       * src/pr.c (first_last_page): Use the not-translated string, "--pages"
+       in diagnostics about invalid arguments.
+       * src/sort.c (specify_sort_size): Add a parameter.  Update callers.
+       (main): Use new macro to pass required string to specify_sort_size.
+
 2007-07-21  Paul Eggert  <address@hidden>

        * bootstrap (slurp): Work even in environments where "ls" defaults
diff --git a/src/od.c b/src/od.c
index c2d419b..1d8fda9 100644
--- 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
@@ -1556,7 +1556,6 @@ dump_strings (void)
 int
 main (int argc, char **argv)
 {
-  int c;
   int n_files;
   size_t i;
   int l_c_m;
@@ -1610,11 +1609,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)
+  while (true)
     {
       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)
        {
@@ -1654,7 +1656,8 @@ 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 (optarg,
+                               OPT_STR (oi, c, long_options), s_err);
          break;

        case 'N':
@@ -1664,7 +1667,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 (optarg, OPT_STR (oi, c, long_options), s_err);
          break;

        case 'S':
@@ -1675,7 +1678,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 (optarg,
+                                   OPT_STR (oi, c, long_options), s_err);

              /* The minimum string length may be no larger than SIZE_MAX,
                 since we may allocate a buffer of this size.  */
@@ -1747,7 +1751,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 (optarg,
+                                   OPT_STR (oi, c, long_options), s_err);
              if (SIZE_MAX < w_tmp)
                error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
              desired_width = w_tmp;
diff --git a/src/pr.c b/src/pr.c
index 8a65ca0..ca75226 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -804,7 +804,7 @@ first_last_page (char const *pages)
   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_ERROR (EXIT_FAILURE, pages, "--pages", err);

   if (p == pages || !first)
     return false;
@@ -814,7 +814,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_ERROR (EXIT_FAILURE, pages, "--pages", err);
       if (p1 == p || last < first)
        return false;
     }
diff --git a/src/sort.c b/src/sort.c
index 824dd0d..874b188 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1033,7 +1033,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 *s, char const *option_name)
 {
   uintmax_t n;
   char *suffix;
@@ -1089,7 +1089,7 @@ specify_sort_size (char const *s)
       e = LONGINT_OVERFLOW;
     }

-  STRTOL_FATAL_ERROR (s, _("sort size"), e);
+  STRTOL_FATAL_ERROR (s, option_name, e);
 }

 /* Return the default sort size.  */
@@ -2834,6 +2834,7 @@ main (int argc, char **argv)

   for (;;)
     {
+      int oi = -1;
       /* Parse an operand as a file after "--" was seen; or if
         pedantic and a file was seen, unless the POSIX version
         predates 1003.1-2001 and -c was not seen and the operand is
@@ -2847,7 +2848,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)
@@ -3007,7 +3008,7 @@ main (int argc, char **argv)
          break;

        case 'S':
-         specify_sort_size (optarg);
+         specify_sort_size (optarg, OPT_STR (oi, c, long_options));
          break;

        case 't':
diff --git a/src/system.h b/src/system.h
index 79b1d47..20eee2e 100644
--- a/src/system.h
+++ b/src/system.h
@@ -593,3 +593,26 @@ emit_bug_reporting_address (void)
      bugs (typically your translation team's web or email address).  */
   printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
 }
+
+#define OPT_STR(opt_idx, c, long_options) \
+  opt_str (c, ((opt_idx) < 0 ? NULL : (long_options)[opt_idx].name))
+
+/* FIXME: comment. */
+static inline char *
+opt_str (int c, char const *long_name)
+{
+  static char s[80];
+  s[0] = '-';
+  if (long_name == NULL)
+    {
+      s[1] = c;
+      s[2] = '\0';
+    }
+  else
+    {
+      s[1] = '-';
+      strncpy (s + 2, long_name, sizeof s - 2);
+      s[sizeof s - 1] = '\0';
+    }
+  return s;
+}
--
1.5.3.rc2.22.g69a9b




reply via email to

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