]> git.sur5r.net Git - kconfig-frontends/blob - utils/tweak
utils/tweak: make it case-sensitive
[kconfig-frontends] / utils / tweak
1 #!/bin/bash
2 # Manipulate options in a .config file from the command line
3
4 usage() {
5         cat >&2 <<EOL
6 Manipulate options in a .config file from the command line.
7 Usage:
8 config options command ...
9 commands:
10         --enable|-e option   Enable option
11         --disable|-d option  Disable option
12         --module|-m option   Turn option into a module
13         --set-str option string
14                              Set option to "string"
15         --set-val option value
16                              Set option to value
17         --state|-s option    Print state of option (n,y,m,undef)
18
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
28 options:
29         --file .config file to change (default .config)
30
31 config doesn't check the validity of the .config file. This is done at next
32  make time.
33 EOL
34         exit 1
35 }
36
37 checkarg() {
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 }
48
49 set_var() {
50         local name=$1 new=$2 before=$3
51
52         name_re="^($name=|# $name is not set)"
53         before_re="^($before=|# $before is not set)"
54         if test -n "$before" && grep -Eq "$before_re" "$FN"; then
55                 sed -ri "/$before_re/a $new" "$FN"
56         elif grep -Eq "$name_re" "$FN"; then
57                 sed -ri "s:$name_re.*:$new:" "$FN"
58         else
59                 echo "$new" >>"$FN"
60         fi
61 }
62
63 if [ "$1" = "--file" ]; then
64         FN="$2"
65         if [ "$FN" = "" ] ; then
66                 usage
67         fi
68         shift 2
69 else
70         FN=.config
71 fi
72
73 if [ "$1" = "" ] ; then
74         usage
75 fi
76
77 while [ "$1" != "" ] ; do
78         CMD="$1"
79         shift
80         case "$CMD" in
81         --refresh)
82                 ;;
83         --*-after)
84                 checkarg "$1"
85                 A=$ARG
86                 checkarg "$2"
87                 B=$ARG
88                 shift 2
89                 ;;
90         -*)
91                 checkarg "$1"
92                 shift
93                 ;;
94         esac
95         case "$CMD" in
96         --enable|-e)
97                 set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
98                 ;;
99
100         --disable|-d)
101                 set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
102                 ;;
103
104         --module|-m)
105                 set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
106                 ;;
107
108         --set-str)
109                 # sed swallows one level of escaping, so we need double-escaping
110                 set_var "CONFIG_$ARG" "CONFIG_$ARG=\"${1//\"/\\\\\"}\""
111                 shift
112                 ;;
113
114         --set-val)
115                 set_var "CONFIG_$ARG" "CONFIG_$ARG=$1"
116                 shift
117                 ;;
118
119         --state|-s)
120                 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                                 V="${V/%\"/}"
130                                 V="${V/\\\"/\"}"
131                                 echo "${V}"
132                         fi
133                 fi
134                 ;;
135
136         --enable-after|-E)
137                 set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
138                 ;;
139
140         --disable-after|-D)
141                 set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
142                 ;;
143
144         --module-after|-M)
145                 set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
146                 ;;
147
148         # undocumented because it ignores --file (fixme)
149         --refresh)
150                 yes "" | make oldconfig
151                 ;;
152
153         *)
154                 usage
155                 ;;
156         esac
157 done
158