Blame view

scripts/config 4.49 KB
8e54701ea   Andi Kleen   kconfig: add scri...
1
2
  #!/bin/bash
  # Manipulate options in a .config file from the command line
738777851   Clement Chauplannaz   scripts/config: r...
3
  myname=${0##*/}
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
4
5
  # If no prefix forced, use the default CONFIG_
  CONFIG_="${CONFIG_-CONFIG_}"
8e54701ea   Andi Kleen   kconfig: add scri...
6
7
8
9
  usage() {
  	cat >&2 <<EOL
  Manipulate options in a .config file from the command line.
  Usage:
738777851   Clement Chauplannaz   scripts/config: r...
10
  $myname options command ...
8e54701ea   Andi Kleen   kconfig: add scri...
11
12
13
  commands:
  	--enable|-e option   Enable option
  	--disable|-d option  Disable option
1f990cf94   Michal Marek   kbuild: add gener...
14
  	--module|-m option   Turn option into a module
f0a6332ce   Jonas Aaberg   kbuild: add numer...
15
16
17
18
  	--set-str option string
  	                     Set option to "string"
  	--set-val option value
  	                     Set option to value
d5bfb6b38   Yann E. MORIN   scripts/config: a...
19
  	--undefine|-u option Undefine option
1f990cf94   Michal Marek   kbuild: add gener...
20
  	--state|-s option    Print state of option (n,y,m,undef)
8e54701ea   Andi Kleen   kconfig: add scri...
21
22
23
24
25
26
27
28
29
30
31
  
  	--enable-after|-E beforeopt option
                               Enable option directly after other option
  	--disable-after|-D beforeopt option
                               Disable option directly after other option
  	--module-after|-M beforeopt option
                               Turn option into module directly after other option
  
  	commands can be repeated multiple times
  
  options:
4edc7e32a   Yann E. MORIN   scripts/config: a...
32
33
  	--file config-file   .config file to change (default .config)
  	--keep-case|-k       Keep next symbols' case (dont' upper-case it)
8e54701ea   Andi Kleen   kconfig: add scri...
34

738777851   Clement Chauplannaz   scripts/config: r...
35
  $myname doesn't check the validity of the .config file. This is done at next
4edc7e32a   Yann E. MORIN   scripts/config: a...
36
  make time.
738777851   Clement Chauplannaz   scripts/config: r...
37
  By default, $myname will upper-case the given symbol. Use --keep-case to keep
4edc7e32a   Yann E. MORIN   scripts/config: a...
38
  the case of all following symbols unchanged.
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
39

738777851   Clement Chauplannaz   scripts/config: r...
40
41
  $myname uses 'CONFIG_' as the default symbol prefix. Set the environment
  variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
8e54701ea   Andi Kleen   kconfig: add scri...
42
43
44
45
46
47
48
49
50
51
  EOL
  	exit 1
  }
  
  checkarg() {
  	ARG="$1"
  	if [ "$ARG" = "" ] ; then
  		usage
  	fi
  	case "$ARG" in
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
52
53
  	${CONFIG_}*)
  		ARG="${ARG/${CONFIG_}/}"
8e54701ea   Andi Kleen   kconfig: add scri...
54
55
  		;;
  	esac
4edc7e32a   Yann E. MORIN   scripts/config: a...
56
57
58
  	if [ "$MUNGE_CASE" = "yes" ] ; then
  		ARG="`echo $ARG | tr a-z A-Z`"
  	fi
8e54701ea   Andi Kleen   kconfig: add scri...
59
  }
83e8b90e1   Clement Chauplannaz   scripts/config: u...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  txt_append() {
  	local anchor="$1"
  	local insert="$2"
  	local infile="$3"
  	local tmpfile="$infile.swp"
  
  	# sed append cmd: 'a\' + newline + text + newline
  	cmd="$(printf "a\\%b$insert" "
  ")"
  
  	sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
  	# replace original file with the edited one
  	mv "$tmpfile" "$infile"
  }
  
  txt_subst() {
  	local before="$1"
  	local after="$2"
  	local infile="$3"
  	local tmpfile="$infile.swp"
86eb78188   Clement Chauplannaz   scripts/config: f...
80
  	sed -e "s:$before:$after:" "$infile" >"$tmpfile"
83e8b90e1   Clement Chauplannaz   scripts/config: u...
81
82
83
84
85
86
87
88
89
90
91
92
93
  	# replace original file with the edited one
  	mv "$tmpfile" "$infile"
  }
  
  txt_delete() {
  	local text="$1"
  	local infile="$2"
  	local tmpfile="$infile.swp"
  
  	sed -e "/$text/d" "$infile" >"$tmpfile"
  	# replace original file with the edited one
  	mv "$tmpfile" "$infile"
  }
566432224   Michal Marek   kbuild: handle no...
94
95
96
97
98
99
  set_var() {
  	local name=$1 new=$2 before=$3
  
  	name_re="^($name=|# $name is not set)"
  	before_re="^($before=|# $before is not set)"
  	if test -n "$before" && grep -Eq "$before_re" "$FN"; then
83e8b90e1   Clement Chauplannaz   scripts/config: u...
100
101
  		txt_append "^$before=" "$new" "$FN"
  		txt_append "^# $before is not set" "$new" "$FN"
566432224   Michal Marek   kbuild: handle no...
102
  	elif grep -Eq "$name_re" "$FN"; then
83e8b90e1   Clement Chauplannaz   scripts/config: u...
103
104
  		txt_subst "^$name=.*" "$new" "$FN"
  		txt_subst "^# $name is not set" "$new" "$FN"
566432224   Michal Marek   kbuild: handle no...
105
106
107
  	else
  		echo "$new" >>"$FN"
  	fi
8e54701ea   Andi Kleen   kconfig: add scri...
108
  }
