home *** CD-ROM | disk | FTP | other *** search
- /* Open and close files for bison,
- Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
-
- This file is part of Bison, the GNU Compiler Compiler.
-
- Bison 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 1, or (at your option)
- any later version.
-
- Bison 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 Bison; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- /* Convert Unix-ism to ANSI-ism */
- #define unlink(f) remove(f)
-
- #include <stdio.h>
- #include "system.h"
- #include "files.h"
- #include "new.h"
- #include "gram.h"
-
- FILE *finput = NULL;
- FILE *foutput = NULL;
- FILE *fdefines = NULL;
- FILE *ftable = NULL;
- FILE *fattrs = NULL;
- FILE *faction = NULL;
- FILE *fparser = NULL;
-
- /* File name specified with -o for the output file, or 0 if no -o. */
- char *spec_outfile;
-
- char *infile;
- char *outfile;
- char *defsfile;
- char *tabfile;
- char *actfile;
-
- static char *tmpattrsfile;
- static char *tmptabfile;
-
- extern int verboseflag;
- extern int definesflag;
- int fixed_outfiles = 0;
-
-
- char *stringappend(char *string1, char *string2)
- {
- register char *ostring;
- register char *cp, *cp1;
- register int i, j;
-
- cp = string1; i = 0;
- while (*cp++) i++;
-
- cp = string2; j = 0;
- while (*cp++) j++;
-
- ostring = NEW2(i+j+1, char);
-
- cp = ostring;
- cp1 = string1;
- while ((*cp++ = *cp1++) != '\0')
- ;
-
- --cp; /* Overwrite the trailing null */
- cp1 = string2;
- while ((*cp++ = *cp1++) != '\0')
- ;
-
- return ostring;
- }
-
-
- /* 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 *full_name;
- char *name_base;
- char *filename;
-
- if (spec_outfile)
- {
- /* -o was specified. The precise -o name will be used for ftable.
- For other output files, remove the "C" directory. */
-
- name_base = strrchr (spec_outfile, '.');
- if (!name_base)
- name_base = strchr (spec_outfile, ':');
-
- if (!name_base)
- name_base = spec_outfile;
- else
- ++name_base;
- }
- else
- {
- /* -o was not specified; compute output file name from input
- or use C.Y_Tab, etc., if -y was specified. */
-
- full_name = fixed_outfiles ? "Y.Y_Tab" : infile;
-
- name_base = strrchr (full_name, '.');
- if (!name_base)
- name_base = strchr (full_name, ':');
-
- if (!name_base)
- name_base = full_name;
- else
- ++name_base;
- }
-
- finput = tryopen(infile, "r");
-
- filename = getenv("Bison$Parser");
- fparser = tryopen(filename ? filename : PARSER, "r");
-
- if (verboseflag)
- {
- outfile = stringappend("Out.", name_base);
- foutput = tryopen(outfile, "w");
- }
-
- if (definesflag)
- {
- defsfile = stringappend("H.", name_base);
- fdefines = tryopen(defsfile, "w");
- }
-
- actfile = mktemp("BisonAct");
- tmpattrsfile = mktemp("BisonAttr");
- tmptabfile = mktemp("BisonTab");
-
- faction = tryopen(actfile, "w+");
- fattrs = tryopen(tmpattrsfile,"w+");
- ftable = tryopen(tmptabfile, "w+");
-
- /* The output file is opened by `done', if at all */
- if (spec_outfile)
- tabfile = spec_outfile;
- else
- tabfile = stringappend("C.", name_base);
- }
-
-
- /* 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, "Bison: cannot open %s (mode \"%s\")\n", name, mode);
- done(2);
- }
- return ptr;
- }
-
- void done(int k)
- {
- if (faction)
- fclose(faction);
-
- if (fattrs)
- fclose(fattrs);
-
- if (finput)
- fclose(finput);
-
- if (fparser)
- fclose(fparser);
-
- if (foutput)
- fclose(foutput);
-
- /* JF write out the output file */
- if (k == 0 && ftable)
- {
- FILE *ftmp;
- register int c;
-
- ftmp=tryopen(tabfile, "w");
- rewind(ftable);
- while((c=getc(ftable)) != EOF)
- putc(c,ftmp);
- fclose(ftmp);
- fclose(ftable);
- }
-
- if (actfile) unlink(actfile);
- if (tmpattrsfile) unlink(tmpattrsfile);
- if (tmptabfile) unlink(tmptabfile);
-
- exit(k);
- }
-