blob: 06ac9882e1de20ea63966f103e9d01b5b2914f6e [file] [log] [blame]
Finn Behrens2d1f7c92020-11-23 15:15:33 +01001#!/usr/bin/env bash
Andi Kleen8e547012009-01-03 03:21:41 +01002# Manipulate options in a .config file from the command line
3
Clement Chauplannaz73877782013-05-12 21:08:51 +02004myname=${0##*/}
5
Yann E. MORINf5ef2f72012-06-08 01:48:56 +02006# If no prefix forced, use the default CONFIG_
7CONFIG_="${CONFIG_-CONFIG_}"
8
Jeremie Francois (on alpha)4323eed2020-04-10 18:57:40 +02009# We use an uncommon delimiter for sed substitutions
10SED_DELIM=$(echo -en "\001")
11
Andi Kleen8e547012009-01-03 03:21:41 +010012usage() {
13 cat >&2 <<EOL
14Manipulate options in a .config file from the command line.
15Usage:
Clement Chauplannaz73877782013-05-12 21:08:51 +020016$myname options command ...
Andi Kleen8e547012009-01-03 03:21:41 +010017commands:
18 --enable|-e option Enable option
19 --disable|-d option Disable option
Michal Marek1f990cf2009-05-25 16:43:27 +020020 --module|-m option Turn option into a module
Jonas Aabergf0a63322010-12-15 08:37:00 +010021 --set-str option string
22 Set option to "string"
23 --set-val option value
24 Set option to value
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020025 --undefine|-u option Undefine option
Michal Marek1f990cf2009-05-25 16:43:27 +020026 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010027
28 --enable-after|-E beforeopt option
29 Enable option directly after other option
30 --disable-after|-D beforeopt option
31 Disable option directly after other option
32 --module-after|-M beforeopt option
33 Turn option into module directly after other option
34
35 commands can be repeated multiple times
36
37options:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020038 --file config-file .config file to change (default .config)
39 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010040
Clement Chauplannaz73877782013-05-12 21:08:51 +020041$myname doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020042make time.
43
Clement Chauplannaz73877782013-05-12 21:08:51 +020044By default, $myname will upper-case the given symbol. Use --keep-case to keep
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020045the case of all following symbols unchanged.
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020046
Clement Chauplannaz73877782013-05-12 21:08:51 +020047$myname uses 'CONFIG_' as the default symbol prefix. Set the environment
48variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
Andi Kleen8e547012009-01-03 03:21:41 +010049EOL
50 exit 1
51}
52
53checkarg() {
54 ARG="$1"
55 if [ "$ARG" = "" ] ; then
56 usage
57 fi
58 case "$ARG" in
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020059 ${CONFIG_}*)
60 ARG="${ARG/${CONFIG_}/}"
Andi Kleen8e547012009-01-03 03:21:41 +010061 ;;
62 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020063 if [ "$MUNGE_CASE" = "yes" ] ; then
64 ARG="`echo $ARG | tr a-z A-Z`"
65 fi
Andi Kleen8e547012009-01-03 03:21:41 +010066}
67
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020068txt_append() {
69 local anchor="$1"
70 local insert="$2"
71 local infile="$3"
72 local tmpfile="$infile.swp"
73
74 # sed append cmd: 'a\' + newline + text + newline
75 cmd="$(printf "a\\%b$insert" "\n")"
76
77 sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
78 # replace original file with the edited one
79 mv "$tmpfile" "$infile"
80}
81
82txt_subst() {
83 local before="$1"
84 local after="$2"
85 local infile="$3"
86 local tmpfile="$infile.swp"
87
Jeremie Francois (on alpha)4323eed2020-04-10 18:57:40 +020088 sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile"
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020089 # replace original file with the edited one
90 mv "$tmpfile" "$infile"
91}
92
93txt_delete() {
94 local text="$1"
95 local infile="$2"
96 local tmpfile="$infile.swp"
97
98 sed -e "/$text/d" "$infile" >"$tmpfile"
99 # replace original file with the edited one
100 mv "$tmpfile" "$infile"
101}
102
Michal Marek56643222009-06-14 22:48:07 +0200103set_var() {
104 local name=$1 new=$2 before=$3
105
106 name_re="^($name=|# $name is not set)"
107 before_re="^($before=|# $before is not set)"
108 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200109 txt_append "^$before=" "$new" "$FN"
110 txt_append "^# $before is not set" "$new" "$FN"
Michal Marek56643222009-06-14 22:48:07 +0200111 elif grep -Eq "$name_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200112 txt_subst "^$name=.*" "$new" "$FN"
113 txt_subst "^# $name is not set" "$new" "$FN"
Michal Marek56643222009-06-14 22:48:07 +0200114 else
115 echo "$new" >>"$FN"
116 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100117}
118
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200119undef_var() {
120 local name=$1
121
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200122 txt_delete "^$name=" "$FN"
123 txt_delete "^# $name is not set" "$FN"
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200124}
125
Andi Kleen8e547012009-01-03 03:21:41 +0100126if [ "$1" = "--file" ]; then
127 FN="$2"
128 if [ "$FN" = "" ] ; then
129 usage
130 fi
Michal Marek47312d22009-05-25 16:43:25 +0200131 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +0100132else
133 FN=.config
134fi
135
Andi Kleen2302e872009-01-07 22:33:15 +0100136if [ "$1" = "" ] ; then
137 usage
138fi
139
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200140MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +0100141while [ "$1" != "" ] ; do
142 CMD="$1"
143 shift
144 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200145 --keep-case|-k)
146 MUNGE_CASE=no
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200147 continue
148 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200149 --refresh)
150 ;;
Clement Chauplannaz57a9c762013-05-12 21:08:52 +0200151 --*-after|-E|-D|-M)
Andi Kleen8e547012009-01-03 03:21:41 +0100152 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200153 A=$ARG
154 checkarg "$2"
155 B=$ARG
156 shift 2
157 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100158 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200159 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100160 shift
161 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200162 esac
163 case "$CMD" in
164 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200165 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200166 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100167
168 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200169 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100170 ;;
171
172 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200173 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100174 ;;
175
Michal Marek1f990cf2009-05-25 16:43:27 +0200176 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200177 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200178 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200179 shift
180 ;;
181
Jonas Aabergf0a63322010-12-15 08:37:00 +0100182 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200183 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100184 shift
185 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200186 --undefine|-u)
187 undef_var "${CONFIG_}$ARG"
188 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100189
Andi Kleen8e547012009-01-03 03:21:41 +0100190 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200191 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100192 echo n
193 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200194 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100195 if [ $? != 0 ] ; then
196 echo undef
197 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200198 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200199 V="${V/#\"/}"
200 V="${V/%\"/}"
Yann E. MORIN1925a272012-07-15 22:37:35 +0200201 V="${V//\\\"/\"}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200202 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100203 fi
204 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100205 ;;
206
207 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200208 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100209 ;;
210
211 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200212 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100213 ;;
214
215 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200216 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100217 ;;
218
219 # undocumented because it ignores --file (fixme)
220 --refresh)
221 yes "" | make oldconfig
222 ;;
223
224 *)
225 usage
226 ;;
227 esac
228done