]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for bison,
2 Copyright 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
;
58 struct obstack action_obstack
;
60 /* File name specified with -o for the output file, or 0 if no -o. */
67 static char *defsfile
;
69 static char *guardfile
;
71 static char *tmpattrsfile
;
72 static char *tmptabfile
;
73 static char *tmpdefsfile
;
75 extern char *mktemp (); /* So the compiler won't complain */
76 extern char *getenv ();
78 extern char *program_name
;
82 stringappend (const char *string1
, int end1
, const char *string2
)
84 register char *ostring
;
86 register const char *cp1
;
94 ostring
= XCALLOC (char, i
+ end1
+ 1);
98 for (i
= 0; i
< end1
; i
++)
102 while ((*cp
++ = *cp1
++))
108 /*-----------------------------------------------------------------.
109 | Try to open file NAME with mode MODE, and print an error message |
111 `-----------------------------------------------------------------*/
114 xfopen (const char *name
, const char *mode
)
118 ptr
= fopen (name
, mode
);
120 error (2, errno
, _("cannot open file `%s'"), name
);
125 /*-------------------------------------------------------------.
126 | Try to close file PTR, and print an error message if fails. |
127 `-------------------------------------------------------------*/
137 result
= fclose (ptr
);
139 error (2, errno
, _("cannot close file"));
144 /* JF this has been hacked to death. Nowaday it sets up the file names for
145 the output files, and opens the tmp files and the parser */
155 int short_base_length
;
157 #if defined (VMS) & !defined (__VMS_POSIX)
158 const char *tmp_base
= "sys$scratch:b_";
160 const char *tmp_base
= "/tmp/b.";
165 tmp_base
= getenv ("TMP");
171 #if (defined(_WIN32) && !defined(__CYGWIN32__))
172 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
174 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
178 #endif /* _WIN32 && !__CYGWIN32__ */
180 #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
182 char *tmp_ptr
= getenv ("TMPDIR");
185 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
187 #endif /* unix || __unix || __unix__ */
189 tmp_len
= strlen (tmp_base
);
193 /* -o was specified. The precise -o name will be used for ftable.
194 For other output files, remove the ".c" or ".tab.c" suffix. */
195 name_base
= spec_outfile
;
199 /* BASE_LENGTH includes ".tab" but not ".c". */
200 base_length
= strlen (name_base
);
201 if (!strcmp (name_base
+ base_length
- 2, ".c"))
203 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
204 short_base_length
= base_length
;
205 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
206 short_base_length
-= 4;
207 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
208 short_base_length
-= 4;
210 else if (spec_file_prefix
)
212 /* -b was specified. Construct names from it. */
213 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
214 short_base_length
= strlen (spec_file_prefix
);
215 /* Count room for `.tab'. */
216 base_length
= short_base_length
+ 4;
217 name_base
= XMALLOC (char, base_length
+ 1);
219 strcpy (name_base
, spec_file_prefix
);
221 strcat (name_base
, "_tab");
223 strcat (name_base
, ".tab");
231 /* -o was not specified; compute output file name from input
232 or use y.tab.c, etc., if -y was specified. */
234 static char FIXED_NAME_BASE
[] = "y.y";
236 name_base
= yacc_flag
? FIXED_NAME_BASE
: infile
;
238 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
240 base_length
= strlen (name_base
);
241 if (!strcmp (name_base
+ base_length
- 2, ".y"))
243 short_base_length
= base_length
;
246 name_base
= stringappend (name_base
, short_base_length
, "_tab");
249 name_base
= stringappend (name_base
, short_base_length
, "_tab");
251 name_base
= stringappend (name_base
, short_base_length
, ".tab");
252 #endif /* not MSDOS */
254 base_length
= short_base_length
+ 4;
257 finput
= xfopen (infile
, "r");
261 filename
= getenv ("BISON_SIMPLE");
263 /* File doesn't exist in current directory; try in INIT directory. */
264 cp
= getenv ("INIT");
265 if (filename
== 0 && cp
!= NULL
)
267 filename
= XMALLOC (char, strlen (cp
) + strlen (PFILE
) + 2);
268 strcpy (filename
, cp
);
269 cp
= filename
+ strlen (filename
);
274 fparser
= xfopen (filename
? filename
: PFILE
, "r");
280 outfile
= stringappend (name_base
, short_base_length
, ".out");
282 /* We used to use just .out if spec_name_prefix (-p) was used,
283 but that conflicts with Posix. */
284 outfile
= stringappend (name_base
, short_base_length
, ".output");
286 foutput
= xfopen (outfile
, "w");
291 /* use permanent name for actions file */
292 actfile
= stringappend (name_base
, short_base_length
, ".act");
296 tmpattrsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "atXXXXXX"));
297 tmptabfile
= mktemp (stringappend (tmp_base
, tmp_len
, "taXXXXXX"));
298 tmpdefsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "deXXXXXX"));
300 tmpattrsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "attrs.XXXXXX"));
301 tmptabfile
= mktemp (stringappend (tmp_base
, tmp_len
, "tab.XXXXXX"));
302 tmpdefsfile
= mktemp (stringappend (tmp_base
, tmp_len
, "defs.XXXXXX"));
303 #endif /* not MSDOS */
305 fattrs
= xfopen (tmpattrsfile
, "w+");
306 ftable
= xfopen (tmptabfile
, "w+");
310 defsfile
= stringappend (name_base
, base_length
, ".h");
311 fdefines
= xfopen (tmpdefsfile
, "w+");
314 #if !(defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
315 unlink (tmpattrsfile
);
317 unlink (tmpdefsfile
);
318 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
320 /* These are opened by `done' or `open_extra_files', if at all */
322 tabfile
= spec_outfile
;
324 tabfile
= stringappend (name_base
, base_length
, ".c");
327 attrsfile
= stringappend (name_base
, short_base_length
, "_stype.h");
328 guardfile
= stringappend (name_base
, short_base_length
, "_guard.c");
331 attrsfile
= stringappend (name_base
, short_base_length
, ".sth");
332 guardfile
= stringappend (name_base
, short_base_length
, ".guc");
334 attrsfile
= stringappend (name_base
, short_base_length
, ".stype.h");
335 guardfile
= stringappend (name_base
, short_base_length
, ".guard.c");
336 #endif /* not MSDOS */
339 /* Setup the action obstack. */
340 obstack_init (&action_obstack
);
345 /*--------------------------------------------------------------------.
346 | Open the output files needed only for the semantic parser. This |
347 | is done when %semantic_parser is seen in the declarations section. |
348 `--------------------------------------------------------------------*/
351 open_extra_files (void)
364 filename
= (char *) getenv ("BISON_HAIRY");
366 /* File doesn't exist in current directory; try in INIT directory. */
367 cp
= getenv ("INIT");
368 if (filename
== 0 && cp
!= NULL
)
370 filename
= XMALLOC (char, strlen (cp
) + strlen (PFILE1
) + 2);
371 strcpy (filename
, cp
);
372 cp
= filename
+ strlen (filename
);
377 fparser
= xfopen (filename
? filename
: PFILE1
, "r");
380 /* JF change from inline attrs file to separate one */
381 ftmp
= xfopen (attrsfile
, "w");
383 while ((c
= getc (fattrs
)) != EOF
) /* Thank god for buffering */
388 fguard
= xfopen (guardfile
, "w");
401 /* JF write out the output file */
402 if (!complain_message_count
&& ftable
)
407 ftmp
= xfopen (tabfile
, "w");
409 while ((c
= getc (ftable
)) != EOF
)
416 ftmp
= xfopen (defsfile
, "w");
419 while ((c
= getc (fdefines
)) != EOF
)
428 FILE *faction
= xfopen (actfile
, "w");
429 size_t size
= obstack_object_size (&action_obstack
);
430 fwrite (obstack_finish (&action_obstack
), 1, size
, faction
);
433 #if defined (VMS) & !defined (__VMS_POSIX)
435 delete (tmpattrsfile
);
438 /* Don't call exit again, we're in atexit ().
439 if (!complain_message_count)
440 sys$exit(SS$_NORMAL);
441 sys$exit(SS$_ABORT); */
443 #if (defined (MSDOS) || (defined(_WIN32) && !defined(__CYGWIN32__)))
445 unlink (tmpattrsfile
);
449 unlink (tmpdefsfile
);
450 #endif /* MSDOS || (_WIN32 && !__CYGWIN32__) */
451 /* Don't call exit again, we're in atexit ().
452 exit (complain_message_count ? 1 : 0); */
453 #endif /* not VMS, or __VMS_POSIX */