blob: 608d7fdb13e8d6ac901eaf99cc3b1b5df0a7cc95 [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
13 --set-str option value
14 Set option to "value"
15 --state|-s option Print state of option (n,y,m,undef)
Andi Kleen8e547012009-01-03 03:21:41 +010016
17 --enable-after|-E beforeopt option
18 Enable option directly after other option
19 --disable-after|-D beforeopt option
20 Disable option directly after other option
21 --module-after|-M beforeopt option
22 Turn option into module directly after other option
23
24 commands can be repeated multiple times
25
26options:
27 --file .config file to change (default .config)
28
29config doesn't check the validity of the .config file. This is done at next
30 make time.
Andi Kleen8e547012009-01-03 03:21:41 +010031EOL
32 exit 1
33}
34
35checkarg() {
36 ARG="$1"
37 if [ "$ARG" = "" ] ; then
38 usage
39 fi
40 case "$ARG" in
41 CONFIG_*)
42 ARG="${ARG/CONFIG_/}"
43 ;;
44 esac
45 ARG="`echo $ARG | tr a-z A-Z`"
46}
47
Michal Marek566432222009-06-14 22:48:07 +020048set_var() {
49 local name=$1 new=$2 before=$3
50
51 name_re="^($name=|# $name is not set)"
52 before_re="^($before=|# $before is not set)"
53 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
54 sed -ri "/$before_re/a $new" "$FN"
55 elif grep -Eq "$name_re" "$FN"; then
56 sed -ri "s:$name_re.*:$new:" "$FN"
57 else
58 echo "$new" >>"$FN"
59 fi
Andi Kleen8e547012009-01-03 03:21:41 +010060}
61
62if [ "$1" = "--file" ]; then
63 FN="$2"
64 if [ "$FN" = "" ] ; then
65 usage
66 fi
Michal Marek47312d22009-05-25 16:43:25 +020067 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010068else
69 FN=.config
70fi
71
Andi Kleen2302e872009-01-07 22:33:15 +010072if [ "$1" = "" ] ; then
73 usage
74fi
75
Andi Kleen8e547012009-01-03 03:21:41 +010076while [ "$1" != "" ] ; do
77 CMD="$1"
78 shift
79 case "$CMD" in
Michal Marek47312d22009-05-25 16:43:25 +020080 --refresh)
81 ;;
82 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +010083 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +020084 A=$ARG
85 checkarg "$2"
86 B=$ARG
87 shift 2
88 ;;
89 --*)
90 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +010091 shift
92 ;;
Michal Marek47312d22009-05-25 16:43:25 +020093 esac
94 case "$CMD" in
95 --enable|-e)
96 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
97 ;;
Andi Kleen8e547012009-01-03 03:21:41 +010098
99 --disable|-d)
Michal Marek566432222009-06-14 22:48:07 +0200100 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100101 ;;
102
103 --module|-m)
Michal Marek566432222009-06-14 22:48:07 +0200104 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100105 ;;
106
Michal Marek1f990cf2009-05-25 16:43:27 +0200107 --set-str)
108 set_var "CONFIG_$ARG" "CONFIG_$ARG=\"$1\""
109 shift
110 ;;
111
Andi Kleen8e547012009-01-03 03:21:41 +0100112 --state|-s)
Andi Kleen8e547012009-01-03 03:21:41 +0100113 if grep -q "# CONFIG_$ARG is not set" $FN ; then
114 echo n
115 else
116 V="$(grep "^CONFIG_$ARG=" $FN)"
117 if [ $? != 0 ] ; then
118 echo undef
119 else
120 V="${V/CONFIG_$ARG=/}"
121 V="${V/\"/}"
122 echo "$V"
123 fi
124 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100125 ;;
126
127 --enable-after|-E)
Michal Marek566432222009-06-14 22:48:07 +0200128 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100129 ;;
130
131 --disable-after|-D)
Michal Marek566432222009-06-14 22:48:07 +0200132 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100133 ;;
134
135 --module-after|-M)
Michal Marek566432222009-06-14 22:48:07 +0200136 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100137 ;;
138
139 # undocumented because it ignores --file (fixme)
140 --refresh)
141 yes "" | make oldconfig
142 ;;
143
144 *)
145 usage
146 ;;
147 esac
148done
149