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