18e54701eSAndi Kleen#!/bin/bash 28e54701eSAndi Kleen# Manipulate options in a .config file from the command line 38e54701eSAndi Kleen 4f5ef2f7bSYann E. MORIN# If no prefix forced, use the default CONFIG_ 5f5ef2f7bSYann E. MORINCONFIG_="${CONFIG_-CONFIG_}" 6f5ef2f7bSYann E. MORIN 78e54701eSAndi Kleenusage() { 88e54701eSAndi Kleen cat >&2 <<EOL 98e54701eSAndi KleenManipulate options in a .config file from the command line. 108e54701eSAndi KleenUsage: 118e54701eSAndi Kleenconfig options command ... 128e54701eSAndi Kleencommands: 138e54701eSAndi Kleen --enable|-e option Enable option 148e54701eSAndi Kleen --disable|-d option Disable option 158e54701eSAndi Kleen --module|-m option Turn option into a module 16f0a6332cSJonas Aaberg --set-str option string 17f0a6332cSJonas Aaberg Set option to "string" 18f0a6332cSJonas Aaberg --set-val option value 19f0a6332cSJonas Aaberg Set option to value 20*d5bfb6b3SYann E. MORIN --undefine|-u option Undefine option 218e54701eSAndi Kleen --state|-s option Print state of option (n,y,m,undef) 228e54701eSAndi Kleen 238e54701eSAndi Kleen --enable-after|-E beforeopt option 248e54701eSAndi Kleen Enable option directly after other option 258e54701eSAndi Kleen --disable-after|-D beforeopt option 268e54701eSAndi Kleen Disable option directly after other option 278e54701eSAndi Kleen --module-after|-M beforeopt option 288e54701eSAndi Kleen Turn option into module directly after other option 298e54701eSAndi Kleen 308e54701eSAndi Kleen commands can be repeated multiple times 318e54701eSAndi Kleen 328e54701eSAndi Kleenoptions: 334edc7e32SYann E. MORIN --file config-file .config file to change (default .config) 344edc7e32SYann E. MORIN --keep-case|-k Keep next symbols' case (dont' upper-case it) 358e54701eSAndi Kleen 368e54701eSAndi Kleenconfig doesn't check the validity of the .config file. This is done at next 378e54701eSAndi Kleenmake time. 384edc7e32SYann E. MORIN 394edc7e32SYann E. MORINBy default, config will upper-case the given symbol. Use --keep-case to keep 404edc7e32SYann E. MORINthe case of all following symbols unchanged. 41f5ef2f7bSYann E. MORIN 42f5ef2f7bSYann E. MORINconfig uses 'CONFIG_' as the default symbol prefix. Set the environment 43f5ef2f7bSYann E. MORINvariable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ... 448e54701eSAndi KleenEOL 458e54701eSAndi Kleen exit 1 468e54701eSAndi Kleen} 478e54701eSAndi Kleen 488e54701eSAndi Kleencheckarg() { 498e54701eSAndi Kleen ARG="$1" 508e54701eSAndi Kleen if [ "$ARG" = "" ] ; then 518e54701eSAndi Kleen usage 528e54701eSAndi Kleen fi 538e54701eSAndi Kleen case "$ARG" in 54f5ef2f7bSYann E. MORIN ${CONFIG_}*) 55f5ef2f7bSYann E. MORIN ARG="${ARG/${CONFIG_}/}" 568e54701eSAndi Kleen ;; 578e54701eSAndi Kleen esac 584edc7e32SYann E. MORIN if [ "$MUNGE_CASE" = "yes" ] ; then 598e54701eSAndi Kleen ARG="`echo $ARG | tr a-z A-Z`" 604edc7e32SYann E. MORIN fi 618e54701eSAndi Kleen} 628e54701eSAndi Kleen 6356643222SMichal Marekset_var() { 6456643222SMichal Marek local name=$1 new=$2 before=$3 6556643222SMichal Marek 6656643222SMichal Marek name_re="^($name=|# $name is not set)" 6756643222SMichal Marek before_re="^($before=|# $before is not set)" 6856643222SMichal Marek if test -n "$before" && grep -Eq "$before_re" "$FN"; then 6956643222SMichal Marek sed -ri "/$before_re/a $new" "$FN" 7056643222SMichal Marek elif grep -Eq "$name_re" "$FN"; then 7156643222SMichal Marek sed -ri "s:$name_re.*:$new:" "$FN" 7256643222SMichal Marek else 7356643222SMichal Marek echo "$new" >>"$FN" 7456643222SMichal Marek fi 758e54701eSAndi Kleen} 768e54701eSAndi Kleen 77*d5bfb6b3SYann E. MORINundef_var() { 78*d5bfb6b3SYann E. MORIN local name=$1 79*d5bfb6b3SYann E. MORIN 80*d5bfb6b3SYann E. MORIN sed -ri "/^($name=|# $name is not set)/d" "$FN" 81*d5bfb6b3SYann E. MORIN} 82*d5bfb6b3SYann E. MORIN 838e54701eSAndi Kleenif [ "$1" = "--file" ]; then 848e54701eSAndi Kleen FN="$2" 858e54701eSAndi Kleen if [ "$FN" = "" ] ; then 868e54701eSAndi Kleen usage 878e54701eSAndi Kleen fi 8847312d2cSMichal Marek shift 2 898e54701eSAndi Kleenelse 908e54701eSAndi Kleen FN=.config 918e54701eSAndi Kleenfi 928e54701eSAndi Kleen 932302e873SAndi Kleenif [ "$1" = "" ] ; then 942302e873SAndi Kleen usage 952302e873SAndi Kleenfi 962302e873SAndi Kleen 974edc7e32SYann E. MORINMUNGE_CASE=yes 988e54701eSAndi Kleenwhile [ "$1" != "" ] ; do 998e54701eSAndi Kleen CMD="$1" 1008e54701eSAndi Kleen shift 1018e54701eSAndi Kleen case "$CMD" in 1024edc7e32SYann E. MORIN --keep-case|-k) 1034edc7e32SYann E. MORIN MUNGE_CASE=no 1044edc7e32SYann E. MORIN shift 1054edc7e32SYann E. MORIN continue 1064edc7e32SYann E. MORIN ;; 10747312d2cSMichal Marek --refresh) 10847312d2cSMichal Marek ;; 10947312d2cSMichal Marek --*-after) 1108e54701eSAndi Kleen checkarg "$1" 11147312d2cSMichal Marek A=$ARG 11247312d2cSMichal Marek checkarg "$2" 11347312d2cSMichal Marek B=$ARG 11447312d2cSMichal Marek shift 2 11547312d2cSMichal Marek ;; 11645f53cc9SAndi Kleen -*) 11747312d2cSMichal Marek checkarg "$1" 1188e54701eSAndi Kleen shift 1198e54701eSAndi Kleen ;; 12047312d2cSMichal Marek esac 12147312d2cSMichal Marek case "$CMD" in 12247312d2cSMichal Marek --enable|-e) 123f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" 12447312d2cSMichal Marek ;; 1258e54701eSAndi Kleen 1268e54701eSAndi Kleen --disable|-d) 127f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" 1288e54701eSAndi Kleen ;; 1298e54701eSAndi Kleen 1308e54701eSAndi Kleen --module|-m) 131f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" 1328e54701eSAndi Kleen ;; 1338e54701eSAndi Kleen 1341f990cf9SMichal Marek --set-str) 135d6686da8SYann E. MORIN # sed swallows one level of escaping, so we need double-escaping 136f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" 1371f990cf9SMichal Marek shift 1381f990cf9SMichal Marek ;; 1391f990cf9SMichal Marek 140f0a6332cSJonas Aaberg --set-val) 141f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" 142f0a6332cSJonas Aaberg shift 143f0a6332cSJonas Aaberg ;; 144*d5bfb6b3SYann E. MORIN --undefine|-u) 145*d5bfb6b3SYann E. MORIN undef_var "${CONFIG_}$ARG" 146*d5bfb6b3SYann E. MORIN ;; 147f0a6332cSJonas Aaberg 1488e54701eSAndi Kleen --state|-s) 149f5ef2f7bSYann E. MORIN if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then 1508e54701eSAndi Kleen echo n 1518e54701eSAndi Kleen else 152f5ef2f7bSYann E. MORIN V="$(grep "^${CONFIG_}$ARG=" $FN)" 1538e54701eSAndi Kleen if [ $? != 0 ] ; then 1548e54701eSAndi Kleen echo undef 1558e54701eSAndi Kleen else 156f5ef2f7bSYann E. MORIN V="${V/#${CONFIG_}$ARG=/}" 157d6686da8SYann E. MORIN V="${V/#\"/}" 158d6686da8SYann E. MORIN V="${V/%\"/}" 159d6686da8SYann E. MORIN V="${V/\\\"/\"}" 160d6686da8SYann E. MORIN echo "${V}" 1618e54701eSAndi Kleen fi 1628e54701eSAndi Kleen fi 1638e54701eSAndi Kleen ;; 1648e54701eSAndi Kleen 1658e54701eSAndi Kleen --enable-after|-E) 166f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" 1678e54701eSAndi Kleen ;; 1688e54701eSAndi Kleen 1698e54701eSAndi Kleen --disable-after|-D) 170f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" 1718e54701eSAndi Kleen ;; 1728e54701eSAndi Kleen 1738e54701eSAndi Kleen --module-after|-M) 174f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" 1758e54701eSAndi Kleen ;; 1768e54701eSAndi Kleen 1778e54701eSAndi Kleen # undocumented because it ignores --file (fixme) 1788e54701eSAndi Kleen --refresh) 1798e54701eSAndi Kleen yes "" | make oldconfig 1808e54701eSAndi Kleen ;; 1818e54701eSAndi Kleen 1828e54701eSAndi Kleen *) 1838e54701eSAndi Kleen usage 1848e54701eSAndi Kleen ;; 1858e54701eSAndi Kleen esac 1868e54701eSAndi Kleendone 1878e54701eSAndi Kleen 188