[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / yacc

Annotation of src/usr.bin/yacc/main.c, Revision 1.1.1.1

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