1 #include <u.h> 2 #include <libc.h> 3 #include "cpp.h" 4 5 Includelist includelist[NINCLUDE] = { 6 { 0, 1, "/usr/include" }, 7 }; 8 9 char *objname; 10 11 void 12 doinclude(Tokenrow *trp) 13 { 14 char fname[256], iname[256]; 15 Includelist *ip; 16 int angled, len, fd, i; 17 18 trp->tp += 1; 19 if (trp->tp>=trp->lp) 20 goto syntax; 21 if (trp->tp->type!=STRING && trp->tp->type!=LT) { 22 len = trp->tp - trp->bp; 23 expandrow(trp, "<include>"); 24 trp->tp = trp->bp+len; 25 } 26 if (trp->tp->type==STRING) { 27 len = trp->tp->len-2; 28 if (len > sizeof(fname) - 1) 29 len = sizeof(fname) - 1; 30 strncpy(fname, (char*)trp->tp->t+1, len); 31 angled = 0; 32 } else { 33 len = 0; 34 trp->tp++; 35 while (trp->tp->type!=GT) { 36 if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname)) 37 goto syntax; 38 strncpy(fname+len, (char*)trp->tp->t, trp->tp->len); 39 len += trp->tp->len; 40 trp->tp++; 41 } 42 angled = 1; 43 } 44 trp->tp += 2; 45 if (trp->tp < trp->lp || len==0) 46 goto syntax; 47 fname[len] = '\0'; 48 if (fname[0]=='/') { 49 fd = open(fname, 0); 50 strcpy(iname, fname); 51 } else for (fd=-1,i=NINCLUDE-1; i>=0; i--) { 52 ip = &includelist[i]; 53 if (ip->file==NULL || ip->deleted || (angled && ip->always==0)) 54 continue; 55 if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname)) 56 continue; 57 strcpy(iname, ip->file); 58 strcat(iname, "/"); 59 strcat(iname, fname); 60 if ((fd = open(iname, 0)) >= 0) 61 break; 62 } 63 if ( Mflag>1 || !angled&&Mflag==1 ) { 64 write(1,objname,strlen(objname)); 65 write(1,iname,strlen(iname)); 66 write(1,"\n",1); 67 } 68 if (fd >= 0) { 69 if (++incdepth > 10) 70 error(FATAL, "#include too deeply nested"); 71 setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL); 72 genline(); 73 } else { 74 trp->tp = trp->bp+2; 75 error(ERROR, "Could not find include file %r", trp); 76 } 77 return; 78 syntax: 79 error(ERROR, "Syntax error in #include"); 80 return; 81 } 82 83 /* 84 * Generate a line directive for cursource 85 */ 86 void 87 genline(void) 88 { 89 static Token ta = { UNCLASS, NULL, 0, 0 }; 90 static Tokenrow tr = { &ta, &ta, &ta+1, 1 }; 91 uchar *p; 92 93 ta.t = p = (uchar*)outp; 94 strcpy((char*)p, "#line "); 95 p += sizeof("#line ")-1; 96 p = (uchar*)outnum((char*)p, cursource->line); 97 *p++ = ' '; *p++ = '"'; 98 strcpy((char*)p, cursource->filename); 99 p += strlen((char*)p); 100 *p++ = '"'; *p++ = '\n'; 101 ta.len = (char*)p-outp; 102 outp = (char*)p; 103 tr.tp = tr.bp; 104 puttokens(&tr); 105 } 106 107 void 108 setobjname(char *f) 109 { 110 int n = strlen(f); 111 objname = (char*)domalloc(n+5); 112 strcpy(objname,f); 113 if(objname[n-2]=='.'){ 114 strcpy(objname+n-1,"$O: "); 115 }else{ 116 strcpy(objname+n,"$O: "); 117 } 118 } 119