blob: c5639fe5bba8d68afa4f1e6f952147cb43d19545 [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
4usage() {
5 cat >&2 <<EOL
6Manipulate options in a .config file from the command line.
7Usage:
8config options command ...
9commands:
10 --enable|-e option Enable option
11 --disable|-d option Disable option
Michal Marek1f990cf2009-05-25 16:43:27 +020012 --module|-m option Turn option into a module
Jonas Aabergf0a63322010-12-15 08:37:00 +010013 --set-str option string
14 Set option to "string"
15 --set-val option value
16 Set option to value
Michal Marek1f990cf2009-05-25 16:43:27 +020017 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010018
19 --enable-after|-E beforeopt option
20 Enable option directly after other option
21 --disable-after|-D beforeopt option
22 Disable option directly after other option
23 --module-after|-M beforeopt option
24 Turn option into module directly after other option
25
26 commands can be repeated multiple times
27
28options:
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020029 --file config-file .config file to change (default .config)
30 --keep-case|-k Keep next symbols' case (dont' upper-case it)
Andi Kleen8e547012009-01-03 03:21:41 +010031
32config doesn't check the validity of the .config file. This is done at next
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020033make time.
34
35By default, config will upper-case the given symbol. Use --keep-case to keep
36the case of all following symbols unchanged.
Andi Kleen8e547012009-01-03 03:21:41 +010037EOL
38 exit 1
39}
40
41checkarg() {
42 ARG="$1"
43 if [ "$ARG" = "" ] ; then
44 usage
45 fi
46 case "$ARG" in
47 CONFIG_*)
48 ARG="${ARG/CONFIG_/}"
49 ;;
50 esac
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020051 if [ "$MUNGE_CASE" = "yes" ] ; then
52 ARG="`echo $ARG | tr a-z A-Z`"
53 fi
Andi Kleen8e547012009-01-03 03:21:41 +010054}
55
Michal Marek56643222009-06-14 22:48:07 +020056set_var() {
57 local name=$1 new=$2 before=$3
58
59 name_re="^($name=|# $name is not set)"
60 before_re="^($before=|# $before is not set)"
61 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
62 sed -ri "/$before_re/a $new" "$FN"
63 elif grep -Eq "$name_re" "$FN"; then
64 sed -ri "s:$name_re.*:$new:" "$FN"
65 else
66 echo "$new" >>"$FN"
67 fi
Andi Kleen8e547012009-01-03 03:21:41 +010068}
69
70if [ "$1" = "--file" ]; then
71 FN="$2"
72 if [ "$FN" = "" ] ; then
73 usage
74 fi
Michal Marek47312d22009-05-25 16:43:25 +020075 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010076else
77 FN=.config
78fi
79
Andi Kleen2302e872009-01-07 22:33:15 +010080if [ "$1" = "" ] ; then
81 usage
82fi
83
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020084MUNGE_CASE=yes
Andi Kleen8e547012009-01-03 03:21:41 +010085while [ "$1" != "" ] ; do
86 CMD="$1"
87 shift
88 case "$CMD" in
Yann E. MORIN4edc7e32012-06-08 01:48:55 +020089 --keep-case|-k)
90 MUNGE_CASE=no
91 shift
92 continue
93 ;;
Michal Marek47312d22009-05-25 16:43:25 +020094 --refresh)
95 ;;
96 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +010097 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +020098 A=$ARG
99 checkarg "$2"
100 B=$ARG
101 shift 2
102 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +0100103 -*)
Michal Marek47312d22009-05-25 16:43:25 +0200104 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +0100105 shift
106 ;;
Michal Marek47312d22009-05-25 16:43:25 +0200107 esac
108 case "$CMD" in
109 --enable|-e)
110 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
111 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100112
113 --disable|-d)
Michal Marek56643222009-06-14 22:48:07 +0200114 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100115 ;;
116
117 --module|-m)
Michal Marek56643222009-06-14 22:48:07 +0200118 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100119 ;;
120
Michal Marek1f990cf2009-05-25 16:43:27 +0200121 --set-str)
Yann E. MORINd6686da2012-04-09 14:49:10 +0200122 # sed swallows one level of escaping, so we need double-escaping
123 set_var "CONFIG_$ARG" "CONFIG_$ARG=\"${1//\"/\\\\\"}\""
Michal Marek1f990cf2009-05-25 16:43:27 +0200124 shift
125 ;;
126
Jonas Aabergf0a63322010-12-15 08:37:00 +0100127 --set-val)
128 set_var "CONFIG_$ARG" "CONFIG_$ARG=$1"
129 shift
130 ;;
131
Andi Kleen8e547012009-01-03 03:21:41 +0100132 --state|-s)
Andi Kleen8e547012009-01-03 03:21:41 +0100133 if grep -q "# CONFIG_$ARG is not set" $FN ; then
134 echo n
135 else
136 V="$(grep "^CONFIG_$ARG=" $FN)"
137 if [ $? != 0 ] ; then
138 echo undef
139 else
Yann E. MORINd6686da2012-04-09 14:49:10 +0200140 V="${V/#CONFIG_$ARG=/}"
141 V="${V/#\"/}"
142 V="${V/%\"/}"
143 V="${V/\\\"/\"}"
144 echo "${V}"
Andi Kleen8e547012009-01-03 03:21:41 +0100145 fi
146 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100147 ;;
148
149 --enable-after|-E)
Michal Marek56643222009-06-14 22:48:07 +0200150 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100151 ;;
152
153 --disable-after|-D)
Michal Marek56643222009-06-14 22:48:07 +0200154 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100155 ;;
156
157 --module-after|-M)
Michal Marek56643222009-06-14 22:48:07 +0200158 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100159 ;;
160
161 # undocumented because it ignores --file (fixme)
162 --refresh)
163 yes "" | make oldconfig
164 ;;
165
166 *)
167 usage
168 ;;
169 esac
170done
171