Move xstrtol messages into gnulib domain, when --pobase is used.
authorEric Blake <ebb9@byu.net>
Wed, 8 Aug 2007 12:45:54 +0000 (12:45 +0000)
committerEric Blake <ebb9@byu.net>
Wed, 8 Aug 2007 12:45:54 +0000 (12:45 +0000)
* lib/xstrtol.h (_STRTOL_ERROR): Move messages out of macro...
* lib/xstrtol-error.c (xstrtol_error): ...into new file.
* modules/xstrtol (Files): Distribute new file.
* m4/xstrtol.m4 (gl_XSTRTOL): Build new file.
* lib/xstrtol.c (TESTING_XSTRTO): Move tests...
* tests/test-xstrtol.c: ...into new file.
* tests/test-xstrtoul.c: Also test xstrtoul.
* tests/test-xstrtoimax.c: Also test xstrtoimax.
* tests/test-xstrtoumax.c: Also test xstrtoumax.
* tests/test-xstrtol.sh: Drive the tests.
* tests/test-xstrtoimax.sh: Likewise.
* tests/test-xstrtoumax.sh: Likewise.
* modules/xstrtol-tests: New module.
* modules/xstrtoimax-tests: Likewise.
* modules/xstrtoumax-tests: Likewise.

16 files changed:
ChangeLog
lib/xstrtol-error.c [new file with mode: 0644]
lib/xstrtol.c
lib/xstrtol.h
m4/xstrtol.m4
modules/xstrtoimax-tests [new file with mode: 0644]
modules/xstrtol
modules/xstrtol-tests [new file with mode: 0644]
modules/xstrtoumax-tests [new file with mode: 0644]
tests/test-xstrtoimax.c [new file with mode: 0644]
tests/test-xstrtoimax.sh [new file with mode: 0755]
tests/test-xstrtol.c [new file with mode: 0644]
tests/test-xstrtol.sh [new file with mode: 0755]
tests/test-xstrtoul.c [new file with mode: 0644]
tests/test-xstrtoumax.c [new file with mode: 0644]
tests/test-xstrtoumax.sh [new file with mode: 0755]

index 2f091d4..2f403f2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2007-08-08  Eric Blake  <ebb9@byu.net>
+
+       Move xstrtol messages into gnulib domain, when --pobase is used.
+       * lib/xstrtol.h (_STRTOL_ERROR): Move messages out of macro...
+       * lib/xstrtol-error.c (xstrtol_error): ...into new file.
+       * modules/xstrtol (Files): Distribute new file.
+       * m4/xstrtol.m4 (gl_XSTRTOL): Build new file.
+       * lib/xstrtol.c (TESTING_XSTRTO): Move tests...
+       * tests/test-xstrtol.c: ...into new file.
+       * tests/test-xstrtoul.c: Also test xstrtoul.
+       * tests/test-xstrtoimax.c: Also test xstrtoimax.
+       * tests/test-xstrtoumax.c: Also test xstrtoumax.
+       * tests/test-xstrtol.sh: Drive the tests.
+       * tests/test-xstrtoimax.sh: Likewise.
+       * tests/test-xstrtoumax.sh: Likewise.
+       * modules/xstrtol-tests: New module.
+       * modules/xstrtoimax-tests: Likewise.
+       * modules/xstrtoumax-tests: Likewise.
+
 2007-08-08  Jim Meyering  <jim@meyering.net>
 
        New function: mfile_name_concat.
