1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Robert Paul Corbett. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 char copyright[] = 13 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 14 All rights reserved.\n"; 15 #endif /* not lint */ 16 17 #ifndef lint 18 static char sccsid[] = "@(#)main.c 5.2 (Berkeley) 06/01/90"; 19 #endif /* not lint */ 20 21 #include <signal.h> 22 #include "defs.h" 23 24 char dflag; 25 char lflag; 26 char tflag; 27 char vflag; 28 29 char *prefix = "y"; 30 char *myname = "yacc"; 31 char *temp_form = "yacc.XXXXXXX"; 32 33 int lineno; 34 int outline; 35 36 char *action_file_name; 37 char *defines_file_name; 38 char *input_file_name = ""; 39 char *output_file_name; 40 char *text_file_name; 41 char *union_file_name; 42 char *verbose_file_name; 43 44 FILE *action_file; /* a temp file, used to save actions associated */ 45 /* with rules until the parser is written */ 46 FILE *defines_file; /* y.tab.h */ 47 FILE *input_file; /* the input file */ 48 FILE *output_file; /* y.tab.c */ 49 FILE *text_file; /* a temp file, used to save text until all */ 50 /* symbols have been defined */ 51 FILE *union_file; /* a temp file, used to save the union */ 52 /* definition until all symbol have been */ 53 /* defined */ 54 FILE *verbose_file; /* y.output */ 55 56 int nitems; 57 int nrules; 58 int nsyms; 59 int ntokens; 60 int nvars; 61 62 int start_symbol; 63 char **symbol_name; 64 short *symbol_value; 65 short *symbol_prec; 66 char *symbol_assoc; 67 68 short *ritem; 69 short *rlhs; 70 short *rrhs; 71 short *rprec; 72 char *rassoc; 73 short **derives; 74 char *nullable; 75 76 extern char *mktemp(); 77 extern char *getenv(); 78 79 80 done(k) 81 int k; 82 { 83 if (action_file) { fclose(action_file); unlink(action_file_name); } 84 if (text_file) { fclose(text_file); unlink(text_file_name); } 85 if (union_file) { fclose(union_file); unlink(union_file_name); } 86 exit(k); 87 } 88 89 90 onintr() 91 { 92 done(1); 93 } 94 95 96 set_signals() 97 { 98 #ifdef SIGINT 99 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 100 signal(SIGINT, onintr); 101 #endif 102 #ifdef SIGTERM 103 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 104 signal(SIGTERM, onintr); 105 #endif 106 #ifdef SIGHUP 107 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 108 signal(SIGHUP, onintr); 109 #endif 110 } 111 112 113 usage() 114 { 115 fprintf(stderr, "usage: %s [-dltv] [-b prefix] filename\n", myname); 116 exit(1); 117 } 118 119 120 getargs(argc, argv) 121 int argc; 122 char *argv[]; 123 { 124 register int i; 125 register char *s; 126 127 if (argc > 0) myname = argv[0]; 128 for (i = 1; i < argc; ++i) 129 { 130 s = argv[i]; 131 if (*s != '-') break; 132 switch (*++s) 133 { 134 case '\0': 135 input_file = stdin; 136 if (i + 1 < argc) usage(); 137 return; 138 139 case '_': 140 ++i; 141 goto no_more_options; 142 143 case 'b': 144 if (*++s || ++i >= argc) usage(); 145 prefix = argv[i]; 146 continue; 147 148 case 'd': 149 dflag = 1; 150 break; 151 152 case 'l': 153 lflag = 1; 154 break; 155 156 case 't': 157 tflag = 1; 158 break; 159 160 case 'v': 161 vflag = 1; 162 break; 163 164 default: 165 usage(); 166 } 167 168 for (;;) 169 { 170 switch (*++s) 171 { 172 case '\0': 173 goto end_of_option; 174 175 case 'd': 176 dflag = 1; 177 break; 178 179 case 'l': 180 lflag = 1; 181 break; 182 183 case 't': 184 tflag = 1; 185 break; 186 187 case 'v': 188 vflag = 1; 189 break; 190 191 default: 192 usage(); 193 } 194 } 195 end_of_option:; 196 } 197 198 no_more_options:; 199 if (i + 1 != argc) usage(); 200 input_file_name = argv[i]; 201 } 202 203 204 char * 205 allocate(n) 206 unsigned n; 207 { 208 register char *p; 209 210 p = calloc((unsigned) 1, n); 211 if (!p) no_space(); 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(prefix); 261 if (dflag) 262 { 263 /* the number 7 below is the size of ".tab.h"; sizeof is not used */ 264 /* because of a C compiler that thinks sizeof(".tab.h") == 6 */ 265 defines_file_name = MALLOC(len + 7); 266 if (defines_file_name == 0) no_space(); 267 strcpy(defines_file_name, prefix); 268 strcpy(defines_file_name + len, DEFINES_SUFFIX); 269 } 270 271 output_file_name = MALLOC(len + 7); 272 if (output_file_name == 0) no_space(); 273 strcpy(output_file_name, prefix); 274 strcpy(output_file_name + len, OUTPUT_SUFFIX); 275 276 if (vflag) 277 { 278 verbose_file_name = MALLOC(len + 8); 279 if (verbose_file_name == 0) no_space(); 280 strcpy(verbose_file_name, prefix); 281 strcpy(verbose_file_name + len, VERBOSE_SUFFIX); 282 } 283 } 284 285 286 open_files() 287 { 288 create_file_names(); 289 290 if (input_file == 0) 291 { 292 input_file = fopen(input_file_name, "r"); 293 if (input_file == 0) open_error(input_file_name); 294 } 295 296 action_file = fopen(action_file_name, "w"); 297 if (action_file == 0) open_error(action_file_name); 298 299 text_file = fopen(text_file_name, "w"); 300 if (text_file == 0) open_error(text_file_name); 301 302 if (vflag) 303 { 304 verbose_file = fopen(verbose_file_name, "w"); 305 if (verbose_file == 0) open_error(verbose_file_name); 306 } 307 308 if (dflag) 309 { 310 defines_file = fopen(defines_file_name, "w"); 311 if (defines_file == 0) open_error(defines_file_name); 312 union_file = fopen(union_file_name, "w"); 313 if (union_file == 0) open_error(union_file_name); 314 } 315 316 output_file = fopen(output_file_name, "w"); 317 if (output_file == 0) open_error(output_file_name); 318 } 319 320 321 int 322 main(argc, argv) 323 int argc; 324 char *argv[]; 325 { 326 set_signals(); 327 getargs(argc, argv); 328 open_files(); 329 reader(); 330 lr0(); 331 lalr(); 332 make_parser(); 333 verbose(); 334 output(); 335 done(0); 336 /*NOTREACHED*/ 337 } 338