cppsetup.c

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

Generated on Tue Jul 5 14:10:17 2011 for ROOT_528-00b_version by  doxygen 1.5.1