]> git.saurik.com Git - bison.git/blame - src/files.c
* src/print.c, src/print_graph.c (escape): New.
[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"
26cfe0be 26#include "error.h"
a0f6b076 27#include "complain.h"
54bd0db4
RS
28
29FILE *finput = NULL;
54bd0db4 30
8c7ebe49 31struct obstack action_obstack;
dd60faec 32struct obstack attrs_obstack;
ff4423cc 33struct obstack output_obstack;
8c7ebe49 34
b0ce6046 35char *spec_outfile = NULL; /* for -o. */
f0130d39 36char *spec_file_prefix = NULL; /* for -b. */
78af9bbc 37const char *spec_name_prefix = "yy"; /* for -p. */
342b8b6e
AD
38char *spec_verbose_file = NULL; /* for --verbose. */
39char *spec_graph_file = NULL; /* for -g. */
40char *spec_defines_file = NULL; /* for --defines. */
ea52d706 41char *parser_file_name = NULL;
54bd0db4 42
b0ce6046
AD
43char *infile = NULL;
44char *attrsfile = NULL;
4a120d45 45
b0ce6046
AD
46static char *base_name = NULL;
47static char *short_base_name = NULL;
27110317 48
f0130d39 49/* C source file extension (the parser source). */
7333d403 50const char *src_extension = NULL;
f0130d39 51/* Header file extension (if option ``-d'' is specified). */
7333d403 52const char *header_extension = NULL;
54bd0db4 53\f
f0130d39 54
19c50364
AD
55/*--------------------------.
56| Is SUFFIX ending STRING? |
57`--------------------------*/
58
59static int
60strsuffix (const char *string, const char *suffix)
61{
62 size_t string_len = strlen (string);
63 size_t suffix_len = strlen (suffix);
64 if (suffix_len <= string_len)
65 return !strcmp (string + string_len - suffix_len, suffix);
66 else
67 return 0;
68}
69
70
358c15b7
AD
71/*-----------------------------------------------------------------.
72| Return a newly allocated string composed of the concatenation of |
19c50364 73| STRING1, and STRING2. |
358c15b7 74`-----------------------------------------------------------------*/
54bd0db4 75
8963a27b 76static char *
19c50364 77stringappend (const char *string1, const char *string2)
54bd0db4 78{
19c50364
AD
79 size_t len = strlen (string1) + strlen (string2);
80 char *res = XMALLOC (char, len + 1);
358c15b7 81 char *cp;
19c50364
AD
82 cp = stpcpy (res, string1);
83 cp = stpcpy (cp, string2);
358c15b7 84 return res;
54bd0db4
RS
85}
86
0b8afb77 87
270a173c
AD
88/*-----------------------------------------------------------------.
89| Computes the macro name used to avoid double inclusion in the |
90| header of the parser and store it in header_macro_name. Be sure |
91| to produce valid CPP names (don't start with digit, remain |
92| alphanumerical + underscore). |
93`-----------------------------------------------------------------*/
0b8afb77 94
93ede233 95char *
0b8afb77
AD
96compute_header_macro (void)
97{
270a173c 98 const char *prefix = "BISON_";
342b8b6e 99 char *macro_name, *cp;
0b8afb77 100
342b8b6e 101 if (spec_defines_file)
270a173c
AD
102 {
103 macro_name = XMALLOC (char,
104 strlen (prefix) +
105 strlen (spec_defines_file) + 1);
106 cp = stpcpy (macro_name, prefix);
107 cp = stpcpy (cp, spec_defines_file);
108 }
342b8b6e
AD
109 else
110 {
111 macro_name = XMALLOC (char,
270a173c 112 strlen (prefix) +
342b8b6e
AD
113 strlen (base_name) +
114 strlen (header_extension) + 1);
270a173c
AD
115 cp = stpcpy (macro_name, prefix);
116 cp = stpcpy (cp, base_name);
117 cp = stpcpy (cp, header_extension);
342b8b6e
AD
118 }
119
120 for (cp = macro_name; *cp; ++cp)
121 if (islower (*cp))
122 *cp = toupper (*cp);
123 else if (!isalnum (*cp))
124 *cp = '_';
0b8afb77 125
0b8afb77
AD
126 return macro_name;
127}
128
129
cfe5fbc0
AD
130/*-----------------------------------------------------------------.
131| Try to open file NAME with mode MODE, and print an error message |
132| if fails. |
133`-----------------------------------------------------------------*/
54bd0db4 134
ff61dabd 135FILE *
8963a27b 136xfopen (const char *name, const char *mode)
cfe5fbc0 137{
8963a27b 138 FILE *ptr;
cfe5fbc0
AD
139
140 ptr = fopen (name, mode);
141 if (!ptr)
142 error (2, errno, _("cannot open file `%s'"), name);
143
144 return ptr;
145}
146
147/*-------------------------------------------------------------.
148| Try to close file PTR, and print an error message if fails. |
149`-------------------------------------------------------------*/
150
ff61dabd 151int
8963a27b 152xfclose (FILE *ptr)
cfe5fbc0
AD
153{
154 int result;
155
156 if (ptr == NULL)
157 return 0;
158
159 result = fclose (ptr);
160 if (result == EOF)
161 error (2, errno, _("cannot close file"));
162
163 return result;
164}
d8880f69 165
d8880f69 166
ff61dabd
AD
167/*------------------------------------------------------------------.
168| Return the path to the skeleton which locaction might be given in |
b0ce6046 169| ENVVAR, otherwise return SKELETON_NAME. |
ff61dabd
AD
170`------------------------------------------------------------------*/
171
172const char *
b0ce6046 173skeleton_find (const char *envvar, const char *skeleton_name)
9311529b
AD
174{
175 const char *res = getenv (envvar);
176
342b8b6e
AD
177#if defined (MSDOS) || defined (_WIN32)
178 if (!res)
9311529b 179 {
342b8b6e 180 /* Skeleton file name without path */
5e147124 181 const char *skel_name = strrchr (skeleton_name, '/');
342b8b6e 182 if (!skel_name)
5e147124 183 skel_name = strrchr (skeleton_name, '\\');
342b8b6e
AD
184 if (!skel_name)
185 skel_name = skeleton_name;
186 else
187 ++skel_name;
188
189 /* File doesn't exist in current directory; try in INIT directory. */
190 const char *cp = getenv ("INIT");
191 if (cp)
192 {
193 res = XMALLOC (char, strlen (cp) + strlen (skel_name) + 2);
194 sprintf (res, "%s%c%s", cp, '\\', skel_name);
195 }
196 else if (access (skel_name, 4) == 0) /* Look in current dir. */
197 res = skel_name;
198 else
199 {
200 /* Look in program locations dir. */
201 extern char *program_name;
202 cp = strrchr(program_name, '\\');
203 if (!cp)
204 return skeleton_name;
205 else
206 ++cp;
207 res = XMALLOC (char, cp - program_name + strlen (skel_name) + 1);
208 strncpy (res, program_name, cp - program_name);
209 strcpy (res + (cp - program_name), skel_name);
210 }
9311529b 211 }
342b8b6e 212#endif /* defined (MSDOS) || defined (_WIN32) */
9311529b 213 if (!res)
b0ce6046 214 res = skeleton_name;
9311529b
AD
215
216 return res;
217}
cfe5fbc0 218\f
f0130d39 219
234a3be3
AD
220/*----------------------------------------------------------------.
221| Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
222`----------------------------------------------------------------*/
223
b0ce6046 224/* Replace all characters FROM by TO in the string IN.
f0130d39 225 and returns a new allocated string. */
234a3be3 226static char *
f0130d39 227tr (const char *in, char from, char to)
234a3be3
AD
228{
229 char *temp;
230 char *out;
b0ce6046 231
234a3be3
AD
232 out = XMALLOC (char, strlen (in) + 1);
233
234 for (temp = out; *in; in++, out++)
235 if (*in == from)
236 *out = to;
237 else
238 *out = *in;
239 *out = 0;
240 return (temp);
241}
242
f0130d39
AD
243/* Gets the extension index in FILENAME. Returns 0 if fails to
244 find an extension. */
b0ce6046 245static int
f0130d39 246get_extension_index (const char *filename)
234a3be3 247{
f0130d39 248 int len;
234a3be3
AD
249
250 len = strlen (filename);
b0ce6046 251
234a3be3
AD
252 if (filename[len-- - 1] == '.')
253 return (0);
254
255 while ((len > 0) && (filename[len - 1] != '.'))
256 if (filename[len - 1] == '/')
257 return (0);
258 else
259 len--;
260
261 return (len - 1);
262}
263
264/* Computes extensions from the grammar file extension. */
265static void
f0130d39 266compute_exts_from_gf (const char *ext)
234a3be3 267{
342b8b6e
AD
268 src_extension = tr (ext, 'y', 'c');
269 src_extension = tr (src_extension, 'Y', 'C');
270 header_extension = tr (ext, 'y', 'h');
271 header_extension = tr (header_extension, 'Y', 'H');
234a3be3
AD
272}
273
274/* Computes extensions from the given c source file extension. */
275static void
f0130d39 276compute_exts_from_src (const char *ext)
234a3be3 277{
4ecbf796
MA
278 /* We use this function when the user specifies `-o' or `--output',
279 so the extenions must be computed unconditionally from the file name
280 given by this option. */
f0130d39
AD
281 src_extension = xstrdup (ext);
282 header_extension = tr (ext, 'c', 'h');
283 header_extension = tr (header_extension, 'C', 'H');
234a3be3 284}
d8880f69 285
19c50364
AD
286/* FIXME: Should use xstrndup. */
287
288static void
27110317 289compute_base_names (void)
54bd0db4 290{
19c50364
AD
291 size_t base_length;
292 size_t short_base_length;
234a3be3 293 size_t ext_index;
b0ce6046 294
19c50364
AD
295 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
296 BASE_NAME and SHORT_BASE_NAME are `foo'.
54bd0db4 297
19c50364
AD
298 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
299 SHORT_BASE_NAME is `foo'.
300
301 The precise -o name will be used for FTABLE. For other output
302 files, remove the ".c" or ".tab.c" suffix. */
54bd0db4
RS
303 if (spec_outfile)
304 {
54bd0db4 305#ifdef MSDOS
19c50364 306 strlwr (spec_outfile);
54bd0db4
RS
307#endif /* MSDOS */
308 /* BASE_LENGTH includes ".tab" but not ".c". */
19c50364 309 base_length = strlen (spec_outfile);
b0ce6046 310
234a3be3 311 ext_index = get_extension_index (spec_outfile);
f0130d39
AD
312 /* If the initial segment of extension contains 'c' or a 'C', I assume
313 that it is a C or C++ source file. */
234a3be3 314 if (ext_index)
f0130d39
AD
315 ext_index =
316 (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
234a3be3
AD
317 if (ext_index)
318 {
319 base_length -= strlen (spec_outfile + ext_index);
95fb5662 320 compute_exts_from_src (spec_outfile + ext_index);
234a3be3 321 }
b0ce6046 322
956dba3a 323 base_name = strndup (spec_outfile, base_length);
54bd0db4
RS
324 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
325 short_base_length = base_length;
956dba3a 326 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
54bd0db4 327 short_base_length -= 4;
27110317 328 short_base_name = strndup (spec_outfile, short_base_length);
19c50364
AD
329
330 return;
54bd0db4 331 }
19c50364
AD
332
333 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
334 are `foo'.
335
336 Construct names from it. */
337 if (spec_file_prefix)
54bd0db4 338 {
54bd0db4 339#ifdef MSDOS
19c50364 340 strlwr (spec_file_prefix);
54bd0db4 341#endif /* MSDOS */
27110317 342 short_base_name = xstrdup (spec_file_prefix);
27110317
AD
343 base_name = XMALLOC (char,
344 strlen (short_base_name) + strlen (EXT_TAB) + 1);
345 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
19c50364 346
95fb5662
MA
347 /* Computes the extensions from the garmmar file name. */
348 ext_index = get_extension_index (infile);
f0130d39
AD
349 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
350 that it is a yacc or bison grammar file. */
95fb5662
MA
351 if (ext_index)
352 ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0;
353 if (ext_index)
354 compute_exts_from_gf (infile + ext_index);
f0130d39 355
19c50364 356 return;
54bd0db4 357 }
54bd0db4 358
19c50364
AD
359 /* If neither -o nor --file-prefix were specified, and the input
360 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
361 `foo'.
4a120d45 362
19c50364
AD
363 If --yacc is used, do as if the input file was `y.y'. */
364 {
365 const char *name_base = yacc_flag ? "y.y" : infile;
54bd0db4 366
19c50364 367 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
54bd0db4 368
19c50364 369 base_length = strlen (name_base);
b0ce6046 370
234a3be3 371 ext_index = get_extension_index (name_base);
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. */
234a3be3 374 if (ext_index)
95fb5662 375 ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0;
234a3be3
AD
376 if (ext_index)
377 {
378 base_length -= strlen (name_base + ext_index);
95fb5662 379 compute_exts_from_gf (name_base + ext_index);
234a3be3
AD
380 }
381
19c50364 382 short_base_length = base_length;
27110317 383 short_base_name = strndup (name_base, short_base_length);
54bd0db4 384
27110317
AD
385 base_name = XMALLOC (char,
386 strlen (short_base_name) + strlen (EXT_TAB) + 1);
387 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
19c50364
AD
388
389 return;
390 }
391}
392
342b8b6e
AD
393/*-------------------------------------------------------.
394| Close the open files, compute the output files names. |
395`-------------------------------------------------------*/
396
397void
398compute_output_file_names (void)
399{
400 compute_base_names ();
401
ea52d706
AD
402 parser_file_name =
403 spec_outfile ? spec_outfile : stringappend (base_name, src_extension);
404
342b8b6e
AD
405 /* If not yet done. */
406 if (!src_extension)
407 src_extension = ".c";
408 if (!header_extension)
409 header_extension = ".h";
410
411 /* It the defines filename if not given, we create it. */
412 if (!spec_defines_file)
413 spec_defines_file = stringappend (base_name, header_extension);
414
415 /* It the graph filename if not given, we create it. */
416 if (!spec_graph_file)
417 spec_graph_file = stringappend (short_base_name, ".vcg");
418
419 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
420
421 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
422#ifndef MSDOS
423 attrsfile = stringappend (attrsfile, header_extension);
424#endif /* MSDOS */
342b8b6e 425}