blob: a7c7c4b8e957311196f9b2eabef414ceaa144555 [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:
29 --file .config file to change (default .config)
30
31config doesn't check the validity of the .config file. This is done at next
32 make time.
Andi Kleen8e547012009-01-03 03:21:41 +010033EOL
34 exit 1
35}
36
37checkarg() {
38 ARG="$1"
39 if [ "$ARG" = "" ] ; then
40 usage
41 fi
42 case "$ARG" in
43 CONFIG_*)
44 ARG="${ARG/CONFIG_/}"
45 ;;
46 esac
47 ARG="`echo $ARG | tr a-z A-Z`"
48}
49
Michal Marek56643222009-06-14 22:48:07 +020050set_var() {
51 local name=$1 new=$2 before=$3
52
53 name_re="^($name=|# $name is not set)"
54 before_re="^($before=|# $before is not set)"
55 if test -n "$before" && grep -Eq "$before_re" "$FN"; then
56 sed -ri "/$before_re/a $new" "$FN"
57 elif grep -Eq "$name_re" "$FN"; then
58 sed -ri "s:$name_re.*:$new:" "$FN"
59 else
60 echo "$new" >>"$FN"
61 fi
Andi Kleen8e547012009-01-03 03:21:41 +010062}
63
64if [ "$1" = "--file" ]; then
65 FN="$2"
66 if [ "$FN" = "" ] ; then
67 usage
68 fi
Michal Marek47312d22009-05-25 16:43:25 +020069 shift 2
Andi Kleen8e547012009-01-03 03:21:41 +010070else
71 FN=.config
72fi
73
Andi Kleen2302e872009-01-07 22:33:15 +010074if [ "$1" = "" ] ; then
75 usage
76fi
77
Andi Kleen8e547012009-01-03 03:21:41 +010078while [ "$1" != "" ] ; do
79 CMD="$1"
80 shift
81 case "$CMD" in
Michal Marek47312d22009-05-25 16:43:25 +020082 --refresh)
83 ;;
84 --*-after)
Andi Kleen8e547012009-01-03 03:21:41 +010085 checkarg "$1"
Michal Marek47312d22009-05-25 16:43:25 +020086 A=$ARG
87 checkarg "$2"
88 B=$ARG
89 shift 2
90 ;;
Andi Kleen45f53cc2010-11-05 12:06:05 +010091 -*)
Michal Marek47312d22009-05-25 16:43:25 +020092 checkarg "$1"
Andi Kleen8e547012009-01-03 03:21:41 +010093 shift
94 ;;
Michal Marek47312d22009-05-25 16:43:25 +020095 esac
96 case "$CMD" in
97 --enable|-e)
98 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
99 ;;
Andi Kleen8e547012009-01-03 03:21:41 +0100100
101 --disable|-d)
Michal Marek56643222009-06-14 22:48:07 +0200102 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
Andi Kleen8e547012009-01-03 03:21:41 +0100103 ;;
104
105 --module|-m)
Michal Marek56643222009-06-14 22:48:07 +0200106 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
Andi Kleen8e547012009-01-03 03:21:41 +0100107 ;;
108
Michal Marek1f990cf2009-05-25 16:43:27 +0200109 --set-str)
110 set_var "CONFIG_$ARG" "CONFIG_$ARG=\"$1\""
111 shift
112 ;;
113
Jonas Aabergf0a63322010-12-15 08:37:00 +0100114 --set-val)
115 set_var "CONFIG_$ARG" "CONFIG_$ARG=$1"
116 shift
117 ;;
118
Andi Kleen8e547012009-01-03 03:21:41 +0100119 --state|-s)
Andi Kleen8e547012009-01-03 03:21:41 +0100120 if grep -q "# CONFIG_$ARG is not set" $FN ; then
121 echo n
122 else
123 V="$(grep "^CONFIG_$ARG=" $FN)"
124 if [ $? != 0 ] ; then
125 echo undef
126 else
127 V="${V/CONFIG_$ARG=/}"
128 V="${V/\"/}"
129 echo "$V"
130 fi
131 fi
Andi Kleen8e547012009-01-03 03:21:41 +0100132 ;;
133
134 --enable-after|-E)
Michal Marek56643222009-06-14 22:48:07 +0200135 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100136 ;;
137
138 --disable-after|-D)
Michal Marek56643222009-06-14 22:48:07 +0200139 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100140 ;;
141
142 --module-after|-M)
Michal Marek56643222009-06-14 22:48:07 +0200143 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
Andi Kleen8e547012009-01-03 03:21:41 +0100144 ;;
145
146 # undocumented because it ignores --file (fixme)
147 --refresh)
148 yes "" | make oldconfig
149 ;;
150
151 *)
152 usage
153 ;;
154 esac
155done
156