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