1#!/bin/bash 2# Manipulate options in a .config file from the command line 3 4myname=${0##*/} 5 6# If no prefix forced, use the default BR2_ 7BR2_PREFIX="${BR2_PREFIX-BR2_}" 8 9usage() { 10 cat >&2 <<EOL 11Manipulate options in a .config file from the command line. 12Usage: 13$myname options command ... 14commands: 15 --enable|-e option Enable option 16 --disable|-d option Disable option 17 --set-str option string 18 Set option to "string" 19 --set-val option value 20 Set option to value 21 --undefine|-u option Undefine option 22 --state|-s option Print state of option (n,y,m,undef) 23 24 --enable-after|-E beforeopt option 25 Enable option directly after other option 26 --disable-after|-D beforeopt option 27 Disable option 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 --package|-p Operate on package (set prefix to BR2_PACKAGE_) 35 36$myname doesn't check the validity of the .config file. This is done at next 37make time. 38 39By default, $myname will upper-case the given symbol. Use --keep-case to keep 40the case of all following symbols unchanged. 41 42$myname uses 'BR2_' as the default symbol prefix. Set the environment 43variable BR2_PREFIX to the prefix to use. Eg.: BR2_PREFIX="FOO_" $myname ... 44EOL 45 exit 1 46} 47 48checkarg() { 49 ARG="$1" 50 if [ "$ARG" = "" ] ; then 51 usage 52 fi 53 if [ "$MUNGE_CASE" = "yes" ] ; then 54 ARG="`echo $ARG | tr a-z- A-Z_`" 55 fi 56 case "$ARG" in 57 ${BR2_PREFIX}*) 58 ARG="${ARG/${BR2_PREFIX}/}" 59 ;; 60 esac 61} 62 63txt_append() { 64 local anchor="$1" 65 local insert="$2" 66 local infile="$3" 67 local tmpfile="$infile.swp" 68 69 # sed append cmd: 'a\' + newline + text + newline 70 cmd="$(printf "a\\%b$insert" "\n")" 71 72 sed -i -e "/$anchor/$cmd" "$infile" 73} 74 75txt_subst() { 76 local before="$1" 77 local after="$2" 78 local infile="$3" 79 local tmpfile="$infile.swp" 80 81 sed -i -e "s:$before:$after:" "$infile" 82} 83 84txt_delete() { 85 local text="$1" 86 local infile="$2" 87 local tmpfile="$infile.swp" 88 89 sed -i -e "/$text/d" "$infile" 90} 91 92set_var() { 93 local name=$1 new=$2 before=$3 94 95 name_re="^($name=|# $name is not set)" 96 before_re="^($before=|# $before is not set)" 97 if test -n "$before" && grep -Eq "$before_re" "$FN"; then 98 txt_append "^$before=" "$new" "$FN" 99 txt_append "^# $before is not set" "$new" "$FN" 100 elif grep -Eq "$name_re" "$FN"; then 101 txt_subst "^$name=.*" "$new" "$FN" 102 txt_subst "^# $name is not set" "$new" "$FN" 103 else 104 echo "$new" >>"$FN" 105 fi 106} 107 108undef_var() { 109 local name=$1 110 111 txt_delete "^$name=" "$FN" 112 txt_delete "^# $name is not set" "$FN" 113} 114 115if [ "$1" = "--file" ]; then 116 FN="$2" 117 if [ "$FN" = "" ] ; then 118 usage 119 fi 120 shift 2 121else 122 FN=.config 123fi 124 125if [ "$1" = "" ] ; then 126 usage 127fi 128 129MUNGE_CASE=yes 130while [ "$1" != "" ] ; do 131 CMD="$1" 132 shift 133 case "$CMD" in 134 --keep-case|-k) 135 MUNGE_CASE=no 136 continue 137 ;; 138 --package|-p) 139 BR2_PREFIX="BR2_PACKAGE_" 140 continue 141 ;; 142 --*-after|-E|-D|-M) 143 checkarg "$1" 144 A=$ARG 145 checkarg "$2" 146 B=$ARG 147 shift 2 148 ;; 149 -*) 150 checkarg "$1" 151 shift 152 ;; 153 esac 154 case "$CMD" in 155 --enable|-e) 156 set_var "${BR2_PREFIX}$ARG" "${BR2_PREFIX}$ARG=y" 157 ;; 158 159 --disable|-d) 160 set_var "${BR2_PREFIX}$ARG" "# ${BR2_PREFIX}$ARG is not set" 161 ;; 162 163 --set-str) 164 # sed swallows one level of escaping, so we need double-escaping 165 set_var "${BR2_PREFIX}$ARG" "${BR2_PREFIX}$ARG=\"${1//\"/\\\\\"}\"" 166 shift 167 ;; 168 169 --set-val) 170 set_var "${BR2_PREFIX}$ARG" "${BR2_PREFIX}$ARG=$1" 171 shift 172 ;; 173 --undefine|-u) 174 undef_var "${BR2_PREFIX}$ARG" 175 ;; 176 177 --state|-s) 178 if grep -q "# ${BR2_PREFIX}$ARG is not set" $FN ; then 179 echo n 180 else 181 V="$(grep "^${BR2_PREFIX}$ARG=" $FN)" 182 if [ $? != 0 ] ; then 183 echo undef 184 else 185 V="${V/#${BR2_PREFIX}$ARG=/}" 186 V="${V/#\"/}" 187 V="${V/%\"/}" 188 V="${V//\\\"/\"}" 189 echo "${V}" 190 fi 191 fi 192 ;; 193 194 --enable-after|-E) 195 set_var "${BR2_PREFIX}$B" "${BR2_PREFIX}$B=y" "${BR2_PREFIX}$A" 196 ;; 197 198 --disable-after|-D) 199 set_var "${BR2_PREFIX}$B" "# ${BR2_PREFIX}$B is not set" "${BR2_PREFIX}$A" 200 ;; 201 202 *) 203 usage 204 ;; 205 esac 206done 207