Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upstream-linus
[linux-2.6-block.git] / scripts / config
CommitLineData
8e54701e
AK
1#!/bin/bash
2# Manipulate options in a .config file from the command line
3
73877785
CC
4myname=${0##*/}
5
f5ef2f7b
YM
6# If no prefix forced, use the default CONFIG_
7CONFIG_="${CONFIG_-CONFIG_}"
8
8e54701e
AK
9usage() {
10 cat >&2 <<EOL
11Manipulate options in a .config file from the command line.
12Usage:
73877785 13$myname options command ...
8e54701e
AK
14commands:
15 --enable|-e option Enable option
16 --disable|-d option Disable option
1f990cf9 17 --module|-m option Turn option into a module
f0a6332c
JA
18 --set-str option string
19 Set option to "string"
20 --set-val option value
21 Set option to value
d5bfb6b3 22 --undefine|-u option Undefine option
1f990cf9 23 --state|-s option Print state of option (n,y,m,undef)
8e54701e
AK
24
25 --enable-after|-E beforeopt option
26 Enable option directly after other option
27 --disable-after|-D beforeopt option
28 Disable option directly after other option
29 --module-after|-M beforeopt option
30 Turn option into module directly after other option
31
32 commands can be repeated multiple times
33
34options:
4edc7e32
YM
35 --file config-file .config file to change (default .config)
36 --keep-case|-k Keep next symbols' case (dont' upper-case it)
8e54701e 37
73877785 38$myname doesn't check the validity of the .config file. This is done at next
4edc7e32
YM
39make time.
40
73877785 41By default, $myname will upper-case the given symbol. Use --keep-case to keep
4edc7e32 42the case of all following symbols unchanged.
f5ef2f7b 43
73877785
CC
44$myname uses 'CONFIG_' as the default symbol prefix. Set the environment
45variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
8e54701e
AK
46EOL
47 exit 1
48}
49
50checkarg() {
51 ARG="$1"
52 if [ "$ARG" = "" ] ; then
53 usage
54 fi
55 case "$ARG" in
f5ef2f7b
YM
56 ${CONFIG_}*)
57 ARG="${ARG/${CONFIG_}/}"
8e54701e
AK
58 ;;
59 esac
4edc7e32
YM
60 if [ "$MUNGE_CASE" = "yes" ] ; then
61 ARG="`echo $ARG | tr a-z A-Z`"
62 fi
8e54701e
AK
63}
64
83e8b90e
CC
65txt_append() {
66 local anchor="$1"
67 local insert="$2"
68 local infile="$3"
69 local tmpfile="$infile.swp"
70
71 # sed append cmd: 'a\' + newline + text + newline
72 cmd="$(printf "a\\%b$insert" "\n")"
73
74 sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
75 # replace original file with the edited one
76 mv "$tmpfile" "$infile"
77}
78
79txt_subst() {
80 local before="$1"
81 local after="$2"
82 local infile="$3"
83 local tmpfile="$infile.swp"
84
86eb7818 85 sed -e "s:$before:$after:" "$infile" >"$tmpfile"
83e8b90e
CC
86 # replace original file with the edited one
87 mv "$tmpfile" "$infile"
88}
89
90txt_delete() {
91 local text="$1"
92 local infile="$2"
93 local tmpfile="$infile.swp"
94
95 sed -e "/$text/d" "$infile" >"$tmpfile"
96 # replace original file with the edited one
97 mv "$tmpfile" "$infile"
98}
99
56643222
MM
100set_var() {
101 local name=$1 new=$2 before=$3
102
103 name_re="^($name=|# $name is not set)"
104 before_re="^($before=|# $before is not set)"
105 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
83e8b90e
CC
106 txt_append "^$before=" "$new" "$FN"
107 txt_append "^# $before is not set" "$new" "$FN"
56643222 108 elif grep -Eq "$name_re" "$FN"; then
83e8b90e
CC
109 txt_subst "^$name=.*" "$new" "$FN"
110 txt_subst "^# $name is not set" "$new" "$FN"
56643222
MM
111 else
112 echo "$new" >>"$FN"
113 fi
8e54701e
AK
114}
115
d5bfb6b3
YM
116undef_var() {
117 local name=$1
118
83e8b90e
CC
119 txt_delete "^$name=" "$FN"
120 txt_delete "^# $name is not set" "$FN"
d5bfb6b3
YM
121}
122
8e54701e
AK
123if [ "$1" = "--file" ]; then
124 FN="$2"
125 if [ "$FN" = "" ] ; then
126 usage
127 fi
47312d2c 128 shift 2
8e54701e
AK
129else
130 FN=.config
131fi
132
2302e873
AK
133if [ "$1" = "" ] ; then
134 usage
135fi
136
4edc7e32 137MUNGE_CASE=yes
8e54701e
AK
138while [ "$1" != "" ] ; do
139 CMD="$1"
140 shift
141 case "$CMD" in
4edc7e32
YM
142 --keep-case|-k)
143 MUNGE_CASE=no
4edc7e32
YM
144 continue
145 ;;
47312d2c
MM
146 --refresh)
147 ;;
57a9c760 148 --*-after|-E|-D|-M)
47312d2c
MM
149 checkarg "$1"
150 A=$ARG
151 checkarg "$2"
152 B=$ARG
153 shift 2
154 ;;
45f53cc9 155 -*)
8e54701e 156 checkarg "$1"
8e54701e
AK
157 shift
158 ;;
47312d2c
MM
159 esac
160 case "$CMD" in
161 --enable|-e)
f5ef2f7b 162 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
47312d2c 163 ;;
8e54701e
AK
164
165 --disable|-d)
f5ef2f7b 166 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
8e54701e
AK
167 ;;
168
169 --module|-m)
f5ef2f7b 170 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
8e54701e
AK
171 ;;
172
1f990cf9 173 --set-str)
d6686da8 174 # sed swallows one level of escaping, so we need double-escaping
f5ef2f7b 175 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
1f990cf9
MM
176 shift
177 ;;
178
f0a6332c 179 --set-val)
f5ef2f7b 180 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
f0a6332c
JA
181 shift
182 ;;
d5bfb6b3
YM
183 --undefine|-u)
184 undef_var "${CONFIG_}$ARG"
185 ;;
f0a6332c 186
8e54701e 187 --state|-s)
f5ef2f7b 188 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
8e54701e
AK
189 echo n
190 else
f5ef2f7b 191 V="$(grep "^${CONFIG_}$ARG=" $FN)"
8e54701e
AK
192 if [ $? != 0 ] ; then
193 echo undef
194 else
f5ef2f7b 195 V="${V/#${CONFIG_}$ARG=/}"
d6686da8
YM
196 V="${V/#\"/}"
197 V="${V/%\"/}"
1925a276 198 V="${V//\\\"/\"}"
d6686da8 199 echo "${V}"
8e54701e
AK
200 fi
201 fi
8e54701e
AK
202 ;;
203
204 --enable-after|-E)
f5ef2f7b 205 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
8e54701e
AK
206 ;;
207
208 --disable-after|-D)
f5ef2f7b 209 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
8e54701e
AK
210 ;;
211
212 --module-after|-M)
f5ef2f7b 213 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
8e54701e
AK
214 ;;
215
216 # undocumented because it ignores --file (fixme)
217 --refresh)
218 yes "" | make oldconfig
219 ;;
220
221 *)
222 usage
223 ;;
224 esac
225done