1 #!/usr/bin/env bash !! 1 #!/bin/bash 2 # SPDX-License-Identifier: GPL-2.0 << 3 # Manipulate options in a .config file from th 2 # Manipulate options in a .config file from the command line 4 3 5 myname=${0##*/} 4 myname=${0##*/} 6 5 7 # If no prefix forced, use the default CONFIG_ 6 # If no prefix forced, use the default CONFIG_ 8 CONFIG_="${CONFIG_-CONFIG_}" 7 CONFIG_="${CONFIG_-CONFIG_}" 9 8 10 # We use an uncommon delimiter for sed substit 9 # We use an uncommon delimiter for sed substitutions 11 SED_DELIM=$(echo -en "\001") 10 SED_DELIM=$(echo -en "\001") 12 11 13 usage() { 12 usage() { 14 cat >&2 <<EOL 13 cat >&2 <<EOL 15 Manipulate options in a .config file from the 14 Manipulate options in a .config file from the command line. 16 Usage: 15 Usage: 17 $myname options command ... 16 $myname options command ... 18 commands: 17 commands: 19 --enable|-e option Enable option 18 --enable|-e option Enable option 20 --disable|-d option Disable option 19 --disable|-d option Disable option 21 --module|-m option Turn option into 20 --module|-m option Turn option into a module 22 --set-str option string 21 --set-str option string 23 Set option to "st 22 Set option to "string" 24 --set-val option value 23 --set-val option value 25 Set option to val 24 Set option to value 26 --undefine|-u option Undefine option 25 --undefine|-u option Undefine option 27 --state|-s option Print state of op 26 --state|-s option Print state of option (n,y,m,undef) 28 27 29 --enable-after|-E beforeopt option 28 --enable-after|-E beforeopt option 30 Enable option dir 29 Enable option directly after other option 31 --disable-after|-D beforeopt option 30 --disable-after|-D beforeopt option 32 Disable option di 31 Disable option directly after other option 33 --module-after|-M beforeopt option 32 --module-after|-M beforeopt option 34 Turn option into 33 Turn option into module directly after other option 35 34 36 commands can be repeated multiple time 35 commands can be repeated multiple times 37 36 38 options: 37 options: 39 --file config-file .config file to c 38 --file config-file .config file to change (default .config) 40 --keep-case|-k Keep next symbols 39 --keep-case|-k Keep next symbols' case (dont' upper-case it) 41 40 42 $myname doesn't check the validity of the .con 41 $myname doesn't check the validity of the .config file. This is done at next 43 make time. 42 make time. 44 43 45 By default, $myname will upper-case the given 44 By default, $myname will upper-case the given symbol. Use --keep-case to keep 46 the case of all following symbols unchanged. 45 the case of all following symbols unchanged. 47 46 48 $myname uses 'CONFIG_' as the default symbol p 47 $myname uses 'CONFIG_' as the default symbol prefix. Set the environment 49 variable CONFIG_ to the prefix to use. Eg.: CO 48 variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ... 50 EOL 49 EOL 51 exit 1 50 exit 1 52 } 51 } 53 52 54 checkarg() { 53 checkarg() { 55 ARG="$1" 54 ARG="$1" 56 if [ "$ARG" = "" ] ; then 55 if [ "$ARG" = "" ] ; then 57 usage 56 usage 58 fi 57 fi 59 case "$ARG" in 58 case "$ARG" in 60 ${CONFIG_}*) 59 ${CONFIG_}*) 61 ARG="${ARG/${CONFIG_}/}" 60 ARG="${ARG/${CONFIG_}/}" 62 ;; 61 ;; 63 esac 62 esac 64 if [ "$MUNGE_CASE" = "yes" ] ; then 63 if [ "$MUNGE_CASE" = "yes" ] ; then 65 ARG="`echo $ARG | tr a-z A-Z`" 64 ARG="`echo $ARG | tr a-z A-Z`" 66 fi 65 fi 67 } 66 } 68 67 69 txt_append() { 68 txt_append() { 70 local anchor="$1" 69 local anchor="$1" 71 local insert="$2" 70 local insert="$2" 72 local infile="$3" 71 local infile="$3" 73 local tmpfile="$infile.swp" 72 local tmpfile="$infile.swp" 74 73 75 # sed append cmd: 'a\' + newline + tex 74 # sed append cmd: 'a\' + newline + text + newline 76 cmd="$(printf "a\\%b$insert" "\n")" 75 cmd="$(printf "a\\%b$insert" "\n")" 77 76 78 sed -e "/$anchor/$cmd" "$infile" >"$tm 77 sed -e "/$anchor/$cmd" "$infile" >"$tmpfile" 79 # replace original file with the edite 78 # replace original file with the edited one 80 mv "$tmpfile" "$infile" 79 mv "$tmpfile" "$infile" 81 } 80 } 82 81 83 txt_subst() { 82 txt_subst() { 84 local before="$1" 83 local before="$1" 85 local after="$2" 84 local after="$2" 86 local infile="$3" 85 local infile="$3" 87 local tmpfile="$infile.swp" 86 local tmpfile="$infile.swp" 88 87 89 sed -e "s$SED_DELIM$before$SED_DELIM$a 88 sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile" 90 # replace original file with the edite 89 # replace original file with the edited one 91 mv "$tmpfile" "$infile" 90 mv "$tmpfile" "$infile" 92 } 91 } 93 92 94 txt_delete() { 93 txt_delete() { 95 local text="$1" 94 local text="$1" 96 local infile="$2" 95 local infile="$2" 97 local tmpfile="$infile.swp" 96 local tmpfile="$infile.swp" 98 97 99 sed -e "/$text/d" "$infile" >"$tmpfile 98 sed -e "/$text/d" "$infile" >"$tmpfile" 100 # replace original file with the edite 99 # replace original file with the edited one 101 mv "$tmpfile" "$infile" 100 mv "$tmpfile" "$infile" 102 } 101 } 103 102 104 set_var() { 103 set_var() { 105 local name=$1 new=$2 before=$3 104 local name=$1 new=$2 before=$3 106 105 107 name_re="^($name=|# $name is not set)" 106 name_re="^($name=|# $name is not set)" 108 before_re="^($before=|# $before is not 107 before_re="^($before=|# $before is not set)" 109 if test -n "$before" && grep -Eq "$bef 108 if test -n "$before" && grep -Eq "$before_re" "$FN"; then 110 txt_append "^$before=" "$new" 109 txt_append "^$before=" "$new" "$FN" 111 txt_append "^# $before is not 110 txt_append "^# $before is not set" "$new" "$FN" 112 elif grep -Eq "$name_re" "$FN"; then 111 elif grep -Eq "$name_re" "$FN"; then 113 txt_subst "^$name=.*" "$new" " 112 txt_subst "^$name=.*" "$new" "$FN" 114 txt_subst "^# $name is not set 113 txt_subst "^# $name is not set" "$new" "$FN" 115 else 114 else 116 echo "$new" >>"$FN" 115 echo "$new" >>"$FN" 117 fi 116 fi 118 } 117 } 119 118 120 undef_var() { 119 undef_var() { 121 local name=$1 120 local name=$1 122 121 123 txt_delete "^$name=" "$FN" 122 txt_delete "^$name=" "$FN" 124 txt_delete "^# $name is not set" "$FN" 123 txt_delete "^# $name is not set" "$FN" 125 } 124 } 126 125 127 if [ "$1" = "--file" ]; then 126 if [ "$1" = "--file" ]; then 128 FN="$2" 127 FN="$2" 129 if [ "$FN" = "" ] ; then 128 if [ "$FN" = "" ] ; then 130 usage 129 usage 131 fi 130 fi 132 shift 2 131 shift 2 133 else 132 else 134 FN=.config 133 FN=.config 135 fi 134 fi 136 135 137 if [ "$1" = "" ] ; then 136 if [ "$1" = "" ] ; then 138 usage 137 usage 139 fi 138 fi 140 139 141 MUNGE_CASE=yes 140 MUNGE_CASE=yes 142 while [ "$1" != "" ] ; do 141 while [ "$1" != "" ] ; do 143 CMD="$1" 142 CMD="$1" 144 shift 143 shift 145 case "$CMD" in 144 case "$CMD" in 146 --keep-case|-k) 145 --keep-case|-k) 147 MUNGE_CASE=no 146 MUNGE_CASE=no 148 continue 147 continue 149 ;; 148 ;; 150 --refresh) 149 --refresh) 151 ;; 150 ;; 152 --*-after|-E|-D|-M) 151 --*-after|-E|-D|-M) 153 checkarg "$1" 152 checkarg "$1" 154 A=$ARG 153 A=$ARG 155 checkarg "$2" 154 checkarg "$2" 156 B=$ARG 155 B=$ARG 157 shift 2 156 shift 2 158 ;; 157 ;; 159 -*) 158 -*) 160 checkarg "$1" 159 checkarg "$1" 161 shift 160 shift 162 ;; 161 ;; 163 esac 162 esac 164 case "$CMD" in 163 case "$CMD" in 165 --enable|-e) 164 --enable|-e) 166 set_var "${CONFIG_}$ARG" "${CO 165 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" 167 ;; 166 ;; 168 167 169 --disable|-d) 168 --disable|-d) 170 set_var "${CONFIG_}$ARG" "# ${ 169 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" 171 ;; 170 ;; 172 171 173 --module|-m) 172 --module|-m) 174 set_var "${CONFIG_}$ARG" "${CO 173 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" 175 ;; 174 ;; 176 175 177 --set-str) 176 --set-str) 178 # sed swallows one level of es 177 # sed swallows one level of escaping, so we need double-escaping 179 set_var "${CONFIG_}$ARG" "${CO 178 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" 180 shift 179 shift 181 ;; 180 ;; 182 181 183 --set-val) 182 --set-val) 184 set_var "${CONFIG_}$ARG" "${CO 183 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" 185 shift 184 shift 186 ;; 185 ;; 187 --undefine|-u) 186 --undefine|-u) 188 undef_var "${CONFIG_}$ARG" 187 undef_var "${CONFIG_}$ARG" 189 ;; 188 ;; 190 189 191 --state|-s) 190 --state|-s) 192 if grep -q "# ${CONFIG_}$ARG i 191 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then 193 echo n 192 echo n 194 else 193 else 195 V="$(grep "^${CONFIG_} 194 V="$(grep "^${CONFIG_}$ARG=" $FN)" 196 if [ $? != 0 ] ; then 195 if [ $? != 0 ] ; then 197 echo undef 196 echo undef 198 else 197 else 199 V="${V/#${CONF 198 V="${V/#${CONFIG_}$ARG=/}" 200 V="${V/#\"/}" 199 V="${V/#\"/}" 201 V="${V/%\"/}" 200 V="${V/%\"/}" 202 V="${V//\\\"/\ 201 V="${V//\\\"/\"}" 203 echo "${V}" 202 echo "${V}" 204 fi 203 fi 205 fi 204 fi 206 ;; 205 ;; 207 206 208 --enable-after|-E) 207 --enable-after|-E) 209 set_var "${CONFIG_}$B" "${CONF 208 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" 210 ;; 209 ;; 211 210 212 --disable-after|-D) 211 --disable-after|-D) 213 set_var "${CONFIG_}$B" "# ${CO 212 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" 214 ;; 213 ;; 215 214 216 --module-after|-M) 215 --module-after|-M) 217 set_var "${CONFIG_}$B" "${CONF 216 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" 218 ;; 217 ;; 219 218 220 # undocumented because it ignores --fi 219 # undocumented because it ignores --file (fixme) 221 --refresh) 220 --refresh) 222 yes "" | make oldconfig 221 yes "" | make oldconfig 223 ;; 222 ;; 224 223 225 *) 224 *) 226 echo "bad command: $CMD" >&2 << 227 usage 225 usage 228 ;; 226 ;; 229 esac 227 esac 230 done 228 done
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.