omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / osh / vectors / bash_verify.sh
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # bash_verify.sh: prove the expectations in bash_vectors.tsv against REAL bash 5.2 (not the reference lexer). |
| 18 | # For each vector it runs the input under bash and the "equivalent" script (the continuation-free form, or the |
| 19 | # script rendered from the expected tokens when the equiv column is "-") and requires identical transcripts |
| 20 | # (stdout, stderr, and the files written). Usage: bash_verify.sh [bash_vectors.tsv] Writes bash_verified.log. |
| 21 | set -u |
| 22 | # omega#344: the reference run is pinned, so a caller's locale, PATH, TZ or exported variables cannot change bash's answers |
| 23 | if [ "${BASH_VERIFY_PINNED:-}" != 1 ]; then |
| 24 | exec env -i BASH_VERIFY_PINNED=1 PATH=/usr/bin:/bin LC_ALL=C TZ=UTC TMPDIR="${TMPDIR:-/tmp}" bash "$0" "$@" |
| 25 | fi |
| 26 | here=$(cd "$(dirname "$0")" && pwd) |
| 27 | tsv=${1:-$here/bash_vectors.tsv} |
| 28 | ver=$(bash --version | head -1) |
| 29 | case $BASH_VERSION in 5.2.*) ;; *) echo "bash_verify: need bash 5.2.x, have $BASH_VERSION" >&2; exit 2;; esac |
| 30 | tmp=$(mktemp -d "${TMPDIR:-/tmp}/bashverify.XXXXXX") |
| 31 | trap 'rm -rf "$tmp"' EXIT |
| 32 | cat > "$tmp/prel.sh" <<'P' |
| 33 | e() { printf 'out:'; printf '[%s]' "$@"; printf '\n'; echo errmark >&2; } |
| 34 | P |
| 35 | transcript() { # script text -> transcript on stdout |
| 36 | local d; d=$(mktemp -d "$tmp/run.XXXXXX") |
| 37 | printf '%s' "$1" > "$d/in.sh" |
| 38 | ( cd "$d" && HOME=/HOMEX bash -c '. "$1"; . ./in.sh; e "x=$x"' bash "$tmp/prel.sh" 2>&1 ) |
| 39 | local f |
| 40 | for f in "$d"/*; do |
| 41 | [ "$(basename "$f")" = in.sh ] && continue |
| 42 | printf 'file %s:' "$(basename "$f")"; cat "$f" |
| 43 | done |
| 44 | } |
| 45 | mask() { sed -E -e '/: line [0-9]+: `/d' -e 's/line [0-9]+/line N/g' -e 's/[0-9]{4,}/PID/g' -e 's#/[a-z/]*/bashverify\.[A-Za-z0-9]+#TMP#g'; } |
| 46 | dec() { printf '%b' "$1"; } |
| 47 | squote() { local s=$1; printf "'%s'" "${s//\'/\'\\\'\'}"; } |
| 48 | render() { # expect tokens -> script |
| 49 | local out="" t w |
| 50 | for t in $1; do |
| 51 | case $t in |
| 52 | W:*) w=$(dec "${t#W:}"; printf x); out+=" $(squote "${w%x}")" ;; |
| 53 | PIPE) out+=" |" ;; OR) out+=" ||" ;; AND) out+=" &&" ;; SEMI) out+=" ;" ;; |
| 54 | NL) out+=$'\n' ;; |
| 55 | LT:*) out+=" ${t#LT:}<" ;; GT:*) out+=" ${t#GT:}>" ;; APPEND:*) out+=" ${t#APPEND:}>>" ;; |
| 56 | DUPO:*) out+=" ${t#DUPO:}>&" ;; DUPI:*) out+=" ${t#DUPI:}<&" ;; |
| 57 | *) echo "bash_verify: bad token $t" >&2; return 1 ;; |
| 58 | esac |
| 59 | done |
| 60 | printf '%s' "$out" |
| 61 | } |
| 62 | n=0; bad=0 |
| 63 | log="$here/bash_verified.log" |
| 64 | { echo "bash: $ver ($(command -v bash))"; echo "env: env -i, LC_ALL=C TZ=UTC PATH=/usr/bin:/bin"; echo "file: bash_vectors.tsv"; } > "$log" |
| 65 | while IFS=$'\t' read -r id inp exp eq; do |
| 66 | case $id in c*) ;; *) continue ;; esac |
| 67 | n=$((n + 1)) |
| 68 | in=$(dec "$inp"; printf x); in=${in%x} |
| 69 | if [ "$eq" = "-" ]; then |
| 70 | set -- $exp |
| 71 | shift # ok |
| 72 | alt=$(render "$*"; printf x); alt=${alt%x} |
| 73 | else |
| 74 | alt=$(dec "$eq"; printf x); alt=${alt%x} |
| 75 | fi |
| 76 | t1=$(transcript "$in" | mask); t2=$(transcript "$alt" | mask) |
| 77 | if [ "$t1" = "$t2" ]; then |
| 78 | echo "$id same $(printf '%s' "$t1" | tr '\n' '|')" >> "$log" |
| 79 | else |
| 80 | bad=$((bad + 1)); echo "$id DIFFERENT" | tee -a "$log" |
| 81 | printf ' input: %s\n equiv: %s\n' "$(printf '%s' "$t1" | tr '\n' '|')" "$(printf '%s' "$t2" | tr '\n' '|')" | tee -a "$log" |
| 82 | fi |
| 83 | done < "$tsv" |
| 84 | echo "vectors: $n, mismatches: $bad ($ver)" | tee -a "$log" |
| 85 | [ "$n" -ge 40 ] && [ "$bad" -eq 0 ] |