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