1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include "tkparse.h"
36
37
38
39
40
41
42static void invert_condition(struct condition * cnd)
43{
44
45
46
47
48 for(;cnd; cnd = cnd->next)
49 {
50 switch(cnd->op)
51 {
52 case op_and:
53 cnd->op = op_or;
54 break;
55 case op_or:
56
57
58
59
60
61
62
63
64 cnd->op = op_and1;
65 break;
66 case op_neq:
67 cnd->op = op_eq;
68 break;
69 case op_eq:
70 cnd->op = op_neq;
71 break;
72 default:
73 break;
74 }
75 }
76}
77
78
79
80
81static void free_condition(struct condition * cnd)
82{
83 struct condition * next;
84 for(;cnd; cnd = next)
85 {
86 next = cnd->next;
87
88 if( cnd->variable.str != NULL )
89 free(cnd->variable.str);
90
91 free(cnd);
92 }
93}
94
95
96
97
98
99void fix_choice_cond()
100{
101 struct condition * cond;
102 struct condition * cond2;
103 struct kconfig * cfg;
104 char tmpbuf[10];
105
106 for(cfg = config;cfg != NULL; cfg = cfg->next)
107 {
108 if( cfg->cond == NULL )
109 {
110 continue;
111 }
112
113 for(cond = cfg->cond; cond != NULL; cond = cond->next)
114 {
115 if( cond->op != op_kvariable )
116 continue;
117
118 if( cond->variable.cfg->tok != tok_choice )
119 continue;
120
121
122
123
124
125 cond2 = cond->next->next;
126 strcpy(tmpbuf, cond->variable.cfg->label);
127
128 if( strcmp(cond2->variable.str, "y") == 0 )
129 {
130 cond->variable.cfg = cond->variable.cfg->choice_label;
131 cond2->variable.str = strdup(tmpbuf);
132 }
133 else
134 {
135 fprintf(stderr,"Ooops\n");
136 exit(0);
137 }
138 }
139
140 }
141}
142
143
144
145
146
147
148
149struct condition * get_token_cond(struct condition ** cond, int depth)
150{
151 int i;
152 struct condition * newcond;
153 struct condition * tail;
154 struct condition * new;
155 struct condition * ocond;
156 struct kconfig * cfg;
157
158 newcond = tail = NULL;
159 for(i=0; i<depth; i++, cond++)
160 {
161
162
163
164 new = (struct condition *) malloc(sizeof(struct condition));
165 memset(new, 0, sizeof(*new));
166 new->op = op_lparen;
167 if( tail == NULL )
168 {
169 newcond = tail = new;
170 }
171 else
172 {
173 tail->next = new;
174 tail = new;
175 }
176
177
178
179
180 ocond = *cond;
181 for(;ocond != NULL; ocond = ocond->next)
182 {
183 new = (struct condition *) malloc(sizeof(struct condition));
184 memset(new, 0, sizeof(*new));
185 new->op = ocond->op;
186 if( ocond->variable.str != NULL )
187 {
188 if( ocond->op == op_variable )
189 {
190
191
192
193 for(cfg = config;cfg != NULL; cfg = cfg->next)
194 {
195 if( cfg->tok != tok_bool
196 && cfg->tok != tok_int
197 && cfg->tok != tok_hex
198 && cfg->tok != tok_string
199 && cfg->tok != tok_tristate
200 && cfg->tok != tok_choice
201 && cfg->tok != tok_dep_tristate)
202 {
203 continue;
204 }
205 if( strcmp(cfg->optionname, ocond->variable.str) == 0)
206 {
207 new->variable.cfg = cfg;
208 new->op = op_kvariable;
209 break;
210 }
211 }
212 if( cfg == NULL )
213 {
214 new->variable.str = strdup(ocond->variable.str);
215 }
216 }
217 else
218 {
219 new->variable.str = strdup(ocond->variable.str);
220 }
221 }
222 tail->next = new;
223 tail = new;
224 }
225
226
227
228
229 new = (struct condition *) malloc(sizeof(struct condition));
230 memset(new, 0, sizeof(*new));
231 new->op = op_rparen;
232 tail->next = new;
233 tail = new;
234
235
236
237
238 if( i < depth - 1 )
239 {
240 new = (struct condition *) malloc(sizeof(struct condition));
241 memset(new, 0, sizeof(*new));
242 new->op = op_and;
243 tail->next = new;
244 tail = new;
245 }
246
247 }
248
249 return newcond;
250}
251
252
253
254
255
256struct condition * get_token_cond_frag(struct condition * cond,
257 struct condition ** last)
258{
259 struct condition * newcond;
260 struct condition * tail;
261 struct condition * new;
262 struct condition * ocond;
263
264 newcond = tail = NULL;
265
266
267
268
269 for(ocond = cond;ocond != NULL; ocond = ocond->next)
270 {
271 new = (struct condition *) malloc(sizeof(struct condition));
272 memset(new, 0, sizeof(*new));
273 new->op = ocond->op;
274 new->variable.cfg = ocond->variable.cfg;
275 if( tail == NULL )
276 {
277 newcond = tail = new;
278 }
279 else
280 {
281 tail->next = new;
282 tail = new;
283 }
284 }
285
286 new = (struct condition *) malloc(sizeof(struct condition));
287 memset(new, 0, sizeof(*new));
288 new->op = op_and;
289 tail->next = new;
290 tail = new;
291
292 *last = tail;
293 return newcond;
294}
295
296
297
298
299void fix_conditionals(struct kconfig * scfg)
300{
301 int depth = 0;
302 int i;
303 struct kconfig * cfg;
304 struct kconfig * cfg1;
305 struct condition * conditions[25];
306 struct condition * cnd;
307 struct condition * cnd1;
308 struct condition * cnd2;
309 struct condition * cnd3;
310 struct condition * newcond;
311 struct condition * last;
312
313
314
315
316
317
318
319
320
321
322
323
324
325 memset(conditions, 0, sizeof(conditions));
326 for(cfg=scfg;cfg != NULL; cfg = cfg->next)
327 {
328 switch(cfg->tok)
329 {
330 case tok_if:
331
332
333
334
335 conditions[depth] = cfg->cond;
336 depth++;
337 cfg->tok = tok_nop;
338 cfg->cond = NULL;
339 break;
340 case tok_else:
341
342
343
344
345
346 invert_condition(conditions[depth-1]);
347 cfg->tok = tok_nop;
348 break;
349 case tok_fi:
350 depth--;
351 free_condition(conditions[depth]);
352 conditions[depth] = NULL;
353 cfg->tok = tok_nop;
354 break;
355 case tok_comment:
356 case tok_define:
357 case tok_menuoption:
358 case tok_bool:
359 case tok_tristate:
360 case tok_int:
361 case tok_hex:
362 case tok_string:
363 case tok_choice:
364 case tok_make:
365
366
367
368
369 cfg->cond = get_token_cond(&conditions[0], depth);
370 break;
371 case tok_dep_tristate:
372
373
374
375
376
377 conditions[depth] = cfg->cond;
378 depth++;
379 cfg->cond = get_token_cond(&conditions[0], depth);
380 depth--;
381 free_condition(conditions[depth]);
382 conditions[depth] = NULL;
383 default:
384 break;
385 }
386 }
387
388
389
390
391 fix_choice_cond();
392
393
394
395
396
397
398 for(cfg=scfg;cfg != NULL; cfg = cfg->next)
399 {
400 switch(cfg->tok)
401 {
402 case tok_bool:
403 case tok_tristate:
404 case tok_dep_tristate:
405 case tok_int:
406 case tok_hex:
407 case tok_string:
408 for(cfg1=cfg;cfg1 != NULL; cfg1 = cfg1->next)
409 {
410 switch(cfg1->tok)
411 {
412 case tok_define:
413 case tok_bool:
414 case tok_tristate:
415 case tok_dep_tristate:
416 case tok_int:
417 case tok_hex:
418 case tok_string:
419 if( strcmp(cfg->optionname, cfg1->optionname) == 0)
420 {
421 cfg->flags |= CFG_DUP;
422 cfg1->flags |= CFG_DUP;
423 }
424 break;
425 default:
426 break;
427 }
428 }
429 break;
430 default:
431 break;
432 }
433 }
434
435
436
437
438
439
440
441
442
443
444
445
446 for(cfg=scfg;cfg != NULL; cfg = cfg->next)
447 {
448
449
450
451 if(cfg->cond == NULL) continue;
452 for(cnd = cfg->cond; cnd; cnd=cnd->next)
453 {
454
455
456
457
458 if(cnd->op != op_kvariable) continue;
459 if(cnd->variable.cfg->cond == NULL) continue;
460
461 if(cnd->variable.cfg->flags & CFG_DUP) continue;
462
463
464
465
466 newcond = get_token_cond_frag(cnd->variable.cfg->cond, &last);
467
468
469
470
471 last->next = cfg->cond;
472 cfg->cond = newcond;
473
474 }
475 }
476
477
478
479
480
481
482
483 for(cfg=scfg;cfg != NULL; cfg = cfg->next)
484 {
485
486
487
488 if(cfg->cond == NULL) continue;
489 for(cnd = cfg->cond; cnd; cnd=cnd->next)
490 {
491
492
493
494 if(cnd->op != op_lparen) continue;
495 for(cnd1 = cnd->next; cnd1; cnd1=cnd1->next)
496 {
497
498
499
500
501 if(cnd1->op != op_lparen) continue;
502
503
504
505
506
507
508 cnd2 = cnd;
509 cnd3 = cnd1;
510 for(i=0; i<5; i++, cnd2=cnd2->next, cnd3=cnd3->next)
511 {
512 if(!cnd2 || !cnd3) break;
513 if(cnd2->op != cnd3->op) break;
514 if(i == 1 && (cnd2->op != op_kvariable
515 || cnd2->variable.cfg != cnd3->variable.cfg) ) break;
516 if(i==2 && cnd2->op != op_eq && cnd2->op != op_neq) break;
517 if(i == 3 && cnd2->op != op_constant &&
518 strcmp(cnd2->variable.str, cnd3->variable.str) != 0)
519 break;
520 if(i==4 && cnd2->op != op_rparen) break;
521 }
522
523
524
525
526 if(i==5 && ((cnd3 && cnd3->op == op_and)
527 ||(cnd2 && cnd2->op == op_and)))
528 {
529
530
531
532 cnd3 = cnd1;
533 for(i=0; i<5; i++, cnd3=cnd3->next)
534 {
535 cnd3->op = op_nuked;
536 }
537
538
539
540 if(cnd3 && cnd3->op == op_and) cnd3->op = op_nuked;
541 else if(cnd2 && cnd2->op == op_and) cnd2->op = op_nuked;
542 }
543 }
544 }
545 }
546}
547