Added regedit unit test, a couple minor changes to regedit.
[wine] / tools / wrc / preproc.c
1 /*
2  * Copyright 1998 Bertho A. Stultiens (BS)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "wrc.h"
27 #include "utils.h"
28 #include "preproc.h"
29
30
31 #define HASHKEY         2039
32 static pp_entry_t *pp_defines[HASHKEY];
33
34 #define MAXIFSTACK      64
35 static if_state_t if_stack[MAXIFSTACK];
36 static int if_stack_idx = 0;
37
38 #if 0
39 void pp_status(void) __attribute__((destructor));
40 void pp_status(void)
41 {
42         int i;
43         int sum;
44         int total = 0;
45         pp_entry_t *ppp;
46
47         fprintf(stderr, "Defines statistics:\n");
48         for(i = 0; i < HASHKEY; i++)
49         {
50                 sum = 0;
51                 for(ppp = pp_defines[i]; ppp; ppp = ppp->next)
52                         sum++;
53                 total += sum;
54                 fprintf(stderr, "%4d, %3d\n", i, sum);
55         }
56         fprintf(stderr, "Total defines: %d\n", total);
57 }
58 #endif
59
60 /* Don't comment on the hash, its primitive but functional... */
61 int pphash(char *str)
62 {
63         int sum = 0;
64         while(*str)
65                 sum += *str++;
66         return sum % HASHKEY;
67 }
68
69 pp_entry_t *pplookup(char *ident)
70 {
71         int idx = pphash(ident);
72         pp_entry_t *ppp;
73
74         for(ppp = pp_defines[idx]; ppp; ppp = ppp->next)
75         {
76                 if(!strcmp(ident, ppp->ident))
77                         return ppp;
78         }
79         return NULL;
80 }
81
82 void del_define(char *name)
83 {
84         int idx;
85         pp_entry_t *ppp;
86
87         if((ppp = pplookup(name)) == NULL)
88         {
89                 if(pedantic)
90                         yywarning("%s was not defined", name);
91                 return;
92         }
93
94         if(ppp->iep)
95         {
96                 if(debuglevel & DEBUGLEVEL_PPMSG)
97                         fprintf(stderr, "del_define: %s:%d: includelogic removed, include_ppp='%s', file=%s\n", input_name, line_number, name, ppp->iep->filename);
98                 if(ppp->iep == includelogiclist)
99                 {
100                         includelogiclist = ppp->iep->next;
101                         if(includelogiclist)
102                                 includelogiclist->prev = NULL;
103                 }
104                 else
105                 {
106                         ppp->iep->prev->next = ppp->iep->next;
107                         if(ppp->iep->next)
108                                 ppp->iep->next->prev = ppp->iep->prev;
109                 }
110                 free(ppp->iep->filename);
111                 free(ppp->iep);
112         }
113
114         idx = pphash(name);
115         if(pp_defines[idx] == ppp)
116         {
117                 pp_defines[idx] = ppp->next;
118                 if(pp_defines[idx])
119                         pp_defines[idx]->prev = NULL;
120         }
121         else
122         {
123                 ppp->prev->next = ppp->next;
124                 if(ppp->next)
125                         ppp->next->prev = ppp->prev;
126         }
127
128         free(ppp);
129
130         if(debuglevel & DEBUGLEVEL_PPMSG)
131                 printf("Deleted (%s, %d) <%s>\n", input_name, line_number, name);
132 }
133
134 pp_entry_t *add_define(char *def, char *text)
135 {
136         int len;
137         char *cptr;
138         int idx = pphash(def);
139         pp_entry_t *ppp;
140
141         if((ppp = pplookup(def)) != NULL)
142         {
143                 if(pedantic)
144                         yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
145                 del_define(def);
146         }
147         ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
148         ppp->ident = def;
149         ppp->type = def_define;
150         ppp->subst.text = text;
151         ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
152         ppp->linenumber = input_name ? line_number : 0;
153         ppp->next = pp_defines[idx];
154         pp_defines[idx] = ppp;
155         if(ppp->next)
156                 ppp->next->prev = ppp;
157         if(text)
158         {
159                 /* Strip trailing white space from subst text */
160                 len = strlen(text);
161                 while(len && strchr(" \t\r\n", text[len-1]))
162                 {
163                         text[--len] = '\0';
164                 }
165                 /* Strip leading white space from subst text */
166                 for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
167                 ;
168                 if(text != cptr)
169                         memmove(text, cptr, strlen(cptr)+1);
170         }
171         if(debuglevel & DEBUGLEVEL_PPMSG)
172                 printf("Added define (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, text ? text : "(null)");
173
174         return ppp;
175 }
176
177 pp_entry_t *add_cmdline_define(char *set)
178 {
179         char *cpy = xstrdup(set);       /* Because gcc passes a R/O string */
180         char *cptr = strchr(cpy, '=');
181         if(cptr)
182                 *cptr = '\0';
183         return add_define(cpy, xstrdup(cptr ? cptr+1 : ""));
184 }
185
186 pp_entry_t *add_special_define(char *id)
187 {
188         pp_entry_t *ppp = add_define(xstrdup(id), xstrdup(""));
189         ppp->type = def_special;
190         return ppp;
191 }
192
193 pp_entry_t *add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
194 {
195         int idx = pphash(id);
196         pp_entry_t *ppp;
197
198         if((ppp = pplookup(id)) != NULL)
199         {
200                 if(pedantic)
201                         yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
202                 del_define(id);
203         }
204         ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
205         ppp->ident      = id;
206         ppp->type       = def_macro;
207         ppp->margs      = args;
208         ppp->nargs      = nargs;
209         ppp->subst.mtext= exp;
210         ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
211         ppp->linenumber = input_name ? line_number : 0;
212         ppp->next       = pp_defines[idx];
213         pp_defines[idx] = ppp;
214         if(ppp->next)
215                 ppp->next->prev = ppp;
216
217         if(debuglevel & DEBUGLEVEL_PPMSG)
218         {
219                 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", input_name, line_number, ppp->ident, nargs);
220                 for(; exp; exp = exp->next)
221                 {
222                         switch(exp->type)
223                         {
224                         case exp_text:
225                                 fprintf(stderr, " \"%s\" ", exp->subst.text);
226                                 break;
227                         case exp_stringize:
228                                 fprintf(stderr, " #(%d) ", exp->subst.argidx);
229                                 break;
230                         case exp_concat:
231                                 fprintf(stderr, "##");
232                                 break;
233                         case exp_subst:
234                                 fprintf(stderr, " <%d> ", exp->subst.argidx);
235                                 break;
236                         }
237                 }
238                 fprintf(stderr, ">\n");
239         }
240         return ppp;
241 }
242
243
244 /*
245  *-------------------------------------------------------------------------
246  * Include management
247  *-------------------------------------------------------------------------
248  */
249 #if defined(_Windows) || defined(__MSDOS__)
250 #define INCLUDESEPARATOR        ";"
251 #else
252 #define INCLUDESEPARATOR        ":"
253 #endif
254
255 static char **includepath;
256 static int nincludepath = 0;
257
258 void add_include_path(char *path)
259 {
260         char *tok;
261         char *cpy = xstrdup(path);
262
263         tok = strtok(cpy, INCLUDESEPARATOR);
264         while(tok)
265         {
266                 char *dir;
267                 char *cptr;
268                 if(strlen(tok) == 0)
269                         continue;
270                 dir = xstrdup(tok);
271                 for(cptr = dir; *cptr; cptr++)
272                 {
273                         /* Convert to forward slash */
274                         if(*cptr == '\\')
275                                 *cptr = '/';
276                 }
277                 /* Kill eventual trailing '/' */
278                 if(*(cptr = dir + strlen(dir)-1) == '/')
279                         *cptr = '\0';
280
281                 /* Add to list */
282                 nincludepath++;
283                 includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath));
284                 includepath[nincludepath-1] = dir;
285                 tok = strtok(NULL, INCLUDESEPARATOR);
286         }
287         free(cpy);
288 }
289
290 FILE *open_include(const char *name, int search, char **newpath)
291 {
292         char *cpy = xstrdup(name);
293         char *cptr;
294         FILE *fp;
295         int i;
296
297         for(cptr = cpy; *cptr; cptr++)
298         {
299                 /* kill double backslash */
300                 if(*cptr == '\\' && *(cptr+1) == '\\')
301                         memmove(cptr, cptr+1, strlen(cptr));
302                 /* Convert to forward slash */
303                 if(*cptr == '\\')
304                         *cptr = '/';
305         }
306
307         if(search)
308         {
309                 /* Search current dir and then -I path */
310                 fp = fopen(cpy, "rt");
311                 if(fp)
312                 {
313                         if(debuglevel & DEBUGLEVEL_PPMSG)
314                                 printf("Going to include <%s>\n", name);
315                         if(newpath)
316                                 *newpath = cpy;
317                         else
318                                 free(cpy);
319                         return fp;
320                 }
321         }
322         /* Search -I path */
323         for(i = 0; i < nincludepath; i++)
324         {
325                 char *path;
326                 path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
327                 strcpy(path, includepath[i]);
328                 strcat(path, "/");
329                 strcat(path, cpy);
330                 fp = fopen(path, "rt");
331                 if(fp && (debuglevel & DEBUGLEVEL_PPMSG))
332                         printf("Going to include <%s>\n", path);
333                 if(fp)
334                 {
335                         if(newpath)
336                                 *newpath = path;
337                         else
338                                 free(path);
339                         free(cpy);
340                         return fp;
341                 }
342                 free(path);
343         }
344         free(cpy);
345         if(newpath)
346                 *newpath = NULL;
347         return NULL;
348 }
349
350 /*
351  *-------------------------------------------------------------------------
352  * #if, #ifdef, #ifndef, #else, #elif and #endif state management
353  *
354  * #if state transitions are made on basis of the current TOS and the next
355  * required state. The state transitions are required to housekeep because
356  * #if:s can be nested. The ignore case is activated to prevent output from
357  * within a false clause.
358  * Some special cases come from the fact that the #elif cases are not
359  * binary, but three-state. The problem is that all other elif-cases must
360  * be false when one true one has been found. A second problem is that the
361  * #else clause is a final clause. No extra #else:s may follow.
362  *
363  * The states mean:
364  * if_true      Process input to output
365  * if_false     Process input but no output
366  * if_ignore    Process input but no output
367  * if_elif      Process input but no output
368  * if_elsefalse Process input but no output
369  * if_elsettrue Process input to output
370  *
371  * The possible state-sequences are [state(stack depth)] (rest can be deduced):
372  *      TOS             #if 1           #else                   #endif
373  *      if_true(n)      if_true(n+1)    if_elsefalse(n+1)
374  *      if_false(n)     if_ignore(n+1)  if_ignore(n+1)
375  *      if_elsetrue(n)  if_true(n+1)    if_elsefalse(n+1)
376  *      if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)
377  *      if_elif(n)      if_ignore(n+1)  if_ignore(n+1)
378  *      if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)
379  *
380  *      TOS             #if 1           #elif 0         #else           #endif
381  *      if_true(n)      if_true(n+1)    if_elif(n+1)    if_elif(n+1)
382  *      if_false(n)     if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
383  *      if_elsetrue(n)  if_true(n+1)    if_elif(n+1)    if_elif(n+1)
384  *      if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
385  *      if_elif(n)      if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
386  *      if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
387  *
388  *      TOS             #if 0           #elif 1         #else           #endif
389  *      if_true(n)      if_false(n+1)   if_true(n+1)    if_elsefalse(n+1)
390  *      if_false(n)     if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
391  *      if_elsetrue(n)  if_false(n+1)   if_true(n+1)    if_elsefalse(n+1)
392  *      if_elsefalse(n) if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
393  *      if_elif(n)      if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
394  *      if_ignore(n)    if_ignore(n+1)  if_ignore(n+1)  if_ignore(n+1)
395  *
396  *-------------------------------------------------------------------------
397  */
398 static char *if_state_str[] = {
399         "if_false",
400         "if_true",
401         "if_elif",
402         "if_elsefalse",
403         "if_elsetrue",
404         "if_ignore"
405 };
406
407 void push_if(if_state_t s)
408 {
409         if(if_stack_idx >= MAXIFSTACK)
410                 internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
411
412         if(debuglevel & DEBUGLEVEL_PPLEX)
413                 fprintf(stderr, "Push if %s:%d: %s(%d) -> %s(%d)\n", input_name, line_number, if_state_str[if_state()], if_stack_idx, if_state_str[s], if_stack_idx+1);
414
415         if_stack[if_stack_idx++] = s;
416
417         switch(s)
418         {
419         case if_true:
420         case if_elsetrue:
421                 break;
422         case if_false:
423         case if_elsefalse:
424         case if_elif:
425         case if_ignore:
426                 push_ignore_state();
427                 break;
428         }
429 }
430
431 if_state_t pop_if(void)
432 {
433         if(if_stack_idx <= 0)
434                 yyerror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
435
436         switch(if_state())
437         {
438         case if_true:
439         case if_elsetrue:
440                 break;
441         case if_false:
442         case if_elsefalse:
443         case if_elif:
444         case if_ignore:
445                 pop_ignore_state();
446                 break;
447         }
448
449         if(debuglevel & DEBUGLEVEL_PPLEX)
450                 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
451                                 input_name,
452                                 line_number,
453                                 if_state_str[if_state()],
454                                 if_stack_idx,
455                                 if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
456                                 if_stack_idx-1);
457
458         return if_stack[--if_stack_idx];
459 }
460
461 if_state_t if_state(void)
462 {
463         if(!if_stack_idx)
464                 return if_true;
465         else
466                 return if_stack[if_stack_idx-1];
467 }
468
469
470 void next_if_state(int i)
471 {
472         switch(if_state())
473         {
474         case if_true:
475         case if_elsetrue:
476                 push_if(i ? if_true : if_false);
477                 break;
478         case if_false:
479         case if_elsefalse:
480         case if_elif:
481         case if_ignore:
482                 push_if(if_ignore);
483                 break;
484         default:
485                 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #{if,ifdef,ifndef} directive", (int)if_state());
486         }
487 }
488
489 int get_if_depth(void)
490 {
491         return if_stack_idx;
492 }
493