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