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