]>
Commit | Line | Data |
---|---|---|
54bd0db4 | 1 | /* Open and close files for bison, |
ceed8467 | 2 | Copyright (C) 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc. |
54bd0db4 | 3 | |
ceed8467 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
54bd0db4 | 5 | |
ceed8467 AD |
6 | Bison is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
54bd0db4 | 10 | |
ceed8467 AD |
11 | Bison is distributed in the hope that it will be useful, but |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
54bd0db4 | 15 | |
ceed8467 AD |
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 the Free | |
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
54bd0db4 RS |
20 | |
21 | ||
9eceb6c6 JT |
22 | #include "system.h" |
23 | ||
fcca25ad | 24 | #if defined (VMS) & !defined (__VMS_POSIX) |
ceed8467 AD |
25 | # include <ssdef.h> |
26 | # define unlink delete | |
27 | # ifndef XPFILE | |
28 | # define XPFILE "GNU_BISON:[000000]BISON.SIMPLE" | |
29 | # endif | |
30 | # ifndef XPFILE1 | |
31 | # define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY" | |
32 | # endif | |
54bd0db4 RS |
33 | #endif |
34 | ||
6a5705cf | 35 | #if defined (_MSC_VER) |
ceed8467 AD |
36 | # ifndef XPFILE |
37 | # define XPFILE "c:/usr/local/lib/bison.simple" | |
38 | # endif | |
39 | # ifndef XPFILE1 | |
40 | # define XPFILE1 "c:/usr/local/lib/bison.hairy" | |
41 | # endif | |
dad49092 JT |
42 | #endif |
43 | ||
ceed8467 | 44 | #include "getargs.h" |
54bd0db4 | 45 | #include "files.h" |
d7913476 | 46 | #include "xalloc.h" |
54bd0db4 | 47 | #include "gram.h" |
a0f6b076 | 48 | #include "complain.h" |
54bd0db4 RS |
49 | |
50 | FILE *finput = NULL; | |
51 | FILE *foutput = NULL; | |
52 | FILE *fdefines = NULL; | |
53 | FILE *ftable = NULL; | |
54 | FILE *fattrs = NULL; | |
55 | FILE *fguard = NULL; | |
56 | FILE *faction = NULL; | |
57 | FILE *fparser = NULL; | |
58 | ||
59 | /* File name specified with -o for the output file, or 0 if no -o. */ | |
60 | char *spec_outfile; | |
61 | ||
62 | char *infile; | |
54bd0db4 | 63 | char *attrsfile; |
4a120d45 JT |
64 | |
65 | static char *outfile; | |
66 | static char *defsfile; | |
67 | static char *tabfile; | |
68 | static char *guardfile; | |
69 | static char *actfile; | |
70 | static char *tmpattrsfile; | |
71 | static char *tmptabfile; | |
72 | static char *tmpdefsfile; | |
54bd0db4 | 73 | |
8963a27b AD |
74 | extern char *mktemp (); /* So the compiler won't complain */ |
75 | extern char *getenv (); | |
4a120d45 | 76 | |
54bd0db4 | 77 | extern char *program_name; |
54bd0db4 RS |
78 | \f |
79 | ||
8963a27b | 80 | static char * |
4a120d45 | 81 | stringappend (const char *string1, int end1, const char *string2) |
54bd0db4 RS |
82 | { |
83 | register char *ostring; | |
4a120d45 JT |
84 | register char *cp; |
85 | register const char *cp1; | |
54bd0db4 RS |
86 | register int i; |
87 | ||
4a120d45 JT |
88 | cp1 = string2; |
89 | i = 0; | |
8963a27b AD |
90 | while (*cp1++) |
91 | i++; | |
54bd0db4 | 92 | |
d7913476 | 93 | ostring = XCALLOC (char, i + end1 + 1); |
54bd0db4 RS |
94 | |
95 | cp = ostring; | |
96 | cp1 = string1; | |
97 | for (i = 0; i < end1; i++) | |
98 | *cp++ = *cp1++; | |
99 | ||
100 | cp1 = string2; | |
bd088c91 JT |
101 | while ((*cp++ = *cp1++)) |
102 | ; | |
54bd0db4 RS |
103 | |
104 | return ostring; | |
105 | } | |
106 | ||
cfe5fbc0 AD |
107 | /*-----------------------------------------------------------------. |
108 | | Try to open file NAME with mode MODE, and print an error message | | |
109 | | if fails. | | |
110 | `-----------------------------------------------------------------*/ | |
54bd0db4 | 111 | |
cfe5fbc0 | 112 | static FILE * |
8963a27b | 113 | xfopen (const char *name, const char *mode) |
cfe5fbc0 | 114 | { |
8963a27b | 115 | FILE *ptr; |
cfe5fbc0 AD |
116 | |
117 | ptr = fopen (name, mode); | |
118 | if (!ptr) | |
119 | error (2, errno, _("cannot open file `%s'"), name); | |
120 | ||
121 | return ptr; | |
122 | } | |
123 | ||
124 | /*-------------------------------------------------------------. | |
125 | | Try to close file PTR, and print an error message if fails. | | |
126 | `-------------------------------------------------------------*/ | |
127 | ||
128 | static int | |
8963a27b | 129 | xfclose (FILE *ptr) |
cfe5fbc0 AD |
130 | { |
131 | int result; | |
132 | ||
133 | if (ptr == NULL) | |
134 | return 0; | |
135 | ||
136 | result = fclose (ptr); | |
137 | if (result == EOF) | |
138 | error (2, errno, _("cannot close file")); | |
139 | ||
140 | return result; | |
141 | } | |
142 | \f | |
54bd0db4 RS |
143 | /* JF this has been hacked to death. Nowaday it sets up the file names for |
144 | the output files, and opens the tmp files and the parser */ | |
145 | void | |
8963a27b | 146 | open_files (void) |
54bd0db4 RS |
147 | { |
148 | char *name_base; | |
a693bf18 | 149 | #ifdef MSDOS |
54bd0db4 | 150 | register char *cp; |
a693bf18 | 151 | #endif |
54bd0db4 RS |
152 | char *filename; |
153 | int base_length; | |
154 | int short_base_length; | |
155 | ||
fcca25ad | 156 | #if defined (VMS) & !defined (__VMS_POSIX) |
4a120d45 | 157 | const char *tmp_base = "sys$scratch:b_"; |
54bd0db4 | 158 | #else |
4a120d45 | 159 | const char *tmp_base = "/tmp/b."; |
54bd0db4 RS |
160 | #endif |
161 | int tmp_len; | |
162 | ||
163 | #ifdef MSDOS | |
164 | tmp_base = getenv ("TMP"); | |
165 | if (tmp_base == 0) | |
166 | tmp_base = ""; | |
167 | strlwr (infile); | |
168 | #endif /* MSDOS */ | |
169 | ||
e0672a61 | 170 | #if (defined(_WIN32) && !defined(__CYGWIN32__)) |
8963a27b | 171 | tmp_base = getenv ("TEMP"); /* Windows95 defines this ... */ |
e0672a61 | 172 | if (tmp_base == 0) |
8963a27b | 173 | tmp_base = getenv ("Temp"); /* ... while NT prefers this */ |
e0672a61 RS |
174 | if (tmp_base == 0) |
175 | tmp_base = ""; | |
176 | strlwr (infile); | |
177 | #endif /* _WIN32 && !__CYGWIN32__ */ | |
178 | ||
5191ef24 | 179 | #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__)) |
bd088c91 | 180 | { |
8963a27b | 181 | char *tmp_ptr = getenv ("TMPDIR"); |
bd088c91 JT |
182 | |
183 | if (tmp_ptr != 0) | |
184 | tmp_base = stringappend (tmp_ptr, strlen (tmp_ptr), "/b."); | |
185 | } | |
8963a27b | 186 | #endif /* unix || __unix || __unix__ */ |
bd088c91 | 187 | |
54bd0db4 RS |
188 | tmp_len = strlen (tmp_base); |
189 | ||
190 | if (spec_outfile) | |
191 | { | |
192 | /* -o was specified. The precise -o name will be used for ftable. | |
8963a27b | 193 | For other output files, remove the ".c" or ".tab.c" suffix. */ |
54bd0db4 RS |
194 | name_base = spec_outfile; |
195 | #ifdef MSDOS | |
196 | strlwr (name_base); | |
197 | #endif /* MSDOS */ | |
198 | /* BASE_LENGTH includes ".tab" but not ".c". */ | |
199 | base_length = strlen (name_base); | |
200 | if (!strcmp (name_base + base_length - 2, ".c")) | |
201 | base_length -= 2; | |
202 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ | |
203 | short_base_length = base_length; | |
204 | if (!strncmp (name_base + short_base_length - 4, ".tab", 4)) | |
205 | short_base_length -= 4; | |
206 | else if (!strncmp (name_base + short_base_length - 4, "_tab", 4)) | |
207 | short_base_length -= 4; | |
208 | } | |
209 | else if (spec_file_prefix) | |
210 | { | |
211 | /* -b was specified. Construct names from it. */ | |
212 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ | |
213 | short_base_length = strlen (spec_file_prefix); | |
214 | /* Count room for `.tab'. */ | |
215 | base_length = short_base_length + 4; | |
d7913476 | 216 | name_base = XMALLOC (char, base_length + 1); |
54bd0db4 RS |
217 | /* Append `.tab'. */ |
218 | strcpy (name_base, spec_file_prefix); | |
219 | #ifdef VMS | |
220 | strcat (name_base, "_tab"); | |
221 | #else | |
222 | strcat (name_base, ".tab"); | |
223 | #endif | |
224 | #ifdef MSDOS | |
225 | strlwr (name_base); | |
226 | #endif /* MSDOS */ | |
227 | } | |
228 | else | |
229 | { | |
230 | /* -o was not specified; compute output file name from input | |
8963a27b | 231 | or use y.tab.c, etc., if -y was specified. */ |
54bd0db4 | 232 | |
4a120d45 JT |
233 | static char FIXED_NAME_BASE[] = "y.y"; |
234 | ||
235 | name_base = fixed_outfiles ? FIXED_NAME_BASE : infile; | |
54bd0db4 RS |
236 | |
237 | /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */ | |
238 | ||
239 | base_length = strlen (name_base); | |
240 | if (!strcmp (name_base + base_length - 2, ".y")) | |
241 | base_length -= 2; | |
242 | short_base_length = base_length; | |
243 | ||
244 | #ifdef VMS | |
8963a27b | 245 | name_base = stringappend (name_base, short_base_length, "_tab"); |
54bd0db4 RS |
246 | #else |
247 | #ifdef MSDOS | |
8963a27b | 248 | name_base = stringappend (name_base, short_base_length, "_tab"); |
54bd0db4 | 249 | #else |
8963a27b | 250 | name_base = stringappend (name_base, short_base_length, ".tab"); |
54bd0db4 RS |
251 | #endif /* not MSDOS */ |
252 | #endif | |
253 | base_length = short_base_length + 4; | |
254 | } | |
255 | ||
8963a27b | 256 | finput = xfopen (infile, "r"); |
54bd0db4 | 257 | |
8963a27b | 258 | if (!noparserflag) |
54bd0db4 | 259 | { |
8963a27b | 260 | filename = getenv ("BISON_SIMPLE"); |
28683c54 RS |
261 | #ifdef MSDOS |
262 | /* File doesn't exist in current directory; try in INIT directory. */ | |
8963a27b | 263 | cp = getenv ("INIT"); |
28683c54 | 264 | if (filename == 0 && cp != NULL) |
8963a27b | 265 | { |
d7913476 | 266 | filename = XMALLOC (char, strlen (cp) + strlen (PFILE) + 2); |
8963a27b AD |
267 | strcpy (filename, cp); |
268 | cp = filename + strlen (filename); | |
269 | *cp++ = '/'; | |
270 | strcpy (cp, PFILE); | |
271 | } | |
54bd0db4 | 272 | #endif /* MSDOS */ |
8963a27b | 273 | fparser = xfopen (filename ? filename : PFILE, "r"); |
28683c54 | 274 | } |
54bd0db4 RS |
275 | |
276 | if (verboseflag) | |
277 | { | |
278 | #ifdef MSDOS | |
8963a27b | 279 | outfile = stringappend (name_base, short_base_length, ".out"); |
54bd0db4 RS |
280 | #else |
281 | /* We used to use just .out if spec_name_prefix (-p) was used, | |
8963a27b AD |
282 | but that conflicts with Posix. */ |
283 | outfile = stringappend (name_base, short_base_length, ".output"); | |
54bd0db4 | 284 | #endif |
8963a27b | 285 | foutput = xfopen (outfile, "w"); |
54bd0db4 RS |
286 | } |
287 | ||
28683c54 RS |
288 | if (noparserflag) |
289 | { | |
290 | /* use permanent name for actions file */ | |
8963a27b AD |
291 | actfile = stringappend (name_base, short_base_length, ".act"); |
292 | faction = xfopen (actfile, "w"); | |
a0f6b076 | 293 | } |
28683c54 | 294 | |
54bd0db4 | 295 | #ifdef MSDOS |
8963a27b AD |
296 | if (!noparserflag) |
297 | actfile = mktemp (stringappend (tmp_base, tmp_len, "acXXXXXX")); | |
298 | tmpattrsfile = mktemp (stringappend (tmp_base, tmp_len, "atXXXXXX")); | |
299 | tmptabfile = mktemp (stringappend (tmp_base, tmp_len, "taXXXXXX")); | |
300 | tmpdefsfile = mktemp (stringappend (tmp_base, tmp_len, "deXXXXXX")); | |
54bd0db4 | 301 | #else |
8963a27b AD |
302 | if (!noparserflag) |
303 | actfile = mktemp (stringappend (tmp_base, tmp_len, "act.XXXXXX")); | |
304 | tmpattrsfile = mktemp (stringappend (tmp_base, tmp_len, "attrs.XXXXXX")); | |
305 | tmptabfile = mktemp (stringappend (tmp_base, tmp_len, "tab.XXXXXX")); | |
306 | tmpdefsfile = mktemp (stringappend (tmp_base, tmp_len, "defs.XXXXXX")); | |
54bd0db4 RS |
307 | #endif /* not MSDOS */ |
308 | ||
8963a27b AD |
309 | if (!noparserflag) |
310 | faction = xfopen (actfile, "w+"); | |
311 | fattrs = xfopen (tmpattrsfile, "w+"); | |
312 | ftable = xfopen (tmptabfile, "w+"); | |
54bd0db4 RS |
313 | |
314 | if (definesflag) | |
315 | { | |
8963a27b AD |
316 | defsfile = stringappend (name_base, base_length, ".h"); |
317 | fdefines = xfopen (tmpdefsfile, "w+"); | |
54bd0db4 RS |
318 | } |
319 | ||
e0672a61 | 320 | #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__))) |
8963a27b AD |
321 | if (!noparserflag) |
322 | unlink (actfile); | |
323 | unlink (tmpattrsfile); | |
324 | unlink (tmptabfile); | |
325 | unlink (tmpdefsfile); | |
e0672a61 | 326 | #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */ |
54bd0db4 | 327 | |
8963a27b | 328 | /* These are opened by `done' or `open_extra_files', if at all */ |
54bd0db4 RS |
329 | if (spec_outfile) |
330 | tabfile = spec_outfile; | |
331 | else | |
8963a27b | 332 | tabfile = stringappend (name_base, base_length, ".c"); |
54bd0db4 RS |
333 | |
334 | #ifdef VMS | |
8963a27b AD |
335 | attrsfile = stringappend (name_base, short_base_length, "_stype.h"); |
336 | guardfile = stringappend (name_base, short_base_length, "_guard.c"); | |
54bd0db4 RS |
337 | #else |
338 | #ifdef MSDOS | |
8963a27b AD |
339 | attrsfile = stringappend (name_base, short_base_length, ".sth"); |
340 | guardfile = stringappend (name_base, short_base_length, ".guc"); | |
54bd0db4 | 341 | #else |
8963a27b AD |
342 | attrsfile = stringappend (name_base, short_base_length, ".stype.h"); |
343 | guardfile = stringappend (name_base, short_base_length, ".guard.c"); | |
54bd0db4 RS |
344 | #endif /* not MSDOS */ |
345 | #endif /* not VMS */ | |
346 | } | |
347 | ||
348 | ||
349 | ||
0d533154 AD |
350 | /*--------------------------------------------------------------------. |
351 | | Open the output files needed only for the semantic parser. This | | |
352 | | is done when %semantic_parser is seen in the declarations section. | | |
353 | `--------------------------------------------------------------------*/ | |
54bd0db4 RS |
354 | |
355 | void | |
bd088c91 | 356 | open_extra_files (void) |
54bd0db4 RS |
357 | { |
358 | FILE *ftmp; | |
359 | int c; | |
a693bf18 JT |
360 | char *filename; |
361 | #ifdef MSDOS | |
362 | char *cp; | |
363 | #endif | |
54bd0db4 | 364 | |
8963a27b | 365 | xfclose (fparser); |
54bd0db4 | 366 | |
8963a27b | 367 | if (!noparserflag) |
54bd0db4 | 368 | { |
28683c54 RS |
369 | filename = (char *) getenv ("BISON_HAIRY"); |
370 | #ifdef MSDOS | |
371 | /* File doesn't exist in current directory; try in INIT directory. */ | |
8963a27b | 372 | cp = getenv ("INIT"); |
28683c54 | 373 | if (filename == 0 && cp != NULL) |
8963a27b | 374 | { |
d7913476 | 375 | filename = XMALLOC (char, strlen (cp) + strlen (PFILE1) + 2); |
8963a27b AD |
376 | strcpy (filename, cp); |
377 | cp = filename + strlen (filename); | |
378 | *cp++ = '/'; | |
379 | strcpy (cp, PFILE1); | |
380 | } | |
54bd0db4 | 381 | #endif |
8963a27b | 382 | fparser = xfopen (filename ? filename : PFILE1, "r"); |
28683c54 | 383 | } |
54bd0db4 | 384 | |
8963a27b AD |
385 | /* JF change from inline attrs file to separate one */ |
386 | ftmp = xfopen (attrsfile, "w"); | |
387 | rewind (fattrs); | |
388 | while ((c = getc (fattrs)) != EOF) /* Thank god for buffering */ | |
389 | putc (c, ftmp); | |
390 | xfclose (fattrs); | |
391 | fattrs = ftmp; | |
54bd0db4 | 392 | |
8963a27b | 393 | fguard = xfopen (guardfile, "w"); |
54bd0db4 RS |
394 | |
395 | } | |
396 | ||
a693bf18 | 397 | void |
a0f6b076 | 398 | done (void) |
a693bf18 | 399 | { |
8963a27b AD |
400 | xfclose (faction); |
401 | xfclose (fattrs); | |
402 | xfclose (fguard); | |
403 | xfclose (finput); | |
404 | xfclose (fparser); | |
405 | xfclose (foutput); | |
54bd0db4 | 406 | |
a0f6b076 AD |
407 | /* JF write out the output file */ |
408 | if (!complain_message_count && ftable) | |
54bd0db4 RS |
409 | { |
410 | FILE *ftmp; | |
411 | register int c; | |
412 | ||
8963a27b AD |
413 | ftmp = xfopen (tabfile, "w"); |
414 | rewind (ftable); | |
415 | while ((c = getc (ftable)) != EOF) | |
416 | putc (c, ftmp); | |
417 | xfclose (ftmp); | |
418 | xfclose (ftable); | |
54bd0db4 RS |
419 | |
420 | if (definesflag) | |
8963a27b AD |
421 | { |
422 | ftmp = xfopen (defsfile, "w"); | |
423 | fflush (fdefines); | |
424 | rewind (fdefines); | |
425 | while ((c = getc (fdefines)) != EOF) | |
426 | putc (c, ftmp); | |
427 | xfclose (ftmp); | |
428 | xfclose (fdefines); | |
429 | } | |
54bd0db4 RS |
430 | } |
431 | ||
fcca25ad | 432 | #if defined (VMS) & !defined (__VMS_POSIX) |
8963a27b AD |
433 | if (faction && !noparserflag) |
434 | delete (actfile); | |
54bd0db4 | 435 | if (fattrs) |
8963a27b | 436 | delete (tmpattrsfile); |
54bd0db4 | 437 | if (ftable) |
8963a27b | 438 | delete (tmptabfile); |
a0f6b076 AD |
439 | /* Don't call exit again, we're in atexit (). |
440 | if (!complain_message_count) | |
441 | sys$exit(SS$_NORMAL); | |
442 | sys$exit(SS$_ABORT); */ | |
54bd0db4 | 443 | #else |
e0672a61 | 444 | #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__))) |
8963a27b AD |
445 | if (actfile && !noparserflag) |
446 | unlink (actfile); | |
447 | if (tmpattrsfile) | |
448 | unlink (tmpattrsfile); | |
449 | if (tmptabfile) | |
450 | unlink (tmptabfile); | |
451 | if (tmpdefsfile) | |
452 | unlink (tmpdefsfile); | |
e0672a61 | 453 | #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */ |
a0f6b076 AD |
454 | /* Don't call exit again, we're in atexit (). |
455 | exit (complain_message_count ? 1 : 0); */ | |
fcca25ad | 456 | #endif /* not VMS, or __VMS_POSIX */ |
54bd0db4 | 457 | } |