cppsetup.cpp

Go to the documentation of this file.
00001 /* $XConsortium: cppsetup.c,v 1.13 94/04/17 20:10:32 gildea Exp $ */
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 
00029 #ifdef __WIN32__
00030   #pragma warning(disable : 4996)
00031 #endif
00032 
00033 #include "def.h"
00034 
00035 #ifdef CPP
00036 
00037 #include "ifparser.h"
00038 
00039 /*
00040  * This file is strictly for the sake of cpy.y and yylex.c (if
00041  * you indeed have the source for cpp).
00042  */
00043 #define IB 1
00044 #define SB 2
00045 #define NB 4
00046 #define CB 8
00047 #define QB 16
00048 #define WB 32
00049 #define SALT '#'
00050 #if pdp11 | vax | ns16000 | mc68000 | ibm032
00051 #define COFF 128
00052 #else
00053 #define COFF 0
00054 #endif
00055 /*
00056  * These variables used by cpy.y and yylex.c
00057  */
00058 extern char  *outp, *inp, *newp, *pend;
00059 extern char  *ptrtab;
00060 extern char  fastab[];
00061 extern char  slotab[];
00062 
00063 /*
00064  * cppsetup
00065  */
00066 struct filepointer  *currentfile;
00067 inclist    *currentinc;
00068 
00069 int cppsetup(register char *line, register struct filepointer *filep,
00070     register inclist *inc)
00071 {
00072   register char *p, savec;
00073   static bool setupdone = false;
00074   bool  value;
00075 
00076   if (!setupdone) {
00077     cpp_varsetup();
00078     setupdone = true;
00079   }
00080 
00081   currentfile = filep;
00082   currentinc = inc;
00083   inp = newp = line;
00084   for (p=newp; *p; p++)
00085     ;
00086 
00087   /*
00088    * put a newline back on the end, and set up pend, etc.
00089    */
00090   *p++ = '\n';
00091   savec = *p;
00092   *p = '\0';
00093   pend = p;
00094 
00095   ptrtab = slotab+COFF;
00096   *--inp = SALT; 
00097   outp=inp; 
00098   value = yyparse();
00099   *p = savec;
00100   return(value);
00101 }
00102 
00103 struct symtab *lookup(char *symbol)
00104 {
00105   static struct symtab    undefined;
00106   struct symtab   *sp;
00107 
00108   sp = isdefined(symbol, currentinc, NULL);
00109   if (sp == NULL) {
00110     sp = &undefined;
00111     sp->s_value = NULL;
00112   }
00113   return (sp);
00114 }
00115 
00116 int pperror(int tag, int x0, int x1, int x2, int x3, int x4)
00117 {
00118   warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
00119   warning(x0,x1,x2,x3,x4);
00120 }
00121 
00122 
00123 int yyerror(register char *s)
00124 {
00125   fatalerr("Fatal error: %s\n", s);
00126 }
00127 
00128 #else /* not CPP */
00129 
00130 #include "ifparser.h"
00131 
00132 #include <string.h>
00133 
00134 struct _parse_data {
00135     struct filepointer *filep;
00136     inclist *inc;
00137     const char *line;
00138 };
00139 
00140 static const char *_my_if_errors(IfParser *ip, const char *cp,
00141     const char *expecting)
00142 {
00143     struct _parse_data *pd = (struct _parse_data *) ip->data;
00144     int lineno = pd->filep->f_line;
00145     char *filename = pd->inc->i_file;
00146     char prefix[300];
00147     int prefixlen;
00148     int i;
00149 
00150     sprintf (prefix, "\"%s\":%d", filename, lineno);
00151     prefixlen = int(strlen(prefix));
00152     fprintf (stderr, "%s:  %s", prefix, pd->line);
00153     i = int(cp - pd->line);
00154     if (i > 0 && pd->line[i-1] != '\n') {
00155   putc ('\n', stderr);
00156     }
00157     for (i += prefixlen + 3; i > 0; i--) {
00158   putc (' ', stderr);
00159     }
00160     fprintf (stderr, "^--- expecting %s\n", expecting);
00161     return NULL;
00162 }
00163 
00164 
00165 #define MAXNAMELEN 256
00166 
00167 static struct symtab *_lookup_variable(IfParser *ip, const char *var, int len)
00168 {
00169     char tmpbuf[MAXNAMELEN + 1];
00170     struct _parse_data *pd = (struct _parse_data *) ip->data;
00171 
00172     if (len > MAXNAMELEN)
00173   return 0;
00174 
00175     strncpy (tmpbuf, var, len);
00176     tmpbuf[len] = '\0';
00177     return isdefined (tmpbuf, pd->inc, NULL);
00178 }
00179 
00180 
00181 static int _my_eval_defined(IfParser *ip, const char *var, int len)
00182 {
00183   if (_lookup_variable (ip, var, len)) return 1;
00184   else return 0;
00185 }
00186 
00187 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
00188 
00189 static int _my_eval_variable(IfParser *ip, const char *var, int len)
00190 {
00191     struct symtab *s;
00192 
00193     s = _lookup_variable (ip, var, len);
00194     if (!s)
00195   return 0;
00196     do {
00197   var = s->s_value;
00198   if (!isvarfirstletter(*var))
00199       break;
00200   s = _lookup_variable (ip, var, int(strlen(var)));
00201     } while (s);
00202 
00203     return atoi(var);
00204 }
00205 
00206 
00207 int cppsetup(register char *line, register struct filepointer *filep,
00208     register inclist *inc)
00209 {
00210     IfParser ip;
00211     struct _parse_data pd;
00212     int val = 0;
00213 
00214     pd.filep = filep;
00215     pd.inc = inc;
00216     pd.line = line;
00217     ip.funcs.handle_error = _my_if_errors;
00218     ip.funcs.eval_defined = _my_eval_defined;
00219     ip.funcs.eval_variable = _my_eval_variable;
00220     ip.data = (char *) &pd;
00221 
00222     (void) ParseIfExpression (&ip, line, &val);
00223     if (val)
00224   return IF;
00225     else
00226   return IFFALSE;
00227 }
00228 
00229 #endif /* CPP */
00230 

Generated on Fri Nov 28 04:28:50 2008 for HOOPLE Libraries by  doxygen 1.5.1