Back to home page

LXR

 
 

    


0001 #!/bin/bash
0002 # Manipulate options in a .config file from the command line
0003 
0004 myname=${0##*/}
0005 
0006 # If no prefix forced, use the default CONFIG_
0007 CONFIG_="${CONFIG_-CONFIG_}"
0008 
0009 usage() {
0010         cat >&2 <<EOL
0011 Manipulate options in a .config file from the command line.
0012 Usage:
0013 $myname options command ...
0014 commands:
0015         --enable|-e option   Enable option
0016         --disable|-d option  Disable option
0017         --module|-m option   Turn option into a module
0018         --set-str option string
0019                              Set option to "string"
0020         --set-val option value
0021                              Set option to value
0022         --undefine|-u option Undefine option
0023         --state|-s option    Print state of option (n,y,m,undef)
0024 
0025         --enable-after|-E beforeopt option
0026                              Enable option directly after other option
0027         --disable-after|-D beforeopt option
0028                              Disable option directly after other option
0029         --module-after|-M beforeopt option
0030                              Turn option into module directly after other option
0031 
0032         commands can be repeated multiple times
0033 
0034 options:
0035         --file config-file   .config file to change (default .config)
0036         --keep-case|-k       Keep next symbols' case (dont' upper-case it)
0037 
0038 $myname doesn't check the validity of the .config file. This is done at next
0039 make time.
0040 
0041 By default, $myname will upper-case the given symbol. Use --keep-case to keep
0042 the case of all following symbols unchanged.
0043 
0044 $myname uses 'CONFIG_' as the default symbol prefix. Set the environment
0045 variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
0046 EOL
0047         exit 1
0048 }
0049 
0050 checkarg() {
0051         ARG="$1"
0052         if [ "$ARG" = "" ] ; then
0053                 usage
0054         fi
0055         case "$ARG" in
0056         ${CONFIG_}*)
0057                 ARG="${ARG/${CONFIG_}/}"
0058                 ;;
0059         esac
0060         if [ "$MUNGE_CASE" = "yes" ] ; then
0061                 ARG="`echo $ARG | tr a-z A-Z`"
0062         fi
0063 }
0064 
0065 txt_append() {
0066         local anchor="$1"
0067         local insert="$2"
0068         local infile="$3"
0069         local tmpfile="$infile.swp"
0070 
0071         # sed append cmd: 'a\' + newline + text + newline
0072         cmd="$(printf "a\\%b$insert" "\n")"
0073 
0074         sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
0075         # replace original file with the edited one
0076         mv "$tmpfile" "$infile"
0077 }
0078 
0079 txt_subst() {
0080         local before="$1"
0081         local after="$2"
0082         local infile="$3"
0083         local tmpfile="$infile.swp"
0084 
0085         sed -e "s:$before:$after:" "$infile" >"$tmpfile"
0086         # replace original file with the edited one
0087         mv "$tmpfile" "$infile"
0088 }
0089 
0090 txt_delete() {
0091         local text="$1"
0092         local infile="$2"
0093         local tmpfile="$infile.swp"
0094 
0095         sed -e "/$text/d" "$infile" >"$tmpfile"
0096         # replace original file with the edited one
0097         mv "$tmpfile" "$infile"
0098 }
0099 
0100 set_var() {
0101         local name=$1 new=$2 before=$3
0102 
0103         name_re="^($name=|# $name is not set)"
0104         before_re="^($before=|# $before is not set)"
0105         if test -n "$before" && grep -Eq "$before_re" "$FN"; then
0106                 txt_append "^$before=" "$new" "$FN"
0107                 txt_append "^# $before is not set" "$new" "$FN"
0108         elif grep -Eq "$name_re" "$FN"; then
0109                 txt_subst "^$name=.*" "$new" "$FN"
0110                 txt_subst "^# $name is not set" "$new" "$FN"
0111         else
0112                 echo "$new" >>"$FN"
0113         fi
0114 }
0115 
0116 undef_var() {
0117         local name=$1
0118 
0119         txt_delete "^$name=" "$FN"
0120         txt_delete "^# $name is not set" "$FN"
0121 }
0122 
0123 if [ "$1" = "--file" ]; then
0124         FN="$2"
0125         if [ "$FN" = "" ] ; then
0126                 usage
0127         fi
0128         shift 2
0129 else
0130         FN=.config
0131 fi
0132 
0133 if [ "$1" = "" ] ; then
0134         usage
0135 fi
0136 
0137 MUNGE_CASE=yes
0138 while [ "$1" != "" ] ; do
0139         CMD="$1"
0140         shift
0141         case "$CMD" in
0142         --keep-case|-k)
0143                 MUNGE_CASE=no
0144                 continue
0145                 ;;
0146         --refresh)
0147                 ;;
0148         --*-after|-E|-D|-M)
0149                 checkarg "$1"
0150                 A=$ARG
0151                 checkarg "$2"
0152                 B=$ARG
0153                 shift 2
0154                 ;;
0155         -*)
0156                 checkarg "$1"
0157                 shift
0158                 ;;
0159         esac
0160         case "$CMD" in
0161         --enable|-e)
0162                 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
0163                 ;;
0164 
0165         --disable|-d)
0166                 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
0167                 ;;
0168 
0169         --module|-m)
0170                 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
0171                 ;;
0172 
0173         --set-str)
0174                 # sed swallows one level of escaping, so we need double-escaping
0175                 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
0176                 shift
0177                 ;;
0178 
0179         --set-val)
0180                 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
0181                 shift
0182                 ;;
0183         --undefine|-u)
0184                 undef_var "${CONFIG_}$ARG"
0185                 ;;
0186 
0187         --state|-s)
0188                 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
0189                         echo n
0190                 else
0191                         V="$(grep "^${CONFIG_}$ARG=" $FN)"
0192                         if [ $? != 0 ] ; then
0193                                 echo undef
0194                         else
0195                                 V="${V/#${CONFIG_}$ARG=/}"
0196                                 V="${V/#\"/}"
0197                                 V="${V/%\"/}"
0198                                 V="${V//\\\"/\"}"
0199                                 echo "${V}"
0200                         fi
0201                 fi
0202                 ;;
0203 
0204         --enable-after|-E)
0205                 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
0206                 ;;
0207 
0208         --disable-after|-D)
0209                 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
0210                 ;;
0211 
0212         --module-after|-M)
0213                 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
0214                 ;;
0215 
0216         # undocumented because it ignores --file (fixme)
0217         --refresh)
0218                 yes "" | make oldconfig
0219                 ;;
0220 
0221         *)
0222                 usage
0223                 ;;
0224         esac
0225 done