1c25ce589SFinn Behrens#!/usr/bin/env bash 2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 38e54701eSAndi Kleen# Manipulate options in a .config file from the command line 48e54701eSAndi Kleen 573877785SClement Chauplannazmyname=${0##*/} 673877785SClement Chauplannaz 7f5ef2f7bSYann E. MORIN# If no prefix forced, use the default CONFIG_ 8f5ef2f7bSYann E. MORINCONFIG_="${CONFIG_-CONFIG_}" 9f5ef2f7bSYann E. MORIN 10e461bc9fSJeremie Francois (on alpha)# We use an uncommon delimiter for sed substitutions 11e461bc9fSJeremie Francois (on alpha)SED_DELIM=$(echo -en "\001") 12e461bc9fSJeremie Francois (on alpha) 138e54701eSAndi Kleenusage() { 148e54701eSAndi Kleen cat >&2 <<EOL 158e54701eSAndi KleenManipulate options in a .config file from the command line. 168e54701eSAndi KleenUsage: 1773877785SClement Chauplannaz$myname options command ... 188e54701eSAndi Kleencommands: 198e54701eSAndi Kleen --enable|-e option Enable option 208e54701eSAndi Kleen --disable|-d option Disable option 218e54701eSAndi Kleen --module|-m option Turn option into a module 22f0a6332cSJonas Aaberg --set-str option string 23f0a6332cSJonas Aaberg Set option to "string" 24f0a6332cSJonas Aaberg --set-val option value 25f0a6332cSJonas Aaberg Set option to value 26d5bfb6b3SYann E. MORIN --undefine|-u option Undefine option 278e54701eSAndi Kleen --state|-s option Print state of option (n,y,m,undef) 288e54701eSAndi Kleen 298e54701eSAndi Kleen --enable-after|-E beforeopt option 308e54701eSAndi Kleen Enable option directly after other option 318e54701eSAndi Kleen --disable-after|-D beforeopt option 328e54701eSAndi Kleen Disable option directly after other option 338e54701eSAndi Kleen --module-after|-M beforeopt option 348e54701eSAndi Kleen Turn option into module directly after other option 358e54701eSAndi Kleen 368e54701eSAndi Kleen commands can be repeated multiple times 378e54701eSAndi Kleen 388e54701eSAndi Kleenoptions: 394edc7e32SYann E. MORIN --file config-file .config file to change (default .config) 404edc7e32SYann E. MORIN --keep-case|-k Keep next symbols' case (dont' upper-case it) 418e54701eSAndi Kleen 4273877785SClement Chauplannaz$myname doesn't check the validity of the .config file. This is done at next 438e54701eSAndi Kleenmake time. 444edc7e32SYann E. MORIN 4573877785SClement ChauplannazBy default, $myname will upper-case the given symbol. Use --keep-case to keep 464edc7e32SYann E. MORINthe case of all following symbols unchanged. 47f5ef2f7bSYann E. MORIN 4873877785SClement Chauplannaz$myname uses 'CONFIG_' as the default symbol prefix. Set the environment 4973877785SClement Chauplannazvariable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ... 508e54701eSAndi KleenEOL 518e54701eSAndi Kleen exit 1 528e54701eSAndi Kleen} 538e54701eSAndi Kleen 548e54701eSAndi Kleencheckarg() { 558e54701eSAndi Kleen ARG="$1" 568e54701eSAndi Kleen if [ "$ARG" = "" ] ; then 578e54701eSAndi Kleen usage 588e54701eSAndi Kleen fi 598e54701eSAndi Kleen case "$ARG" in 60f5ef2f7bSYann E. MORIN ${CONFIG_}*) 61f5ef2f7bSYann E. MORIN ARG="${ARG/${CONFIG_}/}" 628e54701eSAndi Kleen ;; 638e54701eSAndi Kleen esac 644edc7e32SYann E. MORIN if [ "$MUNGE_CASE" = "yes" ] ; then 658e54701eSAndi Kleen ARG="`echo $ARG | tr a-z A-Z`" 664edc7e32SYann E. MORIN fi 678e54701eSAndi Kleen} 688e54701eSAndi Kleen 6983e8b90eSClement Chauplannaztxt_append() { 7083e8b90eSClement Chauplannaz local anchor="$1" 7183e8b90eSClement Chauplannaz local insert="$2" 7283e8b90eSClement Chauplannaz local infile="$3" 7383e8b90eSClement Chauplannaz local tmpfile="$infile.swp" 7483e8b90eSClement Chauplannaz 7583e8b90eSClement Chauplannaz # sed append cmd: 'a\' + newline + text + newline 7683e8b90eSClement Chauplannaz cmd="$(printf "a\\%b$insert" "\n")" 7783e8b90eSClement Chauplannaz 7883e8b90eSClement Chauplannaz sed -e "/$anchor/$cmd" "$infile" >"$tmpfile" 7983e8b90eSClement Chauplannaz # replace original file with the edited one 8083e8b90eSClement Chauplannaz mv "$tmpfile" "$infile" 8183e8b90eSClement Chauplannaz} 8283e8b90eSClement Chauplannaz 8383e8b90eSClement Chauplannaztxt_subst() { 8483e8b90eSClement Chauplannaz local before="$1" 8583e8b90eSClement Chauplannaz local after="$2" 8683e8b90eSClement Chauplannaz local infile="$3" 8783e8b90eSClement Chauplannaz local tmpfile="$infile.swp" 8883e8b90eSClement Chauplannaz 89e461bc9fSJeremie Francois (on alpha) sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile" 9083e8b90eSClement Chauplannaz # replace original file with the edited one 9183e8b90eSClement Chauplannaz mv "$tmpfile" "$infile" 9283e8b90eSClement Chauplannaz} 9383e8b90eSClement Chauplannaz 9483e8b90eSClement Chauplannaztxt_delete() { 9583e8b90eSClement Chauplannaz local text="$1" 9683e8b90eSClement Chauplannaz local infile="$2" 9783e8b90eSClement Chauplannaz local tmpfile="$infile.swp" 9883e8b90eSClement Chauplannaz 9983e8b90eSClement Chauplannaz sed -e "/$text/d" "$infile" >"$tmpfile" 10083e8b90eSClement Chauplannaz # replace original file with the edited one 10183e8b90eSClement Chauplannaz mv "$tmpfile" "$infile" 10283e8b90eSClement Chauplannaz} 10383e8b90eSClement Chauplannaz 10456643222SMichal Marekset_var() { 10556643222SMichal Marek local name=$1 new=$2 before=$3 10656643222SMichal Marek 10756643222SMichal Marek name_re="^($name=|# $name is not set)" 10856643222SMichal Marek before_re="^($before=|# $before is not set)" 10956643222SMichal Marek if test -n "$before" && grep -Eq "$before_re" "$FN"; then 11083e8b90eSClement Chauplannaz txt_append "^$before=" "$new" "$FN" 11183e8b90eSClement Chauplannaz txt_append "^# $before is not set" "$new" "$FN" 11256643222SMichal Marek elif grep -Eq "$name_re" "$FN"; then 11383e8b90eSClement Chauplannaz txt_subst "^$name=.*" "$new" "$FN" 11483e8b90eSClement Chauplannaz txt_subst "^# $name is not set" "$new" "$FN" 11556643222SMichal Marek else 11656643222SMichal Marek echo "$new" >>"$FN" 11756643222SMichal Marek fi 1188e54701eSAndi Kleen} 1198e54701eSAndi Kleen 120d5bfb6b3SYann E. MORINundef_var() { 121d5bfb6b3SYann E. MORIN local name=$1 122d5bfb6b3SYann E. MORIN 12383e8b90eSClement Chauplannaz txt_delete "^$name=" "$FN" 12483e8b90eSClement Chauplannaz txt_delete "^# $name is not set" "$FN" 125d5bfb6b3SYann E. MORIN} 126d5bfb6b3SYann E. MORIN 1278e54701eSAndi Kleenif [ "$1" = "--file" ]; then 1288e54701eSAndi Kleen FN="$2" 1298e54701eSAndi Kleen if [ "$FN" = "" ] ; then 1308e54701eSAndi Kleen usage 1318e54701eSAndi Kleen fi 13247312d2cSMichal Marek shift 2 1338e54701eSAndi Kleenelse 1348e54701eSAndi Kleen FN=.config 1358e54701eSAndi Kleenfi 1368e54701eSAndi Kleen 1372302e873SAndi Kleenif [ "$1" = "" ] ; then 1382302e873SAndi Kleen usage 1392302e873SAndi Kleenfi 1402302e873SAndi Kleen 1414edc7e32SYann E. MORINMUNGE_CASE=yes 1428e54701eSAndi Kleenwhile [ "$1" != "" ] ; do 1438e54701eSAndi Kleen CMD="$1" 1448e54701eSAndi Kleen shift 1458e54701eSAndi Kleen case "$CMD" in 1464edc7e32SYann E. MORIN --keep-case|-k) 1474edc7e32SYann E. MORIN MUNGE_CASE=no 1484edc7e32SYann E. MORIN continue 1494edc7e32SYann E. MORIN ;; 15047312d2cSMichal Marek --refresh) 15147312d2cSMichal Marek ;; 15257a9c760SClement Chauplannaz --*-after|-E|-D|-M) 1538e54701eSAndi Kleen checkarg "$1" 15447312d2cSMichal Marek A=$ARG 15547312d2cSMichal Marek checkarg "$2" 15647312d2cSMichal Marek B=$ARG 15747312d2cSMichal Marek shift 2 15847312d2cSMichal Marek ;; 15945f53cc9SAndi Kleen -*) 16047312d2cSMichal Marek checkarg "$1" 1618e54701eSAndi Kleen shift 1628e54701eSAndi Kleen ;; 16347312d2cSMichal Marek esac 16447312d2cSMichal Marek case "$CMD" in 16547312d2cSMichal Marek --enable|-e) 166f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" 16747312d2cSMichal Marek ;; 1688e54701eSAndi Kleen 1698e54701eSAndi Kleen --disable|-d) 170f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" 1718e54701eSAndi Kleen ;; 1728e54701eSAndi Kleen 1738e54701eSAndi Kleen --module|-m) 174f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" 1758e54701eSAndi Kleen ;; 1768e54701eSAndi Kleen 1771f990cf9SMichal Marek --set-str) 178d6686da8SYann E. MORIN # sed swallows one level of escaping, so we need double-escaping 179f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" 1801f990cf9SMichal Marek shift 1811f990cf9SMichal Marek ;; 1821f990cf9SMichal Marek 183f0a6332cSJonas Aaberg --set-val) 184f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" 185f0a6332cSJonas Aaberg shift 186f0a6332cSJonas Aaberg ;; 187d5bfb6b3SYann E. MORIN --undefine|-u) 188d5bfb6b3SYann E. MORIN undef_var "${CONFIG_}$ARG" 189d5bfb6b3SYann E. MORIN ;; 190f0a6332cSJonas Aaberg 1918e54701eSAndi Kleen --state|-s) 192f5ef2f7bSYann E. MORIN if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then 1938e54701eSAndi Kleen echo n 1948e54701eSAndi Kleen else 195f5ef2f7bSYann E. MORIN V="$(grep "^${CONFIG_}$ARG=" $FN)" 1968e54701eSAndi Kleen if [ $? != 0 ] ; then 1978e54701eSAndi Kleen echo undef 1988e54701eSAndi Kleen else 199f5ef2f7bSYann E. MORIN V="${V/#${CONFIG_}$ARG=/}" 200d6686da8SYann E. MORIN V="${V/#\"/}" 201d6686da8SYann E. MORIN V="${V/%\"/}" 2021925a276SYann E. MORIN V="${V//\\\"/\"}" 203d6686da8SYann E. MORIN echo "${V}" 2048e54701eSAndi Kleen fi 2058e54701eSAndi Kleen fi 2068e54701eSAndi Kleen ;; 2078e54701eSAndi Kleen 2088e54701eSAndi Kleen --enable-after|-E) 209f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" 2108e54701eSAndi Kleen ;; 2118e54701eSAndi Kleen 2128e54701eSAndi Kleen --disable-after|-D) 213f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" 2148e54701eSAndi Kleen ;; 2158e54701eSAndi Kleen 2168e54701eSAndi Kleen --module-after|-M) 217f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" 2188e54701eSAndi Kleen ;; 2198e54701eSAndi Kleen 2208e54701eSAndi Kleen # undocumented because it ignores --file (fixme) 2218e54701eSAndi Kleen --refresh) 2228e54701eSAndi Kleen yes "" | make oldconfig 2238e54701eSAndi Kleen ;; 2248e54701eSAndi Kleen 2258e54701eSAndi Kleen *) 226*d39648ebSRandy Dunlap echo "bad command: $CMD" >&2 2278e54701eSAndi Kleen usage 2288e54701eSAndi Kleen ;; 2298e54701eSAndi Kleen esac 2308e54701eSAndi Kleendone 231