]>
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 BISON_SIMPLE "GNU_BISON:[000000]BISON.SIMPLE"
29 # define BISON_HARIRY "GNU_BISON:[000000]BISON.HAIRY"
33 #if defined (_MSC_VER)
35 # define BISON_SIMPLE "c:/usr/local/lib/bison.simple"
38 # define BISON_HAIRY "c:/usr/local/lib/bison.hairy"
53 struct obstack action_obstack
;
54 struct obstack attrs_obstack
;
55 struct obstack table_obstack
;
56 struct obstack defines_obstack
;
58 /* File name specified with -o for the output file, or 0 if no -o. */
65 static char *defsfile
;
67 static char *guardfile
;
70 extern char *getenv ();
72 extern char *program_name
;
76 stringappend (const char *string1
, int end1
, const char *string2
)
78 register char *ostring
;
80 register const char *cp1
;
88 ostring
= XCALLOC (char, i
+ end1
+ 1);
92 for (i
= 0; i
< end1
; i
++)
96 while ((*cp
++ = *cp1
++))
102 /*-----------------------------------------------------------------.
103 | Try to open file NAME with mode MODE, and print an error message |
105 `-----------------------------------------------------------------*/
108 xfopen (const char *name
, const char *mode
)
112 ptr
= fopen (name
, mode
);
114 error (2, errno
, _("cannot open file `%s'"), name
);
119 /*-------------------------------------------------------------.
120 | Try to close file PTR, and print an error message if fails. |
121 `-------------------------------------------------------------*/
131 result
= fclose (ptr
);
133 error (2, errno
, _("cannot close file"));
138 /*--------------------------------------------------.
139 | Save the content of the obstack OBS in FILENAME. |
140 `--------------------------------------------------*/
143 obstack_save (struct obstack
*obs
, const char *filename
)
145 FILE *out
= xfopen (filename
, "w");
146 size_t size
= obstack_object_size (obs
);
147 fwrite (obstack_finish (obs
), 1, size
, out
);
153 skeleton_find (const char *envvar
, const char *skeleton
)
155 const char *res
= getenv (envvar
);
160 /* File doesn't exist in current directory; try in INIT directory. */
161 if (!res
&& (cp
= getenv ("INIT")))
163 res
= XMALLOC (char, strlen (cp
) + strlen (skeleton
) + 2);
164 sprintf (res
, "%s%c%s", cp
, '/', skeleton
);
175 /*-----------------------------------------------------------------.
176 | Open the input file. Look for the skeletons. Find the names of |
177 | the output files. Prepare the obstacks. |
178 `-----------------------------------------------------------------*/
188 int short_base_length
;
190 #if defined (VMS) & !defined (__VMS_POSIX)
191 const char *tmp_base
= "sys$scratch:b_";
193 const char *tmp_base
= "/tmp/b.";
198 tmp_base
= getenv ("TMP");
204 #if (defined(_WIN32) && !defined(__CYGWIN32__))
205 tmp_base
= getenv ("TEMP"); /* Windows95 defines this ... */
207 tmp_base
= getenv ("Temp"); /* ... while NT prefers this */
211 #endif /* _WIN32 && !__CYGWIN32__ */
213 #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
215 char *tmp_ptr
= getenv ("TMPDIR");
218 tmp_base
= stringappend (tmp_ptr
, strlen (tmp_ptr
), "/b.");
220 #endif /* unix || __unix || __unix__ */
222 tmp_len
= strlen (tmp_base
);
226 /* -o was specified. The precise -o name will be used for FTABLE.
227 For other output files, remove the ".c" or ".tab.c" suffix. */
228 name_base
= spec_outfile
;
232 /* BASE_LENGTH includes ".tab" but not ".c". */
233 base_length
= strlen (name_base
);
234 if (!strcmp (name_base
+ base_length
- 2, ".c"))
236 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
237 short_base_length
= base_length
;
238 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
239 short_base_length
-= 4;
240 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
241 short_base_length
-= 4;
243 else if (spec_file_prefix
)
245 /* -b was specified. Construct names from it. */
246 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
247 short_base_length
= strlen (spec_file_prefix
);
248 /* Count room for `.tab'. */
249 base_length
= short_base_length
+ 4;
250 name_base
= XMALLOC (char, base_length
+ 1);
252 strcpy (name_base
, spec_file_prefix
);
254 strcat (name_base
, "_tab");
256 strcat (name_base
, ".tab");
264 /* -o was not specified; compute output file name from input
265 or use y.tab.c, etc., if -y was specified. */
267 static char FIXED_NAME_BASE
[] = "y.y";
269 name_base
= yacc_flag
? FIXED_NAME_BASE
: infile
;
271 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
273 base_length
= strlen (name_base
);
274 if (!strcmp (name_base
+ base_length
- 2, ".y"))
276 short_base_length
= base_length
;
279 name_base
= stringappend (name_base
, short_base_length
, "_tab");
282 name_base
= stringappend (name_base
, short_base_length
, "_tab");
284 name_base
= stringappend (name_base
, short_base_length
, ".tab");
285 #endif /* not MSDOS */
287 base_length
= short_base_length
+ 4;
290 finput
= xfopen (infile
, "r");
293 fparser
= xfopen (skeleton_find ("BISON_SIMPLE", BISON_SIMPLE
), "r");
298 outfile
= stringappend (name_base
, short_base_length
, ".out");
300 /* We used to use just .out if spec_name_prefix (-p) was used,
301 but that conflicts with Posix. */
302 outfile
= stringappend (name_base
, short_base_length
, ".output");
304 foutput
= xfopen (outfile
, "w");
309 /* use permanent name for actions file */
310 actfile
= stringappend (name_base
, short_base_length
, ".act");
315 defsfile
= stringappend (name_base
, base_length
, ".h");
318 /* These are opened by `done' or `open_extra_files', if at all */
320 tabfile
= spec_outfile
;
322 tabfile
= stringappend (name_base
, base_length
, ".c");
325 attrsfile
= stringappend (name_base
, short_base_length
, "_stype.h");
326 guardfile
= stringappend (name_base
, short_base_length
, "_guard.c");
329 attrsfile
= stringappend (name_base
, short_base_length
, ".sth");
330 guardfile
= stringappend (name_base
, short_base_length
, ".guc");
332 attrsfile
= stringappend (name_base
, short_base_length
, ".stype.h");
333 guardfile
= stringappend (name_base
, short_base_length
, ".guard.c");
334 #endif /* not MSDOS */
337 /* Initialize the obstacks. */
338 obstack_init (&action_obstack
);
339 obstack_init (&attrs_obstack
);
340 obstack_init (&table_obstack
);
341 obstack_init (&defines_obstack
);
346 /*--------------------------------------------------------------------.
347 | Open the output files needed only for the semantic parser. This |
348 | is done when %semantic_parser is seen in the declarations section. |
349 `--------------------------------------------------------------------*/
352 open_extra_files (void)
357 fparser
= xfopen (skeleton_find ("BISON_HAIRY", BISON_HAIRY
), "r");
358 fguard
= xfopen (guardfile
, "w");
362 /*-----------------------------------------------------.
363 | Close the open files, produce all the output files. |
364 `-----------------------------------------------------*/
374 /* Output the main file. */
375 obstack_save (&table_obstack
, tabfile
);
377 /* Output the header file if wanted. */
379 obstack_save (&defines_obstack
, defsfile
);
381 /* If we output only the table, dump the actions in ACTFILE.
384 obstack_save (&action_obstack
, actfile
);
386 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
387 into its own file, ATTTRSFILE. */
389 obstack_save (&attrs_obstack
, attrsfile
);