michael@0: /* $Xorg: cppsetup.c,v 1.5 2001/02/09 02:03:16 xorgcvs Exp $ */ michael@0: /* michael@0: michael@0: Copyright (c) 1993, 1994, 1998 The Open Group michael@0: michael@0: Permission to use, copy, modify, distribute, and sell this software and its michael@0: documentation for any purpose is hereby granted without fee, provided that michael@0: the above copyright notice appear in all copies and that both that michael@0: copyright notice and this permission notice appear in supporting michael@0: documentation. michael@0: michael@0: The above copyright notice and this permission notice shall be included in michael@0: all copies or substantial portions of the Software. michael@0: michael@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE michael@0: OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN michael@0: AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN michael@0: CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: Except as contained in this notice, the name of The Open Group shall not be michael@0: used in advertising or otherwise to promote the sale, use or other dealings michael@0: in this Software without prior written authorization from The Open Group. michael@0: michael@0: */ michael@0: /* $XFree86: xc/config/makedepend/cppsetup.c,v 3.11 2001/12/17 20:52:22 dawes Exp $ */ michael@0: michael@0: #include "def.h" michael@0: michael@0: #ifdef CPP michael@0: /* michael@0: * This file is strictly for the sake of cpy.y and yylex.c (if michael@0: * you indeed have the source for cpp). michael@0: */ michael@0: #define IB 1 michael@0: #define SB 2 michael@0: #define NB 4 michael@0: #define CB 8 michael@0: #define QB 16 michael@0: #define WB 32 michael@0: #define SALT '#' michael@0: #if defined(pdp11) || defined(vax) || defined(ns16000) || defined(mc68000) || defined(ibm032) michael@0: #define COFF 128 michael@0: #else michael@0: #define COFF 0 michael@0: #endif michael@0: /* michael@0: * These variables used by cpy.y and yylex.c michael@0: */ michael@0: extern char *outp, *inp, *newp, *pend; michael@0: extern char *ptrtab; michael@0: extern char fastab[]; michael@0: extern char slotab[]; michael@0: michael@0: /* michael@0: * cppsetup michael@0: */ michael@0: struct filepointer *currentfile; michael@0: struct inclist *currentinc; michael@0: michael@0: int michael@0: cppsetup(char *line, struct filepointer *filep, struct inclist *inc) michael@0: { michael@0: char *p, savec; michael@0: static boolean setupdone = FALSE; michael@0: boolean value; michael@0: michael@0: if (!setupdone) { michael@0: cpp_varsetup(); michael@0: setupdone = TRUE; michael@0: } michael@0: michael@0: currentfile = filep; michael@0: currentinc = inc; michael@0: inp = newp = line; michael@0: for (p=newp; *p; p++) michael@0: ; michael@0: michael@0: /* michael@0: * put a newline back on the end, and set up pend, etc. michael@0: */ michael@0: *p++ = '\n'; michael@0: savec = *p; michael@0: *p = '\0'; michael@0: pend = p; michael@0: michael@0: ptrtab = slotab+COFF; michael@0: *--inp = SALT; michael@0: outp=inp; michael@0: value = yyparse(); michael@0: *p = savec; michael@0: return(value); michael@0: } michael@0: michael@0: struct symtab **lookup(symbol) michael@0: char *symbol; michael@0: { michael@0: static struct symtab *undefined; michael@0: struct symtab **sp; michael@0: michael@0: sp = isdefined(symbol, currentinc, NULL); michael@0: if (sp == NULL) { michael@0: sp = &undefined; michael@0: (*sp)->s_value = NULL; michael@0: } michael@0: return (sp); michael@0: } michael@0: michael@0: pperror(tag, x0,x1,x2,x3,x4) michael@0: int tag,x0,x1,x2,x3,x4; michael@0: { michael@0: warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line); michael@0: warning(x0,x1,x2,x3,x4); michael@0: } michael@0: michael@0: michael@0: yyerror(s) michael@0: register char *s; michael@0: { michael@0: fatalerr("Fatal error: %s\n", s); michael@0: } michael@0: #else /* not CPP */ michael@0: michael@0: #include "ifparser.h" michael@0: struct _parse_data { michael@0: struct filepointer *filep; michael@0: struct inclist *inc; michael@0: char *filename; michael@0: const char *line; michael@0: }; michael@0: michael@0: static const char * michael@0: my_if_errors (IfParser *ip, const char *cp, const char *expecting) michael@0: { michael@0: struct _parse_data *pd = (struct _parse_data *) ip->data; michael@0: int lineno = pd->filep->f_line; michael@0: char *filename = pd->filename; michael@0: char prefix[300]; michael@0: int prefixlen; michael@0: int i; michael@0: michael@0: sprintf (prefix, "\"%s\":%d", filename, lineno); michael@0: prefixlen = strlen(prefix); michael@0: fprintf (stderr, "%s: %s", prefix, pd->line); michael@0: i = cp - pd->line; michael@0: if (i > 0 && pd->line[i-1] != '\n') { michael@0: putc ('\n', stderr); michael@0: } michael@0: for (i += prefixlen + 3; i > 0; i--) { michael@0: putc (' ', stderr); michael@0: } michael@0: fprintf (stderr, "^--- expecting %s\n", expecting); michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: #define MAXNAMELEN 256 michael@0: michael@0: static struct symtab ** michael@0: lookup_variable (IfParser *ip, const char *var, int len) michael@0: { michael@0: char tmpbuf[MAXNAMELEN + 1]; michael@0: struct _parse_data *pd = (struct _parse_data *) ip->data; michael@0: michael@0: if (len > MAXNAMELEN) michael@0: return 0; michael@0: michael@0: strncpy (tmpbuf, var, len); michael@0: tmpbuf[len] = '\0'; michael@0: return isdefined (tmpbuf, pd->inc, NULL); michael@0: } michael@0: michael@0: michael@0: static int michael@0: my_eval_defined (IfParser *ip, const char *var, int len) michael@0: { michael@0: if (lookup_variable (ip, var, len)) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: } michael@0: michael@0: #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_') michael@0: michael@0: static long michael@0: my_eval_variable (IfParser *ip, const char *var, int len) michael@0: { michael@0: long val; michael@0: struct symtab **s; michael@0: michael@0: s = lookup_variable (ip, var, len); michael@0: if (!s) michael@0: return 0; michael@0: do { michael@0: var = (*s)->s_value; michael@0: if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var)) michael@0: break; michael@0: s = lookup_variable (ip, var, strlen(var)); michael@0: } while (s); michael@0: michael@0: var = ParseIfExpression(ip, var, &val); michael@0: if (var && *var) debug(4, ("extraneous: '%s'\n", var)); michael@0: return val; michael@0: } michael@0: michael@0: int michael@0: cppsetup(char *filename, michael@0: char *line, michael@0: struct filepointer *filep, michael@0: struct inclist *inc) michael@0: { michael@0: IfParser ip; michael@0: struct _parse_data pd; michael@0: long val = 0; michael@0: michael@0: pd.filep = filep; michael@0: pd.inc = inc; michael@0: pd.line = line; michael@0: pd.filename = filename; michael@0: ip.funcs.handle_error = my_if_errors; michael@0: ip.funcs.eval_defined = my_eval_defined; michael@0: ip.funcs.eval_variable = my_eval_variable; michael@0: ip.data = (char *) &pd; michael@0: michael@0: (void) ParseIfExpression (&ip, line, &val); michael@0: if (val) michael@0: return IF; michael@0: else michael@0: return IFFALSE; michael@0: } michael@0: #endif /* CPP */ michael@0: