blob: ff88e2faefd35c2d763a1d485eef39140efaa300 [file] [log] [blame]
Finn Behrensc25ce582020-11-23 15:15:33 +01001#!/usr/bin/env 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
Jeremie Francois (on alpha)e461bc92020-04-10 18:57:40 +020010# We use an uncommon delimiter for sed substitutions
11SED_DELIM=$(echo -en "\001")
12
Andi Kleen8e547012009-01-03 03:21:41 +010013usage() {
14 cat >&2 <<EOL
15Manipulate options in a .config file from the command line.
16Usage:
Clement Chauplannaz73877782013-05-12 21:08:51 +020017$myname options command ...
Andi Kleen8e547012009-01-03 03:21:41 +010018commands:
19 --enable|-e option Enable option
20 --disable|-d option Disable option
Michal Marek1f990cf2009-05-25 16:43:27 +020021 --module|-m option Turn option into a module
Jonas Aabergf0a63322010-12-15 08:37:00 +010022 --set-str option string
23 Set option to "string"
24 --set-val option value
25 Set option to value
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020026 --undefine|-u option Undefine option
Michal Marek1f990cf2009-05-25 16:43:27 +020027 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010028
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
35
36 commands can be repeated multiple times
37
38options:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020039 --file config-file .config file to change (default .config)
40 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010041
Clement Chauplannaz73877782013-05-12 21:08:51 +020042$myname doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020043make time.
44
Clement Chauplannaz73877782013-05-12 21:08:51 +020045By default, $myname will upper-case the given symbol. Use --keep-case to keep
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020046the case of all following symbols unchanged.
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020047
Clement Chauplannaz73877782013-05-12 21:08:51 +020048$myname uses 'CONFIG_' as the default symbol prefix. Set the environment
49variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
Andi Kleen8e547012009-01-03 03:21:41 +010050EOL
51 exit 1
52}
53
54checkarg() {
55 ARG="$1"
56 if [ "$ARG" = "" ] ; then
57 usage
58 fi
59 case "$ARG" in
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020060 ${CONFIG_}*)
61 ARG="${ARG/${CONFIG_}/}"
Andi Kleen8e547012009-01-03 03:21:41 +010062 ;;
63 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020064 if [ "$MUNGE_CASE" = "yes" ] ; then
65 ARG="`echo $ARG | tr a-z A-Z`"
66 fi
Andi Kleen8e547012009-01-03 03:21:41 +010067}
68
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020069txt_append() {
70 local anchor="$1"
71 local insert="$2"
72 local infile="$3"
73 local tmpfile="$infile.swp"
74
75 # sed append cmd: 'a\' + newline + text + newline
76 cmd="$(printf "a\\%b$insert" "\n")"
77
78 sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
79 # replace original file with the edited one
80 mv "$tmpfile" "$infile"
81}
82
83txt_subst() {
84 local before="$1"
85 local after="$2"
86 local infile="$3"
87 local tmpfile="$infile.swp"
88
Jeremie Francois (on alpha)e461bc92020-04-10 18:57:40 +020089 sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile"
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020090 # replace original file with the edited one
91 mv "$tmpfile" "$infile"
92}
93
94txt_delete() {
95 local text="$1"
96 local infile="$2"
97 local tmpfile="$infile.swp"
98
99 sed -e "/$text/d" "$infile" >"$tmpfile"
100 # replace original file with the edited one
101 mv "$tmpfile" "$infile"
102}
103
Michal Marek56643222009-06-14 22:48:07 +0200104set_var() {
105 local name=$1 new=$2 before=$3
106
107 name_re="^($name=|# $name is not set)"
108 before_re="^($before=|# $before is not set)"
109 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200110 txt_append "^$before=" "$new" "$FN"
111 txt_append "^# $before is not set" "$new" "$FN"
Michal Marek56643222009-06-14 22:48:07 +0200112 elif grep -Eq "$name_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200113 txt_subst "^$name=.*" "$new" "$FN"
114 txt_subst "^# $name is not set" "$new" "$FN"
Michal Marek56643222009-06-14 22:48:07 +0200115 else
116 echo "$new" >>"$FN"
117 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100118}
119
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200120undef_var() {
121 local name=$1
122
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200123 txt_delete "^$name=" "$FN"
124 txt_delete "^# $name is not set" "$FN"
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200125}
126
Andi Kleen8e547012009-01-03 03:21:41 +0100127if [ "$1" = "--file" ]; then
128 FN="$2"
129 if [ "$FN" = "" ] ; then
130 usage
131 fi
Michal Marek47312d22009-05-25 16:43:25 +0200132 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +0100133else
134 FN=.config
135fi
136
Andi Kleen2302e872009-01-07 22:33:15 +0100137if [ "$1" = "" ] ; then
138 usage
139fi
140
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200141MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +0100142while [ "$1" != "" ] ; do
143 CMD="$1"
144 shift
145 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200146 --keep-case|-k)
147 MUNGE_CASE=no
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200148 continue
149 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200150 --refresh)
151 ;;
Clement Chauplannaz57a9c762013-05-12 21:08:52 +0200152 --*-after|-E|-D|-M)
Andi Kleen8e547012009-01-03 03:21:41 +0100153 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200154 A=$ARG
155 checkarg "$2"
156 B=$ARG
157 shift 2
158 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100159 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200160 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100161 shift
162 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200163 esac
164 case "$CMD" in
165 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200166 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200167 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100168
169 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200170 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100171 ;;
172
173 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200174 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100175 ;;
176
Michal Marek1f990cf2009-05-25 16:43:27 +0200177 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200178 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200179 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200180 shift
181 ;;
182
Jonas Aabergf0a63322010-12-15 08:37:00 +0100183 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200184 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100185 shift
186 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200187 --undefine|-u)
188 undef_var "${CONFIG_}$ARG"
189 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100190
Andi Kleen8e547012009-01-03 03:21:41 +0100191 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200192 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100193 echo n
194 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200195 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100196 if [ $? != 0 ] ; then
197 echo undef
198 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200199 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200200 V="${V/#\"/}"
201 V="${V/%\"/}"
Yann E. MORIN1925a272012-07-15 22:37:35 +0200202 V="${V//\\\"/\"}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200203 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100204 fi
205 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100206 ;;
207
208 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200209 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100210 ;;
211
212 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200213 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100214 ;;
215
216 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200217 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100218 ;;
219
220 # undocumented because it ignores --file (fixme)
221 --refresh)
222 yes "" | make oldconfig
223 ;;
224
225 *)
Randy Dunlapd39648e2020-12-19 09:08:05 -0800226 echo "bad command: $CMD" >&2
Andi Kleen8e547012009-01-03 03:21:41 +0100227 usage
228 ;;
229 esac
230done