dtudcfonted: Resolve CID 86280
[oweals/cde.git] / cde / config / makedepend / cppsetup.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $TOG: cppsetup.c /main/18 1998/02/06 11:09:35 kaleb $ */
24 /*
25
26 Copyright (c) 1993, 1994, 1998  The Open Group
27
28 All Rights Reserved.
29
30 The above copyright notice and this permission notice shall be included in
31 all copies or substantial portions of the Software.
32
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
36 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
37 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
38 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
40 Except as contained in this notice, the name of The Open Group shall not be
41 used in advertising or otherwise to promote the sale, use or other dealings
42 in this Software without prior written authorization from The Open Group.
43
44 */
45
46 #include "def.h"
47
48 #ifdef  CPP
49 /*
50  * This file is strictly for the sake of cpy.y and yylex.c (if
51  * you indeed have the source for cpp).
52  */
53 #define IB 1
54 #define SB 2
55 #define NB 4
56 #define CB 8
57 #define QB 16
58 #define WB 32
59 #define SALT '#'
60 #if pdp11 | vax | ns16000 | mc68000 | ibm032
61 #define COFF 128
62 #else
63 #define COFF 0
64 #endif
65 /*
66  * These variables used by cpy.y and yylex.c
67  */
68 extern char     *outp, *inp, *newp, *pend;
69 extern char     *ptrtab;
70 extern char     fastab[];
71 extern char     slotab[];
72
73 /*
74  * cppsetup
75  */
76 struct filepointer      *currentfile;
77 struct inclist          *currentinc;
78
79 cppsetup(line, filep, inc)
80         register char   *line;
81         register struct filepointer     *filep;
82         register struct inclist         *inc;
83 {
84         register char *p, savec;
85         static boolean setupdone = FALSE;
86         boolean value;
87
88         if (!setupdone) {
89                 cpp_varsetup();
90                 setupdone = TRUE;
91         }
92
93         currentfile = filep;
94         currentinc = inc;
95         inp = newp = line;
96         for (p=newp; *p; p++)
97                 ;
98
99         /*
100          * put a newline back on the end, and set up pend, etc.
101          */
102         *p++ = '\n';
103         savec = *p;
104         *p = '\0';
105         pend = p;
106
107         ptrtab = slotab+COFF;
108         *--inp = SALT; 
109         outp=inp; 
110         value = yyparse();
111         *p = savec;
112         return(value);
113 }
114
115 struct symtab **lookup(symbol)
116         char    *symbol;
117 {
118         static struct symtab    *undefined;
119         struct symtab   **sp;
120
121         sp = isdefined(symbol, currentinc, NULL);
122         if (sp == NULL) {
123                 sp = &undefined;
124                 (*sp)->s_value = NULL;
125         }
126         return (sp);
127 }
128
129 pperror(tag, x0,x1,x2,x3,x4)
130         int     tag,x0,x1,x2,x3,x4;
131 {
132         warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
133         warning(x0,x1,x2,x3,x4);
134 }
135
136
137 yyerror(s)
138         register char   *s;
139 {
140         fatalerr("Fatal error: %s\n", s);
141 }
142 #else /* not CPP */
143
144 #include "ifparser.h"
145 struct _parse_data {
146     struct filepointer *filep;
147     struct inclist *inc;
148     const char *line;
149 };
150
151 static const char *
152 my_if_errors (ip, cp, expecting)
153     IfParser *ip;
154     const char *cp;
155     const char *expecting;
156 {
157     struct _parse_data *pd = (struct _parse_data *) ip->data;
158     int lineno = pd->filep->f_line;
159     char *filename = pd->inc->i_file;
160     char prefix[300];
161     int prefixlen;
162     int i;
163
164     snprintf (prefix, 300, "\"%s\":%d", filename, lineno);
165     prefixlen = strlen(prefix);
166     fprintf (stderr, "%s:  %s", prefix, pd->line);
167     i = cp - pd->line;
168     if (i > 0 && pd->line[i-1] != '\n') {
169         putc ('\n', stderr);
170     }
171     for (i += prefixlen + 3; i > 0; i--) {
172         putc (' ', stderr);
173     }
174     fprintf (stderr, "^--- expecting %s\n", expecting);
175     return NULL;
176 }
177
178
179 #define MAXNAMELEN 256
180
181 static struct symtab **
182 lookup_variable (ip, var, len)
183     IfParser *ip;
184     const char *var;
185     int len;
186 {
187     char tmpbuf[MAXNAMELEN + 1];
188     struct _parse_data *pd = (struct _parse_data *) ip->data;
189
190     if (len > MAXNAMELEN)
191         return 0;
192
193     strncpy (tmpbuf, var, len);
194     tmpbuf[len] = '\0';
195     return isdefined (tmpbuf, pd->inc, NULL);
196 }
197
198
199 static int
200 my_eval_defined (ip, var, len)
201     IfParser *ip;
202     const char *var;
203     int len;
204 {
205     if (lookup_variable (ip, var, len))
206         return 1;
207     else
208         return 0;
209 }
210
211 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
212
213 static long
214 my_eval_variable (ip, var, len)
215     IfParser *ip;
216     const char *var;
217     int len;
218 {
219     long val;
220     struct symtab **s;
221
222     s = lookup_variable (ip, var, len);
223     if (!s)
224         return 0;
225     do {
226         var = (*s)->s_value;
227         if (!isvarfirstletter((int)*var) || !strcmp((*s)->s_name, var))
228             break;
229         s = lookup_variable (ip, var, strlen(var));
230     } while (s);
231
232     var = ParseIfExpression(ip, var, &val);
233     if (var && *var) debug(4, ("extraneous: '%s'\n", var));
234     return val;
235 }
236
237
238 int cppsetup(line, filep, inc)
239         register char   *line;
240         register struct filepointer     *filep;
241         register struct inclist         *inc;
242 {
243     IfParser ip;
244     struct _parse_data pd;
245     long val = 0;
246
247     pd.filep = filep;
248     pd.inc = inc;
249     pd.line = line;
250     ip.funcs.handle_error = my_if_errors;
251     ip.funcs.eval_defined = my_eval_defined;
252     ip.funcs.eval_variable = my_eval_variable;
253     ip.data = (char *) &pd;
254
255     (void) ParseIfExpression (&ip, line, &val);
256     if (val)
257         return IF;
258     else
259         return IFFALSE;
260 }
261 #endif /* CPP */
262