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