summaryrefslogtreecommitdiffstats
path: root/scripts/tkcond.c
blob: 34c923fe9e5f7921a47584268a4c7824fb0555d2 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
 * tkcond.c
 *
 * Eric Youngdale was the original author of xconfig.
 * Michael Elizabeth Chastain (mec@shout.net) is the current maintainer.
 *
 * This file takes the tokenized statement list and transforms 'if ...'
 * statements.  For each simple statement, I find all of the 'if' statements
 * that enclose it, and attach the aggregate conditionals of those 'if'
 * statements to the cond list of the simple statement.
 *
 * 14 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
 * - Steam-clean this file.  I tested this by generating kconfig.tk for
 *   every architecture and comparing it character-for-character against
 *   the output of the old tkparse.
 *
 * 07 July 1999, Andrzej M. Krzysztofowicz <ankry@mif.pg.gda.pl>
 * - kvariables removed; all variables are stored in a single table now
 * - some elimination of options non-valid for current architecture
 *   implemented.
 * - negation (!) eliminated from conditions
 *
 * TO DO:
 * - xconfig is at the end of its life cycle.  Contact <mec@shout.net> if
 *   you are interested in working on the replacement.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tkparse.h"



/*
 * Mark variables which are defined anywhere.
 */
static void mark_variables( struct kconfig * scfg )
{
    struct kconfig * cfg;
    int i;

    for ( i = 1; i <= max_varnum; i++ )
	vartable[i].defined = 0;
    for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
    {
	if ( cfg->token == token_bool
	||   cfg->token == token_choice_item
	||   cfg->token == token_define_bool
	||   cfg->token == token_define_hex
	||   cfg->token == token_define_int
	||   cfg->token == token_define_string
	||   cfg->token == token_define_tristate
	||   cfg->token == token_dep_bool
	||   cfg->token == token_dep_mbool
	||   cfg->token == token_dep_tristate
	||   cfg->token == token_hex
	||   cfg->token == token_int
	||   cfg->token == token_string
	||   cfg->token == token_tristate
	||   cfg->token == token_unset )
	{
	    if ( cfg->nameindex > 0 )	/* paranoid */
	    {
		vartable[cfg->nameindex].defined = 1;
	    }
	}
    }
}



static void free_cond( struct condition *cond )
{
    struct condition *tmp, *tmp1;
    for ( tmp = cond; tmp; tmp = tmp1 )
    {
	tmp1 = tmp->next;
	free( (void*)tmp );
    }
}



/*
 * Remove the bang operator from a condition to avoid priority problems.
 * "!" has different priorities as "test" command argument and in 
 * a tk script.
 */
static struct condition * remove_bang( struct condition * condition )
{
    struct condition * conda, * condb, * prev = NULL;

    for ( conda = condition; conda; conda = conda->next )
    {
	if ( conda->op == op_bang && conda->next &&
	   ( condb = conda->next->next ) )
	{
	    if ( condb->op == op_eq || condb->op == op_neq )
	    {
		condb->op = (condb->op == op_eq) ? op_neq : op_eq;
		conda->op = op_nuked;
		if ( prev )
		{
		    prev->next = conda->next;
		}
		else
		{
		    condition = conda->next;
		}
		conda->next = NULL;
		free_cond( conda );
		conda = condb;
	    }
	}
	prev = conda;
    }
    return condition;
}



/*
 * Make a new condition chain by joining the current condition stack with
 * the "&&" operator for glue.
 */
