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