diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2023-04-24 07:48:35 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2023-04-24 07:48:35 +0200 |
commit | a0c2aee4b238912f8fa45af5d90d1ddda25e14a3 (patch) | |
tree | eeea914cb5fad652b939041e2ad03046dee15c8f | |
parent | bb822df58fdb702d973fcdc8e5f3a2491d67bd14 (diff) |
Support fully-quoted variables on command line
GRUB rewrites x="y" as "x=y".
-rwxr-xr-x | buildos | 24 | ||||
-rwxr-xr-x | init | 24 |
2 files changed, 42 insertions, 6 deletions
@@ -59,6 +59,11 @@ info "starting build os monitor..." # foo='foo fox' # bar="bar 'box'" # +# Or (as rewritten by GRUB): +# +# 'foo=foo fox' +# "bar=bar 'box'" +# # First we separete quoted variables and arguments with newlines (giving # priority to assignments). Then we replace whitespaces with newline on # lines that don't contain quites. Finally, we clean up by removing blank @@ -81,11 +86,24 @@ declare -A toolchains toolchains["default"]="" for v in "${cmdline[@]}"; do - var="$(sed -n -re 's/^buildos\.([^=]+)=.*$/\1/p' <<<"$v")" # Extract name. + + # Rewrite "x=y" as x="y" (as well as the single-quote variant). + # + v1="$(sed -n -re "s/^\"([^= ]+)=(.*)\"\$/\1=\"\2\"/p" <<<"$v")" + if [ -n "$v1" ]; then + v="$v1" + else + v1="$(sed -n -re "s/^'([^= ]+)=(.*)'\$/\1='\2'/p" <<<"$v")" + if [ -n "$v1" ]; then + v="$v1" + fi + fi + + var="$(sed -n -re 's/^buildos\.([^= ]+)=.*$/\1/p' <<<"$v")" # Extract name. if [ -n "$var" ]; then - val="$(sed -re 's/^[^=]+=(.*)$/\1/' <<<"$v")" # Extract value. - val="$(sed -re "s/^('(.*)'|\"(.*)\")$/\2\3/" <<<"$val")" # Strip quoted. + val="$(sed -re 's/^[^= ]+=(.*)$/\1/' <<<"$v")" # Extract value. + val="$(sed -re "s/^('(.*)'|\"(.*)\")\$/\2\3/" <<<"$val")" # Strip quoted. # Recognize some variables as arrays. # @@ -104,6 +104,11 @@ sensors-detect --auto # foo='foo fox' # bar="bar 'box'" # +# Or (as rewritten by GRUB): +# +# 'foo=foo fox' +# "bar=bar 'box'" +# # First we separete quoted variables and arguments with newlines (giving # priority to assignments). Then we replace whitespaces with newline on # lines that don't contain quotes. Finally, clean up by removing blank @@ -120,11 +125,24 @@ readarray -t cmdline < <(cat /proc/cmdline | \ # info "command line:" for v in "${cmdline[@]}"; do - var="$(sed -n -re 's/^buildos\.([^=]+)=.*$/\1/p' <<<"$v")" # Extract name. + + # Rewrite "x=y" as x="y" (as well as the single-quote variant). + # + v1="$(sed -n -re "s/^\"([^= ]+)=(.*)\"\$/\1=\"\2\"/p" <<<"$v")" + if [ -n "$v1" ]; then + v="$v1" + else + v1="$(sed -n -re "s/^'([^= ]+)=(.*)'\$/\1='\2'/p" <<<"$v")" + if [ -n "$v1" ]; then + v="$v1" + fi + fi + + var="$(sed -n -re 's/^buildos\.([^= ]+)=.*$/\1/p' <<<"$v")" # Extract name. if [ -n "$var" ]; then - val="$(sed -re 's/^[^=]+=(.*)$/\1/' <<<"$v")" # Extract value. - val="$(sed -re "s/^('(.*)'|\"(.*)\")$/\2\3/" <<<"$val")" # Strip quoted. + val="$(sed -re 's/^[^= ]+=(.*)$/\1/' <<<"$v")" # Extract value. + val="$(sed -re "s/^('(.*)'|\"(.*)\")\$/\2\3/" <<<"$val")" # Strip quoted. info " $var=$val" # If the variable contains a dot, then it is a toolchain variable and we |