2 * Copyright (c) 1989 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40 All rights reserved.\n";
44 static char sccsid
[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
50 // FIXME autoconf or use remove instead of unlink
61 const char *file_prefix
= (char*)"y";
62 char *myname
= (char*)"yacc";
63 const char *temp_form
= "yacc.XXXXXXX";
68 char *action_file_name
;
69 char *input_file_name
= (char*)"";
70 char *prolog_file_name
;
71 char *local_file_name
;
72 char *verbose_file_name
;
73 char *output_file_name
= 0;
75 FILE *action_file
; /* a temp file, used to save actions associated */
76 /* with rules until the parser is written */
77 FILE *input_file
; /* the input file */
78 FILE *prolog_file
; /* temp files, used to save text until all */
79 FILE *local_file
; /* symbols have been defined */
80 FILE *verbose_file
; /* y.output */
81 FILE *output_file
; /* defaults to stdout */
105 #if defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
106 extern char* mktemp();
107 #define mkstemp mktemp
110 extern char *getenv();
115 if (action_file
) { fclose(action_file
); unlink(action_file_name
); }
116 if (prolog_file
) { fclose(prolog_file
); unlink(prolog_file_name
); }
117 if (local_file
) { fclose(local_file
); unlink(local_file_name
); }
118 if (output_file
&& (output_file
!= stdout
)) { fclose(output_file
); if (k
!= 0) unlink(output_file_name
); }
125 (void)signo
; // unused
133 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
134 signal(SIGINT
, onintr
);
137 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
138 signal(SIGTERM
, onintr
);
141 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
142 signal(SIGHUP
, onintr
);
149 fprintf(stderr
, "usage: %s [-tvcp] [-b file_prefix] [-o output_filename] input_filename\n", myname
);
156 printf ("%s\n", SKEL_DIRECTORY
);
161 getargs (int argc
, char *argv
[])
166 if (argc
> 0) myname
= argv
[0];
167 for (i
= 1; i
< argc
; ++i
)
170 if (*s
!= '-') break;
175 if (i
+ 1 < argc
) usage();
180 goto no_more_options
;
186 file_prefix
= argv
[i
];
193 output_file_name
= s
;
195 output_file_name
= argv
[i
];
210 line_format
= "#line %d \"%s\"\n";
211 default_line_format
= "#line default\n";
243 line_format
= "#line %d \"%s\"\n";
244 default_line_format
= "#line default\n";
256 if (i
+ 1 != argc
) usage();
257 input_file_name
= argv
[i
];
261 allocate (unsigned n
)
268 p
= (char*)CALLOC(1, n
);
275 #define GNUC_UNUSED __attribute__((__unused__))
281 create_file_names (void)
285 int mkstemp_res GNUC_UNUSED
;
287 #if defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
290 tmpdir
= getenv("TMPDIR");
291 if (tmpdir
== 0) tmpdir
= getenv ("TMP");
292 if (tmpdir
== 0) tmpdir
= getenv ("TEMP");
293 if (tmpdir
== 0) tmpdir
= "/tmp";
296 len
= strlen(tmpdir
);
298 if (len
&& tmpdir
[len
-1] != '/')
301 action_file_name
= (char*)MALLOC(i
);
302 if (action_file_name
== 0) no_space();
303 prolog_file_name
= (char*)MALLOC(i
);
304 if (prolog_file_name
== 0) no_space();
305 local_file_name
= (char*)MALLOC(i
);
306 if (local_file_name
== 0) no_space();
308 strcpy(action_file_name
, tmpdir
);
309 strcpy(prolog_file_name
, tmpdir
);
310 strcpy(local_file_name
, tmpdir
);
312 if (len
&& tmpdir
[len
- 1] != '/')
314 action_file_name
[len
] = '/';
315 prolog_file_name
[len
] = '/';
316 local_file_name
[len
] = '/';
320 strcpy(action_file_name
+ len
, temp_form
);
321 strcpy(prolog_file_name
+ len
, temp_form
);
322 strcpy(local_file_name
+ len
, temp_form
);
324 action_file_name
[len
+ 5] = 'a';
325 prolog_file_name
[len
+ 5] = 'p';
326 local_file_name
[len
+ 5] = 'l';
328 mkstemp_res
= mkstemp(action_file_name
);
329 mkstemp_res
= mkstemp(prolog_file_name
);
330 mkstemp_res
= mkstemp(local_file_name
);
332 len
= strlen(file_prefix
);
336 verbose_file_name
= (char*)MALLOC(len
+ 8);
337 if (verbose_file_name
== 0)
339 strcpy(verbose_file_name
, file_prefix
);
340 strcpy(verbose_file_name
+ len
, VERBOSE_SUFFIX
);
351 input_file
= fopen(input_file_name
, "r");
353 open_error(input_file_name
);
356 action_file
= fopen(action_file_name
, "w");
357 if (action_file
== 0)
358 open_error(action_file_name
);
360 prolog_file
= fopen(prolog_file_name
, "w");
361 if (prolog_file
== 0)
362 open_error(prolog_file_name
);
364 local_file
= fopen(local_file_name
, "w");
366 open_error(local_file_name
);
370 verbose_file
= fopen(verbose_file_name
, "w");
371 if (verbose_file
== 0)
372 open_error(verbose_file_name
);
375 if (output_file
== 0)
377 if (output_file_name
!= 0) {
378 output_file
= fopen(output_file_name
, "w");
379 if (output_file
== 0)
380 open_error(output_file_name
);
382 output_file
= stdout
;
389 main (int argc
, char *argv
[])