diff --git a/lib/xstrtol-error.c b/lib/xstrtol-error.c
new file mode 100644 (file)
index 0000000..5316fc0
--- /dev/null
@@ -0,0 +1,59 @@
+/* A more useful interface to strtol.
+
+   Copyright (C) 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004, 2006, 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
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#include <config.h>
+#include "xstrtol.h"
+
+#include "error.h"
+#include "gettext.h"
+
+#define _(str) gettext (str)
+
+/* Report an error for an out-of-range integer argument.
+   EXIT_CODE is the exit code (0 for a non-fatal error).
+   OPTION is the option that takes the argument
+    (usually starting with one or two minus signs).
+   ARG is the option's argument.
+   ERR is the error code returned by one of the xstrto* functions.  */
+void
+xstrtol_error (int exit_code, char const *option, char const *arg,
+              strtol_error err)
+{
+  switch (err)
+    {
+    default:
+      abort ();
+
+    case LONGINT_INVALID:
+      error (exit_code, 0, _("invalid %s argument `%s'"),
+            option, arg);
+      break;
+
+    case LONGINT_INVALID_SUFFIX_CHAR:
+    case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW:
+      error (exit_code, 0, _("invalid suffix in %s argument `%s'"),
+            option, arg);
+      break;
+
+    case LONGINT_OVERFLOW:
+      error (exit_code, 0, _("%s argument `%s' too large"),
+            option, arg);
+      break;
+    }
+}
index d57b3da..ed5bcdd 100644 (file)
@@ -227,37 +227,3 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
   *val = tmp;
   return err;
 }
-
-#ifdef TESTING_XSTRTO
-
-# include <stdio.h>
-# include "error.h"
-
-char *program_name;
-
-int
-main (int argc, char **argv)
-{
-  strtol_error s_err;
-  int i;
-
-  program_name = argv[0];
-  for (i=1; i<argc; i++)
-    {
-      char *p;
-      __strtol_t val;
-
-      s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
-      if (s_err == LONGINT_OK)
-       {
-         printf ("%s->%lu (%s)\n", argv[i], val, p);
-       }
-      else
-       {
-         STRTOL_FATAL_ERROR ("arg", argv[i], s_err);
-       }
-    }
-  exit (0);
-}
-
-#endif /* TESTING_XSTRTO */
index 4df140c..96fc4d2 100644 (file)
@@ -24,8 +24,6 @@
 
 # include <inttypes.h>
 
