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