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 | |
| 4 | usage() { |
| 5 | cat >&2 <<EOL |
| 6 | Manipulate options in a .config file from the command line. |
| 7 | Usage: |
| 8 | config options command ... |
| 9 | commands: |
| 10 | --enable|-e option Enable option |
| 11 | --disable|-d option Disable option |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 12 | --module|-m option Turn option into a module |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 13 | --set-str option string |
| 14 | Set option to "string" |
| 15 | --set-val option value |
| 16 | Set option to value |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 17 | --state|-s option Print state of option (n,y,m,undef) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 18 | |
| 19 | --enable-after|-E beforeopt option |
| 20 | Enable option directly after other option |
| 21 | --disable-after|-D beforeopt option |
| 22 | Disable option directly after other option |
| 23 | --module-after|-M beforeopt option |
| 24 | Turn option into module directly after other option |
| 25 | |
| 26 | commands can be repeated multiple times |
| 27 | |
| 28 | options: |
| 29 | --file .config file to change (default .config) |
| 30 | |
| 31 | config doesn't check the validity of the .config file. This is done at next |
| 32 | make time. |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 33 | EOL |
| 34 | exit 1 |
| 35 | } |
| 36 | |
| 37 | checkarg() { |
| 38 | ARG="$1" |
| 39 | if [ "$ARG" = "" ] ; then |
| 40 | usage |
| 41 | fi |
| 42 | case "$ARG" in |
| 43 | CONFIG_*) |
| 44 | ARG="${ARG/CONFIG_/}" |
| 45 | ;; |
| 46 | esac |
| 47 | ARG="`echo $ARG | tr a-z A-Z`" |
| 48 | } |
| 49 | |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 50 | set_var() { |
| 51 | local name=$1 new=$2 before=$3 |
| 52 | |
| 53 | name_re="^($name=|# $name is not set)" |
| 54 | before_re="^($before=|# $before is not set)" |
| 55 | if test -n "$before" && grep -Eq "$before_re" "$FN"; then |
| 56 | sed -ri "/$before_re/a $new" "$FN" |
| 57 | elif grep -Eq "$name_re" "$FN"; then |
| 58 | sed -ri "s:$name_re.*:$new:" "$FN" |
| 59 | else |
| 60 | echo "$new" >>"$FN" |
| 61 | fi |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if [ "$1" = "--file" ]; then |
| 65 | FN="$2" |
| 66 | if [ "$FN" = "" ] ; then |
| 67 | usage |
| 68 | fi |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 69 | shift 2 |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 70 | else |
| 71 | FN=.config |
| 72 | fi |
| 73 | |
Andi Kleen | 2302e87 | 2009-01-07 22:33:15 +0100 | [diff] [blame] | 74 | if [ "$1" = "" ] ; then |
| 75 | usage |
| 76 | fi |
| 77 | |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 78 | while [ "$1" != "" ] ; do |
| 79 | CMD="$1" |
| 80 | shift |
| 81 | case "$CMD" in |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 82 | --refresh) |
| 83 | ;; |
| 84 | --*-after) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 85 | checkarg "$1" |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 86 | A=$ARG |
| 87 | checkarg "$2" |
| 88 | B=$ARG |
| 89 | shift 2 |
| 90 | ;; |
Andi Kleen | 45f53cc | 2010-11-05 12:06:05 +0100 | [diff] [blame] | 91 | -*) |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 92 | checkarg "$1" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 93 | shift |
| 94 | ;; |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 95 | esac |
| 96 | case "$CMD" in |
| 97 | --enable|-e) |
| 98 | set_var "CONFIG_$ARG" "CONFIG_$ARG=y" |
| 99 | ;; |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 100 | |
| 101 | --disable|-d) |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 102 | set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 103 | ;; |
| 104 | |
| 105 | --module|-m) |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 106 | set_var "CONFIG_$ARG" "CONFIG_$ARG=m" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 107 | ;; |
| 108 | |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 109 | --set-str) |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 110 | # sed swallows one level of escaping, so we need double-escaping |
| 111 | set_var "CONFIG_$ARG" "CONFIG_$ARG=\"${1//\"/\\\\\"}\"" |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 112 | shift |
| 113 | ;; |
| 114 | |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 115 | --set-val) |
| 116 | set_var "CONFIG_$ARG" "CONFIG_$ARG=$1" |
| 117 | shift |
| 118 | ;; |
| 119 | |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 120 | --state|-s) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 121 | if grep -q "# CONFIG_$ARG is not set" $FN ; then |
| 122 | echo n |
| 123 | else |
| 124 | V="$(grep "^CONFIG_$ARG=" $FN)" |
| 125 | if [ $? != 0 ] ; then |
| 126 | echo undef |
| 127 | else |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 128 | V="${V/#CONFIG_$ARG=/}" |
| 129 | V="${V/#\"/}" |
| 130 | V="${V/%\"/}" |
| 131 | V="${V/\\\"/\"}" |
| 132 | echo "${V}" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 133 | fi |
| 134 | fi |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 135 | ;; |
| 136 | |
| 137 | --enable-after|-E) |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 138 | set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 139 | ;; |
| 140 | |
| 141 | --disable-after|-D) |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 142 | set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 143 | ;; |
| 144 | |
| 145 | --module-after|-M) |
Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame] | 146 | set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 147 | ;; |
| 148 | |
| 149 | # undocumented because it ignores --file (fixme) |
| 150 | --refresh) |
| 151 | yes "" | make oldconfig |
| 152 | ;; |
| 153 | |
| 154 | *) |
| 155 | usage |
| 156 | ;; |
| 157 | esac |
| 158 | done |
| 159 | |