Commit | Line | Data |
---|---|---|
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 |
5 | myname=${0##*/} |
6 | ||
f5ef2f7b YM |
7 | # If no prefix forced, use the default CONFIG_ |
8 | CONFIG_="${CONFIG_-CONFIG_}" | |
9 | ||
e461bc9f JF |
10 | # We use an uncommon delimiter for sed substitutions |
11 | SED_DELIM=$(echo -en "\001") | |
12 | ||
8e54701e AK |
13 | usage() { |
14 | cat >&2 <<EOL | |
15 | Manipulate options in a .config file from the command line. | |
16 | Usage: | |
73877785 | 17 | $myname options command ... |
8e54701e AK |
18 | commands: |
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 | |
35 | ||
36 | commands can be repeated multiple times | |
37 | ||
38 | options: | |
4edc7e32 YM |
39 | --file config-file .config file to change (default .config) |
40 | --keep-case|-k Keep next symbols' case (dont' upper-case it) | |
8e54701e | 41 | |
73877785 | 42 | $myname doesn't check the validity of the .config file. This is done at next |
4edc7e32 YM |
43 | make time. |
44 | ||
73877785 | 45 | By default, $myname will upper-case the given symbol. Use --keep-case to keep |
4edc7e32 | 46 | the case of all following symbols unchanged. |
f5ef2f7b | 47 | |
73877785 CC |
48 | $myname uses 'CONFIG_' as the default symbol prefix. Set the environment |
49 | variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ... | |
8e54701e AK |
50 | EOL |
51 | exit 1 | |
52 | } | |
53 | ||
54 | checkarg() { | |
55 | ARG="$1" | |
56 | if [ "$ARG" = "" ] ; then | |
57 | usage | |
58 | fi | |
59 | case "$ARG" in | |
f5ef2f7b YM |
60 | ${CONFIG_}*) |
61 | ARG="${ARG/${CONFIG_}/}" | |
8e54701e AK |
62 | ;; |
63 | esac | |
4edc7e32 YM |
64 | if [ "$MUNGE_CASE" = "yes" ] ; then |
65 | ARG="`echo $ARG | tr a-z A-Z`" | |
66 | fi | |
8e54701e AK |
67 | } |
68 | ||
83e8b90e CC |
69 | txt_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 | ||
83 | txt_subst() { | |
84 | local before="$1" | |
85 | local after="$2" | |
86 | local infile="$3" | |
87 | local tmpfile="$infile.swp" | |
88 | ||
e461bc9f | 89 | sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile" |
83e8b90e CC |
90 | # replace original file with the edited one |
91 | mv "$tmpfile" "$infile" | |
92 | } | |
93 | ||
94 | txt_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 | ||
56643222 MM |
104 | set_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 | |
83e8b90e CC |
110 | txt_append "^$before=" "$new" "$FN" |
111 | txt_append "^# $before is not set" "$new" "$FN" | |
56643222 | 112 | elif grep -Eq "$name_re" "$FN"; then |
83e8b90e CC |
113 | txt_subst "^$name=.*" "$new" "$FN" |
114 | txt_subst "^# $name is not set" "$new" "$FN" | |
56643222 MM |
115 | else |
116 | echo "$new" >>"$FN" | |
117 | fi | |
8e54701e AK |
118 | } |
119 | ||
d5bfb6b3 YM |
120 | undef_var() { |
121 | local name=$1 | |
122 | ||
83e8b90e CC |
123 | txt_delete "^$name=" "$FN" |
124 | txt_delete "^# $name is not set" "$FN" | |
d5bfb6b3 YM |
125 | } |
126 | ||
8e54701e AK |
127 | if [ "$1" = "--file" ]; then |
128 | FN="$2" | |
129 | if [ "$FN" = "" ] ; then | |
130 | usage | |
131 | fi | |
47312d2c | 132 | shift 2 |
8e54701e AK |
133 | else |
134 | FN=.config | |
135 | fi | |
136 | ||
2302e873 AK |
137 | if [ "$1" = "" ] ; then |
138 | usage | |
139 | fi | |
140 | ||
4edc7e32 | 141 | MUNGE_CASE=yes |
8e54701e AK |
142 | while [ "$1" != "" ] ; do |
143 | CMD="$1" | |
144 | shift | |
145 | case "$CMD" in | |
4edc7e32 YM |
146 | --keep-case|-k) |
147 | MUNGE_CASE=no | |
4edc7e32 YM |
148 | continue |
149 | ;; | |
47312d2c MM |
150 | --refresh) |
151 | ;; | |
57a9c760 | 152 | --*-after|-E|-D|-M) |
47312d2c MM |
153 | checkarg "$1" |
154 | A=$ARG | |
155 | checkarg "$2" | |
156 | B=$ARG | |
157 | shift 2 | |
158 | ;; | |
45f53cc9 | 159 | -*) |
8e54701e | 160 | checkarg "$1" |
8e54701e AK |
161 | shift |
162 | ;; | |
47312d2c MM |
163 | esac |
164 | case "$CMD" in | |
165 | --enable|-e) | |
f5ef2f7b | 166 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" |
47312d2c | 167 | ;; |
8e54701e AK |
168 | |
169 | --disable|-d) | |
f5ef2f7b | 170 | set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" |
8e54701e AK |
171 | ;; |
172 | ||
173 | --module|-m) | |
f5ef2f7b | 174 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" |
8e54701e AK |
175 | ;; |
176 | ||
1f990cf9 | 177 | --set-str) |
d6686da8 | 178 | # sed swallows one level of escaping, so we need double-escaping |
f5ef2f7b | 179 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" |
1f990cf9 MM |
180 | shift |
181 | ;; | |
182 | ||
f0a6332c | 183 | --set-val) |
f5ef2f7b | 184 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" |
f0a6332c JA |
185 | shift |
186 | ;; | |
d5bfb6b3 YM |
187 | --undefine|-u) |
188 | undef_var "${CONFIG_}$ARG" | |
189 | ;; | |
f0a6332c | 190 | |
8e54701e | 191 | --state|-s) |
f5ef2f7b | 192 | if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then |
8e54701e AK |
193 | echo n |
194 | else | |
f5ef2f7b | 195 | V="$(grep "^${CONFIG_}$ARG=" $FN)" |
8e54701e AK |
196 | if [ $? != 0 ] ; then |
197 | echo undef | |
198 | else | |
f5ef2f7b | 199 | V="${V/#${CONFIG_}$ARG=/}" |
d6686da8 YM |
200 | V="${V/#\"/}" |
201 | V="${V/%\"/}" | |
1925a276 | 202 | V="${V//\\\"/\"}" |
d6686da8 | 203 | echo "${V}" |
8e54701e AK |
204 | fi |
205 | fi | |
8e54701e AK |
206 | ;; |
207 | ||
208 | --enable-after|-E) | |
f5ef2f7b | 209 | set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" |
8e54701e AK |
210 | ;; |
211 | ||
212 | --disable-after|-D) | |
f5ef2f7b | 213 | set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" |
8e54701e AK |
214 | ;; |
215 | ||
216 | --module-after|-M) | |
f5ef2f7b | 217 | set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" |
8e54701e AK |
218 | ;; |
219 | ||
220 | # undocumented because it ignores --file (fixme) | |
221 | --refresh) | |
222 | yes "" | make oldconfig | |
223 | ;; | |
224 | ||
225 | *) | |
d39648eb | 226 | echo "bad command: $CMD" >&2 |
8e54701e AK |
227 | usage |
228 | ;; | |
229 | esac | |
230 | done |