| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # Manipulate options in a .config file from the command line | 
 | 3 |  | 
 | 4 | usage() { | 
 | 5 | 	cat >&2 <<EOL | 
 | 6 | Manipulate options in a .config file from the command line. | 
 | 7 | Usage: | 
 | 8 | config options command ... | 
 | 9 | commands: | 
 | 10 | 	--enable|-e option   Enable option | 
 | 11 | 	--disable|-d option  Disable option | 
 | 12 | 	--module|-m option      Turn option into a module | 
 | 13 | 	--state|-s option       Print state of option (n,y,m,undef) | 
 | 14 |  | 
 | 15 | 	--enable-after|-E beforeopt option | 
 | 16 |                              Enable option directly after other option | 
 | 17 | 	--disable-after|-D beforeopt option | 
 | 18 |                              Disable option directly after other option | 
 | 19 | 	--module-after|-M beforeopt option | 
 | 20 |                              Turn option into module directly after other option | 
 | 21 |  | 
 | 22 | 	commands can be repeated multiple times | 
 | 23 |  | 
 | 24 | options: | 
 | 25 | 	--file .config file to change (default .config) | 
 | 26 |  | 
 | 27 | config doesn't check the validity of the .config file. This is done at next | 
 | 28 |  make time. | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 29 | EOL | 
 | 30 | 	exit 1 | 
 | 31 | } | 
 | 32 |  | 
 | 33 | checkarg() { | 
 | 34 | 	ARG="$1" | 
 | 35 | 	if [ "$ARG" = "" ] ; then | 
 | 36 | 		usage | 
 | 37 | 	fi | 
 | 38 | 	case "$ARG" in | 
 | 39 | 	CONFIG_*) | 
 | 40 | 		ARG="${ARG/CONFIG_/}" | 
 | 41 | 		;; | 
 | 42 | 	esac | 
 | 43 | 	ARG="`echo $ARG | tr a-z A-Z`" | 
 | 44 | } | 
 | 45 |  | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 46 | set_var() { | 
 | 47 | 	local name=$1 new=$2 before=$3 | 
 | 48 |  | 
 | 49 | 	name_re="^($name=|# $name is not set)" | 
 | 50 | 	before_re="^($before=|# $before is not set)" | 
 | 51 | 	if test -n "$before" && grep -Eq "$before_re" "$FN"; then | 
 | 52 | 		sed -ri "/$before_re/a $new" "$FN" | 
 | 53 | 	elif grep -Eq "$name_re" "$FN"; then | 
 | 54 | 		sed -ri "s:$name_re.*:$new:" "$FN" | 
 | 55 | 	else | 
 | 56 | 		echo "$new" >>"$FN" | 
 | 57 | 	fi | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 58 | } | 
 | 59 |  | 
 | 60 | if [ "$1" = "--file" ]; then | 
 | 61 | 	FN="$2" | 
 | 62 | 	if [ "$FN" = "" ] ; then | 
 | 63 | 		usage | 
 | 64 | 	fi | 
 | 65 | 	shift | 
 | 66 | 	shift | 
 | 67 | else | 
 | 68 | 	FN=.config | 
 | 69 | fi | 
 | 70 |  | 
| Andi Kleen | 2302e87 | 2009-01-07 22:33:15 +0100 | [diff] [blame] | 71 | if [ "$1" = "" ] ; then | 
 | 72 | 	usage | 
 | 73 | fi | 
 | 74 |  | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 75 | while [ "$1" != "" ] ; do | 
 | 76 | 	CMD="$1" | 
 | 77 | 	shift | 
 | 78 | 	case "$CMD" in | 
 | 79 | 	--enable|-e) | 
 | 80 | 		checkarg "$1" | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 81 | 		set_var "CONFIG_$ARG" "CONFIG_$ARG=y" | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 82 | 		shift | 
 | 83 | 		;; | 
 | 84 |  | 
 | 85 | 	--disable|-d) | 
 | 86 | 		checkarg "$1" | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 87 | 		set_var "CONFIG_$ARG" "# CONFIG_$ARG is not set" | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 88 | 		shift | 
 | 89 | 		;; | 
 | 90 |  | 
 | 91 | 	--module|-m) | 
 | 92 | 		checkarg "$1" | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 93 | 		set_var "CONFIG_$ARG" "CONFIG_$ARG=m" | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 94 | 		shift | 
 | 95 | 		;; | 
 | 96 |  | 
 | 97 | 	--state|-s) | 
 | 98 | 		checkarg "$1" | 
 | 99 | 		if grep -q "# CONFIG_$ARG is not set" $FN ; then | 
 | 100 | 			echo n | 
 | 101 | 		else | 
 | 102 | 			V="$(grep "^CONFIG_$ARG=" $FN)" | 
 | 103 | 			if [ $? != 0 ] ; then | 
 | 104 | 				echo undef | 
 | 105 | 			else | 
 | 106 | 				V="${V/CONFIG_$ARG=/}" | 
 | 107 | 				V="${V/\"/}" | 
 | 108 | 				echo "$V" | 
 | 109 | 			fi | 
 | 110 | 		fi | 
 | 111 | 		shift | 
 | 112 | 		;; | 
 | 113 |  | 
 | 114 | 	--enable-after|-E) | 
 | 115 | 		checkarg "$1" | 
 | 116 | 		A=$ARG | 
 | 117 | 		checkarg "$2" | 
 | 118 | 		B=$ARG | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 119 | 		set_var "CONFIG_$B" "CONFIG_$B=y" "CONFIG_$A" | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 120 | 		shift | 
 | 121 | 		shift | 
 | 122 | 		;; | 
 | 123 |  | 
 | 124 | 	--disable-after|-D) | 
 | 125 | 		checkarg "$1" | 
 | 126 | 		A=$ARG | 
 | 127 | 		checkarg "$2" | 
 | 128 | 		B=$ARG | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 129 | 		set_var "CONFIG_$B" "# CONFIG_$B is not set" "CONFIG_$A" | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 130 | 		shift | 
 | 131 | 		shift | 
 | 132 | 		;; | 
 | 133 |  | 
 | 134 | 	--module-after|-M) | 
 | 135 | 		checkarg "$1" | 
 | 136 | 		A=$ARG | 
 | 137 | 		checkarg "$2" | 
 | 138 | 		B=$ARG | 
| Michal Marek | 5664322 | 2009-06-14 22:48:07 +0200 | [diff] [blame^] | 139 | 		set_var "CONFIG_$B" "CONFIG_$B=m" "CONFIG_$A" | 
| Andi Kleen | 8e54701 | 2009-01-03 03:21:41 +0100 | [diff] [blame] | 140 | 		shift | 
 | 141 | 		shift | 
 | 142 | 		;; | 
 | 143 |  | 
 | 144 | 	# undocumented because it ignores --file (fixme) | 
 | 145 | 	--refresh) | 
 | 146 | 		yes "" | make oldconfig | 
 | 147 | 		;; | 
 | 148 |  | 
 | 149 | 	*) | 
 | 150 | 		usage | 
 | 151 | 		;; | 
 | 152 | 	esac | 
 | 153 | done | 
 | 154 |  |