blob: bb4d3deb6d1c0add7815115a14a574212c0f3d90 [file] [log] [blame]
Andi Kleen8e547012009-01-03 03:21:41 +01001#!/bin/bash
2# Manipulate options in a .config file from the command line
3
Yann E. MORINf5ef2f72012-06-08 01:48:56 +02004# If no prefix forced, use the default CONFIG_
5CONFIG_="${CONFIG_-CONFIG_}"
6
Andi Kleen8e547012009-01-03 03:21:41 +01007usage() {
8 cat >&2 <<EOL
9Manipulate options in a .config file from the command line.
10Usage:
11config options command ...
12commands:
13 --enable|-e option Enable option
14 --disable|-d option Disable option
Michal Marek1f990cf2009-05-25 16:43:27 +020015 --module|-m option Turn option into a module
Jonas Aabergf0a63322010-12-15 08:37:00 +010016 --set-str option string
17 Set option to "string"
18 --set-val option value
19 Set option to value
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020020 --undefine|-u option Undefine option
Michal Marek1f990cf2009-05-25 16:43:27 +020021 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010022
23 --enable-after|-E beforeopt option
24 Enable option directly after other option
25 --disable-after|-D beforeopt option
26 Disable option directly after other option
27 --module-after|-M beforeopt option
28 Turn option into module directly after other option
29
30 commands can be repeated multiple times
31
32options:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020033 --file config-file .config file to change (default .config)
34 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010035
36config doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020037make time.
38
39By default, config will upper-case the given symbol. Use --keep-case to keep
40the case of all following symbols unchanged.
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020041
42config uses 'CONFIG_' as the default symbol prefix. Set the environment
43variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ...
Andi Kleen8e547012009-01-03 03:21:41 +010044EOL
45 exit 1
46}
47
48checkarg() {
49 ARG="$1"
50 if [ "$ARG" = "" ] ; then
51 usage
52 fi
53 case "$ARG" in
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020054 ${CONFIG_}*)
55 ARG="${ARG/${CONFIG_}/}"
Andi Kleen8e547012009-01-03 03:21:41 +010056 ;;
57 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020058 if [ "$MUNGE_CASE" = "yes" ] ; then
59 ARG="`echo $ARG | tr a-z A-Z`"
60 fi
Andi Kleen8e547012009-01-03 03:21:41 +010061}
62
Michal Marek566432222009-06-14 22:48:07 +020063set_var() {
64 local name=$1 new=$2 before=$3
65
66 name_re="^($name=|# $name is not set)"
67 before_re="^($before=|# $before is not set)"
68 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
69 sed -ri "/$before_re/a $new" "$FN"
70 elif grep -Eq "$name_re" "$FN"; then
71 sed -ri "s:$name_re.*:$new:" "$FN"
72 else
73 echo "$new" >>"$FN"
74 fi
Andi Kleen8e547012009-01-03 03:21:41 +010075}
76
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020077undef_var() {
78 local name=$1
79
80 sed -ri "/^($name=|# $name is not set)/d" "$FN"
81}
82
Andi Kleen8e547012009-01-03 03:21:41 +010083if [ "$1" = "--file" ]; then
84 FN="$2"
85 if [ "$FN" = "" ] ; then
86 usage
87 fi
Michal Marek47312d22009-05-25 16:43:25 +020088 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010089else
90 FN=.config
91fi
92
Andi Kleen2302e872009-01-07 22:33:15 +010093if [ "$1" = "" ] ; then
94 usage
95fi
96
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020097MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +010098while [ "$1" != "" ] ; do
99 CMD="$1"
100 shift
101 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200102 --keep-case|-k)
103 MUNGE_CASE=no
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200104 continue
105 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200106 --refresh)
107 ;;
108 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +0100109 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200110 A=$ARG
111 checkarg "$2"
112 B=$ARG
113 shift 2
114 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100115 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200116 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100117 shift
118 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200119 esac
120 case "$CMD" in
121 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200122 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200123 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100124
125 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200126 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100127 ;;
128
129 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200130 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100131 ;;
132
Michal Marek1f990cf2009-05-25 16:43:27 +0200133 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200134 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200135 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200136 shift
137 ;;
138
Jonas Aabergf0a63322010-12-15 08:37:00 +0100139 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200140 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100141 shift
142 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200143 --undefine|-u)
144 undef_var "${CONFIG_}$ARG"
145 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100146
Andi Kleen8e547012009-01-03 03:21:41 +0100147 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200148 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100149 echo n
150 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200151 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100152 if [ $? != 0 ] ; then
153 echo undef
154 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200155 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200156 V="${V/#\"/}"
157 V="${V/%\"/}"
Yann E. MORIN1925a272012-07-15 22:37:35 +0200158 V="${V//\\\"/\"}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200159 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100160 fi
161 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100162 ;;
163
164 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200165 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100166 ;;
167
168 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200169 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100170 ;;
171
172 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200173 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100174 ;;
175
176 # undocumented because it ignores --file (fixme)
177 --refresh)
178 yes "" | make oldconfig
179 ;;
180
181 *)
182 usage
183 ;;
184 esac
185done
186