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; }
void process(Tokenrow *trp) { int anymacros = 0; for (;;) { if (trp->tp >= trp->lp) { trp->tp = trp->lp = trp->bp; outbufp = outbuf; anymacros |= gettokens(trp, 1); trp->tp = trp->bp; } if (trp->tp->type == END) { if (--incdepth >= 0) { if (cursource->ifdepth) error(ERROR, "Unterminated conditional in #include"); unsetsource(); cursource->line += cursource->lineinc; trp->tp = trp->lp; genline(); continue; } if (ifdepth) error(ERROR, "Unterminated #if/#ifdef/#ifndef"); break; } if (trp->tp->type == SHARP) { trp->tp += 1; control(trp); } else if (!skipping && anymacros) expandrow(trp, NULL); if (skipping) setempty(trp); puttokens(trp); anymacros = 0; cursource->line += cursource->lineinc; if (cursource->lineinc > 1) { genline(); } } }
/** * process - 处理c源程序的总函数 * @trp: 用来存储c源程序中的一行Token */ void process(Tokenrow *trp) { int anymacros = 0; for (;;) { if (trp->tp >= trp->lp) { /* 如果当前token row中没有有效数据 */ trp->tp = trp->lp = trp->bp; /* 重置token row的当前token指针 */ outp = outbuf; /* 重置输出缓冲区的当前指针 */ anymacros |= gettokens(trp, 1); /* 得到一行Token */ trp->tp = trp->bp; } if (trp->tp->type == END) { /* 如果遇到EOFC结束符 */ if (--incdepth >= 0) { if (cursource->ifdepth) error(ERROR,"Unterminated conditional in #include"); unsetsource(); cursource->line += cursource->lineinc; trp->tp = trp->lp; genline(); continue; } if (ifdepth) error(ERROR, "Unterminated #if/#ifdef/#ifndef"); break; } if (trp->tp->type==SHARP) { /* 如果当前Token是'#'字符 */ trp->tp += 1; /* tp移动到token row中的下一个token */ control(trp); /* 处理预处理指令部分(宏定义,条件编译,头文件包含) */ } else if (!skipping && anymacros) expandrow(trp, NULL); if (skipping) /* 如果当前是忽略状态 */ setempty(trp); /* 置空当前行 */ puttokens(trp); /* 输出当前行 */ anymacros = 0; cursource->line += cursource->lineinc; if (cursource->lineinc>1) { genline(); } } }
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; }
void doinclude(Tokenrow* trp) { char fname[256], iname[256]; Includelist* ip; int angled, len, fd, i; trp->tp += 1; if (trp->tp >= trp->lp) goto syntax; if (trp->tp->type != STRING && trp->tp->type != LT) { len = trp->tp - trp->bp; expandrow(trp, "<include>"); trp->tp = trp->bp + len; } if (trp->tp->type == STRING) { len = trp->tp->len - 2; if (len > sizeof(fname) - 1) len = sizeof(fname) - 1; strncpy(fname, (char*)trp->tp->t + 1, len); angled = 0; } else if (trp->tp->type == LT) { len = 0; trp->tp++; while (trp->tp->type != GT) { if (trp->tp > trp->lp || len + trp->tp->len + 2 >= sizeof(fname)) goto syntax; strncpy(fname + len, (char*)trp->tp->t, trp->tp->len); len += trp->tp->len; trp->tp++; } angled = 1; } else goto syntax; trp->tp += 2; if (trp->tp < trp->lp || len == 0) goto syntax; fname[len] = '\0'; appendDirToIncludeList(basepath(fname)); if (fname[0] == '/') { fd = open(fname, 0); strcpy(iname, fname); } else for (fd = -1, i = NINCLUDE - 1; i >= 0; i--) { ip = &includelist[i]; if (ip->file == NULL || ip->deleted || (angled && ip->always == 0)) continue; if (strlen(fname) + strlen(ip->file) + 2 > sizeof(iname)) continue; strcpy(iname, ip->file); strcat(iname, "/"); strcat(iname, fname); if ((fd = open(iname, 0)) >= 0) break; } if (Mflag > 1 || (!angled && Mflag == 1)) { write(1, objname, strlen(objname)); write(1, iname, strlen(iname)); write(1, "\n", 1); } if (fd >= 0) { if (++incdepth > 10) error(FATAL, "#include too deeply nested"); setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL); genline(); } else { trp->tp = trp->bp + 2; error(ERROR, "Could not find include file %r", trp); } return; syntax: error(ERROR, "Syntax error in #include"); return; }