From 6c64d97e70984c77be3cd8d4e03e9adf717e02fb Mon Sep 17 00:00:00 2001 From: user Date: Thu, 16 Jul 2026 08:25:35 +0200 Subject: shell: I/O redirection (>/<) and pipes (|); rewrite shell in C - kernel I/O routing: PCB gains PROC_STDIN/PROC_STDOUT (default console $FF), inherited across fork. KGetc/KPutc route read/write/putc to the console or a file fd. New syscalls: putc(16), setin(17), setout(18), lookup(19). read/write now go through the routing; putc/puts/nl use SYS_PUTC. - sys_lookup + NameTable move command-name resolution into the kernel. - shell rewritten in C (c/sh.c): tokenizes a line, parses > / < / |, and drives fork+setin/setout+exec+wait. Pipes run as 'a > __pipe ; b < __pipe' (temp file). asm shell + cmdtab removed; StrEqual/SkipName kept for sys_lookup. - libc: fork/exec/wait/setin/setout/lookup wrappers. - verified: echo>file, cat file, wc A = one input byte from the console (blocking). -; Uses an external-clock serial transfer as a clean RX: it completes only when -; the host has a byte (no stdout echo). While waiting we yield so other procs -; run. Preserves BC/DE/HL (the context switch saves/restores them). +; CurStdin / CurStdout -> A = current process PROC_STDIN / PROC_STDOUT ; ----------------------------------------------------------------------------- -sys_read: +CurStdin: + ld a, [wCurProc] + add PROC_STDIN + ld l, a + ld a, [wCurProc+1] + adc 0 + ld h, a + ld a, [hl] + ret +CurStdout: + ld a, [wCurProc] + add PROC_STDOUT + ld l, a + ld a, [wCurProc+1] + adc 0 + ld h, a + ld a, [hl] + ret + +; ----------------------------------------------------------------------------- +; KGetc -> A = byte, CF set on EOF. Reads this process's stdin (console/file). +; ----------------------------------------------------------------------------- +KGetc:: + call CurStdin + cp STD_CONSOLE + jr z, .console + ld b, a + jp sys_getb ; B=fd -> A=byte, CF=EOF +.console .poll ld a, $80 - ld [rSC], a ; request a byte (external clock) + ld [rSC], a ld a, [rSC] bit 7, a - jr z, .got ; bit7 clear => transfer completed - call SchedYield ; nothing yet: let others run, then retry + jr z, .got + call SchedYield jr .poll .got ld a, [rSB] + and a ; CF = 0 + ret + +; ----------------------------------------------------------------------------- +; KPutc(E = byte) - write to this process's stdout (console/file). +; ----------------------------------------------------------------------------- +KPutc:: + ld a, e + ld [wKChar], a + call CurStdout + cp STD_CONSOLE + jr z, .console + ld b, a ; fd + ld a, [wKChar] + ld e, a + jp sys_putb ; B=fd, E=byte +.console + ld a, [wKChar] + ld [rSB], a + ld a, $81 + ld [rSC], a + ret + +; ----------------------------------------------------------------------------- +; sys_putc(E = byte), sys_setin/sys_setout(B = fd) +; ----------------------------------------------------------------------------- +sys_putc: + call KPutc + ret +sys_setin: + ld a, [wCurProc] + add PROC_STDIN + ld l, a + ld a, [wCurProc+1] + adc 0 + ld h, a + ld a, b + ld [hl], a + ret +sys_setout: + ld a, [wCurProc] + add PROC_STDOUT + ld l, a + ld a, [wCurProc+1] + adc 0 + ld h, a + ld a, b + ld [hl], a + ret + +; ----------------------------------------------------------------------------- +; sys_lookup(DE = name) -> A = program id, or $FF if not a known command. +; ----------------------------------------------------------------------------- +sys_lookup: + ld h, d + ld l, e ; HL = query name + ld de, NameTable +.l + ld a, [de] + or a + jr z, .none + push hl + push de + call StrEqual ; HL=query vs DE=table name -> Z if equal + pop de + pop hl + jr z, .match + call SkipName ; DE -> NUL + inc de ; -> id + inc de ; -> next entry + jr .l +.match + call SkipName + inc de ; -> id + ld a, [de] + ret +.none + ld a, $FF + ret + +; ----------------------------------------------------------------------------- +; sys_read() -> A = one input byte from the console (blocking). +; Uses an external-clock serial transfer as a clean RX: it completes only when +; the host has a byte (no stdout echo). While waiting we yield so other procs +; run. Preserves BC/DE/HL (the context switch saves/restores them). +; ----------------------------------------------------------------------------- +sys_read: + call KGetc ; A=byte, CF=EOF (file); console EOF is $04 + ret nc + ld a, $04 ; file EOF -> EOT ret ; ----------------------------------------------------------------------------- -- cgit v1.3.1-sl0p