]>
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
34 struct obstack action_obstack
;
35 struct obstack attrs_obstack
;
36 struct obstack table_obstack
;
37 struct obstack defines_obstack
;
39 /* File name specified with -o for the output file, or 0 if no -o. */
46 static char *defsfile
;
48 static char *guardfile
;
51 /*-----------------------------------------------------------------.
52 | Return a newly allocated string composed of the concatenation of |
53 | the END1 first chars of STRING1, and STRING2. |
54 `-----------------------------------------------------------------*/
57 stringappend (const char *string1
, int end1
, const char *string2
)
69 res
= XCALLOC (char, i
+ end1
+ 1);
73 for (i
= 0; i
< end1
; i
++)
77 while ((*cp
++ = *cp1
++))
83 /*-----------------------------------------------------------------.
84 | Try to open file NAME with mode MODE, and print an error message |
86 `-----------------------------------------------------------------*/
89 xfopen (const char *name
, const char *mode
)
93 ptr
= fopen (name
, mode
);
95 error (2, errno
, _("cannot open file `%s'"), name
);
100 /*-------------------------------------------------------------.
101 | Try to close file PTR, and print an error message if fails. |
102 `-------------------------------------------------------------*/
112 result
= fclose (ptr
);
114 error (2, errno
, _("cannot close file"));
119 /*--------------------------------------------------.
120 | Save the content of the obstack OBS in FILENAME. |
121 `--------------------------------------------------*/
124 obstack_save (struct obstack
*obs
, const char *filename
)
126 FILE *out
= xfopen (filename
, "w");
127 size_t size
= obstack_object_size (obs
);
128 fwrite (obstack_finish (obs
), 1, size
, out
);
134 skeleton_find (const char *envvar
, const char *skeleton
)
136 const char *res
= getenv (envvar
);
141 /* File doesn't exist in current directory; try in INIT directory. */
142 if (!res
&& (cp
= getenv ("INIT")))
144 res
= XMALLOC (char, strlen (cp
) + strlen (skeleton
) + 2);
145 sprintf (res
, "%s%c%s", cp
, '/', skeleton
);
156 /*-----------------------------------------------------------------.
157 | Open the input file. Look for the skeletons. Find the names of |
158 | the output files. Prepare the obstacks. |
159 `-----------------------------------------------------------------*/
166 int short_base_length
;
170 /* -o was specified. The precise -o name will be used for FTABLE.
171 For other output files, remove the ".c" or ".tab.c" suffix. */
172 name_base
= spec_outfile
;
176 /* BASE_LENGTH includes ".tab" but not ".c". */
177 base_length
= strlen (name_base
);
178 if (!strcmp (name_base
+ base_length
- 2, ".c"))
180 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
181 short_base_length
= base_length
;
182 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
183 short_base_length
-= 4;
184 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
185 short_base_length
-= 4;
187 else if (spec_file_prefix
)
189 /* -b was specified. Construct names from it. */
190 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
191 short_base_length
= strlen (spec_file_prefix
);
192 /* Count room for `.tab'. */
193 base_length
= short_base_length
+ 4;
194 name_base
= XMALLOC (char, base_length
+ 1);
196 strcpy (name_base
, spec_file_prefix
);
197 strcat (name_base
, EXT_TAB
);
204 /* -o was not specified; compute output file name from input
205 or use y.tab.c, etc., if -y was specified. */
207 static char FIXED_NAME_BASE
[] = "y.y";
209 name_base
= yacc_flag
? FIXED_NAME_BASE
: infile
;
211 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
213 base_length
= strlen (name_base
);
214 if (!strcmp (name_base
+ base_length
- 2, ".y"))
216 short_base_length
= base_length
;
218 name_base
= stringappend (name_base
, short_base_length
, EXT_TAB
);
219 base_length
= short_base_length
+ 4;
222 finput
= xfopen (infile
, "r");
225 fparser
= xfopen (skeleton_find ("BISON_SIMPLE", BISON_SIMPLE
), "r");
229 /* We used to use just .out if spec_name_prefix (-p) was used,
230 but that conflicts with Posix. */
231 outfile
= stringappend (name_base
, short_base_length
, EXT_OUTPUT
);
232 foutput
= xfopen (outfile
, "w");
237 /* use permanent name for actions file */
238 actfile
= stringappend (name_base
, short_base_length
, ".act");
243 defsfile
= stringappend (name_base
, base_length
, ".h");
246 /* These are opened by `done' or `open_extra_files', if at all */
248 tabfile
= spec_outfile
;
250 tabfile
= stringappend (name_base
, base_length
, ".c");
252 attrsfile
= stringappend (name_base
, short_base_length
, EXT_STYPE_H
);
253 guardfile
= stringappend (name_base
, short_base_length
, EXT_GUARD_C
);
255 /* Initialize the obstacks. */
256 obstack_init (&action_obstack
);
257 obstack_init (&attrs_obstack
);
258 obstack_init (&table_obstack
);
259 obstack_init (&defines_obstack
);
264 /*--------------------------------------------------------------------.
265 | Open the output files needed only for the semantic parser. This |
266 | is done when %semantic_parser is seen in the declarations section. |
267 `--------------------------------------------------------------------*/
270 open_extra_files (void)
275 fparser
= xfopen (skeleton_find ("BISON_HAIRY", BISON_HAIRY
), "r");
276 fguard
= xfopen (guardfile
, "w");
280 /*-----------------------------------------------------.
281 | Close the open files, produce all the output files. |
282 `-----------------------------------------------------*/
292 /* Output the main file. */
293 obstack_save (&table_obstack
, tabfile
);
295 /* Output the header file if wanted. */
297 obstack_save (&defines_obstack
, defsfile
);
299 /* If we output only the table, dump the actions in ACTFILE.
302 obstack_save (&action_obstack
, actfile
);
304 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
305 into its own file, ATTTRSFILE. */
307 obstack_save (&attrs_obstack
, attrsfile
);