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