OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [cpp/] [include.c] - Blame information for rev 243

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include "cpp.h"
5
 
6
Includelist     includelist[NINCLUDE];
7
 
8
extern char     *objname;
9
 
10
void
11
doinclude(Tokenrow *trp)
12
{
13
        char fname[256], iname[256];
14
        Includelist *ip;
15
        int angled, len, i;
16
        FILE *fd;
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 if (trp->tp->type==LT) {
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
        } else
44
                goto syntax;
45
        trp->tp += 2;
46
        if (trp->tp < trp->lp || len==0)
47
                goto syntax;
48
        fname[len] = '\0';
49
        if (fname[0]=='/') {
50
                fd = fopen(fname, "r");
51
                strcpy(iname, fname);
52
        } else for (fd = NULL,i=NINCLUDE-1; i>=0; i--) {
53
                ip = &includelist[i];
54
                if (ip->file==NULL || ip->deleted || (angled && ip->always==0))
55
                        continue;
56
                if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname))
57
                        continue;
58
                strcpy(iname, ip->file);
59
                strcat(iname, "/");
60
                strcat(iname, fname);
61
                if ((fd = fopen(iname, "r")) != NULL)
62
                        break;
63
        }
64
        if ( Mflag>1 || !angled&&Mflag==1 ) {
65
                fwrite(objname,1,strlen(objname),stdout);
66
                fwrite(iname,1,strlen(iname),stdout);
67
                fwrite("\n",1,1,stdout);
68
        }
69
        if (fd != NULL) {
70
                if (++incdepth > 10)
71
                        error(FATAL, "#include too deeply nested");
72
                setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
73
                genline();
74
        } else {
75
                trp->tp = trp->bp+2;
76
                error(ERROR, "Could not find include file %r", trp);
77
        }
78
        return;
79
syntax:
80
        error(ERROR, "Syntax error in #include");
81
        return;
82
}
83
 
84
/*
85
 * Generate a line directive for cursource
86
 */
87
void
88
genline(void)
89
{
90
        static Token ta = { UNCLASS };
91
        static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
92
        uchar *p;
93
 
94
        ta.t = p = (uchar*)outp;
95
        strcpy((char*)p, "#line ");
96
        p += sizeof("#line ")-1;
97
        p = (uchar*)outnum((char*)p, cursource->line);
98
        *p++ = ' '; *p++ = '"';
99
        strcpy((char*)p, cursource->filename);
100
        p += strlen((char*)p);
101
        *p++ = '"'; *p++ = '\n';
102
        ta.len = (char*)p-outp;
103
        outp = (char*)p;
104
        tr.tp = tr.bp;
105
        puttokens(&tr);
106
}
107
 
108
void
109
setobjname(char *f)
110
{
111
        int n = strlen(f);
112
        objname = (char*)domalloc(n+5);
113
        strcpy(objname,f);
114
        if(objname[n-2]=='.'){
115
                strcpy(objname+n-1,"$O: ");
116
        }else{
117
                strcpy(objname+n,"$O: ");
118
        }
119
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.