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