]>
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #if defined (VMS) & !defined (__VMS_POSIX)
26 #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
29 #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
33 #if defined (_MSC_VER)
35 #define XPFILE "c:/usr/local/lib/bison.simple"
38 #define XPFILE1 "c:/usr/local/lib/bison.hairy"
50 FILE *fdefines
= NULL
;
57 /* File name specified with -o for the output file, or 0 if no -o. */
71 extern int noparserflag
;
73 extern char *mktemp(); /* So the compiler won't complain */
74 extern char *getenv();
77 char *stringappend
PARAMS((char *, int, char *));
78 void openfiles
PARAMS((void));
79 void open_extra_files
PARAMS((void));
80 FILE *tryopen
PARAMS((char *, char *)); /* This might be a good idea */
81 void done
PARAMS((int));
83 extern char *program_name
;
84 extern int verboseflag
;
85 extern int definesflag
;
86 int fixed_outfiles
= 0;
90 stringappend (char *string1
, int end1
, char *string2
)
92 register char *ostring
;
93 register char *cp
, *cp1
;
99 ostring
= NEW2(i
+end1
+1, char);
103 for (i
= 0; i
< end1
; i
++)
107 while ((*cp
++ = *cp1
++))
114 /* JF this has been hacked to death. Nowaday it sets up the file names for
115 the output files, and opens the tmp files and the parser */
123 int short_base_length
;
125 #if defined (VMS) & !defined (__VMS_POSIX)
126 char *tmp_base
= "sys$scratch:b_";
128 char *tmp_base
= "/tmp/b.";
133 tmp_base
= getenv ("TMP");
139 #if (defined(_WIN32) && !defined(__CYGWIN32__))
140 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
142 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
146 #endif /* _WIN32 && !__CYGWIN32__ */
148 #if (defined(unix) || defined(__unix) || defined(__unix__))
150 char *tmp_ptr
= getenv("TMPDIR");
153 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
155 #endif /* unix || __unix || __unix__ */
157 tmp_len
= strlen (tmp_base
);
161 /* -o was specified. The precise -o name will be used for ftable.
162 For other output files, remove the ".c" or ".tab.c" suffix. */
163 name_base
= spec_outfile
;
167 /* BASE_LENGTH includes ".tab" but not ".c". */
168 base_length
= strlen (name_base
);
169 if (!strcmp (name_base
+ base_length
- 2, ".c"))
171 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
172 short_base_length
= base_length
;
173 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
174 short_base_length
-= 4;
175 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
176 short_base_length
-= 4;
178 else if (spec_file_prefix
)
180 /* -b was specified. Construct names from it. */
181 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
182 short_base_length
= strlen (spec_file_prefix
);
183 /* Count room for `.tab'. */
184 base_length
= short_base_length
+ 4;
185 name_base
= (char *) xmalloc (base_length
+ 1);
187 strcpy (name_base
, spec_file_prefix
);
189 strcat (name_base
, "_tab");
191 strcat (name_base
, ".tab");
199 /* -o was not specified; compute output file name from input
200 or use y.tab.c, etc., if -y was specified. */
202 name_base
= fixed_outfiles
? "y.y" : infile
;
204 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
206 base_length
= strlen (name_base
);
207 if (!strcmp (name_base
+ base_length
- 2, ".y"))
209 short_base_length
= base_length
;
212 name_base
= stringappend(name_base
, short_base_length
, "_tab");
215 name_base
= stringappend(name_base
, short_base_length
, "_tab");
217 name_base
= stringappend(name_base
, short_base_length
, ".tab");
218 #endif /* not MSDOS */
220 base_length
= short_base_length
+ 4;
223 finput
= tryopen(infile
, "r");
227 filename
= getenv("BISON_SIMPLE");
229 /* File doesn't exist in current directory; try in INIT directory. */
231 if (filename
== 0 && cp
!= NULL
)
233 filename
= xmalloc(strlen(cp
) + strlen(PFILE
) + 2);
234 strcpy(filename
, cp
);
235 cp
= filename
+ strlen(filename
);
240 fparser
= tryopen(filename
? filename
: PFILE
, "r");
246 outfile
= stringappend(name_base
, short_base_length
, ".out");
248 /* We used to use just .out if spec_name_prefix (-p) was used,
249 but that conflicts with Posix. */
250 outfile
= stringappend(name_base
, short_base_length
, ".output");
252 foutput
= tryopen(outfile
, "w");
257 /* use permanent name for actions file */
258 actfile
= stringappend(name_base
, short_base_length
, ".act");
259 faction
= tryopen(actfile
, "w");
264 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "acXXXXXX"));
265 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "atXXXXXX"));
266 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "taXXXXXX"));
267 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "deXXXXXX"));
270 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "act.XXXXXX"));
271 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "attrs.XXXXXX"));
272 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "tab.XXXXXX"));
273 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "defs.XXXXXX"));
274 #endif /* not MSDOS */
277 faction
= tryopen(actfile
, "w+");
278 fattrs
= tryopen(tmpattrsfile
,"w+");
279 ftable
= tryopen(tmptabfile
, "w+");
283 defsfile
= stringappend(name_base
, base_length
, ".h");
284 fdefines
= tryopen(tmpdefsfile
, "w+");
287 #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
290 unlink(tmpattrsfile
);
293 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
295 /* These are opened by `done' or `open_extra_files', if at all */
297 tabfile
= spec_outfile
;
299 tabfile
= stringappend(name_base
, base_length
, ".c");
302 attrsfile
= stringappend(name_base
, short_base_length
, "_stype.h");
303 guardfile
= stringappend(name_base
, short_base_length
, "_guard.c");
306 attrsfile
= stringappend(name_base
, short_base_length
, ".sth");
307 guardfile
= stringappend(name_base
, short_base_length
, ".guc");
309 attrsfile
= stringappend(name_base
, short_base_length
, ".stype.h");
310 guardfile
= stringappend(name_base
, short_base_length
, ".guard.c");
311 #endif /* not MSDOS */
317 /* open the output files needed only for the semantic parser.
318 This is done when %semantic_parser is seen in the declarations section. */
321 open_extra_files (void)
332 filename
= (char *) getenv ("BISON_HAIRY");
334 /* File doesn't exist in current directory; try in INIT directory. */
336 if (filename
== 0 && cp
!= NULL
)
338 filename
= xmalloc(strlen(cp
) + strlen(PFILE1
) + 2);
339 strcpy(filename
, cp
);
340 cp
= filename
+ strlen(filename
);
345 fparser
= tryopen(filename
? filename
: PFILE1
, "r");
348 /* JF change from inline attrs file to separate one */
349 ftmp
= tryopen(attrsfile
, "w");
351 while((c
=getc(fattrs
))!=EOF
) /* Thank god for buffering */
356 fguard
= tryopen(guardfile
, "w");
360 /* JF to make file opening easier. This func tries to open file
361 NAME with mode MODE, and prints an error message if it fails. */
363 tryopen (char *name
, char *mode
)
367 ptr
= fopen(name
, mode
);
370 fprintf(stderr
, "%s: ", program_name
);
398 /* JF write out the output file */
399 if (k
== 0 && ftable
)
404 ftmp
=tryopen(tabfile
, "w");
406 while((c
=getc(ftable
)) != EOF
)
413 ftmp
= tryopen(defsfile
, "w");
416 while((c
=getc(fdefines
)) != EOF
)
423 #if defined (VMS) & !defined (__VMS_POSIX)
424 if (faction
&& ! noparserflag
)
427 delete(tmpattrsfile
);
430 if (k
==0) sys$
exit(SS$_NORMAL
);
433 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
434 if (actfile
&& ! noparserflag
) unlink(actfile
);
435 if (tmpattrsfile
) unlink(tmpattrsfile
);
436 if (tmptabfile
) unlink(tmptabfile
);
437 if (tmpdefsfile
) unlink(tmpdefsfile
);
438 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
440 #endif /* not VMS, or __VMS_POSIX */