]>
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"
55 struct obstack action_obstack
;
56 struct obstack attrs_obstack
;
57 struct obstack table_obstack
;
58 struct obstack defines_obstack
;
60 /* File name specified with -o for the output file, or 0 if no -o. */
67 static char *defsfile
;
69 static char *guardfile
;
72 extern char *getenv ();
74 extern char *program_name
;
78 stringappend (const char *string1
, int end1
, const char *string2
)
80 register char *ostring
;
82 register const char *cp1
;
90 ostring
= XCALLOC (char, i
+ end1
+ 1);
94 for (i
= 0; i
< end1
; i
++)
98 while ((*cp
++ = *cp1
++))
104 /*-----------------------------------------------------------------.
105 | Try to open file NAME with mode MODE, and print an error message |
107 `-----------------------------------------------------------------*/
110 xfopen (const char *name
, const char *mode
)
114 ptr
= fopen (name
, mode
);
116 error (2, errno
, _("cannot open file `%s'"), name
);
121 /*-------------------------------------------------------------.
122 | Try to close file PTR, and print an error message if fails. |
123 `-------------------------------------------------------------*/
133 result
= fclose (ptr
);
135 error (2, errno
, _("cannot close file"));
140 /* JF this has been hacked to death. Nowaday it sets up the file names for
141 the output files, and opens the tmp files and the parser */
151 int short_base_length
;
153 #if defined (VMS) & !defined (__VMS_POSIX)
154 const char *tmp_base
= "sys$scratch:b_";
156 const char *tmp_base
= "/tmp/b.";
161 tmp_base
= getenv ("TMP");
167 #if (defined(_WIN32) && !defined(__CYGWIN32__))
168 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
170 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
174 #endif /* _WIN32 && !__CYGWIN32__ */
176 #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
178 char *tmp_ptr
= getenv ("TMPDIR");
181 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
183 #endif /* unix || __unix || __unix__ */
185 tmp_len
= strlen (tmp_base
);
189 /* -o was specified. The precise -o name will be used for FTABLE.
190 For other output files, remove the ".c" or ".tab.c" suffix. */
191 name_base
= spec_outfile
;
195 /* BASE_LENGTH includes ".tab" but not ".c". */
196 base_length
= strlen (name_base
);
197 if (!strcmp (name_base
+ base_length
- 2, ".c"))
199 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
200 short_base_length
= base_length
;
201 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
202 short_base_length
-= 4;
203 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
204 short_base_length
-= 4;
206 else if (spec_file_prefix
)
208 /* -b was specified. Construct names from it. */
209 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
210 short_base_length
= strlen (spec_file_prefix
);
211 /* Count room for `.tab'. */
212 base_length
= short_base_length
+ 4;
213 name_base
= XMALLOC (char, base_length
+ 1);
215 strcpy (name_base
, spec_file_prefix
);
217 strcat (name_base
, "_tab");
219 strcat (name_base
, ".tab");
227 /* -o was not specified; compute output file name from input
228 or use y.tab.c, etc., if -y was specified. */
230 static char FIXED_NAME_BASE
[] = "y.y";
232 name_base
= yacc_flag
? FIXED_NAME_BASE
: infile
;
234 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
236 base_length
= strlen (name_base
);
237 if (!strcmp (name_base
+ base_length
- 2, ".y"))
239 short_base_length
= base_length
;
242 name_base
= stringappend (name_base
, short_base_length
, "_tab");
245 name_base
= stringappend (name_base
, short_base_length
, "_tab");
247 name_base
= stringappend (name_base
, short_base_length
, ".tab");
248 #endif /* not MSDOS */
250 base_length
= short_base_length
+ 4;
253 finput
= xfopen (infile
, "r");
257 filename
= getenv ("BISON_SIMPLE");
259 /* File doesn't exist in current directory; try in INIT directory. */
260 cp
= getenv ("INIT");
261 if (filename
== 0 && cp
!= NULL
)
263 filename
= XMALLOC (char, strlen (cp
) + strlen (PFILE
) + 2);
264 strcpy (filename
, cp
);
265 cp
= filename
+ strlen (filename
);
270 fparser
= xfopen (filename
? filename
: PFILE
, "r");
276 outfile
= stringappend (name_base
, short_base_length
, ".out");
278 /* We used to use just .out if spec_name_prefix (-p) was used,
279 but that conflicts with Posix. */
280 outfile
= stringappend (name_base
, short_base_length
, ".output");
282 foutput
= xfopen (outfile
, "w");
287 /* use permanent name for actions file */
288 actfile
= stringappend (name_base
, short_base_length
, ".act");
293 defsfile
= stringappend (name_base
, base_length
, ".h");
296 /* These are opened by `done' or `open_extra_files', if at all */
298 tabfile
= spec_outfile
;
300 tabfile
= stringappend (name_base
, base_length
, ".c");
303 attrsfile
= stringappend (name_base
, short_base_length
, "_stype.h");
304 guardfile
= stringappend (name_base
, short_base_length
, "_guard.c");
307 attrsfile
= stringappend (name_base
, short_base_length
, ".sth");
308 guardfile
= stringappend (name_base
, short_base_length
, ".guc");
310 attrsfile
= stringappend (name_base
, short_base_length
, ".stype.h");
311 guardfile
= stringappend (name_base
, short_base_length
, ".guard.c");
312 #endif /* not MSDOS */
315 /* Initialize the obstacks. */
316 obstack_init (&action_obstack
);
317 obstack_init (&attrs_obstack
);
318 obstack_init (&table_obstack
);
319 obstack_init (&defines_obstack
);
324 /*--------------------------------------------------------------------.
325 | Open the output files needed only for the semantic parser. This |
326 | is done when %semantic_parser is seen in the declarations section. |
327 `--------------------------------------------------------------------*/
330 open_extra_files (void)
339 filename
= (char *) getenv ("BISON_HAIRY");
342 /* File doesn't exist in current directory; try in INIT
344 char *cp
= getenv ("INIT");
345 if (filename
== 0 && cp
!= NULL
)
347 filename
= XMALLOC (char, strlen (cp
) + strlen (PFILE1
) + 2);
348 strcpy (filename
, cp
);
349 cp
= filename
+ strlen (filename
);
355 fparser
= xfopen (filename
? filename
: PFILE1
, "r");
358 fguard
= xfopen (guardfile
, "w");
369 /* JF write out the output file */
370 if (!complain_message_count
)
372 /* Output the main file. */
373 FILE *ftab
= xfopen (tabfile
, "w");
374 size_t size
= obstack_object_size (&table_obstack
);
375 fwrite (obstack_finish (&table_obstack
), 1, size
, ftab
);
378 /* Output the header file is wanted. */
381 FILE *fdefs
= xfopen (defsfile
, "w");
382 size_t size
= obstack_object_size (&defines_obstack
);
383 fwrite (obstack_finish (&defines_obstack
), 1, size
, fdefs
);
388 /* If we output only the table, dump the actions in ACTION_OBSTACK.
392 FILE *faction
= xfopen (actfile
, "w");
393 size_t size
= obstack_object_size (&action_obstack
);
394 fwrite (obstack_finish (&action_obstack
), 1, size
, faction
);
398 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
399 into its own file, ATTTRSFILE. */
402 FILE *fattrs
= xfopen (attrsfile
, "w");
403 size_t size
= obstack_object_size (&attrs_obstack
);
404 fwrite (obstack_finish (&attrs_obstack
), 1, size
, fattrs
);
408 /* Don't call exit again, we're in atexit ().
409 exit (complain_message_count ? 1 : 0); */