Skip to content

contrib/awk: add GNU parity coverage and sandbox I/O - #769

Merged
ewhauser merged 2 commits into
mainfrom
ewhauser/awk-gnu-parity
Mar 28, 2026
Merged

ewhauser merged 2 commits into
mainfrom
ewhauser/awk-gnu-parity

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

What changed

This updates contrib/awk toward GNU awk parity inside the gbash sandbox.

  • replace the minimal awk flag parser with GNU-style handling for -f, -e, -E, -i, long options, and GNU info output
  • route awk file reads, file writes, system(), cmd | getline, and print | cmd through sandbox-owned Invocation.FS and nested Invocation.Exec hooks instead of host-global file or process APIs
  • add GNU-compatible helpers and runtime behavior for gensub, strtonum, systime, mktime, strftime, bitwise builtins, ARGIND, PROCINFO["version"], IGNORECASE, FIELDWIDTHS, and FPAT
  • split the awk conformance corpus into category files for CLI, GNU functions, GNU vars, I/O and exec, modes, and numeric behavior, and remove the old awk xfails
  • document the contrib awk sandbox contract in SPEC.md

Why

The previous awk adapter intentionally disabled shell execution, redirected output, and non-input-file reads, so it diverged from GNU awk in exactly the areas users expect to work for real programs. The vendored interpreter also lacked the GNU-specific runtime hooks and variable behavior needed to make those features work safely inside gbash.

This change keeps the sandbox boundary intact while making awk behave like GNU awk within that boundary.

Impact

  • awk programs can now read and write sandbox-local files and use shell-based system() and pipe operators without escaping the gbash runtime
  • the awk conformance suite now tracks GNU awk behavior by category instead of hiding the important deltas in one script
  • downstream contributors have a clearer contract for future awk parity work because the runtime boundary is now explicit in the spec and tests

Validation

  • go test ./contrib/awk/...
  • GBASH_CONFORMANCE_AWK=$(./scripts/ensure-awk.sh) GBASH_CONFORMANCE_BASH=$(./scripts/ensure-bash.sh) GBASH_RUN_AWK_CONFORMANCE=1 go test ./contrib/awk -run 'TestAWKConformance$' -count=1
  • make lint

@ewhauser
ewhauser marked this pull request as ready for review March 28, 2026 21:23
@ewhauser
ewhauser merged commit f01d164 into main Mar 28, 2026
16 checks passed
@ewhauser
ewhauser deleted the ewhauser/awk-gnu-parity branch March 28, 2026 21:24

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0ccec7c8fd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 865 to +869
p.fieldSep = p.toString(v)
if utf8.RuneCountInString(p.fieldSep) > 1 { // compare to interp.ensureFields
re, err := regexp.Compile(compiler.AddRegexFlags(p.fieldSep))
re, err := p.compileRegex(p.fieldSep)
if err != nil {
return newError("invalid regex %q: %s", p.fieldSep, err)
return err

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reset field splitting mode when FS is assigned

After FIELDWIDTHS or FPAT has been set, assigning FS should switch record splitting back to FS-based behavior (matching gawk), but this branch only updates fieldSep/fieldSepRegex and never clears the alternative split modes. Because ensureFields() checks fieldWidths and fieldPatternRegex first, scripts that change split mode at runtime keep using stale FIELDWIDTHS/FPAT rules and produce incorrect fields.

Useful? React with 👍 / 👎.

Comment thread contrib/awk/awk.go
if len(rest) > 0 && rest[0] != 0 {
loc = time.UTC
}
t := time.Date(values[0], time.Month(values[1]), values[2], values[3], values[4], values[5], 0, loc)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the DST component in mktime datespec

The 7th mktime datespec field is parsed into values[6] but never applied when constructing the timestamp. GNU awk uses this field to force DST/non-DST interpretation, so around DST boundaries this implementation returns the wrong epoch (for example, forcing DST=0 in a summer timestamp should differ by one hour in zones like America/New_York).

Useful? React with 👍 / 👎.

Comment thread contrib/awk/awk.go
Comment on lines +730 to +733
target := ""
if len(rest) > 0 {
target = rest[0]
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Default gensub target to current record when arg is omitted

With GNU awk, calling gensub(regex, repl, how) (3 args) applies substitutions to $0. Here the target defaults to an empty string unless a 4th argument is present, so 3-argument calls silently operate on "" and return incorrect results instead of transforming the current record.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant