cppsetup.c revision 079e7944
1/*
2
3Copyright (c) 1993, 1994, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27#include "def.h"
28
29#include "ifparser.h"
30
31struct _parse_data {
32    struct filepointer *filep;
33    struct inclist *inc;
34    const char *filename;
35    const char *line;
36};
37
38static const char *
39my_if_errors (IfParser *ip, const char *cp, const char *expecting)
40{
41    struct _parse_data *pd = (struct _parse_data *) ip->data;
42    int lineno = pd->filep->f_line;
43    const char *filename = pd->filename;
44    char prefix[300];
45    int prefixlen;
46    int i;
47
48    snprintf (prefix, sizeof(prefix), "\"%s\":%d", filename, lineno);
49    prefixlen = strlen(prefix);
50    fprintf (stderr, "%s:  %s", prefix, pd->line);
51    i = cp - pd->line;
52    if (i > 0 && pd->line[i-1] != '\n') {
53	putc ('\n', stderr);
54    }
55    for (i += prefixlen + 3; i > 0; i--) {
56	putc (' ', stderr);
57    }
58    fprintf (stderr, "^--- expecting %s\n", expecting);
59    return NULL;
60}
61
62
63#define MAXNAMELEN 256
64
65static struct symtab **
66lookup_variable (IfParser *ip, const char *var, int len)
67{
68    char tmpbuf[MAXNAMELEN + 1];
69    struct _parse_data *pd = (struct _parse_data *) ip->data;
70
71    if (len > MAXNAMELEN)
72	return NULL;
73
74    strncpy (tmpbuf, var, len);
75    tmpbuf[len] = '\0';
76    return isdefined (tmpbuf, pd->inc, NULL);
77}
78
79
80static int
81my_eval_defined (IfParser *ip, const char *var, int len)
82{
83    if (lookup_variable (ip, var, len))
84	return 1;
85    else
86	return 0;
87}
88
89#define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
90
91static long
92my_eval_variable (IfParser *ip, const char *var, int len)
93{
94    long val;
95    struct symtab **s;
96
97    s = lookup_variable (ip, var, len);
98    if (!s)
99	return 0;
100    do {
101	var = (*s)->s_value;
102	if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var))
103	    break;
104	s = lookup_variable (ip, var, strlen(var));
105    } while (s);
106
107    var = ParseIfExpression(ip, var, &val);
108    if (var && *var) debug(4, ("extraneous: '%s'\n", var));
109    return val;
110}
111
112int
113cppsetup(const char *filename,
114	 const char *line,
115	 struct filepointer *filep,
116	 struct inclist *inc)
117{
118    IfParser ip;
119    struct _parse_data pd;
120    long val = 0;
121
122    pd.filep = filep;
123    pd.inc = inc;
124    pd.line = line;
125    pd.filename = filename;
126    ip.funcs.handle_error = my_if_errors;
127    ip.funcs.eval_defined = my_eval_defined;
128    ip.funcs.eval_variable = my_eval_variable;
129    ip.data = (char *) &pd;
130
131    (void) ParseIfExpression (&ip, line, &val);
132    if (val)
133	return IF;
134    else
135	return IFFALSE;
136}
137
138