static struct condition * join_condition_stack( struct condition * conditions [],
    int depth )
{
    struct condition * cond_list;
    struct condition * cond_last;
    int i, is_first = 1;

    cond_list = cond_last = NULL;

    for ( i = 0; i < depth; i++ )
    {
	if ( conditions[i]->op == op_false )
	{
	    struct condition * cnew;

	    /* It is always false condition */
	    cnew = malloc( sizeof(*cnew) );
	    memset( cnew, 0, sizeof(*cnew) );
	    cnew->op = op_false;
	    cond_list = cond_last = cnew;
	    goto join_done;
	}
    }
    for ( i = 0; i < depth; i++ )
    {
	struct condition * cond;
	struct condition * cnew;
	int add_paren;

	/* omit always true conditions */
	if ( conditions[i]->op == op_true )
	    continue;

	/* if i have another condition, add an '&&' operator */
	if ( !is_first )
	{
	    cnew = malloc( sizeof(*cnew) );
	    memset( cnew, 0, sizeof(*cnew) );
	    cnew->op = op_and;
	    cond_last->next = cnew;
	    cond_last = cnew;
	}

	if ( conditions[i]->op != op_lparen )
	{
	    /* add a '(' */
	    add_paren = 1;
	    cnew = malloc( sizeof(*cnew) );
	    memset( cnew, 0, sizeof(*cnew) );
	    cnew->op = op_lparen;
	    if ( cond_last == NULL )
		{ cond_list = cond_last = cnew; }
	    else
		{ cond_last->next = cnew; cond_last = cnew; }
	}
	else
	{
	    add_paren = 0;
	}

	/* duplicate the chain */
	for ( cond = conditions [i]; cond != NULL; cond = cond->next )
	{
	    cnew            = malloc( sizeof(*cnew) );
	    cnew->next      = NULL;
	    cnew->op        = cond->op;
	    cnew->str       = cond->str ? strdup( cond->str ) : NULL;
	    cnew->nameindex = cond->nameindex;
	    if ( cond_last == NULL )
		{ cond_list = cond_last = cnew; }
	    else
		{ cond_last->next = cnew; cond_last = cnew; }
	}

	if ( add_paren )
	{
	    /* add a ')' */
	    cnew = malloc( sizeof(*cnew) );
	    memset( cnew, 0, sizeof(*cnew) );
	    cnew->op = op_rparen;
	    cond_last->next = cnew;
	    cond_last = cnew;
	}
	is_first = 0;
    }

    /*
     * Remove duplicate conditions.
     */
    {
	struct condition *cond1, *cond1b, *cond1c, *cond1d, *cond1e, *cond1f;

	for ( cond1 = cond_list; cond1 != NULL; cond1 = cond1->next )
	{
	    if ( cond1->op == op_lparen )
	    {
		cond1b = cond1 ->next; if ( cond1b == NULL ) break;
		cond1c = cond1b->next; if ( cond1c == NULL ) break;
		cond1d = cond1c->next; if ( cond1d == NULL ) break;
		cond1e = cond1d->next; if ( cond1e == NULL ) break;
		cond1f = cond1e->next; if ( cond1f == NULL ) break;

		if ( cond1b->op == op_variable
		&& ( cond1c->op == op_eq || cond1c->op == op_neq )
		&&   cond1d->op == op_constant 
		&&   cond1e->op == op_rparen )
		{
		    struct condition *cond2, *cond2b, *cond2c, *cond2d, *cond2e, *cond2f;

		    for ( cond2 = cond1f->next; cond2 != NULL; cond2 = cond2->next )
		    {
			if ( cond2->op == op_lparen )
			{
			    cond2b = cond2 ->next; if ( cond2b == NULL ) break;
			    cond2c = cond2b->next; if ( cond2c == NULL ) break;
			    cond2d = cond2c->next; if ( cond2d == NULL ) break;
			    cond2e = cond2d->next; if ( cond2e == NULL ) break;
			    cond2f = cond2e->next;

			    /* look for match */
			    if ( cond2b->op == op_variable
			    &&   cond2b->nameindex == cond1b->nameindex
			    &&   cond2c->op == cond1c->op
			    &&   cond2d->op == op_constant
			    &&   strcmp( cond2d->str, cond1d->str ) == 0
			    &&   cond2e->op == op_rparen )
			    {
				/* one of these must be followed by && */
				if ( cond1f->op == op_and
				|| ( cond2f != NULL && cond2f->op == op_and ) )
				{
				    /* nuke the first duplicate */
				    cond1 ->op = op_nuked;
				    cond1b->op = op_nuked;
				    cond1c->op = op_nuked;
				    cond1d->op = op_nuked;
				    cond1e->op = op_nuked;
				    if ( cond1f->op == op_and )
					cond1f->op = op_nuked;
				    else
					cond2f->op = op_nuked;
				}
			    }
			}
		    }
		}
	    }
	}
    }

join_done:
    return cond_list;
}



static char * current_arch = NULL;

/*
 * Eliminating conditions with ARCH = <not current>.
 */
