]>
Commit | Line | Data |
---|---|---|
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 | |
29 | FILE *finput = NULL; | |
54bd0db4 | 30 | |
8c7ebe49 | 31 | struct obstack action_obstack; |
dd60faec | 32 | struct obstack attrs_obstack; |
896fe5c1 AD |
33 | struct obstack table_obstack; |
34 | struct obstack defines_obstack; | |
ea5607fd | 35 | struct obstack guard_obstack; |
ff4423cc | 36 | struct obstack output_obstack; |
8c7ebe49 | 37 | |
ea5607fd AD |
38 | char *spec_outfile; /* for -o. */ |
39 | char *spec_file_prefix; /* for -b. */ | |
40 | char *spec_name_prefix; /* for -p. */ | |
54bd0db4 RS |
41 | |
42 | char *infile; | |
54bd0db4 | 43 | char *attrsfile; |
4a120d45 | 44 | |
27110317 AD |
45 | static char *base_name; |
46 | static char *short_base_name; | |
47 | ||
54bd0db4 | 48 | \f |
19c50364 AD |
49 | /*--------------------------. |
50 | | Is SUFFIX ending STRING? | | |
51 | `--------------------------*/ | |
52 | ||
53 | static int | |
54 | strsuffix (const char *string, const char *suffix) | |
55 | { | |
56 | size_t string_len = strlen (string); | |
57 | size_t suffix_len = strlen (suffix); | |
58 | if (suffix_len <= string_len) | |
59 | return !strcmp (string + string_len - suffix_len, suffix); | |
60 | else | |
61 | return 0; | |
62 | } | |
63 | ||
64 | ||
358c15b7 AD |
65 | /*-----------------------------------------------------------------. |
66 | | Return a newly allocated string composed of the concatenation of | | |
19c50364 | 67 | | STRING1, and STRING2. | |
358c15b7 | 68 | `-----------------------------------------------------------------*/ |
54bd0db4 | 69 | |
8963a27b | 70 | static char * |
19c50364 | 71 | stringappend (const char *string1, const char *string2) |
54bd0db4 | 72 | { |
19c50364 AD |
73 | size_t len = strlen (string1) + strlen (string2); |
74 | char *res = XMALLOC (char, len + 1); | |
358c15b7 | 75 | char *cp; |
19c50364 AD |
76 | cp = stpcpy (res, string1); |
77 | cp = stpcpy (cp, string2); | |
358c15b7 | 78 | return res; |
54bd0db4 RS |
79 | } |
80 | ||
cfe5fbc0 AD |
81 | /*-----------------------------------------------------------------. |
82 | | Try to open file NAME with mode MODE, and print an error message | | |
83 | | if fails. | | |
84 | `-----------------------------------------------------------------*/ | |
54bd0db4 | 85 | |
ff61dabd | 86 | FILE * |
8963a27b | 87 | xfopen (const char *name, const char *mode) |
cfe5fbc0 | 88 | { |
8963a27b | 89 | FILE *ptr; |
cfe5fbc0 AD |
90 | |
91 | ptr = fopen (name, mode); | |
92 | if (!ptr) | |
93 | error (2, errno, _("cannot open file `%s'"), name); | |
94 | ||
95 | return ptr; | |
96 | } | |
97 | ||
98 | /*-------------------------------------------------------------. | |
99 | | Try to close file PTR, and print an error message if fails. | | |
100 | `-------------------------------------------------------------*/ | |
101 | ||
ff61dabd | 102 | int |
8963a27b | 103 | xfclose (FILE *ptr) |
cfe5fbc0 AD |
104 | { |
105 | int result; | |
106 | ||
107 | if (ptr == NULL) | |
108 | return 0; | |
109 | ||
110 | result = fclose (ptr); | |
111 | if (result == EOF) | |
112 | error (2, errno, _("cannot close file")); | |
113 | ||
114 | return result; | |
115 | } | |
d8880f69 AD |
116 | |
117 | /*--------------------------------------------------. | |
118 | | Save the content of the obstack OBS in FILENAME. | | |
119 | `--------------------------------------------------*/ | |
120 | ||
9311529b | 121 | static void |
d8880f69 AD |
122 | obstack_save (struct obstack *obs, const char *filename) |
123 | { | |
124 | FILE *out = xfopen (filename, "w"); | |
125 | size_t size = obstack_object_size (obs); | |
126 | fwrite (obstack_finish (obs), 1, size, out); | |
127 | xfclose (out); | |
128 | } | |
129 | ||
9311529b | 130 | |
ff61dabd AD |
131 | /*------------------------------------------------------------------. |
132 | | Return the path to the skeleton which locaction might be given in | | |
133 | | ENVVAR, otherwise return SKELETON. | | |
134 | `------------------------------------------------------------------*/ | |
135 | ||
136 | const char * | |
9311529b AD |
137 | skeleton_find (const char *envvar, const char *skeleton) |
138 | { | |
139 | const char *res = getenv (envvar); | |
140 | ||
141 | #ifdef MSDOS | |
142 | const char *cp; | |
143 | ||
144 | /* File doesn't exist in current directory; try in INIT directory. */ | |
145 | if (!res && (cp = getenv ("INIT"))) | |
146 | { | |
147 | res = XMALLOC (char, strlen (cp) + strlen (skeleton) + 2); | |
148 | sprintf (res, "%s%c%s", cp, '/', skeleton); | |
149 | } | |
150 | #endif /* !MSDOS */ | |
151 | ||
152 | if (!res) | |
153 | res = skeleton; | |
154 | ||
155 | return res; | |
156 | } | |
157 | ||
cfe5fbc0 | 158 | \f |
19c50364 AD |
159 | /*----------------------------------------. |
160 | | Compute BASE_NAME and SHORT_BASE_NAME. | | |
161 | `----------------------------------------*/ | |
d8880f69 | 162 | |
19c50364 AD |
163 | /* FIXME: Should use xstrndup. */ |
164 | ||
165 | static void | |
27110317 | 166 | compute_base_names (void) |
54bd0db4 | 167 | { |
19c50364 AD |
168 | size_t base_length; |
169 | size_t short_base_length; | |
170 | ||
171 | /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c), | |
172 | BASE_NAME and SHORT_BASE_NAME are `foo'. | |
54bd0db4 | 173 | |
19c50364 AD |
174 | If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and |
175 | SHORT_BASE_NAME is `foo'. | |
176 | ||
177 | The precise -o name will be used for FTABLE. For other output | |
178 | files, remove the ".c" or ".tab.c" suffix. */ | |
54bd0db4 RS |
179 | if (spec_outfile) |
180 | { | |
54bd0db4 | 181 | #ifdef MSDOS |
19c50364 | 182 | strlwr (spec_outfile); |
54bd0db4 RS |
183 | #endif /* MSDOS */ |
184 | /* BASE_LENGTH includes ".tab" but not ".c". */ | |
19c50364 AD |
185 | base_length = strlen (spec_outfile); |
186 | if (strsuffix (spec_outfile, ".c")) | |
54bd0db4 | 187 | base_length -= 2; |
956dba3a | 188 | base_name = strndup (spec_outfile, base_length); |
54bd0db4 RS |
189 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ |
190 | short_base_length = base_length; | |
956dba3a | 191 | if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab")) |
54bd0db4 | 192 | short_base_length -= 4; |
27110317 | 193 | short_base_name = strndup (spec_outfile, short_base_length); |
19c50364 AD |
194 | |
195 | return; | |
54bd0db4 | 196 | } |
19c50364 AD |
197 | |
198 | /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME | |
199 | are `foo'. | |
200 | ||
201 | Construct names from it. */ | |
202 | if (spec_file_prefix) | |
54bd0db4 | 203 | { |
54bd0db4 | 204 | #ifdef MSDOS |
19c50364 | 205 | strlwr (spec_file_prefix); |
54bd0db4 | 206 | #endif /* MSDOS */ |
27110317 | 207 | short_base_name = xstrdup (spec_file_prefix); |
19c50364 | 208 | |
27110317 AD |
209 | base_name = XMALLOC (char, |
210 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
211 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 AD |
212 | |
213 | return; | |
54bd0db4 | 214 | } |
54bd0db4 | 215 | |
19c50364 AD |
216 | /* If neither -o nor --file-prefix were specified, and the input |
217 | file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is | |
218 | `foo'. | |
4a120d45 | 219 | |
19c50364 AD |
220 | If --yacc is used, do as if the input file was `y.y'. */ |
221 | { | |
222 | const char *name_base = yacc_flag ? "y.y" : infile; | |
54bd0db4 | 223 | |
19c50364 | 224 | /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */ |
54bd0db4 | 225 | |
19c50364 AD |
226 | base_length = strlen (name_base); |
227 | if (strsuffix (name_base, ".y")) | |
228 | base_length -= 2; | |
229 | short_base_length = base_length; | |
27110317 | 230 | short_base_name = strndup (name_base, short_base_length); |
54bd0db4 | 231 | |
27110317 AD |
232 | base_name = XMALLOC (char, |
233 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
234 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 AD |
235 | |
236 | return; | |
237 | } | |
238 | } | |
239 | ||
240 | /*-----------------------------------------------------------------. | |
241 | | Open the input file. Look for the skeletons. Find the names of | | |
242 | | the output files. Prepare the obstacks. | | |
243 | `-----------------------------------------------------------------*/ | |
244 | ||
245 | void | |
246 | open_files (void) | |
247 | { | |
8963a27b | 248 | finput = xfopen (infile, "r"); |
54bd0db4 | 249 | |
dd60faec | 250 | /* Initialize the obstacks. */ |
8c7ebe49 | 251 | obstack_init (&action_obstack); |
dd60faec | 252 | obstack_init (&attrs_obstack); |
896fe5c1 AD |
253 | obstack_init (&table_obstack); |
254 | obstack_init (&defines_obstack); | |
ea5607fd | 255 | obstack_init (&guard_obstack); |
ff4423cc | 256 | obstack_init (&output_obstack); |
54bd0db4 RS |
257 | } |
258 | ||
259 | ||
260 | ||
d8880f69 AD |
261 | /*-----------------------------------------------------. |
262 | | Close the open files, produce all the output files. | | |
263 | `-----------------------------------------------------*/ | |
264 | ||
a693bf18 | 265 | void |
d8880f69 | 266 | output_files (void) |
a693bf18 | 267 | { |
8963a27b | 268 | xfclose (finput); |
54bd0db4 | 269 | |
6deb4447 AD |
270 | compute_base_names (); |
271 | attrsfile = stringappend (short_base_name, EXT_STYPE_H); | |
272 | ||
d8880f69 | 273 | /* Output the main file. */ |
27110317 AD |
274 | if (spec_outfile) |
275 | obstack_save (&table_obstack, spec_outfile); | |
276 | else | |
277 | obstack_save (&table_obstack, stringappend (base_name, ".c")); | |
d8880f69 AD |
278 | |
279 | /* Output the header file if wanted. */ | |
280 | if (defines_flag) | |
27110317 | 281 | obstack_save (&defines_obstack, stringappend (base_name, ".h")); |
54bd0db4 | 282 | |
27110317 | 283 | /* If we output only the table, dump the actions in ACTFILE. */ |
8c7ebe49 | 284 | if (no_parser_flag) |
27110317 | 285 | obstack_save (&action_obstack, stringappend (short_base_name, ".act")); |
dd60faec AD |
286 | |
287 | /* If we produced a semantic parser ATTRS_OBSTACK must be dumped | |
288 | into its own file, ATTTRSFILE. */ | |
289 | if (semantic_parser) | |
ea5607fd AD |
290 | { |
291 | obstack_save (&attrs_obstack, attrsfile); | |
292 | obstack_save (&guard_obstack, | |
293 | stringappend (short_base_name, EXT_GUARD_C)); | |
294 | } | |
ff4423cc AD |
295 | |
296 | if (verbose_flag) | |
297 | /* We used to use just .out if spec_name_prefix (-p) was used, but | |
298 | that conflicts with Posix. */ | |
299 | obstack_save (&output_obstack, stringappend (short_base_name, EXT_OUTPUT)); | |
54bd0db4 | 300 | } |