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