Blame view

scripts/config 4.53 KB
8e54701ea   Andi Kleen   kconfig: add scri...
1
  #!/bin/bash
b24413180   Greg Kroah-Hartman   License cleanup: ...
2
  # SPDX-License-Identifier: GPL-2.0
8e54701ea   Andi Kleen   kconfig: add scri...
3
  # Manipulate options in a .config file from the command line
738777851   Clement Chauplannaz   scripts/config: r...
4
  myname=${0##*/}
f5ef2f7bf   Yann E. MORIN   scripts/config: a...
5
6
  # If no prefix forced, use the default CONFIG_
  CONFIG_="${CONFIG_-CONFIG_}"
8e54701ea   Andi Kleen   kconfig: add scri...
7
8
9
10
  usage() {
  	cat >&2 <<EOL
  Manipulate options in a .config file from the command line.
  Usage:
738777851   Clement Chauplannaz   scripts/config: r...
11
  $myname options command ...
8e54701ea   Andi Kleen   kconfig: add scri...
12
13
14
  commands:
  	--enable|-e option   Enable option
  	--disable|-d option  Disable option
1f990cf94   Michal Marek   kbuild: add gener...
15
  	--module|-m option   Turn option into a module
f0a6332ce   Jonas Aaberg   kbuild: add numer...
16
17
18
19
  	--set-str option string
  	                     Set option to "string"
  	--set-val option value
  	                     Set option to value
d5bfb6b38   Yann E. MORIN   scripts/config: a...
20
  	--undefine|-u option Undefine option
1f990cf94   Michal Marek   kbuild: add gener...
21
  	--state|-s option    Print state of option (n,y,m,undef)
8e54701ea   Andi Kleen   kconfig: add scri...
22
23
24
25
26
27
28
29
30
31
32
  
  	--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...
33
34
  	--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...
35

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

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

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