1# FILE: header.tk
2# This file is boilerplate TCL/TK function definitions for 'make xconfig'.
3#
4# CHANGES
5# =======
6#
7# 8 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
8# - Remove unused do_cmd function (part of the 2.0 sound support).
9# - Arrange buttons in three columns for better screen fitting.
10# - Add CONSTANT_Y, CONSTANT_M, CONSTANT_N for commands like:
11#     dep_tristate 'foo' CONFIG_FOO m
12#
13# 23 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
14# - Shut vfix the hell up.
15#
16# 24 January 1999, Michael Elizabeth Chastain, <mec@shout.net>
17# - Improve the exit message (Jeff Ronne).
18
19#
20# This is a handy replacement for ".widget cget" that requires neither tk4
21# nor additional source code uglification.
22#
23proc cget { w option } {
24	return "[lindex [$w configure $option] 4]"
25}
26
27#
28# Function to compensate for broken config.in scripts like the sound driver,
29# which make dependencies on variables that are never even conditionally
30# defined.
31#
32proc vfix { var } {
33	global $var
34	if [ catch {eval concat $$var} ] {
35		set $var 4
36	}
37}
38
39#
40# Constant values used by certain dep_tristate commands.
41#
42set CONSTANT_Y 1
43set CONSTANT_M 2
44set CONSTANT_N 0
45set CONSTANT_E 4
46
47#
48# Create a "reference" object to steal colors from.
49#
50button .ref
51
52#
53# On monochrome displays, -disabledforeground is blank by default; that's
54# bad.  Fill it with -foreground instead.
55#
56if { [cget .ref -disabledforeground] == "" } {
57	.ref configure -disabledforeground [cget .ref -foreground]
58}
59
60
61#
62# Define some macros we will need to parse the config.in file.
63#
64
65proc mainmenu_name { text } {
66	wm title . "$text"
67}
68
69proc menu_option { w menu_num text } {
70	global menus_per_column
71	global processed_top_level
72	set processed_top_level [expr $processed_top_level + 1]
73	if { $processed_top_level <= $menus_per_column } then {
74	    set myframe left
75	} elseif { $processed_top_level <= [expr 2 * $menus_per_column] } then {
76	    set myframe middle
77	} else {
78	    set myframe right
79	}
80	button .f0.x$menu_num -anchor w -text "$text" \
81	    -command "$w .$w \"$text\""
82	pack .f0.x$menu_num -pady 0 -side top -fill x -in .f0.$myframe
83}
84
85proc load_configfile { w title func } {
86	catch {destroy $w}
87	toplevel $w -class Dialog
88	global loadfile
89	frame $w.x
90	label $w.bm -bitmap questhead
91	pack  $w.bm -pady 10 -side top -padx 10
92	label $w.x.l -text "Enter filename:" -relief raised
93	entry $w.x.x -width 35 -relief sunken -borderwidth 2 \
94		-textvariable loadfile
95	pack $w.x.l $w.x.x -anchor w -side left
96	pack $w.x -side top -pady 10
97	wm title $w "$title"
98
99	set oldFocus [focus]
100	frame $w.f
101	button $w.f.back -text "OK" -width 20 \
102		-command "destroy $w; focus $oldFocus;$func .fileio"
103	button $w.f.canc -text "Cancel" \
104		-width 20 -command "destroy $w; focus $oldFocus"
105	pack $w.f.back $w.f.canc -side left -pady 10 -padx 45
106	pack $w.f -pady 10 -side bottom -padx 10 -anchor w
107	focus $w
108	global winx; global winy
109	set winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]
110	wm geometry $w +$winx+$winy
111}
112
113bind all <Alt-q> {maybe_exit .maybe}
114
115proc maybe_exit { w } {
116	catch {destroy $w}
117	toplevel $w -class Dialog
118	label $w.bm -bitmap questhead
119	pack  $w.bm -pady 10 -side top -padx 10
120	message $w.m -width 400 -aspect 300 \
121		-text "Changes will be lost.  Are you sure?" -relief flat
122	pack  $w.m -pady 10 -side top -padx 10
123	wm title $w "Are you sure?"
124
125	set oldFocus [focus]
126	frame $w.f
127	button $w.f.back -text "OK" -width 20 \
128		-command "exit"
129	button $w.f.canc -text "Cancel" \
130		-width 20 -command "destroy $w; focus $oldFocus"
131	pack $w.f.back $w.f.canc -side left -pady 10 -padx 45
132	pack $w.f -pady 10 -side bottom -padx 10 -anchor w
133  	bind $w <Return> "exit"
134    	bind $w <Escape> "destroy $w; focus $oldFocus"
135	focus $w
136	global winx; global winy
137	set winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]
138	wm geometry $w +$winx+$winy
139}
140
141proc read_config_file { w } {
142	global loadfile
143	if { [string length $loadfile] != 0 && [file readable $loadfile] == 1 } then {
144		read_config $loadfile
145	} else {
146		catch {destroy $w}
147		toplevel $w -class Dialog
148		message $w.m -width 400 -aspect 300 -text \
149			"Unable to read file $loadfile" \
150			 -relief raised
151		label $w.bm -bitmap error
152		pack $w.bm $w.m -pady 10 -side top -padx 10
153		wm title $w "Xconfig Internal Error"
154
155		set oldFocus [focus]
156		frame $w.f
157		button $w.f.back -text "Bummer" \
158			-width 10 -command "destroy $w; focus $oldFocus"
159		pack $w.f.back -side bottom -pady 10 -anchor s
160		pack $w.f -pady 10 -side top -padx 10 -anchor s
161		focus $w
162		global winx; global winy
163		set winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]
164		wm geometry $w +$winx+$winy
165	}
166}
167
168proc write_config_file  { w } {
169	global loadfile
170	if { [string length $loadfile] != 0
171    		&& ([file writable $loadfile] == 1 || ([file exists $loadfile] == 0 && [file writable [file dirname $loadfile]] == 1)) } then {
172		writeconfig $loadfile /dev/null
173	} else {
174		catch {destroy $w}
175		toplevel $w -class Dialog
176		message $w.m -width 400 -aspect 300 -text \
177			"Unable to write file $loadfile" \
178			 -relief raised
179		label $w.bm -bitmap error
180		pack $w.bm $w.m -pady 10 -side top -padx 10
181		wm title $w "Xconfig Internal Error"
182
183		set oldFocus [focus]
184		frame $w.f
185		button $w.f.back -text "OK" \
186			-width 10 -command "destroy $w; focus $oldFocus"
187		pack $w.f.back -side bottom -pady 10 -anchor s
188		pack $w.f -pady 10 -side top -padx 10 -anchor s
189		focus $w
190		global winx; global winy
191		set winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]
192		wm geometry $w +$winx+$winy
193	}
194}
195
196proc read_config { filename } {
197	set file1 [open $filename r]
198	clear_choices
199	while { [gets $file1 line] >= 0} {
200		if [regexp {([0-9A-Za-z_]+)=([ynm])} $line foo var value] {
201			if { $value == "y" } then { set cmd "global $var; set $var 1" }
202			if { $value == "n" } then { set cmd "global $var; set $var 0" }
203			if { $value == "m" } then { set cmd "global $var; set $var 2" }
204			eval $cmd
205		}
206		if [regexp {# ([0-9A-Za-z_]+) is not set} $line foo var] {
207			set cmd "global $var; set $var 0"
208			eval $cmd
209		}
210		if [regexp {([0-9A-Za-z_]+)=([0-9A-Fa-f]+)} $line foo var value] {
211			set cmd "global $var; set $var $value"
212			eval $cmd
213		}
214		if [regexp {([0-9A-Za-z_]+)="([^"]*)"} $line foo var value] {
215			set cmd "global $var; set $var \"$value\""
216			eval $cmd
217		}
218	}
219	close $file1
220	update_choices
221	update_mainmenu
222}
223proc write_comment { file1 file2 text } {
224    	puts $file1 ""
225	puts $file1 "#"
226	puts $file1 "# $text"
227	puts $file1 "#"
228	puts $file2 "/*"
229	puts $file2 " * $text"
230	puts $file2 " */"
231}
232
233proc effective_dep { deplist } {
234	global CONFIG_MODULES
235	set depend 1
236	foreach i $deplist {
237		if {$i == 0} then {set depend 0}
238		if {$i == 2 && $depend == 1} then {set depend 2}
239	}
240	if {$depend == 2 && $CONFIG_MODULES == 0} then {set depend 0}
241	return $depend
242}
243
244proc sync_tristate { var dep } {
245	global CONFIG_MODULES
246	if {$dep == 0 && ($var == 1 || $var == 2)} then {
247		set var 0
248	} elseif {$dep == 2 && $var == 1} then {
249		set var 2
250	} elseif {$var == 2 && $CONFIG_MODULES == 0} then {
251		if {$dep == 1} then {set var 1} else {set var 0}
252	}
253	return $var
254}
255
256proc sync_bool { var dep modset } {
257	set var [sync_tristate $var $dep]
258	if {$dep == 2 && $var == 2} then {
259		set var $modset
260	}
261	return $var
262}
263
264proc write_tristate { file1 file2 varname variable deplist modset } {
265	set variable [sync_tristate $variable [effective_dep $deplist]]
266	if { $variable == 2 } \
267		then { set variable $modset }
268	if { $variable == 1 } \
269		then { puts $file1 "$varname=y"; \
270		       puts $file2 "#define $varname 1" } \
271	elseif { $variable == 2 } \
272		then { puts $file1 "$varname=m"; \
273		       puts $file2 "#undef  $varname"; \
274		       puts $file2 "#define ${varname}_MODULE 1" } \
275	elseif { $variable == 0 } \
276		then { puts $file1 "# $varname is not set"; \
277		       puts $file2 "#undef  $varname"} \
278	else { \
279	    puts stdout "ERROR - Attempting to write value for unconfigured variable ($varname)." \
280	}
281}
282
283proc write_int { file1 file2 varname variable dep } {
284	if { $dep == 0 } \
285		then { puts $file1 "# $varname is not set"; \
286		       puts $file2 "#undef  $varname"} \
287	else {
288		puts $file1 "$varname=$variable"; \
289		puts $file2 "#define $varname ($variable)"; \
290	}
291}
292
293proc write_hex { file1 file2 varname variable dep } {
294	if { $dep == 0 } \
295		then { puts $file1 "# $varname is not set"; \
296		       puts $file2 "#undef  $varname"} \
297	else {
298		puts $file1 "$varname=$variable"; \
299		puts -nonewline $file2 "#define $varname 0x"; \
300		puts $file2 [exec echo $variable | sed s/^0\[xX\]//]; \
301	}
302}
303
304proc write_string { file1 file2 varname variable dep } {
305	if { $dep == 0 } \
306		then { puts $file1 "# $varname is not set"; \
307		       puts $file2 "#undef  $varname"} \
308	else {
309		puts $file1 "$varname=\"$variable\""; \
310		puts $file2 "#define $varname \"$variable\""; \
311	}
312}
313
314proc option_name {w mnum line text helpidx} {
315	button $w.x$line.l -text "$text" -relief groove -anchor w
316	$w.x$line.l configure -activefore [cget $w.x$line.l -fg] \
317				-activeback [cget $w.x$line.l -bg]
318	button $w.x$line.help -text "Help" -relief raised \
319		-command "dohelp .dohelp $helpidx .menu$mnum"
320	pack $w.x$line.help -side right -fill y
321	pack $w.x$line.l -side right -fill both -expand on
322}
323
324proc toggle_switch2 {w mnum line text variable} {
325	frame $w.x$line -relief sunken
326	radiobutton $w.x$line.y -text "y" -variable $variable -value 1 \
327		-relief groove -width 2 -command "update_active"
328	radiobutton $w.x$line.m -text "-"  -variable $variable -value 2 \
329		-relief groove -width 2 -command "update_active"
330	radiobutton $w.x$line.n -text "n"  -variable $variable -value 0 \
331		-relief groove -width 2 -command "update_active"
332
333	option_name $w $mnum $line $text $variable
334
335	pack $w.x$line.n $w.x$line.m $w.x$line.y -side right -fill y
336}
337
338proc toggle_switch3 {w mnum line text variable} {
339	frame $w.x$line -relief sunken
340	radiobutton $w.x$line.y -text "y" -variable $variable -value 1 \
341		-relief groove -width 2 -command "update_active"
342	radiobutton $w.x$line.m -text "m"  -variable $variable -value 2 \
343		-relief groove -width 2 -command "update_active"
344	radiobutton $w.x$line.n -text "n"  -variable $variable -value 0 \
345		-relief groove -width 2 -command "update_active"
346
347	option_name $w $mnum $line $text $variable
348
349	global CONFIG_MODULES
350	if {($CONFIG_MODULES == 0)} then {
351		$w.x$line.m configure -state disabled
352	}
353	pack $w.x$line.n $w.x$line.m $w.x$line.y -side right -fill y
354}
355
356proc bool {w mnum line text variable} {
357	toggle_switch2 $w $mnum $line $text $variable
358	$w.x$line.m configure -state disabled
359	pack $w.x$line -anchor w -fill both -expand on
360}
361
362proc tristate {w mnum line text variable } {
363	toggle_switch3 $w $mnum $line $text $variable
364	pack $w.x$line -anchor w -fill both -expand on
365}
366
367proc dep_tristate {w mnum line text variable } {
368	tristate $w $mnum $line $text $variable
369}
370
371proc dep_bool {w mnum line text variable } {
372	bool $w $mnum $line $text $variable
373}
374
375proc int { w mnum line text variable } {
376	frame $w.x$line
377	entry $w.x$line.x -width 18 -relief sunken -borderwidth 2 \
378		-textvariable $variable
379	option_name $w $mnum $line $text $variable
380	pack $w.x$line.x -anchor w -side right -fill y
381	pack $w.x$line -anchor w -fill both -expand on
382}
383
384proc hex { w mnum line text variable } {
385	int $w $mnum $line $text $variable
386}
387
388proc istring { w mnum line text variable } {
389	frame $w.x$line
390	entry $w.x$line.x -width 18 -relief sunken -borderwidth 2 \
391		-textvariable $variable
392	option_name $w $mnum $line $text $variable
393	pack $w.x$line.x -anchor w -side right -fill y
394	pack $w.x$line -anchor w -fill both -expand on
395}
396
397proc minimenu { w mnum line text variable helpidx } {
398	frame $w.x$line
399	menubutton $w.x$line.x -textvariable $variable -menu \
400		$w.x$line.x.menu -relief raised \
401		-anchor w
402	option_name $w $mnum $line $text $helpidx
403	pack $w.x$line.x -anchor w -side right -fill y
404	pack $w.x$line -anchor w -fill both -expand on
405}
406
407proc menusplit {w m n} {
408	if { $n > 2 } then {
409		update idletasks
410		set menuoptsize [expr [$m yposition 2] - [$m yposition 1]]
411		set maxsize [winfo screenheight $w]
412		set splitpoint [expr $maxsize * 4 / 5 / $menuoptsize - 1]
413		for {set i [expr $splitpoint + 1]} {$i <= $n} {incr i $splitpoint} {
414			$m entryconfigure $i -columnbreak 1
415		}
416	}
417}
418
419proc menutitle {text menu w} {
420	wm title $w "$text"
421}
422
423proc submenu { w mnum line text subnum } {
424	frame $w.x$line
425	button $w.x$line.l -text "" -width 15 -relief groove
426	$w.x$line.l configure -activefore [cget $w.x$line.l -fg] \
427		-activeback [cget $w.x$line.l -bg] -state disabled
428	button $w.x$line.m -text "$text" -relief raised -anchor w \
429		-command "catch {destroy .menu$subnum}; menu$subnum .menu$subnum \"$text\""
430	pack $w.x$line.l -side left -fill both
431	pack $w.x$line.m -anchor w -side right -fill both -expand on
432	pack $w.x$line -anchor w -fill both -expand on
433}
434
435proc comment {w mnum line text } {
436	frame $w.x$line
437	button $w.x$line.l -text "" -width 15 -relief groove
438	$w.x$line.l configure -activefore [cget $w.x$line.l -fg] \
439		-activeback [cget $w.x$line.l -bg] -state disabled
440	button $w.x$line.m -text "$text" -relief groove -anchor w
441	$w.x$line.m configure -activefore [cget $w.x$line.m -fg] \
442		-activeback [cget $w.x$line.m -bg]
443	pack $w.x$line.l -side left -fill both
444	pack $w.x$line.m -anchor w -side right -fill both -expand on
445	pack $w.x$line -anchor w -fill both -expand on
446}
447
448proc dohelp {w var parent}  {
449	catch {destroy $w}
450	toplevel $w -class Dialog
451
452	set filefound 0
453	set found 0
454	set lineno 0
455
456	if { [file readable Documentation/Configure.help] == 1} then {
457		set filefound 1
458		# First escape sed regexp special characters in var:
459		set var [exec echo "$var" | sed s/\[\]\[\/.^$*\]/\\\\&/g]
460		# Now pick out right help text:
461		set message [exec sed -n "
462			/^$var\[ 	\]*\$/,\${
463				/^$var\[ 	\]*\$/c\\
464${var}:\\
465
466				/^#/b
467				/^\[^ 	\]/q
468				s/^  //
469				/<file:\\(\[^>\]*\\)>/s//\\1/g
470				p
471			}
472			" Documentation/Configure.help]
473		set found [expr [string length "$message"] > 0]
474	}
475
476	frame $w.f1
477	pack $w.f1 -fill both -expand on
478
479	# Do the OK button
480	#
481	set oldFocus [focus]
482	frame $w.f2
483	button $w.f2.ok -text "OK" \
484		-width 10 -command "destroy $w; catch {focus $oldFocus}"
485	pack $w.f2.ok -side bottom -pady 6 -anchor n
486	pack $w.f2 -side bottom -padx 10 -anchor s
487
488	scrollbar $w.f1.vscroll -command "$w.f1.canvas yview"
489	pack $w.f1.vscroll -side right -fill y
490
491	canvas $w.f1.canvas -relief flat -borderwidth 0 \
492		-yscrollcommand "$w.f1.vscroll set"
493	frame $w.f1.f
494	pack $w.f1.canvas -side right -fill y -expand on
495
496	if { $found == 0 } then {
497		if { $filefound == 0 } then {
498		message $w.f1.f.m -width 750 -aspect 300 -relief flat -text \
499			"No help available - unable to open file Documentation/Configure.help.  This file should have come with your kernel."
500		} else {
501		message $w.f1.f.m -width 400 -aspect 300 -relief flat -text \
502			"No help available for $var"
503		}
504		label $w.f1.bm -bitmap error
505		wm title $w "RTFM"
506	} else {
507		text $w.f1.f.m -width 73 -relief flat -wrap word
508		$w.f1.f.m insert 0.0 $message
509		$w.f1.f.m conf -state disabled -height [$w.f1.f.m index end]
510
511		label $w.f1.bm -bitmap info
512		wm title $w "Configuration help"
513	}
514	pack $w.f1.f.m -side left
515	pack $w.f1.bm $w.f1.f -side left -padx 10
516
517	focus $w
518	set winx [expr [winfo x $parent]+20]
519	set winy [expr [winfo y $parent]+20]
520	wm geometry $w +$winx+$winy
521	set sizok [expr [winfo reqheight $w.f2.ok] + 12]
522	set maxy [expr [winfo screenheight .] * 3 / 4]
523	set canvtotal [winfo reqheight $w.f1.f.m]
524	if [expr $sizok + $canvtotal < $maxy] {
525		set sizy $canvtotal
526	} else {
527		set sizy [expr $maxy - $sizok]
528	}
529	$w.f1.canvas configure -height $sizy -width [winfo reqwidth $w.f1.f.m] \
530		-scrollregion "0 0 [winfo reqwidth $w.f1.f.m] \
531			[winfo reqheight $w.f1.f.m]"
532	$w.f1.canvas create window 0 0 -anchor nw -window $w.f1.f
533	update idletasks
534
535	set maxy [winfo screenheight .]
536	if [expr $sizok + $canvtotal < $maxy] {
537		set sizy [expr $sizok + $canvtotal]
538	} else {
539		set sizy $maxy
540	}
541	wm maxsize $w [winfo width $w] $sizy
542}
543
544bind all <Alt-s> { catch {exec cp -f .config .config.old}; \
545		writeconfig .config include/linux/autoconf.h; wrapup .wrap }
546
547proc wrapup {w }  {
548	catch {destroy $w}
549	toplevel $w -class Dialog
550
551	global CONFIG_MODVERSIONS; vfix CONFIG_MODVERSIONS
552	if { ([file exists .hdepend] != 1) || ($CONFIG_MODVERSIONS == 1) } then {
553	    message $w.m -width 400 -aspect 300 -relief raised -text \
554		"End of Linux kernel configuration.  Check the top-level Makefile for additional configuration.  Next, you must run 'make dep'."
555	} else {
556	    message $w.m -width 400 -aspect 300 -relief raised -text \
557		"End of Linux kernel configuration.  Check the top-level Makefile for additional configuration.  Next, you may 'make bzImage', 'make bzdisk', or 'make bzlilo.'"
558	}
559	label $w.bm -bitmap info
560	pack $w.bm $w.m -pady 10 -side top -padx 10
561	wm title $w "Kernel build instructions"
562
563	set oldFocus [focus]
564	frame $w.f
565	button $w.f.back -text "OK" \
566		-width 10 -command "exit"
567	pack $w.f.back -side bottom -pady 10 -anchor s
568	pack $w.f -pady 10 -side top -padx 10 -anchor s
569	focus $w
570        bind $w <Return> "exit"
571	global winx; global winy
572	set winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]
573	wm geometry $w +$winx+$winy
574
575}
576
577proc unregister_active {num} {
578	global active_menus
579	set index [lsearch -exact $active_menus $num]
580	if {$index != -1} then {set active_menus [lreplace $active_menus $index $index]}
581}
582
583proc update_active {} {
584	global active_menus total_menus
585	set max 0
586	if {[llength $active_menus] > 0} then {
587		set max [lindex $active_menus end]
588		update_define [toplevel_menu [lindex $active_menus 0]] $max 0
589	}
590	foreach i $active_menus {
591		if {[winfo exists .menu$i] == 0} then {
592			unregister_active $i
593		} else {
594			update_menu$i
595		}
596	}
597	update_define [expr $max + 1] $total_menus 1
598	update_mainmenu
599}
600
601proc configure_entry {w option items} {
602	foreach i $items {
603		$w.$i configure -state $option
604	}
605}
606
607proc validate_int {name val default} {
608	if {([exec echo $val | sed s/^-//g | tr -d \[:digit:\] ] != "")} then {
609		global $name; set $name $default
610	}
611}
612
613proc validate_hex {name val default} {
614	if {([exec echo $val | tr -d \[:xdigit:\] ] != "")} then {
615		global $name; set $name $default
616	}
617}
618
619proc update_define {first last allow_update} {
620	for {set i $first} {$i <= $last} {incr i} {
621		update_define_menu$i
622		if {$allow_update == 1} then update
623	}
624}
625
626#
627# Next set up the particulars for the top level menu, and define a few
628# buttons which we will stick down at the bottom.
629#
630
631frame .f0
632frame .f0.left
633frame .f0.middle
634frame .f0.right
635
636set active_menus [list]
637set processed_top_level 0
638