blob: 9723c7de07cc1f2b905b48029ac677aeab3bfc32 [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
Michal Marek1f990cf2009-05-25 16:43:27 +020020 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010021
22 --enable-after|-E beforeopt option
23 Enable option directly after other option
24 --disable-after|-D beforeopt option
25 Disable option directly after other option
26 --module-after|-M beforeopt option
27 Turn option into module directly after other option
28
29 commands can be repeated multiple times
30
31options:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020032 --file config-file .config file to change (default .config)
33 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010034
35config doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020036make time.
37
38By default, config will upper-case the given symbol. Use --keep-case to keep
39the case of all following symbols unchanged.
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020040
41config uses 'CONFIG_' as the default symbol prefix. Set the environment
42variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ...
Andi Kleen8e547012009-01-03 03:21:41 +010043EOL
44 exit 1
45}
46
47checkarg() {
48 ARG="$1"
49 if [ "$ARG" = "" ] ; then
50 usage
51 fi
52 case "$ARG" in
Yann E. MORINf5ef2f72012-06-08 01:48:56 +020053 ${CONFIG_}*)
54 ARG="${ARG/${CONFIG_}/}"
Andi Kleen8e547012009-01-03 03:21:41 +010055 ;;
56 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020057 if [ "$MUNGE_CASE" = "yes" ] ; then
58 ARG="`echo $ARG | tr a-z A-Z`"
59 fi
Andi Kleen8e547012009-01-03 03:21:41 +010060}
61
Michal Marek566432222009-06-14 22:48:07 +020062set_var() {
63 local name=$1 new=$2 before=$3
64
65 name_re="^($name=|# $name is not set)"
66 before_re="^($before=|# $before is not set)"
67 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
68 sed -ri "/$before_re/a $new" "$FN"
69 elif grep -Eq "$name_re" "$FN"; then
70 sed -ri "s:$name_re.*:$new:" "$FN"
71 else
72 echo "$new" >>"$FN"
73 fi
Andi Kleen8e547012009-01-03 03:21:41 +010074}
75
76if [ "$1" = "--file" ]; then
77 FN="$2"
78 if [ "$FN" = "" ] ; then
79 usage
80 fi
Michal Marek47312d22009-05-25 16:43:25 +020081 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010082else
83 FN=.config
84fi
85
Andi Kleen2302e872009-01-07 22:33:15 +010086if [ "$1" = "" ] ; then
87 usage
88fi
89
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020090MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +010091while [ "$1" != "" ] ; do
92 CMD="$1"
93 shift
94 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020095 --keep-case|-k)
96 MUNGE_CASE=no
97 shift
98 continue
99 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200100 --refresh)
101 ;;
102 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +0100103 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +0200104 A=$ARG
105 checkarg "$2"
106 B=$ARG
107 shift 2
108 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100109 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200110 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100111 shift
112 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200113 esac
114 case "$CMD" in
115 --enable|-e)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200116 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
Michal Marek47312d22009-05-25 16:43:25 +0200117 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100118
119 --disable|-d)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200120 set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100121 ;;
122
123 --module|-m)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200124 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100125 ;;
126
Michal Marek1f990cf2009-05-25 16:43:27 +0200127 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200128 # sed swallows one level of escaping, so we need double-escaping
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200129 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200130 shift
131 ;;
132
Jonas Aabergf0a63322010-12-15 08:37:00 +0100133 --set-val)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200134 set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
Jonas Aabergf0a63322010-12-15 08:37:00 +0100135 shift
136 ;;
137
Andi Kleen8e547012009-01-03 03:21:41 +0100138 --state|-s)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200139 if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
Andi Kleen8e547012009-01-03 03:21:41 +0100140 echo n
141 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200142 V="$(grep "^${CONFIG_}$ARG=" $FN)"
Andi Kleen8e547012009-01-03 03:21:41 +0100143 if [ $? != 0 ] ; then
144 echo undef
145 else
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200146 V="${V/#${CONFIG_}$ARG=/}"
Yann E. MORINd6686da2012-04-09 14:49:10 +0200147 V="${V/#\"/}"
148 V="${V/%\"/}"
149 V="${V/\\\"/\"}"
150 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100151 fi
152 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100153 ;;
154
155 --enable-after|-E)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200156 set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100157 ;;
158
159 --disable-after|-D)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200160 set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100161 ;;
162
163 --module-after|-M)
Yann E. MORINf5ef2f72012-06-08 01:48:56 +0200164 set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100165 ;;
166
167 # undocumented because it ignores --file (fixme)
168 --refresh)
169 yes "" | make oldconfig
170 ;;
171
172 *)
173 usage
174 ;;
175 esac
176done
177