]>
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"
32 #if defined (_MSC_VER)
34 #define XPFILE "c:/usr/local/lib/bison.simple"
37 #define XPFILE1 "c:/usr/local/lib/bison.hairy"
49 FILE *fdefines
= NULL
;
56 /* File name specified with -o for the output file, or 0 if no -o. */
70 extern int noparserflag
;
72 extern char *mktemp(); /* So the compiler won't complain */
73 extern char *getenv();
76 char *stringappend
PARAMS((char *, int, char *));
77 void openfiles
PARAMS((void));
78 void open_extra_files
PARAMS((void));
79 FILE *tryopen
PARAMS((char *, char *)); /* This might be a good idea */
80 void done
PARAMS((int));
82 extern char *program_name
;
83 extern int verboseflag
;
84 extern int definesflag
;
85 int fixed_outfiles
= 0;
89 stringappend (char *string1
, int end1
, char *string2
)
91 register char *ostring
;
92 register char *cp
, *cp1
;
98 ostring
= NEW2(i
+end1
+1, char);
102 for (i
= 0; i
< end1
; i
++)
106 while ((*cp
++ = *cp1
++))
113 /* JF this has been hacked to death. Nowaday it sets up the file names for
114 the output files, and opens the tmp files and the parser */
122 int short_base_length
;
124 #if defined (VMS) & !defined (__VMS_POSIX)
125 char *tmp_base
= "sys$scratch:b_";
127 char *tmp_base
= "/tmp/b.";
132 tmp_base
= getenv ("TMP");
138 #if (defined(_WIN32) && !defined(__CYGWIN32__))
139 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
141 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
145 #endif /* _WIN32 && !__CYGWIN32__ */
147 #if (defined(unix) || defined(__unix) || defined(__unix__))
149 char *tmp_ptr
= getenv("TMPDIR");
152 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
154 #endif /* unix || __unix || __unix__ */
156 tmp_len
= strlen (tmp_base
);
160 /* -o was specified. The precise -o name will be used for ftable.
161 For other output files, remove the ".c" or ".tab.c" suffix. */
162 name_base
= spec_outfile
;
166 /* BASE_LENGTH includes ".tab" but not ".c". */
167 base_length
= strlen (name_base
);
168 if (!strcmp (name_base
+ base_length
- 2, ".c"))
170 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
171 short_base_length
= base_length
;
172 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
173 short_base_length
-= 4;
174 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
175 short_base_length
-= 4;
177 else if (spec_file_prefix
)
179 /* -b was specified. Construct names from it. */
180 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
181 short_base_length
= strlen (spec_file_prefix
);
182 /* Count room for `.tab'. */
183 base_length
= short_base_length
+ 4;
184 name_base
= (char *) xmalloc (base_length
+ 1);
186 strcpy (name_base
, spec_file_prefix
);
188 strcat (name_base
, "_tab");
190 strcat (name_base
, ".tab");
198 /* -o was not specified; compute output file name from input
199 or use y.tab.c, etc., if -y was specified. */
201 name_base
= fixed_outfiles
? "y.y" : infile
;
203 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
205 base_length
= strlen (name_base
);
206 if (!strcmp (name_base
+ base_length
- 2, ".y"))
208 short_base_length
= base_length
;
211 name_base
= stringappend(name_base
, short_base_length
, "_tab");
214 name_base
= stringappend(name_base
, short_base_length
, "_tab");
216 name_base
= stringappend(name_base
, short_base_length
, ".tab");
217 #endif /* not MSDOS */
219 base_length
= short_base_length
+ 4;
222 finput
= tryopen(infile
, "r");
226 filename
= getenv("BISON_SIMPLE");
228 /* File doesn't exist in current directory; try in INIT directory. */
230 if (filename
== 0 && cp
!= NULL
)
232 filename
= xmalloc(strlen(cp
) + strlen(PFILE
) + 2);
233 strcpy(filename
, cp
);
234 cp
= filename
+ strlen(filename
);
239 fparser
= tryopen(filename
? filename
: PFILE
, "r");
245 outfile
= stringappend(name_base
, short_base_length
, ".out");
247 /* We used to use just .out if spec_name_prefix (-p) was used,
248 but that conflicts with Posix. */
249 outfile
= stringappend(name_base
, short_base_length
, ".output");
251 foutput
= tryopen(outfile
, "w");
256 /* use permanent name for actions file */
257 actfile
= stringappend(name_base
, short_base_length
, ".act");
258 faction
= tryopen(actfile
, "w");
263 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "acXXXXXX"));
264 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "atXXXXXX"));
265 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "taXXXXXX"));
266 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "deXXXXXX"));
269 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "act.XXXXXX"));
270 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "attrs.XXXXXX"));
271 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "tab.XXXXXX"));
272 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "defs.XXXXXX"));
273 #endif /* not MSDOS */
276 faction
= tryopen(actfile
, "w+");
277 fattrs
= tryopen(tmpattrsfile
,"w+");
278 ftable
= tryopen(tmptabfile
, "w+");
282 defsfile
= stringappend(name_base
, base_length
, ".h");
283 fdefines
= tryopen(tmpdefsfile
, "w+");
286 #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
289 unlink(tmpattrsfile
);
292 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
294 /* These are opened by `done' or `open_extra_files', if at all */
296 tabfile
= spec_outfile
;
298 tabfile
= stringappend(name_base
, base_length
, ".c");
301 attrsfile
= stringappend(name_base
, short_base_length
, "_stype.h");
302 guardfile
= stringappend(name_base
, short_base_length
, "_guard.c");
305 attrsfile
= stringappend(name_base
, short_base_length
, ".sth");
306 guardfile
= stringappend(name_base
, short_base_length
, ".guc");
308 attrsfile
= stringappend(name_base
, short_base_length
, ".stype.h");
309 guardfile
= stringappend(name_base
, short_base_length
, ".guard.c");
310 #endif /* not MSDOS */
316 /* open the output files needed only for the semantic parser.
317 This is done when %semantic_parser is seen in the declarations section. */
320 open_extra_files (void)
331 filename
= (char *) getenv ("BISON_HAIRY");
333 /* File doesn't exist in current directory; try in INIT directory. */
335 if (filename
== 0 && cp
!= NULL
)
337 filename
= xmalloc(strlen(cp
) + strlen(PFILE1
) + 2);
338 strcpy(filename
, cp
);
339 cp
= filename
+ strlen(filename
);
344 fparser
= tryopen(filename
? filename
: PFILE1
, "r");
347 /* JF change from inline attrs file to separate one */
348 ftmp
= tryopen(attrsfile
, "w");
350 while((c
=getc(fattrs
))!=EOF
) /* Thank god for buffering */
355 fguard
= tryopen(guardfile
, "w");
359 /* JF to make file opening easier. This func tries to open file
360 NAME with mode MODE, and prints an error message if it fails. */
362 tryopen (char *name
, char *mode
)
366 ptr
= fopen(name
, mode
);
369 fprintf(stderr
, "%s: ", program_name
);
397 /* JF write out the output file */
398 if (k
== 0 && ftable
)
403 ftmp
=tryopen(tabfile
, "w");
405 while((c
=getc(ftable
)) != EOF
)
412 ftmp
= tryopen(defsfile
, "w");
415 while((c
=getc(fdefines
)) != EOF
)
422 #if defined (VMS) & !defined (__VMS_POSIX)
423 if (faction
&& ! noparserflag
)
426 delete(tmpattrsfile
);
429 if (k
==0) sys$
exit(SS$_NORMAL
);
432 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
433 if (actfile
&& ! noparserflag
) unlink(actfile
);
434 if (tmpattrsfile
) unlink(tmpattrsfile
);
435 if (tmptabfile
) unlink(tmptabfile
);
436 if (tmpdefsfile
) unlink(tmpdefsfile
);
437 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
439 #endif /* not VMS, or __VMS_POSIX */