xref: /linux-6.15/scripts/config (revision 4edc7e32)
1#!/bin/bash
2# Manipulate options in a .config file from the command line
3
4usage() {
5	cat >&2 <<EOL
6Manipulate options in a .config file from the command line.
7Usage:
8config options command ...
9commands:
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
28options:
29	--file config-file   .config file to change (default .config)
30	--keep-case|-k       Keep next symbols' case (dont' upper-case it)
31
32config doesn't check the validity of the .config file. This is done at next
33make time.
34
35By default, config will upper-case the given symbol. Use --keep-case to keep
36the case of all following symbols unchanged.
37EOL
38	exit 1
39}
40
41checkarg() {
42	ARG="$1"
43	if [ "$ARG" = "" ] ; then
44		usage
45	fi
46	case "$ARG" in
47	CONFIG_*)
48		ARG="${ARG/CONFIG_/}"
49		;;
50	esac
51	if [ "$MUNGE_CASE" = "yes" ] ; then
52		ARG="`echo $ARG | tr a-z A-Z`"
53	fi
54}
55
56set_var() {
57	local name=$1 new=$2 before=$3
58
59	name_re="^($name=|# $name is not set)"
60	before_re="^($before=|# $before is not set)"
61	if test -n "$before" && grep -Eq "$before_re" "$FN"; then
62		sed -ri "/$before_re/a $new" "$FN"
63	elif grep -Eq "$name_re" "$FN"; then
64		sed -ri "s:$name_re.*:$new:" "$FN"
65	else
66		echo "$new" >>"$FN"
67	fi
68}
69
70if [ "$1" = "--file" ]; then
71	FN="$2"
72	if [ "$FN" = "" ] ; then
73		usage
74	fi
75	shift 2
76else
77	FN=.config
78fi
79
80if [ "$1" = "" ] ; then
81	usage
82fi
83
84MUNGE_CASE=yes
85while [ "$1" != "" ] ; do
86	CMD="$1"
87	shift
88	case "$CMD" in
89	--keep-case|-k)
90		MUNGE_CASE=no
91		shift
92		continue
93		;;
94	--refresh)
95		;;
96	--*-after)
97		checkarg "$1"
98		A=$ARG
99		checkarg "$2"
100		B=$ARG
101		shift 2
102		;;
103	-*)
104		checkarg "$1"
105		shift
106		;;
107	esac
108	case "$CMD" in
109	--enable|-e)
110		set_var "CONFIG_$ARG" "CONFIG_$ARG=y"
111		;;
112
113	--disable|-d)
114		set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set"
115		;;
116
117	--module|-m)
118		set_var "CONFIG_$ARG" "CONFIG_$ARG=m"
119		;;
120
121	--set-str)
122		# sed swallows one level of escaping, so we need double-escaping
123		set_var "CONFIG_$ARG" "CONFIG_$ARG=\"${1//\"/\\\\\"}\""
124		shift
125		;;
126
127	--set-val)
128		set_var "CONFIG_$ARG" "CONFIG_$ARG=$1"
129		shift
130		;;
131
132	--state|-s)
133		if grep -q "# CONFIG_$ARG is not set" $FN ; then
134			echo n
135		else
136			V="$(grep "^CONFIG_$ARG=" $FN)"
137			if [ $? != 0 ] ; then
138				echo undef
139			else
140				V="${V/#CONFIG_$ARG=/}"
141				V="${V/#\"/}"
142				V="${V/%\"/}"
143				V="${V/\\\"/\"}"
144				echo "${V}"
145			fi
146		fi
147		;;
148
149	--enable-after|-E)
150		set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A"
151		;;
152
153	--disable-after|-D)
154		set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A"
155		;;
156
157	--module-after|-M)
158		set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A"
159		;;
160
161	# undocumented because it ignores --file (fixme)
162	--refresh)
163		yes "" | make oldconfig
164		;;
165
166	*)
167		usage
168		;;
169	esac
170done
171
172