-# include "gettext.h"
-
 # ifndef _STRTOL_ERROR
 enum strtol_error
   {
@@ -56,35 +54,10 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
     (usually starting with one or two minus signs).
    ARG is the option's argument.
    ERR is the error code returned by one of the xstrto* functions.  */
-# define _STRTOL_ERROR(Exit_code, Option, Arg, Err)                    \
-  do                                                                   \
-    {                                                                  \
-      switch ((Err))                                                   \
-       {                                                               \
-       default:                                                        \
-         abort ();                                                     \
-                                                                       \
-       case LONGINT_INVALID:                                           \
-         error (Exit_code, 0, gettext ("invalid %s argument `%s'"),    \
-                Option, Arg);                                          \
-         break;                                                        \
-                                                                       \
-       case LONGINT_INVALID_SUFFIX_CHAR:                               \
-       case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW:            \
-         error ((Exit_code), 0,                                        \
-                gettext ("invalid suffix in %s argument `%s'"),        \
-                Option, Arg);                                          \
-         break;                                                        \
-                                                                       \
-       case LONGINT_OVERFLOW:                                          \
-         error (Exit_code, 0, gettext ("%s argument `%s' too large"),  \
-                Option, Arg);                                          \
-         break;                                                        \
-       }                                                               \
-    }                                                                  \
-  while (0)
+void xstrtol_error (int exit_code, char const *option, char const *arg,
+                   strtol_error err);
 
 # define STRTOL_FATAL_ERROR(Option, Arg, Err)                          \
-  _STRTOL_ERROR (exit_failure, Option, Arg, Err)
+  xstrtol_error (exit_failure, Option, Arg, Err)
 
 #endif /* not XSTRTOL_H_ */
index cd5a549..1fdcf71 100644 (file)
@@ -1,5 +1,6 @@
-#serial 9
-dnl Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+#serial 10
+dnl Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
+dnl Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
@@ -8,4 +9,5 @@ AC_DEFUN([gl_XSTRTOL],
 [
   AC_LIBOBJ([xstrtol])
   AC_LIBOBJ([xstrtoul])
+  AC_LIBOBJ([xstrtol-error])
 ])
diff --git a/modules/xstrtoimax-tests b/modules/xstrtoimax-tests
new file mode 100644 (file)
index 0000000..a856317
--- /dev/null
@@ -0,0 +1,14 @@
+Files:
+tests/test-xstrtoimax.c
+tests/test-xstrtoimax.sh
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-xstrtoimax.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@'
+check_PROGRAMS += test-xstrtoimax
+EXTRA_DIST += test-xstrtoimax.sh
+test_xstrtoimax_LDADD = $(LDADD) @LIBINTL@
index 0d136bf..cf48d8e 100644 (file)
@@ -5,6 +5,7 @@ Files:
 lib/xstrtol.h
 lib/xstrtol.c
 lib/xstrtoul.c
+lib/xstrtol-error.c
 m4/xstrtol.m4
 
 Depends-on:
diff --git a/modules/xstrtol-tests b/modules/xstrtol-tests
new file mode 100644 (file)
index 0000000..91ff626
--- /dev/null
@@ -0,0 +1,16 @@
+Files:
+tests/test-xstrtol.c
+tests/test-xstrtoul.c
+tests/test-xstrtol.sh
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-xstrtol.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@'
+check_PROGRAMS += test-xstrtol test-xstrtoul
+EXTRA_DIST += test-xstrtol.sh
+test_xstrtol_LDADD = $(LDADD) @LIBINTL@
+test_xstrtoul_LDADD = $(LDADD) @LIBINTL@
diff --git a/modules/xstrtoumax-tests b/modules/xstrtoumax-tests
new file mode 100644 (file)
index 0000000..030dcf8
--- /dev/null
@@ -0,0 +1,14 @@
+Files:
+tests/test-xstrtoumax.c
+tests/test-xstrtoumax.sh
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-xstrtoumax.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@'
+check_PROGRAMS += test-xstrtoumax
+EXTRA_DIST += test-xstrtoumax.sh
+test_xstrtoumax_LDADD = $(LDADD) @LIBINTL@
diff --git a/tests/test-xstrtoimax.c b/tests/test-xstrtoimax.c
new file mode 100644 (file)
index 0000000..cc2f6ae
--- /dev/null
@@ -0,0 +1,4 @@
+#define __xstrtol xstrtoimax
+#define __strtol_t intmax_t
+#define __spec PRIdMAX
+#include "test-xstrtol.c"
diff --git a/tests/test-xstrtoimax.sh b/tests/test-xstrtoimax.sh
new file mode 100755 (executable)
index 0000000..d053751
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="t-xstrtoimax.tmp t-xstrtoimax.xo"
+: > t-xstrtoimax.tmp
+too_big=99999999999999999999999999999999999999999999999999999999999999999999
+result=0
+
+# test xstrtoimax
+./test-xstrtoimax${EXEEXT} 1 >> t-xstrtoimax.tmp 2>&1 || result=1
+./test-xstrtoimax${EXEEXT} -1 >> t-xstrtoimax.tmp 2>&1 || result=1
+./test-xstrtoimax${EXEEXT} 1k >> t-xstrtoimax.tmp 2>&1 || result=1
+./test-xstrtoimax${EXEEXT} ${too_big}h >> t-xstrtoimax.tmp 2>&1 && result=1
+./test-xstrtoimax${EXEEXT} $too_big >> t-xstrtoimax.tmp 2>&1 && result=1
+./test-xstrtoimax${EXEEXT} x >> t-xstrtoimax.tmp 2>&1 && result=1
+./test-xstrtoimax${EXEEXT} 9x >> t-xstrtoimax.tmp 2>&1 && result=1
+./test-xstrtoimax${EXEEXT} 010 >> t-xstrtoimax.tmp 2>&1 || result=1
+./test-xstrtoimax${EXEEXT} MiB >> t-xstrtoimax.tmp 2>&1 || result=1
+
+# normalize output
+sed -e 's/^[^:]*: //' < t-xstrtoimax.tmp > t-xstrtoimax.xo
+mv t-xstrtoimax.xo t-xstrtoimax.tmp
+
+# compare expected output
+cat > t-xstrtoimax.xo <<EOF
+1->1 ()
+-1->-1 ()
+1k->1024 ()
+invalid suffix in arg argument \`${too_big}h'
+arg argument \`$too_big' too large
+invalid arg argument \`x'
+invalid suffix in arg argument \`9x'
+010->8 ()
+MiB->1048576 ()
+EOF
+
+diff t-xstrtoimax.xo t-xstrtoimax.tmp || result=1
+
+rm -fr $tmpfiles
+
+exit $result
diff --git a/tests/test-xstrtol.c b/tests/test-xstrtol.c
new file mode 100644 (file)
index 0000000..5f09452
--- /dev/null
@@ -0,0 +1,59 @@
+/* Test of xstrtol module.
+   Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
+   2006, 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
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#include <config.h>
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "xstrtol.h"
+#include "error.h"
+
+#ifndef __xstrtol
+# define __xstrtol xstrtol
+# define __strtol_t long int
+# define __spec "ld"
+#endif
+
+char *program_name;
+
+int
+main (int argc, char **argv)
+{
+  strtol_error s_err;
+  int i;
+
+  program_name = argv[0];
+  for (i = 1; i < argc; i++)
+    {
+      char *p;
+      __strtol_t val;
+
+      s_err = __xstrtol (argv[i], &p, 0, &val, "bckMw0");
+      if (s_err == LONGINT_OK)
+       {
+         printf ("%s->%" __spec " (%s)\n", argv[i], val, p);
+       }
+      else
+       {
+         STRTOL_FATAL_ERROR ("arg", argv[i], s_err);
+       }
+    }
+  exit (0);
+}
diff --git a/tests/test-xstrtol.sh b/tests/test-xstrtol.sh
new file mode 100755 (executable)
index 0000000..7bf8833
--- /dev/null
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="t-xstrtol.tmp t-xstrtol.xo"
+: > t-xstrtol.tmp
+too_big=99999999999999999999999999999999999999999999999999999999999999999999
+result=0
+
+# test xstrtol
+./test-xstrtol${EXEEXT} 1 >> t-xstrtol.tmp 2>&1 || result=1
+./test-xstrtol${EXEEXT} -1 >> t-xstrtol.tmp 2>&1 || result=1
+./test-xstrtol${EXEEXT} 1k >> t-xstrtol.tmp 2>&1 || result=1
+./test-xstrtol${EXEEXT} ${too_big}h >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtol${EXEEXT} $too_big >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtol${EXEEXT} x >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtol${EXEEXT} 9x >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtol${EXEEXT} 010 >> t-xstrtol.tmp 2>&1 || result=1
+# suffix without integer is valid
+./test-xstrtol${EXEEXT} MiB >> t-xstrtol.tmp 2>&1 || result=1
+
+# test xstrtoul
+./test-xstrtoul${EXEEXT} 1 >> t-xstrtol.tmp 2>&1 || result=1
+./test-xstrtoul${EXEEXT} -1 >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtoul${EXEEXT} 1k >> t-xstrtol.tmp 2>&1 || result=1
+./test-xstrtoul${EXEEXT} ${too_big}h >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtoul${EXEEXT} $too_big >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtoul${EXEEXT} x >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtoul${EXEEXT} 9x >> t-xstrtol.tmp 2>&1 && result=1
+./test-xstrtoul${EXEEXT} 010 >> t-xstrtol.tmp 2>&1 || result=1
+./test-xstrtoul${EXEEXT} MiB >> t-xstrtol.tmp 2>&1 || result=1
+
+# normalize output
+sed -e 's/^[^:]*: //' < t-xstrtol.tmp > t-xstrtol.xo
+mv t-xstrtol.xo t-xstrtol.tmp
+
+# compare expected output
+cat > t-xstrtol.xo <<EOF
+1->1 ()
+-1->-1 ()
+1k->1024 ()
+invalid suffix in arg argument \`${too_big}h'
+arg argument \`$too_big' too large
+invalid arg argument \`x'
+invalid suffix in arg argument \`9x'
+010->8 ()
+MiB->1048576 ()
+1->1 ()
+invalid arg argument \`-1'
+1k->1024 ()
+invalid suffix in arg argument \`${too_big}h'
+arg argument \`$too_big' too large
+invalid arg argument \`x'
+invalid suffix in arg argument \`9x'
+010->8 ()
+MiB->1048576 ()
+EOF
+
+diff t-xstrtol.xo t-xstrtol.tmp || result=1
+
+rm -fr $tmpfiles
+
+exit $result
diff --git a/tests/test-xstrtoul.c b/tests/test-xstrtoul.c
new file mode 100644 (file)
index 0000000..9dda9ee
--- /dev/null
@@ -0,0 +1,4 @@
+#define __xstrtol xstrtoul
+#define __strtol_t unsigned long int
+#define __spec "lu"
+#include "test-xstrtol.c"
diff --git a/tests/test-xstrtoumax.c b/tests/test-xstrtoumax.c
new file mode 100644 (file)
index 0000000..2e68c2e
--- /dev/null
@@ -0,0 +1,4 @@
+#define __xstrtol xstrtoumax
+#define __strtol_t uintmax_t
+#define __spec PRIuMAX
+#include "test-xstrtol.c"
diff --git a/tests/test-xstrtoumax.sh b/tests/test-xstrtoumax.sh
new file mode 100755 (executable)
index 0000000..ab065ba
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="t-xstrtoumax.tmp t-xstrtoumax.xo"
+: > t-xstrtoumax.tmp
+too_big=99999999999999999999999999999999999999999999999999999999999999999999
+result=0
+
+# test xstrtoumax
+./test-xstrtoumax${EXEEXT} 1 >> t-xstrtoumax.tmp 2>&1 || result=1
+./test-xstrtoumax${EXEEXT} -1 >> t-xstrtoumax.tmp 2>&1 && result=1
+./test-xstrtoumax${EXEEXT} 1k >> t-xstrtoumax.tmp 2>&1 || result=1
+./test-xstrtoumax${EXEEXT} ${too_big}h >> t-xstrtoumax.tmp 2>&1 && result=1
+./test-xstrtoumax${EXEEXT} $too_big >> t-xstrtoumax.tmp 2>&1 && result=1
+./test-xstrtoumax${EXEEXT} x >> t-xstrtoumax.tmp 2>&1 && result=1
+./test-xstrtoumax${EXEEXT} 9x >> t-xstrtoumax.tmp 2>&1 && result=1
+./test-xstrtoumax${EXEEXT} 010 >> t-xstrtoumax.tmp 2>&1 || result=1
+./test-xstrtoumax${EXEEXT} MiB >> t-xstrtoumax.tmp 2>&1 || result=1
+
+# normalize output
+sed -e 's/^[^:]*: //' < t-xstrtoumax.tmp > t-xstrtoumax.xo
+mv t-xstrtoumax.xo t-xstrtoumax.tmp
+
+# compare expected output
+cat > t-xstrtoumax.xo <<EOF
+1->1 ()
+invalid arg argument \`-1'
+1k->1024 ()
+invalid suffix in arg argument \`${too_big}h'
+arg argument \`$too_big' too large
+invalid arg argument \`x'
+invalid suffix in arg argument \`9x'
+010->8 ()
+MiB->1048576 ()
+EOF
+
+diff t-xstrtoumax.xo t-xstrtoumax.tmp || result=1
+
+rm -fr $tmpfiles
+
+exit $result