blob: f65a44a7886a7192d3c9cea6457e1aa8dee4d42f [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
104 shift
105 continue
106 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200107 --refresh)
108 ;;
109 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +0100110 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200111 A=$ARG
112 checkarg "$2"
113 B=$ARG
114 shift 2
115 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100116 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200117 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100118 shift
119 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200120 esac
121 case "$CMD" in
122 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200123 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200124 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100125
126 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200127 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100128 ;;
129
130 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200131 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100132 ;;
133
Michal Marek1f990cf2009-05-25 16:43:27 +0200134 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200135 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200136 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200137 shift
138 ;;
139
Jonas Aabergf0a63322010-12-15 08:37:00 +0100140 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200141 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100142 shift
143 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200144 --undefine|-u)
145 undef_var "${CONFIG_}$ARG"
146 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100147
Andi Kleen8e547012009-01-03 03:21:41 +0100148 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200149 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100150 echo n
151 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200152 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100153 if [ $? != 0 ] ; then
154 echo undef
155 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200156 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200157 V="${V/#\"/}"
158 V="${V/%\"/}"
159 V="${V/\\\"/\"}"
160 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100161 fi
162 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100163 ;;
164
165 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200166 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100167 ;;
168
169 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200170 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100171 ;;
172
173 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200174 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100175 ;;
176
177 # undocumented because it ignores --file (fixme)
178 --refresh)
179 yes "" | make oldconfig
180 ;;
181
182 *)
183 usage
184 ;;
185 esac
186done
187