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