zash

# zash

Sean Kurtz

[![CI](https://github.com/skurtz97/zash/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/skurtz97/zash/actions/workflows/ci.yml) A small, fast POSIX shell for z/OS UNIX System Services, in the Almquist (ash) tradition. zash aims to be for z/OS what FreeBSD's `sh` is for FreeBSD: a lean, standards-following `/bin/sh` that is quick at running scripts and still pleasant to type at. It is written from scratch in C23. No code is taken from another shell. ## Contents - [Why](#why) - [Goals](#goals) - [Non-goals](#non-goals) - [Toolchain](#toolchain) - [Building](#building) - [Testing](#testing) - [Status](#status) - [Recommendations](#recommendations) - [Roadmap](#roadmap) - [License](#license) ## Why The z/OS `/bin/sh` is dated, and the usual alternatives are bash and zsh. Those are comfortable interactively but slow as scripting shells, large, and encourage scripts that run nowhere else. On z/OS that cost is real: CPU time is billed, and every `fork()` copies an address space. zash is for shops that care about that: - Low-MSU shops and anyone watching script CPU time. The shell does its work in-process wherever the language allows. When a process is needed it uses `spawn()`, so `_BPX_SHAREAS` can keep the child in the same address space. - Shops that want a modern `/bin/sh`, not a different language. Scripts written for zash are POSIX `sh` scripts. They run under the existing `/bin/sh`, dash, FreeBSD sh and bash. - People who type at it. File and command completion, history and line editing are in scope, provided they cost a script nothing. It aims to be the dash of z/OS, but more usable interactively, and the successor to the shell it replaces: IBM's `/bin/sh` is of the KornShell line, and the ksh88 constructs z/OS scripts use -- `print`, `whence`, `typeset`, `function f { }`, `[[ ]]`, `(( ))`, `let`, aliases, `trap ... ERR`, `read -t` -- are all here, so a script written for the IBM shell runs under zash unchanged. Where the two do differ, it is because the IBM shell departs from POSIX, or because the feature is one zash declines; [Deliberate differences](DESIGN.md#deliberate-differences-from-other-shells) says which, and `bench/README.md` records every difference the two shells show on the POSIX test corpus. ## Goals 1. A drop-in replacement for the z/OS `/bin/sh` for POSIX scripts and for the ksh88 constructs those scripts use, including the z/OS behaviours scripts rely on (`_BPX_SHAREAS`, `_BPX_SPAWN_SCRIPT`, `STEPLIB`, file tagging). 2. Script performance at least equal to `/bin/sh`, measured on z/OS, by using `spawn()` instead of `fork()` and by not creating a process at all for builtins, even in pipelines and command substitutions. 3. Interactive comfort that is free for scripts: job control, tab completion for files and commands, history, emacs- and vi-style editing. The keys are bash's, and `bind '^[[H'=beginning-of-line` moves one in the BSD shells' notation. The editor is built in, with no terminfo or curses dependency, and a non-interactive shell never initialises it. 4. The Korn shell features z/OS scripts actually use: `print`, `whence`, `typeset`, `function name { }`, `(( ))`, `let`, aliases. They are accepted only where they cost other scripts nothing. 5. ASCII or EBCDIC, chosen at compile time. The source makes no assumption about the character set. 6. Small and readable. One directory, a plain POSIX Makefile, every function documented. 7. Heavily tested. See [Testing](#testing). ## Non-goals - bash or ksh93 extensions: arrays, process substitution, `${var//a/b}`, brace expansion, coprocesses. - Being buildable with the z/OS XL C/C++ compiler (`xlc`). - Portability to every UNIX. Linux is supported: most development and all sanitizer testing happens there. Other POSIX systems will probably work, but are not a target. ## Toolchain | Platform | Compiler | Standard | | --- | --- | --- | | Linux | Clang | `-std=c23` | | z/OS | IBM Open XL C/C++ (`ibm-clang`) | `-std=c23` | The code is plain C23. It draws the line at the language itself: no ``, `` or other library additions newer than the platforms' C libraries, so a system whose libc is older still builds it. See [DESIGN.md](DESIGN.md#language-level) for the language level and the coding style. The z/OS build is 64-bit (`-m64`) XPLINK. It used to be 31-bit, because a 64-bit `fork()` cost 4.2 ms against 0.10 and a start 29.5 ms against 1.1; both were the 64-bit C run-time being loaded out of the linklist on every start, and with that run-time in the LPA the two widths cost the same. See [bench/README.md](bench/README.md#is-it-the-address-mode-or-the-c-runtime) for the measurements and [Recommendations](#recommendations) for the LPA change on a z/OS system. Where the z/OS code generator leaves measurable performance on the table, a hot routine may be rewritten in HLASM. Any such routine keeps its C version. That stays the reference, and it is what Linux builds use. ## Building ```sh make # Linux, clang, -std=c23 make zash # Linux: the same build, named exactly make check # run the test suite make asan # build zash-asan (ASan + UBSan) and run the tests make valgrind # run the tests under Valgrind make bench # CPU time against dash, bash, ksh, zsh, oksh (or /bin/sh) make compdb # write compile_commands.json for clangd make format tidy # clang-format, clang-tidy make install # copy ./zash into $(DESTDIR)$(BINDIR) make doc # the source reference: docs/html, docs/zash-reference.pdf make docs # that, plus HTML and PDF of this file and DESIGN.md make spec # formal/zash-spec.pdf, typeset with typst make other-docs # spec and doc: the documentation tools z/OS lacks make zos-ascii # z/OS default (make, make zash): 64-bit ASCII ./zash make zos-ebcdic # z/OS EBCDIC (IBM-1047), writes ./zash-ebcdic make zos-check # z/OS: functional tests only, no sanitizers make zos-heapcheck # z/OS: the same tests with LE heap check zones on make zos-lint # z/OS code paths compiled on Linux, against shims make mvs-asan # the suite with SYS_MVS forced on, under ASan make zos-debug # z/OS: unoptimised ./zash-dbg for dbx make zos-dbx # z/OS: run ./zash-dbg under dbx make zos-amblist # z/OS: CSECT map of the program object ``` Set `CC`, `CLANG_FORMAT` and `CLANG_TIDY` to pick tool versions. The Makefile is one flat, plain `.POSIX` makefile with no GNU extensions, so any `make` can read it. On z/OS the default build, `make` or `make zash`, is the ASCII `./zash`; only the EBCDIC variant gets a suffix (`./zash-ebcdic`). Variant builds (sanitizers, the z/OS modes) are single compile-and-link commands over the source list; the only places that re-enter `make` are the switchovers on z/OS (`all`, `check`) and the LaTeX pass inside `doc`. `make doc` needs Doxygen, and for the PDF a working `pdflatex` with the packages Doxygen's LaTeX output uses (the Fedora list is a comment above the `doc` target in the Makefile); without `pdflatex` it builds `docs/html` and says so. `make docs` also needs `pandoc` and a Chrome/Chromium to turn the two Markdown documents into HTML and PDF, and `make spec` needs `typst` (the CLI, or the Python package from `pip install typst`). On z/OS the tree carries a zopen `buildenv`, so `zopen build -gp` builds both test passes, installs into `$ZOPEN_INSTALL_DIR`, and writes a pax under `install/`. `zopen build -s` stops in the prepared shell. ## Testing The project is tested at several levels: - Behavioural tests for every feature, as shell scripts with expected output. Portable tests are also run under dash and the results compared, so a disagreement with an existing ash-family shell is always visible. - AddressSanitizer and UndefinedBehaviorSanitizer builds run the whole suite on Linux. - Valgrind runs the whole suite on Linux, including leak checking. - Process accounting tests assert the exact process count of a construct (for example, `$(echo x)` creates none), so a performance regression fails the suite. - Security tests (`tests/zos/security.sh`) check what the shell must never do: create a file more permissive than the umask allows, look for a command in the current directory, or run what an expansion or a file name produced. - Interactive tests drive the shell through a pseudo-terminal: editor keys in both modes, completion, history, prompts and job control. They need python3 and are skipped without it. - On z/OS `make zos-check` runs the same behavioural and process-accounting tests, plus a z/OS-only group (`tests/zos/`), in either character mode. There is no ASan, UBSan or Valgrind there, but Language Environment can put a check zone after every heap element and validate the heap on every request, so an overlay becomes a U4042 abend: `make zos-heapcheck` runs the whole suite that way, and `tests/zos/fuzz.sh` runs its own cases under it. `zos-check` also puts a bundled `sleep` first in `PATH`, because `/bin/sleep` rejects a fractional interval and four tests of job ordering need one. Both character modes pass `zos-check`; under the heap checker two signal tests fail, as they do not in `zos-check` (see [DESIGN.md](DESIGN.md#testing)). - A debugger is `lldb` on Linux (`make debug`, `make attach`) and `dbx` on z/OS (`make zos-debug`, `make zos-dbx`). `make zos-amblist` maps the CSECTs of the program object when a module-level question comes up. Break by function (`st in main`) or by address. On a build from ibm-clang, `st at "file":line` does not resolve the source. `next` and `where` report file and line correctly. The tests use nothing beyond POSIX utilities (`awk`, `sed`, `tr`, `wc`, `sort`, `cat`, `grep`, `sleep`), and the few that depend on ASCII code points are skipped automatically for an EBCDIC build. See [tests/README.md](tests/README.md) for the layout. Memory and undefined-behaviour bugs are rarely platform specific: a use-after-free Valgrind finds on Linux is the same bug on z/OS, where there is no Valgrind. The shell stays portable, with the z/OS-specific code in one small file. That is what makes the Linux tooling useful. ## Status On Linux the shell is feature-complete for its first release: the scripting core, the FreeBSD and Korn extensions, job control, and the interactive layer (built-in editor with emacs and vi modes, completion, history and `fc`). It builds warning-free with Clang as C23 and passes its suite plain, under ASan/UBSan, and under Valgrind. On z/OS it builds in both character modes with Open XL, 64-bit, passes its suite (including the `zos/` group) and debugs with `dbx`. The LE heap-checker run is the one exception: two signal tests fail there and pass in `zos-check`. `make zos-debug`, `zos-dbx`, `zos-heapcheck` and `zos-amblist` are the entry points. Data set redirection and `tso` are the parts still written from documentation. See [DESIGN.md](DESIGN.md#roadmap). CPU seconds (user+sys, children included) on Linux x86-64, `make bench`. Timings vary by a few hundredths between runs, and the rows that create processes by more: | benchmark | zash | dash | bash | ksh93 | oksh | | --- | --- | --- | --- | --- | --- | | `$(builtin)` x 40000 | 0.01 | 8.20 | 17.99 | 0.11 | 25.24 | | function calls | 0.03 | 0.07 | 0.25 | 0.13 | 0.12 | | arithmetic loop | 0.07 | 0.19 | 0.53 | 0.24 | 0.31 | | `let` and integer variables | 0.10 | n/a | 0.78 | 0.35 | 0.43 | | `(( ))` as a command | 0.06 | n/a | 0.33 | 0.09 | 0.30 | | `[[ ]]` in a loop | 0.04 | n/a | 0.29 | 0.12 | n/a | | `[ ]` tests in a loop | 0.05 | 0.16 | 0.43 | 0.15 | 0.22 | | `case` dispatch | 0.03 | 0.07 | 0.20 | 0.09 | 0.14 | | small pipelines and `$(a \| b)` | 1.10 | 2.35 | 3.16 | 1.33 | 2.32 | | 2000 external commands | 0.55 | 0.53 | 0.81 | 0.56 | 0.84 | | subshells | 0.01 | 0.37 | 0.62 | 0.01 | 0.48 | | background jobs | 0.03 | 0.03 | 0.06 | 0.04 | 0.03 | | redirections | 0.07 | 0.08 | 0.11 | 0.09 | 5.85 | | here-documents | 0.03 | 0.03 | 0.08 | 0.10 | 0.13 | | `while read` over a file | 0.04 | 0.07 | 0.21 | 0.12 | 0.10 | | recursion | 0.02 | 0.04 | 0.17 | 0.05 | 0.06 | | `printf` | 0.09 | 0.12 | 0.23 | 0.16 | 19.70 | | `getopts` | 0.02 | 0.03 | 0.09 | 0.03 | 0.05 | | parameter trimming | 0.05 | 0.09 | 0.32 | 0.20 | 0.14 | | `${v/a/b}` substitutions | n/a | n/a | 0.17 | 0.08 | n/a | | IFS field splitting | 0.03 | 0.04 | 0.11 | 0.05 | 0.06 | | pattern matching and `case` | 0.11 | 0.18 | 0.53 | 0.38 | 0.35 | | count a log by level and hour | 0.04 | 0.05 | 0.13 | 0.07 | 0.08 | | render and check 100 configs | 0.38 | 0.46 | 0.53 | 0.26 | 0.52 | The `bench/` scripts double as tests: `bench/run.sh` compares each shell's output with the reference shell's, so a wrong answer fails instead of merely being timed. `bench/report.sh` (count a log by level and hour) and `bench/deploy.sh` (render and check 100 configs) are the two that look most like the scripts this shell is for. Running an external command costs zash about what it costs dash on Linux, where `fork()` is cheap. On z/OS it is not, and that is where the gap opens. The same benchmarks on z/OS (ASCII build), run as `sh bench/run.sh $(pwd)/zash /bin/sh bash zsh oksh`. The `z/OS sh` column is IBM's `/bin/sh`. It is not ksh93 (its usage string is `sh [-abCefhikKLmnprtuvx]`, and it rejects `${.sh.version}`), so `[[ ]]`, `let` and `typeset -i` are measured against it, and the `(( ))` command, which ksh93 added, is `n/a` there: IBM's `sh` reads `((1))` as two nested subshells and reports `1: FSUM7351 not found`. `run.sh` gives that column the label `z/OS sh` when it is handed `/bin/sh` on z/OS: | benchmark | zash | z/OS sh | bash | zsh | oksh | | --- | --- | --- | --- | --- | --- | | `$(builtin)` x 40000 | 0.04 | 20.63 | 22.44 | 51.34 | 36.49 | | `(( ))` as a command | 0.14 | n/a | 0.94 | 0.39 | n/a | | `[[ ]]` in a loop | 0.08 | 0.93 | 1.08 | 0.28 | n/a | | `[ ]` tests in a loop | 0.12 | 1.40 | 1.62 | 1.10 | n/a | | `case` dispatch | 0.06 | 0.64 | 0.78 | 0.35 | 0.59 | | small pipelines and `$(a \| b)` | 0.62 | 1.60 | 4.21 | 6.74 | 3.37 | | 2000 external commands | 0.49 | 0.52 | 1.55 | 2.87 | 1.39 | | subshells | 0.00 | 0.78 | 1.32 | 3.66 | 0.80 | | background jobs | 0.08 | 0.12 | 0.24 | 0.24 | 0.12 | | redirections | 0.26 | 0.35 | 0.32 | 0.32 | port bug | | here-documents | 0.31 | 1.09 | 0.72 | 1.03 | n/a | | `while read` over a file | 0.30 | 0.93 | 0.93 | 3.27 | port bug | | recursion | 0.04 | 0.24 | 0.28 | 0.22 | 0.20 | | `printf` | 1.19 | 1.92 | 1.40 | 1.40 | n/a | | `getopts` | 0.04 | 0.28 | 0.21 | 0.21 | n/a | | parameter trimming | 0.12 | 0.98 | 0.58 | 0.58 | n/a | | `${v/a/b}` substitutions | n/a | n/a | 0.28 | 0.28 | n/a | | IFS field splitting | 0.04 | 0.32 | 0.05 | differs | n/a | | pattern matching and `case` | 0.27 | 3.04 | 0.98 | 0.98 | n/a | | count a log by level and hour | 0.17 | 0.54 | 1.20 | 1.20 | n/a | | render and check 100 configs | 0.16 | 0.20 | 0.62 | 0.62 | n/a | | file tagging and auto-conversion | 0.38 | 1.03 | 2.33 | 2.33 | n/a | The gap is process creation: every command substitution and pipeline stage costs a full z/OS spawn there, from 22 s (bash, 40000 substitutions) to 51 s (zsh). zash does those substitutions and `print`/`echo` pipeline stages in-shell, so it beats the `z/OS sh` column on every row but two. Plain external commands cost zash 0.49 against `sh` 0.52, so nothing was traded away for the rest, and `zz-fgcd`, 200 foreground subshells, is 0.08 against `sh` 0.05: the one row still lost. A `fork()` costs more on z/OS than a `spawn()`, because the fork copies the address space where the spawn can share it. That used to be visible in the subshell row: 3000 `( : )` shells, each forking, took 14.2 s. A subshell whose body is one output-only builtin now runs in the shell, like a pipeline stage already did, so the same 3000 cost 0.00 s here against the 0.78 of `sh`. The fork itself used to cost 4.2 ms, against 0.10 for a 31-bit shell, and a program start 29.5 ms against 1.1. Both were the 64-bit C run-time being loaded out of the linklist in every address space. With the run-time in the dynamic LPA a 64-bit `fork()` is 0.10 ms and a start 1.3 ms, so zash is built 64-bit and the rows above are measured that way. [bench/README.md](bench/README.md) has the before-and-after table, and [Recommendations](#recommendations) says what to change on the system. The `port bug` entries are the third-party `oksh` port's, not zash's: on there `echo hi | cat` prints two bytes of garbage, so those rows measure nothing and the run reports a differing result. n/a means the shell lacks the feature the benchmark uses, or here that the build on z/OS has neither: ksh93's syntax and `(( ))` are in neither IBM's sh nor `oksh`. `differs` means the shell computed something else: zsh, which does not split fields on an unquoted expansion even in sh mode, answers the IFS benchmark with one word instead of eight. Every row is from one run, since the same binary can read 50% slower an hour later on a loaded LPAR. ## Recommendations ### z/OS: put the C run-time in the LPA A z/OS system that runs 64-bit programs often should load the 64-bit C run-time once, into the dynamic LPA, from the linklist library it already lives in. Without it every program load pays to load and relocate the same DLL for itself; it is also the one change that made a 64-bit `fork()` as cheap as a 31-bit one on the machine these numbers come from. One command: ```text SETPROG LPA,ADD,MODNAME=CELQLIB,DSNAME=CEE.SCEERUN2 ``` and the same for `CELQDCPP`, `CELQDEST`, `CELQDMNP`, `CELQDSNF`, `CELQIOTD`, `CELQMCHK`, `CELQV004` and `IBMHV011` --- about 14 MB in all. The permanent form is an `LPA ADD` in the PROGxx member that `PROG=` in `IEASYSxx` names: ```text LPA ADD DSNAME(CEE.SCEERUN2) MODNAME(CELQDCPP,CELQDEST,CELQDMNP,CELQDSNF, CELQIOTD,CELQLIB,CELQMCHK, CELQV004,IBMHV011) ``` The installer may want `CSAMIN=(4M,0)` on that statement: a dynamic LPA add takes extended common storage, and this system's LPA runs with no floor at all (`D PROG,LPA,CSAMIN` answers `0,0`). `notes/celq-lpa.rexx` and `notes/celq-lpa.jcl` issue the command through SDSF if the operator would rather not type it. On the machine measured here PLPA is full, so the IPL-time LPA already overflows into the common page data set (`ILR005E`). That is a system to clean up rather than a reason to skip the add: the add put 12 MB into the common area, which had 255 MB free. ### zopen / ASCII builds: avoid double pipe conversion An ASCII build tags pipes IBM-1047 (EBCDIC) so that EBCDIC readers see EBCDIC. Between two ASCII programs that costs two conversions per byte -- write through the tag to 1047, then read through it back to ASCII. `set +o pipetag` avoids both conversions when no EBCDIC tool is in the pipeline. The setting can go in `.profile` or a per-script `set +o pipetag` at the top. ```sh # In an ASCII build, make pipes between ASCII tools pay zero conversion: set +o pipetag ``` ## Roadmap Linux is feature-complete for the first release (see [Status](#status)). On z/OS, in order: 1. Manual pages: `zash(1)` and a page per builtin group, installed by `make install`, kept beside the code they document. 2. The editor on a real terminal: interactive tests on a 3270-style terminal, in both character modes. 3. `_BPX_SHAREAS`: the benchmark rows above re-run under `_BPX_SHAREAS=YES`, `NO` and `REUSE`. 4. The remaining `VERIFY` notes: `sys_zos.c`, `charset.h`, the EBCDIC `$'\cX'` codes and the data set paths, each checked on hardware and the note removed. 5. z/OS conveniences: `tag`, `alloc`/`free` and `tso` builtins, MVS data set redirection, job names, message IDs. [DESIGN.md](DESIGN.md#roadmap) has the longer version, including why each item is where it is. ## License SPDX: `MIT OR Apache-2.0 OR BSD-2-Clause`, at your option. See [LICENSE](LICENSE).