Pristine Ack-5.5
[Ack-5.5.git] / util / byacc / main.c
1 #include <signal.h>
2 #include "defs.h"
3
4 char dflag;
5 char lflag;
6 char rflag;
7 char tflag;
8 char vflag;
9
10 char *file_prefix = "y";
11 char *myname = "yacc";
12 char *temp_form = "yacc.XXXXXXX";
13
14 int lineno;
15 int outline;
16
17 char *action_file_name;
18 char *code_file_name;
19 char *defines_file_name;
20 char *input_file_name = "";
21 char *output_file_name;
22 char *text_file_name;
23 char *union_file_name;
24 char *verbose_file_name;
25
26 FILE *action_file;      /*  a temp file, used to save actions associated    */
27                         /*  with rules until the parser is written          */
28 FILE *code_file;        /*  y.code.c (used when the -r option is specified) */
29 FILE *defines_file;     /*  y.tab.h                                         */
30 FILE *input_file;       /*  the input file                                  */
31 FILE *output_file;      /*  y.tab.c                                         */
32 FILE *text_file;        /*  a temp file, used to save text until all        */
33                         /*  symbols have been defined                       */
34 FILE *union_file;       /*  a temp file, used to save the union             */
35                         /*  definition until all symbol have been           */
36                         /*  defined                                         */
37 FILE *verbose_file;     /*  y.output                                        */
38
39 int nitems;
40 int nrules;
41 int nsyms;
42 int ntokens;
43 int nvars;
44
45 int   start_symbol;
46 char  **symbol_name;
47 short *symbol_value;
48 short *symbol_prec;
49 char  *symbol_assoc;
50
51 short *ritem;
52 short *rlhs;
53 short *rrhs;
54 short *rprec;
55 char  *rassoc;
56 short **derives;
57 char *nullable;
58
59 extern char *mktemp();
60 extern char *getenv();
61
62
63 done(k)
64 int k;
65 {
66     if (action_file) { fclose(action_file); unlink(action_file_name); }
67     if (text_file) { fclose(text_file); unlink(text_file_name); }
68     if (union_file) { fclose(union_file); unlink(union_file_name); }
69     exit(k);
70 }
71
72
73 void
74 onintr()
75 {
76     done(1);
77 }
78
79
80 set_signals()
81 {
82 #ifdef SIGINT
83     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
84         signal(SIGINT, onintr);
85 #endif
86 #ifdef SIGTERM
87     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
88         signal(SIGTERM, onintr);
89 #endif
90 #ifdef SIGHUP
91     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
92         signal(SIGHUP, onintr);
93 #endif
94 }
95
96
97 usage()
98 {
99     fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
100     exit(1);
101 }
102
103
104 getargs(argc, argv)
105 int argc;
106 char *argv[];
107 {
108     register int i;
109     register char *s;
110
111     if (argc > 0) myname = argv[0];
112     for (i = 1; i < argc; ++i)
113     {
114         s = argv[i];
115         if (*s != '-') break;
116         switch (*++s)
117         {
118         case '\0':
119             input_file = stdin;
120             if (i + 1 < argc) usage();
121             return;
122
123         case '-':
124             ++i;
125             goto no_more_options;
126
127         case 'b':
128             if (*++s)
129                  file_prefix = s;
130             else if (++i < argc)
131                 file_prefix = argv[i];
132             else
133                 usage();
134             continue;
135
136         case 'd':
137             dflag = 1;
138             break;
139
140         case 'l':
141             lflag = 1;
142             break;
143
144         case 'r':
145             rflag = 1;
146             break;
147
148         case 't':
149             tflag = 1;
150             break;
151
152         case 'v':
153             vflag = 1;
154             break;
155
156         default:
157             usage();
158         }
159
160         for (;;)
161         {
162             switch (*++s)
163             {
164             case '\0':
165                 goto end_of_option;
166
167             case 'd':
168                 dflag = 1;
169                 break;
170
171             case 'l':
172                 lflag = 1;
173                 break;
174
175             case 'r':
176                 rflag = 1;
177                 break;
178
179             case 't':
180                 tflag = 1;
181                 break;
182
183             case 'v':
184                 vflag = 1;
185                 break;
186
187             default:
188                 usage();
189             }
190         }
191 end_of_option:;
192     }
193
194 no_more_options:;
195     if (i + 1 != argc) usage();
196     input_file_name = argv[i];
197 }
198
199
200 char *
201 allocate(n)
202 unsigned n;
203 {
204     register char *p;
205
206     p = NULL;
207     if (n)
208     {
209         p = CALLOC(1, n);
210         if (!p) no_space();
211     }
212     return (p);
213 }
214
215
216 create_file_names()
217 {
218     int i, len;
219     char *tmpdir;
220
221     tmpdir = getenv("TMPDIR");
222     if (tmpdir == 0) tmpdir = "/tmp";
223
224     len = strlen(tmpdir);
225     i = len + 13;
226     if (len && tmpdir[len-1] != '/')
227         ++i;
228
229     action_file_name = MALLOC(i);
230     if (action_file_name == 0) no_space();
231     text_file_name = MALLOC(i);
232     if (text_file_name == 0) no_space();
233     union_file_name = MALLOC(i);
234     if (union_file_name == 0) no_space();
235
236     strcpy(action_file_name, tmpdir);
237     strcpy(text_file_name, tmpdir);
238     strcpy(union_file_name, tmpdir);
239
240     if (len && tmpdir[len - 1] != '/')
241     {
242         action_file_name[len] = '/';
243         text_file_name[len] = '/';
244         union_file_name[len] = '/';
245         ++len;
246     }
247
248     strcpy(action_file_name + len, temp_form);
249     strcpy(text_file_name + len, temp_form);
250     strcpy(union_file_name + len, temp_form);
251
252     action_file_name[len + 5] = 'a';
253     text_file_name[len + 5] = 't';
254     union_file_name[len + 5] = 'u';
255
256     mktemp(action_file_name);
257     mktemp(text_file_name);
258     mktemp(union_file_name);
259
260     len = strlen(file_prefix);
261
262     output_file_name = MALLOC(len + 7);
263     if (output_file_name == 0)
264         no_space();
265     strcpy(output_file_name, file_prefix);
266     strcpy(output_file_name + len, OUTPUT_SUFFIX);
267
268     if (rflag)
269     {
270         code_file_name = MALLOC(len + 8);
271         if (code_file_name == 0)
272             no_space();
273         strcpy(code_file_name, file_prefix);
274         strcpy(code_file_name + len, CODE_SUFFIX);
275     }
276     else
277         code_file_name = output_file_name;
278
279     if (dflag)
280     {
281         defines_file_name = MALLOC(len + 7);
282         if (defines_file_name == 0)
283             no_space();
284         strcpy(defines_file_name, file_prefix);
285         strcpy(defines_file_name + len, DEFINES_SUFFIX);
286     }
287
288     if (vflag)
289     {
290         verbose_file_name = MALLOC(len + 8);
291         if (verbose_file_name == 0)
292             no_space();
293         strcpy(verbose_file_name, file_prefix);
294         strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
295     }
296 }
297
298
299 open_files()
300 {
301     create_file_names();
302
303     if (input_file == 0)
304     {
305         input_file = fopen(input_file_name, "r");
306         if (input_file == 0)
307             open_error(input_file_name);
308     }
309
310     action_file = fopen(action_file_name, "w");
311     if (action_file == 0)
312         open_error(action_file_name);
313
314     text_file = fopen(text_file_name, "w");
315     if (text_file == 0)
316         open_error(text_file_name);
317
318     if (vflag)
319     {
320         verbose_file = fopen(verbose_file_name, "w");
321         if (verbose_file == 0)
322             open_error(verbose_file_name);
323     }
324
325     if (dflag)
326     {
327         defines_file = fopen(defines_file_name, "w");
328         if (defines_file == 0)
329             open_error(defines_file_name);
330         union_file = fopen(union_file_name, "w");
331         if (union_file ==  0)
332             open_error(union_file_name);
333     }
334
335     output_file = fopen(output_file_name, "w");
336     if (output_file == 0)
337         open_error(output_file_name);
338
339     if (rflag)
340     {
341         code_file = fopen(code_file_name, "w");
342         if (code_file == 0)
343             open_error(code_file_name);
344     }
345     else
346         code_file = output_file;
347 }
348
349
350 int
351 main(argc, argv)
352 int argc;
353 char *argv[];
354 {
355     set_signals();
356     getargs(argc, argv);
357     open_files();
358     reader();
359     lr0();
360     lalr();
361     make_parser();
362     verbose();
363     output();
364     done(0);
365     /*NOTREACHED*/
366 }