Back to index

lightning-sunbird  0.9+nobinonly
cppsetup.c
Go to the documentation of this file.
00001 /* $Xorg: cppsetup.c,v 1.5 2001/02/09 02:03:16 xorgcvs Exp $ */
00002 /*
00003 
00004 Copyright (c) 1993, 1994, 1998  The Open Group
00005 
00006 Permission to use, copy, modify, distribute, and sell this software and its
00007 documentation for any purpose is hereby granted without fee, provided that
00008 the above copyright notice appear in all copies and that both that
00009 copyright notice and this permission notice appear in supporting
00010 documentation.
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00018 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00019 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00020 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00021 
00022 Except as contained in this notice, the name of The Open Group shall not be
00023 used in advertising or otherwise to promote the sale, use or other dealings
00024 in this Software without prior written authorization from The Open Group.
00025 
00026 */
00027 /* $XFree86: xc/config/makedepend/cppsetup.c,v 3.11 2001/12/17 20:52:22 dawes Exp $ */
00028 
00029 #include "def.h"
00030 
00031 #ifdef CPP
00032 /*
00033  * This file is strictly for the sake of cpy.y and yylex.c (if
00034  * you indeed have the source for cpp).
00035  */
00036 #define IB 1
00037 #define SB 2
00038 #define NB 4
00039 #define CB 8
00040 #define QB 16
00041 #define WB 32
00042 #define SALT '#'
00043 #if defined(pdp11) || defined(vax) || defined(ns16000) || defined(mc68000) || defined(ibm032)
00044 #define COFF 128
00045 #else
00046 #define COFF 0
00047 #endif
00048 /*
00049  * These variables used by cpy.y and yylex.c
00050  */
00051 extern char   *outp, *inp, *newp, *pend;
00052 extern char   *ptrtab;
00053 extern char   fastab[];
00054 extern char   slotab[];
00055 
00056 /*
00057  * cppsetup
00058  */
00059 struct filepointer   *currentfile;
00060 struct inclist              *currentinc;
00061 
00062 int
00063 cppsetup(char *line, struct filepointer *filep, struct inclist *inc)
00064 {
00065        char *p, savec;
00066        static boolean setupdone = FALSE;
00067        boolean       value;
00068 
00069        if (!setupdone) {
00070               cpp_varsetup();
00071               setupdone = TRUE;
00072        }
00073 
00074        currentfile = filep;
00075        currentinc = inc;
00076        inp = newp = line;
00077        for (p=newp; *p; p++)
00078               ;
00079 
00080        /*
00081         * put a newline back on the end, and set up pend, etc.
00082         */
00083        *p++ = '\n';
00084        savec = *p;
00085        *p = '\0';
00086        pend = p;
00087 
00088        ptrtab = slotab+COFF;
00089        *--inp = SALT; 
00090        outp=inp; 
00091        value = yyparse();
00092        *p = savec;
00093        return(value);
00094 }
00095 
00096 struct symtab **lookup(symbol)
00097        char   *symbol;
00098 {
00099        static struct symtab    *undefined;
00100        struct symtab   **sp;
00101 
00102        sp = isdefined(symbol, currentinc, NULL);
00103        if (sp == NULL) {
00104               sp = &undefined;
00105               (*sp)->s_value = NULL;
00106        }
00107        return (sp);
00108 }
00109 
00110 pperror(tag, x0,x1,x2,x3,x4)
00111        int    tag,x0,x1,x2,x3,x4;
00112 {
00113        warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
00114        warning(x0,x1,x2,x3,x4);
00115 }
00116 
00117 
00118 yyerror(s)
00119        register char *s;
00120 {
00121        fatalerr("Fatal error: %s\n", s);
00122 }
00123 #else /* not CPP */
00124 
00125 #include "ifparser.h"
00126 struct _parse_data {
00127     struct filepointer *filep;
00128     struct inclist *inc;
00129     char *filename;
00130     const char *line;
00131 };
00132 
00133 static const char *
00134 my_if_errors (IfParser *ip, const char *cp, const char *expecting)
00135 {
00136     struct _parse_data *pd = (struct _parse_data *) ip->data;
00137     int lineno = pd->filep->f_line;
00138     char *filename = pd->filename;
00139     char prefix[300];
00140     int prefixlen;
00141     int i;
00142 
00143     sprintf (prefix, "\"%s\":%d", filename, lineno);
00144     prefixlen = strlen(prefix);
00145     fprintf (stderr, "%s:  %s", prefix, pd->line);
00146     i = cp - pd->line;
00147     if (i > 0 && pd->line[i-1] != '\n') {
00148        putc ('\n', stderr);
00149     }
00150     for (i += prefixlen + 3; i > 0; i--) {
00151        putc (' ', stderr);
00152     }
00153     fprintf (stderr, "^--- expecting %s\n", expecting);
00154     return NULL;
00155 }
00156 
00157 
00158 #define MAXNAMELEN 256
00159 
00160 static struct symtab **
00161 lookup_variable (IfParser *ip, const char *var, int len)
00162 {
00163     char tmpbuf[MAXNAMELEN + 1];
00164     struct _parse_data *pd = (struct _parse_data *) ip->data;
00165 
00166     if (len > MAXNAMELEN)
00167        return 0;
00168 
00169     strncpy (tmpbuf, var, len);
00170     tmpbuf[len] = '\0';
00171     return isdefined (tmpbuf, pd->inc, NULL);
00172 }
00173 
00174 
00175 static int
00176 my_eval_defined (IfParser *ip, const char *var, int len)
00177 {
00178     if (lookup_variable (ip, var, len))
00179        return 1;
00180     else
00181        return 0;
00182 }
00183 
00184 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
00185 
00186 static long
00187 my_eval_variable (IfParser *ip, const char *var, int len)
00188 {
00189     long val;
00190     struct symtab **s;
00191 
00192     s = lookup_variable (ip, var, len);
00193     if (!s)
00194        return 0;
00195     do {
00196        var = (*s)->s_value;
00197        if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var))
00198            break;
00199        s = lookup_variable (ip, var, strlen(var));
00200     } while (s);
00201 
00202     var = ParseIfExpression(ip, var, &val);
00203     if (var && *var) debug(4, ("extraneous: '%s'\n", var));
00204     return val;
00205 }
00206 
00207 int
00208 cppsetup(char *filename,
00209         char *line,
00210         struct filepointer *filep,
00211         struct inclist *inc)
00212 {
00213     IfParser ip;
00214     struct _parse_data pd;
00215     long val = 0;
00216 
00217     pd.filep = filep;
00218     pd.inc = inc;
00219     pd.line = line;
00220     pd.filename = filename;
00221     ip.funcs.handle_error = my_if_errors;
00222     ip.funcs.eval_defined = my_eval_defined;
00223     ip.funcs.eval_variable = my_eval_variable;
00224     ip.data = (char *) &pd;
00225 
00226     (void) ParseIfExpression (&ip, line, &val);
00227     if (val)
00228        return IF;
00229     else
00230        return IFFALSE;
00231 }
00232 #endif /* CPP */
00233