From dd5611579b3978eff5c4aa22dc8459b8f53c2fc0 Mon Sep 17 00:00:00 2001
From: Akim Demaille <demaille@gostai.com>
Date: Sat, 18 Feb 2012 19:19:26 +0100
Subject: [PATCH] maint: avoid "magic number exit".

	* cfg.mk (local-checks-to-skip): No longer skip it.
	* bootstrap.conf (gnulib_modules): Add sysexits.
	* doc/bison.texinfo, etc/bench.pl.in, src/parse-gram.y,
	* src/system.h, tests/calc.at, tests/named-refs.at: Use assert
	where appropriate instead of "if (...) exit".
	Use symbolic exit status elsewhere.

Conflicts:

	doc/bison.texinfo
	src/parse-gram.y
---
 bootstrap.conf      |  1 +
 cfg.mk              |  1 -
 doc/bison.texinfo   | 14 ++++++++++----
 etc/bench.pl.in     |  3 +--
 lib/.gitignore      |  1 +
 m4/.gitignore       |  1 +
 src/parse-gram.y    |  4 ++--
 src/system.h        |  1 +
 tests/calc.at       |  3 +--
 tests/named-refs.at |  4 ++--
 10 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 7d6f3fbd..04a03361 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -24,6 +24,7 @@ gnulib_modules='
   javaexec-script ldexpl maintainer-makefile malloc-gnu mbschr mbsrchr
   mbswidth obstack perror pipe-posix quote quotearg realloc-posix
   spawn-pipe stdbool stpcpy strdup-posix strerror strtoul strverscmp
+  sysexits
   unistd unistd-safer unlocked-io update-copyright unsetenv verify
   warnings xalloc xalloc-die xstrndup
 
diff --git a/cfg.mk b/cfg.mk
index a7b16c57..5659a932 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -47,7 +47,6 @@ local-checks-to-skip = \
   sc_prohibit_atoi_atof			\
   sc_prohibit_doubled_word		\
   sc_prohibit_empty_lines_at_EOF	\
-  sc_prohibit_magic_number_exit		\
   sc_prohibit_strcmp
 
 # The local directory containing the checked-out copy of gnulib used in
diff --git a/doc/bison.texinfo b/doc/bison.texinfo
index c121dd4e..329d81d0 100644
--- a/doc/bison.texinfo
+++ b/doc/bison.texinfo
@@ -9366,8 +9366,8 @@ calcxx_driver::scan_begin ()
     yyin = stdin;
   else if (!(yyin = fopen (file.c_str (), "r")))
     @{
-      error (std::string ("cannot open ") + file);
-      exit (1);
+      error (std::string ("cannot open ") + file + ": " + strerror(errno));
+      exit (EXIT_FAILURE);
     @}
 @}
 
@@ -10045,11 +10045,17 @@ yyparse (char const *file)
 {
   yyin = fopen (file, "r");
   if (!yyin)
-    exit (2);
+  {
+    perror ("fopen");
+    exit (EXIT_FAILURE);
+  }
   /* One token only.  */
   yylex ();
   if (fclose (yyin) != 0)
-    exit (3);
+  {
+    perror ("fclose");
+    exit (EXIT_FAILURE);
+  }
   return 0;
 }
 
diff --git a/etc/bench.pl.in b/etc/bench.pl.in
index 829a760c..ff3ce230 100755
--- a/etc/bench.pl.in
+++ b/etc/bench.pl.in
@@ -291,9 +291,8 @@ yylex (void)
 static int
 power (int base, int exponent)
 {
+  assert (0 <= exponent);
   int res = 1;
-  if (exponent < 0)
-    exit (3);
   for (/* Niente */; exponent; --exponent)
     res *= base;
   return res;
diff --git a/lib/.gitignore b/lib/.gitignore
index 5b14836b..832cab85 100644
--- a/lib/.gitignore
+++ b/lib/.gitignore
@@ -263,3 +263,4 @@
 /closeout.h
 /fpending.c
 /fpending.h
+/sysexits.in.h
diff --git a/m4/.gitignore b/m4/.gitignore
index 01e5f645..90e5ae15 100644
--- a/m4/.gitignore
+++ b/m4/.gitignore
@@ -167,3 +167,4 @@
 /xalloc.m4
 /xsize.m4
 /xstrndup.m4
+/sysexits.m4
diff --git a/src/parse-gram.y b/src/parse-gram.y
index 5f46d293..da0aef71 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -758,8 +758,8 @@ version_check (location const *loc, char const *version)
   if (strverscmp (version, PACKAGE_VERSION) > 0)
     {
       complain_at (*loc, "require bison %s, but have %s",
-		   version, PACKAGE_VERSION);
-      exit (63);
+                   version, PACKAGE_VERSION);
+      exit (EX_CONFIG);
     }
 }
 
diff --git a/src/system.h b/src/system.h
index 52b597b1..1c02f253 100644
--- a/src/system.h
+++ b/src/system.h
@@ -52,6 +52,7 @@
 typedef size_t uintptr_t;
 #endif
 
+#include <sysexits.h>
 
 /*---------.
 | Gnulib.  |
diff --git a/tests/calc.at b/tests/calc.at
index 46b798f5..f0e30ad3 100644
--- a/tests/calc.at
+++ b/tests/calc.at
@@ -347,8 +347,7 @@ static int
 power (int base, int exponent)
 {
   int res = 1;
-  if (exponent < 0)
-    exit (3);
+  assert (0 <= exponent);
   for (/* Niente */; exponent; --exponent)
     res *= base;
   return res;
diff --git a/tests/named-refs.at b/tests/named-refs.at
index c3721c03..35c2382b 100644
--- a/tests/named-refs.at
+++ b/tests/named-refs.at
@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+# FIXME: Duplication with calc.at.
 AT_BANNER([[Named references tests.]])
 
 AT_SETUP([Tutorial calculator])
@@ -142,8 +143,7 @@ int yylex (void)
 static int power (int base, int exponent)
 {
   int res = 1;
-  if (exponent < 0)
-    exit (3);
+  assert (0 <= exponent);
   for (/* Niente */; exponent; --exponent)
     res *= base;
   return res;
-- 
2.47.2