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