static struct condition *eliminate_other_arch( struct condition *list )
{
    struct condition *cond1a = list, *cond1b = NULL, *cond1c = NULL, *cond1d = NULL;
    if ( current_arch == NULL )
	current_arch = getenv( "ARCH" );
    if ( current_arch == NULL )
    {
	fprintf( stderr, "error: ARCH undefined\n" );
	exit( 1 );
    }
    if ( cond1a->op == op_variable
    && ! strcmp( vartable[cond1a->nameindex].name, "ARCH" ) )
    {
	cond1b = cond1a->next; if ( cond1b == NULL ) goto done;
	cond1c = cond1b->next; if ( cond1c == NULL ) goto done;
	cond1d = cond1c->next;
	if ( cond1c->op == op_constant && cond1d == NULL )
	{
	    if ( (cond1b->op == op_eq && strcmp( cond1c->str, current_arch ))
	    ||   (cond1b->op == op_neq && ! strcmp( cond1c->str, current_arch )) )
	    {
		/* This is for another architecture */ 
		cond1a->op = op_false;
		cond1a->next = NULL;
		free_cond( cond1b );
		return cond1a;
	    }
	    else if ( (cond1b->op == op_neq && strcmp( cond1c->str, current_arch ))
		 ||   (cond1b->op == op_eq && ! strcmp( cond1c->str, current_arch )) )
	    {
		/* This is for current architecture */
		cond1a->op = op_true;
		cond1a->next = NULL;
		free_cond( cond1b );
		return cond1a;
	    }
	}
	else if ( cond1c->op == op_constant && cond1d->op == op_or )
	{
	    if ( (cond1b->op == op_eq && strcmp( cond1c->str, current_arch ))
	    ||   (cond1b->op == op_neq && ! strcmp( cond1c->str, current_arch )) )
	    {
		/* This is for another architecture */ 
		cond1b = cond1d->next;
		cond1d->next = NULL;
		free_cond( cond1a );
		return eliminate_other_arch( cond1b );
	    }
	    else if ( (cond1b->op == op_neq && strcmp( cond1c->str, current_arch ))
		 || (cond1b->op == op_eq && ! strcmp( cond1c->str, current_arch )) )
	    {
		/* This is for current architecture */
		cond1a->op = op_true;
		cond1a->next = NULL;
		free_cond( cond1b );
		return cond1a;
	    }
	}
	else if ( cond1c->op == op_constant && cond1d->op == op_and )
	{
	    if ( (cond1b->op == op_eq && strcmp( cond1c->str, current_arch ))
	    ||   (cond1b->op == op_neq && ! strcmp( cond1c->str, current_arch )) )
	    {
		/* This is for another architecture */
		int l_par = 0;
		
		for ( cond1c = cond1d->next; cond1c; cond1c = cond1c->next )
		{
		    if ( cond1c->op == op_lparen )
			l_par++;
		    else if ( cond1c->op == op_rparen )
			l_par--;
		    else if ( cond1c->op == op_or && l_par == 0 )
		    /* Expression too complex - don't touch */
			return cond1a;
		    else if ( l_par < 0 )
		    {
			fprintf( stderr, "incorrect condition: programming error ?\n" );
			exit( 1 );
		    }
		}
		cond1a->op = op_false;
		cond1a->next = NULL;
		free_cond( cond1b );
		return cond1a;
	    }
	    else if ( (cond1b->op == op_neq && strcmp( cond1c->str, current_arch ))
		 || (cond1b->op == op_eq && ! strcmp( cond1c->str, current_arch )) )
	    {
		/* This is for current architecture */
		cond1b = cond1d->next;
		cond1d->next = NULL;
		free_cond( cond1a );
		return eliminate_other_arch( cond1b );
	    }
	}
    }
    if ( cond1a->op == op_variable && ! vartable[cond1a->nameindex].defined )
    {
	cond1b = cond1a->next; if ( cond1b == NULL ) goto done;
	cond1c = cond1b->next; if ( cond1c == NULL ) goto done;
	cond1d = cond1c->next;

	if ( cond1c->op == op_constant
	&& ( cond1d == NULL || cond1d->op == op_and ) ) /*???*/
	{
	    if ( cond1b->op == op_eq && strcmp( cond1c->str, "" ) )
	    {
		cond1a->op = op_false;
		cond1a->next = NULL;
		free_cond( cond1b );
		return cond1a;
	    }
	}
	else if ( cond1c->op == op_constant && cond1d->op == op_or )
	{
	    if ( cond1b->op == op_eq && strcmp( cond1c->str, "" ) )
	    {
		cond1b = cond1d->next;
		cond1d->next = NULL;
		free_cond( cond1a );
		return eliminate_other_arch( cond1b );
	    }
	}
    }
done:
    return list;
}



