summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2007-08-08 12:45:54 +0000
committerEric Blake <ebb9@byu.net>2007-08-08 12:45:54 +0000
commitc7110f3b833159c91e8cbe6a14349174aeef4ab6 (patch)
treeb1f30e57f6dab6db962d6ac823191b940964c6d6
parentfe32d83182dd651bbdaee9a8b6a54c6e3ad9ad2e (diff)
downloadgnulib-c7110f3b833159c91e8cbe6a14349174aeef4ab6.tar.gz
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.
-rw-r--r--ChangeLog19
-rw-r--r--lib/xstrtol-error.c59
-rw-r--r--lib/xstrtol.c34
-rw-r--r--lib/xstrtol.h33
-rw-r--r--m4/xstrtol.m46
-rw-r--r--modules/xstrtoimax-tests14
-rw-r--r--modules/xstrtol1
-rw-r--r--modules/xstrtol-tests16
-rw-r--r--modules/xstrtoumax-tests14
-rw-r--r--tests/test-xstrtoimax.c4
-rwxr-xr-xtests/test-xstrtoimax.sh43
-rw-r--r--tests/test-xstrtol.c59
-rwxr-xr-xtests/test-xstrtol.sh64
-rw-r--r--tests/test-xstrtoul.c4
-rw-r--r--tests/test-xstrtoumax.c4
-rwxr-xr-xtests/test-xstrtoumax.sh43
16 files changed, 351 insertions, 66 deletions
diff --git a/ChangeLog b/ChangeLog
index 2f091d416d..2f403f29b5 100644
--- 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
index 0000000000..5316fc0aba
--- /dev/null
+++ b/lib/xstrtol-error.c
@@ -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;
+ }
+}
diff --git a/lib/xstrtol.c b/lib/xstrtol.c
index d57b3da853..ed5bcdd0b2 100644
--- a/lib/xstrtol.c
+++ b/lib/xstrtol.c
@@ -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 */
diff --git a/lib/xstrtol.h b/lib/xstrtol.h
index 4df140c2ef..96fc4d23e4 100644
--- a/lib/xstrtol.h
+++ b/lib/xstrtol.h
@@ -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_ */
diff --git a/m4/xstrtol.m4 b/m4/xstrtol.m4
index cd5a549dd3..1fdcf717f5 100644
--- a/m4/xstrtol.m4
+++ b/m4/xstrtol.m4
@@ -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
index 0000000000..a856317281
--- /dev/null
+++ b/modules/xstrtoimax-tests
@@ -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@
diff --git a/modules/xstrtol b/modules/xstrtol
index 0d136bf88c..cf48d8e685 100644
--- a/modules/xstrtol
+++ b/modules/xstrtol
@@ -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
index 0000000000..91ff626d12
--- /dev/null
+++ b/modules/xstrtol-tests
@@ -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
index 0000000000..030dcf84d3
--- /dev/null
+++ b/modules/xstrtoumax-tests
@@ -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
index 0000000000..cc2f6ae895
--- /dev/null
+++ b/tests/test-xstrtoimax.c
@@ -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
index 0000000000..d053751c50
--- /dev/null
+++ b/tests/test-xstrtoimax.sh
@@ -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
index 0000000000..5f09452a54
--- /dev/null
+++ b/tests/test-xstrtol.c
@@ -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
index 0000000000..7bf883379b
--- /dev/null
+++ b/tests/test-xstrtol.sh
@@ -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
index 0000000000..9dda9ee67e
--- /dev/null
+++ b/tests/test-xstrtoul.c
@@ -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
index 0000000000..2e68c2e5d8
--- /dev/null
+++ b/tests/test-xstrtoumax.c
@@ -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
index 0000000000..ab065ba580
--- /dev/null
+++ b/tests/test-xstrtoumax.sh
@@ -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