]>
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 JT |
22 | #include "system.h" |
23 | ||
fcca25ad | 24 | #if defined (VMS) & !defined (__VMS_POSIX) |
095a3fb5 AD |
25 | # ifndef BISON_SIMPLE |
26 | # define BISON_SIMPLE "GNU_BISON:[000000]BISON.SIMPLE" | |
ceed8467 | 27 | # endif |
095a3fb5 AD |
28 | # ifndef BISON_HAIRY |
29 | # define BISON_HARIRY "GNU_BISON:[000000]BISON.HAIRY" | |
ceed8467 | 30 | # endif |
54bd0db4 RS |
31 | #endif |
32 | ||
6a5705cf | 33 | #if defined (_MSC_VER) |
095a3fb5 AD |
34 | # ifndef BISON_SIMPLE |
35 | # define BISON_SIMPLE "c:/usr/local/lib/bison.simple" | |
ceed8467 | 36 | # endif |
095a3fb5 AD |
37 | # ifndef BISON_HAIRY |
38 | # define BISON_HAIRY "c:/usr/local/lib/bison.hairy" | |
ceed8467 | 39 | # endif |
dad49092 JT |
40 | #endif |
41 | ||
ceed8467 | 42 | #include "getargs.h" |
54bd0db4 | 43 | #include "files.h" |
d7913476 | 44 | #include "xalloc.h" |
54bd0db4 | 45 | #include "gram.h" |
a0f6b076 | 46 | #include "complain.h" |
54bd0db4 RS |
47 | |
48 | FILE *finput = NULL; | |
49 | FILE *foutput = NULL; | |
54bd0db4 | 50 | FILE *fguard = NULL; |
54bd0db4 RS |
51 | FILE *fparser = NULL; |
52 | ||
8c7ebe49 | 53 | struct obstack action_obstack; |
dd60faec | 54 | struct obstack attrs_obstack; |
896fe5c1 AD |
55 | struct obstack table_obstack; |
56 | struct obstack defines_obstack; | |
8c7ebe49 | 57 | |
54bd0db4 RS |
58 | /* File name specified with -o for the output file, or 0 if no -o. */ |
59 | char *spec_outfile; | |
60 | ||
61 | char *infile; | |
54bd0db4 | 62 | char *attrsfile; |
4a120d45 JT |
63 | |
64 | static char *outfile; | |
65 | static char *defsfile; | |
66 | static char *tabfile; | |
67 | static char *guardfile; | |
68 | static char *actfile; | |
54bd0db4 | 69 | |
8963a27b | 70 | extern char *getenv (); |
4a120d45 | 71 | |
54bd0db4 | 72 | extern char *program_name; |
54bd0db4 RS |
73 | \f |
74 | ||
8963a27b | 75 | static char * |
4a120d45 | 76 | stringappend (const char *string1, int end1, const char *string2) |
54bd0db4 RS |
77 | { |
78 | register char *ostring; | |
4a120d45 JT |
79 | register char *cp; |
80 | register const char *cp1; | |
54bd0db4 RS |
81 | register int i; |
82 | ||
4a120d45 JT |
83 | cp1 = string2; |
84 | i = 0; | |
8963a27b AD |
85 | while (*cp1++) |
86 | i++; | |
54bd0db4 | 87 | |
d7913476 | 88 | ostring = XCALLOC (char, i + end1 + 1); |
54bd0db4 RS |
89 | |
90 | cp = ostring; | |
91 | cp1 = string1; | |
92 | for (i = 0; i < end1; i++) | |
93 | *cp++ = *cp1++; | |
94 | ||
95 | cp1 = string2; | |
bd088c91 JT |
96 | while ((*cp++ = *cp1++)) |
97 | ; | |
54bd0db4 RS |
98 | |
99 | return ostring; | |
100 | } | |
101 | ||
cfe5fbc0 AD |
102 | /*-----------------------------------------------------------------. |
103 | | Try to open file NAME with mode MODE, and print an error message | | |
104 | | if fails. | | |
105 | `-----------------------------------------------------------------*/ | |
54bd0db4 | 106 | |
cfe5fbc0 | 107 | static FILE * |
8963a27b | 108 | xfopen (const char *name, const char *mode) |
cfe5fbc0 | 109 | { |
8963a27b | 110 | FILE *ptr; |
cfe5fbc0 AD |
111 | |
112 | ptr = fopen (name, mode); | |
113 | if (!ptr) | |
114 | error (2, errno, _("cannot open file `%s'"), name); | |
115 | ||
116 | return ptr; | |
117 | } | |
118 | ||
119 | /*-------------------------------------------------------------. | |
120 | | Try to close file PTR, and print an error message if fails. | | |
121 | `-------------------------------------------------------------*/ | |
122 | ||
123 | static int | |
8963a27b | 124 | xfclose (FILE *ptr) |
cfe5fbc0 AD |
125 | { |
126 | int result; | |
127 | ||
128 | if (ptr == NULL) | |
129 | return 0; | |
130 | ||
131 | result = fclose (ptr); | |
132 | if (result == EOF) | |
133 | error (2, errno, _("cannot close file")); | |
134 | ||
135 | return result; | |
136 | } | |
d8880f69 AD |
137 | |
138 | /*--------------------------------------------------. | |
139 | | Save the content of the obstack OBS in FILENAME. | | |
140 | `--------------------------------------------------*/ | |
141 | ||
9311529b | 142 | static void |
d8880f69 AD |
143 | obstack_save (struct obstack *obs, const char *filename) |
144 | { | |
145 | FILE *out = xfopen (filename, "w"); | |
146 | size_t size = obstack_object_size (obs); | |
147 | fwrite (obstack_finish (obs), 1, size, out); | |
148 | xfclose (out); | |
149 | } | |
150 | ||
9311529b AD |
151 | |
152 | static const char * | |
153 | skeleton_find (const char *envvar, const char *skeleton) | |
154 | { | |
155 | const char *res = getenv (envvar); | |
156 | ||
157 | #ifdef MSDOS | |
158 | const char *cp; | |
159 | ||
160 | /* File doesn't exist in current directory; try in INIT directory. */ | |
161 | if (!res && (cp = getenv ("INIT"))) | |
162 | { | |
163 | res = XMALLOC (char, strlen (cp) + strlen (skeleton) + 2); | |
164 | sprintf (res, "%s%c%s", cp, '/', skeleton); | |
165 | } | |
166 | #endif /* !MSDOS */ | |
167 | ||
168 | if (!res) | |
169 | res = skeleton; | |
170 | ||
171 | return res; | |
172 | } | |
173 | ||
cfe5fbc0 | 174 | \f |
d8880f69 AD |
175 | /*-----------------------------------------------------------------. |
176 | | Open the input file. Look for the skeletons. Find the names of | | |
177 | | the output files. Prepare the obstacks. | | |
178 | `-----------------------------------------------------------------*/ | |
179 | ||
54bd0db4 | 180 | void |
8963a27b | 181 | open_files (void) |
54bd0db4 RS |
182 | { |
183 | char *name_base; | |
a693bf18 | 184 | #ifdef MSDOS |
54bd0db4 | 185 | register char *cp; |
a693bf18 | 186 | #endif |
54bd0db4 RS |
187 | int base_length; |
188 | int short_base_length; | |
189 | ||
fcca25ad | 190 | #if defined (VMS) & !defined (__VMS_POSIX) |
4a120d45 | 191 | const char *tmp_base = "sys$scratch:b_"; |
54bd0db4 | 192 | #else |
4a120d45 | 193 | const char *tmp_base = "/tmp/b."; |
54bd0db4 RS |
194 | #endif |
195 | int tmp_len; | |
196 | ||
197 | #ifdef MSDOS | |
198 | tmp_base = getenv ("TMP"); | |
199 | if (tmp_base == 0) | |
200 | tmp_base = ""; | |
201 | strlwr (infile); | |
202 | #endif /* MSDOS */ | |
203 | ||
e0672a61 | 204 | #if (defined(_WIN32) && !defined(__CYGWIN32__)) |
8963a27b | 205 | tmp_base = getenv ("TEMP"); /* Windows95 defines this ... */ |
e0672a61 | 206 | if (tmp_base == 0) |
8963a27b | 207 | tmp_base = getenv ("Temp"); /* ... while NT prefers this */ |
e0672a61 RS |
208 | if (tmp_base == 0) |
209 | tmp_base = ""; | |
210 | strlwr (infile); | |
211 | #endif /* _WIN32 && !__CYGWIN32__ */ | |
212 | ||
5191ef24 | 213 | #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__)) |
bd088c91 | 214 | { |
8963a27b | 215 | char *tmp_ptr = getenv ("TMPDIR"); |
bd088c91 JT |
216 | |
217 | if (tmp_ptr != 0) | |
218 | tmp_base = stringappend (tmp_ptr, strlen (tmp_ptr), "/b."); | |
219 | } | |
8963a27b | 220 | #endif /* unix || __unix || __unix__ */ |
bd088c91 | 221 | |
54bd0db4 RS |
222 | tmp_len = strlen (tmp_base); |
223 | ||
224 | if (spec_outfile) | |
225 | { | |
896fe5c1 | 226 | /* -o was specified. The precise -o name will be used for FTABLE. |
8963a27b | 227 | For other output files, remove the ".c" or ".tab.c" suffix. */ |
54bd0db4 RS |
228 | name_base = spec_outfile; |
229 | #ifdef MSDOS | |
230 | strlwr (name_base); | |
231 | #endif /* MSDOS */ | |
232 | /* BASE_LENGTH includes ".tab" but not ".c". */ | |
233 | base_length = strlen (name_base); | |
234 | if (!strcmp (name_base + base_length - 2, ".c")) | |
235 | base_length -= 2; | |
236 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ | |
237 | short_base_length = base_length; | |
238 | if (!strncmp (name_base + short_base_length - 4, ".tab", 4)) | |
239 | short_base_length -= 4; | |
240 | else if (!strncmp (name_base + short_base_length - 4, "_tab", 4)) | |
241 | short_base_length -= 4; | |
242 | } | |
243 | else if (spec_file_prefix) | |
244 | { | |
245 | /* -b was specified. Construct names from it. */ | |
246 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ | |
247 | short_base_length = strlen (spec_file_prefix); | |
248 | /* Count room for `.tab'. */ | |
249 | base_length = short_base_length + 4; | |
d7913476 | 250 | name_base = XMALLOC (char, base_length + 1); |
54bd0db4 RS |
251 | /* Append `.tab'. */ |
252 | strcpy (name_base, spec_file_prefix); | |
253 | #ifdef VMS | |
254 | strcat (name_base, "_tab"); | |
255 | #else | |
256 | strcat (name_base, ".tab"); | |
257 | #endif | |
258 | #ifdef MSDOS | |
259 | strlwr (name_base); | |
260 | #endif /* MSDOS */ | |
261 | } | |
262 | else | |
263 | { | |
264 | /* -o was not specified; compute output file name from input | |
8963a27b | 265 | or use y.tab.c, etc., if -y was specified. */ |
54bd0db4 | 266 | |
4a120d45 JT |
267 | static char FIXED_NAME_BASE[] = "y.y"; |
268 | ||
89cab50d | 269 | name_base = yacc_flag ? FIXED_NAME_BASE : infile; |
54bd0db4 RS |
270 | |
271 | /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */ | |
272 | ||
273 | base_length = strlen (name_base); | |
274 | if (!strcmp (name_base + base_length - 2, ".y")) | |
275 | base_length -= 2; | |
276 | short_base_length = base_length; | |
277 | ||
278 | #ifdef VMS | |
8963a27b | 279 | name_base = stringappend (name_base, short_base_length, "_tab"); |
54bd0db4 RS |
280 | #else |
281 | #ifdef MSDOS | |
8963a27b | 282 | name_base = stringappend (name_base, short_base_length, "_tab"); |
54bd0db4 | 283 | #else |
8963a27b | 284 | name_base = stringappend (name_base, short_base_length, ".tab"); |
54bd0db4 RS |
285 | #endif /* not MSDOS */ |
286 | #endif | |
287 | base_length = short_base_length + 4; | |
288 | } | |
289 | ||
8963a27b | 290 | finput = xfopen (infile, "r"); |
54bd0db4 | 291 | |
89cab50d | 292 | if (!no_parser_flag) |
095a3fb5 | 293 | fparser = xfopen (skeleton_find ("BISON_SIMPLE", BISON_SIMPLE), "r"); |
54bd0db4 | 294 | |
89cab50d | 295 | if (verbose_flag) |
54bd0db4 RS |
296 | { |
297 | #ifdef MSDOS | |
8963a27b | 298 | outfile = stringappend (name_base, short_base_length, ".out"); |
54bd0db4 RS |
299 | #else |
300 | /* We used to use just .out if spec_name_prefix (-p) was used, | |
8963a27b AD |
301 | but that conflicts with Posix. */ |
302 | outfile = stringappend (name_base, short_base_length, ".output"); | |
54bd0db4 | 303 | #endif |
8963a27b | 304 | foutput = xfopen (outfile, "w"); |
54bd0db4 RS |
305 | } |
306 | ||
89cab50d | 307 | if (no_parser_flag) |
28683c54 RS |
308 | { |
309 | /* use permanent name for actions file */ | |
8963a27b | 310 | actfile = stringappend (name_base, short_base_length, ".act"); |
a0f6b076 | 311 | } |
28683c54 | 312 | |
89cab50d | 313 | if (defines_flag) |
54bd0db4 | 314 | { |
8963a27b | 315 | defsfile = stringappend (name_base, base_length, ".h"); |
54bd0db4 RS |
316 | } |
317 | ||
8963a27b | 318 | /* These are opened by `done' or `open_extra_files', if at all */ |
54bd0db4 RS |
319 | if (spec_outfile) |
320 | tabfile = spec_outfile; | |
321 | else | |
8963a27b | 322 | tabfile = stringappend (name_base, base_length, ".c"); |
54bd0db4 RS |
323 | |
324 | #ifdef VMS | |
8963a27b AD |
325 | attrsfile = stringappend (name_base, short_base_length, "_stype.h"); |
326 | guardfile = stringappend (name_base, short_base_length, "_guard.c"); | |
54bd0db4 RS |
327 | #else |
328 | #ifdef MSDOS | |
8963a27b AD |
329 | attrsfile = stringappend (name_base, short_base_length, ".sth"); |
330 | guardfile = stringappend (name_base, short_base_length, ".guc"); | |
54bd0db4 | 331 | #else |
8963a27b AD |
332 | attrsfile = stringappend (name_base, short_base_length, ".stype.h"); |
333 | guardfile = stringappend (name_base, short_base_length, ".guard.c"); | |
54bd0db4 RS |
334 | #endif /* not MSDOS */ |
335 | #endif /* not VMS */ | |
8c7ebe49 | 336 | |
dd60faec | 337 | /* Initialize the obstacks. */ |
8c7ebe49 | 338 | obstack_init (&action_obstack); |
dd60faec | 339 | obstack_init (&attrs_obstack); |
896fe5c1 AD |
340 | obstack_init (&table_obstack); |
341 | obstack_init (&defines_obstack); | |
54bd0db4 RS |
342 | } |
343 | ||
344 | ||
345 | ||
0d533154 AD |
346 | /*--------------------------------------------------------------------. |
347 | | Open the output files needed only for the semantic parser. This | | |
348 | | is done when %semantic_parser is seen in the declarations section. | | |
349 | `--------------------------------------------------------------------*/ | |
54bd0db4 RS |
350 | |
351 | void | |
bd088c91 | 352 | open_extra_files (void) |
54bd0db4 | 353 | { |
8963a27b | 354 | xfclose (fparser); |
54bd0db4 | 355 | |
89cab50d | 356 | if (!no_parser_flag) |
095a3fb5 | 357 | fparser = xfopen (skeleton_find ("BISON_HAIRY", BISON_HAIRY), "r"); |
8963a27b | 358 | fguard = xfopen (guardfile, "w"); |
54bd0db4 RS |
359 | } |
360 | ||
d8880f69 AD |
361 | |
362 | /*-----------------------------------------------------. | |
363 | | Close the open files, produce all the output files. | | |
364 | `-----------------------------------------------------*/ | |
365 | ||
a693bf18 | 366 | void |
d8880f69 | 367 | output_files (void) |
a693bf18 | 368 | { |
8963a27b AD |
369 | xfclose (fguard); |
370 | xfclose (finput); | |
371 | xfclose (fparser); | |
372 | xfclose (foutput); | |
54bd0db4 | 373 | |
d8880f69 AD |
374 | /* Output the main file. */ |
375 | obstack_save (&table_obstack, tabfile); | |
376 | ||
377 | /* Output the header file if wanted. */ | |
378 | if (defines_flag) | |
379 | obstack_save (&defines_obstack, defsfile); | |
54bd0db4 | 380 | |
d8880f69 | 381 | /* If we output only the table, dump the actions in ACTFILE. |
dd60faec | 382 | */ |
8c7ebe49 | 383 | if (no_parser_flag) |
d8880f69 | 384 | obstack_save (&action_obstack, actfile); |
dd60faec AD |
385 | |
386 | /* If we produced a semantic parser ATTRS_OBSTACK must be dumped | |
387 | into its own file, ATTTRSFILE. */ | |
388 | if (semantic_parser) | |
d8880f69 | 389 | obstack_save (&attrs_obstack, attrsfile); |
54bd0db4 | 390 | } |