diff options
Diffstat (limited to 'scripts/config')
| -rwxr-xr-x | scripts/config | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/scripts/config b/scripts/config index a65ecbbdd32a..68041793698c 100755 --- a/scripts/config +++ b/scripts/config | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | #!/bin/bash | 1 | #!/bin/bash |
| 2 | # Manipulate options in a .config file from the command line | 2 | # Manipulate options in a .config file from the command line |
| 3 | 3 | ||
| 4 | myname=${0##*/} | ||
| 5 | |||
| 4 | # If no prefix forced, use the default CONFIG_ | 6 | # If no prefix forced, use the default CONFIG_ |
| 5 | CONFIG_="${CONFIG_-CONFIG_}" | 7 | CONFIG_="${CONFIG_-CONFIG_}" |
| 6 | 8 | ||
| @@ -8,7 +10,7 @@ usage() { | |||
| 8 | cat >&2 <<EOL | 10 | cat >&2 <<EOL |
| 9 | Manipulate options in a .config file from the command line. | 11 | Manipulate options in a .config file from the command line. |
| 10 | Usage: | 12 | Usage: |
| 11 | config options command ... | 13 | $myname options command ... |
| 12 | commands: | 14 | commands: |
| 13 | --enable|-e option Enable option | 15 | --enable|-e option Enable option |
| 14 | --disable|-d option Disable option | 16 | --disable|-d option Disable option |
| @@ -33,14 +35,14 @@ options: | |||
| 33 | --file config-file .config file to change (default .config) | 35 | --file config-file .config file to change (default .config) |
| 34 | --keep-case|-k Keep next symbols' case (dont' upper-case it) | 36 | --keep-case|-k Keep next symbols' case (dont' upper-case it) |
| 35 | 37 | ||
| 36 | config doesn't check the validity of the .config file. This is done at next | 38 | $myname doesn't check the validity of the .config file. This is done at next |
| 37 | make time. | 39 | make time. |
| 38 | 40 | ||
| 39 | By default, config will upper-case the given symbol. Use --keep-case to keep | 41 | By default, $myname will upper-case the given symbol. Use --keep-case to keep |
| 40 | the case of all following symbols unchanged. | 42 | the case of all following symbols unchanged. |
| 41 | 43 | ||
| 42 | config uses 'CONFIG_' as the default symbol prefix. Set the environment | 44 | $myname uses 'CONFIG_' as the default symbol prefix. Set the environment |
| 43 | variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" config ... | 45 | variable CONFIG_ to the prefix to use. Eg.: CONFIG_="FOO_" $myname ... |
| 44 | EOL | 46 | EOL |
| 45 | exit 1 | 47 | exit 1 |
| 46 | } | 48 | } |
| @@ -60,15 +62,52 @@ checkarg() { | |||
| 60 | fi | 62 | fi |
| 61 | } | 63 | } |
| 62 | 64 | ||
| 65 | txt_append() { | ||
| 66 | local anchor="$1" | ||
| 67 | local insert="$2" | ||
| 68 | local infile="$3" | ||
| 69 | local tmpfile="$infile.swp" | ||
| 70 | |||
| 71 | # sed append cmd: 'a\' + newline + text + newline | ||
| 72 | cmd="$(printf "a\\%b$insert" "\n")" | ||
| 73 | |||
| 74 | sed -e "/$anchor/$cmd" "$infile" >"$tmpfile" | ||
| 75 | # replace original file with the edited one | ||
| 76 | mv "$tmpfile" "$infile" | ||
| 77 | } | ||
| 78 | |||
| 79 | txt_subst() { | ||
| 80 | local before="$1" | ||
| 81 | local after="$2" | ||
| 82 | local infile="$3" | ||
| 83 | local tmpfile="$infile.swp" | ||
| 84 | |||
| 85 | sed -e "s:$before:$after:" "$infile" >"$tmpfile" | ||
| 86 | # replace original file with the edited one | ||
| 87 | mv "$tmpfile" "$infile" | ||
| 88 | } | ||
| 89 | |||
| 90 | txt_delete() { | ||
| 91 | local text="$1" | ||
| 92 | local infile="$2" | ||
| 93 | local tmpfile="$infile.swp" | ||
| 94 | |||
| 95 | sed -e "/$text/d" "$infile" >"$tmpfile" | ||
| 96 | # replace original file with the edited one | ||
| 97 | mv "$tmpfile" "$infile" | ||
| 98 | } | ||
| 99 | |||
| 63 | set_var() { | 100 | set_var() { |
| 64 | local name=$1 new=$2 before=$3 | 101 | local name=$1 new=$2 before=$3 |
| 65 | 102 | ||
| 66 | name_re="^($name=|# $name is not set)" | 103 | name_re="^($name=|# $name is not set)" |
| 67 | before_re="^($before=|# $before is not set)" | 104 | before_re="^($before=|# $before is not set)" |
| 68 | if test -n "$before" && grep -Eq "$before_re" "$FN"; then | 105 | if test -n "$before" && grep -Eq "$before_re" "$FN"; then |
| 69 | sed -ri "/$before_re/a $new" "$FN" | 106 | txt_append "^$before=" "$new" "$FN" |
| 107 | txt_append "^# $before is not set" "$new" "$FN" | ||
| 70 | elif grep -Eq "$name_re" "$FN"; then | 108 | elif grep -Eq "$name_re" "$FN"; then |
| 71 | sed -ri "s:$name_re.*:$new:" "$FN" | 109 | txt_subst "^$name=.*" "$new" "$FN" |
| 110 | txt_subst "^# $name is not set" "$new" "$FN" | ||
| 72 | else | 111 | else |
| 73 | echo "$new" >>"$FN" | 112 | echo "$new" >>"$FN" |
| 74 | fi | 113 | fi |
| @@ -77,7 +116,8 @@ set_var() { | |||
| 77 | undef_var() { | 116 | undef_var() { |
| 78 | local name=$1 | 117 | local name=$1 |
| 79 | 118 | ||
| 80 | sed -ri "/^($name=|# $name is not set)/d" "$FN" | 119 | txt_delete "^$name=" "$FN" |
| 120 | txt_delete "^# $name is not set" "$FN" | ||
| 81 | } | 121 | } |
| 82 | 122 | ||
| 83 | if [ "$1" = "--file" ]; then | 123 | if [ "$1" = "--file" ]; then |
