blob: 640c6fe4d644ab6e1e0ae7c815b9096266a9a413 [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
Michal Marek47312d22009-05-25 16:43:25 +020065 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010066else
67 FN=.config
68fi
69
Andi Kleen2302e872009-01-07 22:33:15 +010070if [ "$1" = "" ] ; then
71 usage
72fi
73
Andi Kleen8e547012009-01-03 03:21:41 +010074while [ "$1" != "" ] ; do
75 CMD="$1"
76 shift
77 case "$CMD" in
Michal Marek47312d22009-05-25 16:43:25 +020078 --refresh)
79 ;;
80 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +010081 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +020082 A=$ARG
83 checkarg "$2"
84 B=$ARG
85 shift 2
86 ;;
87 --*)
88 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +010089 shift
90 ;;
Michal Marek47312d22009-05-25 16:43:25 +020091 esac
92 case "$CMD" in
93 --enable|-e)
94 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
95 ;;
Andi Kleen8e547012009-01-03 03:21:41 +010096
97 --disable|-d)
Michal Marek56643222009-06-14 22:48:07 +020098 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +010099 ;;
100
101 --module|-m)
Michal Marek56643222009-06-14 22:48:07 +0200102 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100103 ;;
104
105 --state|-s)
Andi Kleen8e547012009-01-03 03:21:41 +0100106 if grep -q "# CONFIG_$ARG is not set" $FN ; then
107 echo n
108 else
109 V="$(grep "^CONFIG_$ARG=" $FN)"
110 if [ $? != 0 ] ; then
111 echo undef
112 else
113 V="${V/CONFIG_$ARG=/}"
114 V="${V/\"/}"
115 echo "$V"
116 fi
117 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100118 ;;
119
120 --enable-after|-E)
Michal Marek56643222009-06-14 22:48:07 +0200121 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100122 ;;
123
124 --disable-after|-D)
Michal Marek56643222009-06-14 22:48:07 +0200125 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100126 ;;
127
128 --module-after|-M)
Michal Marek56643222009-06-14 22:48:07 +0200129 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100130 ;;
131
132 # undocumented because it ignores --file (fixme)
133 --refresh)
134 yes "" | make oldconfig
135 ;;
136
137 *)
138 usage
139 ;;
140 esac
141done
142