2 * Copyright 1998 Bertho A. Stultiens (BS)
18 extern void set_pp_ignore(int); /* From parser.l */
20 static char *current_define
;
23 static struct pp_entry
*pp_defines
[HASHKEY
];
26 static struct if_state ifstack
[MAXIFSTACK
];
27 static int ifstackidx
= 0;
37 printf("Defines statistics:\n");
38 for(i
= 0; i
< HASHKEY
; i
++)
41 for(ppp
= pp_defines
[i
]; ppp
; ppp
= ppp
->next
)
44 printf("%4d, %3d\n", i
, sum
);
46 printf("Total defines: %d\n", total
);
48 #pragma exit pp_status
51 /* Don't comment on the hash, its primitive but functional... */
52 int pp_hash(char *str
)
60 struct pp_entry
*pp_lookup(char *ident
)
62 int index
= pp_hash(ident
);
64 for(ppp
= pp_defines
[index
]; ppp
; ppp
= ppp
->next
)
66 if(!strcmp(ident
, ppp
->ident
))
72 void set_define(char *name
)
74 current_define
= xstrdup(name
);
77 void del_define(char *name
)
82 if((ppp
= pp_lookup(name
)) == NULL
)
85 yywarning("%s was not defined", name
);
89 index
= pp_hash(name
);
90 if(pp_defines
[index
] == ppp
)
92 pp_defines
[index
] = ppp
->next
;
94 pp_defines
[index
]->prev
= NULL
;
98 ppp
->prev
->next
= ppp
->next
;
100 ppp
->next
->prev
= ppp
->prev
;
105 void add_define(char *text
)
109 int index
= pp_hash(current_define
);
110 struct pp_entry
*ppp
;
111 if(pp_lookup(current_define
) != NULL
)
114 yywarning("Redefinition of %s", current_define
);
115 del_define(current_define
);
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
;
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]))
128 ppp
->subst
[--len
] = '\0';
130 /* Strip leading white space from subst text */
131 for(cptr
= ppp
->subst
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
133 if(ppp
->subst
!= cptr
)
134 memmove(ppp
->subst
, cptr
, strlen(cptr
)+1);
136 printf("Added (%s, %d) <%s> to <%s>\n", input_name
, line_number
, ppp
->ident
, ppp
->subst
);
139 void add_cmdline_define(char *set
)
141 char *cpy
= xstrdup(set
); /* Because gcc passes a R/O string */
142 char *cptr
= strchr(cpy
, '=');
146 add_define(cptr
? cptr
+1 : "");
150 #if defined(_Windows) || defined(__MSDOS__)
151 #define INCLUDESEPARATOR ";"
153 #define INCLUDESEPARATOR ":"
156 static char **includepath
;
157 static int nincludepath
= 0;
159 void add_include_path(char *path
)
162 char *cpy
= xstrdup(path
);
164 tok
= strtok(cpy
, INCLUDESEPARATOR
);
172 for(cptr
= dir
; *cptr
; cptr
++)
174 /* Convert to forward slash */
178 /* Kill eventual trailing '/' */
179 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
184 includepath
= (char **)xrealloc(includepath
, nincludepath
* sizeof(*includepath
));
185 includepath
[nincludepath
-1] = dir
;
186 tok
= strtok(NULL
, INCLUDESEPARATOR
);
191 FILE *open_include(const char *name
, int search
)
193 char *cpy
= xstrdup(name
);
198 for(cptr
= cpy
; *cptr
; cptr
++)
200 /* kill double backslash */
201 if(*cptr
== '\\' && *(cptr
+1) == '\\')
202 memmove(cptr
, cptr
+1, strlen(cptr
));
203 /* Convert to forward slash */
210 /* Search current dir and then -I path */
211 fp
= fopen(name
, "rt");
215 printf("Going to include <%s>\n", name
);
221 for(i
= 0; i
< nincludepath
; i
++)
224 path
= (char *)xmalloc(strlen(includepath
[i
]) + strlen(cpy
) + 2);
225 strcpy(path
, includepath
[i
]);
228 fp
= fopen(path
, "rt");
230 printf("Going to include <%s>\n", path
);
243 void push_if(int truecase
, int wastrue
, int nevertrue
)
245 if(ifstackidx
>= MAXIFSTACK
-1)
246 internal_error(__FILE__
, __LINE__
, "#if stack overflow");
247 ifstack
[ifstackidx
].current
= truecase
&& !wastrue
;
248 ifstack
[ifstackidx
].hasbeentrue
= wastrue
;
249 ifstack
[ifstackidx
].nevertrue
= nevertrue
;
250 if(nevertrue
|| !(truecase
&& !wastrue
))
253 printf("push_if: %d %d %d (%d %d %d)\n",
257 ifstack
[ifstackidx
].current
,
258 ifstack
[ifstackidx
].hasbeentrue
,
259 ifstack
[ifstackidx
].nevertrue
);
266 yyerror("#endif without #if|#ifdef|#ifndef (#if stack underflow)");
269 printf("pop_if: %d %d %d\n",
270 ifstack
[ifstackidx
].current
,
271 ifstack
[ifstackidx
].hasbeentrue
,
272 ifstack
[ifstackidx
].nevertrue
);
273 if(ifstack
[ifstackidx
].nevertrue
|| !ifstack
[ifstackidx
].current
)
275 return ifstack
[ifstackidx
].hasbeentrue
|| ifstack
[ifstackidx
].current
;
278 int isnevertrue_if(void)
280 return ifstackidx
> 0 && ifstack
[ifstackidx
-1].nevertrue
;