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