| 1 | #!/bin/bash |
| 2 | |
| 3 | function ble/history:bash/resolve-multiline/.worker { |
| 4 | local apos=\' |
| 5 | local HISTTIMEFORMAT=__ble_ext__ |
| 6 | local -x HISTORY_SOURCE=$1 |
| 7 | local -x dev_stdin_available=0 |
| 8 | [[ -r /dev/stdin && ! -d /dev/stdin ]] && dev_stdin_available=1 |
| 9 | builtin history $arg_count | ble/bin/awk -v apos="$apos" ' |
| 10 | BEGIN { |
| 11 | n = 0; |
| 12 | |
| 13 | FILE_HISTORY = ENVIRON["HISTORY_SOURCE"]; |
| 14 | dev_stdin_available = ENVIRON["dev_stdin_available"]; |
| 15 | q = apos; |
| 16 | Q = apos "\\" apos apos; |
| 17 | |
| 18 | print "builtin history -c" > FILE_HISTORY; |
| 19 | multiline_count = 0; |
| 20 | modification_count = 0; |
| 21 | } |
| 22 | |
| 23 | function write_scalar(line) { |
| 24 | scalar_array[scalar_count++] = line; |
| 25 | } |
| 26 | function write_complex(value) { |
| 27 | write_flush(); |
| 28 | print "builtin history -s -- " value > FILE_HISTORY; |
| 29 | } |
| 30 | function write_flush(_, i, text) { |
| 31 | if (scalar_count == 0) return; |
| 32 | if (dev_stdin_available && scalar_count >= 8) { |
| 33 | text = scalar_array[0]; |
| 34 | for (i = 1; i < scalar_count; i++) |
| 35 | text = text "\n" scalar_array[i]; |
| 36 | gsub(/'$apos'/, Q, text); |
| 37 | print "builtin history -r /dev/stdin <<< " q text q > FILE_HISTORY; |
| 38 | } else { |
| 39 | for (i = 0; i < scalar_count; i++) { |
| 40 | text = scalar_array[i]; |
| 41 | gsub(/'$apos'/, Q, text); |
| 42 | print "builtin history -s -- " q text q > FILE_HISTORY; |
| 43 | } |
| 44 | } |
| 45 | scalar_count = 0; |
| 46 | } |
| 47 | |
| 48 | function flush_line() { |
| 49 | if (n < 1) return; |
| 50 | |
| 51 | if (entry ~ /^eval -- \$'$apos'([^'$apos'\\]|\\.)*'$apos'$/) { |
| 52 | multiline_count++; |
| 53 | modification_count++; |
| 54 | write_complex(substr(entry, 9)); |
| 55 | } else if (n > 1) { |
| 56 | multiline_count++; |
| 57 | gsub(/'$apos'/, Q, entry); |
| 58 | write_complex(q entry q); |
| 59 | } else { |
| 60 | write_scalar(entry); |
| 61 | } |
| 62 | |
| 63 | n = 0; |
| 64 | entry = ""; |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | if (sub(/^ *[0-9]+\*? +(__ble_ext__|\?\?)/, "", $0)) |
| 69 | flush_line(); |
| 70 | entry = ++n == 1 ? $0 : entry "\n" $0; |
| 71 | } |
| 72 | |
| 73 | END { |
| 74 | flush_line(); |
| 75 | write_flush(); |
| 76 | print "builtin history -a /dev/null" > FILE_HISTORY |
| 77 | print "multiline_count=" multiline_count; |
| 78 | print "modification_count=" modification_count; |
| 79 | } |
| 80 | ' |
| 81 | } |
| 82 | function ble/history:bash/resolve-multiline { |
| 83 | local foutput=history_resolve_multiline.out |
| 84 | local multiline_count=0 modification_count=0 |
| 85 | eval -- $(ble/history:bash/resolve-multiline/.worker "$foutput" 2>/dev/null) |
| 86 | echo "modification_count=$modification_count" |
| 87 | if ((modification_count)); then |
| 88 | local HISTCONTROL= HISTSIZE= HISTIGNORE= |
| 89 | time source -- "$foutput" |
| 90 | fi |
| 91 | } |
| 92 | ble/history:bash/resolve-multiline |