c语言realine6,Nasm源代码解析 预处理器--readline(六)

/*

* Read a line from the top file in istk, handling multiple CR/LFs

* at the end of the line read, and handling spurious ^Zs. Will

* return lines from the standard macro set if this has not already

* been done.

*/

/*

* 先处理nasm自己定义的宏定义,等宏定义处理完之后在处理

*/

static char *read_line(void)

{

char *buffer, *p, *q;

int bufsize, continued_count;

if (stdmacpos)

{

if (*stdmacpos)

{

char *ret = nasm_strdup(*stdmacpos++);

if (!*stdmacpos && any_extrastdmac) /* 判断是否到达stdmacpos最后一个参数,并且是否有额外的宏要处理 */

{

stdmacpos = extrastdmac;

any_extrastdmac = FALSE;

return ret;

}

/*

* Nasty hack: here we push the contents of `predef' on

* to the top-level expansion stack, since this is the

* most convenient way to implement the pre-include and

* pre-define features.

*/

if (!*stdmacpos) /* 宏定义是否处理完了 */

{

Line *pd, *l;

Token *head, **tail, *t;

/* 将 predef(static Line *) 链表复制到一份副本到 istk->expansion,但是其总的l->finishes = FALSE(bug?逻辑上应该是l->finishes = NULL) */

for (pd = predef; pd; pd = pd->next)

{

head = NULL;

tail = &head;

for (t = pd->first; t; t = t->next) /* 复制pd->first(Token *)链表 */

{

*tail = new_Token(NULL, t->type, t->text, 0);

tail = &(*tail)->next;

}

l = nasm_malloc(sizeof(Line));

l->next = istk->expansion; /* 插入链表头 */

l->first = head;

l->finishes = FALSE;

istk->expansion = l;

}

}

return ret;

}

else

{

stdmacpos = NULL;

}

}

bufsize = BUF_DELTA; /* #define BUF_DELTA 512 */

buffer = nasm_malloc(BUF_DELTA); /* 建立缓冲区 */

p = buffer;

continued_count = 0; /* 与处理行号有关 */

while (1) {

q = fgets(p, bufsize - (p - buffer), istk->fp); /* 行输入 */

if (!q)

break;

p += strlen(p);

/* 将通过续行符分开的代码语句合并成单行的一条语句 */

if (p > buffer && p[-1] == '\n') {

/* Convert backslash-CRLF line continuation sequences into

nothing at all (for DOS and Windows) */

if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {

p -= 3;

*p = 0;

continued_count++;

}

/* Also convert backslash-LF line continuation sequences into

nothing at all (for Unix) */

else if (((p - 1) > buffer) && (p[-2] == '\\')) {

p -= 2;

*p = 0;

continued_count++;

} else {

break;

}

}

if (p - buffer > bufsize - 10) { /* 如果缓冲区不充足,则在原来大小的基础上 +BUF_DELTA */

long offset = p - buffer;

bufsize += BUF_DELTA;

buffer = nasm_realloc(buffer, bufsize);

p = buffer + offset; /* prevent stale-pointer problems */

}

}

if (!q && p == buffer) {

nasm_free(buffer);

return NULL;

}

src_set_linnum(src_get_linnum() + istk->lineinc + /* 改变行号 */

(continued_count * istk->lineinc));

/*

* Play safe: remove CRs as well as LFs, if any of either are

* present at the end of the line.

*/

while (--p >= buffer && (*p == '\n' || *p == '\r'))

*p = '\0';

/*

* Handle spurious ^Z, which may be inserted into source files

* by some file transfer utilities.

*/

buffer[strcspn(buffer, "\032")] = '\0';

list->line(LIST_READ, buffer); /* listing.c 中实现 */

return buffer;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值