Boston, MA 02111-1307, USA. */
+#include "system.h"
+
#if defined (VMS) & !defined (__VMS_POSIX)
#include <ssdef.h>
#define unlink delete
#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;
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);
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;
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");
/* -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. */
finput = tryopen(infile, "r");
- if (! noparserflag)
+ if (! noparserflag)
{
filename = getenv("BISON_SIMPLE");
#ifdef MSDOS
/* use permanent name for actions file */
actfile = stringappend(name_base, short_base_length, ".act");
faction = tryopen(actfile, "w");
- }
+ }
#ifdef MSDOS
if (! noparserflag)
{
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
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;
rewind(ftable);
while((c=getc(ftable)) != EOF)
putc(c,ftmp);
- fclose(ftmp);
- fclose(ftable);
+ tryclose(ftmp);
+ tryclose(ftable);
if (definesflag)
{
rewind(fdefines);
while((c=getc(fdefines)) != EOF)
putc(c,ftmp);
- fclose(ftmp);
- fclose(fdefines);
+ tryclose(ftmp);
+ tryclose(fdefines);
}
}
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);
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 */
}