]> git.saurik.com Git - bison.git/blob - src/files.c
.
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23 #include "system.h"
24 #include "getargs.h"
25 #include "files.h"
26 #include "gram.h"
27 #include "error.h"
28 #include "complain.h"
29
30 /* From basename.c. Almost a lie, as it returns a char *. */
31 const char *base_name PARAMS ((char const *name));
32 /* From xstrndup.c. */
33 char *xstrndup PARAMS ((const char *s, size_t n));
34
35 FILE *finput = NULL;
36
37 struct obstack action_obstack;
38 struct obstack attrs_obstack;
39 struct obstack table_obstack;
40 struct obstack defines_obstack;
41 struct obstack guard_obstack;
42 struct obstack output_obstack;
43
44 char *spec_outfile = NULL; /* for -o. */
45 char *spec_file_prefix = NULL; /* for -b. */
46 char *spec_name_prefix = NULL; /* for -p. */
47 char *spec_verbose_file = NULL; /* for --verbose. */
48 char *spec_graph_file = NULL; /* for -g. */
49 char *spec_defines_file = NULL; /* for --defines. */
50
51 char *infile = NULL;
52 char *attrsfile = NULL;
53
54 static char *full_base_name = NULL;
55 static char *short_base_name = NULL;
56
57 /* C source file extension (the parser source). */
58 const char *src_extension = NULL;
59 /* Header file extension (if option ``-d'' is specified). */
60 const char *header_extension = NULL;
61 \f
62
63 /*--------------------------.
64 | Is SUFFIX ending STRING? |
65 `--------------------------*/
66
67 int
68 strsuffix (const char *string, const char *suffix)
69 {
70 size_t string_len = strlen (string);
71 size_t suffix_len = strlen (suffix);
72 if (suffix_len <= string_len)
73 return !strcmp (string + string_len - suffix_len, suffix);
74 else
75 return 0;
76 }
77
78
79 /*-----------------------------------------------------------------.
80 | Return a newly allocated string composed of the concatenation of |
81 | STRING1, and STRING2. |
82 `-----------------------------------------------------------------*/
83
84 static char *
85 stringappend (const char *string1, const char *string2)
86 {
87 size_t len = strlen (string1) + strlen (string2);
88 char *res = XMALLOC (char, len + 1);
89 char *cp;
90 cp = stpcpy (res, string1);
91 cp = stpcpy (cp, string2);
92 return res;
93 }
94
95
96 /*-----------------------------------------------------------------.
97 | Computes the macro name used to avoid double inclusion in the |
98 | header of the parser and store it in header_macro_name. Be sure |
99 | to produce valid CPP names (don't start with digit, remain |
100 | alphanumerical + underscore). |
101 `-----------------------------------------------------------------*/
102
103 static char *
104 compute_header_macro (void)
105 {
106 const char *prefix = "BISON_";
107 char *macro_name, *cp;
108
109 if (spec_defines_file)
110 {
111 macro_name = XMALLOC (char,
112 strlen (prefix) +
113 strlen (spec_defines_file) + 1);
114 cp = stpcpy (macro_name, prefix);
115 cp = stpcpy (cp, spec_defines_file);
116 }
117 else
118 {
119 macro_name = XMALLOC (char,
120 strlen (prefix) +
121 strlen (full_base_name) +
122 strlen (header_extension) + 1);
123 cp = stpcpy (macro_name, prefix);
124 cp = stpcpy (cp, full_base_name);
125 cp = stpcpy (cp, header_extension);
126 }
127
128 for (cp = macro_name; *cp; ++cp)
129 if (islower (*cp))
130 *cp = toupper (*cp);
131 else if (!isalnum (*cp))
132 *cp = '_';
133
134 return macro_name;
135 }
136
137
138 /*-----------------------------------------------------------------.
139 | Try to open file NAME with mode MODE, and print an error message |
140 | if fails. |
141 `-----------------------------------------------------------------*/
142
143 FILE *
144 xfopen (const char *name, const char *mode)
145 {
146 FILE *ptr;
147
148 ptr = fopen (name, mode);
149 if (!ptr)
150 error (2, errno, _("cannot open file `%s'"), name);
151
152 return ptr;
153 }
154
155 /*-------------------------------------------------------------.
156 | Try to close file PTR, and print an error message if fails. |
157 `-------------------------------------------------------------*/
158
159 int
160 xfclose (FILE *ptr)
161 {
162 int result;
163
164 if (ptr == NULL)
165 return 0;
166
167 result = fclose (ptr);
168 if (result == EOF)
169 error (2, errno, _("cannot close file"));
170
171 return result;
172 }
173
174 /*--------------------------------------------------.
175 | Save the content of the obstack OBS in FILENAME. |
176 `--------------------------------------------------*/
177
178 static void
179 obstack_save (struct obstack *obs, const char *filename)
180 {
181 FILE *out = xfopen (filename, "w");
182 size_t size = obstack_object_size (obs);
183 fwrite (obstack_finish (obs), 1, size, out);
184 xfclose (out);
185 }
186
187 /*---------------------------------------------------------------------.
188 | Output double inclusion protection macros and saves defines_obstack |
189 `---------------------------------------------------------------------*/
190
191 static void
192 defines_obstack_save (const char *filename)
193 {
194 FILE *out = xfopen (filename, "w");
195 size_t size = obstack_object_size (&defines_obstack);
196 char *macro_name = compute_header_macro ();
197
198 fprintf (out, "#ifndef %s\n", macro_name);
199 fprintf (out, "# define %s\n\n", macro_name);
200 fwrite (obstack_finish (&defines_obstack), 1, size, out);
201 fprintf (out, "\n#endif /* not %s */\n", macro_name);
202
203 free (macro_name);
204 xfclose (out);
205 }
206
207 /*------------------------------------------------------------------.
208 | Return the path to the skeleton which locaction might be given in |
209 | ENVVAR, otherwise return SKELETON_NAME. |
210 `------------------------------------------------------------------*/
211
212 const char *
213 skeleton_find (const char *envvar, const char *skeleton_name)
214 {
215 const char *res = getenv (envvar);
216
217 #if defined (MSDOS) || defined (_WIN32)
218 const char *cp = getenv ("INIT");
219 if (!res)
220 {
221 /* Skeleton file name without path */
222 const char *skel_name = strrchr (skeleton_name, '/');
223 if (!skel_name)
224 skel_name = strrchr (skeleton_name, '\\');
225 if (!skel_name)
226 skel_name = skeleton_name;
227 else
228 ++skel_name;
229
230 /* File doesn't exist in current directory; try in INIT directory. */
231 if (cp)
232 {
233 res = XMALLOC (char, strlen (cp) + strlen (skel_name) + 2);
234 sprintf (res, "%s%c%s", cp, '\\', skel_name);
235 }
236 else if (access (skel_name, 4) == 0) /* Look in current dir. */
237 res = skel_name;
238 else
239 {
240 /* Look in program locations dir. */
241 extern char *program_name;
242 cp = strrchr(program_name, '\\');
243 if (!cp)
244 return skeleton_name;
245 else
246 ++cp;
247 res = XMALLOC (char, cp - program_name + strlen (skel_name) + 1);
248 strncpy (res, program_name, cp - program_name);
249 strcpy (res + (cp - program_name), skel_name);
250 }
251 }
252 #endif /* defined (MSDOS) || defined (_WIN32) */
253 if (!res)
254 res = skeleton_name;
255
256 return res;
257 }
258 \f
259
260 /*----------------------------------------------------------------.
261 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
262 `----------------------------------------------------------------*/
263
264 /* Replace all characters FROM by TO in the string IN.
265 and returns a new allocated string. */
266 static char *
267 tr (const char *in, char from, char to)
268 {
269 char *temp;
270 char *out;
271
272 out = XMALLOC (char, strlen (in) + 1);
273
274 for (temp = out; *in; in++, out++)
275 if (*in == from)
276 *out = to;
277 else
278 *out = *in;
279 *out = 0;
280 return (temp);
281 }
282
283 /* Computes extensions from the grammar file extension. */
284 static void
285 compute_exts_from_gf (const char *ext)
286 {
287 src_extension = tr (ext, 'y', 'c');
288 src_extension = tr (src_extension, 'Y', 'C');
289 header_extension = tr (ext, 'y', 'h');
290 header_extension = tr (header_extension, 'Y', 'H');
291 }
292
293 /* Computes extensions from the given c source file extension. */
294 static void
295 compute_exts_from_src (const char *ext)
296 {
297 /* We use this function when the user specifies `-o' or `--output',
298 so the extenions must be computed unconditionally from the file name
299 given by this option. */
300 src_extension = xstrdup (ext);
301 header_extension = tr (ext, 'c', 'h');
302 header_extension = tr (header_extension, 'C', 'H');
303 }
304
305
306 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
307 part, (the directory) is ranging from FILENAME to the char before
308 *BASE, so we don't need an additional parameter.
309
310 *EXT points to the last period in the basename, or NULL if none.
311
312 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
313 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
314 cannot be equal to *BASE.
315
316 None are allocated, they are simply pointers to parts of FILENAME.
317 Examples:
318
319 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
320 '.c'
321
322 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
323
324 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
325
326 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
327
328 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
329
330 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
331
332 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
333
334 static void
335 filename_split (const char *filename,
336 const char **base, const char **tab, const char **ext)
337 {
338 *base = base_name (filename);
339
340 /* Look for the extension, i.e., look for the last dot. */
341 *ext = strrchr (*base, '.');
342 *tab = NULL;
343
344 /* If there is an exentension, check if there is a `.tab' part right
345 before. */
346 if (*ext
347 && (*ext - *base) > (int) strlen (".tab")
348 && (!strncmp (*ext - strlen (".tab"), ".tab", strlen (".tab"))
349 || !strncmp (*ext - strlen ("_tab"), "_tab", strlen ("_tab"))))
350 *tab = *ext - strlen (".tab");
351 }
352
353
354 /* FIXME: Should use xstrndup. */
355
356 static void
357 compute_base_names (void)
358 {
359 const char *base, *tab, *ext;
360
361 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
362 BASE_NAME and SHORT_BASE_NAME are `foo'.
363
364 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
365 SHORT_BASE_NAME is `foo'.
366
367 The precise -o name will be used for FTABLE. For other output
368 files, remove the ".c" or ".tab.c" suffix. */
369 if (spec_outfile)
370 {
371 filename_split (spec_outfile, &base, &tab, &ext);
372
373 /* The full base name goes up the EXT, excluding it. */
374 full_base_name =
375 xstrndup (spec_outfile,
376 (strlen (spec_outfile) - (ext ? strlen (ext) : 0)));
377 /* The short base name goes up to TAB, excluding it. */
378 short_base_name =
379 xstrndup (spec_outfile,
380 (strlen (spec_outfile)
381 - (tab ? strlen (tab) : (ext ? strlen (ext) : 0))));
382
383 if (ext)
384 compute_exts_from_src (ext);
385 }
386
387 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
388 and SHORT_BASE_NAME = `foo'.
389
390 Construct names from it. */
391 else
392 {
393 if (spec_file_prefix)
394 {
395 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
396 `foo'. */
397 short_base_name = xstrdup (spec_file_prefix);
398 }
399 else if (yacc_flag)
400 {
401 /* If --yacc, then the output is `y.tab.c'. */
402 short_base_name = xstrdup ("y");
403 }
404 else
405 {
406 /* Otherwise, the short base name is computed from the input
407 grammar: `foo.yy' => `foo'. */
408 filename_split (infile, &base, &tab, &ext);
409 short_base_name =
410 xstrndup (infile,
411 (strlen (infile) - (ext ? strlen (ext) : 0)));
412 }
413
414 /* In these cases, always append `.tab'. */
415 full_base_name = XMALLOC (char,
416 strlen (short_base_name)
417 + strlen (EXT_TAB) + 1);
418 stpcpy (stpcpy (full_base_name, short_base_name), EXT_TAB);
419
420 /* Computes the extensions from the grammar file name. */
421 filename_split (infile, &base, &tab, &ext);
422
423 if (ext && !yacc_flag)
424 compute_exts_from_gf (ext);
425 }
426 }
427
428
429 /*-------------------------------------------------------.
430 | Close the open files, compute the output files names. |
431 `-------------------------------------------------------*/
432
433 void
434 compute_output_file_names (void)
435 {
436 compute_base_names ();
437
438 /* If not yet done. */
439 if (!src_extension)
440 src_extension = ".c";
441 if (!header_extension)
442 header_extension = ".h";
443
444 /* It the defines filename if not given, we create it. */
445 if (!spec_defines_file)
446 spec_defines_file = stringappend (full_base_name, header_extension);
447
448 /* It the graph filename if not given, we create it. */
449 if (!spec_graph_file)
450 spec_graph_file = stringappend (short_base_name, ".vcg");
451
452 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
453
454 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
455 #ifndef MSDOS
456 attrsfile = stringappend (attrsfile, header_extension);
457 #endif /* MSDOS */
458
459 }
460
461 /*-----------------------------------------------------------------.
462 | Open the input file. Look for the skeletons. Find the names of |
463 | the output files. Prepare the obstacks. |
464 `-----------------------------------------------------------------*/
465
466 void
467 open_files (void)
468 {
469 finput = xfopen (infile, "r");
470
471 /* Initialize the obstacks. */
472 obstack_init (&action_obstack);
473 obstack_init (&attrs_obstack);
474 obstack_init (&table_obstack);
475 obstack_init (&defines_obstack);
476 obstack_init (&guard_obstack);
477 obstack_init (&output_obstack);
478 }
479
480
481
482 /*-----------------------.
483 | Close the open file.. |
484 `-----------------------*/
485
486 void
487 close_files (void)
488 {
489 xfclose (finput);
490 }
491
492 /*---------------------------.
493 | Produce the output files. |
494 `---------------------------*/
495
496 void
497 output_files (void)
498 {
499 /* Output the main file. */
500 if (spec_outfile)
501 obstack_save (&table_obstack, spec_outfile);
502 else
503 obstack_save (&table_obstack,
504 stringappend (full_base_name, src_extension));
505 obstack_free (&table_obstack, NULL);
506
507 /* Output the header file if wanted. */
508 if (defines_flag)
509 defines_obstack_save (spec_defines_file);
510 obstack_free (&defines_obstack, NULL);
511
512 /* If we output only the table, dump the actions in ACTFILE. */
513 if (no_parser_flag)
514 obstack_save (&action_obstack, stringappend (short_base_name, ".act"));
515 obstack_free (&action_obstack, NULL);
516
517 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
518 into its own file, ATTTRSFILE. */
519 if (semantic_parser)
520 {
521 char *temp_name;
522
523 obstack_save (&attrs_obstack, attrsfile);
524 obstack_free (&attrs_obstack, NULL);
525 temp_name = stringappend (short_base_name, EXT_GUARD_C);
526 #ifndef MSDOS
527 temp_name = stringappend (temp_name, src_extension);
528 #endif /* MSDOS */
529 obstack_save (&guard_obstack, temp_name);
530 obstack_free (&guard_obstack, NULL);
531 }
532 }