Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:41 UTC

view on githubraw file Latest commit bf5846c3 on 2004-02-25 18:19:54 UTC
bf5846c3a1 Ed H*0001 /* $XConsortium: cppsetup.c,v 1.13 94/04/17 20:10:32 gildea Exp $ */
                0002 
                0003 /*
                0004  * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
                0005  *
                0006  * Redistribution and use in source and binary forms, with or without
                0007  * modification, are permitted provided that the following conditions
                0008  * are met:
                0009  *
                0010  * 1. Redistributions of source code must retain the above copyright
                0011  *    notice, this list of conditions and the following disclaimer. 
                0012  *
                0013  * 2. Redistributions in binary form must reproduce the above copyright
                0014  *    notice, this list of conditions and the following disclaimer in
                0015  *    the documentation and/or other materials provided with the
                0016  *    distribution.
                0017  *
                0018  * 3. The name "Carnegie Mellon University" must not be used to
                0019  *    endorse or promote products derived from this software without
                0020  *    prior written permission. For permission or any other legal
                0021  *    details, please contact  
                0022  *      Office of Technology Transfer
                0023  *      Carnegie Mellon University
                0024  *      5000 Forbes Avenue
                0025  *      Pittsburgh, PA  15213-3890
                0026  *      (412) 268-4387, fax: (412) 268-7395
                0027  *      tech-transfer@andrew.cmu.edu
                0028  *
                0029  * 4. Redistributions of any form whatsoever must retain the following
                0030  *    acknowledgment:
                0031  *    "This product includes software developed by Computing Services
                0032  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
                0033  *
                0034  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0035  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0036  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0037  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0038  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0039  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0040  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                0041  *
                0042  */
                0043 /*
                0044 
                0045 Copyright (c) 1993, 1994  X Consortium
                0046 
                0047 Permission is hereby granted, free of charge, to any person obtaining a copy
                0048 of this software and associated documentation files (the "Software"), to deal
                0049 in the Software without restriction, including without limitation the rights
                0050 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                0051 copies of the Software, and to permit persons to whom the Software is
                0052 furnished to do so, subject to the following conditions:
                0053 
                0054 The above copyright notice and this permission notice shall be included in
                0055 all copies or substantial portions of the Software.
                0056 
                0057 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                0058 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                0059 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
                0060 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
                0061 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
                0062 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
                0063 
                0064 Except as contained in this notice, the name of the X Consortium shall not be
                0065 used in advertising or otherwise to promote the sale, use or other dealings
                0066 in this Software without prior written authorization from the X Consortium.
                0067 
                0068 */
                0069 
                0070 #include "def.h"
                0071 
                0072 #ifdef  CPP
                0073 /*
                0074  * This file is strictly for the sake of cpy.y and yylex.c (if
                0075  * you indeed have the source for cpp).
                0076  */
                0077 #define IB 1
                0078 #define SB 2
                0079 #define NB 4
                0080 #define CB 8
                0081 #define QB 16
                0082 #define WB 32
                0083 #define SALT '#'
                0084 #if pdp11 | vax | ns16000 | mc68000 | ibm032
                0085 #define COFF 128
                0086 #else
                0087 #define COFF 0
                0088 #endif
                0089 /*
                0090  * These variables used by cpy.y and yylex.c
                0091  */
                0092 extern char *outp, *inp, *newp, *pend;
                0093 extern char *ptrtab;
                0094 extern char fastab[];
                0095 extern char slotab[];
                0096 
                0097 /*
                0098  * cppsetup
                0099  */
                0100 struct filepointer  *currentfile;
                0101 struct inclist      *currentinc;
                0102 
                0103 cppsetup(line, filep, inc)
                0104     register char   *line;
                0105     register struct filepointer *filep;
                0106     register struct inclist     *inc;
                0107 {
                0108     register char *p, savec;
                0109     static boolean setupdone = FALSE;
                0110     boolean value;
                0111 
                0112     if (!setupdone) {
                0113         cpp_varsetup();
                0114         setupdone = TRUE;
                0115     }
                0116 
                0117     currentfile = filep;
                0118     currentinc = inc;
                0119     inp = newp = line;
                0120     for (p=newp; *p; p++)
                0121         ;
                0122 
                0123     /*
                0124      * put a newline back on the end, and set up pend, etc.
                0125      */
                0126     *p++ = '\n';
                0127     savec = *p;
                0128     *p = '\0';
                0129     pend = p;
                0130 
                0131     ptrtab = slotab+COFF;
                0132     *--inp = SALT; 
                0133     outp=inp; 
                0134     value = yyparse();
                0135     *p = savec;
                0136     return(value);
                0137 }
                0138 
                0139 struct symtab *lookup(symbol)
                0140     char    *symbol;
                0141 {
                0142     static struct symtab    undefined;
                0143     struct symtab   *sp;
                0144 
                0145     sp = isdefined(symbol, currentinc, NULL);
                0146     if (sp == NULL) {
                0147         sp = &undefined;
                0148         sp->s_value = NULL;
                0149     }
                0150     return (sp);
                0151 }
                0152 
                0153 pperror(tag, x0,x1,x2,x3,x4)
                0154     int tag,x0,x1,x2,x3,x4;
                0155 {
                0156     warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
                0157     warning(x0,x1,x2,x3,x4);
                0158 }
                0159 
                0160 
                0161 yyerror(s)
                0162     register char   *s;
                0163 {
                0164     fatalerr("Fatal error: %s\n", s);
                0165 }
                0166 #else /* not CPP */
                0167 
                0168 #include "ifparser.h"
                0169 struct _parse_data {
                0170     struct filepointer *filep;
                0171     struct inclist *inc;
                0172     const char *line;
                0173 };
                0174 
                0175 static const char *
                0176 _my_if_errors (ip, cp, expecting)
                0177     IfParser *ip;
                0178     const char *cp;
                0179     const char *expecting;
                0180 {
                0181     struct _parse_data *pd = (struct _parse_data *) ip->data;
                0182     int lineno = pd->filep->f_line;
                0183     char *filename = pd->inc->i_file;
                0184     char prefix[300];
                0185     int prefixlen;
                0186     int i;
                0187 
                0188     sprintf (prefix, "\"%s\":%d", filename, lineno);
                0189     prefixlen = strlen(prefix);
                0190     fprintf (stderr, "%s:  %s", prefix, pd->line);
                0191     i = cp - pd->line;
                0192     if (i > 0 && pd->line[i-1] != '\n') {
                0193     putc ('\n', stderr);
                0194     }
                0195     for (i += prefixlen + 3; i > 0; i--) {
                0196     putc (' ', stderr);
                0197     }
                0198     fprintf (stderr, "^--- expecting %s\n", expecting);
                0199     return NULL;
                0200 }
                0201 
                0202 
                0203 #define MAXNAMELEN 256
                0204 
                0205 static struct symtab *
                0206 _lookup_variable (ip, var, len)
                0207     IfParser *ip;
                0208     const char *var;
                0209     int len;
                0210 {
                0211     char tmpbuf[MAXNAMELEN + 1];
                0212     struct _parse_data *pd = (struct _parse_data *) ip->data;
                0213 
                0214     if (len > MAXNAMELEN)
                0215     return 0;
                0216 
                0217     strncpy (tmpbuf, var, len);
                0218     tmpbuf[len] = '\0';
                0219     return isdefined (tmpbuf, pd->inc, NULL);
                0220 }
                0221 
                0222 
                0223 static int
                0224 _my_eval_defined (ip, var, len)
                0225     IfParser *ip;
                0226     const char *var;
                0227     int len;
                0228 {
                0229     if (_lookup_variable (ip, var, len))
                0230     return 1;
                0231     else
                0232     return 0;
                0233 }
                0234 
                0235 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
                0236 
                0237 static int
                0238 _my_eval_variable (ip, var, len)
                0239     IfParser *ip;
                0240     const char *var;
                0241     int len;
                0242 {
                0243     struct symtab *s;
                0244 
                0245     s = _lookup_variable (ip, var, len);
                0246     if (!s)
                0247     return 0;
                0248     do {
                0249     var = s->s_value;
                0250     if (!isvarfirstletter(*var))
                0251         break;
                0252     s = _lookup_variable (ip, var, strlen(var));
                0253     } while (s);
                0254 
                0255     return atoi(var);
                0256 }
                0257 
                0258 
                0259 cppsetup(line, filep, inc)
                0260     register char   *line;
                0261     register struct filepointer *filep;
                0262     register struct inclist     *inc;
                0263 {
                0264     IfParser ip;
                0265     struct _parse_data pd;
                0266     int val = 0;
                0267 
                0268     pd.filep = filep;
                0269     pd.inc = inc;
                0270     pd.line = line;
                0271     ip.funcs.handle_error = _my_if_errors;
                0272     ip.funcs.eval_defined = _my_eval_defined;
                0273     ip.funcs.eval_variable = _my_eval_variable;
                0274     ip.data = (char *) &pd;
                0275 
                0276     (void) ParseIfExpression (&ip, line, &val);
                0277     if (val)
                0278     return IF;
                0279     else
                0280     return IFFALSE;
                0281 }
                0282 #endif /* CPP */
                0283