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 note # redirect stdout to a file +$ cat note +hello +$ wc < note # redirect stdin from a file +1 1 6 +$ cat readme | wc -l # pipe (via a temp file) +2 +$ echo one two three | wc +1 3 14 ``` +Operators must be space-separated (`cat readme > out`). Command names are +resolved by the kernel (`sys_lookup` + `NameTable`). + +**How I/O routing works.** Each process has a `stdin`/`stdout` fd in its PCB +(default `$FF` = console). `read`/`write`/`putc` route through the kernel +(`KGetc`/`KPutc`): console when the fd is `$FF`, otherwise the filesystem +(`getb`/`putb`). The shell forks a child, the child `setin`/`setout`s to open +files, then `exec`s — so the program's I/O lands in the file. A pipe +`a | b` is run as `a > __pipe ; b < __pipe ; rm __pipe`. + ### Process lifecycle (exit / wait / reaping) The classic Unix zombie/reap dance, adapted to banked memory: @@ -295,7 +306,9 @@ $ rm notes - [x] `wc`, `head`, and an `argc/argv` demo (`args`) + `argv_parse` in libc - [x] option parsing (`hasflag`/`optval`): `wc -l/-w/-c`, `head -n N` - [x] a RAM filesystem (WRAMX) + `ls`/`cat`/`save`/`rm`; `wc`/`head` read files -- [ ] more tools (`rev`, `grep`, `tail`), shell redirection (`>`/`|`) +- [x] shell in C with redirection (`>`/`<`) and pipes (`|`, via a temp file) +- [x] per-process stdin/stdout routing (`KGetc`/`KPutc`, `setin`/`setout`) +- [ ] more tools (`rev`, `grep`, `tail`), true concurrent pipes - [ ] battery-backed files (move the FS to cart SRAM), directories - [ ] Preemptive scheduling: real context save in `TimerISR` → `hSwitchTo` - [ ] Use the `$D000-$DFFF` u-area (SVBK) for per-process kernel state / kstack -- cgit v1.3.1-sl0p