1#!/bin/bash 2# Manipulate options in a .config file from the command line 3 4# If no prefix forced, use the default CONFIG_ 5CONFIG_="${CONFIG_-CONFIG_}" 6 7usage() { 8 cat >&2 <<EOL 9Manipulate options in a .config file from the command line. 10Usage: 11config options command ... 12commands: 13 --enable|-e option Enable option 14 --disable|-d option Disable option 15 --module|-m option Turn option into a module 16 --set-str option string 17 Set option to "string" 18 --set-val option value 19 Set option to value 20 --undefine|-u option Undefine option 21 --state|-s option Print state of option (n,y,m,undef) 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 32options: 33 --file config-file .config file to change (default .config) 34 --keep-case|-k Keep next symbols' case (dont' upper-case it) 35 36config doesn't check the validity of the .config file. This is done at next 37make time. 38 39By default, config will upper-case the given symbol. Use --keep-case to keep 40the case of all following symbols unchanged. 41 42config uses 'CONFIG_' as the default symbol prefix. Set the environment 43variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ... 44EOL 45 exit 1 46} 47 48checkarg() { 49 ARG="$1" 50 if [ "$ARG" = "" ] ; then 51 usage 52 fi 53 case "$ARG" in 54 ${CONFIG_}*) 55 ARG="${ARG/${CONFIG_}/}" 56 ;; 57 esac 58 if [ "$MUNGE_CASE" = "yes" ] ; then 59 ARG="`echo $ARG | tr a-z A-Z`" 60 fi 61} 62 63set_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 75} 76 77undef_var() { 78 local name=$1 79 80 sed -ri "/^($name=|# $name is not set)/d" "$FN" 81} 82 83if [ "$1" = "--file" ]; then 84 FN="$2" 85 if [ "$FN" = "" ] ; then 86 usage 87 fi 88 shift 2 89else 90 FN=.config 91fi 92 93if [ "$1" = "" ] ; then 94 usage 95fi 96 97MUNGE_CASE=yes 98while [ "$1" != "" ] ; do 99 CMD="$1" 100 shift 101 case "$CMD" in 102 --keep-case|-k) 103 MUNGE_CASE=no 104 shift 105 continue 106 ;; 107 --refresh) 108 ;; 109 --*-after) 110 checkarg "$1" 111 A=$ARG 112 checkarg "$2" 113 B=$ARG 114 shift 2 115 ;; 116 -*) 117 checkarg "$1" 118 shift 119 ;; 120 esac 121 case "$CMD" in 122 --enable|-e) 123 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" 124 ;; 125 126 --disable|-d) 127 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" 128 ;; 129 130 --module|-m) 131 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" 132 ;; 133 134 --set-str) 135 # sed swallows one level of escaping, so we need double-escaping 136 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" 137 shift 138 ;; 139 140 --set-val) 141 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" 142 shift 143 ;; 144 --undefine|-u) 145 undef_var "${CONFIG_}$ARG" 146 ;; 147 148 --state|-s) 149 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then 150 echo n 151 else 152 V="$(grep "^${CONFIG_}$ARG=" $FN)" 153 if [ $? != 0 ] ; then 154 echo undef 155 else 156 V="${V/#${CONFIG_}$ARG=/}" 157 V="${V/#\"/}" 158 V="${V/%\"/}" 159 V="${V//\\\"/\"}" 160 echo "${V}" 161 fi 162 fi 163 ;; 164 165 --enable-after|-E) 166 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" 167 ;; 168 169 --disable-after|-D) 170 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" 171 ;; 172 173 --module-after|-M) 174 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" 175 ;; 176 177 # undocumented because it ignores --file (fixme) 178 --refresh) 179 yes "" | make oldconfig 180 ;; 181 182 *) 183 usage 184 ;; 185 esac 186done 187 188