blob: 2283be2bb62c067d3d318431d1071328a7d649fb [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
Clement Chauplannaz83e8b902013-07-13 16:36:56 +020065txt_append() {
66 local anchor="$1"
67 local insert="$2"
68 local infile="$3"
69 local tmpfile="$infile.swp"
70
71 # sed append cmd: 'a\' + newline + text + newline
72 cmd="$(printf "a\\%b$insert" "\n")"
73
74 sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
75 # replace original file with the edited one
76 mv "$tmpfile" "$infile"
77}
78
79txt_subst() {
80 local before="$1"
81 local after="$2"
82 local infile="$3"
83 local tmpfile="$infile.swp"
84
85 sed -e "s/$before/$after/" "$infile" >"$tmpfile"
86 # replace original file with the edited one
87 mv "$tmpfile" "$infile"
88}
89
90txt_delete() {
91 local text="$1"
92 local infile="$2"
93 local tmpfile="$infile.swp"
94
95 sed -e "/$text/d" "$infile" >"$tmpfile"
96 # replace original file with the edited one
97 mv "$tmpfile" "$infile"
98}
99
Michal Marek566432222009-06-14 22:48:07 +0200100set_var() {
101 local name=$1 new=$2 before=$3
102
103 name_re="^($name=|# $name is not set)"
104 before_re="^($before=|# $before is not set)"
105 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200106 txt_append "^$before=" "$new" "$FN"
107 txt_append "^# $before is not set" "$new" "$FN"
Michal Marek566432222009-06-14 22:48:07 +0200108 elif grep -Eq "$name_re" "$FN"; then
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200109 txt_subst "^$name=.*" "$new" "$FN"
110 txt_subst "^# $name is not set" "$new" "$FN"
Michal Marek566432222009-06-14 22:48:07 +0200111 else
112 echo "$new" >>"$FN"
113 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100114}
115
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200116undef_var() {
117 local name=$1
118
Clement Chauplannaz83e8b902013-07-13 16:36:56 +0200119 txt_delete "^$name=" "$FN"
120 txt_delete "^# $name is not set" "$FN"
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200121}
122
Andi Kleen8e547012009-01-03 03:21:41 +0100123if [ "$1" = "--file" ]; then
124 FN="$2"
125 if [ "$FN" = "" ] ; then
126 usage
127 fi
Michal Marek47312d22009-05-25 16:43:25 +0200128 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +0100129else
130 FN=.config
131fi
132
Andi Kleen2302e872009-01-07 22:33:15 +0100133if [ "$1" = "" ] ; then
134 usage
135fi
136
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200137MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +0100138while [ "$1" != "" ] ; do
139 CMD="$1"
140 shift
141 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200142 --keep-case|-k)
143 MUNGE_CASE=no
Yann E. MORIN4edc7e32012-06-08 01:48:55 +0200144 continue
145 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200146 --refresh)
147 ;;
Clement Chauplannaz57a9c762013-05-12 21:08:52 +0200148 --*-after|-E|-D|-M)
Andi Kleen8e547012009-01-03 03:21:41 +0100149 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200150 A=$ARG
151 checkarg "$2"
152 B=$ARG
153 shift 2
154 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100155 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200156 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100157 shift
158 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200159 esac
160 case "$CMD" in
161 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200162 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200163 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100164
165 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200166 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100167 ;;
168
169 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200170 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100171 ;;
172
Michal Marek1f990cf2009-05-25 16:43:27 +0200173 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200174 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200175 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200176 shift
177 ;;
178
Jonas Aabergf0a63322010-12-15 08:37:00 +0100179 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200180 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100181 shift
182 ;;
Yann E. MORINd5bfb6b2012-06-08 01:48:57 +0200183 --undefine|-u)
184 undef_var "${CONFIG_}$ARG"
185 ;;
Jonas Aabergf0a63322010-12-15 08:37:00 +0100186
Andi Kleen8e547012009-01-03 03:21:41 +0100187 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200188 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100189 echo n
190 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200191 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100192 if [ $? != 0 ] ; then
193 echo undef
194 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200195 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200196 V="${V/#\"/}"
197 V="${V/%\"/}"
Yann E. MORIN1925a272012-07-15 22:37:35 +0200198 V="${V//\\\"/\"}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200199 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100200 fi
201 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100202 ;;
203
204 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200205 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100206 ;;
207
208 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200209 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100210 ;;
211
212 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200213 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100214 ;;
215
216 # undocumented because it ignores --file (fixme)
217 --refresh)
218 yes "" | make oldconfig
219 ;;
220
221 *)
222 usage
223 ;;
224 esac
225done
226