int main(int argc, char **argv) { Tokenrow tr; time_t t; STATIC char ebuf[BUFSIZ]; #if defined(__GNO__) && defined(__STACK_CHECK__) atexit(printStack); #endif setbuf(stderr, ebuf); t = time(NULL); curtime = ctime(&t); maketokenrow(3, &tr); expandlex(); setup(argc, argv); fixlex(); iniths(); genline(); process(&tr); flushout(); fflush(stderr); exit(nerrs > 0); return 0; }
/* * Produce a copy of a row of tokens. Start at trp->tp. * The value strings are copied as well. The first token * has WS available. */ Tokenrow * normtokenrow(Tokenrow *trp) { Token *tp; Tokenrow *ntrp = new(Tokenrow); int len; len = trp->lp - trp->tp; if (len<=0) len = 1; maketokenrow(len, ntrp); for (tp=trp->tp; tp < trp->lp; tp++) { *ntrp->lp = *tp; if (tp->len) { ntrp->lp->t = newstring(tp->t, tp->len, 1); *ntrp->lp->t++ = ' '; if (tp->wslen) ntrp->lp->wslen = 1; } ntrp->lp++; } if (ntrp->lp > ntrp->bp) ntrp->bp->wslen = 0; return ntrp; }
/* * Copy a row of tokens into the destination holder, allocating * the space for the contents. Return the destination. */ Tokenrow * copytokenrow(Tokenrow *dtr, Tokenrow *str) { int len = rowlen(str); maketokenrow(len, dtr); movetokenrow(dtr, str); dtr->lp += len; return dtr; }
int main(int argc, char **argv) { Tokenrow tr; /* token row */ time_t t; /* 保存当前时间的整数值 */ char ebuf[BUFSIZ]; /* stderr buffer(stderr默认是没有buffer的,我们为其建立一个buffer) */ setbuf(stderr, ebuf); /* 设定标准错误的输出缓冲区为ebuf */ t = time(NULL); /* 获得当前时间的整数值 */ curtime = ctime(&t); /* 获得当前时间的字串值(例如: Sat Jul 4 12:27:13 2003)保存到全局变量curtime中 */ maketokenrow(3, &tr); /* 建立一个 token row(默认分配3个Token结构) */ expandlex(); /* 展开状态机 */ setup(argc, argv); /* 建立关键字hash表;处理命令行选项参数;将输入源文件压栈 */ fixlex(); /* 适时的关闭兼容C++单行注释兼容特性 */ iniths(); /* 初始化hideset,TODO: 啥是hideset? */ genline(); /* 产生一个行控制(line control)信息 */ process(&tr); /* 开始处理源程序 */ flushout(); /* flush output buffer to stdout */ fflush(stderr); /* flush stderr buffer */ exit(nerrs > 0); /* 退出进程 */ return 0; }
int main(int argc, char **argv) { Tokenrow tr; time_t t; char ebuf[BUFSIZ]; setbuf(stderr, ebuf); t = time(NULL); curtime = ctime(&t); maketokenrow(3, &tr); expandlex(); setup(argc, argv); fixlex(); iniths(); genline(); process(&tr); flushout(); fflush(stderr); exit(nerrs > 0); return 0; }
/* * Evaluate the ## operators in a tokenrow */ void doconcat(Tokenrow *trp) { Token *ltp, *ntp; STATIC Tokenrow ntr; int len; CHECKIN(); for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) { if (trp->tp->type==DSHARP1) trp->tp->type = DSHARP; else if (trp->tp->type==DSHARP) { STATIC char tt[128]; ltp = trp->tp-1; ntp = trp->tp+1; if (ltp<trp->bp || ntp>=trp->lp) { error(ERROR, "## occurs at border of replacement"); continue; } len = ltp->len + ntp->len; strncpy((char*)tt, (char*)ltp->t, ltp->len); strncpy((char*)tt+ltp->len, (char*)ntp->t, ntp->len); tt[len] = '\0'; setsource("<##>", -1, tt); maketokenrow(3, &ntr); gettokens(&ntr, 1); unsetsource(); if (ntr.lp-ntr.bp!=2 || ntr.bp->type==UNCLASS) error(WARNING, "Bad token %r produced by ##", &ntr); ntr.lp = ntr.bp+1; trp->tp = ltp; makespace(&ntr); insertrow(trp, (ntp-ltp)+1, &ntr); dofree(ntr.bp); trp->tp--; } } CHECKOUT(); }
void setup(int argc, char **argv) { int c, fd, i; char *fp, *dp; Tokenrow tr; extern void setup_kwtab(void); setup_kwtab(); while ((c = getopt(argc, argv, "MNOVv+I:D:U:F:lg")) != -1) switch (c) { case 'N': for (i=0; i<NINCLUDE; i++) if (includelist[i].always==1) includelist[i].deleted = 1; break; case 'I': for (i=NINCLUDE-2; i>=0; i--) { if (includelist[i].file==NULL) { includelist[i].always = 1; includelist[i].file = optarg; break; } } if (i<0) error(FATAL, "Too many -I directives"); break; case 'D': case 'U': setsource("<cmdarg>", -1, optarg); maketokenrow(3, &tr); gettokens(&tr, 1); doadefine(&tr, c); unsetsource(); break; case 'M': Mflag++; break; case 'v': fprintf(stderr, "%s %s\n", argv[0], rcsid); break; case 'V': verbose++; break; case '+': Cplusplus++; break; default: break; } dp = "."; fp = "<stdin>"; fd = 0; if (optind<argc) { if ((fp = strrchr(argv[optind], '/')) != NULL) { int len = fp - argv[optind]; dp = (char*)newstring((uchar*)argv[optind], len+1, 0); dp[len] = '\0'; } fp = (char*)newstring((uchar*)argv[optind], strlen(argv[optind]), 0); if ((fd = open(fp, 0)) <= 0) error(FATAL, "Can't open input file %s", fp); } if (optind+1<argc) { int fdo = creat(argv[optind+1], 0666); if (fdo<0) error(FATAL, "Can't open output file %s", argv[optind+1]); dup2(fdo, 1); } if(Mflag) setobjname(fp); includelist[NINCLUDE-1].always = 0; includelist[NINCLUDE-1].file = dp; setsource(fp, fd, NULL); }
/* * do a macro definition. tp points to the name being defined in the line */ void dodefine(Tokenrow *trp) { Token *tp; Nlist *np; Tokenrow *def, *args; tp = trp->tp+1; if (tp>=trp->lp || tp->type!=NAME) { error(ERROR, "#defined token is not a name"); return; } np = lookup(tp, 1); if (np->flag&ISUNCHANGE) { error(ERROR, "#defined token %t can't be redefined", tp); return; } /* collect arguments */ tp += 1; args = NULL; if (tp<trp->lp && tp->type==LP && tp->wslen==0) { /* macro with args */ int narg = 0; tp += 1; args = new(Tokenrow); maketokenrow(2, args); if (tp->type!=RP) { int err = 0; for (;;) { Token *atp; if (tp->type!=NAME) { err++; break; } if (narg>=args->max) growtokenrow(args); for (atp=args->bp; atp<args->lp; atp++) if (atp->len==tp->len && strncmp((char*)atp->t, (char*)tp->t, tp->len)==0) error(ERROR, "Duplicate macro argument"); *args->lp++ = *tp; narg++; tp += 1; if (tp->type==RP) break; if (tp->type!=COMMA) { err++; break; } tp += 1; } if (err) { error(ERROR, "Syntax error in macro parameters"); return; } } tp += 1; } trp->tp = tp; if (((trp->lp)-1)->type==NL) trp->lp -= 1; def = normtokenrow(trp); if (np->flag&ISDEFINED) { if (comparetokens(def, np->vp) || (np->ap==NULL) != (args==NULL) || np->ap && comparetokens(args, np->ap)) error(ERROR, "Macro redefinition of %t", trp->bp+2); } if (args) { Tokenrow *tap; tap = normtokenrow(args); dofree(args->bp); args = tap; } np->ap = args; np->vp = def; np->flag |= ISDEFINED; }
/* * Evaluate the ## operators in a tokenrow */ void doconcat(Tokenrow * trp) { Token *ltp, *ntp; Tokenrow ntr; size_t len; for (trp->tp = trp->bp; trp->tp < trp->lp; trp->tp++) { if (trp->tp->type == DSHARP1) trp->tp->type = DSHARP; else if (trp->tp->type == DSHARP) { int i; char tt[NCONCAT]; ltp = trp->tp - 1; ntp = trp->tp + 1; if (ltp < trp->bp || ntp >= trp->lp) { error(ERROR, "## occurs at border of replacement"); continue; } ntp = ltp; i = 1; len = 0; do { if (len + ntp->len + ntp->wslen > sizeof(tt)) { error(ERROR, "## string concatination buffer overrun"); break; } if (ntp != trp->tp + 1) { strncpy((char *) tt + len, (char *) ntp->t - ntp->wslen, ntp->len + ntp->wslen); len += ntp->len + ntp->wslen; } else // Leerzeichen um ## herum entfernen: { strncpy((char *) tt + len, (char *) ntp->t, ntp->len); len += ntp->len; } ntp = trp->tp + i; i++; } while (ntp < trp->lp); tt[len] = '\0'; setsource("<##>", -1, -1, tt, 0); maketokenrow(3, &ntr); gettokens(&ntr, 1); unsetsource(); if (ntr.bp->type == UNCLASS) error(WARNING, "Bad token %r produced by ##", &ntr); while ((ntr.lp-1)->len == 0 && ntr.lp != ntr.bp) ntr.lp--; doconcat(&ntr); trp->tp = ltp; makespace(&ntr, ltp); insertrow(trp, (int)(ntp - ltp), &ntr); dofree(ntr.bp); trp->tp--; } } }
/* * do a macro definition. tp points to the name being defined in the line */ void dodefine(Tokenrow * trp) { Token *tp; Nlist *np; Source *s; Tokenrow *def, *args; static uchar location[(PATH_MAX + 8) * NINC], *cp; tp = trp->tp + 1; if (tp >= trp->lp || tp->type != NAME) { error(ERROR, "#defined token is not a name"); return; } np = lookup(tp, 1); if (np->flag & ISUNCHANGE) { error(ERROR, "#defined token %t can't be redefined", tp); return; } /* collect arguments */ tp += 1; args = NULL; if (tp < trp->lp && tp->type == LP && tp->wslen == 0) { tp += 1; args = new(Tokenrow); maketokenrow(2, args); if (tp->type != RP) { /* macro with args */ size_t narg = 0; int err = 0; for (;;) { Token *atp; if (tp->type != NAME) { err++; break; } if (narg >= args->max) growtokenrow(args); for (atp = args->bp; atp < args->lp; atp++) if (atp->len == tp->len && strncmp((char *) atp->t, (char *) tp->t, tp->len) == 0) error(ERROR, "Duplicate macro argument"); *args->lp++ = *tp; narg++; tp += 1; if (tp->type == RP) break; if (tp->type != COMMA) { err++; break; } tp += 1; } if (err) { error(ERROR, "Syntax error in macro parameters"); return; } } tp += 1; } trp->tp = tp; if (((trp->lp) - 1)->type == NL) trp->lp -= 1; def = normtokenrow(trp); if (np->flag & ISDEFINED) { if (comparetokens(def, np->vp) || (np->ap == NULL) != (args == NULL) || (np->ap && comparetokens(args, np->ap))) { if ( np->loc ) error(ERROR, "Macro redefinition of %t (already defined at %s)", trp->bp + 2, np->loc); else error(ERROR, "Macro redefinition of %t (already defined at %s)", trp->bp + 2, "commandline" ); } } if (args) { Tokenrow *tap; tap = normtokenrow(args); dofree(args->bp); dofree(args); args = tap; } np->ap = args; np->vp = def; np->flag |= ISDEFINED; /* build location string of macro definition */ for (cp = location, s = cursource; s; s = s->next) if (*s->filename) { if (cp != location) *cp++ = ' '; sprintf((char *)cp, "%s:%d", s->filename, s->line); cp += strlen((char *)cp); } np->loc = newstring(location, strlen((char *)location), 0); if (Mflag) { if (np->ap) error(INFO, "Macro definition of %s(%r) [%r]", np->name, np->ap, np->vp); else error(INFO, "Macro definition of %s [%r]", np->name, np->vp); } }
void setup(int argc, char **argv) { int c, fd, i, n; char *fp, *dp; Tokenrow tr; setup_kwtab(); #if defined(MACOSX) || defined(AIX) || defined(_WIN32) while ((c = stgetopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1) #else while ((c = getopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1) #endif switch (c) { case 'N': for (i = 0; i < NINCLUDE; i++) if (includelist[i].always == 1) includelist[i].deleted = 1; break; case 'I': for (i = NINCLUDE - 2; i >= 0; i--) { if (includelist[i].file == NULL) { includelist[i].always = 1; includelist[i].file = optarg; break; } } if (i < 0) error(FATAL, "Too many -I directives"); break; case 'D': case 'U': case 'A': setsource("<cmdarg>", -1, -1, optarg, 0); maketokenrow(3, &tr); gettokens(&tr, 1); doadefine(&tr, c); dofree(tr.bp); unsetsource(); break; case 'P': /* Lineinfo */ Pflag++; break; case 'V': for (n = 0; (c = optarg[n]) != '\0'; n++) switch (c) { case 'i': Iflag++; break; case 'm': Mflag = 1; break; case 'x': Mflag = 2; break; case 't': Vflag++; break; case 'v': fprintf(stderr, "%s\n", argv[0]); break; default: error(WARNING, "Unknown verbose option %c", c); } break; case 'X': for (n = 0; (c = optarg[n]) != '\0'; n++) switch (c) { case 'a': Aflag++; break; case 'i': Xflag++; break; case 'c': Cflag++; break; case 'd': Dflag++; break; case 'w': dp = &optarg[n + 1]; n += (int)strlen(dp); while (isspace(*dp)) dp++; for (i = NINCLUDE - 1; i >= 0; i--) { if (wraplist[i].file == NULL) { wraplist[i].file = dp; break; } } if (i < 0) error(WARNING, "Too many -Xw directives"); break; default: error(WARNING, "Unknown extension option %c", c); } break; case '+': Cplusplus++; break; case 'u': /* -undef fuer GCC (dummy) */ case 'l': /* -lang-c++ fuer GCC (dummy) */ break; default: break; } dp = "."; fp = "<stdin>"; fd = 0; if (optind < argc) { if ((fp = strrchr(argv[optind], '/')) != NULL) { int len = (int)(fp - argv[optind]); dp = (char *) newstring((uchar *) argv[optind], len + 1, 0); dp[len] = '\0'; } fp = (char *) newstring((uchar *) argv[optind], strlen(argv[optind]), 0); if ((fd = open(fp, O_RDONLY)) <= 0) error(FATAL, "Can't open input file %s", fp); } if (optind + 1 < argc) { int fdo = creat(argv[optind + 1], 0666); if (fdo < 0) error(FATAL, "Can't open output file %s", argv[optind + 1]); dup2(fdo, 1); } includelist[NINCLUDE - 1].always = 0; includelist[NINCLUDE - 1].file = dp; setsource(fp, -1, fd, NULL, 0); }