| | @@ -368,3 +368,125 @@ |
| 368 | 368 | `ret` does. D's experience is that the corners (fields, generics, |
| 369 | 369 | procedure values) take years; the narrow form here leaves record |
| 370 | 370 | lifetimes out, and so the first false alarm above, on purpose. |
| 371 | + |
| 372 | +## What a shell needs of `Sys` |
| 373 | + |
| 374 | +For concha, the shell written in raz. `examples/Sh.raz` shows how far |
| 375 | +`Sys` goes today: pipelines, `<`, `>`, `>>` and `2>`, and job control |
| 376 | +through `spawn`'s `group` and `foreground`. A shell people use every |
| 377 | +day needs more. Each item below is library growth that breaks no |
| 378 | +program, and each has a C call behind it, for `Sys.raz` over the C |
| 379 | +library. |
| 380 | + |
| 381 | +**Moving file descriptors.** `2>&1`, `3>log`, `exec >out` and `{ a; |
| 382 | +b; } >f` move or copy the shell's own descriptors, around a builtin |
| 383 | +or for good; `Program.fds` covers only a child's first three. |
| 384 | + |
| 385 | +``` |
| 386 | +proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) |
| 387 | +proc dupTo*(fd, to: INT): INT; (* to made the same file as fd, closing what it was *) |
| 388 | +proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd passed to programs spawn starts, or not *) |
| 389 | +``` |
| 390 | + |
| 391 | +`Program` gains `more*: []INT`, descriptors passed on to the program |
| 392 | +at the same numbers, for `3<file cmd` and for `<(...)`, which hands a |
| 393 | +program `/dev/fd/N`. `spawn` closes every other one, as it does now. |
| 394 | +The C calls: `dup`, `dup2`, `fcntl` with `FD_CLOEXEC`. |
| 395 | + |
| 396 | +**Replacing the shell.** `exec cmd` runs a program in the shell's |
| 397 | +place, as does the last command of `sh -c`, saving a process. |
| 398 | + |
| 399 | +``` |
| 400 | +proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't, with minus an errno *) |
| 401 | +``` |
| 402 | + |
| 403 | +`p.group`, `p.foreground` and `p.tty` are as in `spawn`. The C call |
| 404 | +is `execve`. |
| 405 | + |
| 406 | +**Finding programs.** A shell looks a name up along `PATH`, and `test |
| 407 | +-x`, `-r` and `-w` ask whether this user may run, read or write a |
| 408 | +file, which `Info.mode` alone can't say: it depends on the user, the |
| 409 | +file's owner and group, and the file system (read-only, `noexec`). |
| 410 | + |
| 411 | +``` |
| 412 | +proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may, by its real ids; reading, writing, and running *) |
| 413 | +``` |
| 414 | + |
| 415 | +`Access` gains `running`, which `open` refuses. The C call is |
| 416 | +`access`. |
| 417 | + |
| 418 | +**The environment.** `env` reads one variable. A shell starts from |
| 419 | +its whole environment, to list it (`env`, `export -p`) and pass it on |
| 420 | +with what was exported: |
| 421 | + |
| 422 | +``` |
| 423 | +proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) |
| 424 | +``` |
| 425 | + |
| 426 | +Nothing to set it: the shell keeps its own table and passes it whole |
| 427 | +with `onlyEnv`. |
| 428 | + |
| 429 | +**The umask.** `umask` shows and sets the bits taken from every file |
| 430 | +the shell, and what it starts, makes. |
| 431 | + |
| 432 | +``` |
| 433 | +proc umask*(mask: INT): INT; (* the mask set to mask; the one it was *) |
| 434 | +``` |
| 435 | + |
| 436 | +**A terminal's modes, kept for each job.** A job stopped by Ctrl-Z |
| 437 | +in vi leaves the terminal raw; `fg` must give it back the modes it |
| 438 | +had, and the shell its own between. `rawMode` and `restoreMode` keep |
| 439 | +one saved state; a shell needs as many as it has jobs. |
| 440 | + |
| 441 | +``` |
| 442 | +type Mode* = record ... end; (* a terminal's modes; the target's *) |
| 443 | +proc getMode*(fd: INT): (Mode, INT); |
| 444 | +proc setMode*(fd: INT; m: Mode): INT; (* once the output written is sent, as TCSADRAIN *) |
| 445 | +``` |
| 446 | + |
| 447 | +The C calls: `tcgetattr` and `tcsetattr`. |
| 448 | + |
| 449 | +**Time used.** `time pipeline` and `times` report CPU time used, the |
| 450 | +shell's and its children's, which `clock` can't tell. |
| 451 | + |
| 452 | +``` |
| 453 | +proc cpuTime*(children: BOOL): (I64, I64); (* user and system time, in ns: this program's, or its waited-for children's *) |
| 454 | +``` |
| 455 | + |
| 456 | +The C call is `getrusage`. |
| 457 | + |
| 458 | +**Limits.** `ulimit -n`, `-s`, `-c`: |
| 459 | + |
| 460 | +``` |
| 461 | +const limitFiles* = 7; limitStack* = 3; limitCore* = 4; ... (* each target's numbers *) |
| 462 | +proc limit*(which: INT): (I64, I64, INT); (* soft and hard; -1 for none *) |
| 463 | +proc setLimit*(which: INT; soft, hard: I64): INT; |
| 464 | +``` |
| 465 | + |
| 466 | +The C calls: `getrlimit` and `setrlimit`. |
| 467 | + |
| 468 | +**Who and where.** A prompt shows the user, the host, and `#` for |
| 469 | +root; `~` is the home directory, and `~name` someone else's. |
| 470 | + |
| 471 | +``` |
| 472 | +proc uid*: INT; (* the real user id *) |
| 473 | +proc euid*: INT; (* the effective one: 0 is root *) |
| 474 | +proc hostName*: ([]U8, INT); (* good until the next call *) |
| 475 | +``` |
| 476 | + |
| 477 | +`~name` is the sixth field of `name`'s line in `/etc/passwd`, which a |
| 478 | +shell reads with `Files.read`; `HOME` is `~`. The C calls: `getuid`, |
| 479 | +`geteuid`, `gethostname`. |
| 480 | + |
| 481 | +**More signals.** `trap` and `kill -USR1` name them: |
| 482 | + |
| 483 | +``` |
| 484 | +const sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; |
| 485 | +``` |
| 486 | + |
| 487 | +**Not needed.** Globs are rwalk's `Glob` over `openDir` and |
| 488 | +`entryKind`. `read -t` is `ready`. Here-documents are a `pipe`, or a |
| 489 | +`memFile` when long. `wait` already says `stopped`; a job continued |
| 490 | +by another program (`WCONTINUED`) would need a fifth `Status`, which |
| 491 | +breaks a `match` on `Status` that names all four, so it is left out: |
| 492 | +the shell knows when it continues a job itself. |