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 20d5bfb6b3SYann 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 77d5bfb6b3SYann E. MORINundef_var() { 78d5bfb6b3SYann E. MORIN local name=$1 79d5bfb6b3SYann E. MORIN 80d5bfb6b3SYann E. MORIN sed -ri "/^($name=|# $name is not set)/d" "$FN" 81d5bfb6b3SYann E. MORIN} 82d5bfb6b3SYann 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 continue 1054edc7e32SYann E. MORIN ;; 10647312d2cSMichal Marek --refresh) 10747312d2cSMichal Marek ;; 108*57a9c760SClement Chauplannaz --*-after|-E|-D|-M) 1098e54701eSAndi Kleen checkarg "$1" 11047312d2cSMichal Marek A=$ARG 11147312d2cSMichal Marek checkarg "$2" 11247312d2cSMichal Marek B=$ARG 11347312d2cSMichal Marek shift 2 11447312d2cSMichal Marek ;; 11545f53cc9SAndi Kleen -*) 11647312d2cSMichal Marek checkarg "$1" 1178e54701eSAndi Kleen shift 1188e54701eSAndi Kleen ;; 11947312d2cSMichal Marek esac 12047312d2cSMichal Marek case "$CMD" in 12147312d2cSMichal Marek --enable|-e) 122f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y" 12347312d2cSMichal Marek ;; 1248e54701eSAndi Kleen 1258e54701eSAndi Kleen --disable|-d) 126f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set" 1278e54701eSAndi Kleen ;; 1288e54701eSAndi Kleen 1298e54701eSAndi Kleen --module|-m) 130f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m" 1318e54701eSAndi Kleen ;; 1328e54701eSAndi Kleen 1331f990cf9SMichal Marek --set-str) 134d6686da8SYann E. MORIN # sed swallows one level of escaping, so we need double-escaping 135f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\"" 1361f990cf9SMichal Marek shift 1371f990cf9SMichal Marek ;; 1381f990cf9SMichal Marek 139f0a6332cSJonas Aaberg --set-val) 140f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1" 141f0a6332cSJonas Aaberg shift 142f0a6332cSJonas Aaberg ;; 143d5bfb6b3SYann E. MORIN --undefine|-u) 144d5bfb6b3SYann E. MORIN undef_var "${CONFIG_}$ARG" 145d5bfb6b3SYann E. MORIN ;; 146f0a6332cSJonas Aaberg 1478e54701eSAndi Kleen --state|-s) 148f5ef2f7bSYann E. MORIN if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then 1498e54701eSAndi Kleen echo n 1508e54701eSAndi Kleen else 151f5ef2f7bSYann E. MORIN V="$(grep "^${CONFIG_}$ARG=" $FN)" 1528e54701eSAndi Kleen if [ $? != 0 ] ; then 1538e54701eSAndi Kleen echo undef 1548e54701eSAndi Kleen else 155f5ef2f7bSYann E. MORIN V="${V/#${CONFIG_}$ARG=/}" 156d6686da8SYann E. MORIN V="${V/#\"/}" 157d6686da8SYann E. MORIN V="${V/%\"/}" 1581925a276SYann E. MORIN V="${V//\\\"/\"}" 159d6686da8SYann E. MORIN echo "${V}" 1608e54701eSAndi Kleen fi 1618e54701eSAndi Kleen fi 1628e54701eSAndi Kleen ;; 1638e54701eSAndi Kleen 1648e54701eSAndi Kleen --enable-after|-E) 165f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A" 1668e54701eSAndi Kleen ;; 1678e54701eSAndi Kleen 1688e54701eSAndi Kleen --disable-after|-D) 169f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A" 1708e54701eSAndi Kleen ;; 1718e54701eSAndi Kleen 1728e54701eSAndi Kleen --module-after|-M) 173f5ef2f7bSYann E. MORIN set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A" 1748e54701eSAndi Kleen ;; 1758e54701eSAndi Kleen 1768e54701eSAndi Kleen # undocumented because it ignores --file (fixme) 1778e54701eSAndi Kleen --refresh) 1788e54701eSAndi Kleen yes "" | make oldconfig 1798e54701eSAndi Kleen ;; 1808e54701eSAndi Kleen 1818e54701eSAndi Kleen *) 1828e54701eSAndi Kleen usage 1838e54701eSAndi Kleen ;; 1848e54701eSAndi Kleen esac 1858e54701eSAndi Kleendone 1868e54701eSAndi Kleen 187