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