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