kbuild: simplify argument loop in scripts/config
[linux-2.6] / scripts / config
1 #!/bin/bash
2 # Manipulate options in a .config file from the command line
3
4 usage() {
5         cat >&2 <<EOL
6 Manipulate options in a .config file from the command line.
7 Usage:
8 config options command ...
9 commands:
10         --enable|-e option   Enable option
11         --disable|-d option  Disable option
12         --module|-m option      Turn option into a module
13         --state|-s option       Print state of option (n,y,m,undef)
14
15         --enable-after|-E beforeopt option
16                              Enable option directly after other option
17         --disable-after|-D beforeopt option
18                              Disable option directly after other option
19         --module-after|-M beforeopt option
20                              Turn option into module directly after other option
21
22         commands can be repeated multiple times
23
24 options:
25         --file .config file to change (default .config)
26
27 config doesn't check the validity of the .config file. This is done at next
28  make time.
29 EOL
30         exit 1
31 }
32
33 checkarg() {
34         ARG="$1"
35         if [ "$ARG" = "" ] ; then
36                 usage
37         fi
38         case "$ARG" in
39         CONFIG_*)
40                 ARG="${ARG/CONFIG_/}"
41                 ;;
42         esac
43         ARG="`echo $ARG | tr a-z A-Z`"
44 }
45
46 set_var() {
47         local name=$1 new=$2 before=$3
48
49         name_re="^($name=|# $name is not set)"
50         before_re="^($before=|# $before is not set)"
51         if test -n "$before" && grep -Eq "$before_re" "$FN"; then
52                 sed -ri "/$before_re/a $new" "$FN"
53         elif grep -Eq "$name_re" "$FN"; then
54                 sed -ri "s:$name_re.*:$new:" "$FN"
55         else
56                 echo "$new" >>"$FN"
57         fi
58 }
59
60 if [ "$1" = "--file" ]; then
61         FN="$2"
62         if [ "$FN" = "" ] ; then
63                 usage
64         fi
65         shift 2
66 else
67         FN=.config
68 fi
69
70 if [ "$1" = "" ] ; then
71         usage
72 fi
73
74 while [ "$1" != "" ] ; do
75         CMD="$1"
76         shift
77         case "$CMD" in
78         --refresh)
79                 ;;
80         --*-after)
81                 checkarg "$1"
82                 A=$ARG
83                 checkarg "$2"
84                 B=$ARG
85                 shift 2
86                 ;;
87         --*)
88                 checkarg "$1"
89                 shift
90                 ;;
91         esac
92         case "$CMD" in
93         --enable|-e)
94                 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
95                 ;;
96
97         --disable|-d)
98                 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
99                 ;;
100
101         --module|-m)
102                 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
103                 ;;
104
105         --state|-s)
106                 if grep -q "# CONFIG_$ARG is not set" $FN ; then
107                         echo n
108                 else
109                         V="$(grep "^CONFIG_$ARG=" $FN)"
110                         if [ $? != 0 ] ; then
111                                 echo undef
112                         else
113                                 V="${V/CONFIG_$ARG=/}"
114                                 V="${V/\"/}"
115                                 echo "$V"
116                         fi
117                 fi
118                 ;;
119
120         --enable-after|-E)
121                 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
122                 ;;
123
124         --disable-after|-D)
125                 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
126                 ;;
127
128         --module-after|-M)
129                 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
130                 ;;
131
132         # undocumented because it ignores --file (fixme)
133         --refresh)
134                 yes "" | make oldconfig
135                 ;;
136
137         *)
138                 usage
139                 ;;
140         esac
141 done
142