* added compilers lcc and bcc (linux86)
[mascara-docs.git] / compilers / lcc / cpp / include.c
blob1bb8847538ce00979914e00faf5c3702c587548a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "cpp.h"
6 Includelist includelist[NINCLUDE];
8 extern char *objname;
10 void appendDirToIncludeList( char *dir )
12 int i;
13 char *fqdir;
15 fqdir = (char *)newstring( (uchar *)includelist[NINCLUDE-1].file, 256, 0 );
16 strcat( fqdir, "/" );
17 strcat( fqdir, dir );
19 //avoid adding it more than once
20 for (i=NINCLUDE-2; i>=0; i--) {
21 if (includelist[i].file &&
22 !strcmp (includelist[i].file, fqdir)) {
23 return;
27 for (i=NINCLUDE-2; i>=0; i--) {
28 if (includelist[i].file==NULL) {
29 includelist[i].always = 1;
30 includelist[i].file = fqdir;
31 break;
34 if (i<0)
35 error(FATAL, "Too many -I directives");
38 void
39 doinclude(Tokenrow *trp)
41 char fname[256], iname[256];
42 Includelist *ip;
43 int angled, len, fd, i;
45 trp->tp += 1;
46 if (trp->tp>=trp->lp)
47 goto syntax;
48 if (trp->tp->type!=STRING && trp->tp->type!=LT) {
49 len = trp->tp - trp->bp;
50 expandrow(trp, "<include>");
51 trp->tp = trp->bp+len;
53 if (trp->tp->type==STRING) {
54 len = trp->tp->len-2;
55 if (len > sizeof(fname) - 1)
56 len = sizeof(fname) - 1;
57 strncpy(fname, (char*)trp->tp->t+1, len);
58 angled = 0;
59 } else if (trp->tp->type==LT) {
60 len = 0;
61 trp->tp++;
62 while (trp->tp->type!=GT) {
63 if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname))
64 goto syntax;
65 strncpy(fname+len, (char*)trp->tp->t, trp->tp->len);
66 len += trp->tp->len;
67 trp->tp++;
69 angled = 1;
70 } else
71 goto syntax;
72 trp->tp += 2;
73 if (trp->tp < trp->lp || len==0)
74 goto syntax;
75 fname[len] = '\0';
77 appendDirToIncludeList( basepath( fname ) );
79 if (fname[0]=='/') {
80 fd = open(fname, 0);
81 strcpy(iname, fname);
82 } else for (fd = -1,i=NINCLUDE-1; i>=0; i--) {
83 ip = &includelist[i];
84 if (ip->file==NULL || ip->deleted || (angled && ip->always==0))
85 continue;
86 if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname))
87 continue;
88 strcpy(iname, ip->file);
89 strcat(iname, "/");
90 strcat(iname, fname);
91 if ((fd = open(iname, 0)) >= 0)
92 break;
94 if ( Mflag>1 || (!angled&&Mflag==1) ) {
95 write(1,objname,strlen(objname));
96 write(1,iname,strlen(iname));
97 write(1,"\n",1);
99 if (fd >= 0) {
100 if (++incdepth > 10)
101 error(FATAL, "#include too deeply nested");
102 setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
103 genline();
104 } else {
105 trp->tp = trp->bp+2;
106 error(ERROR, "Could not find include file %r", trp);
108 return;
109 syntax:
110 error(ERROR, "Syntax error in #include");
111 return;
115 * Generate a line directive for cursource
117 void
118 genline(void)
120 static Token ta = { UNCLASS };
121 static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
122 uchar *p;
124 ta.t = p = (uchar*)outp;
125 strcpy((char*)p, "#line ");
126 p += sizeof("#line ")-1;
127 p = (uchar*)outnum((char*)p, cursource->line);
128 *p++ = ' '; *p++ = '"';
129 if (cursource->filename[0]!='/' && wd[0]) {
130 strcpy((char*)p, wd);
131 p += strlen(wd);
132 *p++ = '/';
134 strcpy((char*)p, cursource->filename);
135 p += strlen((char*)p);
136 *p++ = '"'; *p++ = '\n';
137 ta.len = (char*)p-outp;
138 outp = (char*)p;
139 tr.tp = tr.bp;
140 puttokens(&tr);
143 void
144 setobjname(char *f)
146 int n = strlen(f);
147 objname = (char*)domalloc(n+5);
148 strcpy(objname,f);
149 if(objname[n-2]=='.'){
150 strcpy(objname+n-1,"$O: ");
151 }else{
152 strcpy(objname+n,"$O: ");