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