blob: e0e39826dae90ab735d1510e882bd32a9536e005 [file] [log] [blame]
Andi Kleen8e547012009-01-03 03:21:41 +01001#!/bin/bash
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Andi Kleen8e547012009-01-03 03:21:41 +01003# Manipulate options in a .config file from the command line
4
Clement Chauplannaz73877782013-05-12 21:08:51 +02005myname=${0##*/}
6
Yann E. MORINf5ef2f72012-06-08 01:48:56 +02007# If no prefix forced, use the default CONFIG_
8CONFIG_="${CONFIG_-CONFIG_}"
9
Andi Kleen8e547012009-01-03 03:21:41 +010010usage() {
11 cat >&2 <<EOL
12Manipulate options in a .config file from the command line.
13Usage:
Clement Chauplannaz73877782013-05-12 21:08:51 +020014$myname options command ...
Andi Kleen8e547012009-01-03 03:21:41 +010015commands:
16 --enable|-e option Enable option
17 --disable|-d option Disable option
Michal Marek1f990cf2009-05-25 16:43:27 +020018 --module|-m option Turn option into a module
Jonas Aabergf0a63322010-12-15 08:37:00 +010019 --set-str option string
20 Set option to "string"
21 --set-val option value
22 Set option to value
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020023 --undefine|-u option Undefine option
Michal Marek1f990cf2009-05-25 16:43:27 +020024 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010025
26 --enable-after|-E beforeopt option
27 Enable option directly after other option
28 --disable-after|-D beforeopt option
29 Disable option directly after other option
30 --module-after|-M beforeopt option
31 Turn option into module directly after other option
32
33 commands can be repeated multiple times
34
35options:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020036 --file config-file .config file to change (default .config)
37 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010038
Clement Chauplannaz73877782013-05-12 21:08:51 +020039$myname doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020040make time.
41
Clement Chauplannaz73877782013-05-12 21:08:51 +020042By default, $myname will upper-case the given symbol. Use --keep-case to keep
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020043the case of all following symbols unchanged.
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020044
Clement Chauplannaz73877782013-05-12 21:08:51 +020045$myname uses 'CONFIG_' as the default symbol prefix. Set the environment
46variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
Andi Kleen8e547012009-01-03 03:21:41 +010047EOL
48 exit 1
49}
50
51checkarg() {
52 ARG="$1"
53 if [ "$ARG" = "" ] ; then
54 usage
55 fi
56 case "$ARG" in
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020057 ${CONFIG_}*)
58 ARG="${ARG/${CONFIG_}/}"
Andi Kleen8e547012009-01-03 03:21:41 +010059 ;;
60 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020061 if [ "$MUNGE_CASE" = "yes" ] ; then
62 ARG="`echo $ARG | tr a-z A-Z`"
63 fi
Andi Kleen8e547012009-01-03 03:21:41 +010064}
65
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020066txt_append() {
67 local anchor="$1"
68 local insert="$2"
69 local infile="$3"
70 local tmpfile="$infile.swp"
71
72 # sed append cmd: 'a\' + newline + text + newline
73 cmd="$(printf "a\\%b$insert" "\n")"
74
75 sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
76 # replace original file with the edited one
77 mv "$tmpfile" "$infile"
78}
79
80txt_subst() {
81 local before="$1"
82 local after="$2"
83 local infile="$3"
84 local tmpfile="$infile.swp"
85
Clement Chauplannaz86eb78182013-09-13 10:45:13 +020086 sed -e "s:$before:$after:" "$infile" >"$tmpfile"
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020087 # replace original file with the edited one
88 mv "$tmpfile" "$infile"
89}
90
91txt_delete() {
92 local text="$1"
93 local infile="$2"
94 local tmpfile="$infile.swp"
95
96 sed -e "/$text/d" "$infile" >"$tmpfile"
97 # replace original file with the edited one
98 mv "$tmpfile" "$infile"
99}
100
Michal Marek566432222009-06-14 22:48:07 +0200101set_var() {
102 local name=$1 new=$2 before=$3
103
104 name_re="^($name=|# $name is not set)"
105 before_re="^($before=|# $before is not set)"
106 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200107 txt_append "^$before=" "$new" "$FN"
108 txt_append "^# $before is not set" "$new" "$FN"
Michal Marek566432222009-06-14 22:48:07 +0200109 elif grep -Eq "$name_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200110 txt_subst "^$name=.*" "$new" "$FN"
111 txt_subst "^# $name is not set" "$new" "$FN"
Michal Marek566432222009-06-14 22:48:07 +0200112 else
113 echo "$new" >>"$FN"
114 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100115}
116
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200117undef_var() {
118 local name=$1
119
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200120 txt_delete "^$name=" "$FN"
121 txt_delete "^# $name is not set" "$FN"
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200122}
123
Andi Kleen8e547012009-01-03 03:21:41 +0100124if [ "$1" = "--file" ]; then
125 FN="$2"
126 if [ "$FN" = "" ] ; then
127 usage
128 fi
Michal Marek47312d22009-05-25 16:43:25 +0200129 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +0100130else
131 FN=.config
132fi
133
Andi Kleen2302e872009-01-07 22:33:15 +0100134if [ "$1" = "" ] ; then
135 usage
136fi
137
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200138MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +0100139while [ "$1" != "" ] ; do
140 CMD="$1"
141 shift
142 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200143 --keep-case|-k)
144 MUNGE_CASE=no
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200145 continue
146 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200147 --refresh)
148 ;;
Clement Chauplannaz57a9c762013-05-12 21:08:52 +0200149 --*-after|-E|-D|-M)
Andi Kleen8e547012009-01-03 03:21:41 +0100150 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200151 A=$ARG
152 checkarg "$2"
153 B=$ARG
154 shift 2
155 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100156 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200157 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100158 shift
159 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200160 esac
161 case "$CMD" in
162 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200163 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200164 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100165
166 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200167 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100168 ;;
169
170 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200171 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100172 ;;
173
Michal Marek1f990cf2009-05-25 16:43:27 +0200174 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200175 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200176 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200177 shift
178 ;;
179
Jonas Aabergf0a63322010-12-15 08:37:00 +0100180 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200181 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100182 shift
183 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200184 --undefine|-u)
185 undef_var "${CONFIG_}$ARG"
186 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100187
Andi Kleen8e547012009-01-03 03:21:41 +0100188 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200189 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100190 echo n
191 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200192 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100193 if [ $? != 0 ] ; then
194 echo undef
195 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200196 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200197 V="${V/#\"/}"
198 V="${V/%\"/}"
Yann E. MORIN1925a272012-07-15 22:37:35 +0200199 V="${V//\\\"/\"}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200200 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100201 fi
202 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100203 ;;
204
205 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200206 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100207 ;;
208
209 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200210 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100211 ;;
212
213 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200214 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100215 ;;
216
217 # undocumented because it ignores --file (fixme)
218 --refresh)
219 yes "" | make oldconfig
220 ;;
221
222 *)
223 usage
224 ;;
225 esac
226done