]>
git.saurik.com Git - bison.git/blob - src/files.c
f2ac8582d4c2f84ede0d965c49e8a5f91cc9c57f
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
33 struct obstack action_obstack
;
34 struct obstack attrs_obstack
;
35 struct obstack table_obstack
;
36 struct obstack defines_obstack
;
38 /* File name specified with -o for the output file, or 0 if no -o. */
46 static char *defsfile
;
50 /*-----------------------------------------------------------------.
51 | Return a newly allocated string composed of the concatenation of |
52 | the END1 first chars of STRING1, and STRING2. |
53 `-----------------------------------------------------------------*/
56 stringappend (const char *string1
, int end1
, const char *string2
)
68 res
= XCALLOC (char, i
+ end1
+ 1);
72 for (i
= 0; i
< end1
; i
++)
76 while ((*cp
++ = *cp1
++))
82 /*-----------------------------------------------------------------.
83 | Try to open file NAME with mode MODE, and print an error message |
85 `-----------------------------------------------------------------*/
88 xfopen (const char *name
, const char *mode
)
92 ptr
= fopen (name
, mode
);
94 error (2, errno
, _("cannot open file `%s'"), name
);
99 /*-------------------------------------------------------------.
100 | Try to close file PTR, and print an error message if fails. |
101 `-------------------------------------------------------------*/
111 result
= fclose (ptr
);
113 error (2, errno
, _("cannot close file"));
118 /*--------------------------------------------------.
119 | Save the content of the obstack OBS in FILENAME. |
120 `--------------------------------------------------*/
123 obstack_save (struct obstack
*obs
, const char *filename
)
125 FILE *out
= xfopen (filename
, "w");
126 size_t size
= obstack_object_size (obs
);
127 fwrite (obstack_finish (obs
), 1, size
, out
);
132 /*------------------------------------------------------------------.
133 | Return the path to the skeleton which locaction might be given in |
134 | ENVVAR, otherwise return SKELETON. |
135 `------------------------------------------------------------------*/
138 skeleton_find (const char *envvar
, const char *skeleton
)
140 const char *res
= getenv (envvar
);
145 /* File doesn't exist in current directory; try in INIT directory. */
146 if (!res
&& (cp
= getenv ("INIT")))
148 res
= XMALLOC (char, strlen (cp
) + strlen (skeleton
) + 2);
149 sprintf (res
, "%s%c%s", cp
, '/', skeleton
);
160 /*-----------------------------------------------------------------.
161 | Open the input file. Look for the skeletons. Find the names of |
162 | the output files. Prepare the obstacks. |
163 `-----------------------------------------------------------------*/
170 int short_base_length
;
174 /* -o was specified. The precise -o name will be used for FTABLE.
175 For other output files, remove the ".c" or ".tab.c" suffix. */
176 name_base
= spec_outfile
;
180 /* BASE_LENGTH includes ".tab" but not ".c". */
181 base_length
= strlen (name_base
);
182 if (!strcmp (name_base
+ base_length
- 2, ".c"))
184 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
185 short_base_length
= base_length
;
186 if (!strncmp (name_base
+ short_base_length
- 4, ".tab", 4))
187 short_base_length
-= 4;
188 else if (!strncmp (name_base
+ short_base_length
- 4, "_tab", 4))
189 short_base_length
-= 4;
191 else if (spec_file_prefix
)
193 /* -b was specified. Construct names from it. */
194 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
195 short_base_length
= strlen (spec_file_prefix
);
196 /* Count room for `.tab'. */
197 base_length
= short_base_length
+ 4;
198 name_base
= XMALLOC (char, base_length
+ 1);
200 strcpy (name_base
, spec_file_prefix
);
201 strcat (name_base
, EXT_TAB
);
208 /* -o was not specified; compute output file name from input
209 or use y.tab.c, etc., if -y was specified. */
211 static char FIXED_NAME_BASE
[] = "y.y";
213 name_base
= yacc_flag
? FIXED_NAME_BASE
: infile
;
215 /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
217 base_length
= strlen (name_base
);
218 if (!strcmp (name_base
+ base_length
- 2, ".y"))
220 short_base_length
= base_length
;
222 name_base
= stringappend (name_base
, short_base_length
, EXT_TAB
);
223 base_length
= short_base_length
+ 4;
226 finput
= xfopen (infile
, "r");
230 /* We used to use just .out if spec_name_prefix (-p) was used,
231 but that conflicts with Posix. */
232 outfile
= stringappend (name_base
, short_base_length
, EXT_OUTPUT
);
233 foutput
= xfopen (outfile
, "w");
238 /* use permanent name for actions file */
239 actfile
= stringappend (name_base
, short_base_length
, ".act");
244 defsfile
= stringappend (name_base
, base_length
, ".h");
247 /* These are opened by `done' or `open_extra_files', if at all */
249 tabfile
= spec_outfile
;
251 tabfile
= stringappend (name_base
, base_length
, ".c");
253 attrsfile
= stringappend (name_base
, short_base_length
, EXT_STYPE_H
);
254 guardfile
= stringappend (name_base
, short_base_length
, EXT_GUARD_C
);
256 /* Initialize the obstacks. */
257 obstack_init (&action_obstack
);
258 obstack_init (&attrs_obstack
);
259 obstack_init (&table_obstack
);
260 obstack_init (&defines_obstack
);
265 /*-----------------------------------------------------.
266 | Close the open files, produce all the output files. |
267 `-----------------------------------------------------*/
276 /* Output the main file. */
277 obstack_save (&table_obstack
, tabfile
);
279 /* Output the header file if wanted. */
281 obstack_save (&defines_obstack
, defsfile
);
283 /* If we output only the table, dump the actions in ACTFILE.
286 obstack_save (&action_obstack
, actfile
);
288 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
289 into its own file, ATTTRSFILE. */
291 obstack_save (&attrs_obstack
, attrsfile
);