]>
git.saurik.com Git - bison.git/blob - src/files.c
3dc9ca5b2a90cc7b202a34a5d20a9b551aa83219
1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000 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 it
7 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, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 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 the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #if defined (VMS) & !defined (__VMS_POSIX)
26 # define unlink delete
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"
52 FILE *fdefines
= NULL
;
59 /* File name specified with -o for the output file, or 0 if no -o. */
66 static char *defsfile
;
68 static char *guardfile
;
70 static char *tmpattrsfile
;
71 static char *tmptabfile
;
72 static char *tmpdefsfile
;
74 extern char *mktemp (); /* So the compiler won't complain */
75 extern char *getenv ();
77 extern char *program_name
;
81 stringappend (const char *string1
, int end1
, const char *string2
)
83 register char *ostring
;
85 register const char *cp1
;
93 ostring
= NEW2 (i
+ end1
+ 1, char);
97 for (i
= 0; i
< end1
; i
++)
101 while ((*cp
++ = *cp1
++))
107 /*-----------------------------------------------------------------.
108 | Try to open file NAME with mode MODE, and print an error message |
110 `-----------------------------------------------------------------*/
113 xfopen (const char *name
, const char *mode
)
117 ptr
= fopen (name
, mode
);
119 error (2, errno
, _("cannot open file `%s'"), name
);
124 /*-------------------------------------------------------------.
125 | Try to close file PTR, and print an error message if fails. |
126 `-------------------------------------------------------------*/
136 result
= fclose (ptr
);
138 error (2, errno
, _("cannot close file"));
143 /* JF this has been hacked to death. Nowaday it sets up the file names for
144 the output files, and opens the tmp files and the parser */
154 int short_base_length
;
156 #if defined (VMS) & !defined (__VMS_POSIX)
157 const char *tmp_base
= "sys$scratch:b_";
159 const char *tmp_base
= "/tmp/b.";
164 tmp_base
= getenv ("TMP");
170 #if (defined(_WIN32) && !defined(__CYGWIN32__))
171 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
173 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
177 #endif /* _WIN32 && !__CYGWIN32__ */
179 #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
181 char *tmp_ptr
= getenv ("TMPDIR");
184 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
186 #endif /* unix || __unix || __unix__ */
188 tmp_len
= strlen (tmp_base
);
192 /* -o was specified. The precise -o name will be used for ftable.
193 For other output files, remove the ".c" or ".tab.c" suffix. */
194 name_base
= spec_outfile
;
198 /* BASE_LENGTH includes ".tab" but not ".c". */
199 base_length
= strlen (name_base
);
200 if (!strcmp (name_base
+ base_length
- 2, ".c"))
202 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
203 short_base_length
= base_length
;
204 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
205 short_base_length
-= 4;
206 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
207 short_base_length
-= 4;
209 else if (spec_file_prefix
)
211 /* -b was specified. Construct names from it. */
212 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
213 short_base_length
= strlen (spec_file_prefix
);
214 /* Count room for `.tab'. */
215 base_length
= short_base_length
+ 4;
216 name_base
= (char *) xmalloc (base_length
+ 1);
218 strcpy (name_base
, spec_file_prefix
);
220 strcat (name_base
, "_tab");
222 strcat (name_base
, ".tab");
230 /* -o was not specified; compute output file name from input
231 or use y.tab.c, etc., if -y was specified. */
233 static char FIXED_NAME_BASE
[] = "y.y";
235 name_base
= fixed_outfiles
? FIXED_NAME_BASE
: infile
;
237 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
239 base_length
= strlen (name_base
);
240 if (!strcmp (name_base
+ base_length
- 2, ".y"))
242 short_base_length
= base_length
;
245 name_base
= stringappend (name_base
, short_base_length
, "_tab");
248 name_base
= stringappend (name_base
, short_base_length
, "_tab");
250 name_base
= stringappend (name_base
, short_base_length
, ".tab");
251 #endif /* not MSDOS */
253 base_length
= short_base_length
+ 4;
256 finput
= xfopen (infile
, "r");
260 filename
= getenv ("BISON_SIMPLE");
262 /* File doesn't exist in current directory; try in INIT directory. */
263 cp
= getenv ("INIT");
264 if (filename
== 0 && cp
!= NULL
)
266 filename
= xmalloc (strlen (cp
) + strlen (PFILE
) + 2);
267 strcpy (filename
, cp
);
268 cp
= filename
+ strlen (filename
);
273 fparser
= xfopen (filename
? filename
: PFILE
, "r");
279 outfile
= stringappend (name_base
, short_base_length
, ".out");
281 /* We used to use just .out if spec_name_prefix (-p) was used,
282 but that conflicts with Posix. */
283 outfile
= stringappend (name_base
, short_base_length
, ".output");
285 foutput
= xfopen (outfile
, "w");
290 /* use permanent name for actions file */
291 actfile
= stringappend (name_base
, short_base_length
, ".act");
292 faction
= xfopen (actfile
, "w");
297 actfile
= mktemp (stringappend (tmp_base
, tmp_len
, "acXXXXXX"));
298 tmpattrsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "atXXXXXX"));
299 tmptabfile
= mktemp (stringappend (tmp_base
, tmp_len
, "taXXXXXX"));
300 tmpdefsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "deXXXXXX"));
303 actfile
= mktemp (stringappend (tmp_base
, tmp_len
, "act.XXXXXX"));
304 tmpattrsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "attrs.XXXXXX"));
305 tmptabfile
= mktemp (stringappend (tmp_base
, tmp_len
, "tab.XXXXXX"));
306 tmpdefsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "defs.XXXXXX"));
307 #endif /* not MSDOS */
310 faction
= xfopen (actfile
, "w+");
311 fattrs
= xfopen (tmpattrsfile
, "w+");
312 ftable
= xfopen (tmptabfile
, "w+");
316 defsfile
= stringappend (name_base
, base_length
, ".h");
317 fdefines
= xfopen (tmpdefsfile
, "w+");
320 #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
323 unlink (tmpattrsfile
);
325 unlink (tmpdefsfile
);
326 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
328 /* These are opened by `done' or `open_extra_files', if at all */
330 tabfile
= spec_outfile
;
332 tabfile
= stringappend (name_base
, base_length
, ".c");
335 attrsfile
= stringappend (name_base
, short_base_length
, "_stype.h");
336 guardfile
= stringappend (name_base
, short_base_length
, "_guard.c");
339 attrsfile
= stringappend (name_base
, short_base_length
, ".sth");
340 guardfile
= stringappend (name_base
, short_base_length
, ".guc");
342 attrsfile
= stringappend (name_base
, short_base_length
, ".stype.h");
343 guardfile
= stringappend (name_base
, short_base_length
, ".guard.c");
344 #endif /* not MSDOS */
350 /*--------------------------------------------------------------------.
351 | Open the output files needed only for the semantic parser. This |
352 | is done when %semantic_parser is seen in the declarations section. |
353 `--------------------------------------------------------------------*/
356 open_extra_files (void)
369 filename
= (char *) getenv ("BISON_HAIRY");
371 /* File doesn't exist in current directory; try in INIT directory. */
372 cp
= getenv ("INIT");
373 if (filename
== 0 && cp
!= NULL
)
375 filename
= xmalloc (strlen (cp
) + strlen (PFILE1
) + 2);
376 strcpy (filename
, cp
);
377 cp
= filename
+ strlen (filename
);
382 fparser
= xfopen (filename
? filename
: PFILE1
, "r");
385 /* JF change from inline attrs file to separate one */
386 ftmp
= xfopen (attrsfile
, "w");
388 while ((c
= getc (fattrs
)) != EOF
) /* Thank god for buffering */
393 fguard
= xfopen (guardfile
, "w");
407 /* JF write out the output file */
408 if (!complain_message_count
&& ftable
)
413 ftmp
= xfopen (tabfile
, "w");
415 while ((c
= getc (ftable
)) != EOF
)
422 ftmp
= xfopen (defsfile
, "w");
425 while ((c
= getc (fdefines
)) != EOF
)
432 #if defined (VMS) & !defined (__VMS_POSIX)
433 if (faction
&& !noparserflag
)
436 delete (tmpattrsfile
);
439 /* Don't call exit again, we're in atexit ().
440 if (!complain_message_count)
441 sys$exit(SS$_NORMAL);
442 sys$exit(SS$_ABORT); */
444 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
445 if (actfile
&& !noparserflag
)
448 unlink (tmpattrsfile
);
452 unlink (tmpdefsfile
);
453 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
454 /* Don't call exit again, we're in atexit ().
455 exit (complain_message_count ? 1 : 0); */
456 #endif /* not VMS, or __VMS_POSIX */