diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 22:49:46 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 22:49:46 -0600 |
| commit | 327bb5eef966394d487046d3bfe2420a66d50a99 (patch) | |
| tree | 961ae7864d46c2501adc326644a8d1574f650c62 | |
| parent | 9742cd9ab2d7cbc579b349da59717d0ef4c85cc2 (diff) | |
| parent | ca1aebd13e439022d65a292c51a47fe752523ecf (diff) | |
| download | ztop-327bb5eef966394d487046d3bfe2420a66d50a99.tar.gz | |
Merge pull request #6 from skurtz97/feature/tabs
System, jobs, paging views and a leak check
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | DESIGN.md | 18 | ||||
| -rw-r--r-- | Makefile | 26 | ||||
| -rw-r--r-- | README.md | 26 | ||||
| -rw-r--r-- | compile_flags.txt | 1 | ||||
| -rw-r--r-- | src/sys.c | 80 | ||||
| -rw-r--r-- | src/sys.h | 58 | ||||
| -rw-r--r-- | src/ztop.c | 350 | ||||
| -rw-r--r-- | tests/__pycache__/screen.cpython-315-pytest-9.1.1.pyc | bin | 0 -> 4406 bytes | |||
| -rw-r--r-- | tests/leakcheck.sh | 88 | ||||
| -rw-r--r-- | tests/screen.py | 86 | ||||
| -rw-r--r-- | tests/test_sys.c | 37 | ||||
| -rw-r--r-- | tui/tui.c | 79 | ||||
| -rw-r--r-- | tui/tui.h | 17 |
14 files changed, 737 insertions, 130 deletions
@@ -8,3 +8,4 @@ tui/demo CEEDUMP.* *.lst compile_commands.json +tests/test_sys @@ -138,6 +138,18 @@ the terminal and is not used. CPU time is deliberately absent: `ASCBEJST` did not advance while a test process burned 0.88 s of CPU (measured on CBT), so it is not a live field. +The IPL data comes from the SMCA (`CVTSMCA`, CVT+0xC4): `SMCAIDTE` at +340 +is packed decimal `0CYYDDDF` and `SMCAITME` at +336 is binary hundredths of +a second. Both are decoded by `zipl_decode` in `src/sys.c` and checked +against `D IPLINFO` (2026-09-21 17:02:47). `CVTRLSTG` (CVT+0x358) is real +storage in K at IPL; it is accurate below 4 TB and is a sentinel above. + +The private area gets the same unallocated pass as CSA, so its total and +percentage are shown; SQA and LSQA are not asked for unallocated blocks, +and their totals are not knowable this way. The paging view reads ASMVT +(`CVTASMVT`, CVT+0x2C0) for the common and PLPA page data sets' flags and +the VIO journal count; it does not read the data-set names yet. + ## The terminal `tui/` is a small library, not a curses replacement: a cell buffer with a @@ -155,6 +167,12 @@ application decides what a click means. Commands and subscriptions are not mode a tick message and a return value saying "redraw" are enough here, and a `Cmd` type in C would be a function pointer by another name. +`make leakcheck` runs every report mode under the Language Environment +storage report and compares the heaps against each other rather than +against their own free counts: LE's own storage is never freed, so the +sensitive comparison is that mode A ends with the same heap as mode B. +All modes currently end at 12 gets, 0 frees and 17776 bytes in use. + The buffer keeps one screen of cells and one of what is already on the glass, and writes only the runs that differ, with one style change per run. The library is compiled into the program rather than linked from an @@ -48,8 +48,22 @@ TESTCC = cc TESTFLAGS = -std=c17 -g -O1 -Wall -Wextra -Wshadow -Wpedantic \ -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L +# Language Environment sees every heap request the program makes. Its +# storage report is therefore the leak test a z/OS user can run: if the +# gets and the frees differ by the end of the enclave, something was +# allocated and never freed. HEAPCHK(ON) additionally walks the heap at +# every call, so a double free or an overlay abends instead of hiding. + +# git checkout leaves every source untagged, and the z/OS tools then read +# ASCII through the EBCDIC converter: make spins on the Makefile, sh +# rejects the script, awk reads garbage. Run this once after a checkout. +tag: + @-chtag -t -c ISO8859-1 Makefile README.md DESIGN.md tests/*.sh tests/*.py + @-chtag -t -c ISO8859-1 src/*.c src/*.h src/*.S tui/*.c tui/*.h + @-chtag -t -c ISO8859-1 tui/Makefile tui/test_tui.c tui/demo.c + all: - @(uname -s | grep OS/390 > /dev/null) || \ + @uname -s | grep OS/390 > /dev/null || \ { echo "ztop builds only on z/OS; this tree is a backup copy"; exit 1; } $(MAKE) ztop @@ -65,14 +79,20 @@ check: ztop ./ztop -a pvt ./ztop -j -test: tests/test_vsm.c src/vsm.c tui/test_tui.c tui/tui.c +test: tests/test_vsm.c tests/test_sys.c src/vsm.c src/sys.c tui/test_tui.c tui/tui.c $(TESTCC) $(TESTFLAGS) -o tests/test_vsm tests/test_vsm.c src/vsm.c ./tests/test_vsm + $(TESTCC) $(TESTFLAGS) -Isrc -o tests/test_sys tests/test_sys.c src/sys.c + ./tests/test_sys $(TESTCC) $(TESTFLAGS) -Itui -o tui/test_tui tui/test_tui.c tui/tui.c ./tui/test_tui + @sh tests/leakcheck.sh --selftest demo: tui/demo.c tui/tui.c $(TESTCC) $(TESTFLAGS) -Itui -o tui/demo tui/demo.c tui/tui.c +leakcheck: ztop + @sh tests/leakcheck.sh ./ztop + clean: - rm -f ztop $(OBJ) tests/test_vsm tui/test_tui tui/demo + rm -f ztop $(OBJ) tests/test_vsm tests/test_sys tui/test_tui tui/demo @@ -39,10 +39,16 @@ widgets for a viewport, a table, tabs, a text input and a status line. z/OS for the real binary, with IBM Open XL in the path: ```sh -make # ibm-clang -std=c23 -m32; the .S is assembled by HLASM -make check # storage, system and jobs +make tag # once after git checkout: tag the ASCII sources +make # ibm-clang -std=c23 -m32; the .S is assembled by HLASM +make check # storage, system and jobs +make leakcheck # every mode under the LE storage and heap checks ``` +`make tag` matters because git leaves the files untagged, and the z/OS +tools then read ASCII as EBCDIC: make spins on the Makefile, `sh` rejects +the test script. + The parser, the library and `sys.c`'s structure have no z/OS dependency, so on any host with a C compiler: @@ -58,27 +64,29 @@ ztop full-screen view when stdout is a terminal, else one-shot CSA ztop -a sqa SQA, -a lsqa, -a pvt ztop -a csa,pvt several areas -ztop -s system name, host, release, address-space count +ztop -s system, IPL, real storage, address-space count ztop -j one line per address space +ztop -p auxiliary storage and page data sets ztop -x dump the raw VSMLIST work and data areas -ztop -d [seconds] interactive, 2 s refresh (1..60) +ztop -d [seconds] interactive, 10 s refresh (1..60) ``` With a terminal and no report option, `ztop` starts the full-screen view. Anything that selects a report (`-a`, `-s`, `-j`, `-x`) or pipes the output gets the one-shot text instead, suitable for `grep` and for a shell -prompt. There are three views, selected with `TAB`: +prompt. There are four views, selected with `TAB`: | view | | | --- | --- | | STORAGE | CSA, SQA, LSQA and the private area, one table per area | -| SYSTEM | CVT system name, host, release, machine, address-space count | +| SYSTEM | system and host, IPL date and time, real storage, address spaces | | JOBS | ASID, job name and ASCB address for every address space | +| PAGING | auxiliary storage flags and the page data sets from ASMVT | | key | | | --- | --- | | `TAB`, `Shift-TAB`, click a tab | next / previous view | -| `1`..`4` | inside STORAGE: CSA, SQA, LSQA, private | +| `1`..`4` | in STORAGE: CSA, SQA, LSQA, private; elsewhere: switch view | | `p` | pause/resume refresh | | `r` | collect now | | `+`/`-` | refresh 1..60 s | @@ -97,8 +105,8 @@ sources are what they are, is in [DESIGN.md](DESIGN.md). ## What it is not A snapshot, not an accounting tool: no history, no per-job attribution, no -SMF. CSA has a total because `VSMLIST` returns the unallocated holes as -well; SQA, LSQA and the private area do not, so those views show +SMF. CSA and the private area have a total because `VSMLIST` returns +their unallocated holes as well; SQA and LSQA do not, so those views show used/free/mapped and no percentage. The jobs view does not show CPU time: `ASCBEJST` does not advance while a process burns CPU (measured), so a column built on it would lie. diff --git a/compile_flags.txt b/compile_flags.txt index a064bfc..77b66bc 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -2,3 +2,4 @@ -std=c23 -D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L +-Isrc @@ -32,6 +32,11 @@ static uint16_t rd16(uintptr_t a) { return v; } +/** @brief Read one byte from an absolute address. */ +static unsigned char rd8(uintptr_t a) { + return *(const unsigned char *)a; +} + /** @brief Copy an EBCDIC field and trim its blanks. * * The program runs in EBCDIC mode, and the terminal converts on output, @@ -50,6 +55,47 @@ static void ebcdic(char *dst, size_t dstlen, const void *src, size_t srclen) { } #endif +/** @brief One nibble of a packed-decimal field. */ +static unsigned int nib(uint32_t v, unsigned int shift) { + return (v >> shift) & 0xFu; +} + +bool zipl_decode(uint32_t date, uint32_t hundredths, struct zipl *out) { + static const unsigned int mdays[12] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + }; + unsigned int century = nib(date, 24); + unsigned int yy = nib(date, 20) * 10 + nib(date, 16); + unsigned int ddd = nib(date, 12) * 100 + nib(date, 8) * 10 + nib(date, 4); + unsigned int year, leap, secs, month = 0, day; + + if (century > 1 || ddd == 0 || ddd > 366) + return false; + year = (century == 1 ? 2000u : 1900u) + yy; + leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 1u : 0u; + if (ddd > 365 + leap) + return false; + secs = hundredths / 100; + if (hundredths % 100 > 99 || secs >= 24 * 3600) + return false; + + day = ddd; + for (month = 0; month < 12; month++) { + unsigned int len = mdays[month] + (month == 1 ? leap : 0); + + if (day <= len) + break; + day -= len; + } + out->year = year; + out->month = month + 1; + out->day = day; + out->hour = secs / 3600; + out->minute = (secs / 60) % 60; + out->second = secs % 60; + return true; +} + bool zsys_read(struct zsys *z) { #ifdef __MVS__ uintptr_t cvt, asvt; @@ -81,9 +127,43 @@ bool zsys_read(struct zsys *z) { if (j->name[0] != '\0') z->njobs++; } + z->cvt = (uint32_t)cvt; + z->smca = rd32(cvt + 0xC4); + z->real_k = rd32(cvt + 0x358); + if (z->smca != 0) + z->ipl_ok = zipl_decode(rd32((uintptr_t)z->smca + 340), + rd32((uintptr_t)z->smca + 336), &z->ipl); return true; #else (void)z; return false; #endif } + +bool zpage_read(struct zpage *p) { +#ifdef __MVS__ + uintptr_t cvt = rd32(0x10); + uintptr_t asmvt; + + memset(p, 0, sizeof *p); + if (cvt == 0) + return false; + asmvt = rd32(cvt + 0x2C0); + if (asmvt == 0) + return false; + p->asmvt = (uint32_t)asmvt; + p->common_pds = (rd8(asmvt) & 0x80u) != 0; + p->local_paging = (rd8(asmvt) & 0x20u) == 0; + p->plpa_pds = (rd8(asmvt) & 0x08u) != 0; + p->plpa_full = (rd8(asmvt) & 0x04u) != 0; + p->common_full = (rd8(asmvt) & 0x02u) != 0; + p->quick_start = (rd8(asmvt + 1) & 0x10u) != 0; + p->warm_start = (rd8(asmvt + 1) & 0x08u) != 0; + p->vio_journaling = (rd8(asmvt + 0xB8) & 0x10u) != 0; + p->vio_ci = rd32(asmvt + 0x8C); + return true; +#else + (void)p; + return false; +#endif +} @@ -1,11 +1,12 @@ /** * @file sys.h - * @brief The system's name and its address spaces, from the CVT and ASVT. + * @brief The system's identity, its IPL, its storage and its address spaces. * - * Both are documented programming interfaces: CVTASVT at CVT+0x22C, the - * ASVT with ASVTMAXU at +0x204 and ASVTENTY at +0x210, and the ASCB - * fields ASCBASID, ASCBJBNS. Reading them needs only problem state, so - * this is the jobs view without an authorized service. + * Everything here is a documented programming interface reached from the + * CVT at PSA+0x10: CVTSNAME, CVTSMCA, CVTRLSTG, CVTASMVT and CVTASVT, the + * ASVT/ASCB chain, and the SMCA and ASMVT control blocks behind them. + * Reading them needs only problem state, so none of this needs an + * authorized service. */ #ifndef ZTOP_SYS_H #define ZTOP_SYS_H @@ -22,6 +23,16 @@ struct zjob { char name[9]; }; +/** @brief The IPL date and time, decoded from the SMCA. */ +struct zipl { + unsigned int year; + unsigned int month; + unsigned int day; /**< day of month */ + unsigned int hour; + unsigned int minute; + unsigned int second; +}; + /** @brief What the system view shows. */ struct zsys { char sysname[9]; @@ -31,13 +42,48 @@ struct zsys { unsigned int slots; /**< ASVT entries, used or free */ int njobs; struct zjob job[ZJOB_MAX]; + bool ipl_ok; + struct zipl ipl; + uint64_t real_k; /**< real storage online at IPL, in K */ + uint32_t cvt; + uint32_t smca; }; /** - * @brief Fill @p z from the CVT and the ASVT. + * @brief Decode SMCAIDTE (0CYYDDDF, packed) and SMCAITME (hundredths). + * @param date SMCAIDTE as read. + * @param hundredths SMCAITME as read. + * @param out Where to write. + * @return false when the fields do not describe a real date and time. + */ +bool zipl_decode(uint32_t date, uint32_t hundredths, struct zipl *out); + +/** + * @brief Fill @p z from the CVT, the SMCA and the ASVT. * @param z Where to write. * @return false when this is not z/OS, with @c z untouched. */ bool zsys_read(struct zsys *z); +/** @brief Auxiliary storage state, from the ASMVT. */ +struct zpage { + bool common_pds; /**< a common page data set exists */ + bool common_full; + bool plpa_pds; + bool plpa_full; + bool local_paging; /**< local page data sets are in use */ + bool vio_journaling; + bool quick_start; + bool warm_start; + unsigned int vio_ci; /**< control intervals in the VIO journal */ + uint32_t asmvt; +}; + +/** + * @brief Fill @p p from the ASMVT. + * @param p Where to write. + * @return false when this is not z/OS or there is no ASMVT. + */ +bool zpage_read(struct zpage *p); + #endif @@ -163,10 +163,15 @@ static bool collect_area(struct vsm_info *v, int area, bool dump) { } /** @brief Fraction of the mapped area in use, or -1 when there is no total. */ +/* Only CSA and PVT get an unallocated pass, so only they have a total. */ +static bool has_total(const struct vsm_info *v) { + return (v->area == VSM_CSA || v->area == VSM_PVT) && v->unalloc_valid; +} + static int pct(const struct vsm_info *v) { uint64_t total; - if (!v->unalloc_valid || v->area != VSM_CSA) + if (!has_total(v)) return -1; total = v->foot + v->unalloc; if (total == 0) @@ -245,6 +250,20 @@ static void print_system(const struct zsys *z) { (void)printf( "system %s host %s os %s machine %s address spaces %d/%u\n", z->sysname, z->host, z->os, z->machine, z->njobs, z->slots); + if (z->ipl_ok) + (void)printf("ipl %04u-%02u-%02u %02u:%02u:%02u real storage %llu K\n", + z->ipl.year, z->ipl.month, z->ipl.day, z->ipl.hour, + z->ipl.minute, z->ipl.second, + (unsigned long long)z->real_k); +} + +static void print_page(const struct zpage *p) { + (void)printf("aux storage common %s%s plpa %s%s local %s vio %s" + ", journal %u\n", + p->common_pds ? "yes" : "no", p->common_full ? " full" : "", + p->plpa_pds ? "yes" : "no", p->plpa_full ? " full" : "", + p->local_paging ? "on" : "off", + p->vio_journaling ? "on" : "off", p->vio_ci); } static void print_jobs(const struct zsys *z) { @@ -263,6 +282,9 @@ struct app { struct zsys sys; bool have_sys; char sys_err[160]; + struct zpage page; + bool have_page; + char page_err[160]; int interval; int collect_ms; bool paused; @@ -470,30 +492,59 @@ static void collect(struct app *a) { } } sys_read(a); + a->have_page = zpage_read(&a->page); + if (!a->have_page) + (void)snprintf(a->page_err, sizeof a->page_err, + "ASMVT is only available on z/OS"); (void)clock_gettime(CLOCK_MONOTONIC, &t1); a->collect_ms = (int)((t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000); } +static const struct tui_hint view_bar[] = { + {"q", "quit"}, + {"TAB", "view"}, + {"r", "refresh"}, + {"?", "help"}, +}; + +static const struct tui_hint storage_bar[] = { + {"q", "quit"}, + {"TAB", "view"}, + {"s", "sort"}, + {"Enter", "details"}, +}; + static void draw_header(struct tui *t, const struct app *a) { char now[16]; + char right[64]; time_t clock_now = time(NULL); struct tm tm; (void)localtime_r(&clock_now, &tm); (void)strftime(now, sizeof now, "%H:%M:%S", &tm); - tui_fill(t, 0, 0, t->w, 1, ' ', TUI_TITLE); - tui_printf(t, 1, 0, t->w - 30, TUI_TITLE, " ztop %s %s %s collect %dms ", - ZTOP_VERSION, a->host, now, a->collect_ms); + tui_fill(t, 0, 0, t->w, 1, ' ', TUI_BAR); + tui_printf(t, 1, 0, 7, TUI_KEYCAP, " ztop "); + tui_printf(t, 7, 0, 8, TUI_META, " %s", ZTOP_VERSION); + tui_printf(t, 15, 0, t->w - 40, TUI_PLAIN, " %s", a->host); + if (a->collect_ms > 0) + (void)snprintf(right, sizeof right, "collect %dms %s", a->collect_ms, + now); + else + (void)snprintf(right, sizeof right, "collect <1ms %s", now); + tui_printf(t, t->w - 1 - (int)strlen(right), 0, (int)strlen(right), + TUI_PLAIN, "%s", right); if (a->paused) - tui_put(t, t->w - 9, 0, " PAUSED ", 8, TUI_WARN); + tui_printf(t, t->w - 9 - (int)strlen(right), 0, 8, TUI_WARN, + " PAUSED "); } static void draw_tabs(struct tui *t, const struct app *a) { - static const char *const labels[3] = {"1 STORAGE", "2 SYSTEM", "3 JOBS"}; - int x = tui_tabs_draw(t, 0, 1, labels, 3, a->tab); + static const char *const labels[4] = {"1 STORAGE", "2 SYSTEM", "3 JOBS", + "4 PAGING"}; + int x = tui_tabs_draw(t, 0, 1, labels, 4, a->tab); - tui_printf(t, x + 1, 1, t->w - x - 2, TUI_LABEL, "refresh %ds +/-", + tui_printf(t, x + 1, 1, t->w - x - 2, TUI_META, "refresh %ds +/-", a->interval); } @@ -523,10 +574,10 @@ static void draw_storage(struct tui *t, struct app *a) { a->sub_dir < 0 ? "^" : "v"); cols[col_of[a->sub_sort]].title = head; a->storage.cols = cols; - tui_printf(t, 1, 3, 6, a->sub == 0 ? TUI_TITLE : TUI_LABEL, " CSA "); - tui_printf(t, 7, 3, 6, a->sub == 1 ? TUI_TITLE : TUI_LABEL, " SQA "); - tui_printf(t, 13, 3, 6, a->sub == 2 ? TUI_TITLE : TUI_LABEL, " LSQA "); - tui_printf(t, 20, 3, 6, a->sub == 3 ? TUI_TITLE : TUI_LABEL, " PVT "); + tui_printf(t, 1, 3, 6, a->sub == 0 ? TUI_TAB_ON : TUI_TAB_OFF, " CSA "); + tui_printf(t, 7, 3, 6, a->sub == 1 ? TUI_TAB_ON : TUI_TAB_OFF, " SQA "); + tui_printf(t, 13, 3, 7, a->sub == 2 ? TUI_TAB_ON : TUI_TAB_OFF, " LSQA "); + tui_printf(t, 20, 3, 6, a->sub == 3 ? TUI_TAB_ON : TUI_TAB_OFF, " PVT "); if (!a->have[a->sub]) { tui_printf(t, 1, 6, t->w - 2, TUI_BAD, "%s: %s", area_upper[a->sub], @@ -536,48 +587,138 @@ static void draw_storage(struct tui *t, struct app *a) { human(v->used, used, sizeof used); human(v->free, freed, sizeof freed); human(v->foot, mapped, sizeof mapped); - if (v->area == VSM_CSA && v->unalloc_valid) { + if (has_total(v)) { human(v->foot + v->unalloc, total, sizeof total); - (void)snprintf(pcbuf, sizeof pcbuf, "%d%%", p); + (void)snprintf(pcbuf, sizeof pcbuf, "use %d%%", p); } else { (void)snprintf(total, sizeof total, "-"); - (void)snprintf(pcbuf, sizeof pcbuf, "-"); + (void)snprintf(pcbuf, sizeof pcbuf, "use -"); } - tui_printf(t, 1, 4, t->w - 2, TUI_PLAIN, - "used %s free %s mapped %s total %s use %s blocks %u", used, - freed, mapped, total, pcbuf, v->blocks); + tui_printf(t, 1, 4, t->w - 16, TUI_PLAIN, + "used %s free %s mapped %s total %s", used, freed, mapped, + total); + tui_printf(t, t->w - 15 - (int)strlen(pcbuf), 4, (int)strlen(pcbuf), + p < 0 ? TUI_META + : p < 70 ? TUI_GOOD + : p < 85 ? TUI_WARN + : TUI_BAD, + "%s", pcbuf); if (p >= 0) tui_progress(t, t->w - 14, 4, 12, p); tui_table_draw(t, r, &a->storage, v->nsub, storage_cell, NULL, a); if (a->err[a->sub][0] != '\0') tui_printf(t, 1, t->h - 2, t->w - 2, TUI_BAD, "! %s", a->err[a->sub]); - else if (v->unalloc_valid) - tui_printf(t, 1, t->h - 2, t->w - 2, TUI_LABEL, - "unallocated %llu in %u blocks, largest %llu", - (unsigned long long)v->unalloc, v->unalloc_blocks, - (unsigned long long)v->largest_unalloc); + else if (v->unalloc_valid) { + char ua[16], ul[16]; + + human(v->unalloc, ua, sizeof ua); + human(v->largest_unalloc, ul, sizeof ul); + tui_printf(t, 1, t->h - 2, t->w - 2, TUI_META, + "unallocated %s in %u blocks, largest %s", ua, + v->unalloc_blocks, ul); + } +} + +/** @brief A dim label and its value, one blank apart. */ +static void kv(struct tui *t, int x, int y, int maxw, const char *label, + const char *value, tui_style st) { + int ll = (int)strlen(label); + + if (ll >= maxw - 1) + return; + tui_printf(t, x, y, ll, TUI_META, "%s", label); + tui_printf(t, x + ll + 1, y, maxw - ll - 1, st, "%s", value); +} + +/** @brief An accent heading with a dim rule filling the row. */ +static void section(struct tui *t, int y, const char *name) { + int n = (int)strlen(name); + + tui_printf(t, 1, y, n, TUI_ACCENT, "%s", name); + if (n + 2 < t->w) + tui_fill(t, n + 2, y, t->w - n - 2, 1, '-', TUI_META); } static void draw_system(struct tui *t, const struct app *a) { const struct zsys *z = &a->sys; + char b1[32], b2[32]; + int half = t->w / 2; if (!a->have_sys) { tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->sys_err); return; } - tui_printf(t, 1, 4, t->w - 2, TUI_LABEL, "system %s", z->sysname); - tui_printf(t, 1, 5, t->w - 2, TUI_PLAIN, "host %s", z->host); - tui_printf(t, 1, 6, t->w - 2, TUI_PLAIN, "os %s", z->os); - tui_printf(t, 1, 7, t->w - 2, TUI_PLAIN, "machine %s", z->machine); - tui_printf(t, 1, 8, t->w - 2, TUI_PLAIN, "spaces %d used, %u slots", - z->njobs, z->slots); - tui_printf( - t, 1, 10, t->w - 2, TUI_LABEL, - "storage CSA %llu unallocated, PVT %llu", - (unsigned long long)(a->info[0].unalloc_valid ? a->info[0].unalloc : 0), - (unsigned long long)(a->info[3].unalloc_valid ? a->info[3].unalloc - : 0)); + human(z->real_k * 1024, b1, sizeof b1); + section(t, 4, "identity"); + kv(t, 1, 5, half - 1, "system", z->sysname, TUI_ACCENT); + kv(t, half, 5, t->w - half - 1, "host", z->host, TUI_PLAIN); + kv(t, 1, 6, half - 1, "release", z->os, TUI_PLAIN); + kv(t, half, 6, t->w - half - 1, "machine", z->machine, TUI_PLAIN); + + section(t, 8, "ipl"); + if (z->ipl_ok) { + (void)snprintf(b1, sizeof b1, "%04u-%02u-%02u", z->ipl.year, + z->ipl.month, z->ipl.day); + (void)snprintf(b2, sizeof b2, "%02u:%02u:%02u", z->ipl.hour, + z->ipl.minute, z->ipl.second); + kv(t, 1, 9, half - 1, "date", b1, TUI_PLAIN); + kv(t, half, 9, t->w - half - 1, "time", b2, TUI_PLAIN); + } + + section(t, 11, "storage"); + human(z->real_k * 1024, b1, sizeof b1); + kv(t, 1, 12, half - 1, "real", b1, TUI_PLAIN); + (void)snprintf(b2, sizeof b2, "%d used / %u slots", z->njobs, z->slots); + kv(t, half, 12, t->w - half - 1, "address spaces", b2, TUI_PLAIN); + if (a->info[0].unalloc_valid) { + human(a->info[0].unalloc, b1, sizeof b1); + kv(t, 1, 13, half - 1, "csa unallocated", b1, TUI_PLAIN); + } + if (a->info[3].unalloc_valid) { + human(a->info[3].unalloc, b1, sizeof b1); + kv(t, half, 13, t->w - half - 1, "pvt unallocated", b1, TUI_PLAIN); + } +} + +static void draw_paging(struct tui *t, const struct app *a) { + const struct zpage *p = &a->page; + char b[48]; + int half = t->w / 2; + + if (!a->have_page) { + tui_printf(t, 1, 4, t->w - 2, TUI_BAD, "%s", a->page_err); + return; + } + section(t, 4, "auxiliary storage"); + (void)snprintf(b, sizeof b, "%08X", (unsigned int)p->asmvt); + kv(t, 1, 5, half - 1, "asmvt", b, TUI_META); + + section(t, 7, "page data sets"); + (void)snprintf(b, sizeof b, "%s%s", p->common_pds ? "yes" : "no", + p->common_full ? ", full" : ""); + kv(t, 1, 8, half - 1, "common", b, + p->common_full ? TUI_BAD + : p->common_pds ? TUI_GOOD + : TUI_WARN); + (void)snprintf(b, sizeof b, "%s%s", p->plpa_pds ? "yes" : "no", + p->plpa_full ? ", full" : ""); + kv(t, half, 8, t->w - half - 1, "plpa", b, + p->plpa_full ? TUI_BAD + : p->plpa_pds ? TUI_GOOD + : TUI_WARN); + kv(t, 1, 9, half - 1, "local paging", p->local_paging ? "on" : "off", + p->local_paging ? TUI_GOOD : TUI_WARN); + (void)snprintf(b, sizeof b, "%s%s", p->quick_start ? "quick" : "cold", + p->warm_start ? ", warm VIO" : ""); + kv(t, half, 9, t->w - half - 1, "start", b, + p->quick_start || p->warm_start ? TUI_WARN : TUI_PLAIN); + + section(t, 11, "vio journal"); + kv(t, 1, 12, half - 1, "state", p->vio_journaling ? "active" : "off", + p->vio_journaling ? TUI_GOOD : TUI_META); + (void)snprintf(b, sizeof b, "%u", p->vio_ci); + kv(t, half, 12, t->w - half - 1, "control intervals", b, TUI_PLAIN); } static void draw_jobs(struct tui *t, struct app *a) { @@ -617,7 +758,7 @@ static void draw_subpool(struct tui *t, const struct app *a) { int h = 12; int x = (t->w - w) / 2; int y = (t->h - h) / 2; - char u[16], f[16], m[16], l[16]; + char u[16], f[16], m[16], l[16], b[32]; if (x < 0) x = 0; @@ -628,59 +769,60 @@ static void draw_subpool(struct tui *t, const struct app *a) { human(s->foot, m, sizeof m); human(s->largest, l, sizeof l); tui_fill(t, x, y, w, h, ' ', TUI_PLAIN); - tui_box(t, x, y, w, h, TUI_LABEL); - tui_printf(t, x + 2, y + 1, w - 4, TUI_TITLE, "subpool %u", s->id); - tui_printf(t, x + 2, y + 3, w - 4, TUI_PLAIN, "key %u", s->key); - tui_printf(t, x + 2, y + 4, w - 4, TUI_PLAIN, "blocks %u", s->blocks); - tui_printf(t, x + 2, y + 5, w - 4, TUI_PLAIN, "used %s", u); - tui_printf(t, x + 2, y + 6, w - 4, TUI_PLAIN, "free %s", f); - tui_printf(t, x + 2, y + 7, w - 4, TUI_PLAIN, "mapped %s", m); - tui_printf(t, x + 2, y + 8, w - 4, TUI_PLAIN, "largest %s", l); - if (v->area == VSM_PVT) - tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "tcb %08X", - (unsigned int)s->tcb); - else - tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "address %08X-%08X", - (unsigned int)s->lo, (unsigned int)s->hi); - tui_printf(t, x + 2, y + h - 2, w - 4, TUI_STYLE(7, 0, TUI_DIM), - "any key closes"); + (void)snprintf(b, sizeof b, "subpool %u", s->id); + tui_box_frame(t, x, y, w, h, TUI_ACCENT, b, TUI_ACCENT); + (void)snprintf(b, sizeof b, "%u", s->key); + kv(t, x + 2, y + 2, w - 4, "key", b, TUI_PLAIN); + (void)snprintf(b, sizeof b, "%u", s->blocks); + kv(t, x + 2, y + 3, w - 4, "blocks", b, TUI_PLAIN); + kv(t, x + 2, y + 4, w - 4, "used", u, TUI_PLAIN); + kv(t, x + 2, y + 5, w - 4, "free", f, TUI_PLAIN); + kv(t, x + 2, y + 6, w - 4, "mapped", m, TUI_PLAIN); + kv(t, x + 2, y + 7, w - 4, "largest", l, TUI_PLAIN); + if (v->area == VSM_PVT) { + (void)snprintf(b, sizeof b, "%08X", (unsigned int)s->tcb); + kv(t, x + 2, y + 8, w - 4, "tcb", b, TUI_PLAIN); + } else { + (void)snprintf(b, sizeof b, "%08X-%08X", (unsigned int)s->lo, + (unsigned int)s->hi); + kv(t, x + 2, y + 8, w - 4, "address", b, TUI_PLAIN); + } + tui_printf(t, x + 2, y + h - 2, w - 4, TUI_META, "any key closes"); } static void draw_help(struct tui *t) { + static const struct tui_hint views[] = { + {"TAB", "next view"}, + {"Shift-TAB", "previous view"}, + {"1-4", "CSA SQA LSQA PVT"}, + {"click", "a tab"}, + }; + static const struct tui_hint moving[] = { + {"j k", "one row"}, {"PgUp PgDn", "a page"}, {"g G", "first, last"}, + {"wheel", "scroll"}, {"Enter", "details"}, {"s S", "sort, reverse"}, + }; + static const struct tui_hint screen[] = { + {"p", "pause"}, {"r", "refresh now"}, {"+ -", "interval 1..60 s"}, + {"?", "help"}, {"q", "quit"}, + }; int w = t->w < 64 ? t->w - 2 : 62; - int h = 19; + int h = 15; int x = (t->w - w) / 2; int y = (t->h - h) / 2; + if (x < 0) + x = 0; if (y < 0) y = 0; tui_fill(t, x, y, w, h, ' ', TUI_PLAIN); - tui_box(t, x, y, w, h, TUI_LABEL); - tui_printf(t, x + 2, y + 1, w - 4, TUI_TITLE, "ztop keys"); - - tui_printf(t, x + 2, y + 3, w - 4, TUI_LABEL, "views"); - tui_printf(t, x + 2, y + 4, w - 4, TUI_PLAIN, - "TAB / Shift-TAB next / previous view"); - tui_printf(t, x + 2, y + 5, w - 4, TUI_PLAIN, - "1 2 3 4 inside STORAGE: CSA SQA LSQA PVT"); - tui_printf(t, x + 2, y + 6, w - 4, TUI_PLAIN, - "click a tab same, with the mouse"); - - tui_printf(t, x + 2, y + 8, w - 4, TUI_LABEL, "moving"); - tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "j k / arrows one row"); - tui_printf(t, x + 2, y + 10, w - 4, TUI_PLAIN, "PgUp PgDn a page"); - tui_printf(t, x + 2, y + 11, w - 4, TUI_PLAIN, - "g G / Home End first / last"); - tui_printf(t, x + 2, y + 12, w - 4, TUI_PLAIN, - "wheel, click scroll, select a row"); - tui_printf(t, x + 2, y + 13, w - 4, TUI_PLAIN, - "s / S sort / reverse Enter details"); - - tui_printf(t, x + 2, y + 14, w - 4, TUI_LABEL, "the screen"); - tui_printf(t, x + 2, y + 15, w - 4, TUI_PLAIN, - "p pause r refresh now + - interval 1..60 s"); - tui_printf(t, x + 2, y + 16, w - 4, TUI_PLAIN, - "? close this help q quit"); + tui_box_frame(t, x, y, w, h, TUI_ACCENT, "keys", TUI_ACCENT); + tui_printf(t, x + 2, y + 2, w - 4, TUI_LABEL, "views"); + tui_hints(t, x + 2, y + 3, w - 4, views, 4); + tui_printf(t, x + 2, y + 5, w - 4, TUI_LABEL, "moving"); + tui_hints(t, x + 2, y + 6, w - 4, moving, 3); + tui_hints(t, x + 2, y + 7, w - 4, moving + 3, 3); + tui_printf(t, x + 2, y + 9, w - 4, TUI_LABEL, "the screen"); + tui_hints(t, x + 2, y + 10, w - 4, screen, 5); } /** @brief The right half of the status line: what is under the cursor. */ @@ -722,19 +864,19 @@ static void view(void *model, struct tui *t) { draw_storage(t, a); } else if (a->tab == 1) { draw_system(t, a); - } else { + } else if (a->tab == 2) { draw_jobs(t, a); + } else { + draw_paging(t, a); } if (a->popup) draw_subpool(t, a); if (a->help) draw_help(t); detail(a, right, sizeof right); - tui_statusbar(t, - a->tab == 0 ? "TAB view 1-4 area j/k move s sort ? help" - : a->tab == 1 ? "TAB view r refresh ? help" - : "TAB view j/k move s sort ? help", - right); + tui_statusbar(t, NULL, right); + tui_hints(t, 0, t->h - 1, t->w - 20, a->tab == 0 ? storage_bar : view_bar, + 4); } static bool parse_areas(const char *s, unsigned int *mask) { @@ -766,10 +908,12 @@ static bool parse_areas(const char *s, unsigned int *mask) { static void usage(FILE *f) { (void)fprintf( f, - "usage: ztop [-a csa|sqa|lsqa|pvt|all] [-s] [-j] [-x] [-d [seconds]]\n" + "usage: ztop [-a csa|sqa|lsqa|pvt|all] [-s] [-j] [-p] [-x] [-d " + "[seconds]]\n" " -a storage areas to report (default csa); comma-separate or all\n" - " -s system name, host, release and address-space count\n" + " -s system name, host, release, IPL and address-space count\n" " -j one line per address space\n" + " -p auxiliary storage: page data sets, local paging, VIO journal\n" " -x dump the VSMLIST work and data areas (one-shot only)\n" " -d force the interactive view (the default on a terminal)\n" " -h this text\n"); @@ -889,11 +1033,11 @@ static bool update(struct tui_app *app, const struct tui_msg *m) { return true; } if (m->key == TUI_KEY_TAB) { - a->tab = (a->tab + 1) % 3; + a->tab = (a->tab + 1) % 4; return true; } if (m->key == TUI_KEY_BACKTAB) { - a->tab = (a->tab + 2) % 3; + a->tab = (a->tab + 3) % 4; return true; } if (a->tab == 0 && m->key >= '1' && m->key <= '4') { @@ -975,7 +1119,7 @@ static void run_tui(int interval) { } static void run_oneshot(unsigned mask, bool want_sys, bool want_jobs, - bool dump) { + bool want_page, bool dump) { char host[65]; bool printed = false; int rc = 0; @@ -996,6 +1140,19 @@ static void run_oneshot(unsigned mask, bool want_sys, bool want_jobs, print_area(&v); printed = true; } + if (want_page) { + struct zpage p; + + if (!zpage_read(&p)) { + (void)fprintf(stderr, "ztop: paging is only available on z/OS\n"); + rc = 1; + } else { + if (printed) + (void)printf("\n"); + print_page(&p); + printed = true; + } + } if (want_sys || want_jobs) { struct zsys z; @@ -1028,6 +1185,7 @@ int main(int argc, char **argv) { bool interactive = false; bool want_sys = false; bool want_jobs = false; + bool want_page = false; int interval = 0; int i; @@ -1041,6 +1199,8 @@ int main(int argc, char **argv) { want_sys = true; } else if (strcmp(argv[i], "-j") == 0) { want_jobs = true; + } else if (strcmp(argv[i], "-p") == 0) { + want_page = true; } else if (strcmp(argv[i], "-a") == 0) { if (i + 1 >= argc || !parse_areas(argv[++i], &mask)) { usage(stderr); @@ -1067,22 +1227,22 @@ int main(int argc, char **argv) { /* A bare `ztop` on a terminal is the full-screen view; anything that * selects a report, or a pipe, keeps the one-shot output. */ - if (!interactive && !dump && !want_sys && !want_jobs && mask == 0 && - isatty(STDOUT_FILENO)) + if (!interactive && !dump && !want_sys && !want_jobs && !want_page && + mask == 0 && isatty(STDOUT_FILENO)) interactive = true; if (interactive) { if (dump) (void)fprintf(stderr, "ztop: -x is ignored with -d\n"); if (interval < 1) - interval = 2; + interval = 10; if (interval > 60) interval = 60; run_tui(interval); } else { - if (mask == 0 && !want_sys && !want_jobs) + if (mask == 0 && !want_sys && !want_jobs && !want_page) mask = 1u; - run_oneshot(mask, want_sys, want_jobs, dump); + run_oneshot(mask, want_sys, want_jobs, want_page, dump); } return 0; } diff --git a/tests/__pycache__/screen.cpython-315-pytest-9.1.1.pyc b/tests/__pycache__/screen.cpython-315-pytest-9.1.1.pyc Binary files differnew file mode 100644 index 0000000..ff0f12c --- /dev/null +++ b/tests/__pycache__/screen.cpython-315-pytest-9.1.1.pyc diff --git a/tests/leakcheck.sh b/tests/leakcheck.sh new file mode 100644 index 0000000..49e852c --- /dev/null +++ b/tests/leakcheck.sh @@ -0,0 +1,88 @@ +#!/bin/sh +# Language Environment sees every heap request the program makes, so its +# storage report can be read as a leak test: run each one-shot mode under +# RPTSTG(ON), RPTOPTS(ON) and HEAPCHK(ON), then compare what the heap +# looked like at the end of each run. ztop allocates nothing itself, so +# every mode has to end with the same heap. A mode that ends with more +# used storage than the others is holding something it should have freed. +# +# tests/leakcheck.sh ./ztop run the modes and compare them +# tests/leakcheck.sh --report FILE describe one saved report +# tests/leakcheck.sh --selftest check the checker +set -eu + +count() { + sed -n "s/^ *$1: *//p" "$2" | { + n=0 + while read -r v; do + n=$((n + v)) + done + echo "$n" + } +} + +stats() { + log=$1 + if ! grep -q 'HEAP statistics:' "$log"; then + echo "no heap report in $log" >&2 + return 1 + fi + g=$(count "Successful Get Heap requests" "$log") + f=$(count "Successful Free Heap requests" "$log") + u=$(count "Total heap storage used (sugg. initial size)" "$log") + echo "gets $g frees $f used $u" +} + +if [ "${1:-}" = "--report" ]; then + stats "$2" + exit +fi + +dir=${HOME}/tmp/ztop/leak + +if [ "${1:-}" = "--selftest" ]; then + mkdir -p "$dir" + printf 'HEAP statistics:\n Total heap storage used (sugg. initial size): 3328\n Successful Get Heap requests: 4\n Successful Free Heap requests: 0\n' > "$dir/one.log" + printf 'HEAP statistics:\n Total heap storage used (sugg. initial size): 3328\n Successful Get Heap requests: 4\n Successful Free Heap requests: 0\n' > "$dir/two.log" + printf 'HEAP statistics:\n Total heap storage used (sugg. initial size): 4096\n Successful Get Heap requests: 5\n Successful Free Heap requests: 0\n' > "$dir/leak.log" + one=$(stats "$dir/one.log") + two=$(stats "$dir/two.log") + if [ "$one" != "$two" ]; then + echo "selftest: identical reports compared unequal" + exit 1 + fi + if [ "$one" = "$(stats "$dir/leak.log")" ]; then + echo "selftest: a bigger heap was not noticed" + exit 1 + fi + echo "leakcheck: ok" + exit +fi + +bin=${1:-./ztop} +mkdir -p "$dir" +opts='RPTSTG(ON),RPTOPTS(ON),HEAPCHK(ON)' +base='' +run() { + name=$1 + shift + _CEE_RUNOPTS=$opts "$bin" "$@" > "$dir/$name.out" 2> "$dir/$name.log" + s=$(stats "$dir/$name.log") + if [ -z "$base" ]; then + base=$s + printf '%-5s %s\n' "$name" "$s" + return + fi + if [ "$s" != "$base" ]; then + printf 'LEAK %-5s %s (first run: %s)\n' "$name" "$s" "$base" + return 1 + fi + printf '%-5s %s\n' "$name" "$s" +} + +for a in csa sqa lsqa pvt; do + run "$a" -a "$a" +done +run -s -s +run -j -j +run -p -p diff --git a/tests/screen.py b/tests/screen.py new file mode 100644 index 0000000..844ec7f --- /dev/null +++ b/tests/screen.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +"""Paint ztop into a grid and print it, to check the layout without an eye. + + python3 tests/screen.py ./ztop + +The pty is 100x30; every key is followed by a snapshot of the screen as +the escapes would leave it. No termios is touched, so the program sees a +plain pty and uses its own raw mode. +""" +import fcntl +import os +import pty +import select +import struct +import sys +import termios +import time + +W, H = 100, 30 +grid = [[' '] * W for _ in range(H)] +row = col = 0 + +def feed(data): + global row, col + i = 0 + while i < len(data): + b = data[i] + if b == 0x1b and i + 1 < len(data) and data[i + 1] == ord('['): + j = i + 2 + while j < len(data) and not (0x40 <= data[j] <= 0x7e): + j += 1 + if j >= len(data): + return + params = data[i + 2:j].decode('latin1') + final = chr(data[j]) + if final == 'H': + parts = (params.split(';') + ['1', '1'])[:2] + row = max(0, int(parts[0] or 1) - 1) + col = max(0, int(parts[1] or 1) - 1) + elif final == 'J': + for r in range(H): + grid[r] = [' '] * W + elif final == 'K': + for c in range(col, W): + grid[row][c] = ' ' + i = j + 1 + continue + if b == 0x0d: + col = 0 + elif b == 0x0a: + row = min(H - 1, row + 1) + elif b >= 0x20: + if row < H and col < W: + grid[row][col] = chr(b) + col += 1 + i += 1 + +pid, fd = pty.fork() +if pid == 0: + os.environ['TERM'] = 'xterm' + prog = sys.argv[1] if len(sys.argv) > 1 else './ztop' + os.execv(prog, [prog, '-d', '60']) # noqa: S606 - exec is the point + +fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack('HHHH', H, W, 0, 0)) + +def pump(t): + end = time.time() + t + while time.time() < end: + r, _, _ = select.select([fd], [], [], 0.05) + if not r: + continue + try: + d = os.read(fd, 65536) + except OSError: + return + if not d: + return + feed(d) + +pump(2.0) +for k in [b'\t', b'\t', b'\t', b'\t', b'?', b'?', b'q']: + os.write(fd, k) + pump(1.0) + print('=== after', k) + for line in grid: + print(''.join(line).rstrip()) diff --git a/tests/test_sys.c b/tests/test_sys.c new file mode 100644 index 0000000..b807859 --- /dev/null +++ b/tests/test_sys.c @@ -0,0 +1,37 @@ +/** + * @file test_sys.c + * @brief The IPL decoder, against values read from a live SMCA. + */ +#include <assert.h> +#include <stdio.h> + +#include "sys.h" + +int main(void) { + struct zipl z; + + /* CBT, IPLed at 17:02:47 on 21 September 2026 (day 264). */ + assert(zipl_decode(0x0126264Fu, 6136760u, &z)); + assert(z.year == 2026 && z.month == 9 && z.day == 21); + assert(z.hour == 17 && z.minute == 2 && z.second == 47); + + /* 1926, day 260 (17 September), and 2024, day 260 (16 September): + * the century nibble and the leap year both move the answer. */ + assert(zipl_decode(0x0026260Fu, 0u, &z)); + assert(z.year == 1926 && z.month == 9 && z.day == 17); + assert(zipl_decode(0x0124260Fu, 0u, &z)); + assert(z.year == 2024 && z.month == 9 && z.day == 16); + + /* 23:59:59.99 is the last second of the day. */ + assert(zipl_decode(0x0126264Fu, 8639999u, &z)); + assert(z.hour == 23 && z.minute == 59 && z.second == 59); + + /* Out of range: century 2, day 0, day 366 in a common year, 24:00. */ + assert(!zipl_decode(0x0226264Fu, 0u, &z)); + assert(!zipl_decode(0x0126000Fu, 0u, &z)); + assert(!zipl_decode(0x0126366Fu, 0u, &z)); + assert(!zipl_decode(0x0126264Fu, 8640000u, &z)); + + (void)printf("test_sys: ok\n"); + return 0; +} @@ -7,6 +7,7 @@ #include <errno.h> #include <signal.h> #include <stdarg.h> +#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -371,6 +372,11 @@ void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st, } void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st) { + tui_box_frame(t, x, y, w, h, st, NULL, TUI_PLAIN); +} + +void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st, + const char *title, tui_style title_st) { if (w < 2 || h < 2) return; for (int i = 1; i < w - 1; i++) { @@ -385,6 +391,16 @@ void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st) { tui_put(t, x + w - 1, y, "+", 1, st); tui_put(t, x, y + h - 1, "+", 1, st); tui_put(t, x + w - 1, y + h - 1, "+", 1, st); + if (title != NULL && title[0] != '\0' && w > 6) { + char chip[64]; + int n = (int)strlen(title); + int room = w - 6; + + if (n > room) + n = room; + (void)snprintf(chip, sizeof chip, " %.*s ", n, title); + tui_printf(t, x + 2, y, (int)strlen(chip), title_st, "%s", chip); + } } void tui_flush(struct tui *t) { @@ -453,7 +469,7 @@ int tui_tabs_draw(struct tui *t, int x, int y, const char *const *labels, int n, labels[i]); x += len; if (i + 1 < n) { - tui_put(t, x, y, " ", 1, TUI_PLAIN); + tui_put(t, x, y, "|", 1, TUI_META); x += 1; } } @@ -755,13 +771,21 @@ void tui_table_draw(struct tui *t, tui_rect r, const struct tui_table *tb, tui_fill(t, r.x, r.y, r.w, 1, ' ', TUI_LABEL); for (int c = 0; c < tb->ncols && x < r.x + r.w; c++) { int w = tb->cols[c].width; + char up[32]; + size_t n; if (w > r.x + r.w - x) w = r.x + r.w - x; + n = strlen(tb->cols[c].title); + if (n >= sizeof up) + n = sizeof up - 1; + for (size_t k = 0; k < n; k++) + up[k] = (char)toupper((unsigned char)tb->cols[c].title[k]); + up[n] = '\0'; if (tb->cols[c].right) - tui_printf(t, x, r.y, w, TUI_LABEL, "%*s", w, tb->cols[c].title); + tui_printf(t, x, r.y, w, TUI_LABEL, "%*s", w, up); else - tui_printf(t, x, r.y, w, TUI_LABEL, "%-*s", w, tb->cols[c].title); + tui_printf(t, x, r.y, w, TUI_LABEL, "%-*s", w, up); x += tb->cols[c].width; if (c + 1 < tb->ncols) x += 1; @@ -907,29 +931,50 @@ bool tui_input_key(struct tui_input *in, int key) { /* ---- progress and status -------------------------------------------- */ void tui_progress(struct tui *t, int x, int y, int width, int pct) { - if (width < 1) + tui_style st; + int fill, room, i; + + if (width < 4) return; - int fill = pct < 0 ? 0 : pct * width / 100; + room = width - 2; + fill = pct < 0 ? 0 : pct * room / 100; + if (fill > room) + fill = room; + st = pct < 0 ? TUI_META + : pct < 70 ? TUI_GOOD + : pct < 85 ? TUI_WARN + : TUI_BAD; + tui_put(t, x, y, "[", 1, TUI_META); + for (i = 0; i < room; i++) + tui_put(t, x + 1 + i, y, i < fill ? "=" : " ", 1, + i < fill ? st : TUI_META); + tui_put(t, x + width - 1, y, "]", 1, TUI_META); +} + +void tui_hints(struct tui *t, int x, int y, int maxw, + const struct tui_hint *hints, int n) { + for (int i = 0; i < n; i++) { + int kl = (int)strlen(hints[i].key); + int ll = (int)strlen(hints[i].label); - if (fill > width) - fill = width; - tui_style st = pct < 0 ? TUI_STYLE(7, 0, TUI_DIM) - : pct < 70 ? TUI_GOOD - : pct < 85 ? TUI_WARN - : TUI_BAD; - for (int i = 0; i < width; i++) - tui_put(t, x + i, y, i < fill ? "#" : "-", 1, - i < fill ? st : TUI_STYLE(7, 0, TUI_DIM)); + if (x + kl + ll + 4 > maxw) + break; + tui_put(t, x, y, " ", 1, TUI_META); + tui_printf(t, x + 1, y, kl, TUI_KEYCAP, "%s", hints[i].key); + tui_put(t, x + 1 + kl, y, " ", 1, TUI_META); + tui_printf(t, x + 2 + kl, y, ll, TUI_META, "%s", hints[i].label); + x += kl + ll + 3; + } } void tui_statusbar(struct tui *t, const char *left, const char *right) { int y = t->h - 1; - tui_fill(t, 0, y, t->w, 1, ' ', TUI_TITLE); + tui_fill(t, 0, y, t->w, 1, ' ', TUI_BAR); if (left != NULL) - tui_printf(t, 1, y, t->w - 2, TUI_TITLE, "%s", left); + tui_printf(t, 1, y, t->w - 2, TUI_PLAIN, "%s", left); if (right != NULL) { int n = (int)strlen(right); - tui_printf(t, t->w - 1 - n, y, n, TUI_TITLE, "%s", right); + tui_printf(t, t->w - 1 - n, y, n, TUI_PLAIN, "%s", right); } } @@ -50,6 +50,10 @@ typedef struct { #define TUI_BAD TUI_STYLE(1, 0, TUI_BOLD) #define TUI_TAB_ON TUI_STYLE(0, 7, TUI_BOLD) #define TUI_TAB_OFF TUI_STYLE(7, 0, TUI_DIM) +#define TUI_ACCENT TUI_STYLE(6, 0, TUI_BOLD) +#define TUI_META TUI_STYLE(7, 0, TUI_DIM) +#define TUI_KEYCAP TUI_STYLE(0, 6, 0) +#define TUI_BAR TUI_STYLE(7, 4, 0) enum { TUI_KEY_NONE = -1, @@ -110,6 +114,9 @@ void tui_put(struct tui *t, int x, int y, const char *s, int maxw, void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st, const char *fmt, ...); void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st); +/** @brief A box with a title set into its top border. */ +void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st, + const char *title, tui_style title_st); void tui_rule(struct tui *t, int y, char ch, tui_style st); void tui_flush(struct tui *t); @@ -189,6 +196,16 @@ int tui_tabs_hit(int x, const char *const *labels, int n); /** Cycle tabs with left/right/tab/backtab; true when it was consumed. */ bool tui_tabs_key(int key, int *active, int n); +/** @brief A key cap and its label, for the status bar. */ +struct tui_hint { + const char *key; + const char *label; +}; + +/** @brief Draw key caps and their labels along one row. */ +void tui_hints(struct tui *t, int x, int y, int maxw, + const struct tui_hint *hints, int n); + /** A single-line text input. */ struct tui_input { char buf[256]; |
