int mkmakefile() { register FILE *ifp, *ofp; register int lineno; register int (*fn) __P((FILE *)); register char *ofname; char line[BUFSIZ], ifname[200]; (void)sprintf(ifname, "Makefile.%s", machine); if ((ifp = fopen(ifname, "r")) == NULL) { (void)fprintf(stderr, "config: cannot read %s: %s\n", ifname, strerror(errno)); return (1); } ofname = path("Makefile"); if ((ofp = fopen(ofname, "w")) == NULL) { (void)fprintf(stderr, "config: cannot write %s: %s\n", ofname, strerror(errno)); free(ofname); return (1); } if (emitdefs(ofp) != 0) goto wrerror; lineno = 0; while (fgets(line, sizeof(line), ifp) != NULL) { lineno++; if (line[0] != '%') { if (fputs(line, ofp) < 0) goto wrerror; continue; } if (strcmp(line, "%OBJS\n") == 0) fn = emitobjs; else if (strcmp(line, "%CFILES\n") == 0) fn = emitcfiles; else if (strcmp(line, "%SFILES\n") == 0) fn = emitsfiles; else if (strcmp(line, "%RULES\n") == 0) fn = emitrules; else if (strcmp(line, "%LOAD\n") == 0) fn = emitload; else { xerror(ifname, lineno, "unknown %% construct ignored: %s", line); continue; } if ((*fn)(ofp)) goto wrerror; } if (ferror(ifp)) { (void)fprintf(stderr, "config: error reading %s (at line %d): %s\n", ifname, lineno, strerror(errno)); goto bad; /* (void)unlink(ofname); */ free(ofname); return (1); } if (fclose(ofp)) { ofp = NULL; goto wrerror; } (void)fclose(ifp); free(ofname); return (0); wrerror: (void)fprintf(stderr, "config: error writing %s: %s\n", ofname, strerror(errno)); bad: if (ofp != NULL) (void)fclose(ofp); /* (void)unlink(ofname); */ free(ofname); return (1); }
int main(int argc, char *argv[]) { int c, i; Nonterm p; for (i = 1; i < argc; i++) if (strcmp(argv[i], "-T") == 0) Tflag = 1; else if (strncmp(argv[i], "-p", 2) == 0 && argv[i][2]) prefix = &argv[i][2]; else if (strncmp(argv[i], "-p", 2) == 0 && i + 1 < argc) prefix = argv[++i]; else if (*argv[i] == '-' && argv[i][1]) { yyerror("usage: %s [-T | -p prefix]... [ [ input ] output ] \n", argv[0]); exit(1); } else if (infp == NULL) { if (strcmp(argv[i], "-") == 0) infp = stdin; else if ((infp = fopen(argv[i], "r")) == NULL) { yyerror("%s: can't read `%s'\n", argv[0], argv[i]); exit(1); } } else if (outfp == NULL) { if (strcmp(argv[i], "-") == 0) outfp = stdout; if ((outfp = fopen(argv[i], "w")) == NULL) { yyerror("%s: can't write `%s'\n", argv[0], argv[i]); exit(1); } } if (infp == NULL) infp = stdin; if (outfp == NULL) outfp = stdout; yyparse(); if (start) ckreach(start); for (p = nts; p; p = p->link) { if (p->rules == NULL) yyerror("undefined nonterminal `%s'\n", p->name); if (!p->reached) yyerror("can't reach nonterminal `%s'\n", p->name); } emitheader(); emitdefs(nts, ntnumber); emitstruct(nts, ntnumber); emitnts(rules, nrules); emitstring(rules); emitrule(nts); emitclosure(nts); if (start) emitlabel(terms, start, ntnumber); emitkids(rules, nrules); if (!feof(infp)) while ((c = getc(infp)) != EOF) putc(c, outfp); while (memlist) { /* for purify */ struct block *q = memlist->link; free(memlist); memlist = q; } return errcnt > 0; }