blob: 30825a5677f668a53ada6c6eb385a8b0769a841a [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
12 --module|-m option Turn option into a module
13 --state|-s option Print state of option (n,y,m,undef)
14
15 --enable-after|-E beforeopt option
16 Enable option directly after other option
17 --disable-after|-D beforeopt option
18 Disable option directly after other option
19 --module-after|-M beforeopt option
20 Turn option into module directly after other option
21
22 commands can be repeated multiple times
23
24options:
25 --file .config file to change (default .config)
26
27config doesn't check the validity of the .config file. This is done at next
28 make time.
Andi Kleen8e547012009-01-03 03:21:41 +010029EOL
30 exit 1
31}
32
33checkarg() {
34 ARG="$1"
35 if [ "$ARG" = "" ] ; then
36 usage
37 fi
38 case "$ARG" in
39 CONFIG_*)
40 ARG="${ARG/CONFIG_/}"
41 ;;
42 esac
43 ARG="`echo $ARG | tr a-z A-Z`"
44}
45
Michal Marek56643222009-06-14 22:48:07 +020046set_var() {
47 local name=$1 new=$2 before=$3
48
49 name_re="^($name=|# $name is not set)"
50 before_re="^($before=|# $before is not set)"
51 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
52 sed -ri "/$before_re/a $new" "$FN"
53 elif grep -Eq "$name_re" "$FN"; then
54 sed -ri "s:$name_re.*:$new:" "$FN"
55 else
56 echo "$new" >>"$FN"
57 fi
Andi Kleen8e547012009-01-03 03:21:41 +010058}
59
60if [ "$1" = "--file" ]; then
61 FN="$2"
62 if [ "$FN" = "" ] ; then
63 usage
64 fi
65 shift
66 shift
67else
68 FN=.config
69fi
70
Andi Kleen2302e872009-01-07 22:33:15 +010071if [ "$1" = "" ] ; then
72 usage
73fi
74
Andi Kleen8e547012009-01-03 03:21:41 +010075while [ "$1" != "" ] ; do
76 CMD="$1"
77 shift
78 case "$CMD" in
79 --enable|-e)
80 checkarg "$1"
Michal Marek56643222009-06-14 22:48:07 +020081 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
Andi Kleen8e547012009-01-03 03:21:41 +010082 shift
83 ;;
84
85 --disable|-d)
86 checkarg "$1"
Michal Marek56643222009-06-14 22:48:07 +020087 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +010088 shift
89 ;;
90
91 --module|-m)
92 checkarg "$1"
Michal Marek56643222009-06-14 22:48:07 +020093 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +010094 shift
95 ;;
96
97 --state|-s)
98 checkarg "$1"
99 if grep -q "# CONFIG_$ARG is not set" $FN ; then
100 echo n
101 else
102 V="$(grep "^CONFIG_$ARG=" $FN)"
103 if [ $? != 0 ] ; then
104 echo undef
105 else
106 V="${V/CONFIG_$ARG=/}"
107 V="${V/\"/}"
108 echo "$V"
109 fi
110 fi
111 shift
112 ;;
113
114 --enable-after|-E)
115 checkarg "$1"
116 A=$ARG
117 checkarg "$2"
118 B=$ARG
Michal Marek56643222009-06-14 22:48:07 +0200119 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100120 shift
121 shift
122 ;;
123
124 --disable-after|-D)
125 checkarg "$1"
126 A=$ARG
127 checkarg "$2"
128 B=$ARG
Michal Marek56643222009-06-14 22:48:07 +0200129 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100130 shift
131 shift
132 ;;
133
134 --module-after|-M)
135 checkarg "$1"
136 A=$ARG
137 checkarg "$2"
138 B=$ARG
Michal Marek56643222009-06-14 22:48:07 +0200139 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100140 shift
141 shift
142 ;;
143
144 # undocumented because it ignores --file (fixme)
145 --refresh)
146 yes "" | make oldconfig
147 ;;
148
149 *)
150 usage
151 ;;
152 esac
153done
154