/*
 * This is the main transformation function.
 */
void fix_conditionals( struct kconfig * scfg )
{
    struct kconfig * cfg;

    /*
     * Transform op_variable to op_kvariable.
     */
    mark_variables( scfg );

    /*
     * Walk the statement list, maintaining a stack of current conditions.
     *   token_if      push its condition onto the stack.
     *   token_else    invert the condition on the top of the stack.
     *   token_endif   pop the stack.
     *
     * For a simple statement, create a condition chain by joining together
     * all of the conditions on the stack.
     */
    {
	struct condition * cond_stack [32];
	int depth = 0;
	struct kconfig * prev = NULL;

	for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
	{
	    int good = 1;
	    switch ( cfg->token )
	    {
	    default:
		break;

	    case token_if:
		cond_stack [depth++] =
		    remove_bang( eliminate_other_arch( cfg->cond ) );
		cfg->cond = NULL;
		break;

	    case token_else:
		{
		    /*
		     * Invert the condition chain.
		     *
		     * Be careful to transfrom op_or to op_and1, not op_and.
		     * I will need this later in the code that removes
		     * duplicate conditions.
		     */
		    struct condition * cond;

		    for ( cond  = cond_stack [depth-1];
			  cond != NULL;
			  cond  = cond->next )
		    {
			switch( cond->op )
			{
			default:     break;
			case op_and: cond->op = op_or;   break;
			case op_or:  cond->op = op_and1; break;
			case op_neq: cond->op = op_eq;   break;
			case op_eq:  cond->op = op_neq;  break;
			case op_true: cond->op = op_false;break;
			case op_false:cond->op = op_true; break;
			}
		    }
		}
		break;

	    case token_fi:
		--depth;
		break;

	    case token_bool:
	    case token_choice_item:
	    case token_choice_header:
	    case token_comment:
	    case token_define_bool:
	    case token_define_hex:
	    case token_define_int:
	    case token_define_string:
	    case token_define_tristate:
	    case token_endmenu:
	    case token_hex:
	    case token_int:
	    case token_mainmenu_option:
	    case token_string:
	    case token_tristate:
	    case token_unset:
		cfg->cond = join_condition_stack( cond_stack, depth );
		if ( cfg->cond && cfg->cond->op == op_false )
		{
		    good = 0;
		    if ( prev )
			prev->next = cfg->next;
		    else
			scfg = cfg->next;
		}
		break;

	    case token_dep_bool:
	    case token_dep_mbool:
	    case token_dep_tristate:
		/*
		 * Same as the other simple statements, plus an additional
		 * condition for the dependency.
		 */
		if ( cfg->cond )
		{
		    cond_stack [depth] = eliminate_other_arch( cfg->cond );
		    cfg->cond = join_condition_stack( cond_stack, depth+1 );
		}
		else
		{
		    cfg->cond = join_condition_stack( cond_stack, depth );
		}
		if ( cfg->cond && cfg->cond->op == op_false )
		{
		    good = 0;
		    if ( prev )
			prev->next = cfg->next;
		    else
			scfg = cfg->next;
		}
		break;
	    }
	    if ( good )
		prev = cfg;
	}
    }
}



#if 0
void dump_condition( struct condition *list )
{
    struct condition *tmp;
    for ( tmp = list; tmp; tmp = tmp->next )
    {
	switch (tmp->op)
	{
	default:
	    break;
	case op_variable:
	    printf( " %s", vartable[tmp->nameindex].name );
	    break;
	case op_constant: 
	    printf( " %s", tmp->str );
	    break;
	case op_eq:
	    printf( " =" );
	    break;
	case op_bang:
	    printf( " !" );
	    break;
	case op_neq:
	    printf( " !=" );
	    break;
	case op_and:
	case op_and1:
	    printf( " -a" );
	    break;
	case op_or:
	    printf( " -o" );
	    break;
	case op_true:
	    printf( " TRUE" );
	    break;
	case op_false:
	    printf( " FALSE" );
	    break;
	case op_lparen:
	    printf( " (" );
	    break;
	case op_rparen:
	    printf( " )" );
	    break;
	}
    }
    printf( "\n" );
}
#endif