|
| 1 | +#! /usr/bin/env bash |
| 2 | +function atparse { |
| 3 | + # Usage: |
| 4 | + # source atparse.bash # defines the "atparse" function; only do this once |
| 5 | + # atparse [ var1=value1 [ var2=value2 [...] ] ] < input_file > output_file |
| 6 | + # This function filters text from stdin to stdout. It scans for text sequences like: |
| 7 | + # @[varname] |
| 8 | + # And replaces them with the value of the corresponding ${varname} variable. |
| 9 | + # You can provide variables that are not set in bash by providing them on the command line. |
| 10 | + # If set -u is enabled, it will exit the process when a variable is empty or undefined via set -u. |
| 11 | + |
| 12 | + # Use __ in names to avoid clashing with variables in {var} blocks. |
| 13 | + local __text # current line of text being parsed, or the current command-line argument being parsed |
| 14 | + local __before # all text before the next @[...] option |
| 15 | + local __after # all text after the next @[...] option |
| 16 | + local __during # the contents of the @[...] option, including the @[ and ] |
| 17 | + local __set_x=":" # will be "set -x" if the calling script had that option enabled |
| 18 | + local __set_u=":" # will be "set -u" if the calling script had that option enabled |
| 19 | + local __set_e=":" # will be "set -e" if the calling script had that option enabled |
| 20 | + local __abort_on_undefined=NO # YES = script should abort if a variable is undefined, NO otherwise |
| 21 | + |
| 22 | + # Ensure "set -x -e -u" are all inactive, but remember if they |
| 23 | + # were active so we can reset them later. |
| 24 | + if [[ -o xtrace ]] ; then |
| 25 | + __set_x="set -x" |
| 26 | + fi |
| 27 | + if [[ -o errexit ]] ; then |
| 28 | + __set_e="set -e" |
| 29 | + fi |
| 30 | + if [[ -o nounset ]] ; then |
| 31 | + __set_u="set -u" |
| 32 | + fi |
| 33 | + set +eux |
| 34 | + |
| 35 | + # Allow setting variables on the atparse command line rather than the environment. |
| 36 | + # They will be local variables in this function. |
| 37 | + for __text in "$@" ; do |
| 38 | + if [[ $__text =~ ^([a-zA-Z][a-zA-Z0-9_]*)=(.*)$ ]] ; then |
| 39 | + eval "local ${BASH_REMATCH[1]}" |
| 40 | + eval "${BASH_REMATCH[1]}="'"${BASH_REMATCH[2]}"' |
| 41 | + else |
| 42 | + echo "ERROR: Ignoring invalid argument $__text" 1>&2 |
| 43 | + fi |
| 44 | + done |
| 45 | + |
| 46 | + # Loop over all lines of text. |
| 47 | + while [[ 1 == 1 ]] ; do |
| 48 | + # Read the next line of text. This will "fail" if no more text |
| 49 | + # is left OR if the last line lacks an end-of-line character. |
| 50 | + read -d '' -r __text |
| 51 | + |
| 52 | + # Stop when "read" reports it is done ($? -ne 0) AND the text is |
| 53 | + # non-empty (! -n "$__text"). This ensures we read the final line |
| 54 | + # even if it lacks an end-of-line character. |
| 55 | + if [[ $? -ne 0 ]] ; then |
| 56 | + if [[ -n "$__text" ]] ; then |
| 57 | + # Text remained, but it had no end-of-line. |
| 58 | + : |
| 59 | + else |
| 60 | + break |
| 61 | + fi |
| 62 | + fi |
| 63 | + # Search for strings like @[varname] or @['string'] or @[@] |
| 64 | + while [[ "$__text" =~ ^([^@]*)(@\[[a-zA-Z_][a-zA-Z_0-9]*\]|@\[\'[^\']*\'\]|@\[@\]|@)(.*) ]] ; do |
| 65 | + __before="${BASH_REMATCH[1]}" |
| 66 | + __during="${BASH_REMATCH[2]}" |
| 67 | + __after="${BASH_REMATCH[3]}" |
| 68 | + printf %s "$__before" |
| 69 | + # @['string'] inserts string |
| 70 | + if [[ "$__during" =~ ^@\[\'(.*)\'\]$ ]] ; then |
| 71 | + printf %s "${BASH_REMATCH[1]}" |
| 72 | + # @[@] inserts @ |
| 73 | + elif [[ "$__during" == '@[@]' ]] ; then |
| 74 | + printf @ |
| 75 | + # @[varname] inserts $varname |
| 76 | + elif [[ "$__during" =~ ^@\[([a-zA-Z_][a-zA-Z_0-9]*)\] ]] ; then |
| 77 | + # Flag unknown variables at this step only. |
| 78 | + if [[ ${__abort_on_undefined} == YES ]] ; then |
| 79 | + set -u |
| 80 | + fi |
| 81 | + eval 'printf %s "$'"${BASH_REMATCH[1]}"'"' |
| 82 | + if [[ ${__abort_on_undefined} == YES ]] ; then |
| 83 | + set +u |
| 84 | + fi |
| 85 | + # Unrecognized sequences are inserted verbatim. |
| 86 | + else |
| 87 | + printf '%s' "$__during" |
| 88 | + fi |
| 89 | + # Continue until we run out of text in this line. |
| 90 | + if [[ "$__after" == "$__text" ]] ; then |
| 91 | + break |
| 92 | + fi |
| 93 | + __text="$__after" |
| 94 | + done |
| 95 | + # Print the corrected text |
| 96 | + printf '%s\n' "$__text" |
| 97 | + done |
| 98 | + |
| 99 | + # Restore the calling script's shell options. |
| 100 | + eval "$__set_x" |
| 101 | + eval "$__set_u" |
| 102 | + eval "$__set_e" |
| 103 | +} |
| 104 | + |
| 105 | +function test_atparse { |
| 106 | + # Note that these cannot be local since they will be invisible |
| 107 | + # to atparse: |
| 108 | + testvar='[testvar]' |
| 109 | + var1='[var1]' |
| 110 | + var2='[var2]' |
| 111 | + var4='[var4]' |
| 112 | + ( cat<<\EOF ; echo -n "line with no end-of-line character [var4] = @[var4]" ) | atparse var3='**' |
| 113 | +Nothing special here. = @['Nothing special here.'] |
| 114 | +[testvar] = @[testvar] |
| 115 | +[var1] [var2] = @[var1] @[var2] |
| 116 | +** = @[var3] |
| 117 | +[var4] == @[var4] |
| 118 | +@ = @[@] = @['@'] |
| 119 | +@[undefined_variable_that_should_exit_script_if_set_minus_u_is_used] |
| 120 | +-n |
| 121 | + eval "export PE$c=\${PE$c:-0}" = @[' eval "export PE$c=\${PE$c:-0}"'] |
| 122 | +EOF |
| 123 | + echo " ... this text should be on the same line as the line with no end-of-line character" |
| 124 | + echo "After block, \$var3 = \"$var3\" should be empty" |
| 125 | +} |
0 commit comments