blob: 567120a87c39c0152643fba45f5487979b305afc [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
Clement Chauplannaz73877782013-05-12 21:08:51 +02004myname=${0##*/}
5
Yann E. MORINf5ef2f72012-06-08 01:48:56 +02006# If no prefix forced, use the default CONFIG_
7CONFIG_="${CONFIG_-CONFIG_}"
8
Andi Kleen8e547012009-01-03 03:21:41 +01009usage() {
10 cat >&2 <<EOL
11Manipulate options in a .config file from the command line.
12Usage:
Clement Chauplannaz73877782013-05-12 21:08:51 +020013$myname options command ...
Andi Kleen8e547012009-01-03 03:21:41 +010014commands:
15 --enable|-e option Enable option
16 --disable|-d option Disable option
Michal Marek1f990cf2009-05-25 16:43:27 +020017 --module|-m option Turn option into a module
Jonas Aabergf0a63322010-12-15 08:37:00 +010018 --set-str option string
19 Set option to "string"
20 --set-val option value
21 Set option to value
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020022 --undefine|-u option Undefine option
Michal Marek1f990cf2009-05-25 16:43:27 +020023 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010024
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:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020035 --file config-file .config file to change (default .config)
36 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010037
Clement Chauplannaz73877782013-05-12 21:08:51 +020038$myname doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020039make time.
40
Clement Chauplannaz73877782013-05-12 21:08:51 +020041By default, $myname will upper-case the given symbol. Use --keep-case to keep
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020042the case of all following symbols unchanged.
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020043
Clement Chauplannaz73877782013-05-12 21:08:51 +020044$myname uses 'CONFIG_' as the default symbol prefix. Set the environment
45variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
Andi Kleen8e547012009-01-03 03:21:41 +010046EOL
47 exit 1
48}
49
50checkarg() {
51 ARG="$1"
52 if [ "$ARG" = "" ] ; then
53 usage
54 fi
55 case "$ARG" in
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020056 ${CONFIG_}*)
57 ARG="${ARG/${CONFIG_}/}"
Andi Kleen8e547012009-01-03 03:21:41 +010058 ;;
59 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020060 if [ "$MUNGE_CASE" = "yes" ] ; then
61 ARG="`echo $ARG | tr a-z A-Z`"
62 fi
Andi Kleen8e547012009-01-03 03:21:41 +010063}
64
Michal Marek566432222009-06-14 22:48:07 +020065set_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 Kleen8e547012009-01-03 03:21:41 +010077}
78
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +020079undef_var() {
80 local name=$1
81
82 sed -ri "/^($name=|# $name is not set)/d" "$FN"
83}
84
Andi Kleen8e547012009-01-03 03:21:41 +010085if [ "$1" = "--file" ]; then
86 FN="$2"
87 if [ "$FN" = "" ] ; then
88 usage
89 fi
Michal Marek47312d22009-05-25 16:43:25 +020090 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010091else
92 FN=.config
93fi
94
Andi Kleen2302e872009-01-07 22:33:15 +010095if [ "$1" = "" ] ; then
96 usage
97fi
98
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020099MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +0100100while [ "$1" != "" ] ; do
101 CMD="$1"
102 shift
103 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200104 --keep-case|-k)
105 MUNGE_CASE=no
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200106 continue
107 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200108 --refresh)
109 ;;
Clement Chauplannaz57a9c762013-05-12 21:08:52 +0200110 --*-after|-E|-D|-M)
Andi Kleen8e547012009-01-03 03:21:41 +0100111 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200112 A=$ARG
113 checkarg "$2"
114 B=$ARG
115 shift 2
116 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100117 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200118 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100119 shift
120 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200121 esac
122 case "$CMD" in
123 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200124 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200125 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100126
127 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200128 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100129 ;;
130
131 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200132 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100133 ;;
134
Michal Marek1f990cf2009-05-25 16:43:27 +0200135 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200136 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200137 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200138 shift
139 ;;
140
Jonas Aabergf0a63322010-12-15 08:37:00 +0100141 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200142 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100143 shift
144 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200145 --undefine|-u)
146 undef_var "${CONFIG_}$ARG"
147 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100148
Andi Kleen8e547012009-01-03 03:21:41 +0100149 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200150 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100151 echo n
152 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200153 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100154 if [ $? != 0 ] ; then
155 echo undef
156 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200157 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200158 V="${V/#\"/}"
159 V="${V/%\"/}"
Yann E. MORIN1925a272012-07-15 22:37:35 +0200160 V="${V//\\\"/\"}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200161 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100162 fi
163 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100164 ;;
165
166 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200167 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100168 ;;
169
170 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200171 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100172 ;;
173
174 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200175 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100176 ;;
177
178 # undocumented because it ignores --file (fixme)
179 --refresh)
180 yes "" | make oldconfig
181 ;;
182
183 *)
184 usage
185 ;;
186 esac
187done
188