d5bfb6b38   Yann E. MORIN   scripts/config: a...
109
110
  undef_var() {
  	local name=$1
83e8b90e1   Clement Chauplannaz   scripts/config: u...
111
112
  	txt_delete "^$name=" "$FN"
  	txt_delete "^# $name is not set" "$FN"
d5bfb6b38   Yann E. MORIN   scripts/config: a...
113
  }
8e54701ea   Andi Kleen   kconfig: add scri...
114
115
116
117
118
  if [ "$1" = "--file" ]; then
  	FN="$2"
  	if [ "$FN" = "" ] ; then
  		usage
  	fi
47312d2cf   Michal Marek   kbuild: simplify ...
119
  	shift 2
8e54701ea   Andi Kleen   kconfig: add scri...
120
121
122
  else
  	FN=.config
  fi
2302e8730   Andi Kleen   kbuild: print usa...
123
124
125
  if [ "$1" = "" ] ; then
  	usage
  fi
4edc7e32a   Yann E. MORIN   scripts/config: a...
126
  MUNGE_CASE=yes
8e54701ea   Andi Kleen   kconfig: add scri...
127
128
129
130
  while [ "$1" != "" ] ; do
  	CMD="$1"
  	shift
  	case "$CMD" in
4edc7e32a   Yann E. MORIN   scripts/config: a...
131
132
  	--keep-case|-k)
  		MUNGE_CASE=no
4edc7e32a   Yann E. MORIN   scripts/config: a...
133
134
  		continue
  		;;
47312d2cf   Michal Marek   kbuild: simplify ...
135
136
  	--refresh)
  		;;
57a9c7609   Clement Chauplannaz   scripts/config: f...
137
  	--*-after|-E|-D|-M)
47312d2cf   Michal Marek   kbuild: simplify ...
138
139
140
141
142
143
  		checkarg "$1"
  		A=$ARG
  		checkarg "$2"
  		B=$ARG
  		shift 2
  		;;
45f53cc90   Andi Kleen   Kconfig: fix sing...
144
  	-*)
8e54701ea   Andi Kleen   kconfig: add scri...
145
  		checkarg "$1"
8e54701ea   Andi Kleen   kconfig: add scri...
146
147
  		shift
  		;;
47312d2cf   Michal Marek   kbuild: simplify ...
148
149
150
  	esac
  	case "$CMD" in
  	--enable|-e)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
151
  		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
47312d2cf   Michal Marek   kbuild: simplify ...
152
  		;;
8e54701ea   Andi Kleen   kconfig: add scri...
153
154
  
  	--disable|-d)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
155
  		set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
8e54701ea   Andi Kleen   kconfig: add scri...
156
157
158
  		;;
  
  	--module|-m)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
159
  		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
8e54701ea   Andi Kleen   kconfig: add scri...
160
  		;;
1f990cf94   Michal Marek   kbuild: add gener...
161
  	--set-str)
d6686da81   Yann E. MORIN   scripts/config: p...
162
  		# sed swallows one level of escaping, so we need double-escaping
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
163
  		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
1f990cf94   Michal Marek   kbuild: add gener...
164
165
  		shift
  		;;
f0a6332ce   Jonas Aaberg   kbuild: add numer...
166
  	--set-val)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
167
  		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
f0a6332ce   Jonas Aaberg   kbuild: add numer...
168
169
  		shift
  		;;
d5bfb6b38   Yann E. MORIN   scripts/config: a...
170
171
172
  	--undefine|-u)
  		undef_var "${CONFIG_}$ARG"
  		;;
f0a6332ce   Jonas Aaberg   kbuild: add numer...
173

8e54701ea   Andi Kleen   kconfig: add scri...
174
  	--state|-s)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
175
  		if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
8e54701ea   Andi Kleen   kconfig: add scri...
176
177
  			echo n
  		else
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
178
  			V="$(grep "^${CONFIG_}$ARG=" $FN)"
8e54701ea   Andi Kleen   kconfig: add scri...
179
180
181
  			if [ $? != 0 ] ; then
  				echo undef
  			else
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
182
  				V="${V/#${CONFIG_}$ARG=/}"
d6686da81   Yann E. MORIN   scripts/config: p...
183
184
  				V="${V/#\"/}"
  				V="${V/%\"/}"
1925a276a   Yann E. MORIN   scripts/config: f...
185
  				V="${V//\\\"/\"}"
d6686da81   Yann E. MORIN   scripts/config: p...
186
  				echo "${V}"
8e54701ea   Andi Kleen   kconfig: add scri...
187
188
  			fi
  		fi
8e54701ea   Andi Kleen   kconfig: add scri...
189
190
191
  		;;
  
  	--enable-after|-E)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
192
  		set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
8e54701ea   Andi Kleen   kconfig: add scri...
193
194
195
  		;;
  
  	--disable-after|-D)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
196
  		set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
8e54701ea   Andi Kleen   kconfig: add scri...
197
198
199
  		;;
  
  	--module-after|-M)
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
200
  		set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
8e54701ea   Andi Kleen   kconfig: add scri...
201
202
203
204
205
206
207
208
209
210
211
212
  		;;
  
  	# undocumented because it ignores --file (fixme)
  	--refresh)
  		yes "" | make oldconfig
  		;;
  
  	*)
  		usage
  		;;
  	esac
  done