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