remove CVS
[aversive.git] / config / scripts / Menuconfig
1 #! /bin/sh
2 #
3 # This script is used to configure an Aversive project. It is mainly
4 # coming from the Linux Kernel Menuconfig script. Thanks to the authors.
5 #
6
7 #
8 # Change this to TRUE if you prefer all kernel options listed
9 # in a single menu rather than the standard menu hierarchy.
10 #
11 single_menu_mode=
12
13 #
14 # Make sure we're really running bash.
15 #
16 [ -z "$BASH" ] && { echo "Menuconfig requires bash" 1>&2; exit 1; }
17
18 #
19 # Cache function definitions, turn off posix compliance
20 #
21 set -h +o posix
22
23
24
25 # Given a configuration variable, set the global variable $x to its value,
26 # and the global variable $info to the string " (NEW)" if this is a new
27 # variable.
28 #
29 # This function looks for: (1) the current value, or (2) the default value
30 # from the arch-dependent defconfig file, or (3) a default passed by the caller.
31
32 function set_x_info () {
33     eval x=\$$1
34     if [ -z "$x" ]; then
35         eval `sed -n -e 's/# \(.*\) is not set.*/\1=n/' -e "/^$1=/p" arch/$ARCH/defconfig`
36         eval x=\${$1:-"$2"}
37         eval $1=$x
38         eval INFO_$1="' (NEW)'"
39     fi
40     eval info="\$INFO_$1"
41 }
42
43 #
44 # Load the functions used by the config.in files.
45 #
46 # I do this because these functions must be redefined depending
47 # on whether they are being called for interactive use or for
48 # saving a configuration to a file.
49 #
50 # Thank the heavens bash supports nesting function definitions.
51 #
52 load_functions () {
53
54 #
55 # Additional comments
56 #
57 function comment () {
58         comment_ctr=$[ comment_ctr + 1 ]
59         echo -ne "': $comment_ctr' '--- $1' " >>MCmenu
60 }
61
62 #
63 # Define a boolean to a specific value.
64 #
65 function define_bool () {
66         eval $1=$2
67 }
68
69 function define_hex () {
70         eval $1=$2
71 }
72
73 function define_int () {
74         eval $1=$2
75 }
76
77 function define_string () {
78         eval $1="$2"
79 }
80
81 #
82 # Create a boolean (Yes/No) function for our current menu
83 # which calls our local bool function.
84 #
85 function bool () {
86         set_x_info "$2" "n"
87
88         case $x in
89         y|m)    flag="*" ;;
90         n)      flag=" " ;;
91         esac
92
93         echo -ne "'$2' '[$flag] $1$info' " >>MCmenu
94
95         echo -e "function $2 () { l_bool '$2' \"\$1\" ;}\n" >>MCradiolists
96 }
97
98 #
99 #   Same as above, but now only Y and N are allowed as dependency
100 #   (i.e. third and next arguments).
101 #
102 function dep_bool () {
103         ques="$1"
104         var="$2"
105         dep=y
106         shift 2
107         while [ $# -gt 0 ]; do
108                 if [ "$1" = y ]; then
109                         shift
110                 else
111                         dep=n
112                         shift $#
113                 fi
114         done
115         if [ "$dep" = y ]; then
116             bool "$ques" "$var"
117         else 
118             define_bool "$var" n
119         fi
120 }
121
122 #
123 # Add a menu item which will call our local int function.
124
125 function int () {
126         set_x_info "$2" "$3"
127
128         echo -ne "'$2' '($x) $1$info' " >>MCmenu
129
130         echo -e "function $2 () { l_int '$1' '$2' '$3' '$x' ;}" >>MCradiolists
131 }
132
133 #
134 # Add a menu item which will call our local hex function.
135
136 function hex () {
137         set_x_info "$2" "$3"
138         x=${x##*[x,X]}
139
140         echo -ne "'$2' '($x) $1$info' " >>MCmenu
141
142         echo -e "function $2 () { l_hex '$1' '$2' '$3' '$x' ;}" >>MCradiolists
143 }
144
145 #
146 # Add a menu item which will call our local string function.
147
148 function string () {
149         set_x_info "$2" "$3"
150
151         echo -ne "'$2' '     $1: \"$x\"$info' " >>MCmenu
152
153         echo -e "function $2 () { l_string '$1' '$2' '$3' '$x' ;}" >>MCradiolists
154 }
155
156 #
157 # Add a menu item which will call our local One-of-Many choice list.
158 #
159 function choice () {
160         #
161         # Need to remember params cause they're gonna get reset.
162         #
163         title=$1
164         choices=$2
165         default=$3
166         current=
167
168         #
169         # Find out if one of the choices is already set.
170         # If it's not then make it the default.
171         #
172         set -- $choices
173         firstchoice=$2
174
175         while [ -n "$2" ]
176         do
177                 if eval [ "_\$$2" = "_y" ]
178                 then
179                         current=$1
180                         break
181                 fi
182                 shift ; shift
183         done
184
185         : ${current:=$default}
186
187         echo -ne "'$firstchoice' '($current) $title' " >>MCmenu
188
189         echo -e "
190         function $firstchoice () \
191                 { l_choice '$title' \"$choices\" $current ;}" >>MCradiolists
192 }
193
194 } # END load_functions()
195
196
197
198 #################################################################
199
200 #
201 # Extract available help for an option from Configure.help
202 # and send it to standard output.
203 #
204 # Most of this function was borrowed from the original kernel
205 # Configure script.
206 #
207 function extract_help () {
208   if [ -f ${HELP_FILE} ]
209   then
210      #first escape regexp special characters in the argument:
211      var=$(echo "$1"|sed 's/[][\/.^$*]/\\&/g')
212      #now pick out the right help text:
213      text=$(sed -n "/^$var[     ]*\$/,\${
214                         /^$var[         ]*\$/c\\
215 ${var}:\\
216
217                         /^#/b
218                         /^[^    ]/q
219                         s/^  //
220                         p
221                     }" ${HELP_FILE})
222
223      if [ -z "$text" ]
224      then
225           echo "There is no help available for this Aversive option."
226           return 1
227      else
228           echo "$text"
229      fi
230   else
231          echo "There is no help available for this Aversive option."
232          return 1
233   fi
234 }
235
236 #
237 # Activate a help dialog.
238 #
239 function help () {
240         if extract_help $1 >help.out
241         then
242                 $DIALOG --backtitle "$backtitle" --title "$2"\
243                         --textbox help.out $ROWS $COLS
244         else
245                 $DIALOG --backtitle "$backtitle" \
246                         --textbox help.out $ROWS $COLS
247         fi
248         rm -f help.out
249 }
250
251 #
252 # Show the README file.
253 #
254 function show_readme () {
255         $DIALOG --backtitle "$backtitle" \
256                 --textbox scripts/README.Menuconfig $ROWS $COLS
257 }
258
259 #
260 # Begin building the dialog menu command and Initialize the 
261 # Radiolist function file.
262 #
263 function menu_name () {
264         echo -ne "$DIALOG --title '$1'\
265                         --backtitle '$backtitle' \
266                         --menu '$menu_instructions' \
267                         $ROWS $COLS $((ROWS-10)) \
268                         '$default' " >MCmenu
269         >MCradiolists
270 }
271
272 #
273 # Add a submenu option to the menu currently under construction.
274 #
275 function submenu () {
276         echo -ne "'activate_menu $2' '$1  --->' " >>MCmenu
277 }
278
279 #
280 # Handle a boolean (Yes/No) option.
281 #
282 function l_bool () {
283         if [ -n "$2" ]
284         then
285                 case "$2" in
286                 y|m)    eval $1=y ;;
287                 c)      eval x=\$$1
288                         case $x in
289                         y) eval $1=n ;;
290                         n) eval $1=y ;;
291                         *) eval $1=y ;;
292                         esac ;;
293                 *)      eval $1=n ;;
294                 esac
295         else
296                 echo -ne "\007"
297         fi
298 }
299
300 #
301 # Create a dialog for entering an integer into a kernel option.
302 #
303 function l_int () {
304         while true
305         do
306                 if $DIALOG --title "$1" \
307                         --backtitle "$backtitle" \
308                         --inputbox "$inputbox_instructions_int" \
309                         10 75 "$4" 2>MCdialog.out
310                 then
311                         answer="`cat MCdialog.out`"
312                         answer="${answer:-$3}"
313
314                         # Semantics of + and ? in GNU expr changed, so
315                         # we avoid them:
316                         if expr "$answer" : '0$' '|' "$answer" : '[1-9][0-9]*$' '|' "$answer" : '-[1-9][0-9]*$' >/dev/null
317                         then
318                                 eval $2="$answer"
319                         else
320                                 eval $2="$3"
321                                 echo -en "\007"
322                                 ${DIALOG} --backtitle "$backtitle" \
323                                         --infobox "You have made an invalid entry." 3 43
324                                 sleep 2
325                         fi
326
327                         break
328                 fi
329
330                 help "$2" "$1"
331         done
332 }
333
334 #
335 # Create a dialog for entering a hexadecimal into a kernel option.
336 #
337 function l_hex () {
338         while true
339         do
340                 if $DIALOG --title "$1" \
341                         --backtitle "$backtitle" \
342                         --inputbox "$inputbox_instructions_hex" \
343                         10 75 "$4" 2>MCdialog.out
344                 then
345                         answer="`cat MCdialog.out`"
346                         answer="${answer:-$3}"
347                         answer="${answer##*[x,X]}"
348
349                         if expr "$answer" : '[0-9a-fA-F][0-9a-fA-F]*$' >/dev/null
350                         then
351                                 eval $2="$answer"
352                         else
353                                 eval $2="$3"
354                                 echo -en "\007"
355                                 ${DIALOG} --backtitle "$backtitle" \
356                                         --infobox "You have made an invalid entry." 3 43
357                                 sleep 2
358                         fi
359
360                         break
361                 fi
362
363                 help "$2" "$1"
364         done
365 }
366
367 #
368 # Create a dialog for entering a string into a kernel option.
369 #
370 function l_string () {
371         while true
372         do
373                 if $DIALOG --title "$1" \
374                         --backtitle "$backtitle" \
375                         --inputbox "$inputbox_instructions_string" \
376                         10 75 "$4" 2>MCdialog.out
377                 then
378                         answer="`cat MCdialog.out`"
379                         answer="${answer:-$3}"
380
381                         #
382                         # Someone may add a nice check for the entered
383                         # string here...
384                         #
385                         eval $2=\"$answer\"
386
387                         break
388                 fi
389
390                 help "$2" "$1"
391         done
392 }
393
394
395 #
396 # Handle a one-of-many choice list.
397 #
398 function l_choice () {
399         #
400         # Need to remember params cause they're gonna get reset.
401         #
402         title="$1"
403         choices="$2"
404         current="$3"
405         chosen=
406
407         #
408         # Scan current value of choices and set radiolist switches.
409         #
410         list=
411         set -- $choices
412         firstchoice=$2
413         while [ -n "$2" ]
414         do
415                 case "$1" in
416                 "$current"*)    if [ -z "$chosen" ]; then
417                                         list="$list $2 $1 ON "
418                                         chosen=1
419                                 else
420                                         list="$list $2 $1 OFF "
421                                 fi  ;;
422                 *)              list="$list $2 $1 OFF " ;;
423                 esac
424                         
425                 shift ; shift
426         done
427
428         while true
429         do
430                 if $DIALOG --title "$title" \
431                         --backtitle "$backtitle" \
432                         --radiolist "$radiolist_instructions" \
433                         15 70 6 $list 2>MCdialog.out
434                 then
435                         choice=`cat MCdialog.out`
436                         break
437                 fi
438
439                 help "$firstchoice" "$title"
440         done
441
442         #
443         # Now set the boolean value of each option based on
444         # the selection made from the radiolist.
445         #
446         set -- $choices
447         while [ -n "$2" ]
448         do
449                 if [ "$2" = "$choice" ]
450                 then
451                         eval $2="y"
452                 else
453                         eval $2="n"
454                 fi
455                 
456                 shift ; shift
457         done
458 }
459
460 #
461 # Call awk, and watch for error codes, etc.
462 #
463 function callawk () {
464 awk "$1" || echo "Awk died with error code $?. Giving up." || exit 1
465 }
466
467 #
468 # A faster awk based recursive parser. (I hope)
469 #
470 function parser1 () {
471 callawk '
472 BEGIN {
473         menu_no = 0
474         comment_is_option = 0
475         parser("'$CONFIG_IN'","MCmenu0")
476 }
477
478 function parser(ifile,menu) {
479
480         while (getline <ifile) {
481                 if ($1 == "mainmenu_option") {
482                         comment_is_option = "1"
483                 }
484                 else if ($1 == "comment" && comment_is_option == "1") {
485                         comment_is_option= "0"
486                         sub($1,"",$0)
487                         ++menu_no
488
489                         printf("submenu %s MCmenu%s\n", $0, menu_no) >>menu
490
491                         newmenu = sprintf("MCmenu%d", menu_no);
492                         printf( "function MCmenu%s () {\n"\
493                                 "default=$1\n"\
494                                 "menu_name %s\n",\
495                                  menu_no, $0) >newmenu
496
497                         parser(ifile, newmenu)
498                 }
499                 else if ($0 ~ /^#|\$MAKE|mainmenu_name/) {
500                         printf("") >>menu
501                 }
502                 else if ($1 ~ "endmenu") {
503                         printf("}\n") >>menu
504                         return
505                 } 
506                 else if ($1 == "source") {
507                         parser($2,menu)
508                 }
509                 else {
510                         print >>menu
511                 }
512         }
513 }'
514 }
515
516 #
517 # Secondary parser for single menu mode.
518 #
519 function parser2 () {
520 callawk '
521 BEGIN {
522         parser("'$CONFIG_IN'","MCmenu0")
523 }
524
525 function parser(ifile,menu) {
526
527         while (getline <ifile) {
528                 if ($0 ~ /^#|$MAKE|mainmenu_name/) {
529                         printf("") >>menu
530                 }
531                 else if ($1 ~ /mainmenu_option|endmenu/) {
532                         printf("") >>menu
533                 } 
534                 else if ($1 == "source") {
535                         parser($2,menu)
536                 }
537                 else {
538                         print >>menu
539                 }
540         }
541 }'
542 }
543
544 #
545 # Parse all the config.in files into mini scripts.
546 #
547 function parse_config_files () {
548         rm -f MCmenu*
549
550         echo "function MCmenu0 () {" >MCmenu0
551         echo 'default=$1' >>MCmenu0
552         echo "menu_name 'Main Menu'" >>MCmenu0
553
554         if [ "_$single_menu_mode" = "_TRUE" ]
555         then
556                 parser2
557         else
558                 parser1
559         fi
560
561         echo "comment ''"       >>MCmenu0
562         echo "g_alt_config"     >>MCmenu0
563         echo "s_alt_config"     >>MCmenu0
564
565         echo "}" >>MCmenu0
566
567         #
568         # These mini scripts must be sourced into the current
569         # environment in order for all of this to work.  Leaving
570         # them on the disk as executables screws up the recursion
571         # in activate_menu(), among other things.  Once they are
572         # sourced we can discard them.
573         #
574         for i in MCmenu*
575         do
576                 echo -n "."
577                 source ./$i
578         done
579         rm -f MCmenu*
580 }
581
582 #
583 # This is the menu tree's bootstrap.
584 #
585 # Executes the parsed menus on demand and creates a set of functions,
586 # one per configuration option.  These functions will in turn execute
587 # dialog commands or recursively call other menus.
588 #
589 function activate_menu () {
590         rm -f lxdialog.scrltmp
591         while true
592         do
593                 comment_ctr=0           #So comment lines get unique tags
594
595                 $1 "$default" 2> MCerror #Create the lxdialog menu & functions
596
597                 if [ "$?" != "0" ]
598                 then
599                         clear
600                         cat <<EOM
601
602 Menuconfig has encountered a possible error in one of the 
603 configuration files and is unable to continue.  Here is the error
604 report:
605
606 EOM
607                         sed 's/^/ Q> /' MCerror
608                         cat <<EOM
609
610 Please report this to <zer0@droids-corp.org>. 
611
612 EOM
613                         cleanup
614                         exit 1
615                 fi
616                 rm -f MCerror
617
618                 . ./MCradiolists                #Source the menu's functions
619
620                 . ./MCmenu 2>MCdialog.out       #Activate the lxdialog menu
621                 ret=$?
622
623                 read selection <MCdialog.out
624
625                 case "$ret" in
626                 0|3|4|5|6)
627                         defaults="$selection\12$defaults"  #pseudo stack
628                         case "$ret" in
629                         0) eval $selection   ;;
630                         3) eval $selection y ;;
631                         4) eval $selection n ;;
632                         5) eval $selection m ;;
633                         6) eval $selection c ;;
634                         esac
635                         default="${defaults%%\12*}" defaults="${defaults#*\12}"
636                         ;;
637                 2)      
638                         default="${selection%%\ *}"
639
640                         case "$selection" in
641                         *"-->"*|*"alt_config"*)
642                                 show_readme ;;
643                         *)
644                                 eval help $selection ;;
645                         esac
646                         ;;
647                 255|1)
648                         break
649                         ;;
650                 139)
651                         stty sane
652                         clear
653                         cat <<EOM
654
655 There seems to be a problem with the lxdialog companion utility which is
656 built prior to running Menuconfig.  Usually this is an indicator that you
657 have upgraded/downgraded your ncurses libraries and did not remove the 
658 old ncurses header file(s) in /usr/include or /usr/include/ncurses.
659
660 It is VERY important that you have only one set of ncurses header files
661 and that those files are properly version matched to the ncurses libraries 
662 installed on your machine.
663
664 You may also need to rebuild lxdialog.  This can be done by moving to
665 the /usr/src/linux/scripts/lxdialog directory and issuing the 
666 "make clean all" command.
667
668 If you have verified that your ncurses install is correct, you may email
669 <zer0@droids-corp.org>
670
671 EOM
672                         cleanup
673                         exit 139
674                         ;;
675                 esac
676         done
677 }
678
679 #
680 # Create a menu item to load an alternate configuration file.
681 #
682 g_alt_config () {
683         echo -n "get_alt_config 'Load an Alternate Configuration File' "\
684                 >>MCmenu
685 }
686
687 #
688 # Get alternate config file name and load the 
689 # configuration from it.
690 #
691 get_alt_config () {
692         set -f ## Switch file expansion OFF
693
694         while true
695         do
696                 ALT_CONFIG="${ALT_CONFIG:-$DEFAULTS}"
697
698                 $DIALOG --backtitle "$backtitle" \
699                         --inputbox "\
700 Enter the name of the configuration file you wish to load.  \
701 Accept the name shown to restore the configuration you \
702 last retrieved.  Leave blank to abort."\
703                         11 55 "$ALT_CONFIG" 2>MCdialog.out
704
705                 if [ "$?" = "0" ]
706                 then
707                         ALT_CONFIG=`cat MCdialog.out`
708
709                         [ "_" = "_$ALT_CONFIG" ] && break
710
711                         if eval [ -r "$ALT_CONFIG" ]
712                         then
713                                 eval load_config_file "$ALT_CONFIG"
714                                 break
715                         else
716                                 echo -ne "\007"
717                                 $DIALOG --backtitle "$backtitle" \
718                                         --infobox "File does not exist!"  3 38
719                                 sleep 2
720                         fi
721                 else
722                         cat <<EOM >help.out
723
724 For various reasons, one may wish to keep several different Aversive
725 configurations available on a single machine.  
726
727 If you have saved a previous configuration in a file other than the
728 Aversive's default, entering the name of the file here will allow you
729 to modify that configuration.
730
731 If you are uncertain, then you have probably never used alternate 
732 configuration files.  You should therefor leave this blank to abort.
733
734 EOM
735                         $DIALOG --backtitle "$backtitle"\
736                                 --title "Load Alternate Configuration"\
737                                 --textbox help.out $ROWS $COLS
738                 fi
739         done
740
741         set +f ## Switch file expansion ON
742         rm -f help.out MCdialog.out
743 }
744
745 #
746 # Create a menu item to store an alternate config file.
747 #
748 s_alt_config () {
749         echo -n "save_alt_config 'Save Configuration to an Alternate File' "\
750                  >>MCmenu
751 }
752
753 #
754 # Get an alternate config file name and save the current
755 # configuration to it.
756 #
757 save_alt_config () {
758         set -f  ## Switch file expansion OFF
759                         
760         while true
761         do
762                 $DIALOG --backtitle "$backtitle" \
763                         --inputbox "\
764 Enter a filename to which this configuration should be saved \
765 as an alternate.  Leave blank to abort."\
766                         10 55 "$ALT_CONFIG" 2>MCdialog.out
767
768                 if [ "$?" = "0" ]
769                 then
770                         ALT_CONFIG=`cat MCdialog.out`
771
772                         [ "_" = "_$ALT_CONFIG" ] && break
773
774                         if eval touch $ALT_CONFIG 2>/dev/null
775                         then
776                                 eval save_configuration $ALT_CONFIG
777                                 load_functions  ## RELOAD
778                                 break
779                         else
780                                 echo -ne "\007"
781                                 $DIALOG --backtitle "$backtitle" \
782                                         --infobox "Can't create file!  Probably a nonexistent directory." 3 60
783                                 sleep 2
784                         fi
785                 else
786                         cat <<EOM >help.out
787
788 For various reasons, one may wish to keep different kernel
789 configurations available on a single machine.  
790
791 Entering a file name here will allow you to later retrieve, modify
792 and use the current configuration as an alternate to whatever 
793 configuration options you have selected at that time.
794
795 If you are uncertain what all this means then you should probably
796 leave this blank.
797 EOM
798                         $DIALOG --backtitle "$backtitle"\
799                                 --title "Save Alternate Configuration"\
800                                 --textbox help.out $ROWS $COLS
801                 fi
802         done
803
804         set +f  ## Switch file expansion ON
805         rm -f help.out MCdialog.out
806 }
807
808 #
809 # Load config options from a file.
810 # Converts all "# OPTION is not set" lines to "OPTION=n" lines
811 #
812 function load_config_file () {
813         awk '
814           /# .* is not set.*/ { printf("%s=n\n", $2) }
815         ! /# .* is not set.*/ { print }
816         ' $1 >.tmpconfig
817
818         source ./.tmpconfig
819         rm -f .tmpconfig
820 }
821
822 #
823 # Just what it says.
824 #
825 save_configuration () {
826         echo
827         echo -n "Saving your Aversive configuration."
828
829         #
830         # Now, let's redefine the configuration functions for final
831         # output to the config files.
832         #
833         # Nested function definitions, YIPEE!
834         #
835         function bool () {
836                 set_x_info "$2" "n"
837                 eval define_bool "$2" "$x"
838         }
839
840         function dep_bool () {
841                 set_x_info "$2" "n"
842                 var="$2"
843                 shift 2
844                 while [ $# -gt 0 ]; do
845                         if   [ "$1" = y ]; then
846                                 shift
847                         else 
848                                 x=n; shift $#
849                         fi
850                 done
851                 define_bool "$var" "$x"
852         }
853
854         function dep_mbool () {
855                 set_x_info "$2" "n"
856                 var="$2"
857                 shift 2
858                 while [ $# -gt 0 ]; do
859                         if   [ "$1" = y -o "$1" = m ]; then
860                                 shift
861                         else 
862                                 x=n; shift $#
863                         fi
864                 done
865                 define_bool "$var" "$x"
866         }
867
868         function int () {
869                 set_x_info "$2" "$3"
870                 echo "$2=$x"            >>$CONFIG
871                 echo "#define $2 ($x)"  >>$CONFIG_H
872         }
873
874         function hex () {
875                 set_x_info "$2" "$3"
876                 echo "$2=$x"                     >>$CONFIG
877                 echo "#define $2 0x${x##*[x,X]}" >>$CONFIG_H
878         }
879
880         function string () {
881                 set_x_info "$2" "$3"
882                 echo "$2=\"$x\""                         >>$CONFIG
883                 echo "#define $2 \"$x\""        >>$CONFIG_H
884         }
885
886         function define_hex () {
887                 eval $1="$2"
888                 echo "$1=$2"                    >>$CONFIG
889                 echo "#define $1 0x${2##*[x,X]}"        >>$CONFIG_H
890         }
891
892         function define_int () {
893                 eval $1="$2"
894                 echo "$1=$2"                    >>$CONFIG
895                 echo "#define $1 ($2)"          >>$CONFIG_H
896         }
897
898         function define_string () {
899                 eval $1="$2"
900                 echo "$1=\"$2\""                >>$CONFIG
901                 echo "#define $1 \"$2\""        >>$CONFIG_H
902         }
903
904         function define_bool () {
905                 eval $1="$2"
906
907                 case "$2" in
908                 y)
909                         echo "$1=y"             >>$CONFIG
910                         echo "#define $1 1"     >>$CONFIG_H
911                         ;;
912
913                 n)
914                         echo "# $1 is not set"  >>$CONFIG
915                         echo "#undef  $1"       >>$CONFIG_H
916                         ;;
917                 esac
918         }
919
920         function choice () {
921                 #
922                 # Find the first choice that's already set to 'y'
923                 #
924                 choices="$2"
925                 default="$3"
926                 current=
927                 chosen=
928
929                 set -- $choices
930                 while [ -n "$2" ]
931                 do
932                         if eval [ "_\$$2" = "_y" ]
933                         then
934                                 current=$1
935                                 break
936                         fi
937                         shift ; shift
938                 done
939
940                 #
941                 # Use the default if none were set.  
942                 #
943                 : ${current:=$default}
944
945                 #
946                 # Output all choices (to be compatible with other configs).
947                 #
948                 set -- $choices
949                 while [ -n "$2" ]
950                 do
951                         case "$1" in
952                         "$current"*)    if [ -z "$chosen" ]; then
953                                                 define_bool "$2" "y"
954                                                 chosen=1
955                                         else
956                                                 define_bool "$2" "n"
957                                         fi ;;
958                         *)              define_bool "$2" "n" ;;
959                         esac
960                         shift ; shift
961                 done
962         }
963
964         function mainmenu_name () {
965                 :
966         }
967
968         function mainmenu_option () {
969                 comment_is_option=TRUE
970         }
971
972         function endmenu () {
973                 :
974         }
975
976         function comment () {
977                 if [ "$comment_is_option" ]
978                 then
979                         comment_is_option=
980                         echo        >>$CONFIG
981                         echo "#"    >>$CONFIG
982                         echo "# $1" >>$CONFIG
983                         echo "#"    >>$CONFIG
984
985                         echo         >>$CONFIG_H
986                         echo "/*"    >>$CONFIG_H
987                         echo " * $1" >>$CONFIG_H
988                         echo " */"   >>$CONFIG_H
989                 fi
990         }
991
992         echo -n "."
993
994         DEF_CONFIG="${1:-.config}"
995         #DEF_CONFIG_H="include/linux/autoconf.h"
996         DEF_CONFIG_H="autoconf.h"
997
998         CONFIG=.tmpconfig
999         CONFIG_H=.tmpconfig.h
1000
1001         echo "#" >$CONFIG
1002         echo "# Automatically generated by make menuconfig: don't edit" >>$CONFIG
1003         echo "#" >>$CONFIG
1004
1005         echo "/*" >$CONFIG_H
1006         echo " * Automatically generated by make menuconfig: don't edit" >>$CONFIG_H
1007         echo " */" >>$CONFIG_H
1008         echo "#define AUTOCONF_INCLUDED" >> $CONFIG_H
1009
1010         echo -n "."
1011         if . $CONFIG_IN >>.menuconfig.log 2>&1
1012         then
1013                 if [ "$DEF_CONFIG" = ".config" -a -d `dirname $DEF_CONFIG_H` ]
1014                 then
1015                         mv $CONFIG_H $DEF_CONFIG_H
1016                 fi
1017
1018                 if [ -f "$DEF_CONFIG" ]
1019                 then
1020                         rm -f ${DEF_CONFIG}.old
1021                         mv $DEF_CONFIG ${DEF_CONFIG}.old
1022                 fi
1023
1024                 mv $CONFIG $DEF_CONFIG
1025                         
1026                 return 0
1027         else
1028                 return 1
1029         fi
1030 }
1031
1032 #
1033 # Remove temporary files
1034 #
1035 cleanup () {
1036         cleanup1
1037         cleanup2
1038 }
1039
1040 cleanup1 () {
1041         rm -f MCmenu* MCradiolists MCdialog.out help.out
1042 }
1043
1044 cleanup2 () {
1045         rm -f .tmpconfig .tmpconfig.h
1046 }
1047
1048 set_geometry () {
1049         # Some distributions export these with incorrect values
1050         # which can really screw up some ncurses programs.
1051         LINES=  COLUMNS=
1052
1053         ROWS=${1:-24}  COLS=${2:-80} 
1054
1055         # Just in case the nasty rlogin bug returns.
1056         #
1057         [ $ROWS = 0 ] && ROWS=24
1058         [ $COLS = 0 ] && COLS=80
1059
1060         if [ $ROWS -lt 19 -o $COLS -lt 80 ]
1061         then
1062                 echo -e "\n\007Your display is too small to run Menuconfig!"
1063                 echo "It must be at least 19 lines by 80 columns."
1064                 exit 0
1065         fi 
1066
1067         ROWS=$((ROWS-4))  COLS=$((COLS-5))
1068 }
1069
1070
1071 set_geometry `stty size 2>/dev/null`
1072
1073 menu_instructions="\
1074 Arrow keys navigate the menu.  \
1075 <Enter> selects submenus --->.  \
1076 Highlighted letters are hotkeys.  \
1077 Pressing <Y> includes, <N> excludes.  \
1078 Press <Esc><Esc> to exit, <?> for Help.  \
1079 Legend: [*] built-in  [ ] excluded "
1080
1081 radiolist_instructions="\
1082 Use the arrow keys to navigate this window or \
1083 press the hotkey of the item you wish to select \
1084 followed by the <SPACE BAR>.
1085 Press <?> for additional information about this option."
1086
1087 inputbox_instructions_int="\
1088 Please enter a decimal value. \
1089 Fractions will not be accepted.  \
1090 Use the <TAB> key to move from the input field to the buttons below it."
1091
1092 inputbox_instructions_hex="\
1093 Please enter a hexadecimal value. \
1094 Use the <TAB> key to move from the input field to the buttons below it."
1095
1096 inputbox_instructions_string="\
1097 Please enter a string value. \
1098 Use the <TAB> key to move from the input field to the buttons below it."
1099
1100 DIALOG="$AVERSIVE_DIR/config/scripts/lxdialog/lxdialog"
1101
1102 kernel_version="${VERSIONPKG}"
1103
1104 backtitle="Aversive Configuration"
1105
1106 trap "cleanup ; exit 1" 1 2 15
1107
1108
1109 #
1110 # Locate default files.
1111 #
1112 CONFIG_IN=./config.in
1113 if [ "$1" != "" ] ; then
1114         CONFIG_IN=$1
1115 fi
1116
1117 DEFAULTS=$AVERSIVE_DIR/config/.config
1118 if [ -f .config ]; then
1119   DEFAULTS=.config
1120 fi
1121
1122 if [ -f $DEFAULTS ]
1123 then
1124   echo "Using defaults found in" $DEFAULTS
1125   load_config_file $DEFAULTS
1126 else
1127   echo "No defaults found"
1128 fi
1129
1130 if [ -z "$HELP_FILE" ]
1131 then
1132         HELP_FILE="Documentation/Configure.help"
1133 fi
1134
1135 # Fresh new log.
1136 >.menuconfig.log
1137
1138 # Load the functions used by the config.in files.
1139 echo -n "Preparing scripts: functions" 
1140 load_functions
1141
1142 if [ ! -e $CONFIG_IN ]
1143 then
1144         echo "Your main config.in file ($CONFIG_IN) does not exist"
1145         exit 1
1146 fi
1147
1148 if [ ! -x $DIALOG ]
1149 then
1150         echo "Your lxdialog utility does not exist"
1151         exit 1
1152 fi
1153
1154 #
1155 # Read config.in files and parse them into one shell function per menu.
1156 #
1157 echo -n ", parsing"
1158 parse_config_files $CONFIG_IN
1159
1160 echo "done."
1161 #
1162 # Start the ball rolling from the top.
1163 #
1164 activate_menu MCmenu0
1165
1166 #
1167 # All done!
1168 #
1169 cleanup1
1170
1171 #
1172 # Confirm and Save
1173 #
1174 if $DIALOG --backtitle "$backtitle" \
1175            --yesno "Do you wish to save your new configuration?" 5 60
1176 then
1177         save_configuration
1178         $AVERSIVE_DIR/config/generate_aversive_config .config .aversive_conf
1179         echo
1180         echo
1181         echo "*** End of Aversive configuration."
1182         echo "*** Next, you should run 'make clean', then 'make'"
1183         echo
1184 else
1185         echo
1186         echo 
1187         echo Your configuration changes were NOT saved.
1188         echo
1189 fi
1190
1191
1192 # Remove log if empty.
1193 if [ ! -s .menuconfig.log ] ; then
1194         rm -f .menuconfig.log
1195 fi
1196
1197 exit 0