| 1 | /* | 
|---|
| 2 | * Copyright 1998 Bertho A. Stultiens (BS) | 
|---|
| 3 | * | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | #include "config.h" | 
|---|
| 7 |  | 
|---|
| 8 | #include <stdio.h> | 
|---|
| 9 | #include <stdlib.h> | 
|---|
| 10 | #include <string.h> | 
|---|
| 11 |  | 
|---|
| 12 | #include "wrc.h" | 
|---|
| 13 | #include "utils.h" | 
|---|
| 14 | #include "preproc.h" | 
|---|
| 15 | #include "parser.h" | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | extern void set_pp_ignore(int); /* From parser.l */ | 
|---|
| 19 |  | 
|---|
| 20 | static char *current_define; | 
|---|
| 21 |  | 
|---|
| 22 | #define HASHKEY         2039 | 
|---|
| 23 | static struct pp_entry *pp_defines[HASHKEY]; | 
|---|
| 24 |  | 
|---|
| 25 | #define MAXIFSTACK      64 | 
|---|
| 26 | static struct if_state ifstack[MAXIFSTACK]; | 
|---|
| 27 | static int ifstackidx = 0; | 
|---|
| 28 |  | 
|---|
| 29 | #if 0 | 
|---|
| 30 | void pp_status(void) | 
|---|
| 31 | { | 
|---|
| 32 | int i; | 
|---|
| 33 | int sum; | 
|---|
| 34 | int total = 0; | 
|---|
| 35 | struct pp_entry *ppp; | 
|---|
| 36 |  | 
|---|
| 37 | printf("Defines statistics:\n"); | 
|---|
| 38 | for(i = 0; i < HASHKEY; i++) | 
|---|
| 39 | { | 
|---|
| 40 | sum = 0; | 
|---|
| 41 | for(ppp = pp_defines[i]; ppp; ppp = ppp->next) | 
|---|
| 42 | sum++; | 
|---|
| 43 | total += sum; | 
|---|
| 44 | printf("%4d, %3d\n", i, sum); | 
|---|
| 45 | } | 
|---|
| 46 | printf("Total defines: %d\n", total); | 
|---|
| 47 | } | 
|---|
| 48 | #pragma exit pp_status | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 | /* Don't comment on the hash, its primitive but functional... */ | 
|---|
| 52 | int pp_hash(char *str) | 
|---|
| 53 | { | 
|---|
| 54 | int sum = 0; | 
|---|
| 55 | while(*str) | 
|---|
| 56 | sum += *str++; | 
|---|
| 57 | return sum % HASHKEY; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | struct pp_entry *pp_lookup(char *ident) | 
|---|
| 61 | { | 
|---|
| 62 | int index = pp_hash(ident); | 
|---|
| 63 | struct pp_entry *ppp; | 
|---|
| 64 | for(ppp = pp_defines[index]; ppp; ppp = ppp->next) | 
|---|
| 65 | { | 
|---|
| 66 | if(!strcmp(ident, ppp->ident)) | 
|---|
| 67 | return ppp; | 
|---|
| 68 | } | 
|---|
| 69 | return NULL; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void set_define(char *name) | 
|---|
| 73 | { | 
|---|
| 74 | current_define = xstrdup(name); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void del_define(char *name) | 
|---|
| 78 | { | 
|---|
| 79 | int index; | 
|---|
| 80 | struct pp_entry *ppp; | 
|---|
| 81 |  | 
|---|
| 82 | if((ppp = pp_lookup(name)) == NULL) | 
|---|
| 83 | { | 
|---|
| 84 | if(pedantic) | 
|---|
| 85 | yywarning("%s was not defined", name); | 
|---|
| 86 | return; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | index = pp_hash(name); | 
|---|
| 90 | if(pp_defines[index] == ppp) | 
|---|
| 91 | { | 
|---|
| 92 | pp_defines[index] = ppp->next; | 
|---|
| 93 | if(pp_defines[index]) | 
|---|
| 94 | pp_defines[index]->prev = NULL; | 
|---|
| 95 | } | 
|---|
| 96 | else | 
|---|
| 97 | { | 
|---|
| 98 | ppp->prev->next = ppp->next; | 
|---|
| 99 | if(ppp->next) | 
|---|
| 100 | ppp->next->prev = ppp->prev; | 
|---|
| 101 | } | 
|---|
| 102 | free(ppp); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void add_define(char *text) | 
|---|
| 106 | { | 
|---|
| 107 | int len; | 
|---|
| 108 | char *cptr; | 
|---|
| 109 | int index = pp_hash(current_define); | 
|---|
| 110 | struct pp_entry *ppp; | 
|---|
| 111 | if(pp_lookup(current_define) != NULL) | 
|---|
| 112 | { | 
|---|
| 113 | if(pedantic) | 
|---|
| 114 | yywarning("Redefinition of %s", current_define); | 
|---|
| 115 | del_define(current_define); | 
|---|
| 116 | } | 
|---|
| 117 | ppp = (struct pp_entry *)xmalloc(sizeof(struct pp_entry)); | 
|---|
| 118 | ppp->ident = current_define; | 
|---|
| 119 | ppp->subst = xstrdup(text); | 
|---|
| 120 | ppp->next = pp_defines[index]; | 
|---|
| 121 | pp_defines[index] = ppp; | 
|---|
| 122 | if(ppp->next) | 
|---|
| 123 | ppp->next->prev = ppp; | 
|---|
| 124 | /* Strip trailing white space from subst text */ | 
|---|
| 125 | len = strlen(ppp->subst); | 
|---|
| 126 | while(len && strchr(" \t\r\n", ppp->subst[len-1])) | 
|---|
| 127 | { | 
|---|
| 128 | ppp->subst[--len] = '\0'; | 
|---|
| 129 | } | 
|---|
| 130 | /* Strip leading white space from subst text */ | 
|---|
| 131 | for(cptr = ppp->subst; *cptr && strchr(" \t\r", *cptr); cptr++) | 
|---|
| 132 | ; | 
|---|
| 133 | if(ppp->subst != cptr) | 
|---|
| 134 | memmove(ppp->subst, cptr, strlen(cptr)+1); | 
|---|
| 135 | if(yydebug) | 
|---|
| 136 | printf("Added (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, ppp->subst); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | void add_cmdline_define(char *set) | 
|---|
| 140 | { | 
|---|
| 141 | char *cpy = xstrdup(set);       /* Because gcc passes a R/O string */ | 
|---|
| 142 | char *cptr = strchr(cpy, '='); | 
|---|
| 143 | if(cptr) | 
|---|
| 144 | *cptr = '\0'; | 
|---|
| 145 | set_define(cpy); | 
|---|
| 146 | add_define(cptr ? cptr+1 : ""); | 
|---|
| 147 | free(cpy); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /*kso added test for __SEMICOLON__ since __WIN32OS2__ isn't defined! */ | 
|---|
| 151 | #if defined(__SEMICOLON__) || defined(_Windows) || defined(__MSDOS__) || defined(__WIN32OS2__) | 
|---|
| 152 | #define INCLUDESEPARATOR        ";" | 
|---|
| 153 | #warning "Using ; as include separator" | 
|---|
| 154 | #else | 
|---|
| 155 | #warning "Using : as include separator" | 
|---|
| 156 | #define INCLUDESEPARATOR        ":" | 
|---|
| 157 | #endif | 
|---|
| 158 |  | 
|---|
| 159 | static char **includepath; | 
|---|
| 160 | static int nincludepath = 0; | 
|---|
| 161 |  | 
|---|
| 162 | void add_include_path(char *path) | 
|---|
| 163 | { | 
|---|
| 164 | char *tok; | 
|---|
| 165 | char *cpy = xstrdup(path); | 
|---|
| 166 |  | 
|---|
| 167 | tok = strtok(cpy, INCLUDESEPARATOR); | 
|---|
| 168 | while(tok) | 
|---|
| 169 | { | 
|---|
| 170 | char *dir; | 
|---|
| 171 | char *cptr; | 
|---|
| 172 | if(strlen(tok) == 0) | 
|---|
| 173 | continue; | 
|---|
| 174 | dir = xstrdup(tok); | 
|---|
| 175 | for(cptr = dir; *cptr; cptr++) | 
|---|
| 176 | { | 
|---|
| 177 | /* Convert to forward slash */ | 
|---|
| 178 | if(*cptr == '\\') | 
|---|
| 179 | *cptr = '/'; | 
|---|
| 180 | } | 
|---|
| 181 | /* Kill eventual trailing '/' */ | 
|---|
| 182 | if(*(cptr = dir + strlen(dir)-1) == '/') | 
|---|
| 183 | *cptr = '\0'; | 
|---|
| 184 |  | 
|---|
| 185 | /* Add to list */ | 
|---|
| 186 | nincludepath++; | 
|---|
| 187 | includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath)); | 
|---|
| 188 | includepath[nincludepath-1] = dir; | 
|---|
| 189 | tok = strtok(NULL, INCLUDESEPARATOR); | 
|---|
| 190 | } | 
|---|
| 191 | free(cpy); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | FILE *open_include(const char *name, int search) | 
|---|
| 195 | { | 
|---|
| 196 | char *cpy = xstrdup(name); | 
|---|
| 197 | char *cptr; | 
|---|
| 198 | FILE *fp; | 
|---|
| 199 | int i; | 
|---|
| 200 |  | 
|---|
| 201 | for(cptr = cpy; *cptr; cptr++) | 
|---|
| 202 | { | 
|---|
| 203 | /* kill double backslash */ | 
|---|
| 204 | if(*cptr == '\\' && *(cptr+1) == '\\') | 
|---|
| 205 | memmove(cptr, cptr+1, strlen(cptr)); | 
|---|
| 206 | /* Convert to forward slash */ | 
|---|
| 207 | if(*cptr == '\\') | 
|---|
| 208 | *cptr = '/'; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | if(search) | 
|---|
| 212 | { | 
|---|
| 213 | /* Search current dir and then -I path */ | 
|---|
| 214 | fp = fopen(name, "rt"); | 
|---|
| 215 | if(fp) | 
|---|
| 216 | { | 
|---|
| 217 | if(yydebug) | 
|---|
| 218 | printf("Going to include <%s>\n", name); | 
|---|
| 219 | free(cpy); | 
|---|
| 220 | return fp; | 
|---|
| 221 | } | 
|---|
| 222 | } | 
|---|
| 223 | /* Search -I path */ | 
|---|
| 224 | for(i = 0; i < nincludepath; i++) | 
|---|
| 225 | { | 
|---|
| 226 | char *path; | 
|---|
| 227 | path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2); | 
|---|
| 228 | strcpy(path, includepath[i]); | 
|---|
| 229 | strcat(path, "/"); | 
|---|
| 230 | strcat(path, cpy); | 
|---|
| 231 | fp = fopen(path, "rt"); | 
|---|
| 232 | if(fp && yydebug) | 
|---|
| 233 | printf("Going to include <%s>\n", path); | 
|---|
| 234 | free(path); | 
|---|
| 235 | if(fp) | 
|---|
| 236 | { | 
|---|
| 237 | free(cpy); | 
|---|
| 238 | return fp; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | } | 
|---|
| 242 | free(cpy); | 
|---|
| 243 | return NULL; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | void push_if(int truecase, int wastrue, int nevertrue) | 
|---|
| 247 | { | 
|---|
| 248 | if(ifstackidx >= MAXIFSTACK-1) | 
|---|
| 249 | internal_error(__FILE__, __LINE__, "#if stack overflow"); | 
|---|
| 250 | ifstack[ifstackidx].current = truecase && !wastrue; | 
|---|
| 251 | ifstack[ifstackidx].hasbeentrue = wastrue; | 
|---|
| 252 | ifstack[ifstackidx].nevertrue = nevertrue; | 
|---|
| 253 | if(nevertrue || !(truecase && !wastrue)) | 
|---|
| 254 | set_pp_ignore(1); | 
|---|
| 255 | if(yydebug) | 
|---|
| 256 | printf("push_if: %d %d %d (%d %d %d)\n", | 
|---|
| 257 | truecase, | 
|---|
| 258 | wastrue, | 
|---|
| 259 | nevertrue, | 
|---|
| 260 | ifstack[ifstackidx].current, | 
|---|
| 261 | ifstack[ifstackidx].hasbeentrue, | 
|---|
| 262 | ifstack[ifstackidx].nevertrue); | 
|---|
| 263 | ifstackidx++; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | int pop_if(void) | 
|---|
| 267 | { | 
|---|
| 268 | if(ifstackidx <= 0) | 
|---|
| 269 | yyerror("#endif without #if|#ifdef|#ifndef (#if stack underflow)"); | 
|---|
| 270 | ifstackidx--; | 
|---|
| 271 | if(yydebug) | 
|---|
| 272 | printf("pop_if: %d %d %d\n", | 
|---|
| 273 | ifstack[ifstackidx].current, | 
|---|
| 274 | ifstack[ifstackidx].hasbeentrue, | 
|---|
| 275 | ifstack[ifstackidx].nevertrue); | 
|---|
| 276 | if(ifstack[ifstackidx].nevertrue || !ifstack[ifstackidx].current) | 
|---|
| 277 | set_pp_ignore(0); | 
|---|
| 278 | return ifstack[ifstackidx].hasbeentrue || ifstack[ifstackidx].current; | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | int isnevertrue_if(void) | 
|---|
| 282 | { | 
|---|
| 283 | return ifstackidx > 0 && ifstack[ifstackidx-1].nevertrue; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|