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