]> git.saurik.com Git - bison.git/blame - src/files.c
entered into RCS
[bison.git] / src / files.c
CommitLineData
54bd0db4
RS
1/* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#ifdef VMS
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
38FILE *finput = NULL;
39FILE *foutput = NULL;
40FILE *fdefines = NULL;
41FILE *ftable = NULL;
42FILE *fattrs = NULL;
43FILE *fguard = NULL;
44FILE *faction = NULL;
45FILE *fparser = NULL;
46
47/* File name specified with -o for the output file, or 0 if no -o. */
48char *spec_outfile;
49
50char *infile;
51char *outfile;
52char *defsfile;
53char *tabfile;
54char *attrsfile;
55char *guardfile;
56char *actfile;
57char *tmpattrsfile;
58char *tmptabfile;
59char *tmpdefsfile;
60
61extern char *mktemp(); /* So the compiler won't complain */
62extern char *getenv();
63extern void perror();
64FILE *tryopen(); /* This might be a good idea */
65void done();
66
67extern char *program_name;
68extern int verboseflag;
69extern int definesflag;
70int fixed_outfiles = 0;
71\f
72
73char*
74stringappend(string1, end1, string2)
75char *string1;
76int end1;
77char *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 */
102void
103openfiles()
104{
105 char *name_base;
106 register char *cp;
107 char *filename;
108 int base_length;
109 int short_base_length;
110
111#ifdef VMS
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;
40675e7c 153 name_base = (char *) xmalloc (base_length + 1);
54bd0db4
RS
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");
40675e7c 197 if (filename == 0 && cp != NULL)
54bd0db4 198 {
40675e7c 199 filename = xmalloc(strlen(cp) + strlen(PFILE) + 2);
54bd0db4
RS
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.
272This is done when %semantic_parser is seen in the declarations section. */
273
274void
275open_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");
40675e7c 287 if (filename == 0 && cp != NULL)
54bd0db4 288 {
40675e7c 289 filename = xmalloc(strlen(cp) + strlen(PFILE1) + 2);
54bd0db4
RS
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. */
312FILE *
313tryopen(name, mode)
314char *name;
315char *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
329void
330done(k)
331int 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#ifdef VMS
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 */
394}