Finn Behrens | 5ea96ed | 2020-11-23 15:15:33 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 3 | # Manipulate options in a .config file from the command line |
| 4 | |
Clement Chauplannaz | 7387778 | 2013-05-12 21:08:51 +0200 | [diff] [blame] | 5 | myname=${0##*/} |
| 6 | |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 7 | # If no prefix forced, use the default CONFIG_ |
| 8 | CONFIG_="${CONFIG_-CONFIG_}" |
| 9 | |
Jeremie Francois (on alpha) | e6b43bd | 2020-04-10 18:57:40 +0200 | [diff] [blame] | 10 | # We use an uncommon delimiter for sed substitutions |
| 11 | SED_DELIM=$(echo -en "\001") |
| 12 | |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 13 | usage() { |
| 14 | cat >&2 <<EOL |
| 15 | Manipulate options in a .config file from the command line. |
| 16 | Usage: |
Clement Chauplannaz | 7387778 | 2013-05-12 21:08:51 +0200 | [diff] [blame] | 17 | $myname options command ... |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 18 | commands: |
| 19 | --enable|-e option Enable option |
| 20 | --disable|-d option Disable option |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 21 | --module|-m option Turn option into a module |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 22 | --set-str option string |
| 23 | Set option to "string" |
| 24 | --set-val option value |
| 25 | Set option to value |
Yann E. MORIN | d5bfb6b | 2012-06-08 01:48:57 +0200 | [diff] [blame] | 26 | --undefine|-u option Undefine option |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 27 | --state|-s option Print state of option (n,y,m,undef) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 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: |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 39 | --file config-file .config file to change (default .config) |
| 40 | --keep-case|-k Keep next symbols' case (dont' upper-case it) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 41 | |
Clement Chauplannaz | 7387778 | 2013-05-12 21:08:51 +0200 | [diff] [blame] | 42 | $myname doesn't check the validity of the .config file. This is done at next |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 43 | make time. |
| 44 | |
Clement Chauplannaz | 7387778 | 2013-05-12 21:08:51 +0200 | [diff] [blame] | 45 | By default, $myname will upper-case the given symbol. Use --keep-case to keep |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 46 | the case of all following symbols unchanged. |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 47 | |
Clement Chauplannaz | 7387778 | 2013-05-12 21:08:51 +0200 | [diff] [blame] | 48 | $myname uses 'CONFIG_' as the default symbol prefix. Set the environment |
| 49 | variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ... |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 50 | EOL |
| 51 | exit 1 |
| 52 | } |
| 53 | |
| 54 | checkarg() { |
| 55 | ARG="$1" |
| 56 | if [ "$ARG" = "" ] ; then |
| 57 | usage |
| 58 | fi |
| 59 | case "$ARG" in |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 60 | ${CONFIG_}*) |
| 61 | ARG="${ARG/${CONFIG_}/}" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 62 | ;; |
| 63 | esac |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 64 | if [ "$MUNGE_CASE" = "yes" ] ; then |
| 65 | ARG="`echo $ARG | tr a-z A-Z`" |
| 66 | fi |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Clement Chauplannaz | 83e8b90 | 2013-07-13 16:36:56 +0200 | [diff] [blame] | 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 | |
Jeremie Francois (on alpha) | e6b43bd | 2020-04-10 18:57:40 +0200 | [diff] [blame] | 89 | sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile" |
Clement Chauplannaz | 83e8b90 | 2013-07-13 16:36:56 +0200 | [diff] [blame] | 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 | |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 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 |
Clement Chauplannaz | 83e8b90 | 2013-07-13 16:36:56 +0200 | [diff] [blame] | 110 | txt_append "^$before=" "$new" "$FN" |
| 111 | txt_append "^# $before is not set" "$new" "$FN" |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 112 | elif grep -Eq "$name_re" "$FN"; then |
Clement Chauplannaz | 83e8b90 | 2013-07-13 16:36:56 +0200 | [diff] [blame] | 113 | txt_subst "^$name=.*" "$new" "$FN" |
| 114 | txt_subst "^# $name is not set" "$new" "$FN" |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 115 | else |
| 116 | echo "$new" >>"$FN" |
| 117 | fi |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Yann E. MORIN | d5bfb6b | 2012-06-08 01:48:57 +0200 | [diff] [blame] | 120 | undef_var() { |
| 121 | local name=$1 |
| 122 | |
Clement Chauplannaz | 83e8b90 | 2013-07-13 16:36:56 +0200 | [diff] [blame] | 123 | txt_delete "^$name=" "$FN" |
| 124 | txt_delete "^# $name is not set" "$FN" |
Yann E. MORIN | d5bfb6b | 2012-06-08 01:48:57 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 127 | if [ "$1" = "--file" ]; then |
| 128 | FN="$2" |
| 129 | if [ "$FN" = "" ] ; then |
| 130 | usage |
| 131 | fi |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 132 | shift 2 |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 133 | else |
| 134 | FN=.config |
| 135 | fi |
| 136 | |
Andi Kleen | 2302e87 | 2009-01-07 22:33:15 +0100 | [diff] [blame] | 137 | if [ "$1" = "" ] ; then |
| 138 | usage |
| 139 | fi |
| 140 | |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 141 | MUNGE_CASE=yes |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 142 | while [ "$1" != "" ] ; do |
| 143 | CMD="$1" |
| 144 | shift |
| 145 | case "$CMD" in |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 146 | --keep-case|-k) |
| 147 | MUNGE_CASE=no |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 148 | continue |
| 149 | ;; |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 150 | --refresh) |
| 151 | ;; |
Clement Chauplannaz | 57a9c76 | 2013-05-12 21:08:52 +0200 | [diff] [blame] | 152 | --*-after|-E|-D|-M) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 153 | checkarg "$1" |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 154 | A=$ARG |
| 155 | checkarg "$2" |
| 156 | B=$ARG |
| 157 | shift 2 |
| 158 | ;; |
Andi Kleen | 45f53cc | 2010-11-05 12:06:05 +0100 | [diff] [blame] | 159 | -*) |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 160 | checkarg "$1" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 161 | shift |
| 162 | ;; |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 163 | esac |
| 164 | case "$CMD" in |
| 165 | --enable|-e) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 166 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 167 | ;; |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 168 | |
| 169 | --disable|-d) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 170 | set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 171 | ;; |
| 172 | |
| 173 | --module|-m) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 174 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 175 | ;; |
| 176 | |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 177 | --set-str) |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 178 | # sed swallows one level of escaping, so we need double-escaping |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 179 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 180 | shift |
| 181 | ;; |
| 182 | |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 183 | --set-val) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 184 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 185 | shift |
| 186 | ;; |
Yann E. MORIN | d5bfb6b | 2012-06-08 01:48:57 +0200 | [diff] [blame] | 187 | --undefine|-u) |
| 188 | undef_var "${CONFIG_}$ARG" |
| 189 | ;; |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 190 | |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 191 | --state|-s) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 192 | if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 193 | echo n |
| 194 | else |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 195 | V="$(grep "^${CONFIG_}$ARG=" $FN)" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 196 | if [ $? != 0 ] ; then |
| 197 | echo undef |
| 198 | else |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 199 | V="${V/#${CONFIG_}$ARG=/}" |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 200 | V="${V/#\"/}" |
| 201 | V="${V/%\"/}" |
Yann E. MORIN | 1925a27 | 2012-07-15 22:37:35 +0200 | [diff] [blame] | 202 | V="${V//\\\"/\"}" |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 203 | echo "${V}" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 204 | fi |
| 205 | fi |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 206 | ;; |
| 207 | |
| 208 | --enable-after|-E) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 209 | set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 210 | ;; |
| 211 | |
| 212 | --disable-after|-D) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 213 | set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 214 | ;; |
| 215 | |
| 216 | --module-after|-M) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 217 | set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 218 | ;; |
| 219 | |
| 220 | # undocumented because it ignores --file (fixme) |
| 221 | --refresh) |
| 222 | yes "" | make oldconfig |
| 223 | ;; |
| 224 | |
| 225 | *) |
| 226 | usage |
| 227 | ;; |
| 228 | esac |
| 229 | done |