]>
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. */
24 #if defined (VMS) & !defined (__VMS_POSIX)
28 #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
31 #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
35 #if defined (_MSC_VER)
37 #define XPFILE "c:/usr/local/lib/bison.simple"
40 #define XPFILE1 "c:/usr/local/lib/bison.hairy"
57 FILE *fdefines
= NULL
;
64 /* File name specified with -o for the output file, or 0 if no -o. */
71 static char *defsfile
;
73 static char *guardfile
;
75 static char *tmpattrsfile
;
76 static char *tmptabfile
;
77 static char *tmpdefsfile
;
79 extern int noparserflag
;
81 extern char *mktemp(); /* So the compiler won't complain */
82 extern char *getenv();
84 extern char *stringappend
PARAMS((const char *, int, const char *));
85 extern void openfiles
PARAMS((void));
86 extern void open_extra_files
PARAMS((void));
88 int fixed_outfiles
= 0;
90 extern char *program_name
;
91 extern int verboseflag
;
92 extern int definesflag
;
96 stringappend (const char *string1
, int end1
, const char *string2
)
98 register char *ostring
;
100 register const char *cp1
;
107 ostring
= NEW2(i
+end1
+1, char);
111 for (i
= 0; i
< end1
; i
++)
115 while ((*cp
++ = *cp1
++))
121 /*-----------------------------------------------------------------.
122 | Try to open file NAME with mode MODE, and print an error message |
124 `-----------------------------------------------------------------*/
127 tryopen (const char *name
, const char *mode
)
131 ptr
= fopen (name
, mode
);
133 error (2, errno
, _("cannot open file `%s'"), name
);
138 /*-------------------------------------------------------------.
139 | Try to close file PTR, and print an error message if fails. |
140 `-------------------------------------------------------------*/
150 result
= fclose (ptr
);
152 error (2, errno
, _("cannot close file"));
157 /* JF this has been hacked to death. Nowaday it sets up the file names for
158 the output files, and opens the tmp files and the parser */
168 int short_base_length
;
170 #if defined (VMS) & !defined (__VMS_POSIX)
171 const char *tmp_base
= "sys$scratch:b_";
173 const char *tmp_base
= "/tmp/b.";
178 tmp_base
= getenv ("TMP");
184 #if (defined(_WIN32) && !defined(__CYGWIN32__))
185 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
187 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
191 #endif /* _WIN32 && !__CYGWIN32__ */
193 #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
195 char *tmp_ptr
= getenv("TMPDIR");
198 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
200 #endif /* unix || __unix || __unix__ */
202 tmp_len
= strlen (tmp_base
);
206 /* -o was specified. The precise -o name will be used for ftable.
207 For other output files, remove the ".c" or ".tab.c" suffix. */
208 name_base
= spec_outfile
;
212 /* BASE_LENGTH includes ".tab" but not ".c". */
213 base_length
= strlen (name_base
);
214 if (!strcmp (name_base
+ base_length
- 2, ".c"))
216 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
217 short_base_length
= base_length
;
218 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
219 short_base_length
-= 4;
220 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
221 short_base_length
-= 4;
223 else if (spec_file_prefix
)
225 /* -b was specified. Construct names from it. */
226 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
227 short_base_length
= strlen (spec_file_prefix
);
228 /* Count room for `.tab'. */
229 base_length
= short_base_length
+ 4;
230 name_base
= (char *) xmalloc (base_length
+ 1);
232 strcpy (name_base
, spec_file_prefix
);
234 strcat (name_base
, "_tab");
236 strcat (name_base
, ".tab");
244 /* -o was not specified; compute output file name from input
245 or use y.tab.c, etc., if -y was specified. */
247 static char FIXED_NAME_BASE
[] = "y.y";
249 name_base
= fixed_outfiles
? FIXED_NAME_BASE
: infile
;
251 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
253 base_length
= strlen (name_base
);
254 if (!strcmp (name_base
+ base_length
- 2, ".y"))
256 short_base_length
= base_length
;
259 name_base
= stringappend(name_base
, short_base_length
, "_tab");
262 name_base
= stringappend(name_base
, short_base_length
, "_tab");
264 name_base
= stringappend(name_base
, short_base_length
, ".tab");
265 #endif /* not MSDOS */
267 base_length
= short_base_length
+ 4;
270 finput
= tryopen(infile
, "r");
274 filename
= getenv("BISON_SIMPLE");
276 /* File doesn't exist in current directory; try in INIT directory. */
278 if (filename
== 0 && cp
!= NULL
)
280 filename
= xmalloc(strlen(cp
) + strlen(PFILE
) + 2);
281 strcpy(filename
, cp
);
282 cp
= filename
+ strlen(filename
);
287 fparser
= tryopen(filename
? filename
: PFILE
, "r");
293 outfile
= stringappend(name_base
, short_base_length
, ".out");
295 /* We used to use just .out if spec_name_prefix (-p) was used,
296 but that conflicts with Posix. */
297 outfile
= stringappend(name_base
, short_base_length
, ".output");
299 foutput
= tryopen(outfile
, "w");
304 /* use permanent name for actions file */
305 actfile
= stringappend(name_base
, short_base_length
, ".act");
306 faction
= tryopen(actfile
, "w");
311 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "acXXXXXX"));
312 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "atXXXXXX"));
313 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "taXXXXXX"));
314 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "deXXXXXX"));
317 actfile
= mktemp(stringappend(tmp_base
, tmp_len
, "act.XXXXXX"));
318 tmpattrsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "attrs.XXXXXX"));
319 tmptabfile
= mktemp(stringappend(tmp_base
, tmp_len
, "tab.XXXXXX"));
320 tmpdefsfile
= mktemp(stringappend(tmp_base
, tmp_len
, "defs.XXXXXX"));
321 #endif /* not MSDOS */
324 faction
= tryopen(actfile
, "w+");
325 fattrs
= tryopen(tmpattrsfile
,"w+");
326 ftable
= tryopen(tmptabfile
, "w+");
330 defsfile
= stringappend(name_base
, base_length
, ".h");
331 fdefines
= tryopen(tmpdefsfile
, "w+");
334 #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
337 unlink(tmpattrsfile
);
340 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
342 /* These are opened by `done' or `open_extra_files', if at all */
344 tabfile
= spec_outfile
;
346 tabfile
= stringappend(name_base
, base_length
, ".c");
349 attrsfile
= stringappend(name_base
, short_base_length
, "_stype.h");
350 guardfile
= stringappend(name_base
, short_base_length
, "_guard.c");
353 attrsfile
= stringappend(name_base
, short_base_length
, ".sth");
354 guardfile
= stringappend(name_base
, short_base_length
, ".guc");
356 attrsfile
= stringappend(name_base
, short_base_length
, ".stype.h");
357 guardfile
= stringappend(name_base
, short_base_length
, ".guard.c");
358 #endif /* not MSDOS */
364 /*--------------------------------------------------------------------.
365 | Open the output files needed only for the semantic parser. This |
366 | is done when %semantic_parser is seen in the declarations section. |
367 `--------------------------------------------------------------------*/
370 open_extra_files (void)
383 filename
= (char *) getenv ("BISON_HAIRY");
385 /* File doesn't exist in current directory; try in INIT directory. */
387 if (filename
== 0 && cp
!= NULL
)
389 filename
= xmalloc(strlen(cp
) + strlen(PFILE1
) + 2);
390 strcpy(filename
, cp
);
391 cp
= filename
+ strlen(filename
);
396 fparser
= tryopen(filename
? filename
: PFILE1
, "r");
399 /* JF change from inline attrs file to separate one */
400 ftmp
= tryopen(attrsfile
, "w");
402 while((c
=getc(fattrs
))!=EOF
) /* Thank god for buffering */
407 fguard
= tryopen(guardfile
, "w");
421 /* JF write out the output file */
422 if (!complain_message_count
&& ftable
)
427 ftmp
=tryopen(tabfile
, "w");
429 while((c
=getc(ftable
)) != EOF
)
436 ftmp
= tryopen(defsfile
, "w");
439 while((c
=getc(fdefines
)) != EOF
)
446 #if defined (VMS) & !defined (__VMS_POSIX)
447 if (faction
&& ! noparserflag
)
450 delete(tmpattrsfile
);
453 /* Don't call exit again, we're in atexit ().
454 if (!complain_message_count)
455 sys$exit(SS$_NORMAL);
456 sys$exit(SS$_ABORT); */
458 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
459 if (actfile
&& ! noparserflag
) unlink(actfile
);
460 if (tmpattrsfile
) unlink(tmpattrsfile
);
461 if (tmptabfile
) unlink(tmptabfile
);
462 if (tmpdefsfile
) unlink(tmpdefsfile
);
463 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
464 /* Don't call exit again, we're in atexit ().
465 exit (complain_message_count ? 1 : 0); */
466 #endif /* not VMS, or __VMS_POSIX */