]>
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 /*-----------------------------------------------------------------.
71 | Return a newly allocated string composed of the concatenation of |
72 | the END1 first chars of STRING1, and STRING2. |
73 `-----------------------------------------------------------------*/
76 stringappend (const char *string1
, int end1
, const char *string2
)
88 res
= 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
;
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
= XMALLOC (char, base_length
+ 1);
218 strcpy (name_base
, spec_file_prefix
);
219 strcat (name_base
, EXT_TAB
);
226 /* -o was not specified; compute output file name from input
227 or use y.tab.c, etc., if -y was specified. */
229 static char FIXED_NAME_BASE
[] = "y.y";
231 name_base
= yacc_flag
? FIXED_NAME_BASE
: infile
;
233 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
235 base_length
= strlen (name_base
);
236 if (!strcmp (name_base
+ base_length
- 2, ".y"))
238 short_base_length
= base_length
;
240 name_base
= stringappend (name_base
, short_base_length
, EXT_TAB
);
241 base_length
= short_base_length
+ 4;
244 finput
= xfopen (infile
, "r");
247 fparser
= xfopen (skeleton_find ("BISON_SIMPLE", BISON_SIMPLE
), "r");
251 /* We used to use just .out if spec_name_prefix (-p) was used,
252 but that conflicts with Posix. */
253 outfile
= stringappend (name_base
, short_base_length
, EXT_OUTPUT
);
254 foutput
= xfopen (outfile
, "w");
259 /* use permanent name for actions file */
260 actfile
= stringappend (name_base
, short_base_length
, ".act");
265 defsfile
= stringappend (name_base
, base_length
, ".h");
268 /* These are opened by `done' or `open_extra_files', if at all */
270 tabfile
= spec_outfile
;
272 tabfile
= stringappend (name_base
, base_length
, ".c");
274 attrsfile
= stringappend (name_base
, short_base_length
, EXT_STYPE_H
);
275 guardfile
= stringappend (name_base
, short_base_length
, EXT_GUARD_C
);
277 /* Initialize the obstacks. */
278 obstack_init (&action_obstack
);
279 obstack_init (&attrs_obstack
);
280 obstack_init (&table_obstack
);
281 obstack_init (&defines_obstack
);
286 /*--------------------------------------------------------------------.
287 | Open the output files needed only for the semantic parser. This |
288 | is done when %semantic_parser is seen in the declarations section. |
289 `--------------------------------------------------------------------*/
292 open_extra_files (void)
297 fparser
= xfopen (skeleton_find ("BISON_HAIRY", BISON_HAIRY
), "r");
298 fguard
= xfopen (guardfile
, "w");
302 /*-----------------------------------------------------.
303 | Close the open files, produce all the output files. |
304 `-----------------------------------------------------*/
314 /* Output the main file. */
315 obstack_save (&table_obstack
, tabfile
);
317 /* Output the header file if wanted. */
319 obstack_save (&defines_obstack
, defsfile
);
321 /* If we output only the table, dump the actions in ACTFILE.
324 obstack_save (&action_obstack
, actfile
);
326 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
327 into its own file, ATTTRSFILE. */
329 obstack_save (&attrs_obstack
, attrsfile
);