Root/scripts/config

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

Archive Download this file



interactive