]> git.saurik.com Git - bison.git/blobdiff - src/files.c
More explicit use of "const", "extern", and "static", particularly to
[bison.git] / src / files.c
index 5f8fda44407313da468ede6a1d266ea8627cf5b3..10f48f986294f1e8ce09d604f90ffc4ebbe2798e 100644 (file)
@@ -19,6 +19,8 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  */
 
 
+#include "system.h"
+
 #if defined (VMS) & !defined (__VMS_POSIX)
 #include <ssdef.h>
 #define unlink delete
@@ -40,10 +42,15 @@ Boston, MA 02111-1307, USA.  */
 #endif
 
 #include <stdio.h>
-#include "system.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include "files.h"
 #include "alloc.h"
 #include "gram.h"
+#include "complain.h"
 
 FILE *finput = NULL;
 FILE *foutput = NULL;
@@ -58,43 +65,44 @@ FILE *fparser = NULL;
 char *spec_outfile;
 
 char *infile;
-char *outfile;
-char *defsfile;
-char *tabfile;
 char *attrsfile;
-char *guardfile;
-char *actfile;
-char *tmpattrsfile;
-char *tmptabfile;
-char *tmpdefsfile;
+
+static char *outfile;
+static char *defsfile;
+static char *tabfile;
+static char *guardfile;
+static char *actfile;
+static char *tmpattrsfile;
+static char *tmptabfile;
+static char *tmpdefsfile;
 
 extern int noparserflag;
 
 extern char    *mktemp();      /* So the compiler won't complain */
 extern char    *getenv();
-extern void    perror();
 
-char *stringappend PARAMS((char *, int, char *));
-void openfiles PARAMS((void));
-void open_extra_files PARAMS((void));
-FILE *tryopen PARAMS((char *, char *));        /* This might be a good idea */
-void done PARAMS((int));
+extern char *stringappend PARAMS((const char *, int, const char *));
+extern void openfiles PARAMS((void));
+extern void open_extra_files PARAMS((void));
+
+int fixed_outfiles = 0;
 
 extern char *program_name;
 extern int verboseflag;
 extern int definesflag;
-int fixed_outfiles = 0;
 \f
 
 char *
-stringappend (char *string1, int end1, char *string2)
+stringappend (const char *string1, int end1, const char *string2)
 {
   register char *ostring;
-  register char *cp, *cp1;
+  register char *cp;
+  register const char *cp1;
   register int i;
 
-  cp = string2;  i = 0;
-  while (*cp++) i++;
+  cp1 = string2;
+  i = 0;
+  while (*cp1++) i++;
 
   ostring = NEW2(i+end1+1, char);
 
@@ -110,22 +118,59 @@ stringappend (char *string1, int end1, char *string2)
   return ostring;
 }
 
+/*-----------------------------------------------------------------.
+| Try to open file NAME with mode MODE, and print an error message |
+| if fails.                                                        |
+`-----------------------------------------------------------------*/
+
+static FILE *
+tryopen (const char *name, const char *mode)
+{
+  FILE *ptr;
+
+  ptr = fopen (name, mode);
+  if (!ptr)
+    error (2, errno, _("cannot open file `%s'"), name);
+
+  return ptr;
+}
+
+/*-------------------------------------------------------------.
+| Try to close file PTR, and print an error message if fails.  |
+`-------------------------------------------------------------*/
 
