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 |
Yann E. MORIN | 4edc7e3 | 2012-06-08 01:48:55 +0200 | [diff] [blame] | 104 | continue |
| 105 | ;; |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 106 | --refresh) |
| 107 | ;; |
| 108 | --*-after) |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 109 | checkarg "$1" |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 110 | A=$ARG |
| 111 | checkarg "$2" |
| 112 | B=$ARG |
| 113 | shift 2 |
| 114 | ;; |
Andi Kleen | 45f53cc | 2010-11-05 12:06:05 +0100 | [diff] [blame] | 115 | -*) |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 116 | checkarg "$1" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 117 | shift |
| 118 | ;; |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 119 | esac |
| 120 | case "$CMD" in |
| 121 | --enable|-e) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 122 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" |
Michal Marek | 47312d2 | 2009-05-25 16:43:25 +0200 | [diff] [blame] | 123 | ;; |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 124 | |
| 125 | --disable|-d) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 126 | set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 127 | ;; |
| 128 | |
| 129 | --module|-m) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 130 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 131 | ;; |
| 132 | |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 133 | --set-str) |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 134 | # sed swallows one level of escaping, so we need double-escaping |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 135 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" |
Michal Marek | 1f990cf | 2009-05-25 16:43:27 +0200 | [diff] [blame] | 136 | shift |
| 137 | ;; |
| 138 | |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 139 | --set-val) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 140 | set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 141 | shift |
| 142 | ;; |
Yann E. MORIN | d5bfb6b | 2012-06-08 01:48:57 +0200 | [diff] [blame] | 143 | --undefine|-u) |
| 144 | undef_var "${CONFIG_}$ARG" |
| 145 | ;; |
Jonas Aaberg | f0a6332 | 2010-12-15 08:37:00 +0100 | [diff] [blame] | 146 | |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 147 | --state|-s) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 148 | if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 149 | echo n |
| 150 | else |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 151 | V="$(grep "^${CONFIG_}$ARG=" $FN)" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 152 | if [ $? != 0 ] ; then |
| 153 | echo undef |
| 154 | else |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 155 | V="${V/#${CONFIG_}$ARG=/}" |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 156 | V="${V/#\"/}" |
| 157 | V="${V/%\"/}" |
Yann E. MORIN | 1925a27 | 2012-07-15 22:37:35 +0200 | [diff] [blame] | 158 | V="${V//\\\"/\"}" |
Yann E. MORIN | d6686da | 2012-04-09 14:49:10 +0200 | [diff] [blame] | 159 | echo "${V}" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 160 | fi |
| 161 | fi |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 162 | ;; |
| 163 | |
| 164 | --enable-after|-E) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 165 | set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 166 | ;; |
| 167 | |
| 168 | --disable-after|-D) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 169 | set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 170 | ;; |
| 171 | |
| 172 | --module-after|-M) |
Yann E. MORIN | f5ef2f7 | 2012-06-08 01:48:56 +0200 | [diff] [blame] | 173 | set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" |
Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 174 | ;; |
| 175 | |
| 176 | # undocumented because it ignores --file (fixme) |
| 177 | --refresh) |
| 178 | yes "" | make oldconfig |
| 179 | ;; |
| 180 | |
| 181 | *) |
| 182 | usage |
| 183 | ;; |
| 184 | esac |
| 185 | done |
| 186 | |