changeset 9141:2f9763090486

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.
author Eric Blake <ebb9@byu.net>
date Wed, 08 Aug 2007 12:45:54 +0000
parents ffc5b7a10350
children a5e5584188ed
files ChangeLog lib/xstrtol-error.c lib/xstrtol.c lib/xstrtol.h m4/xstrtol.m4 modules/xstrtoimax-tests modules/xstrtol modules/xstrtol-tests modules/xstrtoumax-tests tests/test-xstrtoimax.c tests/test-xstrtoimax.sh tests/test-xstrtol.c tests/test-xstrtol.sh tests/test-xstrtoul.c tests/test-xstrtoumax.c tests/test-xstrtoumax.sh
diffstat 16 files changed, 351 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- 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.
new file mode 100644
--- /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;
+    }
+}
--- a/lib/xstrtol.c
+++ b/lib/xstrtol.c
@@ -227,37 +227,3 @@
   *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 */
--- 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 @@
     (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_ */
--- 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_LIBOBJ([xstrtol])
   AC_LIBOBJ([xstrtoul])
+  AC_LIBOBJ([xstrtol-error])
 ])
new file mode 100644
--- /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@
--- a/modules/xstrtol
+++ b/modules/xstrtol
@@ -5,6 +5,7 @@
 lib/xstrtol.h
 lib/xstrtol.c
 lib/xstrtoul.c
+lib/xstrtol-error.c
 m4/xstrtol.m4
 
 Depends-on:
new file mode 100644
--- /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@
new file mode 100644
--- /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@
new file mode 100644
--- /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"
new file mode 100755
--- /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
new file mode 100644
--- /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);
+}
new file mode 100755
--- /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
new file mode 100644
--- /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"
new file mode 100644
--- /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"
new file mode 100755
--- /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