| 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 | --set-str option value |
| 14 | Set option to "value" |
| 15 | --state|-s option Print state of option (n,y,m,undef) |
| 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. |
| 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 | |
| 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 |
| 60 | } |
| 61 | |
| 62 | if [ "$1" = "--file" ]; then |
| 63 | FN="$2" |
| 64 | if [ "$FN" = "" ] ; then |
| 65 | usage |
| 66 | fi |
| 67 | shift 2 |
| 68 | else |
| 69 | FN=.config |
| 70 | fi |
| 71 | |
| 72 | if [ "$1" = "" ] ; then |
| 73 | usage |
| 74 | fi |
| 75 | |
| 76 | while [ "$1" != "" ] ; do |
| 77 | CMD="$1" |
| 78 | shift |
| 79 | case "$CMD" in |
| 80 | --refresh) |
| 81 | ;; |
| 82 | --*-after) |
| 83 | checkarg "$1" |
| 84 | A=$ARG |
| 85 | checkarg "$2" |
| 86 | B=$ARG |
| 87 | shift 2 |
| 88 | ;; |
| 89 | --*) |
| 90 | checkarg "$1" |
| 91 | shift |
| 92 | ;; |
| 93 | esac |
| 94 | case "$CMD" in |
| 95 | --enable|-e) |
| 96 | set_var "CONFIG_$ARG" "CONFIG_$ARG=y" |
| 97 | ;; |
| 98 | |
| 99 | --disable|-d) |
| 100 | set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set" |
| 101 | ;; |
| 102 | |
| 103 | --module|-m) |
| 104 | set_var "CONFIG_$ARG" "CONFIG_$ARG=m" |
| 105 | ;; |
| 106 | |
| 107 | --set-str) |
| 108 | set_var "CONFIG_$ARG" "CONFIG_$ARG=\"$1\"" |
| 109 | shift |
| 110 | ;; |
| 111 | |
| 112 | --state|-s) |
| 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 |
| 125 | ;; |
| 126 | |
| 127 | --enable-after|-E) |
| 128 | set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A" |
| 129 | ;; |
| 130 | |
| 131 | --disable-after|-D) |
| 132 | set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A" |
| 133 | ;; |
| 134 | |
| 135 | --module-after|-M) |
| 136 | set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A" |
| 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 | |