]> git.saurik.com Git - bison.git/blob - src/files.c
Typos in ChangeLog.
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000 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
24 #if defined (VMS) & !defined (__VMS_POSIX)
25 # ifndef BISON_SIMPLE
26 # define BISON_SIMPLE "GNU_BISON:[000000]BISON.SIMPLE"
27 # endif
28 # ifndef BISON_HAIRY
29 # define BISON_HARIRY "GNU_BISON:[000000]BISON.HAIRY"
30 # endif
31 #endif
32
33 #if defined (_MSC_VER)
34 # ifndef BISON_SIMPLE
35 # define BISON_SIMPLE "c:/usr/local/lib/bison.simple"
36 # endif
37 # ifndef BISON_HAIRY
38 # define BISON_HAIRY "c:/usr/local/lib/bison.hairy"
39 # endif
40 #endif
41
42 #include "getargs.h"
43 #include "files.h"
44 #include "xalloc.h"
45 #include "gram.h"
46 #include "complain.h"
47
48 FILE *finput = NULL;
49 FILE *foutput = NULL;
50 FILE *fguard = NULL;
51 FILE *fparser = NULL;
52
53 struct obstack action_obstack;
54 struct obstack attrs_obstack;
55 struct obstack table_obstack;
56 struct obstack defines_obstack;
57
58 /* File name specified with -o for the output file, or 0 if no -o. */
59 char *spec_outfile;
60
61 char *infile;
62 char *attrsfile;
63
64 static char *outfile;
65 static char *defsfile;
66 static char *tabfile;
67 static char *guardfile;
68 static char *actfile;
69
70 extern char *getenv ();
71
72 extern char *program_name;
73 \f
74
75 static char *
76 stringappend (const char *string1, int end1, const char *string2)
77 {
78 register char *ostring;
79 register char *cp;
80 register const char *cp1;
81 register int i;
82
83 cp1 = string2;
84 i = 0;
85 while (*cp1++)
86 i++;
87
88 ostring = XCALLOC (char, i + end1 + 1);
89
90 cp = ostring;
91 cp1 = string1;
92 for (i = 0; i < end1; i++)
93 *cp++ = *cp1++;
94
95 cp1 = string2;
96 while ((*cp++ = *cp1++))
97 ;
98
99 return ostring;
100 }
101
102 /*-----------------------------------------------------------------.
103 | Try to open file NAME with mode MODE, and print an error message |
104 | if fails. |
105 `-----------------------------------------------------------------*/
106
107 static FILE *
108 xfopen (const char *name, const char *mode)
109 {
110 FILE *ptr;
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
124 xfclose (FILE *ptr)
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 }
137
138 /*--------------------------------------------------.
139 | Save the content of the obstack OBS in FILENAME. |
140 `--------------------------------------------------*/
141
142 static void
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
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
174 \f
175 /*-----------------------------------------------------------------.
176 | Open the input file. Look for the skeletons. Find the names of |
177 | the output files. Prepare the obstacks. |
178 `-----------------------------------------------------------------*/
179
180 void
181 open_files (void)
182 {
183 char *name_base;
184 #ifdef MSDOS
185 register char *cp;
186 #endif
187 int base_length;
188 int short_base_length;
189
190 #if defined (VMS) & !defined (__VMS_POSIX)
191 const char *tmp_base = "sys$scratch:b_";
192 #else
193 const char *tmp_base = "/tmp/b.";
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
204 #if (defined(_WIN32) && !defined(__CYGWIN32__))
205 tmp_base = getenv ("TEMP"); /* Windows95 defines this ... */
206 if (tmp_base == 0)
207 tmp_base = getenv ("Temp"); /* ... while NT prefers this */
208 if (tmp_base == 0)
209 tmp_base = "";
210 strlwr (infile);
211 #endif /* _WIN32 && !__CYGWIN32__ */
212
213 #if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__EMX__))
214 {
215 char *tmp_ptr = getenv ("TMPDIR");
216
217 if (tmp_ptr != 0)
218 tmp_base = stringappend (tmp_ptr, strlen (tmp_ptr), "/b.");
219 }
220 #endif /* unix || __unix || __unix__ */
221
222 tmp_len = strlen (tmp_base);
223
224 if (spec_outfile)
225 {
226 /* -o was specified. The precise -o name will be used for FTABLE.
227 For other output files, remove the ".c" or ".tab.c" suffix. */
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;
250 name_base = XMALLOC (char, base_length + 1);
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
265 or use y.tab.c, etc., if -y was specified. */
266
267 static char FIXED_NAME_BASE[] = "y.y";
268
269 name_base = yacc_flag ? FIXED_NAME_BASE : infile;
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
279 name_base = stringappend (name_base, short_base_length, "_tab");
280 #else
281 #ifdef MSDOS
282 name_base = stringappend (name_base, short_base_length, "_tab");
283 #else
284 name_base = stringappend (name_base, short_base_length, ".tab");
285 #endif /* not MSDOS */
286 #endif
287 base_length = short_base_length + 4;
288 }
289
290 finput = xfopen (infile, "r");
291
292 if (!no_parser_flag)
293 fparser = xfopen (skeleton_find ("BISON_SIMPLE", BISON_SIMPLE), "r");
294
295 if (verbose_flag)
296 {
297 #ifdef MSDOS
298 outfile = stringappend (name_base, short_base_length, ".out");
299 #else
300 /* We used to use just .out if spec_name_prefix (-p) was used,
301 but that conflicts with Posix. */
302 outfile = stringappend (name_base, short_base_length, ".output");
303 #endif
304 foutput = xfopen (outfile, "w");
305 }
306
307 if (no_parser_flag)
308 {
309 /* use permanent name for actions file */
310 actfile = stringappend (name_base, short_base_length, ".act");
311 }
312
313 if (defines_flag)
314 {
315 defsfile = stringappend (name_base, base_length, ".h");
316 }
317
318 /* These are opened by `done' or `open_extra_files', if at all */
319 if (spec_outfile)
320 tabfile = spec_outfile;
321 else
322 tabfile = stringappend (name_base, base_length, ".c");
323
324 #ifdef VMS
325 attrsfile = stringappend (name_base, short_base_length, "_stype.h");
326 guardfile = stringappend (name_base, short_base_length, "_guard.c");
327 #else
328 #ifdef MSDOS
329 attrsfile = stringappend (name_base, short_base_length, ".sth");
330 guardfile = stringappend (name_base, short_base_length, ".guc");
331 #else
332 attrsfile = stringappend (name_base, short_base_length, ".stype.h");
333 guardfile = stringappend (name_base, short_base_length, ".guard.c");
334 #endif /* not MSDOS */
335 #endif /* not VMS */
336
337 /* Initialize the obstacks. */
338 obstack_init (&action_obstack);
339 obstack_init (&attrs_obstack);
340 obstack_init (&table_obstack);
341 obstack_init (&defines_obstack);
342 }
343
344
345
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 `--------------------------------------------------------------------*/
350
351 void
352 open_extra_files (void)
353 {
354 xfclose (fparser);
355
356 if (!no_parser_flag)
357 fparser = xfopen (skeleton_find ("BISON_HAIRY", BISON_HAIRY), "r");
358 fguard = xfopen (guardfile, "w");
359 }
360
361
362 /*-----------------------------------------------------.
363 | Close the open files, produce all the output files. |
364 `-----------------------------------------------------*/
365
366 void
367 output_files (void)
368 {
369 xfclose (fguard);
370 xfclose (finput);
371 xfclose (fparser);
372 xfclose (foutput);
373
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);
380
381 /* If we output only the table, dump the actions in ACTFILE.
382 */
383 if (no_parser_flag)
384 obstack_save (&action_obstack, actfile);
385
386 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
387 into its own file, ATTTRSFILE. */
388 if (semantic_parser)
389 obstack_save (&attrs_obstack, attrsfile);
390 }