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