Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
/* $XConsortium: cppsetup.c /main/15 1995/12/08 18:26:19 gildea $ */
2
/*
3
Copyright (c) 1993, 1994 X Consortium
4
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
of this software and associated documentation files (the "Software"), to deal
7
in the Software without restriction, including without limitation the rights
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the Software is
10
furnished to do so, subject to the following conditions:
11
 
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
14
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
 
22
Except as contained in this notice, the name of the X Consortium shall not be
23
used in advertising or otherwise to promote the sale, use or other dealings
24
in this Software without prior written authorization from the X Consortium.
25
 
26
*/
27
 
28
#include "def.h"
29
 
30
#ifdef  CPP
31
/*
32
 * This file is strictly for the sake of cpy.y and yylex.c (if
33
 * you indeed have the source for cpp).
34
 */
35
#define IB      1
36
#define SB      2
37
#define NB      4
38
#define CB      8
39
#define QB      16
40
#define WB      32
41
#define SALT    '#'
42
#if pdp11 | vax | ns16000 | mc68000 | ibm032
43
#define COFF    128
44
#else
45
#define COFF    0
46
#endif
47
/*
48
 * These variables used by cpy.y and yylex.c
49
 */
50
extern char     *outp, *inp, *newp, *pend;
51
extern char     *ptrtab;
52
extern char     fastab[];
53
extern char     slotab[];
54
 
55
/*
56
 * cppsetup
57
 */
58
struct filepointer      *currentfile;
59
struct inclist          *currentinc;
60
 
61
cppsetup(line, filep, inc)
62
        register char   *line;
63
        register struct filepointer     *filep;
64
        register struct inclist         *inc;
65
{
66
        register char *p, savec;
67
        static boolean setupdone = FALSE;
68
        boolean value;
69
 
70
        if (!setupdone) {
71
                cpp_varsetup();
72
                setupdone = TRUE;
73
        }
74
 
75
        currentfile = filep;
76
        currentinc = inc;
77
        inp = newp = line;
78
        for (p=newp; *p; p++)
79
                ;
80
 
81
        /*
82
         * put a newline back on the end, and set up pend, etc.
83
         */
84
        *p++ = '\n';
85
        savec = *p;
86
        *p = '\0';
87
        pend = p;
88
 
89
        ptrtab = slotab+COFF;
90
        *--inp = SALT; 
91
        outp=inp; 
92
        value = yyparse();
93
        *p = savec;
94
        return(value);
95
}
96
 
97
struct symtab **lookup(symbol)
98
        char    *symbol;
99
{
100
        static struct symtab    *undefined;
101
        struct symtab   **sp;
102
 
103
        sp = isdefined(symbol, currentinc, NULL);
104
        if (sp == NULL) {
105
                sp = &undefined;
106
                (*sp)->s_value = NULL;
107
        }
108
        return (sp);
109
}
110
 
111
pperror(tag, x0,x1,x2,x3,x4)
112
        int     tag,x0,x1,x2,x3,x4;
113
{
114
        warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
115
        warning(x0,x1,x2,x3,x4);
116
}
117
 
118
 
119
yyerror(s)
120
        register char   *s;
121
{
122
        fatalerr("Fatal error: %s\n", s);
123
}
124
#else /* not CPP */
125
 
126
#include "ifparser.h"
127
struct _parse_data {
128
    struct filepointer *filep;
129
    struct inclist *inc;
130
    const char *line;
131
};
132
 
133
static const char *
134
_my_if_errors (ip, cp, expecting)
135
    IfParser *ip;
136
    const char *cp;
137
    const char *expecting;
138
{
139
    struct _parse_data *pd = (struct _parse_data *) ip->data;
140
    int lineno = pd->filep->f_line;
141
    char *filename = pd->inc->i_file;
142
    char prefix[300];
143
    int prefixlen;
144
    int i;
145
 
146
    sprintf (prefix, "\"%s\":%d", filename, lineno);
147
    prefixlen = strlen(prefix);
148
    fprintf (stderr, "%s:  %s", prefix, pd->line);
149
    i = cp - pd->line;
150
    if (i > 0 && pd->line[i-1] != '\n') {
151
        putc ('\n', stderr);
152
    }
153
    for (i += prefixlen + 3; i > 0; i--) {
154
        putc (' ', stderr);
155
    }
156
    fprintf (stderr, "^--- expecting %s\n", expecting);
157
    return NULL;
158
}
159
 
160
 
161
static struct symtab **
162
_lookup_variable (ip, var, len)
163
    IfParser *ip;
164
    const char *var;
165
    int len;
166
{
167
    char tmpbuf[MAX_PATH + 1];
168
    struct _parse_data *pd = (struct _parse_data *) ip->data;
169
 
170
    if (len > MAX_PATH)
171
        return 0;
172
 
173
    strncpy (tmpbuf, var, len);
174
    tmpbuf[len] = '\0';
175
    return isdefined (tmpbuf, pd->inc, NULL);
176
}
177
 
178
 
179
static int
180
_my_eval_defined (ip, var, len)
181
    IfParser *ip;
182
    const char *var;
183
    int len;
184
{
185
    if (_lookup_variable (ip, var, len))
186
        return 1;
187
    else
188
        return 0;
189
}
190
 
191
#define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
192
 
193
static int
194
_my_eval_variable (ip, var, len)
195
    IfParser *ip;
196
    const char *var;
197
    int len;
198
{
199
    struct symtab **s;
200
 
201
    s = _lookup_variable (ip, var, len);
202
    if (!s)
203
        return 0;
204
    do {
205
        var = (*s)->s_value;
206
        if (!isvarfirstletter(*var))
207
            break;
208
        s = _lookup_variable (ip, var, strlen(var));
209
    } while (s);
210
 
211
    return atoi(var);
212
}
213
 
214
 
215
cppsetup(line, filep, inc)
216
        register char   *line;
217
        register struct filepointer     *filep;
218
        register struct inclist         *inc;
219
{
220
    IfParser ip;
221
    struct _parse_data pd;
222
    long val = 0;
223
 
224
    pd.filep = filep;
225
    pd.inc = inc;
226
    pd.line = line;
227
    ip.funcs.handle_error = _my_if_errors;
228
    ip.funcs.eval_defined = _my_eval_defined;
229
    ip.funcs.eval_variable = _my_eval_variable;
230
    ip.data = (char *) &pd;
231
 
232
    (void) ParseIfExpression (&ip, line, &val);
233
    if (val)
234
        return IF;
235
    else
236
        return IFFALSE;
237
}
238
#endif /* CPP */
239