]>
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
));
35 struct obstack action_obstack
;
36 struct obstack attrs_obstack
;
37 struct obstack output_obstack
;
39 /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is
40 tempting, but don't do that: for the time being our handling of the
41 %directive vs --option leaves precedence to the options by deciding
42 that if a %directive sets a variable which is really set (i.e., not
43 NULL), then the %directive is ignored. As a result, %name-prefix,
44 for instance, will not be honored. */
46 char *spec_outfile
= NULL
; /* for -o. */
47 char *spec_file_prefix
= NULL
; /* for -b. */
48 const char *spec_name_prefix
= NULL
; /* for -p. */
49 char *spec_verbose_file
= NULL
; /* for --verbose. */
50 char *spec_graph_file
= NULL
; /* for -g. */
51 char *spec_defines_file
= NULL
; /* for --defines. */
52 char *parser_file_name
= NULL
;
55 char *attrsfile
= NULL
;
57 static char *full_base_name
= NULL
;
59 /* Prefix used to generate output file names. */
60 char *short_base_name
= NULL
;
62 /* Infix used to generate output file names (i.e., `.tab', or `_tab',
64 char *output_infix
= NULL
;
66 /* C source file extension (the parser source). */
67 const char *src_extension
= NULL
;
68 /* Header file extension (if option ``-d'' is specified). */
69 const char *header_extension
= NULL
;
72 /*--------------------------.
73 | Is SUFFIX ending STRING? |
74 `--------------------------*/
77 strsuffix (const char *string
, const char *suffix
)
79 size_t string_len
= strlen (string
);
80 size_t suffix_len
= strlen (suffix
);
81 if (suffix_len
<= string_len
)
82 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
88 /*-----------------------------------------------------------------.
89 | Return a newly allocated string composed of the concatenation of |
90 | STRING1, and STRING2. |
91 `-----------------------------------------------------------------*/
94 stringappend (const char *string1
, const char *string2
)
96 size_t len
= strlen (string1
) + strlen (string2
);
97 char *res
= XMALLOC (char, len
+ 1);
99 cp
= stpcpy (res
, string1
);
100 cp
= stpcpy (cp
, string2
);
105 /*-----------------------------------------------------------------.
106 | Computes the macro name used to avoid double inclusion in the |
107 | header of the parser and store it in header_macro_name. Be sure |
108 | to produce valid CPP names (don't start with digit, remain |
109 | alphanumerical + underscore). |
110 `-----------------------------------------------------------------*/
113 compute_header_macro (void)
115 const char *prefix
= "BISON_";
116 char *macro_name
, *cp
;
118 if (spec_defines_file
)
120 macro_name
= XMALLOC (char,
122 strlen (spec_defines_file
) + 1);
123 cp
= stpcpy (macro_name
, prefix
);
124 cp
= stpcpy (cp
, spec_defines_file
);
128 macro_name
= XMALLOC (char,
130 strlen (full_base_name
) +
131 strlen (header_extension
) + 1);
132 cp
= stpcpy (macro_name
, prefix
);
133 cp
= stpcpy (cp
, full_base_name
);
134 cp
= stpcpy (cp
, header_extension
);
137 for (cp
= macro_name
; *cp
; ++cp
)
140 else if (!isalnum (*cp
))
147 /*-----------------------------------------------------------------.
148 | Try to open file NAME with mode MODE, and print an error message |
150 `-----------------------------------------------------------------*/
153 xfopen (const char *name
, const char *mode
)
157 ptr
= fopen (name
, mode
);
159 error (2, errno
, _("cannot open file `%s'"), name
);
164 /*-------------------------------------------------------------.
165 | Try to close file PTR, and print an error message if fails. |
166 `-------------------------------------------------------------*/
176 result
= fclose (ptr
);
178 error (2, errno
, _("cannot close file"));
184 /*------------------------------------------------------------------.
185 | Return the path to the skeleton which locaction might be given in |
186 | ENVVAR, otherwise return SKELETON_NAME. |
187 `------------------------------------------------------------------*/
190 skeleton_find (const char *envvar
, const char *skeleton_name
)
192 const char *res
= getenv (envvar
);
194 #if defined (MSDOS) || defined (_WIN32)
197 /* Skeleton file name without path */
198 const char *skel_name
= strrchr (skeleton_name
, '/');
200 skel_name
= strrchr (skeleton_name
, '\\');
202 skel_name
= skeleton_name
;
206 /* File doesn't exist in current directory; try in INIT directory. */
207 const char *cp
= getenv ("INIT");
210 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
211 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
213 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
217 /* Look in program locations dir. */
218 extern char *program_name
;
219 cp
= strrchr(program_name
, '\\');
221 return skeleton_name
;
224 res
= XMALLOC (char, cp
- program_name
+ strlen (skel_name
) + 1);
225 strncpy (res
, program_name
, cp
- program_name
);
226 strcpy (res
+ (cp
- program_name
), skel_name
);
229 #endif /* defined (MSDOS) || defined (_WIN32) */
237 /*----------------------------------------------------------------.
238 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
239 `----------------------------------------------------------------*/
241 /* Replace all characters FROM by TO in the string IN.
242 and returns a new allocated string. */
244 tr (const char *in
, char from
, char to
)
249 out
= XMALLOC (char, strlen (in
) + 1);
251 for (temp
= out
; *in
; in
++, out
++)
260 /* Computes extensions from the grammar file extension. */
262 compute_exts_from_gf (const char *ext
)
264 src_extension
= tr (ext
, 'y', 'c');
265 src_extension
= tr (src_extension
, 'Y', 'C');
266 header_extension
= tr (ext
, 'y', 'h');
267 header_extension
= tr (header_extension
, 'Y', 'H');
270 /* Computes extensions from the given c source file extension. */
272 compute_exts_from_src (const char *ext
)
274 /* We use this function when the user specifies `-o' or `--output',
275 so the extenions must be computed unconditionally from the file name
276 given by this option. */
277 src_extension
= xstrdup (ext
);
278 header_extension
= tr (ext
, 'c', 'h');
279 header_extension
= tr (header_extension
, 'C', 'H');
283 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
284 part, (the directory) is ranging from FILENAME to the char before
285 *BASE, so we don't need an additional parameter.
287 *EXT points to the last period in the basename, or NULL if none.
289 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
290 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
291 cannot be equal to *BASE.
293 None are allocated, they are simply pointers to parts of FILENAME.
296 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
299 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
301 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
303 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
305 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
307 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
309 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
312 filename_split (const char *filename
,
313 const char **base
, const char **tab
, const char **ext
)
315 *base
= base_name (filename
);
317 /* Look for the extension, i.e., look for the last dot. */
318 *ext
= strrchr (*base
, '.');
321 /* If there is an exentension, check if there is a `.tab' part right
324 && (*ext
- *base
) > (int) strlen (".tab")
325 && (!strncmp (*ext
- strlen (".tab"), ".tab", strlen (".tab"))
326 || !strncmp (*ext
- strlen ("_tab"), "_tab", strlen ("_tab"))))
327 *tab
= *ext
- strlen (".tab");
331 /* FIXME: Should use xstrndup. */
334 compute_base_names (void)
336 const char *base
, *tab
, *ext
;
338 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
339 BASE_NAME and SHORT_BASE_NAME are `foo'.
341 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
342 SHORT_BASE_NAME is `foo'.
344 The precise -o name will be used for FTABLE. For other output
345 files, remove the ".c" or ".tab.c" suffix. */
348 filename_split (spec_outfile
, &base
, &tab
, &ext
);
350 /* The full base name goes up the EXT, excluding it. */
352 xstrndup (spec_outfile
,
353 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
355 /* The short base name goes up to TAB, excluding it. */
357 xstrndup (spec_outfile
,
358 (strlen (spec_outfile
)
359 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
362 output_infix
= xstrndup (tab
,
363 (strlen (tab
) - (ext
? strlen (ext
) : 0)));
366 compute_exts_from_src (ext
);
369 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
370 and SHORT_BASE_NAME = `foo'.
372 Construct names from it. */
375 if (spec_file_prefix
)
377 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
379 short_base_name
= xstrdup (spec_file_prefix
);
383 /* If --yacc, then the output is `y.tab.c'. */
384 short_base_name
= xstrdup ("y");
388 /* Otherwise, the short base name is computed from the input
389 grammar: `foo.yy' => `foo'. */
390 filename_split (infile
, &base
, &tab
, &ext
);
393 (strlen (infile
) - (ext
? strlen (ext
) : 0)));
396 /* In these cases, always append `.tab'. */
397 output_infix
= xstrdup (EXT_TAB
);
399 full_base_name
= XMALLOC (char,
400 strlen (short_base_name
)
401 + strlen (EXT_TAB
) + 1);
402 stpcpy (stpcpy (full_base_name
, short_base_name
), EXT_TAB
);
404 /* Computes the extensions from the grammar file name. */
405 filename_split (infile
, &base
, &tab
, &ext
);
406 if (ext
&& !yacc_flag
)
407 compute_exts_from_gf (ext
);
411 /*-------------------------------------------------------.
412 | Close the open files, compute the output files names. |
413 `-------------------------------------------------------*/
416 compute_output_file_names (void)
418 compute_base_names ();
421 spec_outfile
? spec_outfile
: stringappend (full_base_name
, src_extension
);
423 /* If not yet done. */
425 src_extension
= ".c";
426 if (!header_extension
)
427 header_extension
= ".h";
429 /* It the defines filename if not given, we create it. */
430 if (!spec_defines_file
)
431 spec_defines_file
= stringappend (full_base_name
, header_extension
);
433 /* It the graph filename if not given, we create it. */
434 if (!spec_graph_file
)
435 spec_graph_file
= stringappend (short_base_name
, ".vcg");
437 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
439 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);