linux/scripts/config
<<
>>
Prefs
   1#!/bin/bash
   2# Manipulate options in a .config file from the command line
   3
   4# If no prefix forced, use the default CONFIG_
   5CONFIG_="${CONFIG_-CONFIG_}"
   6
   7usage() {
   8        cat >&2 <<EOL
   9Manipulate options in a .config file from the command line.
  10Usage:
  11config options command ...
  12commands:
  13        --enable|-e option   Enable option
  14        --disable|-d option  Disable option
  15        --module|-m option   Turn option into a module
  16        --set-str option string
  17                             Set option to "string"
  18        --set-val option value
  19                             Set option to value
  20        --undefine|-u option Undefine option
  21        --state|-s option    Print state of option (n,y,m,undef)
  22
  23        --enable-after|-E beforeopt option
  24                             Enable option directly after other option
  25        --disable-after|-D beforeopt option
  26                             Disable option directly after other option
  27        --module-after|-M beforeopt option
  28                             Turn option into module directly after other option
  29
  30        commands can be repeated multiple times
  31
  32options:
  33        --file config-file   .config file to change (default .config)
  34        --keep-case|-k       Keep next symbols' case (dont' upper-case it)
  35
  36config doesn't check the validity of the .config file. This is done at next
  37make time.
  38
  39By default, config will upper-case the given symbol. Use --keep-case to keep
  40the case of all following symbols unchanged.
  41
  42config uses 'CONFIG_' as the default symbol prefix. Set the environment
  43variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ...
  44EOL
  45        exit 1
  46}
  47
  48checkarg() {
  49        ARG="$1"
  50        if [ "$ARG" = "" ] ; then
  51                usage
  52        fi
  53        case "$ARG" in
  54        ${CONFIG_}*)
  55                ARG="${ARG/${CONFIG_}/}"
  56                ;;
  57        esac
  58        if [ "$MUNGE_CASE" = "yes" ] ; then
  59                ARG="`echo $ARG | tr a-z A-Z`"
  60        fi
  61}
  62
  63set_var() {
  64        local name=$1 new=$2 before=$3
  65
  66        name_re="^($name=|# $name is not set)"
  67        before_re="^($before=|# $before is not set)"
  68        if test -n "$before" && grep -Eq "$before_re" "$FN"; then
  69                sed -ri "/$before_re/a $new" "$FN"
  70        elif grep -Eq "$name_re" "$FN"; then
  71                sed -ri "s:$name_re.*:$new:" "$FN"
  72        else
  73                echo "$new" >>"$FN"
  74        fi
  75}
  76
  77undef_var() {
  78        local name=$1
  79
  80        sed -ri "/^($name=|# $name is not set)/d" "$FN"
  81}
  82
  83if [ "$1" = "--file" ]; then
  84        FN="$2"
  85        if [ "$FN" = "" ] ; then
  86                usage
  87        fi
  88        shift 2
  89else
  90        FN=.config
  91fi
  92
  93if [ "$1" = "" ] ; then
  94        usage
  95fi
  96
  97MUNGE_CASE=yes
  98while [ "$1" != "" ] ; do
  99        CMD="$1"
 100        shift
 101        case "$CMD" in
 102        --keep-case|-k)
 103                MUNGE_CASE=no
 104                continue
 105                ;;
 106        --refresh)
 107                ;;
 108        --*-after)
 109                checkarg "$1"
 110                A=$ARG
 111                checkarg "$2"
 112                B=$ARG
 113                shift 2
 114                ;;
 115        -*)
 116                checkarg "$1"
 117                shift
 118                ;;
 119        esac
 120        case "$CMD" in
 121        --enable|-e)
 122                set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
 123                ;;
 124
 125        --disable|-d)
 126                set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
 127                ;;
 128
 129        --module|-m)
 130                set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
 131                ;;
 132
 133        --set-str)
 134                # sed swallows one level of escaping, so we need double-escaping
 135                set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
 136                shift
 137                ;;
 138
 139        --set-val)
 140                set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
 141                shift
 142                ;;
 143        --undefine|-u)
 144                undef_var "${CONFIG_}$ARG"
 145                ;;
 146
 147        --state|-s)
 148                if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
 149                        echo n
 150                else
 151                        V="$(grep "^${CONFIG_}$ARG=" $FN)"
 152                        if [ $? != 0 ] ; then
 153                                echo undef
 154                        else
 155                                V="${V/#${CONFIG_}$ARG=/}"
 156                                V="${V/#\"/}"
 157                                V="${V/%\"/}"
 158                                V="${V//\\\"/\"}"
 159                                echo "${V}"
 160                        fi
 161                fi
 162                ;;
 163
 164        --enable-after|-E)
 165                set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
 166                ;;
 167
 168        --disable-after|-D)
 169                set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
 170                ;;
 171
 172        --module-after|-M)
 173                set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
 174                ;;
 175
 176        # undocumented because it ignores --file (fixme)
 177        --refresh)
 178                yes "" | make oldconfig
 179                ;;
 180
 181        *)
 182                usage
 183                ;;
 184        esac
 185done
 186
 187
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.