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