Blame view

scripts/config 4.63 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_}"
e461bc9f9   Jeremie Francois (on alpha)   scripts/config: a...
7
8
  # We use an uncommon delimiter for sed substitutions
  SED_DELIM=$(echo -en "\001")
8e54701ea   Andi Kleen   kconfig: add scri...
9
10
11
12
  usage() {
  	cat >&2 <<EOL
  Manipulate options in a .config file from the command line.
  Usage:
738777851   Clement Chauplannaz   scripts/config: r...
13
  $myname options command ...
8e54701ea   Andi Kleen   kconfig: add scri...
14
15
16
  commands:
  	--enable|-e option   Enable option
  	--disable|-d option  Disable option
1f990cf94   Michal Marek   kbuild: add gener...
17
  	--module|-m option   Turn option into a module
f0a6332ce   Jonas Aaberg   kbuild: add numer...
18
19
20
21
  	--set-str option string
  	                     Set option to "string"
  	--set-val option value
  	                     Set option to value
d5bfb6b38   Yann E. MORIN   scripts/config: a...
22
  	--undefine|-u option Undefine option
1f990cf94   Michal Marek   kbuild: add gener...
23
  	--state|-s option    Print state of option (n,y,m,undef)
8e54701ea   Andi Kleen   kconfig: add scri...
24
25
26
27
28
29
30
31
32
33
34
  
  	--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...
35
36
  	--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...
37

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

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

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