]> git.saurik.com Git - bison.git/blob - src/files.c
0051d3f484feea3f91c6ffb70d71230401fcf1d1
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 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 struct obstack graph_obstack;
38
39 char *spec_outfile = NULL; /* for -o. */
40 char *spec_file_prefix = NULL; /* for -b. */
41 char *spec_name_prefix = NULL; /* for -p. */
42 char *spec_verbose_file = NULL; /* for --verbose. */
43 char *spec_graph_file = NULL; /* for -g. */
44 char *spec_defines_file = NULL; /* for --defines. */
45
46 char *infile = NULL;
47 char *attrsfile = NULL;
48
49 static char *base_name = NULL;
50 static char *short_base_name = NULL;
51
52 /* C source file extension (the parser source). */
53 const char *src_extension = NULL;
54 /* Header file extension (if option ``-d'' is specified). */
55 const char *header_extension = NULL;
56 \f
57
58 /*--------------------------.
59 | Is SUFFIX ending STRING? |
60 `--------------------------*/
61
62 static int
63 strsuffix (const char *string, const char *suffix)
64 {
65 size_t string_len = strlen (string);
66 size_t suffix_len = strlen (suffix);
67 if (suffix_len <= string_len)
68 return !strcmp (string + string_len - suffix_len, suffix);
69 else
70 return 0;
71 }
72
73
74 /*-----------------------------------------------------------------.
75 | Return a newly allocated string composed of the concatenation of |
76 | STRING1, and STRING2. |
77 `-----------------------------------------------------------------*/
78
79 static char *
80 stringappend (const char *string1, const char *string2)
81 {
82 size_t len = strlen (string1) + strlen (string2);
83 char *res = XMALLOC (char, len + 1);
84 char *cp;
85 cp = stpcpy (res, string1);
86 cp = stpcpy (cp, string2);
87 return res;
88 }
89
90
91 /*---------------------------------------------------------------.
92 | Computes the macro name used to avoid double inclusion in the |
93 | header of the parser and store it in header_macro_name. |
94 `---------------------------------------------------------------*/
95
96 static char *
97 compute_header_macro (void)
98 {
99 int ite;
100 char *macro_name;
101
102 if (spec_defines_file)
103 macro_name = xstrdup (spec_defines_file);
104 else
105 {
106 macro_name = XMALLOC (char,
107 strlen (base_name) +
108 strlen (header_extension) + 1);
109
110 stpcpy (macro_name, base_name);
111 strcat (macro_name, header_extension);
112 }
113
114 for (ite = 0; macro_name[ite]; ite++)
115 if (macro_name[ite] == '.')
116 macro_name[ite] = '_';
117 else
118 {
119 if (islower (macro_name[ite]))
120 macro_name[ite] -= ('a' - 'A');
121 }
122 return macro_name;
123 }
124
125
126 /*-----------------------------------------------------------------.
127 | Try to open file NAME with mode MODE, and print an error message |
128 | if fails. |
129 `-----------------------------------------------------------------*/
130
131 FILE *
132 xfopen (const char *name, const char *mode)
133 {
134 FILE *ptr;
135
136 ptr = fopen (name, mode);
137 if (!ptr)
138 error (2, errno, _("cannot open file `%s'"), name);
139
140 return ptr;
141 }
142
143 /*-------------------------------------------------------------.
144 | Try to close file PTR, and print an error message if fails. |
145 `-------------------------------------------------------------*/
146
147 int
148 xfclose (FILE *ptr)
149 {
150 int result;
151
152 if (ptr == NULL)
153 return 0;
154
155 result = fclose (ptr);
156 if (result == EOF)
157 error (2, errno, _("cannot close file"));
158
159 return result;
160 }
161
162 /*--------------------------------------------------.
163 | Save the content of the obstack OBS in FILENAME. |
164 `--------------------------------------------------*/
165
166 static void
167 obstack_save (struct obstack *obs, const char *filename)
168 {
169 FILE *out = xfopen (filename, "w");
170 size_t size = obstack_object_size (obs);
171 fwrite (obstack_finish (obs), 1, size, out);
172 xfclose (out);
173 }
174
175 /*---------------------------------------------------------------------.
176 | Output double inclusion protection macros and saves defines_obstack |
177 `---------------------------------------------------------------------*/
178
179 static void
180 defines_obstack_save (const char *filename)
181 {
182 FILE *out = xfopen (filename, "w");
183 size_t size = obstack_object_size (&defines_obstack);
184 char *macro_name = compute_header_macro ();
185
186 fprintf (out, "#ifndef %s\n", macro_name);
187 fprintf (out, "# define %s\n\n", macro_name);
188 fwrite (obstack_finish (&defines_obstack), 1, size, out);
189 fprintf (out, "\n#endif /* not %s */\n", macro_name);
190
191 free (macro_name);
192 xfclose (out);
193 }
194
195 /*------------------------------------------------------------------.
196 | Return the path to the skeleton which locaction might be given in |
197 | ENVVAR, otherwise return SKELETON_NAME. |
198 `------------------------------------------------------------------*/
199
200 const char *
201 skeleton_find (const char *envvar, const char *skeleton_name)
202 {
203 const char *res = getenv (envvar);
204
205 #ifdef MSDOS
206 const char *cp;
207
208 /* File doesn't exist in current directory; try in INIT directory. */
209 if (!res && (cp = getenv ("INIT")))
210 {
211 res = XMALLOC (char, strlen (cp) + strlen (skeleton_name) + 2);
212 sprintf (res, "%s%c%s", cp, '/', skeleton_name);
213 }
214 #endif /* !MSDOS */
215
216 if (!res)
217 res = skeleton_name;
218
219 return res;
220 }
221 \f
222
223 /*----------------------------------------------------------------.
224 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
225 `----------------------------------------------------------------*/
226
227 /* Replace all characters FROM by TO in the string IN.
228 and returns a new allocated string. */
229 static char *
230 tr (const char *in, char from, char to)
231 {
232 char *temp;
233 char *out;
234
235 out = XMALLOC (char, strlen (in) + 1);
236
237 for (temp = out; *in; in++, out++)
238 if (*in == from)
239 *out = to;
240 else
241 *out = *in;
242 *out = 0;
243 return (temp);
244 }
245
246 /* Gets the extension index in FILENAME. Returns 0 if fails to
247 find an extension. */
248 static int
249 get_extension_index (const char *filename)
250 {
251 int len;
252
253 len = strlen (filename);
254
255 if (filename[len-- - 1] == '.')
256 return (0);
257
258 while ((len > 0) && (filename[len - 1] != '.'))
259 if (filename[len - 1] == '/')
260 return (0);
261 else
262 len--;
263
264 return (len - 1);
265 }
266
267 /* Computes extensions from the grammar file extension. */
268 static void
269 compute_exts_from_gf (const char *ext)
270 {
271 src_extension = tr (ext, 'y', 'c');
272 src_extension = tr (src_extension, 'Y', 'C');
273 header_extension = tr (ext, 'y', 'h');
274 header_extension = tr (header_extension, 'Y', 'H');
275 }
276
277 /* Computes extensions from the given c source file extension. */
278 static void
279 compute_exts_from_src (const char *ext)
280 {
281 /* We use this function when the user specifies `-o' or `--output',
282 so the extenions must be computed unconditionally from the file name
283 given by this option. */
284 src_extension = xstrdup (ext);
285 header_extension = tr (ext, 'c', 'h');
286 header_extension = tr (header_extension, 'C', 'H');
287 }
288
289 /* FIXME: Should use xstrndup. */
290
291 static void
292 compute_base_names (void)
293 {
294 size_t base_length;
295 size_t short_base_length;
296 size_t ext_index;
297
298 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
299 BASE_NAME and SHORT_BASE_NAME are `foo'.
300
301 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
302 SHORT_BASE_NAME is `foo'.
303
304 The precise -o name will be used for FTABLE. For other output
305 files, remove the ".c" or ".tab.c" suffix. */
306 if (spec_outfile)
307 {
308 #ifdef MSDOS
309 strlwr (spec_outfile);
310 #endif /* MSDOS */
311 /* BASE_LENGTH includes ".tab" but not ".c". */
312 base_length = strlen (spec_outfile);
313
314 ext_index = get_extension_index (spec_outfile);
315 /* If the initial segment of extension contains 'c' or a 'C', I assume
316 that it is a C or C++ source file. */
317 if (ext_index)
318 ext_index =
319 (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
320 if (ext_index)
321 {
322 base_length -= strlen (spec_outfile + ext_index);
323 compute_exts_from_src (spec_outfile + ext_index);
324 }
325
326 base_name = strndup (spec_outfile, base_length);
327 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
328 short_base_length = base_length;
329 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
330 short_base_length -= 4;
331 short_base_name = strndup (spec_outfile, short_base_length);
332
333 return;
334 }
335
336 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
337 are `foo'.
338
339 Construct names from it. */
340 if (spec_file_prefix)
341 {
342 #ifdef MSDOS
343 strlwr (spec_file_prefix);
344 #endif /* MSDOS */
345 short_base_name = xstrdup (spec_file_prefix);
346 base_name = XMALLOC (char,
347 strlen (short_base_name) + strlen (EXT_TAB) + 1);
348 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
349
350 /* Computes the extensions from the garmmar file name. */
351 ext_index = get_extension_index (infile);
352 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
353 that it is a yacc or bison grammar file. */
354 if (ext_index)
355 ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0;
356 if (ext_index)
357 compute_exts_from_gf (infile + ext_index);
358
359 return;
360 }
361
362 /* If neither -o nor --file-prefix were specified, and the input
363 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
364 `foo'.
365
366 If --yacc is used, do as if the input file was `y.y'. */
367 {
368 const char *name_base = yacc_flag ? "y.y" : infile;
369
370 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
371
372 base_length = strlen (name_base);
373
374 ext_index = get_extension_index (name_base);
375 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
376 that it is a yacc or bison grammar file. */
377 if (ext_index)
378 ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0;
379 if (ext_index)
380 {
381 base_length -= strlen (name_base + ext_index);
382 compute_exts_from_gf (name_base + ext_index);
383 }
384
385 short_base_length = base_length;
386 short_base_name = strndup (name_base, short_base_length);
387
388 base_name = XMALLOC (char,
389 strlen (short_base_name) + strlen (EXT_TAB) + 1);
390 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
391
392 return;
393 }
394 }
395
396 /*-------------------------------------------------------.
397 | Close the open files, compute the output files names. |
398 `-------------------------------------------------------*/
399
400 void
401 compute_output_file_names (void)
402 {
403 compute_base_names ();
404
405 /* If not yet done. */
406 if (!src_extension)
407 src_extension = ".c";
408 if (!header_extension)
409 header_extension = ".h";
410
411 /* It the defines filename if not given, we create it. */
412 if (!spec_defines_file)
413 spec_defines_file = stringappend (base_name, header_extension);
414
415 /* It the graph filename if not given, we create it. */
416 if (!spec_graph_file)
417 spec_graph_file = stringappend (short_base_name, ".vcg");
418
419 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
420
421 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
422 #ifndef MSDOS
423 attrsfile = stringappend (attrsfile, header_extension);
424 #endif /* MSDOS */
425
426 }
427
428 /*-----------------------------------------------------------------.
429 | Open the input file. Look for the skeletons. Find the names of |
430 | the output files. Prepare the obstacks. |
431 `-----------------------------------------------------------------*/
432
433 void
434 open_files (void)
435 {
436 finput = xfopen (infile, "r");
437
438 /* Initialize the obstacks. */
439 obstack_init (&action_obstack);
440 obstack_init (&attrs_obstack);
441 obstack_init (&table_obstack);
442 obstack_init (&defines_obstack);
443 obstack_init (&guard_obstack);
444 obstack_init (&output_obstack);
445 obstack_init (&graph_obstack);
446 }
447
448
449
450 /*-----------------------.
451 | Close the open file.. |
452 `-----------------------*/
453
454 void
455 close_files (void)
456 {
457 xfclose (finput);
458 }
459
460 /*---------------------------.
461 | Produce the output files. |
462 `---------------------------*/
463
464 void
465 output_files (void)
466 {
467 /* Output the main file. */
468 if (spec_outfile)
469 obstack_save (&table_obstack, spec_outfile);
470 else
471 obstack_save (&table_obstack, stringappend (base_name, src_extension));
472
473 /* Output the header file if wanted. */
474 if (defines_flag)
475 defines_obstack_save (spec_defines_file);
476
477 /* If we output only the table, dump the actions in ACTFILE. */
478 if (no_parser_flag)
479 obstack_save (&action_obstack, stringappend (short_base_name, ".act"));
480
481 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
482 into its own file, ATTTRSFILE. */
483 if (semantic_parser)
484 {
485 char *temp_name;
486
487 obstack_save (&attrs_obstack, attrsfile);
488 temp_name = stringappend (short_base_name, EXT_GUARD_C);
489 #ifndef MSDOS
490 temp_name = stringappend (temp_name, src_extension);
491 #endif /* MSDOS */
492 obstack_save (&guard_obstack, temp_name);
493 }
494
495 if (graph_flag)
496 obstack_save (&graph_obstack, spec_graph_file);
497 }