scripts/config: allow alternate prefix to config option symbol
[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
f5ef2f7b
YM
4# If no prefix forced, use the default CONFIG_
5CONFIG_="${CONFIG_-CONFIG_}"
6
8e54701e
AK
7usage() {
8 cat >&2 <<EOL
9Manipulate options in a .config file from the command line.
10Usage:
11config options command ...
12commands:
13 --enable|-e option Enable option
14 --disable|-d option Disable option
1f990cf9 15 --module|-m option Turn option into a module
f0a6332c
JA
16 --set-str option string
17 Set option to "string"
18 --set-val option value
19 Set option to value
1f990cf9 20 --state|-s option Print state of option (n,y,m,undef)
8e54701e
AK
21
22 --enable-after|-E beforeopt option
23 Enable option directly after other option
24 --disable-after|-D beforeopt option
25 Disable option directly after other option
26 --module-after|-M beforeopt option
27 Turn option into module directly after other option
28
29 commands can be repeated multiple times
30
31options:
4edc7e32
YM
32 --file config-file .config file to change (default .config)
33 --keep-case|-k Keep next symbols' case (dont' upper-case it)
8e54701e
AK
34
35config doesn't check the validity of the .config file. This is done at next
4edc7e32
YM
36make time.
37
38By default, config will upper-case the given symbol. Use --keep-case to keep
39the case of all following symbols unchanged.
f5ef2f7b
YM
40
41config uses 'CONFIG_' as the default symbol prefix. Set the environment
42variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ...
8e54701e
AK
43EOL
44 exit 1
45}
46
47checkarg() {
48 ARG="$1"
49 if [ "$ARG" = "" ] ; then
50 usage
51 fi
52 case "$ARG" in
f5ef2f7b
YM
53 ${CONFIG_}*)
54 ARG="${ARG/${CONFIG_}/}"
8e54701e
AK
55 ;;
56 esac
4edc7e32
YM
57 if [ "$MUNGE_CASE" = "yes" ] ; then
58 ARG="`echo $ARG | tr a-z A-Z`"
59 fi
8e54701e
AK
60}
61
56643222
MM
62set_var() {
63 local name=$1 new=$2 before=$3
64
65 name_re="^($name=|# $name is not set)"
66 before_re="^($before=|# $before is not set)"
67 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
68 sed -ri "/$before_re/a $new" "$FN"
69 elif grep -Eq "$name_re" "$FN"; then
70 sed -ri "s:$name_re.*:$new:" "$FN"
71 else
72 echo "$new" >>"$FN"
73 fi
8e54701e
AK
74}
75
76if [ "$1" = "--file" ]; then
77 FN="$2"
78 if [ "$FN" = "" ] ; then
79 usage
80 fi
47312d2c 81 shift 2
8e54701e
AK
82else
83 FN=.config
84fi
85
2302e873
AK
86if [ "$1" = "" ] ; then
87 usage
88fi
89
4edc7e32 90MUNGE_CASE=yes
8e54701e
AK
91while [ "$1" != "" ] ; do
92 CMD="$1"
93 shift
94 case "$CMD" in
4edc7e32
YM
95 --keep-case|-k)
96 MUNGE_CASE=no
97 shift
98 continue
99 ;;
47312d2c
MM
100 --refresh)
101 ;;
102 --*-after)
103 checkarg "$1"
104 A=$ARG
105 checkarg "$2"
106 B=$ARG
107 shift 2
108 ;;
45f53cc9 109 -*)
8e54701e 110 checkarg "$1"
8e54701e
AK
111 shift
112 ;;
47312d2c
MM
113 esac
114 case "$CMD" in
115 --enable|-e)
f5ef2f7b 116 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
47312d2c 117 ;;
8e54701e
AK
118
119 --disable|-d)
f5ef2f7b 120 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
8e54701e
AK
121 ;;
122
123 --module|-m)
f5ef2f7b 124 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
8e54701e
AK
125 ;;
126
1f990cf9 127 --set-str)
d6686da8 128 # sed swallows one level of escaping, so we need double-escaping
f5ef2f7b 129 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
1f990cf9
MM
130 shift
131 ;;
132
f0a6332c 133 --set-val)
f5ef2f7b 134 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
f0a6332c
JA
135 shift
136 ;;
137
8e54701e 138 --state|-s)
f5ef2f7b 139 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
8e54701e
AK
140 echo n
141 else
f5ef2f7b 142 V="$(grep "^${CONFIG_}$ARG=" $FN)"
8e54701e
AK
143 if [ $? != 0 ] ; then
144 echo undef
145 else
f5ef2f7b 146 V="${V/#${CONFIG_}$ARG=/}"
d6686da8
YM
147 V="${V/#\"/}"
148 V="${V/%\"/}"
149 V="${V/\\\"/\"}"
150 echo "${V}"
8e54701e
AK
151 fi
152 fi
8e54701e
AK
153 ;;
154
155 --enable-after|-E)
f5ef2f7b 156 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
8e54701e
AK
157 ;;
158
159 --disable-after|-D)
f5ef2f7b 160 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
8e54701e
AK
161 ;;
162
163 --module-after|-M)
f5ef2f7b 164 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
8e54701e
AK
165 ;;
166
167 # undocumented because it ignores --file (fixme)
168 --refresh)
169 yes "" | make oldconfig
170 ;;
171
172 *)
173 usage
174 ;;
175 esac
176done
177