六. C語言前處理器LCC-CPP之include.c程式碼分析
阿新 • • 發佈:2019-02-18
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;
}
//#include "xxx"
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;
}
//#include <xxx>
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;
//把<>內部的字串copy出來.
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';
// do fopen if it is absolute path
//絕地路徑.
if (fname[0]=='/' || fname[0]=='\\' ||
(fname[1]==':' &&
(fname[0]>='a' && fname[0]<='z' ||
fname[0]>='A' && fname[0]<='Z')
)
)
{
OutputDebugString(fname);
OutputDebugString("\n");
//直接開啟.
fd = fopen(fname, "r");
strcpy(iname, fname);
}
else
{
if (!angled && cursource->filename)
{ // 在當前路徑查詢.
// search in current file directory first
Source *s = cursource;
char *p;
p = strrchr(s->filename,'/');
if (!p)
p = strrchr(s->filename,'\\');
if (p)
{
strncpy(iname,s->filename,p - s->filename);
iname[p - s->filename] = 0;
strcat(iname, "\\");
strcat(iname, fname);
OutputDebugString(iname);
OutputDebugString("\n");
if ((fd = fopen(iname, "r")) != NULL)
goto found_file;
}
}
for (fd = NULL,i=NINCLUDE-1; i>=0; i--)
{ //從所有-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);
OutputDebugString(iname);
OutputDebugString("\n");
//找到誰算誰.
if ((fd = fopen(iname, "r")) != NULL)
break;
}
}
found_file:
if (/* Mflag>1 || */!angled && Mflag == 2) //列印依賴檔案.
{
// fwrite(objname,1,strlen(objname),stdout);
fwrite(iname,1,strlen(iname),stdout);
fwrite("
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;
}
//#include "xxx"
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;
}
//#include <xxx>
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;
//把<>內部的字串copy出來.
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';
// do fopen if it is absolute path
//絕地路徑.
if (fname[0]=='/' || fname[0]=='\\' ||
(fname[1]==':' &&
(fname[0]>='a' && fname[0]<='z' ||
fname[0]>='A' && fname[0]<='Z')
)
)
{
OutputDebugString(fname);
OutputDebugString("\n");
//直接開啟.
fd = fopen(fname, "r");
strcpy(iname, fname);
}
else
{
if (!angled && cursource->filename)
{ // 在當前路徑查詢.
// search in current file directory first
Source *s = cursource;
char *p;
p = strrchr(s->filename,'/');
if (!p)
p = strrchr(s->filename,'\\');
if (p)
{
strncpy(iname,s->filename,p - s->filename);
iname[p - s->filename] = 0;
strcat(iname, "\\");
strcat(iname, fname);
OutputDebugString(iname);
OutputDebugString("\n");
if ((fd = fopen(iname, "r")) != NULL)
goto found_file;
}
}
for (fd = NULL,i=NINCLUDE-1; i>=0; i--)
{ //從所有-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);
OutputDebugString(iname);
OutputDebugString("\n");
//找到誰算誰.
if ((fd = fopen(iname, "r")) != NULL)
break;
}
}
found_file:
if (/* Mflag>1 || */!angled && Mflag == 2) //列印依賴檔案.
{
// fwrite(objname,1,strlen(objname),stdout);
fwrite(iname,1,strlen(iname),stdout);
fwrite("