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