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