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