]> git.saurik.com Git - bison.git/blame - src/files.c
* src/getargs.c, src/getarg.h (skeleton)): Constify.
[bison.git] / src / files.c
CommitLineData
54bd0db4 1/* Open and close files for bison,
8c7ebe49 2 Copyright 1984, 1986, 1989, 1992, 2000 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"
d7913476 25#include "xalloc.h"
54bd0db4 26#include "gram.h"
a0f6b076 27#include "complain.h"
54bd0db4
RS
28
29FILE *finput = NULL;
54bd0db4 30
8c7ebe49 31struct obstack action_obstack;
dd60faec 32struct obstack attrs_obstack;
896fe5c1
AD
33struct obstack table_obstack;
34struct obstack defines_obstack;
ea5607fd 35struct obstack guard_obstack;
ff4423cc 36struct obstack output_obstack;
8c7ebe49 37
b0ce6046
AD
38char *spec_outfile = NULL; /* for -o. */
39char *spec_file_prefix = NULL; /* for -b. */
40char *spec_name_prefix = NULL; /* for -p. */
54bd0db4 41
b0ce6046
AD
42char *infile = NULL;
43char *attrsfile = NULL;
4a120d45 44
b0ce6046
AD
45static char *base_name = NULL;
46static char *short_base_name = NULL;
27110317 47
234a3be3 48/* C source file extension (the parser source). */
b0ce6046 49static const char *src_extension = NULL;
234a3be3 50/* Header file extension (if option ``-d'' is specified). */
b0ce6046 51static const char *header_extension = NULL;
234a3be3 52
54bd0db4 53\f
19c50364
AD
54/*--------------------------.
55| Is SUFFIX ending STRING? |
56`--------------------------*/
57
58static int
59strsuffix (const char *string, const char *suffix)
60{
61 size_t string_len = strlen (string);
62 size_t suffix_len = strlen (suffix);
63 if (suffix_len <= string_len)
64 return !strcmp (string + string_len - suffix_len, suffix);
65 else
66 return 0;
67}
68
69
358c15b7
AD
70/*-----------------------------------------------------------------.
71| Return a newly allocated string composed of the concatenation of |
19c50364 72| STRING1, and STRING2. |
358c15b7 73`-----------------------------------------------------------------*/
54bd0db4 74
8963a27b 75static char *
19c50364 76stringappend (const char *string1, const char *string2)
54bd0db4 77{
19c50364
AD
78 size_t len = strlen (string1) + strlen (string2);
79 char *res = XMALLOC (char, len + 1);
358c15b7 80 char *cp;
19c50364
AD
81 cp = stpcpy (res, string1);
82 cp = stpcpy (cp, string2);
358c15b7 83 return res;
54bd0db4
RS
84}
85
cfe5fbc0
AD
86/*-----------------------------------------------------------------.
87| Try to open file NAME with mode MODE, and print an error message |
88| if fails. |
89`-----------------------------------------------------------------*/
54bd0db4 90
ff61dabd 91FILE *
8963a27b 92xfopen (const char *name, const char *mode)
cfe5fbc0 93{
8963a27b 94 FILE *ptr;
cfe5fbc0
AD
95
96 ptr = fopen (name, mode);
97 if (!ptr)
98 error (2, errno, _("cannot open file `%s'"), name);
99
100 return ptr;
101}
102
103/*-------------------------------------------------------------.
104| Try to close file PTR, and print an error message if fails. |
105`-------------------------------------------------------------*/
106
ff61dabd 107int
8963a27b 108xfclose (FILE *ptr)
cfe5fbc0
AD
109{
110 int result;
111
112 if (ptr == NULL)
113 return 0;
114
115 result = fclose (ptr);
116 if (result == EOF)
117 error (2, errno, _("cannot close file"));
118
119 return result;
120}
d8880f69
AD
121
122/*--------------------------------------------------.
123| Save the content of the obstack OBS in FILENAME. |
124`--------------------------------------------------*/
125
9311529b 126static void
d8880f69
AD
127obstack_save (struct obstack *obs, const char *filename)
128{
129 FILE *out = xfopen (filename, "w");
130 size_t size = obstack_object_size (obs);
131 fwrite (obstack_finish (obs), 1, size, out);
132 xfclose (out);
133}
134
9311529b 135
ff61dabd
AD
136/*------------------------------------------------------------------.
137| Return the path to the skeleton which locaction might be given in |
b0ce6046 138| ENVVAR, otherwise return SKELETON_NAME. |
ff61dabd
AD
139`------------------------------------------------------------------*/
140
141const char *
b0ce6046 142skeleton_find (const char *envvar, const char *skeleton_name)
9311529b
AD
143{
144 const char *res = getenv (envvar);
145
146#ifdef MSDOS
147 const char *cp;
148
149 /* File doesn't exist in current directory; try in INIT directory. */
150 if (!res && (cp = getenv ("INIT")))
151 {
b0ce6046
AD
152 res = XMALLOC (char, strlen (cp) + strlen (skeleton_name) + 2);
153 sprintf (res, "%s%c%s", cp, '/', skeleton_name);
9311529b
AD
154 }
155#endif /* !MSDOS */
156
157 if (!res)
b0ce6046 158 res = skeleton_name;
9311529b
AD
159
160 return res;
161}
162
cfe5fbc0 163\f
234a3be3
AD
164/*----------------------------------------------------------------.
165| Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
166`----------------------------------------------------------------*/
167
b0ce6046
AD
168/* Replace all characters FROM by TO in the string IN.
169 and returns a new allocated string. */
234a3be3
AD
170static char *
171tr(const char *in, char from, char to)
172{
173 char *temp;
174 char *out;
b0ce6046 175
234a3be3
AD
176 out = XMALLOC (char, strlen (in) + 1);
177
178 for (temp = out; *in; in++, out++)
179 if (*in == from)
180 *out = to;
181 else
182 *out = *in;
183 *out = 0;
184 return (temp);
185}
186
b0ce6046 187/* Gets the extension index in FILENAME. Returns 0 if fails to
234a3be3 188 find an extension. */
b0ce6046 189static int
234a3be3
AD
190get_extension_index(const char *filename)
191{
192 int len;
193
194 len = strlen (filename);
b0ce6046 195
234a3be3
AD
196 if (filename[len-- - 1] == '.')
197 return (0);
198
199 while ((len > 0) && (filename[len - 1] != '.'))
200 if (filename[len - 1] == '/')
201 return (0);
202 else
203 len--;
204
205 return (len - 1);
206}
207
208/* Computes extensions from the grammar file extension. */
209static void
210compute_exts_from_gf(const char *ext)
211{
212 src_extension = tr(ext, 'y', 'c');
213 src_extension = tr(src_extension, 'Y', 'C');
214 header_extension = tr(ext, 'y', 'h');
215 header_extension = tr(header_extension, 'Y', 'H');
216}
217
218/* Computes extensions from the given c source file extension. */
219static void
220compute_exts_from_src(const char *ext)
221{
222 src_extension = xstrdup(ext);
223 header_extension = tr(ext, 'c', 'h');
224 header_extension = tr(header_extension, 'C', 'H');
225}
d8880f69 226
19c50364
AD
227/* FIXME: Should use xstrndup. */
228
229static void
27110317 230compute_base_names (void)
54bd0db4 231{
19c50364
AD
232 size_t base_length;
233 size_t short_base_length;
234a3be3 234 size_t ext_index;
b0ce6046 235
234a3be3
AD
236 /* Set default extensions */
237 src_extension = ".c";
238 header_extension = ".h";
19c50364
AD
239
240 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
241 BASE_NAME and SHORT_BASE_NAME are `foo'.
54bd0db4 242
19c50364
AD
243 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
244 SHORT_BASE_NAME is `foo'.
245
246 The precise -o name will be used for FTABLE. For other output
247 files, remove the ".c" or ".tab.c" suffix. */
54bd0db4
RS
248 if (spec_outfile)
249 {
54bd0db4 250#ifdef MSDOS
19c50364 251 strlwr (spec_outfile);
54bd0db4
RS
252#endif /* MSDOS */
253 /* BASE_LENGTH includes ".tab" but not ".c". */
19c50364 254 base_length = strlen (spec_outfile);
b0ce6046 255
234a3be3 256 ext_index = get_extension_index (spec_outfile);
b0ce6046 257 /* if the initial segment of extension contains 'c' or a 'C', I assume
234a3be3
AD
258 that it is a C or C++ source file */
259 if (ext_index)
260 ext_index = (strspn(spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
261 if (ext_index)
262 {
263 base_length -= strlen (spec_outfile + ext_index);
264 compute_exts_from_src(spec_outfile + ext_index);
265 }
b0ce6046 266
956dba3a 267 base_name = strndup (spec_outfile, base_length);
54bd0db4
RS
268 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
269 short_base_length = base_length;
956dba3a 270 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
54bd0db4 271 short_base_length -= 4;
27110317 272 short_base_name = strndup (spec_outfile, short_base_length);
19c50364
AD
273
274 return;
54bd0db4 275 }
19c50364
AD
276
277 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
278 are `foo'.
279
280 Construct names from it. */
281 if (spec_file_prefix)
54bd0db4 282 {
54bd0db4 283#ifdef MSDOS
19c50364 284 strlwr (spec_file_prefix);
54bd0db4 285#endif /* MSDOS */
27110317 286 short_base_name = xstrdup (spec_file_prefix);
27110317
AD
287 base_name = XMALLOC (char,
288 strlen (short_base_name) + strlen (EXT_TAB) + 1);
289 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
19c50364
AD
290
291 return;
54bd0db4 292 }
54bd0db4 293
19c50364
AD
294 /* If neither -o nor --file-prefix were specified, and the input
295 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
296 `foo'.
4a120d45 297
19c50364
AD
298 If --yacc is used, do as if the input file was `y.y'. */
299 {
300 const char *name_base = yacc_flag ? "y.y" : infile;
54bd0db4 301
19c50364 302 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
54bd0db4 303
19c50364 304 base_length = strlen (name_base);
b0ce6046 305
234a3be3 306 ext_index = get_extension_index (name_base);
b0ce6046 307 /* if the initial segment of extension contains a 'y' or a 'Y', I assume
234a3be3
AD
308 that it is a yacc or bison grammar file */
309 if (ext_index)
310 ext_index = (strspn(name_base + ext_index + 1, "yY")) ? ext_index : 0;
311 if (ext_index)
312 {
313 base_length -= strlen (name_base + ext_index);
314 compute_exts_from_gf(name_base + ext_index);
315 }
316
19c50364 317 short_base_length = base_length;
27110317 318 short_base_name = strndup (name_base, short_base_length);
54bd0db4 319
27110317
AD
320 base_name = XMALLOC (char,
321 strlen (short_base_name) + strlen (EXT_TAB) + 1);
322 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
19c50364
AD
323
324 return;
325 }
326}
327
328/*-----------------------------------------------------------------.
329| Open the input file. Look for the skeletons. Find the names of |
330| the output files. Prepare the obstacks. |
331`-----------------------------------------------------------------*/
332
333void
334open_files (void)
335{
8963a27b 336 finput = xfopen (infile, "r");
54bd0db4 337
dd60faec 338 /* Initialize the obstacks. */
8c7ebe49 339 obstack_init (&action_obstack);
dd60faec 340 obstack_init (&attrs_obstack);
896fe5c1
AD
341 obstack_init (&table_obstack);
342 obstack_init (&defines_obstack);
ea5607fd 343 obstack_init (&guard_obstack);
ff4423cc 344 obstack_init (&output_obstack);
54bd0db4
RS
345}
346
347
348
d8880f69
AD
349/*-----------------------------------------------------.
350| Close the open files, produce all the output files. |
351`-----------------------------------------------------*/
352
a693bf18 353void
d8880f69 354output_files (void)
a693bf18 355{
8963a27b 356 xfclose (finput);
54bd0db4 357
6deb4447 358 compute_base_names ();
234a3be3 359
6deb4447 360 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
234a3be3
AD
361#ifndef MSDOS
362 stringappend (attrsfile, header_extension);
b0ce6046 363#endif /* MSDOS */
6deb4447 364
d8880f69 365 /* Output the main file. */
27110317
AD
366 if (spec_outfile)
367 obstack_save (&table_obstack, spec_outfile);
368 else
234a3be3 369 obstack_save (&table_obstack, stringappend (base_name, src_extension));
d8880f69
AD
370
371 /* Output the header file if wanted. */
372 if (defines_flag)
234a3be3 373 obstack_save (&defines_obstack, stringappend (base_name, header_extension));
54bd0db4 374
27110317 375 /* If we output only the table, dump the actions in ACTFILE. */
8c7ebe49 376 if (no_parser_flag)
27110317 377 obstack_save (&action_obstack, stringappend (short_base_name, ".act"));
dd60faec
AD
378
379 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
380 into its own file, ATTTRSFILE. */
381 if (semantic_parser)
ea5607fd 382 {
234a3be3
AD
383 char *temp_name;
384
ea5607fd 385 obstack_save (&attrs_obstack, attrsfile);
234a3be3
AD
386 temp_name = stringappend (short_base_name, EXT_GUARD_C);
387#ifndef MSDOS
388 temp_name = stringappend (temp_name, src_extension);
389#endif /* MSDOS */
390 obstack_save (&guard_obstack, temp_name);
ea5607fd 391 }
ff4423cc
AD
392
393 if (verbose_flag)
394 /* We used to use just .out if spec_name_prefix (-p) was used, but
395 that conflicts with Posix. */
396 obstack_save (&output_obstack, stringappend (short_base_name, EXT_OUTPUT));
54bd0db4 397}