summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-23 11:50:17 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-23 11:50:17 -0600
commitb6574b2f3cb86d33d7e55985c7914dfce6dbf296 (patch)
tree22449ab76bbe49bed35b5e6c54019d2802301d6a /src
parent2161b3c2de4eec4cc5f28cbf683f25acd3c7dbef (diff)
parenta847b938b372995762c8ab581c0be521fd2e8bcb (diff)
downloadzash-docs/logo-and-clean.tar.gz
Merge origin/main into docs/logo-and-cleandocs/logo-and-clean
Resolutions: README.md and DESIGN.md keep the new colour logo (packaging/zash-logo.svg) at width 220; the Makefile keeps main's newer shellcheck header, lists `make docs` in the usage block, and `clean` removes docs, cov and $(ZTOOLS) along with the branch's new tools.
Diffstat (limited to 'src')
-rw-r--r--src/exec.c22
-rw-r--r--src/mvsfmt.c11
-rw-r--r--src/sys_zos.c238
-rw-r--r--src/util.c13
-rw-r--r--src/zos_csi.s99
5 files changed, 169 insertions, 214 deletions
diff --git a/src/exec.c b/src/exec.c
index 72e718c..4758440 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -863,17 +863,6 @@ static void assign_temp(const simple_cmd *sc) {
}
/**
- * @brief Run an external command.
- *
- * @param sc The prepared command.
- * @param flags EV_EXIT allows replacing the shell with exec.
- * @param in Descriptor for its stdin, or -1.
- * @param out Descriptor for its stdout, or -1.
- * @param async When not NULL, receives the pid and the command is not
- * waited for (pipeline stages, `&`, command substitution).
- * @return The exit status. 0 for an async launch.
- */
-/**
* @brief Apply `_TAG_REDIR_*` assignments given on an external command.
*
* Assignments before an external command go only into its environment,
@@ -918,6 +907,17 @@ static void tag_prefix_end(const tag_mode saved[3], int in, int out) {
memcpy(tag_redir, saved, sizeof tag_redir);
}
+/**
+ * @brief Run an external command.
+ *
+ * @param sc The prepared command.
+ * @param flags EV_EXIT allows replacing the shell with exec.
+ * @param in Descriptor for its stdin, or -1.
+ * @param out Descriptor for its stdout, or -1.
+ * @param async When not NULL, receives the pid and the command is not
+ * waited for (pipeline stages, `&`, command substitution).
+ * @return The exit status. 0 for an async launch.
+ */
static int run_external(simple_cmd *sc, unsigned flags, int in, int out,
pid_t *async) {
const node *n = sc->n;
diff --git a/src/mvsfmt.c b/src/mvsfmt.c
index 08494dc..8424ac8 100644
--- a/src/mvsfmt.c
+++ b/src/mvsfmt.c
@@ -292,11 +292,12 @@ bool csi_parse(const unsigned char *work, size_t worklen, mvs_name_fn *fn,
/**
* @brief Most IGGCSI00 calls for one search (resumes included).
*
- * One call fills the 64000-byte work area with roughly 1100 entries (about
- * 58 bytes each for a name and its VOLSER), and a screen holds tens. The
- * only caller is Tab completion, so two calls is already more names than
- * anyone can look at -- and a broad filter on a big catalog is the one way
- * this feature can put real work on catalog management.
+ * One call fills the 32K work area with roughly 560 entries (about
+ * 58 bytes each for a name and its VOLSER), so three calls are about
+ * 1700 names, and a screen holds tens. The only caller is Tab
+ * completion, so three calls is already more names than anyone can look
+ * at -- and a broad filter on a big catalog is the one way this feature
+ * can put real work on catalog management.
*/
#define CSI_MAX_CALLS 3
diff --git a/src/sys_zos.c b/src/sys_zos.c
index 9041edd..48c1315 100644
--- a/src/sys_zos.c
+++ b/src/sys_zos.c
@@ -15,6 +15,7 @@
#include <_Nascii.h>
#include <errno.h>
#include <fcntl.h>
+#include <setjmp.h>
#include <spawn.h>
#include <stddef.h>
#include <stdint.h>
@@ -29,6 +30,12 @@
#include "charset.h"
#include "sys.h"
+/* IGGCSI00 is AMODE 31 and is called through a plain function pointer:
+ * that only works because this program is 31-bit too (bench/README.md). */
+#ifndef ZASH_ZOS_SHIM
+_Static_assert(sizeof(void *) == 4, "the z/OS build must be -m32");
+#endif
+
/**
* @brief Describe a signal.
*
@@ -89,6 +96,8 @@ const char *sys_strsignal(int sig) {
*/
pid_t sys_spawn(const spawn_req *rq) {
int fd_map[SYS_NFD + 16];
+ int dups[SYS_NFD];
+ int ndups = 0;
const int map_max = (int)(sizeof fd_map / sizeof fd_map[0]);
int fd_count = 1; /* 0 would mean "inherit everything" */
struct inheritance inh = {0};
@@ -100,10 +109,31 @@ pid_t sys_spawn(const spawn_req *rq) {
src = FD_CLOSED;
if (src == FD_CLOSED) {
fd_map[i] = SPAWN_FDCLOSED;
- } else {
- fd_map[i] = src;
- fd_count = i + 1;
+ continue;
+ }
+ if (src != i) {
+ /*
+ * The shell keeps its pipe ends close-on-exec (sys_pipe()). A
+ * mapping onto the child's 0..2 has to clear that flag: the child
+ * is the one that execs the command, and close-on-exec would take
+ * its standard output with it -- ps and a #!/bin/sh shim both
+ * failed this way. dup2() clears the flag, but spawn()'s map
+ * gives the child the parent's descriptor as it is, so pass a
+ * plain duplicate, which F_DUPFD makes without the flag.
+ */
+ src = fcntl(src, F_DUPFD, SYS_NFD);
+ if (src < 0) {
+ int saved;
+ saved = errno;
+ while (ndups)
+ close(dups[--ndups]);
+ errno = saved;
+ return -1;
+ }
+ dups[ndups++] = src;
}
+ fd_map[i] = src;
+ fd_count = i + 1;
}
if (rq->sigdfl) {
inh.flags |= SPAWN_SETSIGDEF;
@@ -138,8 +168,14 @@ pid_t sys_spawn(const spawn_req *rq) {
* runtime converts path, argv and envp, and that _BPX_SHAREAS,
* _BPX_SPAWN_SCRIPT and STEPLIB are honoured from `envp`.
*/
- return spawn(rq->path, fd_count, fd_map, &inh, (const char **)rq->argv,
- (const char **)rq->envp);
+ pid_t pid = spawn(rq->path, fd_count, fd_map, &inh, (const char **)rq->argv,
+ (const char **)rq->envp);
+ int saved;
+ saved = errno;
+ while (ndups)
+ close(dups[--ndups]);
+ errno = saved;
+ return pid;
}
/**
@@ -541,70 +577,79 @@ const char *sys_info(sys_info_kind kind) {
/* ---- the catalog ------------------------------------------------------- */
/*
- * IGGCSI00 is AMODE 31 and takes a standard three-word parameter list.
- * zash is AMODE 64, so the call goes through zos_csi.s, which switches
- * addressing mode around a branch to the module. Everything the routine
- * or the glue touches is in one csi_block in 31-bit storage, below 2 GB.
- * The glue keeps the caller's registers there too, so it depends on
- * nothing about the caller's save area or stack, and it writes the entry
- * point of IGGCSI00 back there so later calls skip the directory search.
+ * IGGCSI00 is AMODE 31 and takes a standard OS parameter list of three
+ * addresses: reason area, selection criteria, work area, with the VL bit
+ * on the last. zash is 31-bit XPLINK too, so it is called directly:
+ * fetch() loads it once, and the OS linkage makes the compiler build the
+ * parameter list and give the module a save area. No mode switch, and
+ * no assembler.
+ *
+ * The linkage has to be OS_UPSTACK, not plain OS. Under -m32 (XPLINK),
+ * OS means OS_NOSTACK: no standard save area, so IGGCSI00 -- a
+ * traditional upward-stack OS routine -- takes a program check on the
+ * first call. OS_UPSTACK is what the compiler reference names for
+ * exactly this case; measured on z/OS, OS abends and OS_UPSTACK returns
+ * rc=0 with the work area filled.
*/
-/** @brief Size of the CSI return work area (IBM recommends 64000). */
-#define CSI_WORK 64000
-
-/** @brief Returned when IGGCSI00 cannot be loaded (CSI uses 0-16). */
-#define CSI_NOLOAD 16
-
-/**
- * @brief 31-bit storage for CSI calls. The layout up to `rc` is shared
- * with zos_csi.s (the offsets are checked below).
+/** @brief Size of the CSI return work area.
*
- * Packed, so the compiler cannot insert padding the DSECT does not have,
- * and aligned(8), so the block and its save area start on a doubleword.
- * The name is kept as EBCDIC bytes and not as a C string, so that both
- * character modes spell it the same way.
+ * IBM recommends 64000; 32K already holds more names than a completion
+ * screen shows, and it is taken per search rather than held.
*/
-typedef struct __attribute__((packed, aligned(8))) csi_block {
- unsigned char *__ptr32 parm[3]; /**< +0: A(reason), A(field),
- A(work) with the VL bit */
- uint32_t save[18]; /**< +12: save area for IGGCSI00 */
- unsigned char reason[4]; /**< +84 */
- uint64_t regs[16]; /**< +88: caller's R14-R13 */
- uint32_t ep; /**< +216: EPA from LOAD */
- int32_t rc; /**< +220: R15 from IGGCSI00 */
- unsigned char epname[8]; /**< +224: "IGGCSI00", IBM-1047 */
- unsigned char field[CSI_FIELD_LEN]; /**< +232: selection criteria */
- unsigned char work[CSI_WORK]; /**< return work area */
-} csi_block;
+#define CSI_WORK 32768
+
+/** @brief One search's storage: taken per search, freed before return. */
+typedef struct csi_req {
+ unsigned char reason[4];
+ unsigned char field[CSI_FIELD_LEN];
+ unsigned char work[CSI_WORK] __attribute__((aligned(8)));
+} csi_req;
+
+typedef int iggcsi_fn(void *reason, void *field, void *work);
+#pragma linkage(iggcsi_fn, OS_UPSTACK)
+
+static iggcsi_fn *csi_ep; /**< from fetch(), NULL until loaded */
+static bool csi_dead; /**< fetch failed or a call abended */
+static bool csi_in_flight; /**< one search at a time */
-_Static_assert(offsetof(csi_block, save) == 12, "zos_csi.s SAVE");
-_Static_assert(offsetof(csi_block, reason) == 84, "zos_csi.s REASON");
-_Static_assert(offsetof(csi_block, regs) == 88, "zos_csi.s REGS");
-_Static_assert(offsetof(csi_block, ep) == 216, "zos_csi.s EP");
-_Static_assert(offsetof(csi_block, rc) == 220, "zos_csi.s RC");
-_Static_assert(_Alignof(csi_block) == 8, "csi_block alignment");
-/* Held for the life of the shell: keep it small. */
-_Static_assert(sizeof(csi_block) <= 64 * 1024 + 512, "csi_block size");
+/** @brief Returned by csi_call() after an abend: > 4, so the search stops. */
+#define CSI_ABENDED 20
-#pragma linkage(ZASHCSI, OS)
-int ZASHCSI(csi_block *block);
+static sigjmp_buf csi_jb;
+
+static void csi_trap(int sig) {
+ (void)sig;
+ siglongjmp(csi_jb, 1);
+}
/**
* @brief One IGGCSI00 call (a csi_call_fn).
*
- * The parameter list is rebuilt each time: IGGCSI00 is entitled to
- * assume nothing else in the block survives a call.
+ * A program check or abend inside IGGCSI00 comes back as a signal. It is
+ * caught only for the length of the call, and the call then reports
+ * failure; the caller frees the block and retires the catalog.
*/
static int csi_call(csi_area *a, void *ctx) {
- csi_block *b = ctx;
- (void)a;
- /* 64-bit to __ptr32 is an explicit cast; the block is below 2 GB,
- * so nothing is lost. The VL bit goes on the last entry. */
- b->parm[0] = (unsigned char *__ptr32)b->reason;
- b->parm[1] = (unsigned char *__ptr32)b->field;
- b->parm[2] = (unsigned char *__ptr32)((uintptr_t)b->work | 0x80000000u);
- return ZASHCSI(b);
+ bool *abended = ctx;
+ static const int sigs[] = {SIGABND, SIGSEGV, SIGILL, SIGFPE, SIGBUS};
+ enum { NSIG_CSI = sizeof sigs / sizeof sigs[0] };
+ struct sigaction sa = {.sa_handler = csi_trap}, old[NSIG_CSI];
+ sigemptyset(&sa.sa_mask);
+ for (int i = 0; i < NSIG_CSI; i++)
+ sigaction(sigs[i], &sa, &old[i]);
+ volatile int rc;
+ if (sigsetjmp(csi_jb, 1) == 0) {
+ /* VL bit on the last address; harmless if the compiler sets it. */
+ void *work = (void *)((uintptr_t)a->work | 0x80000000u);
+ rc = csi_ep(a->reason, a->field, work);
+ } else {
+ *abended = true;
+ rc = CSI_ABENDED;
+ }
+ for (int i = 0; i < NSIG_CSI; i++)
+ sigaction(sigs[i], &old[i], NULL);
+ return rc;
}
/**
@@ -615,45 +660,40 @@ bool sys_has_catalog(void) {
}
/**
- * @brief The one CSI block, in 31-bit storage, allocated on the first
- * search and held for the life of the shell.
- */
-static csi_block *csi_blk;
-
-/** @brief True while a search is using the block. */
-static bool csi_in_flight;
-
-/**
* @brief List cataloged names that match a CSI filter key.
*
- * The block is 64 KB of 31-bit storage: IGGCSI00 is AMODE 31, so all
- * three of its addresses have to be below 2 GB. Allocating one for
- * the first Tab and keeping it is better than taking a fresh one per
- * Tab, which is what the shell used to do. A nested search is
- * refused rather than given a second block: the block holds the
- * resume token of the search that is running. The module zash loads
- * is never DELETEd; MVS drops it when the shell ends.
+ * The module is loaded once and, unless a call abends, stays loaded for
+ * the life of the shell: MVS drops it when the shell ends. A nested
+ * search is refused rather than given a second block, because the work
+ * area holds the resume token of the search that is running.
*
* @return false if the catalog could not be searched.
*/
bool sys_catalog_list(const char *filter, mvs_name_fn *fn, void *arg) {
- if (csi_in_flight)
+ if (csi_dead || csi_in_flight)
return false;
- if (!csi_blk) {
- /* Spelled in IBM-1047 whatever this program's own character
- * mode is: LOAD takes the name as the system spells it. */
- static const unsigned char name[8] = {0xC9, 0xC7, 0xC7, 0xC3,
- 0xE2, 0xC9, 0xF0, 0xF0};
- csi_blk = __malloc31(sizeof *csi_blk);
- if (!csi_blk)
+ if (!csi_ep) {
+ csi_ep = (iggcsi_fn *)(void (*)(void))fetch("IGGCSI00");
+ if (!csi_ep) {
+ csi_dead = true; /* never asked for again */
return false;
- memset(csi_blk, 0, offsetof(csi_block, epname));
- memcpy(csi_blk->epname, name, sizeof name);
+ }
}
- csi_area a = {csi_blk->reason, csi_blk->field, csi_blk->work, CSI_WORK};
+ csi_req *r = malloc(sizeof *r);
+ if (!r)
+ return false; /* not sticky: storage may come back */
+ csi_area a = {r->reason, r->field, r->work, CSI_WORK};
+ bool abended = false;
csi_in_flight = true;
- bool ok = csi_search(filter, &a, csi_call, csi_blk, fn, arg);
+ bool ok = csi_search(filter, &a, csi_call, &abended, fn, arg);
csi_in_flight = false;
+ free(r);
+ if (abended) {
+ (void)release((void (*)(void))csi_ep);
+ csi_ep = NULL;
+ csi_dead = true;
+ ok = false;
+ }
return ok;
}
@@ -668,6 +708,9 @@ bool sys_catalog_list(const char *filter, mvs_name_fn *fn, void *arg) {
* PDS under BSAM. A PDSE directory is simulated).
*
* @param dsn Data set name, without quotes.
+ * @param fn Called for each member name.
+ * @param arg Passed to `fn`.
+ * @return false if the directory could not be read or was malformed.
*/
bool sys_pds_members(const char *dsn, mvs_name_fn *fn, void *arg) {
char path[64];
@@ -708,7 +751,7 @@ bool sys_dd_names(mvs_name_fn *fn, void *arg) {
/* ---- dynamic allocation ------------------------------------------------ */
typedef int bpxwdyn_fn(char *);
-#pragma linkage(bpxwdyn_fn, OS)
+#pragma linkage(bpxwdyn_fn, OS_UPSTACK)
/**
* @brief Issue a BPXWDYN request, such as `alloc dd(in) da(x.y) shr`.
@@ -717,9 +760,6 @@ typedef int bpxwdyn_fn(char *);
* names allocated this way belong to the address space, so a command
* spawned with `_BPX_SHAREAS=YES` can open them as `//DD:NAME`.
*
- * VERIFY: calling BPXWDY2 from an AMODE 64 program through fetch(). It
- * may need the same kind of glue as the catalog search.
- *
* @param request The request text.
* @param msg Receives an explanation on failure.
* @param msglen Size of `msg`.
@@ -728,16 +768,19 @@ typedef int bpxwdyn_fn(char *);
*/
int sys_dynalloc(const char *request, char *msg, size_t msglen) {
static bpxwdyn_fn *bpxwdyn;
- if (!bpxwdyn)
+ static bool tried;
+ if (!bpxwdyn && !tried) {
+ tried = true;
bpxwdyn = (bpxwdyn_fn *)(void (*)(void))fetch("BPXWDY2 ");
+ }
if (!bpxwdyn) {
snprintf(msg, msglen, "cannot load BPXWDYN");
return -1;
}
size_t len = strlen(request);
- char *text = __malloc31(len + 1);
+ char *text = malloc(len + 1);
if (!text) {
- snprintf(msg, msglen, "out of storage below 2 GB");
+ snprintf(msg, msglen, "out of storage");
return -1;
}
memcpy(text, request, len + 1);
@@ -775,7 +818,8 @@ int sys_pipe(int fd[2]) {
}
int high = sys_dupfd(fd[i], SYS_NFD);
if (high < 0) {
- int err = errno;
+ int err;
+ err = errno;
close(fd[0]);
close(fd[1]);
errno = err;
@@ -826,6 +870,14 @@ int sys_open(const char *path, int flags, mode_t mode) {
unsigned sys_init(void) {
unsigned mask = 0;
/*
+ * LE prints "CEE5208S The signal SIGxxx was received." on stderr when a
+ * process dies of a signal with the default action -- noise in a script's
+ * output, and /bin/sh does not do it. IBM's zoslib sets this too. The
+ * runtime reads it lazily, so this covers this shell and the children it
+ * forks; "0": a value the user set wins.
+ */
+ (void)setenv("_EDC_SIG_DFLT", "1", 0);
+ /*
* An ASCII build converts to and from tagged files and pipes itself;
* it must not depend on the user having set _BPXK_AUTOCVT. An EBCDIC
* build leaves the process setting alone, like /bin/sh.
diff --git a/src/util.c b/src/util.c
index 39db8ef..69498cd 100644
--- a/src/util.c
+++ b/src/util.c
@@ -531,12 +531,6 @@ void exc_raise(int exc) {
}
/**
- * @brief Print a message on standard error, prefixed with the shell's name and,
- * in a script, the current line number.
- * @param fmt printf() format.
- * @param ap Arguments.
- */
-/**
* @brief Derive a message number from a message's format string.
*
* `set -o msgid` puts an identifier such as `ZSH4711E` in front of every
@@ -561,6 +555,13 @@ static unsigned message_number(const char *fmt) {
return 1000 + h % 9000;
}
+/**
+ * @brief Print a message on standard error, prefixed with the shell's name and,
+ * in a script, the current line number.
+ * @param fmt printf() format.
+ * @param ap Arguments.
+ * @param severity 'W' for a warning, 'E' for an error.
+ */
static void vwarn(const char *fmt, va_list ap, char severity) {
out_flush(&out1);
if (opt.msgid)
diff --git a/src/zos_csi.s b/src/zos_csi.s
deleted file mode 100644
index aa77826..0000000
--- a/src/zos_csi.s
+++ /dev/null
@@ -1,99 +0,0 @@
-***********************************************************************
-* ZASHCSI - call IGGCSI00 (Catalog Search Interface) from AMODE 64
-*
-* int ZASHCSI(csi_block *block); #pragma linkage(ZASHCSI, OS)
-*
-* IGGCSI00 runs AMODE 31 and takes a standard three-word parameter
-* list. zash is 64-bit XPLINK, so sys_zos.c keeps everything the
-* routine needs in one csi_block in 31-bit storage, below 2 GB, with
-* that parameter list at its head:
-*
-* +0 A(reason), A(field), A(work) with the VL bit
-* +12 72-byte save area handed to IGGCSI00
-* +84 reason area
-* +88 16 doublewords: the caller's R14-R13
-* +216 the entry point LOAD gave, 0 until the first call
-* +220 return code from IGGCSI00
-* +224 CL8'IGGCSI00' in IBM-1047, for LOAD
-*
-* Entry (OS linkage from 64-bit C, no stack):
-* R1 -> parameter list of doublewords; the first is the block
-* R14 = return address (64-bit)
-* Exit:
-* R15 = IGGCSI00's return code, sign-extended
-* R0-R14 as on entry
-*
-* The glue stores nothing through the caller's R13: under XPLINK the
-* caller's save area is not one the routine may use, so the caller's
-* registers are kept in the block instead. IGGCSI00 saves and restores
-* the low halves of R2-R14 and never touches a high half, so the
-* registers come back whole and the return code is the only thing left
-* to keep: it is stored in the block because the exit path needs R15 to
-* reach the block.
-*
-* The CSECT is RMODE 31 because SAM31 branches to the next instruction
-* with the address cut to 31 bits: that only works below 2 GB.
-*
-* Assemble: as -mgoff -o src/zos_csi.o src/zos_csi.s
-***********************************************************************
-PARM EQU 0 three-word CSI parameter list
-SAVE EQU 12 save area for IGGCSI00
-REGS EQU 88 the caller's R14..R13
-EP EQU 216 EPA from LOAD, 0 until loaded
-RC EQU 220 return code from IGGCSI00
-NAME EQU 224 CL8'IGGCSI00' in IBM-1047
-NOLOAD EQU 16 the failure code (sys_zos.c)
-ZASHCSI CSECT
-ZASHCSI AMODE 64
-ZASHCSI RMODE 31
- SYSSTATE AMODE64=YES
- LG R8,0(R1) R8 = block (below 2 GB)
- STMG R14,R13,REGS(R8) save R14,R15,R0..R13 in the block
- LA R13,SAVE(R8) save area for IGGCSI00
-* Load IGGCSI00 once; the entry point stays in the block, so
-* only the first search pays for the directory search. It is
-* never DELETEd: MVS drops it when the shell ends.
- LLGT R2,EP(R8)
- LTR R2,R2
- JNZ ZCSICALL
- LA R3,NAME(R8)
- SAM31
- SYSSTATE AMODE64=NO
- LOAD EPLOC=(R3),ERRET=ZCSIFAIL
- SYSSTATE AMODE64=YES
- SAM64
- ST R0,EP-SAVE(R13) R13 = block+SAVE
- LLGT R2,EP-SAVE(R13)
- LTR R2,R2
- JZ ZCSIFAIL
-ZCSICALL DS 0H
- LA R1,PARM(R8) R1 -> parameter list at block+0
- SAM31
- BASR R14,R2
- SAM64
- ST R15,RC-SAVE(R13) keep the return code
-ZCSIRET DS 0H
- LGR R15,R13
- AGHI R15,-SAVE R15 = block
- LG R14,REGS(R15) caller's R14
- LMG R0,R13,REGS+16(R15) caller's R0..R13
- LGF R15,RC(R15) return code, sign-extended
- BR R14
-ZCSIFAIL DS 0H
- LGHI R15,NOLOAD
- ST R15,RC-SAVE(R13)
- J ZCSIRET
- LTORG
-
-***********************************************************************
-* Register equates
-***********************************************************************
-R0 EQU 0
-R1 EQU 1
-R2 EQU 2
-R3 EQU 3
-R8 EQU 8
-R13 EQU 13
-R14 EQU 14
-R15 EQU 15
- END ZASHCSI