]>
git.saurik.com Git - bison.git/blob - src/files.c
348900fe54d414a76aa39a628d9495ca64c6cf28
1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #if defined (VMS) & !defined (__VMS_POSIX)
26 #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
29 #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
33 #if defined (_MSC_VER)
35 #define XPFILE "c:/usr/local/lib/bison.simple"
38 #define XPFILE1 "c:/usr/local/lib/bison.hairy"
50 FILE *fdefines
= NULL
;
57 /* File name specified with -o for the output file, or 0 if no -o. */
71 extern int noparserflag
;
73 extern char *mktemp(); /* So the compiler won't complain */
74 extern char *getenv();
77 char *stringappend
PARAMS((char *, int, char *));
78 void openfiles
PARAMS((void));
79 void open_extra_files
PARAMS((void));
80 FILE *tryopen
PARAMS((char *, char *)); /* This might be a good idea */
81 int tryclose
PARAMS((FILE *));
82 void done
PARAMS((int));
84 extern char *program_name
;
85 extern int verboseflag
;
86 extern int definesflag
;
87 int fixed_outfiles
= 0;
91 stringappend (char *string1
, int end1
, char *string2
)
93 register char *ostring
;
94 register char *cp
, *cp1
;
100 ostring
= NEW2(i
+end1
+1, char);
104 for (i
= 0; i
< end1
; i
++)
108 while ((*cp
++ = *cp1
++))
115 /* JF this has been hacked to death. Nowaday it sets up the file names for
116 the output files, and opens the tmp files and the parser */
126 int short_base_length
;
128 #if defined (VMS) & !defined (__VMS_POSIX)
129 char *tmp_base
= "sys$scratch:b_";
131 char *tmp_base
= "/tmp/b.";
136 tmp_base
= getenv ("TMP");
142 #if (defined(_WIN32) && !defined(__CYGWIN32__))
143 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
145 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
149 #endif /* _WIN32 && !__CYGWIN32__ */
151 #if (defined(unix) || defined(__unix) || defined(__unix__))
153 char *tmp_ptr
= getenv("TMPDIR");
156 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
158 #endif /* unix || __unix || __unix__ */
160 tmp_len
= strlen (tmp_base
);
164 /* -o was specified. The precise -o name will be used for ftable.
165 For other output files, remove the ".c" or ".tab.c" suffix. */
166 name_base
= spec_outfile
;
170 /* BASE_LENGTH includes ".tab" but not ".c". */
171 base_length
= strlen (name_base
);
172 if (!strcmp (name_base
+ base_length
- 2, ".c"))
174 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
175 short_base_length
= base_length
;
176 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
177 short_base_length
-= 4;
178 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
179 short_base_length
-= 4;
181 else if (spec_file_prefix
)
183 /* -b was specified. Construct names from it. */
184 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
185 short_base_length
= strlen (spec_file_prefix
);
186 /* Count room for `.tab'. */
187 base_length
= short_base_length
+ 4;
188 name_base
= (char *) xmalloc (base_length
+ 1);
190 strcpy (name_base
, spec_file_prefix
);
192 strcat (name_base
, "_tab");
194 strcat (name_base
, ".tab");
202 /* -o was not specified; compute output file name from input
203 or use y.tab.c, etc., if -y was specified. */
205 name_base
= fixed_outfiles
? "y.y" : infile
;
207 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
209 base_length
= strlen (name_base
);
210 if (!strcmp (name_base
+ base_length
- 2, ".y"))
212 short_base_length
= base_length
;
215 name_base
= stringappend(name_base
, short_base_length
, "_tab");
218 name_base
= stringappend(name_base
, short_base_length
, "_tab");
220 name_base
= stringappend(name_base
, short_base_length
, ".tab");
221 #endif /* not MSDOS */
223 base_length
= short_base_length
+ 4;
226 finput
= tryopen(infile
, "r");
230 filename
= getenv("BISON_SIMPLE");
232 /* File doesn't exist in current directory; try in INIT directory. */
234 if (filename
== 0 && cp
!= NULL
)
236 filename
= xmalloc(strlen(cp
) + strlen(PFILE
) + 2);
237 strcpy(filename
, cp
);
238 cp
= filename
+ strlen(filename
);
243 fparser
= tryopen(filename
? filename
: PFILE
, "r");
249 outfile
= stringappend(name_base
, short_base_length
, ".out");
251 /* We used to use just .out if spec_name_prefix (-p) was used,
252 but that conflicts with Posix. */
253 outfile
= stringappend(name_base
, short_base_length
, ".output");
255 foutput
= tryopen(outfile
, "w");
260 /* use permanent name for actions file */
261 actfile
= stringappend(name_base
, short_base_length
, ".act");
262 faction
= tryopen(actfile
, "w");
267 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "acXXXXXX"));
268 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "atXXXXXX"));
269 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "taXXXXXX"));
270 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "deXXXXXX"));
273 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "act.XXXXXX"));
274 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "attrs.XXXXXX"));
275 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "tab.XXXXXX"));
276 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "defs.XXXXXX"));
277 #endif /* not MSDOS */
280 faction
= tryopen(actfile
, "w+");
281 fattrs
= tryopen(tmpattrsfile
,"w+");
282 ftable
= tryopen(tmptabfile
, "w+");
286 defsfile
= stringappend(name_base
, base_length
, ".h");
287 fdefines
= tryopen(tmpdefsfile
, "w+");
290 #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
293 unlink(tmpattrsfile
);
296 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
298 /* These are opened by `done' or `open_extra_files', if at all */
300 tabfile
= spec_outfile
;
302 tabfile
= stringappend(name_base
, base_length
, ".c");
305 attrsfile
= stringappend(name_base
, short_base_length
, "_stype.h");
306 guardfile
= stringappend(name_base
, short_base_length
, "_guard.c");
309 attrsfile
= stringappend(name_base
, short_base_length
, ".sth");
310 guardfile
= stringappend(name_base
, short_base_length
, ".guc");
312 attrsfile
= stringappend(name_base
, short_base_length
, ".stype.h");
313 guardfile
= stringappend(name_base
, short_base_length
, ".guard.c");
314 #endif /* not MSDOS */
320 /* open the output files needed only for the semantic parser.
321 This is done when %semantic_parser is seen in the declarations section. */
324 open_extra_files (void)
337 filename
= (char *) getenv ("BISON_HAIRY");
339 /* File doesn't exist in current directory; try in INIT directory. */
341 if (filename
== 0 && cp
!= NULL
)
343 filename
= xmalloc(strlen(cp
) + strlen(PFILE1
) + 2);
344 strcpy(filename
, cp
);
345 cp
= filename
+ strlen(filename
);
350 fparser
= tryopen(filename
? filename
: PFILE1
, "r");
353 /* JF change from inline attrs file to separate one */
354 ftmp
= tryopen(attrsfile
, "w");
356 while((c
=getc(fattrs
))!=EOF
) /* Thank god for buffering */
361 fguard
= tryopen(guardfile
, "w");
365 /* JF to make file opening easier. This func tries to open file
366 NAME with mode MODE, and prints an error message if it fails. */
368 tryopen (char *name
, char *mode
)
372 ptr
= fopen(name
, mode
);
375 fprintf(stderr
, "%s: ", program_name
);
390 result
= fclose (ptr
);
393 fprintf (stderr
, "%s: ", program_name
);
410 /* JF write out the output file */
411 if (k
== 0 && ftable
)
416 ftmp
=tryopen(tabfile
, "w");
418 while((c
=getc(ftable
)) != EOF
)
425 ftmp
= tryopen(defsfile
, "w");
428 while((c
=getc(fdefines
)) != EOF
)
435 #if defined (VMS) & !defined (__VMS_POSIX)
436 if (faction
&& ! noparserflag
)
439 delete(tmpattrsfile
);
442 if (k
==0) sys$
exit(SS$_NORMAL
);
445 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
446 if (actfile
&& ! noparserflag
) unlink(actfile
);
447 if (tmpattrsfile
) unlink(tmpattrsfile
);
448 if (tmptabfile
) unlink(tmptabfile
);
449 if (tmpdefsfile
) unlink(tmpdefsfile
);
450 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
452 #endif /* not VMS, or __VMS_POSIX */