]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989 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. */
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 char *mktemp(); /* So the compiler won't complain */
62 extern char *getenv();
64 FILE *tryopen(); /* This might be a good idea */
67 extern char *program_name
;
68 extern int verboseflag
;
69 extern int definesflag
;
70 int fixed_outfiles
= 0;
74 stringappend(string1
, end1
, string2
)
79 register char *ostring
;
80 register char *cp
, *cp1
;
86 ostring
= NEW2(i
+end1
+1, char);
90 for (i
= 0; i
< end1
; i
++)
94 while (*cp
++ = *cp1
++) ;
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 */
109 int short_base_length
;
112 char *tmp_base
= "sys$scratch:b_";
114 char *tmp_base
= "/tmp/b.";
119 tmp_base
= getenv ("TMP");
125 tmp_len
= strlen (tmp_base
);
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
;
135 /* BASE_LENGTH includes ".tab" but not ".c". */
136 base_length
= strlen (name_base
);
137 if (!strcmp (name_base
+ base_length
- 2, ".c"))
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;
146 else if (spec_file_prefix
)
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;
153 name_base
= (char *) xmalloc (base_length
+ 1);
155 strcpy (name_base
, spec_file_prefix
);
157 strcat (name_base
, "_tab");
159 strcat (name_base
, ".tab");
167 /* -o was not specified; compute output file name from input
168 or use y.tab.c, etc., if -y was specified. */
170 name_base
= fixed_outfiles
? "y.y" : infile
;
172 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
174 base_length
= strlen (name_base
);
175 if (!strcmp (name_base
+ base_length
- 2, ".y"))
177 short_base_length
= base_length
;
180 name_base
= stringappend(name_base
, short_base_length
, "_tab");
183 name_base
= stringappend(name_base
, short_base_length
, "_tab");
185 name_base
= stringappend(name_base
, short_base_length
, ".tab");
186 #endif /* not MSDOS */
188 base_length
= short_base_length
+ 4;
191 finput
= tryopen(infile
, "r");
193 filename
= getenv("BISON_SIMPLE");
195 /* File doesn't exist in current directory; try in INIT directory. */
197 if (filename
== 0 && cp
!= NULL
)
199 filename
= xmalloc(strlen(cp
) + strlen(PFILE
) + 2);
200 strcpy(filename
, cp
);
201 cp
= filename
+ strlen(filename
);
206 fparser
= tryopen(filename
? filename
: PFILE
, "r");
211 outfile
= stringappend(name_base
, short_base_length
, ".out");
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");
217 foutput
= tryopen(outfile
, "w");
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"));
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 */
232 faction
= tryopen(actfile
, "w+");
233 fattrs
= tryopen(tmpattrsfile
,"w+");
234 ftable
= tryopen(tmptabfile
, "w+");
238 defsfile
= stringappend(name_base
, base_length
, ".h");
239 fdefines
= tryopen(tmpdefsfile
, "w+");
244 unlink(tmpattrsfile
);
249 /* These are opened by `done' or `open_extra_files', if at all */
251 tabfile
= spec_outfile
;
253 tabfile
= stringappend(name_base
, base_length
, ".c");
256 attrsfile
= stringappend(name_base
, short_base_length
, "_stype.h");
257 guardfile
= stringappend(name_base
, short_base_length
, "_guard.c");
260 attrsfile
= stringappend(name_base
, short_base_length
, ".sth");
261 guardfile
= stringappend(name_base
, short_base_length
, ".guc");
263 attrsfile
= stringappend(name_base
, short_base_length
, ".stype.h");
264 guardfile
= stringappend(name_base
, short_base_length
, ".guard.c");
265 #endif /* not MSDOS */
271 /* open the output files needed only for the semantic parser.
272 This is done when %semantic_parser is seen in the declarations section. */
283 filename
= (char *) getenv ("BISON_HAIRY");
285 /* File doesn't exist in current directory; try in INIT directory. */
287 if (filename
== 0 && cp
!= NULL
)
289 filename
= xmalloc(strlen(cp
) + strlen(PFILE1
) + 2);
290 strcpy(filename
, cp
);
291 cp
= filename
+ strlen(filename
);
296 fparser
= tryopen(filename
? filename
: PFILE1
, "r");
298 /* JF change from inline attrs file to separate one */
299 ftmp
= tryopen(attrsfile
, "w");
301 while((c
=getc(fattrs
))!=EOF
) /* Thank god for buffering */
306 fguard
= tryopen(guardfile
, "w");
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. */
319 ptr
= fopen(name
, mode
);
322 fprintf(stderr
, "%s: ", program_name
);
351 /* JF write out the output file */
352 if (k
== 0 && ftable
)
357 ftmp
=tryopen(tabfile
, "w");
359 while((c
=getc(ftable
)) != EOF
)
366 ftmp
= tryopen(defsfile
, "w");
369 while((c
=getc(fdefines
)) != EOF
)
380 delete(tmpattrsfile
);
383 if (k
==0) sys$
exit(SS$_NORMAL
);
387 if (actfile
) unlink(actfile
);
388 if (tmpattrsfile
) unlink(tmpattrsfile
);
389 if (tmptabfile
) unlink(tmptabfile
);
390 if (tmpdefsfile
) unlink(tmpdefsfile
);