]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 /* From basename.c. Almost a lie, as it returns a char *. */
31 const char *base_name
PARAMS ((char const *name
));
32 /* From xstrndup.c. */
33 char *xstrndup
PARAMS ((const char *s
, size_t n
));
37 struct obstack action_obstack
;
38 struct obstack attrs_obstack
;
39 struct obstack table_obstack
;
40 struct obstack defines_obstack
;
41 struct obstack guard_obstack
;
42 struct obstack output_obstack
;
44 char *spec_outfile
= NULL
; /* for -o. */
45 char *spec_file_prefix
= NULL
; /* for -b. */
46 char *spec_name_prefix
= NULL
; /* for -p. */
47 char *spec_verbose_file
= NULL
; /* for --verbose. */
48 char *spec_graph_file
= NULL
; /* for -g. */
49 char *spec_defines_file
= NULL
; /* for --defines. */
52 char *attrsfile
= NULL
;
54 static char *full_base_name
= NULL
;
55 static char *short_base_name
= NULL
;
57 /* C source file extension (the parser source). */
58 const char *src_extension
= NULL
;
59 /* Header file extension (if option ``-d'' is specified). */
60 const char *header_extension
= NULL
;
63 /*--------------------------.
64 | Is SUFFIX ending STRING? |
65 `--------------------------*/
68 strsuffix (const char *string
, const char *suffix
)
70 size_t string_len
= strlen (string
);
71 size_t suffix_len
= strlen (suffix
);
72 if (suffix_len
<= string_len
)
73 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
79 /*-----------------------------------------------------------------.
80 | Return a newly allocated string composed of the concatenation of |
81 | STRING1, and STRING2. |
82 `-----------------------------------------------------------------*/
85 stringappend (const char *string1
, const char *string2
)
87 size_t len
= strlen (string1
) + strlen (string2
);
88 char *res
= XMALLOC (char, len
+ 1);
90 cp
= stpcpy (res
, string1
);
91 cp
= stpcpy (cp
, string2
);
96 /*-----------------------------------------------------------------.
97 | Computes the macro name used to avoid double inclusion in the |
98 | header of the parser and store it in header_macro_name. Be sure |
99 | to produce valid CPP names (don't start with digit, remain |
100 | alphanumerical + underscore). |
101 `-----------------------------------------------------------------*/
104 compute_header_macro (void)
106 const char *prefix
= "BISON_";
107 char *macro_name
, *cp
;
109 if (spec_defines_file
)
111 macro_name
= XMALLOC (char,
113 strlen (spec_defines_file
) + 1);
114 cp
= stpcpy (macro_name
, prefix
);
115 cp
= stpcpy (cp
, spec_defines_file
);
119 macro_name
= XMALLOC (char,
121 strlen (full_base_name
) +
122 strlen (header_extension
) + 1);
123 cp
= stpcpy (macro_name
, prefix
);
124 cp
= stpcpy (cp
, full_base_name
);
125 cp
= stpcpy (cp
, header_extension
);
128 for (cp
= macro_name
; *cp
; ++cp
)
131 else if (!isalnum (*cp
))
138 /*-----------------------------------------------------------------.
139 | Try to open file NAME with mode MODE, and print an error message |
141 `-----------------------------------------------------------------*/
144 xfopen (const char *name
, const char *mode
)
148 ptr
= fopen (name
, mode
);
150 error (2, errno
, _("cannot open file `%s'"), name
);
155 /*-------------------------------------------------------------.
156 | Try to close file PTR, and print an error message if fails. |
157 `-------------------------------------------------------------*/
167 result
= fclose (ptr
);
169 error (2, errno
, _("cannot close file"));
174 /*--------------------------------------------------.
175 | Save the content of the obstack OBS in FILENAME. |
176 `--------------------------------------------------*/
179 obstack_save (struct obstack
*obs
, const char *filename
)
181 FILE *out
= xfopen (filename
, "w");
182 size_t size
= obstack_object_size (obs
);
183 fwrite (obstack_finish (obs
), 1, size
, out
);
187 /*---------------------------------------------------------------------.
188 | Output double inclusion protection macros and saves defines_obstack |
189 `---------------------------------------------------------------------*/
192 defines_obstack_save (const char *filename
)
194 FILE *out
= xfopen (filename
, "w");
195 size_t size
= obstack_object_size (&defines_obstack
);
196 char *macro_name
= compute_header_macro ();
198 fprintf (out
, "#ifndef %s\n", macro_name
);
199 fprintf (out
, "# define %s\n\n", macro_name
);
200 fwrite (obstack_finish (&defines_obstack
), 1, size
, out
);
201 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
207 /*------------------------------------------------------------------.
208 | Return the path to the skeleton which locaction might be given in |
209 | ENVVAR, otherwise return SKELETON_NAME. |
210 `------------------------------------------------------------------*/
213 skeleton_find (const char *envvar
, const char *skeleton_name
)
215 const char *res
= getenv (envvar
);
217 #if defined (MSDOS) || defined (_WIN32)
218 const char *cp
= getenv ("INIT");
221 /* Skeleton file name without path */
222 const char *skel_name
= strrchr (skeleton_name
, '/');
224 skel_name
= strrchr (skeleton_name
, '\\');
226 skel_name
= skeleton_name
;
230 /* File doesn't exist in current directory; try in INIT directory. */
233 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
234 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
236 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
240 /* Look in program locations dir. */
241 extern char *program_name
;
242 cp
= strrchr(program_name
, '\\');
244 return skeleton_name
;
247 res
= XMALLOC (char, cp
- program_name
+ strlen (skel_name
) + 1);
248 strncpy (res
, program_name
, cp
- program_name
);
249 strcpy (res
+ (cp
- program_name
), skel_name
);
252 #endif /* defined (MSDOS) || defined (_WIN32) */
260 /*----------------------------------------------------------------.
261 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
262 `----------------------------------------------------------------*/
264 /* Replace all characters FROM by TO in the string IN.
265 and returns a new allocated string. */
267 tr (const char *in
, char from
, char to
)
272 out
= XMALLOC (char, strlen (in
) + 1);
274 for (temp
= out
; *in
; in
++, out
++)
283 /* Computes extensions from the grammar file extension. */
285 compute_exts_from_gf (const char *ext
)
287 src_extension
= tr (ext
, 'y', 'c');
288 src_extension
= tr (src_extension
, 'Y', 'C');
289 header_extension
= tr (ext
, 'y', 'h');
290 header_extension
= tr (header_extension
, 'Y', 'H');
293 /* Computes extensions from the given c source file extension. */
295 compute_exts_from_src (const char *ext
)
297 /* We use this function when the user specifies `-o' or `--output',
298 so the extenions must be computed unconditionally from the file name
299 given by this option. */
300 src_extension
= xstrdup (ext
);
301 header_extension
= tr (ext
, 'c', 'h');
302 header_extension
= tr (header_extension
, 'C', 'H');
306 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
307 part, (the directory) is ranging from FILENAME to the char before
308 *BASE, so we don't need an additional parameter.
310 *EXT points to the last period in the basename, or NULL if none.
312 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
313 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
314 cannot be equal to *BASE.
316 None are allocated, they are simply pointers to parts of FILENAME.
319 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
322 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
324 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
326 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
328 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
330 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
332 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
335 filename_split (const char *filename
,
336 const char **base
, const char **tab
, const char **ext
)
338 *base
= base_name (filename
);
340 /* Look for the extension, i.e., look for the last dot. */
341 *ext
= strrchr (*base
, '.');
344 /* If there is an exentension, check if there is a `.tab' part right
347 && (*ext
- *base
) > (int) strlen (".tab")
348 && (!strncmp (*ext
- strlen (".tab"), ".tab", strlen (".tab"))
349 || !strncmp (*ext
- strlen ("_tab"), "_tab", strlen ("_tab"))))
350 *tab
= *ext
- strlen (".tab");
354 /* FIXME: Should use xstrndup. */
357 compute_base_names (void)
359 const char *base
, *tab
, *ext
;
361 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
362 BASE_NAME and SHORT_BASE_NAME are `foo'.
364 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
365 SHORT_BASE_NAME is `foo'.
367 The precise -o name will be used for FTABLE. For other output
368 files, remove the ".c" or ".tab.c" suffix. */
371 filename_split (spec_outfile
, &base
, &tab
, &ext
);
373 /* The full base name goes up the EXT, excluding it. */
375 xstrndup (spec_outfile
,
376 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
377 /* The short base name goes up to TAB, excluding it. */
379 xstrndup (spec_outfile
,
380 (strlen (spec_outfile
)
381 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
384 compute_exts_from_src (ext
);
387 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
388 and SHORT_BASE_NAME = `foo'.
390 Construct names from it. */
393 if (spec_file_prefix
)
395 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
397 short_base_name
= xstrdup (spec_file_prefix
);
401 /* If --yacc, then the output is `y.tab.c'. */
402 short_base_name
= xstrdup ("y");
406 /* Otherwise, the short base name is computed from the input
407 grammar: `foo.yy' => `foo'. */
408 filename_split (infile
, &base
, &tab
, &ext
);
411 (strlen (infile
) - (ext
? strlen (ext
) : 0)));
414 /* In these cases, always append `.tab'. */
415 full_base_name
= XMALLOC (char,
416 strlen (short_base_name
)
417 + strlen (EXT_TAB
) + 1);
418 stpcpy (stpcpy (full_base_name
, short_base_name
), EXT_TAB
);
420 /* Computes the extensions from the grammar file name. */
421 filename_split (infile
, &base
, &tab
, &ext
);
423 if (ext
&& !yacc_flag
)
424 compute_exts_from_gf (ext
);
429 /*-------------------------------------------------------.
430 | Close the open files, compute the output files names. |
431 `-------------------------------------------------------*/
434 compute_output_file_names (void)
436 compute_base_names ();
438 /* If not yet done. */
440 src_extension
= ".c";
441 if (!header_extension
)
442 header_extension
= ".h";
444 /* It the defines filename if not given, we create it. */
445 if (!spec_defines_file
)
446 spec_defines_file
= stringappend (full_base_name
, header_extension
);
448 /* It the graph filename if not given, we create it. */
449 if (!spec_graph_file
)
450 spec_graph_file
= stringappend (short_base_name
, ".vcg");
452 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
454 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
456 attrsfile
= stringappend (attrsfile
, header_extension
);
461 /*-----------------------------------------------------------------.
462 | Open the input file. Look for the skeletons. Find the names of |
463 | the output files. Prepare the obstacks. |
464 `-----------------------------------------------------------------*/
469 finput
= xfopen (infile
, "r");
471 /* Initialize the obstacks. */
472 obstack_init (&action_obstack
);
473 obstack_init (&attrs_obstack
);
474 obstack_init (&table_obstack
);
475 obstack_init (&defines_obstack
);
476 obstack_init (&guard_obstack
);
477 obstack_init (&output_obstack
);
482 /*-----------------------.
483 | Close the open file.. |
484 `-----------------------*/
492 /*---------------------------.
493 | Produce the output files. |
494 `---------------------------*/
499 /* Output the main file. */
501 obstack_save (&table_obstack
, spec_outfile
);
503 obstack_save (&table_obstack
,
504 stringappend (full_base_name
, src_extension
));
505 obstack_free (&table_obstack
, NULL
);
507 /* Output the header file if wanted. */
509 defines_obstack_save (spec_defines_file
);
510 obstack_free (&defines_obstack
, NULL
);
512 /* If we output only the table, dump the actions in ACTFILE. */
514 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
515 obstack_free (&action_obstack
, NULL
);
517 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
518 into its own file, ATTTRSFILE. */
523 obstack_save (&attrs_obstack
, attrsfile
);
524 obstack_free (&attrs_obstack
, NULL
);
525 temp_name
= stringappend (short_base_name
, EXT_GUARD_C
);
527 temp_name
= stringappend (temp_name
, src_extension
);
529 obstack_save (&guard_obstack
, temp_name
);
530 obstack_free (&guard_obstack
, NULL
);