config 3.7 KB
Newer Older
1 2 3
#!/bin/bash
# Manipulate options in a .config file from the command line

4 5
myname=${0##*/}

6 7 8
# If no prefix forced, use the default CONFIG_
CONFIG_="${CONFIG_-CONFIG_}"

9 10 11 12
usage() {
	cat >&2 <<EOL
Manipulate options in a .config file from the command line.
Usage:
13
$myname options command ...
14 15 16
commands:
	--enable|-e option   Enable option
	--disable|-d option  Disable option
17
	--module|-m option   Turn option into a module
18 19 20 21
	--set-str option string
	                     Set option to "string"
	--set-val option value
	                     Set option to value
22
	--undefine|-u option Undefine option
23
	--state|-s option    Print state of option (n,y,m,undef)
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:
35 36
	--file config-file   .config file to change (default .config)
	--keep-case|-k       Keep next symbols' case (dont' upper-case it)
37

38
$myname doesn't check the validity of the .config file. This is done at next
39 40
make time.

41
By default, $myname will upper-case the given symbol. Use --keep-case to keep
42
the case of all following symbols unchanged.
43

44 45
$myname uses 'CONFIG_' as the default symbol prefix. Set the environment
variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ...
46 47 48 49 50 51 52 53 54 55
EOL
	exit 1
}

checkarg() {
	ARG="$1"
	if [ "$ARG" = "" ] ; then
		usage
	fi
	case "$ARG" in
56 57
	${CONFIG_}*)
		ARG="${ARG/${CONFIG_}/}"
58 59
		;;
	esac
60 61 62
	if [ "$MUNGE_CASE" = "yes" ] ; then
		ARG="`echo $ARG | tr a-z A-Z`"
	fi
63 64
}

65 66 67 68 69 70 71 72 73 74 75 76
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
		sed -ri "/$before_re/a $new" "$FN"
	elif grep -Eq "$name_re" "$FN"; then
		sed -ri "s:$name_re.*:$new:" "$FN"
	else
		echo "$new" >>"$FN"
	fi
77 78
}

79 80 81 82 83 84
undef_var() {
	local name=$1

	sed -ri "/^($name=|# $name is not set)/d" "$FN"
}

85 86 87 88 89
if [ "$1" = "--file" ]; then
	FN="$2"
	if [ "$FN" = "" ] ; then
		usage
	fi
90
	shift 2
91 92 93 94
else
	FN=.config
fi

95 96 97 98
if [ "$1" = "" ] ; then
	usage
fi

99
MUNGE_CASE=yes
100 101 102 103
while [ "$1" != "" ] ; do
	CMD="$1"
	shift
	case "$CMD" in
104 105 106 107
	--keep-case|-k)
		MUNGE_CASE=no
		continue
		;;
108 109
	--refresh)
		;;
110
	--*-after|-E|-D|-M)
111 112 113 114 115 116
		checkarg "$1"
		A=$ARG
		checkarg "$2"
		B=$ARG
		shift 2
		;;
117
	-*)
118 119 120
		checkarg "$1"
		shift
		;;
121 122 123
	esac
	case "$CMD" in
	--enable|-e)
124
		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=y"
125
		;;
126 127

	--disable|-d)
128
		set_var "${CONFIG_}$ARG" "# ${CONFIG_}$ARG is not set"
129 130 131
		;;

	--module|-m)
132
		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=m"
133 134
		;;

135
	--set-str)
136
		# sed swallows one level of escaping, so we need double-escaping
137
		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
138 139 140
		shift
		;;

141
	--set-val)
142
		set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=$1"
143 144
		shift
		;;
145 146 147
	--undefine|-u)
		undef_var "${CONFIG_}$ARG"
		;;
148

149
	--state|-s)
150
		if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
151 152
			echo n
		else
153
			V="$(grep "^${CONFIG_}$ARG=" $FN)"
154 155 156
			if [ $? != 0 ] ; then
				echo undef
			else
157
				V="${V/#${CONFIG_}$ARG=/}"
158 159
				V="${V/#\"/}"
				V="${V/%\"/}"
160
				V="${V//\\\"/\"}"
161
				echo "${V}"
162 163 164 165 166
			fi
		fi
		;;

	--enable-after|-E)
167
		set_var "${CONFIG_}$B" "${CONFIG_}$B=y" "${CONFIG_}$A"
168 169 170
		;;

	--disable-after|-D)
171
		set_var "${CONFIG_}$B" "# ${CONFIG_}$B is not set" "${CONFIG_}$A"
172 173 174
		;;

	--module-after|-M)
175
		set_var "${CONFIG_}$B" "${CONFIG_}$B=m" "${CONFIG_}$A"
176 177 178 179 180 181 182 183 184 185 186 187 188
		;;

	# undocumented because it ignores --file (fixme)
	--refresh)
		yes "" | make oldconfig
		;;

	*)
		usage
		;;
	esac
done