summaryrefslogtreecommitdiffhomepage
path: root/README.md
blob: cff5dbba08c330e88f1a880eb88023dd30fe1246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)"
            srcset="packaging/zash-mono-dark.svg">
    <img src="packaging/zash-mono-light.svg" alt="zash" width="128">
  </picture>
</p>

# zash

<p align="center">Sean Kurtz</p>

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 C17. No code is taken from another shell.

## Contents

- [Why](#why)
- [Goals](#goals)
- [Non-goals](#non-goals)
- [Toolchain](#toolchain)
- [Building](#building)
- [Testing](#testing)
- [Status](#status)
- [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.

Think "the dash of z/OS", but more usable interactively than dash.

## Goals

1. **A drop-in replacement for the z/OS `/bin/sh`** for POSIX scripts,
   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. A built-in editor is the
   default and libedit is a compile-time alternative. Neither is
   initialised by a non-interactive shell.
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. (`[[ ]]` is planned.)
- Being buildable with the legacy 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 (GCC also builds it cleanly) | `-std=c17` |
| z/OS | IBM Open XL C/C++ (`ibm-clang`) | `-std=c17` |

The code is plain C17. The few attributes it uses are behind macros. See
[DESIGN.md](DESIGN.md#language-level) for the language level and the
coding style.

The z/OS build is 31-bit (`-m32`), because a 31-bit `fork()` costs a
ten-th of a 64-bit one and the catalog can then be called from C. Open
XL also takes `-m64`, and the sources build either way unmodified; the
measured difference between the two is discussed in
[bench/README.md](bench/README.md).

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=c17
make EDIT=libedit LDLIBS=-ledit   # use libedit, not the built-in editor
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 doc             # the source reference: docs/html, docs/zash-reference.pdf
make docs            # that, plus HTML and PDF of this file and DESIGN.md

make zos-ascii       # z/OS, ibm-clang, ASCII mode (zopen convention)
make zos-ebcdic      # the same in EBCDIC (IBM-1047), as 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-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: it works with any `make`
and never invokes make recursively. Variant builds (sanitizers, the
z/OS modes) are single compile-and-link commands over the source list.

`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.

## 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`), 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`, libedit optional). It builds warning-free with Clang and
GCC as C17 and passes its suite plain, under ASan/UBSan, and under
Valgrind.

On z/OS it builds in both character modes with Open XL, 31-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`. `run.sh` labels
`/bin/sh` **ksh88** on z/OS, because that is what it is: IBM's port of
ksh88, not ksh93 (its usage string is `sh [-abCefhikKLmnprtuvx]`, and it
rejects `${.sh.version}`). So `[[ ]]`, `let` and `typeset -i` are tested
against ksh88, and the `(( ))` command, which ksh93 added, is reported
as `n/a` there:

| benchmark | zash | ksh88 | bash | zsh | oksh |
| --- | --- | --- | --- | --- | --- |
| `$(builtin)` x 40000 | 0.04 | 19.55 | 283.14 | 225.96 | port bug |
| function calls | 0.08 | 0.68 | 1.12 | 0.60 | 0.52 |
| arithmetic loop | 0.20 | 1.54 | 2.11 | 0.96 | 1.34 |
| `let` and integer variables | 0.25 | 2.24 | 3.16 | 1.47 | 1.79 |
| `(( ))` as a command | 0.13 | n/a | 1.30 | 0.38 | 1.15 |
| `[[ ]]` in a loop | 0.08 | 0.95 | 1.16 | 0.28 | n/a |
| `[ ]` tests in a loop | 0.15 | 1.39 | 1.97 | 1.07 | 0.96 |
| `case` dispatch | 0.08 | 0.64 | 0.80 | 0.33 | 0.59 |
| small pipelines and `$(a \| b)` | 0.77 | 1.69 | 34.78 | 28.00 | port bug |
| 2000 external commands | 0.57 | 0.52 | 14.22 | 11.64 | 10.12 |
| subshells | 0.00 | 0.79 | 20.90 | 16.87 | 14.57 |
| background jobs | 0.93 | 0.14 | 1.36 | 1.09 | 0.92 |
| redirections | 0.28 | 0.36 | 0.46 | 0.33 | 61.63 |
| here-documents | 0.29 | 1.20 | 0.49 | 1.15 | 0.85 |
| `while read` over a file | 0.30 | 0.91 | 0.84 | 3.31 | port bug |
| recursion | 0.03 | 0.24 | 0.63 | 0.25 | 0.20 |
| `printf` | 1.17 | 1.60 | 1.92 | 1.40 | 207.19 |
| `getopts` | 0.06 | 0.28 | 0.40 | 0.21 | 0.16 |
| parameter trimming | 0.12 | 0.99 | 1.07 | 0.57 | 0.55 |
| `${v/a/b}` substitutions | n/a | n/a | 0.66 | 0.28 | n/a |
| IFS field splitting | 0.05 | 0.32 | 0.43 | differs | 0.24 |
| pattern matching and `case` | 0.27 | 3.04 | 1.83 | 0.98 | 1.11 |
| count a log by level and hour | 0.17 | 0.55 | 0.51 | 1.21 | port bug |
| render and check 100 configs | 0.20 | 0.24 | 2.91 | 2.40 | port bug |
| file tagging and auto-conversion | 0.41 | 1.04 | 8.48 | 7.23 | n/a |

The gap is process creation: every command substitution and pipeline
stage costs a full z/OS spawn there, from 14 s (bash, 2000 commands) to
283 s (bash, 40000 substitutions). zash does those substitutions and
`print`/`echo` pipeline stages in-shell, so its rows stay near ksh88's,
which is what z/OS shops already pay. Plain external commands cost zash what
they cost ksh88 (0.57 against 0.52), so nothing was traded away for the
rest.

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 ksh88's 0.79.

What is left of the gap in the `background jobs` row above is the price
of the address mode, not of the design: measured on this machine a
64-bit `fork()` costs 4.2 ms and a 31-bit one 0.10 ms, and the same
program starts in 29.5 ms at 64-bit against 1.1 ms at 31-bit. The z/OS
make targets therefore build `-m32`, and the table above is the last
64-bit run: at 31-bit `background jobs` is 0.08, `zz-bgsub` 0.11 (ksh88
0.12) and `zz-bgcd` 0.08 (ksh88 0.14), with no other row moving.
[bench/README.md](bench/README.md) has the measurements.

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, and `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.

## 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

BSD 2-Clause. See [LICENSE](LICENSE).