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