]> git.saurik.com Git - bison.git/blob - src/files.c
(__yy_memcpy): Really reorder the args, as was supposedly done on Feb 14 1995.
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989, 1992 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 int noparserflag;
62
63 extern char *mktemp(); /* So the compiler won't complain */
64 extern char *getenv();
65 extern void perror();
66 FILE *tryopen(); /* This might be a good idea */
67 void done();
68
69 extern char *program_name;
70 extern int verboseflag;
71 extern int definesflag;
72 int fixed_outfiles = 0;
73 \f
74
75 char*
76 stringappend(string1, end1, string2)
77 char *string1;
78 int end1;
79 char *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 */
104 void
105 openfiles()
106 {
107 char *name_base;
108 register char *cp;
109 char *filename;
110 int base_length;
111 int short_base_length;
112
113 #if defined (VMS) & !defined (__VMS_POSIX)
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;
155 name_base = (char *) xmalloc (base_length + 1);
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
195 if (! noparserflag)
196 {
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 }
209 #endif /* MSDOS */
210 fparser = tryopen(filename ? filename : PFILE, "r");
211 }
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
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
232 #ifdef MSDOS
233 if (! noparserflag)
234 actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
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
239 if (! noparserflag)
240 actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
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
246 if (! noparserflag)
247 faction = tryopen(actfile, "w+");
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
258 if (! noparserflag)
259 unlink(actfile);
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.
288 This is done when %semantic_parser is seen in the declarations section. */
289
290 void
291 open_extra_files()
292 {
293 FILE *ftmp;
294 int c;
295 char *filename, *cp;
296
297 if (fparser)
298 fclose(fparser);
299
300 if (! noparserflag)
301 {
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 }
314 #endif
315 fparser= tryopen(filename ? filename : PFILE1, "r");
316 }
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. */
332 FILE *
333 tryopen(name, mode)
334 char *name;
335 char *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
349 void
350 done(k)
351 int 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
396 #if defined (VMS) & !defined (__VMS_POSIX)
397 if (faction && ! noparserflag)
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
407 if (actfile && ! noparserflag) unlink(actfile);
408 if (tmpattrsfile) unlink(tmpattrsfile);
409 if (tmptabfile) unlink(tmptabfile);
410 if (tmpdefsfile) unlink(tmpdefsfile);
411 #endif /* MSDOS */
412 exit(k);
413 #endif /* not VMS, or __VMS_POSIX */
414 }