]> git.saurik.com Git - bison.git/blob - src/files.c
4946477db8d89d71fd1be154ac608025e6f65ff5
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307, USA. */
20
21
22 #include "system.h"
23 #include "getargs.h"
24 #include "files.h"
25 #include "xalloc.h"
26 #include "gram.h"
27 #include "complain.h"
28
29 FILE *finput = NULL;
30
31 struct obstack action_obstack;
32 struct obstack attrs_obstack;
33 struct obstack table_obstack;
34 struct obstack defines_obstack;
35 struct obstack guard_obstack;
36 struct obstack output_obstack;
37
38 char *spec_outfile = NULL; /* for -o. */
39 char *spec_file_prefix = NULL; /* for -b. */
40 char *spec_name_prefix = NULL; /* for -p. */
41
42 char *infile = NULL;
43 char *attrsfile = NULL;
44
45 static char *base_name = NULL;
46 static char *short_base_name = NULL;
47
48 /* C source file extension (the parser source). */
49 const char *src_extension = NULL;
50 /* Header file extension (if option ``-d'' is specified). */
51 const char *header_extension = NULL;
52
53 \f
54 /*--------------------------.
55 | Is SUFFIX ending STRING? |
56 `--------------------------*/
57
58 static int
59 strsuffix (const char *string, const char *suffix)
60 {
61 size_t string_len = strlen (string);
62 size_t suffix_len = strlen (suffix);
63 if (suffix_len <= string_len)
64 return !strcmp (string + string_len - suffix_len, suffix);
65 else
66 return 0;
67 }
68
69
70 /*-----------------------------------------------------------------.
71 | Return a newly allocated string composed of the concatenation of |
72 | STRING1, and STRING2. |
73 `-----------------------------------------------------------------*/
74
75 static char *
76 stringappend (const char *string1, const char *string2)
77 {
78 size_t len = strlen (string1) + strlen (string2);
79 char *res = XMALLOC (char, len + 1);
80 char *cp;
81 cp = stpcpy (res, string1);
82 cp = stpcpy (cp, string2);
83 return res;
84 }
85
86 /*-----------------------------------------------------------------.
87 | Try to open file NAME with mode MODE, and print an error message |
88 | if fails. |
89 `-----------------------------------------------------------------*/
90
91 FILE *
92 xfopen (const char *name, const char *mode)
93 {
94 FILE *ptr;
95
96 ptr = fopen (name, mode);
97 if (!ptr)
98 error (2, errno, _("cannot open file `%s'"), name);
99
100 return ptr;
101 }
102
103 /*-------------------------------------------------------------.
104 | Try to close file PTR, and print an error message if fails. |
105 `-------------------------------------------------------------*/
106
107 int
108 xfclose (FILE *ptr)
109 {
110 int result;
111
112 if (ptr == NULL)
113 return 0;
114
115 result = fclose (ptr);
116 if (result == EOF)
117 error (2, errno, _("cannot close file"));
118
119 return result;
120 }
121
122 /*--------------------------------------------------.
123 | Save the content of the obstack OBS in FILENAME. |
124 `--------------------------------------------------*/
125
126 static void
127 obstack_save (struct obstack *obs, const char *filename)
128 {
129 FILE *out = xfopen (filename, "w");
130 size_t size = obstack_object_size (obs);
131 fwrite (obstack_finish (obs), 1, size, out);
132 xfclose (out);
133 }
134
135
136 /*------------------------------------------------------------------.
137 | Return the path to the skeleton which locaction might be given in |
138 | ENVVAR, otherwise return SKELETON_NAME. |
139 `------------------------------------------------------------------*/
140
141 const char *
142 skeleton_find (const char *envvar, const char *skeleton_name)
143 {
144 const char *res = getenv (envvar);
145
146 #ifdef MSDOS
147 const char *cp;
148
149 /* File doesn't exist in current directory; try in INIT directory. */
150 if (!res && (cp = getenv ("INIT")))
151 {
152 res = XMALLOC (char, strlen (cp) + strlen (skeleton_name) + 2);
153 sprintf (res, "%s%c%s", cp, '/', skeleton_name);
154 }
155 #endif /* !MSDOS */
156
157 if (!res)
158 res = skeleton_name;
159
160 return res;
161 }
162
163 \f
164 /*----------------------------------------------------------------.
165 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
166 `----------------------------------------------------------------*/
167
168 /* Replace all characters FROM by TO in the string IN.
169 and returns a new allocated string. */
170 static char *
171 tr(const char *in, char from, char to)
172 {
173 char *temp;
174 char *out;
175
176 out = XMALLOC (char, strlen (in) + 1);
177
178 for (temp = out; *in; in++, out++)
179 if (*in == from)
180 *out = to;
181 else
182 *out = *in;
183 *out = 0;
184 return (temp);
185 }
186
187 /* Gets the extension index in FILENAME. Returns 0 if fails to
188 find an extension. */
189 static int
190 get_extension_index(const char *filename)
191 {
192 int len;
193
194 len = strlen (filename);
195
196 if (filename[len-- - 1] == '.')
197 return (0);
198
199 while ((len > 0) && (filename[len - 1] != '.'))
200 if (filename[len - 1] == '/')
201 return (0);
202 else
203 len--;
204
205 return (len - 1);
206 }
207
208 /* Computes extensions from the grammar file extension. */
209 static void
210 compute_exts_from_gf(const char *ext)
211 {
212 if (!src_extension)
213 {
214 src_extension = tr(ext, 'y', 'c');
215 src_extension = tr(src_extension, 'Y', 'C');
216 }
217 if (!header_extension)
218 {
219 header_extension = tr(ext, 'y', 'h');
220 header_extension = tr(header_extension, 'Y', 'H');
221 }
222 }
223
224 /* Computes extensions from the given c source file extension. */
225 static void
226 compute_exts_from_src(const char *ext)
227 {
228 if (!src_extension)
229 src_extension = xstrdup(ext);
230 if (!header_extension)
231 {
232 header_extension = tr(ext, 'c', 'h');
233 header_extension = tr(header_extension, 'C', 'H');
234 }
235 }
236
237 /* FIXME: Should use xstrndup. */
238
239 static void
240 compute_base_names (void)
241 {
242 size_t base_length;
243 size_t short_base_length;
244 size_t ext_index;
245
246 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
247 BASE_NAME and SHORT_BASE_NAME are `foo'.
248
249 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
250 SHORT_BASE_NAME is `foo'.
251
252 The precise -o name will be used for FTABLE. For other output
253 files, remove the ".c" or ".tab.c" suffix. */
254 if (spec_outfile)
255 {
256 #ifdef MSDOS
257 strlwr (spec_outfile);
258 #endif /* MSDOS */
259 /* BASE_LENGTH includes ".tab" but not ".c". */
260 base_length = strlen (spec_outfile);
261
262 ext_index = get_extension_index (spec_outfile);
263 /* if the initial segment of extension contains 'c' or a 'C', I assume
264 that it is a C or C++ source file */
265 if (ext_index)
266 ext_index = (strspn(spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
267 if (ext_index)
268 {
269 base_length -= strlen (spec_outfile + ext_index);
270 compute_exts_from_src(spec_outfile + ext_index);
271 }
272
273 base_name = strndup (spec_outfile, base_length);
274 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
275 short_base_length = base_length;
276 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
277 short_base_length -= 4;
278 short_base_name = strndup (spec_outfile, short_base_length);
279
280 return;
281 }
282
283 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
284 are `foo'.
285
286 Construct names from it. */
287 if (spec_file_prefix)
288 {
289 #ifdef MSDOS
290 strlwr (spec_file_prefix);
291 #endif /* MSDOS */
292 short_base_name = xstrdup (spec_file_prefix);
293 base_name = XMALLOC (char,
294 strlen (short_base_name) + strlen (EXT_TAB) + 1);
295 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
296
297 return;
298 }
299
300 /* If neither -o nor --file-prefix were specified, and the input
301 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
302 `foo'.
303
304 If --yacc is used, do as if the input file was `y.y'. */
305 {
306 const char *name_base = yacc_flag ? "y.y" : infile;
307
308 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
309
310 base_length = strlen (name_base);
311
312 ext_index = get_extension_index (name_base);
313 /* if the initial segment of extension contains a 'y' or a 'Y', I assume
314 that it is a yacc or bison grammar file */
315 if (ext_index)
316 ext_index = (strspn(name_base + ext_index + 1, "yY")) ? ext_index : 0;
317 if (ext_index)
318 {
319 base_length -= strlen (name_base + ext_index);
320 compute_exts_from_gf(name_base + ext_index);
321 }
322
323 short_base_length = base_length;
324 short_base_name = strndup (name_base, short_base_length);
325
326 base_name = XMALLOC (char,
327 strlen (short_base_name) + strlen (EXT_TAB) + 1);
328 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
329
330 return;
331 }
332 }
333
334 /*-----------------------------------------------------------------.
335 | Open the input file. Look for the skeletons. Find the names of |
336 | the output files. Prepare the obstacks. |
337 `-----------------------------------------------------------------*/
338
339 void
340 open_files (void)
341 {
342 finput = xfopen (infile, "r");
343
344 /* Initialize the obstacks. */
345 obstack_init (&action_obstack);
346 obstack_init (&attrs_obstack);
347 obstack_init (&table_obstack);
348 obstack_init (&defines_obstack);
349 obstack_init (&guard_obstack);
350 obstack_init (&output_obstack);
351 }
352
353
354
355 /*-----------------------------------------------------.
356 | Close the open files, produce all the output files. |
357 `-----------------------------------------------------*/
358
359 void
360 output_files (void)
361 {
362 xfclose (finput);
363
364 compute_base_names ();
365
366 /* Set default extensions */
367 if (!src_extension)
368 src_extension = ".c";
369 if (!header_extension)
370 header_extension = ".h";
371
372 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
373 #ifndef MSDOS
374 stringappend (attrsfile, header_extension);
375 #endif /* MSDOS */
376
377 /* Output the main file. */
378 if (spec_outfile)
379 obstack_save (&table_obstack, spec_outfile);
380 else
381 obstack_save (&table_obstack, stringappend (base_name, src_extension));
382
383 /* Output the header file if wanted. */
384 if (defines_flag)
385 obstack_save (&defines_obstack, stringappend (base_name, header_extension));
386
387 /* If we output only the table, dump the actions in ACTFILE. */
388 if (no_parser_flag)
389 obstack_save (&action_obstack, stringappend (short_base_name, ".act"));
390
391 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
392 into its own file, ATTTRSFILE. */
393 if (semantic_parser)
394 {
395 char *temp_name;
396
397 obstack_save (&attrs_obstack, attrsfile);
398 temp_name = stringappend (short_base_name, EXT_GUARD_C);
399 #ifndef MSDOS
400 temp_name = stringappend (temp_name, src_extension);
401 #endif /* MSDOS */
402 obstack_save (&guard_obstack, temp_name);
403 }
404
405 if (verbose_flag)
406 /* We used to use just .out if spec_name_prefix (-p) was used, but
407 that conflicts with Posix. */
408 obstack_save (&output_obstack, stringappend (short_base_name, EXT_OUTPUT));
409 }