]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
21 #if defined (VMS) & !defined (__VMS_POSIX)
25 #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
28 #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
40 FILE *fdefines
= NULL
;
47 /* File name specified with -o for the output file, or 0 if no -o. */
61 extern int noparserflag
;
63 extern char *mktemp(); /* So the compiler won't complain */
64 extern char *getenv();
66 FILE *tryopen(); /* This might be a good idea */
69 extern char *program_name
;
70 extern int verboseflag
;
71 extern int definesflag
;
72 int fixed_outfiles
= 0;
76 stringappend(string1
, end1
, string2
)
81 register char *ostring
;
82 register char *cp
, *cp1
;
88 ostring
= NEW2(i
+end1
+1, char);
92 for (i
= 0; i
< end1
; i
++)
96 while (*cp
++ = *cp1
++) ;
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 */
111 int short_base_length
;
113 #if defined (VMS) & !defined (__VMS_POSIX)
114 char *tmp_base
= "sys$scratch:b_";
116 char *tmp_base
= "/tmp/b.";
121 tmp_base
= getenv ("TMP");
127 tmp_len
= strlen (tmp_base
);
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
;
137 /* BASE_LENGTH includes ".tab" but not ".c". */
138 base_length
= strlen (name_base
);
139 if (!strcmp (name_base
+ base_length
- 2, ".c"))
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;
148 else if (spec_file_prefix
)
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);
157 strcpy (name_base
, spec_file_prefix
);
159 strcat (name_base
, "_tab");
161 strcat (name_base
, ".tab");
169 /* -o was not specified; compute output file name from input
170 or use y.tab.c, etc., if -y was specified. */
172 name_base
= fixed_outfiles
? "y.y" : infile
;
174 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
176 base_length
= strlen (name_base
);
177 if (!strcmp (name_base
+ base_length
- 2, ".y"))
179 short_base_length
= base_length
;
182 name_base
= stringappend(name_base
, short_base_length
, "_tab");
185 name_base
= stringappend(name_base
, short_base_length
, "_tab");
187 name_base
= stringappend(name_base
, short_base_length
, ".tab");
188 #endif /* not MSDOS */
190 base_length
= short_base_length
+ 4;
193 finput
= tryopen(infile
, "r");
197 filename
= getenv("BISON_SIMPLE");
199 /* File doesn't exist in current directory; try in INIT directory. */
201 if (filename
== 0 && cp
!= NULL
)
203 filename
= xmalloc(strlen(cp
) + strlen(PFILE
) + 2);
204 strcpy(filename
, cp
);
205 cp
= filename
+ strlen(filename
);
210 fparser
= tryopen(filename
? filename
: PFILE
, "r");
216 outfile
= stringappend(name_base
, short_base_length
, ".out");
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");
222 foutput
= tryopen(outfile
, "w");
227 /* use permanent name for actions file */
228 actfile
= stringappend(name_base
, short_base_length
, ".act");
229 faction
= tryopen(actfile
, "w");
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"));
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 */
247 faction
= tryopen(actfile
, "w+");
248 fattrs
= tryopen(tmpattrsfile
,"w+");
249 ftable
= tryopen(tmptabfile
, "w+");
253 defsfile
= stringappend(name_base
, base_length
, ".h");
254 fdefines
= tryopen(tmpdefsfile
, "w+");
260 unlink(tmpattrsfile
);
265 /* These are opened by `done' or `open_extra_files', if at all */
267 tabfile
= spec_outfile
;
269 tabfile
= stringappend(name_base
, base_length
, ".c");
272 attrsfile
= stringappend(name_base
, short_base_length
, "_stype.h");
273 guardfile
= stringappend(name_base
, short_base_length
, "_guard.c");
276 attrsfile
= stringappend(name_base
, short_base_length
, ".sth");
277 guardfile
= stringappend(name_base
, short_base_length
, ".guc");
279 attrsfile
= stringappend(name_base
, short_base_length
, ".stype.h");
280 guardfile
= stringappend(name_base
, short_base_length
, ".guard.c");
281 #endif /* not MSDOS */
287 /* open the output files needed only for the semantic parser.
288 This is done when %semantic_parser is seen in the declarations section. */
302 filename
= (char *) getenv ("BISON_HAIRY");
304 /* File doesn't exist in current directory; try in INIT directory. */
306 if (filename
== 0 && cp
!= NULL
)
308 filename
= xmalloc(strlen(cp
) + strlen(PFILE1
) + 2);
309 strcpy(filename
, cp
);
310 cp
= filename
+ strlen(filename
);
315 fparser
= tryopen(filename
? filename
: PFILE1
, "r");
318 /* JF change from inline attrs file to separate one */
319 ftmp
= tryopen(attrsfile
, "w");
321 while((c
=getc(fattrs
))!=EOF
) /* Thank god for buffering */
326 fguard
= tryopen(guardfile
, "w");
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. */
339 ptr
= fopen(name
, mode
);
342 fprintf(stderr
, "%s: ", program_name
);
371 /* JF write out the output file */
372 if (k
== 0 && ftable
)
377 ftmp
=tryopen(tabfile
, "w");
379 while((c
=getc(ftable
)) != EOF
)
386 ftmp
= tryopen(defsfile
, "w");
389 while((c
=getc(fdefines
)) != EOF
)
396 #if defined (VMS) & !defined (__VMS_POSIX)
397 if (faction
&& ! noparserflag
)
400 delete(tmpattrsfile
);
403 if (k
==0) sys$
exit(SS$_NORMAL
);
407 if (actfile
&& ! noparserflag
) unlink(actfile
);
408 if (tmpattrsfile
) unlink(tmpattrsfile
);
409 if (tmptabfile
) unlink(tmptabfile
);
410 if (tmpdefsfile
) unlink(tmpdefsfile
);
413 #endif /* not VMS, or __VMS_POSIX */