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