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