1/* 2 3Copyright (c) 1993, 1994, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27#include "def.h" 28 29#include "ifparser.h" 30 31struct _parse_data { 32 struct filepointer *filep; 33 struct inclist *inc; 34 const char *filename; 35 const char *line; 36}; 37 38static const char * 39my_if_errors(IfParser *ip, const char *cp, const char *expecting) 40{ 41 struct _parse_data *pd = ip->data; 42 long lineno = pd->filep->f_line; 43 const char *filename = pd->filename; 44 char prefix[300]; 45 long prefixlen; 46 long i; 47 48 snprintf(prefix, sizeof(prefix), "\"%s\":%ld", filename, lineno); 49 prefixlen = (long) strlen(prefix); 50 fprintf(stderr, "%s: %s", prefix, pd->line); 51 i = cp - pd->line; 52 if (i > 0 && pd->line[i - 1] != '\n') { 53 putc('\n', stderr); 54 } 55 for (i += prefixlen + 3; i > 0; i--) { 56 putc(' ', stderr); 57 } 58 fprintf(stderr, "^--- expecting %s\n", expecting); 59 return NULL; 60} 61 62 63#define MAXNAMELEN 256 64 65static struct symtab ** 66lookup_variable(IfParser *ip, const char *var, int len) 67{ 68 char tmpbuf[MAXNAMELEN + 1]; 69 struct _parse_data *pd = ip->data; 70 71 if (len > MAXNAMELEN) 72 return NULL; 73 74 strncpy(tmpbuf, var, len); 75 tmpbuf[len] = '\0'; 76 return isdefined(tmpbuf, pd->inc, NULL); 77} 78 79 80static int 81my_eval_defined(IfParser *ip, const char *var, int len) 82{ 83 if (lookup_variable(ip, var, len)) 84 return 1; 85 else 86 return 0; 87} 88 89#define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_') 90 91static long 92my_eval_variable(IfParser *ip, const char *var, int len) 93{ 94 long val; 95 struct symtab **s; 96 97 s = lookup_variable(ip, var, len); 98 if (!s) 99 return 0; 100 do { 101 var = (*s)->s_value; 102 if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var)) 103 break; 104 s = lookup_variable(ip, var, strlen(var)); 105 } while (s); 106 107 var = ParseIfExpression(ip, var, &val); 108 if (var && *var) 109 debug(4, ("extraneous: '%s'\n", var)); 110 return val; 111} 112 113int 114cppsetup(const char *filename, const char *line, 115 struct filepointer *filep, struct inclist *inc) 116{ 117 struct _parse_data pd = { 118 .filep = filep, 119 .inc = inc, 120 .line = line, 121 .filename = filename 122 }; 123 IfParser ip = { 124 .funcs.handle_error = my_if_errors, 125 .funcs.eval_defined = my_eval_defined, 126 .funcs.eval_variable = my_eval_variable, 127 .data = &pd 128 }; 129 long val = 0; 130 131 (void) ParseIfExpression(&ip, line, &val); 132 if (val) 133 return IF; 134 else 135 return IFFALSE; 136} 137