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