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