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