summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-29 10:15:46 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-29 10:15:46 -0600
commitada6177eebef6a67db1be3f13ba67e1128d07e43 (patch)
treed76871ff079d4660d261501cfa212269121bb480
parent8390414d1e26510be7270cd6f7bd00d1618b0aec (diff)
downloadwhoson-fix/emit-no-cvt.tar.gz
Stop changing the shell's character conversionfix/emit-no-cvt
whoson armed F_CONTROL_CVT on its own stdout and stderr instead of converting for a file, and restored them from QUERYCVT, which reports the setting in effect ("on" for a 1047-tagged ssh pty) rather than the explicit setting. Both are the shell's open file descriptions, so after any run the shell wrote every prompt, echo and builtin as EBCDIC at an ASCII terminal: undecodable glyphs, session ruined until the terminal was reset. emit() keeps the descriptor rule it had, and untagged descriptors are now tagged ISO8859-1 only when they are regular files. A terminal or pipe keeps its own tag and is converted, which is the path that arrives readable; a tagged file keeps getting ASCII or EBCDIC as before. The conversion setting is switched off for stdout and stderr before exit, from the fault handler as well, so the shell is left in the state its own writes expect. Verified on z/OS: table, --help, --version, `| head`, `> file`, `> 1047 file` readable; every echo after each of those, and after make and make clean, still ASCII.
-rw-r--r--CHANGELOG.md22
-rw-r--r--Makefile10
-rw-r--r--README.md30
-rw-r--r--whoson.c105
4 files changed, 110 insertions, 57 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ca5c519..e9e9534 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,12 +3,14 @@
## Unreleased
- Running `whoson` no longer changes what the shell writes after it exits.
- It used to switch character conversion on for its own stdout and stderr,
- which are the shell's open file descriptions, so on an ASCII terminal the
- next prompt, echo or builtin came out as EBCDIC bytes, rendering as
- undecodable glyphs until the terminal was reset. `emit()` now converts an
- EBCDIC destination itself, and a terminal or pipe always gets ASCII, so the
- table stays readable through `head`, `grep` or `od` as well.
+ It switched character conversion on for its own stdout and stderr, which
+ are the shell's open file descriptions, and put back what `QUERYCVT`
+ reported, which is "on" for a 1047-tagged ssh pty: the shell then wrote
+ every later prompt, echo and builtin as EBCDIC at an ASCII terminal, and
+ the session stayed unreadable until the terminal was reset. The setting is
+ now switched off before exit. Untagged descriptors are only tagged when
+ they are regular files, so the table stays readable through `head`, `grep`
+ or `od` as well as at the terminal.
- The CPU column prints a compact duration (`2.34s`, `12m31s`, `1h02m`,
`3d04h`) instead of `hours:minutes:seconds`, so a long session no longer
reads `1947:50:11`.
@@ -18,9 +20,11 @@
- The CSCB is validated by its eye-catcher and `CHASID`, and the ASSB by its
eye-catcher, before either supplies a userid, logon procedure or CPU time.
- The runtime's own diagnostics, and the `ps` child's storage report, are
- written as text instead of EBCDIC: an untagged stdout or stderr is tagged
- ISO8859-1 at startup, so `make check` no longer fills an ASCII terminal or
- log with undecodable bytes.
+ written as text instead of EBCDIC: a stdout or stderr that is a regular file
+ is tagged ISO8859-1 at startup, so `make check > log 2>&1` gets readable
+ text. An interactive `make check` still has the runtime write its report
+ straight at the terminal, which is the runtime's own choice of descriptor
+ and not something `emit()` can convert.
- The source follows `C-PROGRAMMING-STANDARDS.md`: C23 (`nullptr`,
`constexpr` widths, `static_assert`), a Doxygen block on every function,
type and global, `-Wstrict-prototypes -Wmissing-prototypes`, and
diff --git a/Makefile b/Makefile
index 992b880..12268f9 100644
--- a/Makefile
+++ b/Makefile
@@ -17,11 +17,11 @@
# the ASCII mode is ever dropped, -m64 goes with it.
#
# ASCII character mode is deliberate: the source is tagged ISO8859-1, so
-# literals stay ASCII. Output is converted from the descriptor's tag by
-# emit(): a file becomes ISO8859-1 (CCSID 819), or receives EBCDIC converted
-# with __a2e_l() when it is tagged IBM-1047; a terminal or pipe gets ASCII
-# whatever its tag says. _OPEN_SYS and _OPEN_SYS_FILE_EXT expose __fchattr(),
-# attrib_t and F_CONTROL_CVT (which tells read() what ps wrote).
+# literals stay ASCII. Output is converted by the C runtime from the
+# descriptor's tag (emit()): an untagged file is tagged ISO8859-1 (CCSID 819),
+# a terminal, pipe or EBCDIC file IBM-1047, and the tag also tells read() what
+# ps wrote. _OPEN_SYS and _OPEN_SYS_FILE_EXT expose __fchattr(), attrib_t and
+# F_CONTROL_CVT.
#
# Every build is the release build: -O3, no -g, and a z/OS program object has
# no symbol table to remove.
diff --git a/README.md b/README.md
index eaf297a..e0e8f79 100644
--- a/README.md
+++ b/README.md
@@ -125,20 +125,22 @@ once, as they are copied.
Output does not go through `printf`, and it does not guess from `isatty()`.
The source and the program are ISO8859-1 (CCSID 819). `emit()` follows the
-[zopen][zopen] convention where it describes the reader: a text file is
-tagged
-ISO8859-1 and receives ASCII (an untagged file is tagged 819 first, so
-`whoson > out` produces a tagged ASCII file), and a file tagged IBM-1047 is
-given EBCDIC, converted with `__a2e_l()`. A terminal or a pipe gets the ASCII
-bytes whatever its tag says: an ssh pty is tagged IBM-1047 even though the
-client reading it decodes ASCII, so the rule for a terminal is the readable
-one, not the labelled one. The descriptor's conversion setting
-(`fcntl(F_CONTROL_CVT)`) is never changed, because a pty is shared with the
-shell that started `whoson`: switching it on made the shell's next prompt and
-every later builtin write come out as EBCDIC at an ASCII terminal. That
-works the same whether `_BPXK_AUTOCVT` is on or off, and on both a tty and a
-pipe, where `printf` and a raw `write()` each get one of the two wrong. Help
-and error text go through the same `emit()`.
+[zopen][zopen] convention, with one correction that this platform needs. A
+text file is tagged ISO8859-1 and receives ASCII (an untagged file is tagged
+819 first, so `whoson > out` produces a tagged ASCII file), and a file or a
+terminal is written as IBM-1047 with the C runtime converting the ASCII
+writes through `fcntl(F_CONTROL_CVT)`. Only a regular file is tagged: an ssh
+pty is labelled IBM-1047 while the client decoding it still wants ASCII, and
+tagging a pipe or terminal 819 makes the runtime pass the raw ASCII through
+undecoded, which garbles it.
+
+The conversion setting belongs to the open file description, and standard
+output and error are the shell's, so it is switched off again before exit
+(also from the fault handler). `QUERYCVT` cannot do that restore: it reports
+the setting in effect, which is "on" for a 1047-tagged pty, and re-applying
+it left the shell writing EBCDIC at the terminal after `whoson` exited --
+the next prompt, every later builtin, all undecodable until the terminal was
+reset. Help and error text go through the same `emit()`.
[zopen]: https://zopen.community/Guides/CommonSolutions
[zopen-guide]: https://zopen.community/Guides/Porting
diff --git a/whoson.c b/whoson.c
index de40c57..f22e5d6 100644
--- a/whoson.c
+++ b/whoson.c
@@ -32,16 +32,23 @@
* synchronously for an unmapped common-storage read; ISO C leaves that
* implementation-defined, but the platform does it.
*
- * Character conversion follows the zopen conventions where they describe the
- * reader: source and output are ISO8859-1 (CCSID 819), a text file with no
- * tag (or a non-EBCDIC tag) is tagged 819 and receives ASCII, and a file
- * tagged IBM-1047 receives EBCDIC, converted here with __a2e_l(). A terminal
- * or pipe is written as ASCII whatever its tag says, because an ssh pty is
- * tagged IBM-1047 while the client that reads it decodes ASCII. A
- * descriptor's own conversion setting is never changed: it belongs to the
- * open file description, which the calling shell shares, and switching it on
- * left the shell writing EBCDIC at a terminal after whoson exited. Nothing is
+ * Character conversion follows the zopen conventions: source and output are
+ * ISO8859-1 (CCSID 819) and the descriptor's tag says what the destination
+ * expects. A text file with no tag (or a non-EBCDIC tag) is tagged 819 and
+ * receives ASCII; an EBCDIC file, terminal or pipe is written as IBM-1047 and
+ * the C runtime converts the ASCII writes (fcntl F_CONTROL_CVT). Nothing is
* decided from isatty(), and no byte is converted twice.
+ *
+ * Only a regular file is tagged here. An ssh pty is labelled IBM-1047 while
+ * the client decoding it still wants ASCII, so tagging a terminal or a pipe
+ * 819 would make emit() write raw ASCII at it and garble the output: those
+ * keep their tag and are converted, which is what arrives readable.
+ *
+ * Standard output and standard error are the calling shell's own open file
+ * descriptions, so their conversion setting is turned off before exit. It
+ * cannot be put back from QUERYCVT: that reports the setting in effect (on
+ * for a 1047-tagged pty), and arming it left the shell writing EBCDIC at the
+ * terminal after whoson exited.
*/
#include <ctype.h>
@@ -255,15 +262,19 @@ static sigjmp_buf fault_env;
/** @brief Non-zero while a guarded read is in progress. */
static volatile sig_atomic_t fault_armed;
+/** @brief Turn conversion off for the shell's descriptors, from a handler. */
+static void disarm_cvt(void);
+
static void vmessage(bool usage, const char *fmt, va_list ap) PRINTFLIKE(2, 0);
static void error_msg(const char *fmt, ...) PRINTFLIKE(1, 2);
static void usage_error(const char *fmt, ...) PRINTFLIKE(1, 2);
/** @brief Turn a faulting read of an address space that went away into a
- * skipped row; otherwise die. */
+ * skipped row; otherwise leave the shell's descriptors alone and die. */
static void on_fault(int sig) {
if (fault_armed)
siglongjmp(fault_env, 1);
+ disarm_cvt();
_exit(128 + sig);
}
@@ -370,6 +381,20 @@ static int set_cvt(int fd, int cmd, int pccsid, int fccsid) {
return fcntl(fd, F_CONTROL_CVT, &c);
}
+/**
+ * @brief Turn automatic conversion off for stdout and stderr.
+ *
+ * The setting belongs to the open file description, which the calling shell
+ * keeps after whoson exits, so leaving ours behind would change the shell's
+ * own output. Turning it off, rather than re-applying what QUERYCVT reported,
+ * is what makes the shell's writes come out as the ASCII it sends. Only
+ * fcntl(), which is async-signal-safe, is used here.
+ */
+static void disarm_cvt(void) {
+ (void)set_cvt(STDOUT_FILENO, SETCVTOFF, 0, 0);
+ (void)set_cvt(STDERR_FILENO, SETCVTOFF, 0, 0);
+}
+
/** @brief Whether a stat structure says the file is EBCDIC text. */
static bool ebcdic_tag(const struct stat *st) {
return st->st_tag.ft_txtflag &&
@@ -379,27 +404,47 @@ static bool ebcdic_tag(const struct stat *st) {
/**
* @brief Write buf in the character set fd expects.
*
- * A regular file follows its tag: an untagged one is tagged ISO8859-1 and
- * gets ASCII, an EBCDIC one gets EBCDIC. A terminal, pipe or socket gets the
- * ASCII bytes whatever its tag says - an ssh pty is tagged IBM-1047 although
- * the client on the other end decodes ASCII, so the tag does not describe
- * what the reader wants. The descriptor's conversion setting is never
- * changed: a pty is shared with the calling shell, and switching it on made
- * the shell's own later output EBCDIC.
+ * Files are the ASCII half of the zopen rule; terminals, pipes and
+ * EBCDIC-tagged files are IBM-1047. Only a file's tag is changed (an untagged
+ * file becomes ISO8859-1); a pipe or pty keeps its tag and just gets the
+ * conversion, so the runtime's own output to the same descriptor is not
+ * relabelled.
* @param fd descriptor
- * @param buf ASCII bytes; converted in place when the destination is an
- * EBCDIC file
+ * @param buf ASCII bytes; converted in place when the destination wants
+ * EBCDIC
* @param len byte count
*/
static void emit(int fd, char *buf, size_t len) {
struct stat st;
+ bool ascii = false;
+ int fccsid = 1047;
- if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
- if (!st.st_tag.ft_txtflag || st.st_tag.ft_ccsid == 0)
- (void)tag_fd(fd, 819);
- else if (ebcdic_tag(&st))
- __a2e_l(buf, len);
+ if (fstat(fd, &st) == 0) {
+ if (S_ISREG(st.st_mode)) {
+ ascii = !ebcdic_tag(&st);
+ if (ascii && (!st.st_tag.ft_txtflag || st.st_tag.ft_ccsid == 0))
+ (void)tag_fd(fd, 819);
+ } else if (S_ISSOCK(st.st_mode)) {
+ ascii = true;
+ } else if (st.st_tag.ft_txtflag && st.st_tag.ft_ccsid != 0 &&
+ !ebcdic_tag(&st)) {
+ ascii = true; /* a pipe or pty that asked for ASCII */
+ } else if (ebcdic_tag(&st)) {
+ fccsid = (int)st.st_tag.ft_ccsid;
+ }
+ }
+ if (ascii) {
+ (void)set_cvt(fd, SETCVTOFF, 0, 0);
+ write_all(fd, buf, len);
+ return;
}
+ if (set_cvt(fd, SETCVTON, 819, fccsid) == 0) {
+ write_all(fd, buf, len);
+ return;
+ }
+ /* No fcntl support (fstat failed): convert by hand. */
+ (void)set_cvt(fd, SETCVTOFF, 0, 0);
+ __a2e_l(buf, len);
write_all(fd, buf, len);
}
@@ -621,18 +666,19 @@ static void ascb_scan(uint32_t entries, unsigned maxu, space *snap,
}
}
-/** @brief Tag an untagged pipe or file ISO8859-1 before anything writes.
+/** @brief Tag an untagged regular file ISO8859-1 before anything writes.
*
* The runtime prints its own diagnostics -- and the ps child prints its
- * storage report -- as EBCDIC text, which reaches an ASCII terminal or log
- * as a wall of undecodable bytes unless the descriptor says it is text.
- * A terminal carries its own tag and is left alone.
+ * storage report -- as EBCDIC text, which reaches an ASCII log as a wall of
+ * undecodable bytes unless the descriptor says it is text. A terminal or a
+ * pipe is left alone: it carries the tag the environment gave it, and emit()
+ * converts for it.
* @param fd descriptor to tag when it has no text tag yet
*/
static void tag_untagged(int fd) {
struct stat st;
- if (fstat(fd, &st) != 0 || S_ISCHR(st.st_mode))
+ if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode))
return;
if (st.st_tag.ft_txtflag && st.st_tag.ft_ccsid != 0)
return;
@@ -1257,6 +1303,7 @@ int main(int argc, char **argv) {
tag_untagged(STDOUT_FILENO);
tag_untagged(STDERR_FILENO);
+ (void)atexit(disarm_cvt);
for (arg = 1; arg < argc; arg++) {
if (strcmp(argv[arg], "-h") == 0 || strcmp(argv[arg], "--help") == 0) {
emit_str(STDOUT_FILENO, usage_text);