+static int
+tryclose (FILE *ptr)
+{
+  int result;
+
+  if (ptr == NULL)
+    return 0;
+
+  result = fclose (ptr);
+  if (result == EOF)
+    error (2, errno, _("cannot close file"));
+
+  return result;
+}
+\f
 /* JF this has been hacked to death.  Nowaday it sets up the file names for
    the output files, and opens the tmp files and the parser */
 void
 openfiles (void)
 {
   char *name_base;
+#ifdef MSDOS
   register char *cp;
+#endif
   char *filename;
   int base_length;
   int short_base_length;
 
 #if defined (VMS) & !defined (__VMS_POSIX)
-  char *tmp_base = "sys$scratch:b_";
+  const char *tmp_base = "sys$scratch:b_";
 #else
-  char *tmp_base = "/tmp/b.";
+  const char *tmp_base = "/tmp/b.";
 #endif
   int tmp_len;
 
@@ -145,7 +190,7 @@ openfiles (void)
   strlwr (infile);
 #endif /* _WIN32 && !__CYGWIN32__ */
 
-#if (defined(unix) || defined(__unix) || defined(__unix__))
+#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
   {
     char *tmp_ptr = getenv("TMPDIR");
 
@@ -199,7 +244,9 @@ openfiles (void)
       /* -o was not specified; compute output file name from input
         or use y.tab.c, etc., if -y was specified.  */
 
-      name_base = fixed_outfiles ? "y.y" : infile;
+      static char FIXED_NAME_BASE[] = "y.y";
+
+      name_base = fixed_outfiles ? FIXED_NAME_BASE : infile;
 
       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
 
@@ -222,7 +269,7 @@ openfiles (void)
 
   finput = tryopen(infile, "r");
 
-  if (! noparserflag) 
+  if (! noparserflag)
     {
       filename = getenv("BISON_SIMPLE");
 #ifdef MSDOS
@@ -257,7 +304,7 @@ openfiles (void)
       /* use permanent name for actions file */
       actfile = stringappend(name_base, short_base_length, ".act");
       faction = tryopen(actfile, "w");
-    } 
+    }
 
 #ifdef MSDOS
   if (! noparserflag)
@@ -322,12 +369,14 @@ open_extra_files (void)
 {
   FILE *ftmp;
   int c;
-  char *filename, *cp;
+  char *filename;
+#ifdef MSDOS
+  char *cp;
+#endif
 
-  if (fparser)
-    fclose(fparser);
+  tryclose(fparser);
 
-  if (! noparserflag) 
+  if (! noparserflag)
     {
       filename = (char *) getenv ("BISON_HAIRY");
 #ifdef MSDOS
@@ -350,53 +399,25 @@ open_extra_files (void)
   rewind(fattrs);
   while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
     putc(c,ftmp);
-  fclose(fattrs);
+  tryclose(fattrs);
   fattrs=ftmp;
 
   fguard = tryopen(guardfile, "w");
 
 }
 
-       /* JF to make file opening easier.  This func tries to open file
-          NAME with mode MODE, and prints an error message if it fails. */
-FILE *
-tryopen (char *name, char *mode)
-{
-  FILE *ptr;
-
-  ptr = fopen(name, mode);
-  if (ptr == NULL)
-    {
-      fprintf(stderr, "%s: ", program_name);
-      perror(name);
-      done(2);
-    }
-  return ptr;
-}
-
 void
-done (int k)
+done (void)
 {
-  if (faction)
-    fclose(faction);
-
-  if (fattrs)
-    fclose(fattrs);
-
-  if (fguard)
-    fclose(fguard);
-
-  if (finput)
-    fclose(finput);
-
-  if (fparser)
-    fclose(fparser);
-
-  if (foutput)
-    fclose(foutput);
-
-       /* JF write out the output file */
-  if (k == 0 && ftable)
+  tryclose(faction);
+  tryclose(fattrs);
+  tryclose(fguard);
+  tryclose(finput);
+  tryclose(fparser);
+  tryclose(foutput);
+
+  /* JF write out the output file */
+  if (!complain_message_count && ftable)
     {
       FILE *ftmp;
       register int c;
@@ -405,8 +426,8 @@ done (int k)
       rewind(ftable);
       while((c=getc(ftable)) != EOF)
         putc(c,ftmp);
-      fclose(ftmp);
-      fclose(ftable);
+      tryclose(ftmp);
+      tryclose(ftable);
 
       if (definesflag)
         {
@@ -415,8 +436,8 @@ done (int k)
           rewind(fdefines);
           while((c=getc(fdefines)) != EOF)
             putc(c,ftmp);
-          fclose(ftmp);
-          fclose(fdefines);
+          tryclose(ftmp);
+          tryclose(fdefines);
         }
     }
 
@@ -427,8 +448,10 @@ done (int k)
     delete(tmpattrsfile);
   if (ftable)
     delete(tmptabfile);
-  if (k==0) sys$exit(SS$_NORMAL);
-  sys$exit(SS$_ABORT);
+/* Don't call exit again, we're in atexit ().
+  if (!complain_message_count)
+    sys$exit(SS$_NORMAL);
+  sys$exit(SS$_ABORT); */
 #else
 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
   if (actfile && ! noparserflag) unlink(actfile);
@@ -436,6 +459,7 @@ done (int k)
   if (tmptabfile) unlink(tmptabfile);
   if (tmpdefsfile) unlink(tmpdefsfile);
 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
-  exit(k);
+/* Don't call exit again, we're in atexit ().
+  exit (complain_message_count ? 1 : 0); */
 #endif /* not VMS, or __VMS_POSIX */
 }