Import 2.2.8pre4
[davej-history.git] / scripts / tkgen.c
blob279072d7833d91a55d989c45795905dc45175009
1 /* Generate tk script based upon config.in
3 * Version 1.0
4 * Eric Youngdale
5 * 10/95
7 * 1996 01 04
8 * Avery Pennarun - Aesthetic improvements.
10 * 1996 01 24
11 * Avery Pennarun - Bugfixes and more aesthetics.
13 * 1996 03 08
14 * Avery Pennarun - The int and hex config.in commands work right.
15 * - Choice buttons are more user-friendly.
16 * - Disabling a text entry line greys it out properly.
17 * - dep_tristate now works like in Configure. (not pretty)
18 * - No warnings in gcc -Wall. (Fixed some "interesting" bugs.)
19 * - Faster/prettier "Help" lookups.
21 * 1996 03 15
22 * Avery Pennarun - Added new sed script from Axel Boldt to make help even
23 * faster. (Actually awk is downright slow on some machines.)
24 * - Fixed a bug I introduced into Choice dependencies. Thanks
25 * to Robert Krawitz for pointing this out.
27 * 1996 03 16
28 * Avery Pennarun - basic "do_make" support added to let sound config work.
30 * 1996 03 25
31 * Axel Boldt - Help now works on "choice" buttons.
33 * 1996 04 06
34 * Avery Pennarun - Improved sound config stuff. (I think it actually works
35 * now!)
36 * - Window-resize-limits don't use ugly /usr/lib/tk4.0 hack.
37 * - int/hex work with tk3 again. (The "cget" error.)
38 * - Next/Prev buttons switch between menus. I can't take
39 * much credit for this; the code was already there, but
40 * ifdef'd out for some reason. It flickers a lot, but
41 * I suspect there's no "easy" fix for that.
42 * - Labels no longer highlight as you move the mouse over
43 * them (although you can still press them... oh well.)
44 * - Got rid of the last of the literal color settings, to
45 * help out people with mono X-Windows systems.
46 * (Apparently there still are some out there!)
47 * - Tabstops seem sensible now.
49 * 1996 04 14
50 * Avery Pennarun - Reduced flicker when creating windows, even with "update
51 * idletasks" hack.
53 * 1997 12 08
54 * Michael Chastain - Remove sound driver special cases.
56 * 1997 11 15
57 * Michael Chastain - For choice buttons, write values for all options,
58 * not just the single chosen one. This is compatible
59 * with 'make config' and 'make oldconfig', and is
60 * needed so smart-config dependencies work if the
61 * user switches from one configuration method to
62 * another.
64 * 1998 03 09
65 * Axel Boldt - Smaller layout of main menu - it's still too big for 800x600.
66 * - Display help in text window to allow for cut and paste.
67 * - Allow for empty lines in help texts.
68 * - update_define should not set all variables unconditionally to
69 * 0: they may have been set to 1 elsewhere. CONFIG_NETLINK is
70 * an example.
72 * 1999 01 04
73 * Michael Elizabeth Chastain <mec@shout.net>
74 * - Call clear_globalflags when writing out update_mainmenu.
75 * This fixes the missing global/vfix lines for ARCH=alpha on 2.2.0-pre4.
77 * 8 January 1999, Michael Elizabeth Chastain <mec@shout.net>
78 * - Emit menus_per_column
80 * 14 January 1999, Michael Elizabeth Chastain <mec@shout.net>
81 * - Steam-clean this file. I tested this by generating kconfig.tk for every
82 * architecture and comparing it character-for-character against the output
83 * of the old tkparse.
84 * - Fix flattening of nested menus. The old code simply assigned items to
85 * the most recent token_mainmenu_option, without paying attention to scope.
86 * For example: "menu-1 bool-a menu-2 bool-b endmenu bool-c bool-d endmenu".
87 * The old code would put bool-a in menu-1, bool-b in menu-2, and bool-c
88 * and bool-d in *menu-2*. This hosed the nested submenus in
89 * drives/net/Config.in and other places.
90 * - Fix menu line wraparound at 128 menus (some fool used a 'char' for
91 * a counter).
93 * 23 January 1999, Michael Elizabeth Chastain <mec@shout.net>
94 * - Remove bug-compatible code.
97 #include <stdio.h>
98 #include <unistd.h>
99 #include "tkparse.h"
104 * Total number of menus.
106 static int tot_menu_num = 0;
111 * Generate portion of wish script for the beginning of a submenu.
112 * The guts get filled in with the various options.
114 static void start_proc( char * label, int menu_num, int flag )
116 if ( flag )
117 printf( "menu_option menu%d %d \"%s\"\n", menu_num, menu_num, label );
118 printf( "proc menu%d {w title} {\n", menu_num );
119 printf( "\tcatch {destroy $w}\n" );
120 printf( "\ttoplevel $w -class Dialog\n" );
121 printf( "\twm withdraw $w\n" );
122 printf( "\tmessage $w.m -width 400 -aspect 300 -text \\\n" );
123 printf( "\t\t\"%s\" -relief raised\n", label );
124 printf( "\tpack $w.m -pady 10 -side top -padx 10\n" );
125 printf( "\twm title $w \"%s\" \n\n", label );
128 * Attach the "Prev", "Next" and "OK" buttons at the end of the window.
130 printf( "\tset oldFocus [focus]\n" );
131 printf( "\tframe $w.f\n" );
132 printf( "\tbutton $w.f.back -text \"Main Menu\" \\\n" );
133 printf( "\t\t-width 15 -command \"destroy $w; focus $oldFocus; update_mainmenu $w\"\n" );
134 printf( "\tbutton $w.f.next -text \"Next\" \\\n" );
135 printf( "\t\t-width 15 -command \" destroy $w; focus $oldFocus; menu%d .menu%d \\\"$title\\\"\"\n", menu_num+1, menu_num+1 );
136 if ( menu_num == tot_menu_num )
137 printf( "\t$w.f.next configure -state disabled\n" );
138 printf( "\tbutton $w.f.prev -text \"Prev\" \\\n" );
139 printf( "\t\t-width 15 -command \" destroy $w; focus $oldFocus; menu%d .menu%d \\\"$title\\\"\"\n", menu_num-1, menu_num-1 );
140 if ( menu_num == 1 )
141 printf( "\t$w.f.prev configure -state disabled\n" );
142 printf( "\tpack $w.f.back $w.f.next $w.f.prev -side left -expand on\n" );
143 printf( "\tpack $w.f -pady 10 -side bottom -anchor w -fill x\n" );
146 * Lines between canvas and other areas of the window.
148 printf( "\tframe $w.topline -relief ridge -borderwidth 2 -height 2\n" );
149 printf( "\tpack $w.topline -side top -fill x\n\n" );
150 printf( "\tframe $w.botline -relief ridge -borderwidth 2 -height 2\n" );
151 printf( "\tpack $w.botline -side bottom -fill x\n\n" );
154 * The "config" frame contains the canvas and a scrollbar.
156 printf( "\tframe $w.config\n" );
157 printf( "\tpack $w.config -fill y -expand on\n\n" );
158 printf( "\tscrollbar $w.config.vscroll -command \"$w.config.canvas yview\"\n" );
159 printf( "\tpack $w.config.vscroll -side right -fill y\n\n" );
162 * The scrollable canvas itself, where the real work (and mess) gets done.
164 printf( "\tcanvas $w.config.canvas -height 1\\\n" );
165 printf( "\t\t-relief flat -borderwidth 0 -yscrollcommand \"$w.config.vscroll set\" \\\n" );
166 printf( "\t\t-width [expr [winfo screenwidth .] * 1 / 2] \n" );
167 printf( "\tframe $w.config.f\n" );
168 printf( "\tpack $w.config.canvas -side right -fill y\n" );
169 printf("\n\n");
175 * Each proc we create needs a global declaration for any global variables we
176 * use. To minimize the size of the file, we set a flag each time we output
177 * a global declaration so we know whether we need to insert one for a
178 * given function or not.
180 void clear_globalflags( struct kconfig * scfg )
182 struct kconfig * cfg;
184 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
185 cfg->global_written = 0;
191 * Output a "global" line for a given variable. Also include the
192 * call to "vfix". (If vfix is not needed, then it's fine to just printf
193 * a "global" line).
195 void global( const char *var )
197 printf( "\tglobal %s; vfix %s\n", var, var );
203 * This function walks the chain of conditions that we got from cond.c
204 * and creates a TCL conditional to enable/disable a given widget.
206 void generate_if( struct kconfig * cfg, struct condition * ocond,
207 int menu_num, int line_num )
209 struct condition * cond;
212 * First write any global declarations we need for this conditional.
214 for ( cond = ocond; cond != NULL; cond = cond->next )
216 switch ( cond->op )
218 default:
219 break;
221 case op_variable:
222 global( cond->str );
223 break;
225 case op_kvariable:
226 if ( ! cond->cfg->global_written )
228 cond->cfg->global_written = 1;
229 global( cond->cfg->optionname );
231 break;
236 * Now write this option.
238 if ( ! cfg->global_written && cfg->optionname != NULL )
240 cfg->global_written = 1;
241 global( cfg->optionname );
245 * Generate the body of the conditional.
247 printf( "\tif {" );
248 for ( cond = ocond; cond != NULL; cond = cond->next )
250 switch ( cond->op )
252 default:
253 break;
255 case op_bang: printf( " ! " ); break;
256 case op_eq: printf( " == " ); break;
257 case op_neq: printf( " != " ); break;
258 case op_and: printf( " && " ); break;
259 case op_and1: printf( " && " ); break;
260 case op_or: printf( " || " ); break;
261 case op_lparen: printf( "(" ); break;
262 case op_rparen: printf( ")" ); break;
264 case op_variable:
265 printf( "$%s", cond->str );
266 break;
268 case op_kvariable:
269 printf( "$%s", cond->cfg->optionname );
270 break;
272 case op_constant:
273 if ( strcmp( cond->str, "y" ) == 0 ) printf( "1" );
274 else if ( strcmp( cond->str, "n" ) == 0 ) printf( "0" );
275 else if ( strcmp( cond->str, "m" ) == 0 ) printf( "2" );
276 else
277 printf( "\"%s\"", cond->str );
278 break;
281 printf( "} then { " );
284 * Generate a procedure call to write the value.
285 * This code depends on procedures in header.tk.
287 switch ( cfg->token )
289 default:
290 printf( " }\n" );
291 break;
293 case token_bool:
294 printf( ".menu%d.config.f.x%d.y configure -state normal;",
295 menu_num, line_num );
296 printf( ".menu%d.config.f.x%d.n configure -state normal;",
297 menu_num, line_num );
298 printf( ".menu%d.config.f.x%d.l configure -state normal;",
299 menu_num, line_num );
300 printf( "set %s [expr $%s&15];",
301 cfg->optionname, cfg->optionname );
302 printf( "} else { ");
303 printf( ".menu%d.config.f.x%d.y configure -state disabled;",
304 menu_num, line_num );
305 printf( ".menu%d.config.f.x%d.n configure -state disabled;",
306 menu_num, line_num );
307 printf( ".menu%d.config.f.x%d.l configure -state disabled;",
308 menu_num, line_num );
309 printf( "set %s [expr $%s|16];}\n",
310 cfg->optionname, cfg->optionname );
311 break;
313 case token_choice_header:
314 fprintf( stderr, "Internal error on token_choice_header\n" );
315 exit( 1 );
317 case token_choice_item:
318 fprintf( stderr, "Internal error on token_choice_item\n" );
319 exit( 1 );
321 case token_define_bool:
322 printf( "set %s %s } \n",
323 cfg->optionname, cfg->value );
324 break;
326 case token_dep_tristate:
327 case token_tristate:
328 if ( cfg->token == token_dep_tristate )
330 global( cfg->depend );
331 printf( "if { $%s != 1 && $%s != 0 } then {",
332 cfg->depend, cfg->depend );
333 printf( ".menu%d.config.f.x%d.y configure -state disabled;",
334 menu_num, line_num );
335 printf( "} else {" );
336 printf( ".menu%d.config.f.x%d.y configure -state normal;",
337 menu_num, line_num);
338 printf( "}; " );
340 else
342 printf( ".menu%d.config.f.x%d.y configure -state normal;",
343 menu_num, line_num );
346 printf( ".menu%d.config.f.x%d.n configure -state normal;",
347 menu_num, line_num );
348 printf( "global CONFIG_MODULES; if {($CONFIG_MODULES == 1)} then { .menu%d.config.f.x%d.m configure -state normal };",
349 menu_num, line_num );
350 printf( ".menu%d.config.f.x%d.l configure -state normal;",
351 menu_num, line_num );
354 * Or in a bit to the variable - this causes all of the radiobuttons
355 * to be deselected (i.e. not be red).
357 printf( "set %s [expr $%s&15];",
358 cfg->optionname, cfg->optionname );
359 printf( "} else { " );
360 printf( ".menu%d.config.f.x%d.y configure -state disabled;",
361 menu_num, line_num );
362 printf( ".menu%d.config.f.x%d.n configure -state disabled;",
363 menu_num, line_num );
364 printf( ".menu%d.config.f.x%d.m configure -state disabled;",
365 menu_num, line_num );
366 printf( ".menu%d.config.f.x%d.l configure -state disabled;",
367 menu_num, line_num );
370 * Clear the disable bit to enable the correct radiobutton.
372 printf( "set %s [expr $%s|16];}\n",
373 cfg->optionname, cfg->optionname );
374 break;
376 case token_hex:
377 case token_int:
378 case token_string:
379 printf( ".menu%d.config.f.x%d.x configure -state normal -foreground [ cget .ref -foreground ]; ",
380 menu_num, line_num);
381 printf( ".menu%d.config.f.x%d.l configure -state normal; ",
382 menu_num, line_num);
383 printf( "} else { " );
384 printf( ".menu%d.config.f.x%d.x configure -state disabled -foreground [ cget .ref -disabledforeground ];",
385 menu_num, line_num );
386 printf( ".menu%d.config.f.x%d.l configure -state disabled;}\n",
387 menu_num, line_num );
388 break;
390 case token_mainmenu_option:
391 printf( ".f0.x%d configure -state normal } else { .f0.x%d configure -state disabled }\n",
392 menu_num, menu_num );
393 break;
400 * Generate a line that writes a variable to the output file.
402 void generate_writeconfig( struct kconfig * cfg )
404 struct condition * cond;
407 * Generate global declaration for this symbol.
409 if ( cfg->token != token_comment )
411 if ( ! cfg->global_written )
413 cfg->global_written = 1;
414 printf( "\tglobal %s\n", cfg->optionname );
419 * Generate global declarations for the condition chain.
421 for ( cond = cfg->cond; cond != NULL; cond = cond->next )
423 switch( cond->op )
425 default:
426 break;
428 case op_variable:
429 global( cond->str );
430 break;
432 case op_kvariable:
433 if ( ! cond->cfg->global_written )
435 cond->cfg->global_written = 1;
436 global( cond->cfg->optionname );
438 break;
443 * Generate indentation.
445 if ( cfg->token != token_choice_header )
446 printf( "\t" );
449 * Generate the conditional.
451 if ( cfg->cond != NULL )
453 printf( "if {" );
454 for ( cond = cfg->cond; cond != NULL; cond = cond->next )
456 switch ( cond->op )
458 default: break;
459 case op_bang: printf( " ! " ); break;
460 case op_eq: printf( " == " ); break;
461 case op_neq: printf( " != " ); break;
462 case op_and: printf( " && " ); break;
463 case op_and1: printf( " && " ); break;
464 case op_or: printf( " || " ); break;
465 case op_lparen: printf( "(" ); break;
466 case op_rparen: printf( ")" ); break;
468 case op_variable:
469 printf( "$%s", cond->str );
470 break;
472 case op_kvariable:
473 printf( "$%s", cond->cfg->optionname );
474 break;
476 case op_constant:
477 if ( strcmp( cond->str, "n" ) == 0 ) printf( "0" );
478 else if ( strcmp( cond->str, "y" ) == 0 ) printf( "1" );
479 else if ( strcmp( cond->str, "m" ) == 0 ) printf( "2" );
480 else
481 printf( "\"%s\"", cond->str );
482 break;
485 printf( "} then {" );
489 * Generate a procedure call to write the value.
490 * This code depends on the write_* procedures in header.tk.
492 switch ( cfg->token )
494 default:
495 if ( cfg->cond != NULL )
496 printf( " }" );
497 printf( "\n" );
498 break;
500 case token_bool:
501 case token_tristate:
502 if ( cfg->cond )
503 printf( " " );
504 printf( "write_tristate $cfg $autocfg %s $%s $notmod",
505 cfg->optionname, cfg->optionname );
506 if ( cfg->cond != NULL )
507 printf( " }" );
508 printf( "\n" );
509 break;
511 case token_choice_header:
513 * This is funky code -- it fails if there were any conditionals.
514 * Fortunately all the conditionals got stripped off somewhere
515 * else.
518 struct kconfig * cfg1;
519 for ( cfg1 = cfg->next;
520 cfg1 != NULL && cfg1->token == token_choice_item;
521 cfg1 = cfg1->next )
523 printf("\tif { $%s == \"%s\" } then { write_tristate $cfg $autocfg %s 1 $notmod } else { write_tristate $cfg $autocfg %s 0 $notmod }\n",
524 cfg->optionname, cfg1->label,
525 cfg1->optionname,
526 cfg1->optionname );
529 break;
531 case token_choice_item:
532 fprintf( stderr, "Internal error on token_choice_item\n" );
533 exit( 1 );
535 case token_comment:
536 printf( "write_comment $cfg $autocfg \"%s\"",
537 cfg->label );
538 if ( cfg->cond != NULL )
539 printf( "}" );
540 printf( "\n" );
541 break;
543 case token_define_bool:
544 if ( cfg->cond == NULL )
546 printf( "write_tristate $cfg $autocfg %s $%s $notmod\n",
547 cfg->optionname, cfg->optionname );
549 else
551 printf( "write_tristate $cfg $autocfg %s %s $notmod }\n",
552 cfg->optionname, cfg->value );
554 break;
556 case token_dep_tristate:
557 if ( cfg->cond )
558 printf( " " );
559 printf( "write_tristate $cfg $autocfg %s $%s $%s",
560 cfg->optionname, cfg->optionname, cfg->depend );
561 if ( cfg->cond != NULL )
562 printf( " }" );
563 printf( " \n" );
564 break;
566 case token_hex:
567 if ( cfg->cond != NULL )
568 printf( " " );
569 printf( "write_hex $cfg $autocfg %s $%s $notmod",
570 cfg->optionname, cfg->optionname );
571 if ( cfg->cond != NULL )
572 printf( " }" );
573 printf( "\n" );
574 break;
576 case token_int:
577 if ( cfg->cond != NULL )
578 printf( " " );
579 printf( "write_int $cfg $autocfg %s $%s $notmod",
580 cfg->optionname, cfg->optionname );
581 if ( cfg->cond != NULL )
582 printf( " }" );
583 printf( "\n" );
584 break;
586 case token_string:
587 if ( cfg->cond != NULL )
588 printf( " " );
589 printf( "write_string $cfg $autocfg %s $%s $notmod",
590 cfg->optionname, cfg->optionname );
591 if ( cfg->cond != NULL )
592 printf( " }" );
593 printf( "\n" );
594 break;
601 * Generates the end of a menu procedure.
603 static void end_proc( struct kconfig * scfg, int menu_num )
605 struct kconfig * cfg;
607 printf( "\n\n\n" );
608 printf( "\tfocus $w\n" );
609 printf( "\tupdate_menu%d $w.config.f\n",
610 menu_num );
611 printf( "\tglobal winx; global winy\n" );
612 printf( "\tset winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]\n" );
613 printf( "\twm geometry $w +$winx+$winy\n" );
616 * Now that the whole window is in place, we need to wait for an "update"
617 * so we can tell the canvas what its virtual size should be.
619 * Unfortunately, this causes some ugly screen-flashing because the whole
620 * window is drawn, and then it is immediately resized. It seems
621 * unavoidable, though, since "frame" objects won't tell us their size
622 * until after an update, and "canvas" objects can't automatically pack
623 * around frames. Sigh.
625 printf( "\tupdate idletasks\n" );
626 printf( "\t$w.config.canvas create window 0 0 -anchor nw -window $w.config.f\n\n" );
627 printf( "\t$w.config.canvas configure \\\n" );
628 printf( "\t\t-width [expr [winfo reqwidth $w.config.f] + 1]\\\n" );
629 printf( "\t\t-scrollregion \"-1 -1 [expr [winfo reqwidth $w.config.f] + 1] \\\n" );
630 printf( "\t\t\t [expr [winfo reqheight $w.config.f] + 1]\"\n\n" );
633 * If the whole canvas will fit in 3/4 of the screen height, do it;
634 * otherwise, resize to around 1/2 the screen and let us scroll.
636 printf( "\tset winy [expr [winfo reqh $w] - [winfo reqh $w.config.canvas]]\n" );
637 printf( "\tset scry [expr [winfo screenh $w] / 2]\n" );
638 printf( "\tset maxy [expr [winfo screenh $w] * 3 / 4]\n" );
639 printf( "\tset canvtotal [expr [winfo reqh $w.config.f] + 2]\n" );
640 printf( "\tif [expr $winy + $canvtotal < $maxy] {\n" );
641 printf( "\t\t$w.config.canvas configure -height $canvtotal\n" );
642 printf( "\t} else {\n" );
643 printf( "\t\t$w.config.canvas configure -height [expr $scry - $winy]\n" );
644 printf( "\t}\n" );
647 * Limit the min/max window size. Height can vary, but not width,
648 * because of the limitations of canvas and our laziness.
650 printf( "\tupdate idletasks\n" );
651 printf( "\twm maxsize $w [winfo width $w] [winfo screenheight $w]\n" );
652 printf( "\twm minsize $w [winfo width $w] 100\n\n" );
653 printf( "\twm deiconify $w\n" );
654 printf( "}\n\n\n" );
657 * Now we generate the companion procedure for the menu we just
658 * generated. This procedure contains all of the code to
659 * disable/enable widgets based upon the settings of the other
660 * widgets, and will be called first when the window is mapped,
661 * and each time one of the buttons in the window are clicked.
663 printf( "proc update_menu%d {w} {\n", menu_num );
664 printf( "\tupdate_define\n" );
667 * Clear all of the booleans that are defined in this menu.
669 clear_globalflags( scfg );
670 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
672 if ( cfg->menu_number == menu_num && cfg->token == token_define_bool
673 && cfg->optionname != NULL )
675 if ( ! cfg->global_written )
677 cfg->global_written = 1;
678 printf( "\tglobal %s\n", cfg->optionname );
679 printf( "\tset %s 0\n", cfg->optionname );
684 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
686 if ( cfg->menu_number == menu_num
687 && cfg->token != token_mainmenu_option
688 && cfg->token != token_choice_item )
690 if ( cfg->cond != NULL )
691 generate_if( cfg, cfg->cond, cfg->menu_number, cfg->menu_line );
692 else
695 * Treat tristate like conditional here.
697 if ( cfg->token == token_dep_tristate )
699 global( cfg->depend );
700 printf( "\tif {$%s != 1 && $%s != 0 } then { .menu%d.config.f.x%d.y configure -state disabled } else { .menu%d.config.f.x%d.y configure -state normal}\n",
701 cfg->depend, cfg->depend,
702 menu_num, cfg->menu_line,
703 menu_num, cfg->menu_line );
709 printf("}\n\n\n");
715 * This is the top level function for generating the tk script.
717 void dump_tk_script( struct kconfig * scfg )
719 int menu_depth;
720 int menu_num [64];
721 struct kconfig * menu_first [256];
722 struct kconfig * menu_last [256];
723 int imenu;
724 struct kconfig * cfg;
725 struct kconfig * cfg1 = NULL;
726 const char * name = "No Name";
729 * Thread the menu pointers so I can walk each menu separately.
731 tot_menu_num = 0;
732 menu_depth = 0;
733 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
735 switch ( cfg->token )
737 default:
738 break;
740 case token_mainmenu_name:
741 name = cfg->label;
742 break;
744 case token_mainmenu_option:
745 if ( ++menu_depth >= 64 )
746 { fprintf( stderr, "menus too deep\n" ); exit( 1 ); }
747 if ( ++tot_menu_num >= 256 )
748 { fprintf( stderr, "too many menus\n" ); exit( 1 ); }
749 menu_num [menu_depth] = tot_menu_num;
750 menu_first [tot_menu_num] = cfg;
751 menu_last [tot_menu_num] = cfg;
752 break;
754 case token_endmenu:
755 /* flatten menus with proper scoping */
756 if ( --menu_depth < 0 )
757 { fprintf( stderr, "unmatched endmenu\n" ); exit( 1 ); }
758 break;
760 case token_bool:
761 case token_choice_header:
762 case token_choice_item:
763 case token_dep_tristate:
764 case token_hex:
765 case token_int:
766 case token_string:
767 case token_tristate:
768 if ( menu_depth == 0 )
769 { fprintf( stderr, "statement not in menu\n" ); exit( 1 ); }
770 menu_last [menu_num [menu_depth]]->menu_next = cfg;
771 menu_last [menu_num [menu_depth]] = cfg;
772 cfg->menu_next = NULL;
773 break;
775 case token_define_bool:
776 break;
781 * Generate menus per column setting.
782 * There are:
783 * four extra buttons for save/quit/load/store;
784 * one blank button
785 * add two to round up for division
787 printf( "set menus_per_column %d\n\n", (tot_menu_num + 4 + 1 + 2) / 3 );
790 * Generate the menus.
792 printf( "mainmenu_name \"%s\"\n", name );
793 for ( imenu = 1; imenu <= tot_menu_num; ++imenu )
795 int menu_line = 0;
797 clear_globalflags( scfg );
798 start_proc( menu_first[imenu]->label, imenu, 1 );
800 for ( cfg = menu_first[imenu]; cfg != NULL; cfg = cfg->menu_next )
802 cfg->menu_number = imenu;
804 switch ( cfg->token )
806 default:
807 break;
809 case token_bool:
810 cfg->menu_line = menu_line++;
811 printf( "\tbool $w.config.f %d %d \"%s\" %s\n",
812 cfg->menu_number, cfg->menu_line, cfg->label,
813 cfg->optionname );
814 break;
816 case token_choice_header:
818 * I need the first token_choice_item to pick out the right
819 * help text from Documentation/Configure.help.
821 cfg->menu_line = menu_line++;
822 printf( "\tglobal %s\n", cfg->optionname );
823 printf( "\tminimenu $w.config.f %d %d \"%s\" %s %s\n",
824 cfg->menu_number, cfg->menu_line, cfg->label,
825 cfg->optionname, cfg->next->optionname );
826 printf( "\tmenu $w.config.f.x%d.x.menu\n", cfg->menu_line );
827 cfg1 = cfg;
828 break;
830 case token_choice_item:
831 /* note: no menu line; uses choice header menu line */
832 printf( "\t$w.config.f.x%d.x.menu add radiobutton -label \"%s\" -variable %s -value \"%s\" -command \"update_menu%d .menu%d.config.f\"\n",
833 cfg1->menu_line, cfg->label, cfg1->optionname,
834 cfg->label, cfg1->menu_number, cfg1->menu_number );
835 break;
837 case token_dep_tristate:
838 cfg->menu_line = menu_line++;
839 printf( "\tdep_tristate $w.config.f %d %d \"%s\" %s %s\n",
840 cfg->menu_number, cfg->menu_line, cfg->label,
841 cfg->optionname, cfg->depend );
842 break;
844 case token_hex:
845 cfg->menu_line = menu_line++;
846 printf( "\thex $w.config.f %d %d \"%s\" %s\n",
847 cfg->menu_number, cfg->menu_line, cfg->label,
848 cfg->optionname );
849 break;
851 case token_int:
852 cfg->menu_line = menu_line++;
853 printf( "\tint $w.config.f %d %d \"%s\" %s\n",
854 cfg->menu_number, cfg->menu_line, cfg->label,
855 cfg->optionname );
856 break;
858 case token_string:
859 cfg->menu_line = menu_line++;
860 printf( "\tistring $w.config.f %d %d \"%s\" %s\n",
861 cfg->menu_number, cfg->menu_line, cfg->label,
862 cfg->optionname );
863 break;
865 case token_tristate:
866 cfg->menu_line = menu_line++;
867 printf( "\ttristate $w.config.f %d %d \"%s\" %s\n",
868 cfg->menu_number, cfg->menu_line, cfg->label,
869 cfg->optionname );
870 break;
874 end_proc( scfg, imenu );
878 * The top level menu also needs an update function. When we exit a
879 * submenu, we may need to disable one or more of the submenus on
880 * the top level menu, and this procedure will ensure that things are
881 * correct.
883 clear_globalflags( scfg );
884 printf( "proc update_mainmenu {w} {\n" );
885 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
887 if ( cfg->token == token_mainmenu_option && cfg->cond != NULL )
888 generate_if( cfg, cfg->cond, cfg->menu_number, cfg->menu_line );
890 printf( "}\n\n\n" );
892 #if 0
894 * Generate code to set the variables that are "defined".
896 for ( cfg = config; cfg != NULL; cfg = cfg->next )
898 if ( cfg->token == token_define_bool )
900 if ( cfg->cond != NULL )
901 generate_if( cfg, cfg->cond, menu_num, cfg->menu_line );
902 else
903 printf( "\twrite_define %s %s\n", cfg->optionname, cfg->value );
906 #endif
909 * Generate code to load the default settings into the variables.
910 * The script in tail.tk will attempt to load .config,
911 * which may override these settings, but that's OK.
913 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
915 switch ( cfg->token )
917 default:
918 break;
920 case token_bool:
921 case token_choice_item:
922 case token_dep_tristate:
923 case token_tristate:
924 printf( "set %s 0\n", cfg->optionname );
925 break;
927 case token_choice_header:
928 printf( "set %s \"(not set)\"\n", cfg->optionname );
929 break;
931 case token_hex:
932 case token_int:
933 printf( "set %s %s\n", cfg->optionname, cfg->value ? cfg->value : "0");
934 break;
936 case token_string:
937 printf( "set %s \"%s\"\n", cfg->optionname, cfg->value ? cfg->value : "");
938 break;
943 * Generate a function to write all of the variables to a file.
945 printf( "proc writeconfig {file1 file2} {\n" );
946 printf( "\tset cfg [open $file1 w]\n" );
947 printf( "\tset autocfg [open $file2 w]\n" );
948 printf( "\tset notmod 1\n" );
949 printf( "\tset notset 0\n" );
950 printf( "\tputs $cfg \"#\"\n");
951 printf( "\tputs $cfg \"# Automatically generated make config: don't edit\"\n");
952 printf( "\tputs $cfg \"#\"\n" );
954 printf( "\tputs $autocfg \"/*\"\n" );
955 printf( "\tputs $autocfg \" * Automatically generated C config: don't edit\"\n" );
956 printf( "\tputs $autocfg \" */\"\n" );
957 printf( "\tputs $autocfg \"#define AUTOCONF_INCLUDED\"\n" );
959 clear_globalflags( scfg );
960 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
962 switch ( cfg->token )
964 default:
965 break;
967 case token_bool:
968 case token_choice_header:
969 case token_comment:
970 case token_define_bool:
971 case token_dep_tristate:
972 case token_hex:
973 case token_int:
974 case token_string:
975 case token_tristate:
976 generate_writeconfig( cfg );
977 break;
980 printf( "\tclose $cfg\n" );
981 printf( "\tclose $autocfg\n" );
982 printf( "}\n\n\n" );
985 * Generate a simple function that updates the master choice
986 * variable depending upon what values were loaded from a .config
987 * file.
989 printf( "proc clear_choices { } {\n" );
990 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
992 if ( cfg->token == token_choice_header )
994 for ( cfg1 = cfg->next;
995 cfg1 != NULL && cfg1->token == token_choice_item;
996 cfg1 = cfg1->next )
998 printf( "\tglobal %s; set %s 0\n",
999 cfg1->optionname, cfg1->optionname );
1003 printf( "}\n\n\n" );
1005 printf( "proc update_choices { } {\n" );
1006 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
1008 if ( cfg->token == token_choice_header )
1010 printf( "\tglobal %s\n", cfg->optionname );
1011 for ( cfg1 = cfg->next;
1012 cfg1 != NULL && cfg1->token == token_choice_item;
1013 cfg1 = cfg1->next )
1015 printf( "\tglobal %s\n", cfg1->optionname );
1016 printf( "\tif { $%s == 1 } then { set %s \"%s\" }\n",
1017 cfg1->optionname, cfg->optionname, cfg1->label );
1021 printf( "}\n\n\n" );
1023 printf( "proc update_define { } {\n" );
1024 clear_globalflags( scfg );
1025 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
1027 if ( cfg->token == token_define_bool )
1029 cfg->global_written = 1;
1030 printf( "\tglobal %s\n", cfg->optionname );
1034 for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
1036 if( cfg->token == token_define_bool )
1038 if ( cfg->cond == NULL )
1039 printf( "\tset %s %s\n", cfg->optionname, cfg->value );
1040 else
1041 generate_if( cfg, cfg->cond, -1, 0 );
1044 printf( "}\n\n\n" );
1047 * That's it. We are done. The output of this file will have header.tk
1048 * prepended and tail.tk appended to create an executable wish script.