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