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