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