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