#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include "cpp.h"
|
#include "cpp.h"
|
|
|
Includelist includelist[NINCLUDE];
|
Includelist includelist[NINCLUDE];
|
|
|
extern char *objname;
|
extern char *objname;
|
|
|
void
|
void
|
doinclude(Tokenrow *trp)
|
doinclude(Tokenrow *trp)
|
{
|
{
|
char fname[256], iname[256];
|
char fname[256], iname[256];
|
Includelist *ip;
|
Includelist *ip;
|
int angled, len, i;
|
int angled, len, i;
|
FILE *fd;
|
FILE *fd;
|
|
|
trp->tp += 1;
|
trp->tp += 1;
|
if (trp->tp>=trp->lp)
|
if (trp->tp>=trp->lp)
|
goto syntax;
|
goto syntax;
|
if (trp->tp->type!=STRING && trp->tp->type!=LT) {
|
if (trp->tp->type!=STRING && trp->tp->type!=LT) {
|
len = trp->tp - trp->bp;
|
len = trp->tp - trp->bp;
|
expandrow(trp, "<include>");
|
expandrow(trp, "<include>");
|
trp->tp = trp->bp+len;
|
trp->tp = trp->bp+len;
|
}
|
}
|
if (trp->tp->type==STRING) {
|
if (trp->tp->type==STRING) {
|
len = trp->tp->len-2;
|
len = trp->tp->len-2;
|
if (len > sizeof(fname) - 1)
|
if (len > sizeof(fname) - 1)
|
len = sizeof(fname) - 1;
|
len = sizeof(fname) - 1;
|
strncpy(fname, (char*)trp->tp->t+1, len);
|
strncpy(fname, (char*)trp->tp->t+1, len);
|
angled = 0;
|
angled = 0;
|
} else if (trp->tp->type==LT) {
|
} else if (trp->tp->type==LT) {
|
len = 0;
|
len = 0;
|
trp->tp++;
|
trp->tp++;
|
while (trp->tp->type!=GT) {
|
while (trp->tp->type!=GT) {
|
if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname))
|
if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname))
|
goto syntax;
|
goto syntax;
|
strncpy(fname+len, (char*)trp->tp->t, trp->tp->len);
|
strncpy(fname+len, (char*)trp->tp->t, trp->tp->len);
|
len += trp->tp->len;
|
len += trp->tp->len;
|
trp->tp++;
|
trp->tp++;
|
}
|
}
|
angled = 1;
|
angled = 1;
|
} else
|
} else
|
goto syntax;
|
goto syntax;
|
trp->tp += 2;
|
trp->tp += 2;
|
if (trp->tp < trp->lp || len==0)
|
if (trp->tp < trp->lp || len==0)
|
goto syntax;
|
goto syntax;
|
fname[len] = '\0';
|
fname[len] = '\0';
|
if (fname[0]=='/') {
|
if (fname[0]=='/') {
|
fd = fopen(fname, "r");
|
fd = fopen(fname, "r");
|
strcpy(iname, fname);
|
strcpy(iname, fname);
|
} else for (fd = NULL,i=NINCLUDE-1; i>=0; i--) {
|
} else for (fd = NULL,i=NINCLUDE-1; i>=0; i--) {
|
ip = &includelist[i];
|
ip = &includelist[i];
|
if (ip->file==NULL || ip->deleted || (angled && ip->always==0))
|
if (ip->file==NULL || ip->deleted || (angled && ip->always==0))
|
continue;
|
continue;
|
if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname))
|
if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname))
|
continue;
|
continue;
|
strcpy(iname, ip->file);
|
strcpy(iname, ip->file);
|
strcat(iname, "/");
|
strcat(iname, "/");
|
strcat(iname, fname);
|
strcat(iname, fname);
|
if ((fd = fopen(iname, "r")) != NULL)
|
if ((fd = fopen(iname, "r")) != NULL)
|
break;
|
break;
|
}
|
}
|
if ( Mflag>1 || !angled&&Mflag==1 ) {
|
if ( Mflag>1 || !angled&&Mflag==1 ) {
|
fwrite(objname,1,strlen(objname),stdout);
|
fwrite(objname,1,strlen(objname),stdout);
|
fwrite(iname,1,strlen(iname),stdout);
|
fwrite(iname,1,strlen(iname),stdout);
|
fwrite("\n",1,1,stdout);
|
fwrite("\n",1,1,stdout);
|
}
|
}
|
if (fd != NULL) {
|
if (fd != NULL) {
|
if (++incdepth > 10)
|
if (++incdepth > 10)
|
error(FATAL, "#include too deeply nested");
|
error(FATAL, "#include too deeply nested");
|
setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
|
setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
|
genline();
|
genline();
|
} else {
|
} else {
|
trp->tp = trp->bp+2;
|
trp->tp = trp->bp+2;
|
error(ERROR, "Could not find include file %r", trp);
|
error(ERROR, "Could not find include file %r", trp);
|
}
|
}
|
return;
|
return;
|
syntax:
|
syntax:
|
error(ERROR, "Syntax error in #include");
|
error(ERROR, "Syntax error in #include");
|
return;
|
return;
|
}
|
}
|
|
|
/*
|
/*
|
* Generate a line directive for cursource
|
* Generate a line directive for cursource
|
*/
|
*/
|
void
|
void
|
genline(void)
|
genline(void)
|
{
|
{
|
static Token ta = { UNCLASS };
|
static Token ta = { UNCLASS };
|
static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
|
static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
|
uchar *p;
|
uchar *p;
|
|
|
ta.t = p = (uchar*)outp;
|
ta.t = p = (uchar*)outp;
|
strcpy((char*)p, "#line ");
|
strcpy((char*)p, "#line ");
|
p += sizeof("#line ")-1;
|
p += sizeof("#line ")-1;
|
p = (uchar*)outnum((char*)p, cursource->line);
|
p = (uchar*)outnum((char*)p, cursource->line);
|
*p++ = ' '; *p++ = '"';
|
*p++ = ' '; *p++ = '"';
|
strcpy((char*)p, cursource->filename);
|
strcpy((char*)p, cursource->filename);
|
p += strlen((char*)p);
|
p += strlen((char*)p);
|
*p++ = '"'; *p++ = '\n';
|
*p++ = '"'; *p++ = '\n';
|
ta.len = (char*)p-outp;
|
ta.len = (char*)p-outp;
|
outp = (char*)p;
|
outp = (char*)p;
|
tr.tp = tr.bp;
|
tr.tp = tr.bp;
|
puttokens(&tr);
|
puttokens(&tr);
|
}
|
}
|
|
|
void
|
void
|
setobjname(char *f)
|
setobjname(char *f)
|
{
|
{
|
int n = strlen(f);
|
int n = strlen(f);
|
objname = (char*)domalloc(n+5);
|
objname = (char*)domalloc(n+5);
|
strcpy(objname,f);
|
strcpy(objname,f);
|
if(objname[n-2]=='.'){
|
if(objname[n-2]=='.'){
|
strcpy(objname+n-1,"$O: ");
|
strcpy(objname+n-1,"$O: ");
|
}else{
|
}else{
|
strcpy(objname+n,"$O: ");
|
strcpy(objname+n,"$O: ");
|
}
|
}
|
}
|
}
|
|
|