Merge editor-plan: Sys gains files, directories, the terminal and the environment The branch made the editors, which now live in rmacs and ravite, and left Sys what they needed of it (report 2026-10.2).
| @@ -51,8 +51,13 @@ | ||
| 51 | 51 | |
| 52 | 52 | Steps 1-18 are done: the language, the tools (`tools/Doc.raz`, and |
| 53 | 53 | the formatter, `tools/Pretty.raz`, over the shared `tools/Scan.raz`), |
| 54 | -`README.md` and the guide to the compiler, `doc/compiler.md`. Nothing | |
| 55 | -further is planned; new work starts only on the owner's word. Keep | |
| 54 | +`README.md` and the guide to the compiler, `doc/compiler.md`. Stage 5 | |
| 55 | +made two editors, which now live in repositories of their own, each | |
| 56 | +pinning the raz commit it builds with: rmacs (emacs's keys) and | |
| 57 | +ravite (vim's). What they needed of the system went into `Sys` (report | |
| 58 | +2026-10.2, approved): the terminal, files, directories and the | |
| 59 | +environment. `tools/Scan.raz` is theirs to use, as a tool, not part of | |
| 60 | +the library. New work starts only on the owner's word. Keep | |
| 56 | 61 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 57 | 62 | compiler changes. Left for later at the owner's word: a rewrite of |
| 58 | 63 | var parameters. |
| @@ -280,3 +280,132 @@ | ||
| 280 | 280 | and `CLAUDE.md` brought up to date; `make loc` within 6,000 and |
| 281 | 281 | 10%, `make bench` at 400,000 lines a second. *agent*, checked by |
| 282 | 282 | *main*. |
| 283 | + | |
| 284 | +Stage 5: ravite, an editor | |
| 285 | + | |
| 286 | +(The editors have since moved to repositories of their own, rmacs with | |
| 287 | +emacs's keys and ravite with vim's, each pinning the raz it builds | |
| 288 | +with; what follows is how they were made here.) | |
| 289 | + | |
| 290 | +ravite (raz visual text editor) is a terminal editor written in raz, | |
| 291 | +good enough to write raz in every day, with emacs's keys. It lives in | |
| 292 | +`ravite/`. Its first version had vim's keys (steps 28 to 33); the | |
| 293 | +owner chose emacs's instead, with more of emacs: buffers, the | |
| 294 | +minibuffer, ido, a small dired, M-x, and settings. Steps 28, 29 and 31 | |
| 295 | +stand as they are; the rest is redone from step 35. | |
| 296 | + | |
| 297 | +Ground rules, besides the plan's: | |
| 298 | + | |
| 299 | +- No line budget: code that is easy to read and follow, and well | |
| 300 | + commented, comes before its length (the owner's word, after step | |
| 301 | + 40). `make loc` counts it, as a measure. | |
| 302 | +- On Linux a program without C, a static executable; what it needs of | |
| 303 | + the system is `Sys`'s (steps 34 and 41), so that over the C `Sys` | |
| 304 | + it runs through the C library. | |
| 305 | +- Every session test, `test/ravite/X.keys`, does what emacs (`emacs -Q | |
| 306 | + -nw`) does with the same keys, checked when it is written, except | |
| 307 | + where ravite says it differs (`doc/ravite.md`). | |
| 308 | +- `make ravite` builds `build/ravite`; `make install` puts it on PATH; | |
| 309 | + `make check` runs its tests; `make fmt` covers `ravite/`. | |
| 310 | + | |
| 311 | +What it does, with emacs's keys and names: | |
| 312 | + | |
| 313 | +- **Editing.** C-f C-b C-n C-p and the arrows, C-a C-e, M-f M-b, M-< | |
| 314 | + M->, C-v M-v, C-l, M-g g, M-{ M-}; typing, RET with the line's | |
| 315 | + indent, TAB to the next multiple of 4, DEL C-d, M-DEL M-d, C-k, C-o, | |
| 316 | + C-t, M-u M-l M-c. The mark and the region, shown: C-SPC, C-x C-x, | |
| 317 | + C-x h, C-w, M-w; the kill ring: C-y, M-y, kills in a row add up. | |
| 318 | + C-/ C-_ C-x u undo; M-x undo-redo. C-u and M-digits for a count. | |
| 319 | + C-g gets out of anything. | |
| 320 | +- **Search.** C-s and C-r search incrementally, for text; M-% replaces, | |
| 321 | + asking y n ! q at each. | |
| 322 | +- **Buffers.** Any number. C-x C-f finds a file, C-x C-s saves, C-x | |
| 323 | + C-w writes to another name, C-x s saves those that need it, C-x b | |
| 324 | + switches, C-x k kills, C-x C-b lists them in a buffer of their own, | |
| 325 | + C-x C-c quits, asking about unsaved buffers. `*scratch*` is there | |
| 326 | + from the start, a buffer that is no file. | |
| 327 | +- **The minibuffer.** One line at the bottom, edited with the same keys | |
| 328 | + as any buffer. ido completes in it, for buffers, files and M-x: | |
| 329 | + the candidates that hold what is typed show after it, C-s and C-r | |
| 330 | + turn them round, TAB completes, RET takes the first, C-j takes what | |
| 331 | + is typed; for files, / goes into a directory and DEL out of one. | |
| 332 | +- **Dired.** C-x d, or finding a directory, lists it: RET or f opens | |
| 333 | + what is on the line, ^ goes up, n and p move, g reads it again, + | |
| 334 | + makes a directory, R renames, D deletes a file after a yes, q leaves. | |
| 335 | +- **M-x and settings.** Every command has a name and runs by M-x. Keys | |
| 336 | + are bound to names in keymaps, with C-x and M-g as prefixes. At | |
| 337 | + start ravite reads `~/.ravite`, lines of `bind KEYS COMMAND` and | |
| 338 | + `set VARIABLE VALUE`, keys written as emacs writes them (`C-x C-s`, | |
| 339 | + `M-%`). M-x global-set-key and set-variable do the same while it | |
| 340 | + runs; C-h k and C-h b say what keys do. The variables: indent, | |
| 341 | + tab-width, colour, ido. | |
| 342 | +- **The screen.** The buffer, a mode line (`-UU-:**- name L12 Top`) | |
| 343 | + and the echo area, which the minibuffer shares. Long lines scroll | |
| 344 | + sideways. A `.raz` buffer is coloured as before; `NO_COLOR` or `set | |
| 345 | + colour off` turns it off. | |
| 346 | + | |
| 347 | +What it leaves out: several windows, the mouse, the system clipboard, | |
| 348 | +undo in a tree (undo and redo are a line), patterns (search is for | |
| 349 | +text), major modes and their indenting, Lisp, the minibuffer's history, | |
| 350 | +and dired's marks. FreeBSD comes once `Sys` has what ravite needs. | |
| 351 | + | |
| 352 | +The modules: | |
| 353 | + | |
| 354 | +| Module | Lines | What | | |
| 355 | +|---|---|---| | |
| 356 | +| `Tty.linux-x86_64.raz` | 120 (155) | the terminal, directories and files, until `Sys` has them | | |
| 357 | +| `Keys.raz` | 220 (271) | a key from the bytes a terminal sends, Meta too; keys written as emacs writes them | | |
| 358 | +| `Text.raz` | 330 (337) | the text, a gap buffer, with undo and redo | | |
| 359 | +| `Colour.raz` | 110 (109) | the colour of each byte on the screen | | |
| 360 | +| `View.raz` | 170 (121) | the screen: the buffer, the mode line, the echo area | | |
| 361 | +| `Core.raz` | 300 (375) | buffers, the current one, the echo area, reading keys and running them, through procedures the main module sets | | |
| 362 | +| `Edit.raz` | 450 (558) | moving, typing, deleting, the mark, the kill ring, undo | | |
| 363 | +| `Mini.raz` | 350 (269) | the minibuffer, ido, yes or no | | |
| 364 | +| `Search.raz` | 200 (241) | incremental search, query-replace | | |
| 365 | +| `Dired.raz` | 250 (336) | directories, and the list of buffers | | |
| 366 | +| `Commands.raz` | 400 (799) | the commands' names, keymaps, M-x, settings, help, files and buffers | | |
| 367 | +| `Ravite.raz` | 250 (271) | the arguments, the screen, the keys from the terminal or a script, the loop | | |
| 368 | +| total | 3,150 (3,842) | | | |
| 369 | + | |
| 370 | +In brackets, what each came to by step 40: 3,842 lines, past the | |
| 371 | +3,300 then planned. The owner set the budget aside for readability; | |
| 372 | +step 41 took `Tty`'s 155 lines out to `Sys`. | |
| 373 | + | |
| 374 | +Testing. `ravite -script W H file...` reads keys as emacs writes them | |
| 375 | +from standard input, on a screen W by H, and at their end, or at C-x | |
| 376 | +C-c, prints the screen as plain text, then the current buffer. Each | |
| 377 | +`test/ravite/X.keys` has its `X.expect`, and may have an `X.txt` to | |
| 378 | +start from, or `X.args` for other arguments. Each module's `main` is | |
| 379 | +its test. | |
| 380 | + | |
| 381 | +Steps 28 to 33 (vim's keys): | |
| 382 | + | |
| 383 | +- [x] 28. `Tty` and `Keys`, with `Keys`'s test; raw mode, the size | |
| 384 | + and the keys tried through a pseudo-terminal. | |
| 385 | +- [x] 29. `Text`: the gap buffer and undo, with its test. | |
| 386 | +- [x] 30. `View` and `Ravite` with vim's keys; `-script` and | |
| 387 | + `test/ravite`; `make ravite`, `make loc` and `make fmt`. | |
| 388 | +- [x] 31. `Colour`. | |
| 389 | +- [x] 32. Visual mode, search, `%`, `f t`, and `file:line`. | |
| 390 | +- [x] 33. `doc/ravite.md`. | |
| 391 | + | |
| 392 | +Steps 34 on (emacs's keys): | |
| 393 | + | |
| 394 | +- [x] 34. The report: what `Sys` gains for ravite, for review before | |
| 395 | + any code (report version `2026-10.2`). | |
| 396 | +- [x] 35. `Keys` with Meta and emacs's notation; `Core`, `View` with a | |
| 397 | + mode line and the echo area, `Edit`, `Commands` with keymaps and | |
| 398 | + C-u, `Mini` without ido, and `Ravite`: editing with emacs's keys, | |
| 399 | + finding, saving and writing files, M-x by full name, quitting; the | |
| 400 | + session tests, checked against emacs. | |
| 401 | +- [x] 36. ido in the minibuffer, for M-x, files and buffers; buffers, | |
| 402 | + `*scratch*`, C-x b, C-x k, C-x C-b, C-x s, C-x C-c. | |
| 403 | +- [x] 37. `Search`: C-s, C-r, M-%. | |
| 404 | +- [x] 38. `Dired`. | |
| 405 | +- [x] 39. Settings: `~/.ravite`, M-x global-set-key and set-variable, | |
| 406 | + C-h k, C-h b. | |
| 407 | +- [x] 40. `doc/ravite.md` again, `README.md`, `CLAUDE.md`; `test/emacs.py`, | |
| 408 | + which checks a session against emacs. | |
| 409 | +- [x] 41. Once the owner approves step 34: `Sys` gains it, on Linux | |
| 410 | + and over C, with tests; ravite's `Tty` goes. `make check` runs | |
| 411 | + ravite's sessions over both. |
| @@ -135,6 +135,10 @@ | ||
| 135 | 135 | $ ./Pretty Module.raz # prints it, formatted |
| 136 | 136 | $ ./Pretty -w *.raz # rewrites those that aren't, and names them |
| 137 | 137 | |
| 138 | +Two editors are written in raz, each in a repository of its own that | |
| 139 | +pins the raz it is built with: rmacs, with emacs's keys, and ravite, | |
| 140 | +with vim's. Both colour raz through `tools/Scan.raz`. | |
| 141 | + | |
| 138 | 142 | ## Versions |
| 139 | 143 | |
| 140 | 144 | The compiler says its own version and that of the report it follows: |
| @@ -873,7 +873,7 @@ | ||
| 873 | 873 | | Module | What | |
| 874 | 874 | |---|---| |
| 875 | 875 | | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail; string comparison | |
| 876 | -| `Sys` | system calls: `openRead`, `create`, `read`, `write`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; one file per target, and `Sys.raz` over the C library for the others | | |
| 876 | +| `Sys` | system calls: `openRead`, `create`, `read`, `write`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `makeDir`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; and `env` and `workDir`. One file per target, and `Sys.raz` over the C library for the others | | |
| 877 | 877 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 878 | 878 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 879 | 879 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| @@ -898,6 +898,44 @@ | ||
| 898 | 898 | end Mem. |
| 899 | 899 | ``` |
| 900 | 900 | |
| 901 | +**Sys** gives every target the same procedures, so that a program | |
| 902 | +that needs only these runs wherever raz does: through system calls on | |
| 903 | +Linux, through the C library elsewhere. A result that can fail is a | |
| 904 | +number, minus an errno, as on Linux; 0 is success. | |
| 905 | + | |
| 906 | +``` | |
| 907 | +type | |
| 908 | + Info* = record | |
| 909 | + size*: I64; (* in bytes *) | |
| 910 | + dir*: BOOL; (* a directory *) | |
| 911 | + end; | |
| 912 | + Dir* = record ... end; (* an open directory; its fields are the target's *) | |
| 913 | + | |
| 914 | +proc rename*(old, new: []U8): I64; (* replaces what new names *) | |
| 915 | +proc remove*(path: []U8): I64; (* a file, not a directory *) | |
| 916 | +proc makeDir*(path: []U8): I64; (* readable by all, writable by its owner *) | |
| 917 | +proc info*(path: []U8): (Info, I64); (* following a symbolic link *) | |
| 918 | + | |
| 919 | +proc openDir*(d: ^Dir; path: []U8): I64; | |
| 920 | +proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) | |
| 921 | +proc closeDir*(d: ^Dir); | |
| 922 | + | |
| 923 | +proc rawMode*(fd: I64): BOOL; (* the terminal on fd: no echo, no lines, no signals *) | |
| 924 | +proc restoreMode*(fd: I64); (* as rawMode found it *) | |
| 925 | +proc termSize*(fd: I64): (I64, I64); (* columns and rows; 80 and 24 if unknown *) | |
| 926 | +proc ready*(fd: I64; ms: I64): BOOL; (* something to read within ms, or ever if ms < 0 *) | |
| 927 | + | |
| 928 | +proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) | |
| 929 | +proc workDir*(var buf: [4096]U8): I64; (* the working directory into buf: its length *) | |
| 930 | +``` | |
| 931 | + | |
| 932 | +`nextEntry` gives every name in the directory except `.` and `..`, in | |
| 933 | +no particular order. `rawMode` turns off what the terminal does with | |
| 934 | +keys before the program reads them: echo, waiting for a whole line, | |
| 935 | +Ctrl-C and Ctrl-Z as signals, Ctrl-S and Ctrl-Q as flow control, and | |
| 936 | +CR to NL; and with output, NL to CR NL. A program that turns it on | |
| 937 | +restores it before it ends: `Rt.atExit` is the place. | |
| 938 | + | |
| 901 | 939 | **Out and C's stdio.** `Out` keeps a buffer of 4 KB. It writes it out |
| 902 | 940 | when it is full, at each line feed when standard output is a terminal, |
| 903 | 941 | at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio` |
| @@ -932,3 +970,5 @@ | ||
| 932 | 970 | - **2026-10.1.** Messages: every error in a run, warnings of what is |
| 933 | 971 | never used and `-Werror` (section 10); failed checks say what failed, |
| 934 | 972 | and signals say where (sections 6.5 and 10). |
| 973 | +- **2026-10.2.** `Sys` gains files and directories, the terminal, and | |
| 974 | + the environment (section 11). |
| @@ -17,6 +17,20 @@ | ||
| 17 | 17 | extern proc sched_yield*: I32; |
| 18 | 18 | extern proc pthread_create*(t: ^U64; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; |
| 19 | 19 | extern proc pthread_join*(t: U64; result: ^^U8): I32; |
| 20 | +extern proc rename*(old, new: ^U8): I32; | |
| 21 | +extern proc unlink*(path: ^U8): I32; | |
| 22 | +extern proc mkdir*(path: ^U8; mode: U32): I32; | |
| 23 | +extern proc lseek*(fd: I32; off: I64; whence: I32): I64; | |
| 24 | +extern proc opendir*(path: ^U8): ^U8; (* DIR * *) | |
| 25 | +extern proc readdir*(d: ^U8): ^U8; (* struct dirent * *) | |
| 26 | +extern proc closedir*(d: ^U8): I32; | |
| 27 | +extern proc tcgetattr*(fd: I32; t: ^U8): I32; | |
| 28 | +extern proc tcsetattr*(fd: I32; when: I32; t: ^U8): I32; | |
| 29 | +extern proc cfmakeraw*(t: ^U8); | |
| 30 | +extern proc ioctl*(fd: I32; request: U64; ...): I32; | |
| 31 | +extern proc poll*(fds: ^U8; n: U64; ms: I32): I32; | |
| 32 | +extern proc getenv*(name: ^U8): ^U8; | |
| 33 | +extern proc getcwd*(buf: ^U8; n: U64): ^U8; | |
| 20 | 34 | extern proc printf*(format: ^U8; ...): I32; |
| 21 | 35 | extern proc fflush*(f: ^U8): I32; |
| 22 | 36 | extern var stdout*: ^U8; (* FILE * *) |
| @@ -7,6 +7,31 @@ | ||
| 7 | 7 | stdout* = 1; |
| 8 | 8 | stderr* = 2; |
| 9 | 9 | |
| 10 | +type | |
| 11 | + (* What info says of a file. *) | |
| 12 | + Info* = record | |
| 13 | + size*: I64; (* in bytes *) | |
| 14 | + dir*: BOOL; (* a directory *) | |
| 15 | + end; | |
| 16 | + | |
| 17 | + (* An open directory: getdents64's records, read a block at a time. *) | |
| 18 | + Dir* = record | |
| 19 | + fd, pos, len: I64; | |
| 20 | + buf: [8192]U8; | |
| 21 | + end; | |
| 22 | + | |
| 23 | + (* struct termios as the kernel's TCGETS fills it: 36 bytes *) | |
| 24 | + Termios = record | |
| 25 | + iflag, oflag, cflag, lflag: U32; | |
| 26 | + line: U8; | |
| 27 | + cc: [19]U8; | |
| 28 | + end; | |
| 29 | + Winsize = record rows, cols, xpixel, ypixel: U16; end; | |
| 30 | + Pollfd = record fd: I32; events, revents: I16; end; | |
| 31 | + | |
| 32 | +var | |
| 33 | + saved: Termios; (* the terminal as rawMode found it *) | |
| 34 | + | |
| 10 | 35 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| 11 | 36 | minus an errno. *) |
| 12 | 37 | proc write*(fd: I64; s: []U8): I64; |
| @@ -79,6 +104,157 @@ | ||
| 79 | 104 | return SYSCALL(16, fd, 5401H, ADR(t)) = 0; |
| 80 | 105 | end; |
| 81 | 106 | |
| 107 | +(* Renames old to new, replacing what new names: 0, or minus an errno. *) | |
| 108 | +proc rename*(old, new: []U8): I64; | |
| 109 | +var a, b: [4096]U8; | |
| 110 | +begin | |
| 111 | + if not cstr(old, a) or not cstr(new, b) then return -36; end; | |
| 112 | + return SYSCALL(82, ADR(a), ADR(b)); | |
| 113 | +end; | |
| 114 | + | |
| 115 | +(* Removes the file at path; not a directory. *) | |
| 116 | +proc remove*(path: []U8): I64; | |
| 117 | +var a: [4096]U8; | |
| 118 | +begin | |
| 119 | + if not cstr(path, a) then return -36; end; | |
| 120 | + return SYSCALL(87, ADR(a)); (* unlink *) | |
| 121 | +end; | |
| 122 | + | |
| 123 | +(* Makes a directory at path, readable by all, writable by its owner. *) | |
| 124 | +proc makeDir*(path: []U8): I64; | |
| 125 | +var a: [4096]U8; | |
| 126 | +begin | |
| 127 | + if not cstr(path, a) then return -36; end; | |
| 128 | + return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *) | |
| 129 | +end; | |
| 130 | + | |
| 131 | +(* The size of what path names, and whether it is a directory, | |
| 132 | + following a symbolic link; and 0, or minus an errno. *) | |
| 133 | +proc info*(path: []U8): (Info, I64); | |
| 134 | +var a: [4096]U8; st: [18]I64; i: Info; r: I64; (* struct stat: 144 bytes *) | |
| 135 | +begin | |
| 136 | + if not cstr(path, a) then return i, -36; end; | |
| 137 | + r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) | |
| 138 | + if r # 0 then return i, r; end; | |
| 139 | + i.size := st[6]; (* st_size *) | |
| 140 | + i.dir := st[3] and 0F000H = 4000H; (* st_mode is S_IFDIR *) | |
| 141 | + return i, 0; | |
| 142 | +end; | |
| 143 | + | |
| 144 | +(* Opens the directory at path, for nextEntry. *) | |
| 145 | +proc openDir*(d: ^Dir; path: []U8): I64; | |
| 146 | +var a: [4096]U8; | |
| 147 | +begin | |
| 148 | + d^.pos := 0; | |
| 149 | + d^.len := 0; | |
| 150 | + if not cstr(path, a) then return -36; end; | |
| 151 | + d^.fd := SYSCALL(2, ADR(a), 10000H); (* open, O_RDONLY, O_DIRECTORY *) | |
| 152 | + if d^.fd < 0 then return d^.fd; end; | |
| 153 | + return 0; | |
| 154 | +end; | |
| 155 | + | |
| 156 | +(* The next name in the directory, good until the next call, but . and | |
| 157 | + .., in no order; false at the end. *) | |
| 158 | +proc nextEntry*(d: ^Dir): ([]U8, BOOL); | |
| 159 | +var n, at: I64; | |
| 160 | +begin | |
| 161 | + while true do | |
| 162 | + if d^.pos >= d^.len then | |
| 163 | + d^.len := SYSCALL(217, d^.fd, ADR(d^.buf), LEN(d^.buf)); (* getdents64 *) | |
| 164 | + d^.pos := 0; | |
| 165 | + if d^.len <= 0 then return "", false; end; | |
| 166 | + end; | |
| 167 | + (* a record: d_ino and d_off, 8 bytes each; d_reclen, 2; d_type, | |
| 168 | + 1; then d_name, ending in a zero *) | |
| 169 | + at := d^.pos + 19; | |
| 170 | + n := 0; | |
| 171 | + while d^.buf[at + n] # 0 do INC(n); end; | |
| 172 | + INC(d^.pos, I64(d^.buf[d^.pos + 16]) + I64(d^.buf[d^.pos + 17]) * 256); | |
| 173 | + if (d^.buf[at..at + n] # ".") and (d^.buf[at..at + n] # "..") then | |
| 174 | + return d^.buf[at..at + n], true; | |
| 175 | + end; | |
| 176 | + end; | |
| 177 | + return "", false; | |
| 178 | +end; | |
| 179 | + | |
| 180 | +proc closeDir*(d: ^Dir); | |
| 181 | +begin | |
| 182 | + SYSCALL(3, d^.fd); | |
| 183 | +end; | |
| 184 | + | |
| 185 | +(* Puts the terminal on fd into raw mode: no echo, no lines, no signals, | |
| 186 | + no flow control, no CR to NL, no NL to CR NL; false if fd isn't a | |
| 187 | + terminal. *) | |
| 188 | +proc rawMode*(fd: I64): BOOL; | |
| 189 | +var t: Termios; | |
| 190 | +begin | |
| 191 | + if SYSCALL(16, fd, 5401H, ADR(saved)) # 0 then return false; end; (* TCGETS *) | |
| 192 | + t := saved; | |
| 193 | + t.iflag := t.iflag and not U32(5EBH); (* IGNBRK BRKINT PARMRK ISTRIP INLCR IGNCR ICRNL IXON *) | |
| 194 | + t.oflag := t.oflag and not U32(1); (* OPOST *) | |
| 195 | + t.cflag := t.cflag and not U32(130H) or 30H; (* CS8, no PARENB *) | |
| 196 | + t.lflag := t.lflag and not U32(804BH); (* ISIG ICANON ECHO ECHONL IEXTEN *) | |
| 197 | + t.cc[5] := 0; (* VTIME: no time limit *) | |
| 198 | + t.cc[6] := 1; (* VMIN: a byte at least *) | |
| 199 | + return SYSCALL(16, fd, 5402H, ADR(t)) = 0; (* TCSETS *) | |
| 200 | +end; | |
| 201 | + | |
| 202 | +(* The terminal on fd as rawMode found it. *) | |
| 203 | +proc restoreMode*(fd: I64); | |
| 204 | +begin | |
| 205 | + SYSCALL(16, fd, 5402H, ADR(saved)); | |
| 206 | +end; | |
| 207 | + | |
| 208 | +(* The columns and rows of the terminal on fd; 80 by 24 if it doesn't | |
| 209 | + say. *) | |
| 210 | +proc termSize*(fd: I64): (I64, I64); | |
| 211 | +var w: Winsize; | |
| 212 | +begin | |
| 213 | + if (SYSCALL(16, fd, 5413H, ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then (* TIOCGWINSZ *) | |
| 214 | + return 80, 24; | |
| 215 | + end; | |
| 216 | + return I64(w.cols), I64(w.rows); | |
| 217 | +end; | |
| 218 | + | |
| 219 | +(* Whether fd has something to read within ms milliseconds; ms < 0 | |
| 220 | + waits as long as it takes. *) | |
| 221 | +proc ready*(fd, ms: I64): BOOL; | |
| 222 | +var p: Pollfd; | |
| 223 | +begin | |
| 224 | + p := {fd: I32(fd), events: 1}; (* POLLIN *) | |
| 225 | + return SYSCALL(7, ADR(p), 1, ms) > 0; (* poll *) | |
| 226 | +end; | |
| 227 | + | |
| 228 | +(* The value of the environment variable name; "" if it isn't set. The | |
| 229 | + environment follows the arguments and their nil. *) | |
| 230 | +proc env*(name: []U8): []U8; | |
| 231 | +var e: ^^U8; s: ^U8; i, n, m: I64; | |
| 232 | +begin | |
| 233 | + e := ARGV() + (ARGC() + 1); | |
| 234 | + i := 0; | |
| 235 | + while e[i] # nil do | |
| 236 | + s := e[i]; | |
| 237 | + n := 0; | |
| 238 | + while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; | |
| 239 | + if (n = LEN(name)) and (s[n] = '=') then | |
| 240 | + m := n + 1; | |
| 241 | + while s[m] # 0 do INC(m); end; | |
| 242 | + return s[n + 1..m]; | |
| 243 | + end; | |
| 244 | + INC(i); | |
| 245 | + end; | |
| 246 | + return ""; | |
| 247 | +end; | |
| 248 | + | |
| 249 | +(* The working directory into buf: its length, or minus an errno. *) | |
| 250 | +proc workDir*(var buf: [4096]U8): I64; | |
| 251 | +var r: I64; | |
| 252 | +begin | |
| 253 | + r := SYSCALL(79, ADR(buf), LEN(buf)); (* getcwd: its length with its zero *) | |
| 254 | + if r < 0 then return r; end; | |
| 255 | + return r - 1; | |
| 256 | +end; | |
| 257 | + | |
| 82 | 258 | (* Gives the processor to another thread. *) |
| 83 | 259 | proc yield*; |
| 84 | 260 | begin |
| @@ -91,9 +267,11 @@ | ||
| 91 | 267 | SYSCALL(231, status); |
| 92 | 268 | end; |
| 93 | 269 | |
| 94 | -(* The module's test: a file written, read back and closed. *) | |
| 270 | +(* The module's test: a file written, read back and closed; a | |
| 271 | + directory made, listed and emptied; no terminal. *) | |
| 95 | 272 | proc main*; |
| 96 | -var fd, n: I64; buf: [16]U8; p: ^U8; | |
| 273 | +var fd, n, err, w, h: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; | |
| 274 | + dir: [4096]U8; | |
| 97 | 275 | begin |
| 98 | 276 | fd := create("/tmp/raz-sys-test"); |
| 99 | 277 | if fd < 0 then exit(2); end; |
| @@ -109,6 +287,36 @@ | ||
| 109 | 287 | p := mmap(100); |
| 110 | 288 | if (p = nil) or (p[99] # 0) then exit(2); end; |
| 111 | 289 | munmap(p, 100); |
| 290 | + (* files and directories *) | |
| 291 | + remove("/tmp/raz-sys-dir/f"); | |
| 292 | + remove("/tmp/raz-sys-dir/g"); | |
| 293 | + makeDir("/tmp/raz-sys-dir"); | |
| 294 | + fd := create("/tmp/raz-sys-dir/f"); | |
| 295 | + if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end; | |
| 296 | + close(fd); | |
| 297 | + if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; | |
| 298 | + i, err := info("/tmp/raz-sys-dir/g"); | |
| 299 | + if (err # 0) or (i.size # 3) or i.dir then exit(2); end; | |
| 300 | + i, err := info("/tmp/raz-sys-dir"); | |
| 301 | + if (err # 0) or not i.dir then exit(2); end; | |
| 302 | + i, err := info("/no/such/file"); | |
| 303 | + if err >= 0 then exit(2); end; | |
| 304 | + if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; | |
| 305 | + name, ok := nextEntry(ADR(d)); | |
| 306 | + if not ok or (name # "g") then exit(2); end; | |
| 307 | + name, ok := nextEntry(ADR(d)); | |
| 308 | + if ok then exit(2); end; | |
| 309 | + closeDir(ADR(d)); | |
| 310 | + if (remove("/tmp/raz-sys-dir/g") # 0) or (remove("/tmp/raz-sys-dir/g") >= 0) then exit(2); end; | |
| 311 | + if openDir(ADR(d), "/no/such/dir") >= 0 then exit(2); end; | |
| 312 | + (* the terminal: none here, fd -1 *) | |
| 313 | + if rawMode(-1) or ready(-1, 0) then exit(2); end; | |
| 314 | + w, h := termSize(-1); | |
| 315 | + if (w # 80) or (h # 24) then exit(2); end; | |
| 316 | + (* the environment *) | |
| 317 | + if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; | |
| 318 | + n := workDir(dir); | |
| 319 | + if (n <= 0) or (dir[0] # '/') then exit(2); end; | |
| 112 | 320 | end; |
| 113 | 321 | |
| 114 | 322 | end Sys. |
| @@ -1,6 +1,12 @@ | ||
| 1 | 1 | (* System calls through the C library: Sys for the targets that have no |
| 2 | 2 | file of their own, with the same exports. A program that imports it |
| 3 | - uses C. *) | |
| 3 | + uses C. | |
| 4 | + | |
| 5 | + It keeps to what POSIX gives: raw mode is cfmakeraw's, and a file's | |
| 6 | + size is where lseek finds its end, so that no struct stat is read. | |
| 7 | + Two things are glibc's: where a name lies in struct dirent, and the | |
| 8 | + number of the TIOCGWINSZ request. A C library that differs needs a | |
| 9 | + Sys of its own. *) | |
| 4 | 10 | module Sys; |
| 5 | 11 | import C; |
| 6 | 12 | |
| @@ -8,7 +14,27 @@ | ||
| 8 | 14 | stdin* = 0; |
| 9 | 15 | stdout* = 1; |
| 10 | 16 | stderr* = 2; |
| 17 | + DirentName = 19; (* the offset of d_name in glibc's struct dirent *) | |
| 18 | + TIOCGWINSZ = 5413H; (* glibc's *) | |
| 11 | 19 | |
| 20 | +type | |
| 21 | + (* What info says of a file. *) | |
| 22 | + Info* = record | |
| 23 | + size*: I64; (* in bytes *) | |
| 24 | + dir*: BOOL; (* a directory *) | |
| 25 | + end; | |
| 26 | + | |
| 27 | + (* An open directory: C's DIR. *) | |
| 28 | + Dir* = record | |
| 29 | + d: ^U8; | |
| 30 | + end; | |
| 31 | + | |
| 32 | + Winsize = record rows, cols, xpixel, ypixel: U16; end; | |
| 33 | + Pollfd = record fd: I32; events, revents: I16; end; | |
| 34 | + | |
| 35 | +var | |
| 36 | + saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *) | |
| 37 | + | |
| 12 | 38 | (* minus errno, as a system call returns it *) |
| 13 | 39 | proc failed: I64; |
| 14 | 40 | var e: ^I32; |
| @@ -89,8 +115,147 @@ | ||
| 89 | 115 | proc munmap*(p: ^U8; size: I64); |
| 90 | 116 | begin |
| 91 | 117 | C.munmap(p, U64(size)); |
| 118 | +end; | |
| 119 | + | |
| 120 | +(* Renames old to new, replacing what new names: 0, or minus an errno. *) | |
| 121 | +proc rename*(old, new: []U8): I64; | |
| 122 | +var a, b: [4096]U8; | |
| 123 | +begin | |
| 124 | + if not cstr(old, a) or not cstr(new, b) then return -36; end; | |
| 125 | + if C.rename(ADR(a[0]), ADR(b[0])) # 0 then return failed(); end; | |
| 126 | + return 0; | |
| 127 | +end; | |
| 128 | + | |
| 129 | +(* Removes the file at path; not a directory. *) | |
| 130 | +proc remove*(path: []U8): I64; | |
| 131 | +var a: [4096]U8; | |
| 132 | +begin | |
| 133 | + if not cstr(path, a) then return -36; end; | |
| 134 | + if C.unlink(ADR(a[0])) # 0 then return failed(); end; | |
| 135 | + return 0; | |
| 136 | +end; | |
| 137 | + | |
| 138 | +(* Makes a directory at path, readable by all, writable by its owner. *) | |
| 139 | +proc makeDir*(path: []U8): I64; | |
| 140 | +var a: [4096]U8; | |
| 141 | +begin | |
| 142 | + if not cstr(path, a) then return -36; end; | |
| 143 | + if C.mkdir(ADR(a[0]), 1EDH) # 0 then return failed(); end; (* mode 755 *) | |
| 144 | + return 0; | |
| 145 | +end; | |
| 146 | + | |
| 147 | +(* The size of what path names, and whether it is a directory, | |
| 148 | + following a symbolic link; and 0, or minus an errno. A directory is | |
| 149 | + what opendir opens; a file's size is where lseek finds its end, so a | |
| 150 | + file that can't be read has none. *) | |
| 151 | +proc info*(path: []U8): (Info, I64); | |
| 152 | +var a: [4096]U8; i: Info; d: ^U8; fd: I32; | |
| 153 | +begin | |
| 154 | + if not cstr(path, a) then return i, -36; end; | |
| 155 | + d := C.opendir(ADR(a[0])); | |
| 156 | + if d # nil then | |
| 157 | + C.closedir(d); | |
| 158 | + i.dir := true; | |
| 159 | + return i, 0; | |
| 160 | + end; | |
| 161 | + fd := C.open(ADR(a[0]), 0); (* O_RDONLY *) | |
| 162 | + if fd < 0 then return i, failed(); end; | |
| 163 | + i.size := C.lseek(fd, 0, 2); (* SEEK_END *) | |
| 164 | + C.close(fd); | |
| 165 | + return i, 0; | |
| 166 | +end; | |
| 167 | + | |
| 168 | +(* Opens the directory at path, for nextEntry. *) | |
| 169 | +proc openDir*(d: ^Dir; path: []U8): I64; | |
| 170 | +var a: [4096]U8; | |
| 171 | +begin | |
| 172 | + if not cstr(path, a) then return -36; end; | |
| 173 | + d^.d := C.opendir(ADR(a[0])); | |
| 174 | + if d^.d = nil then return failed(); end; | |
| 175 | + return 0; | |
| 176 | +end; | |
| 177 | + | |
| 178 | +(* The next name in the directory, good until the next call, but . and | |
| 179 | + .., in no order; false at the end. *) | |
| 180 | +proc nextEntry*(d: ^Dir): ([]U8, BOOL); | |
| 181 | +var e: ^U8; n: I64; | |
| 182 | +begin | |
| 183 | + while true do | |
| 184 | + e := C.readdir(d^.d); | |
| 185 | + if e = nil then return "", false; end; | |
| 186 | + e := e + DirentName; | |
| 187 | + n := 0; | |
| 188 | + while e[n] # 0 do INC(n); end; | |
| 189 | + if (e[0..n] # ".") and (e[0..n] # "..") then return e[0..n], true; end; | |
| 190 | + end; | |
| 191 | + return "", false; | |
| 92 | 192 | end; |
| 93 | 193 | |
| 194 | +proc closeDir*(d: ^Dir); | |
| 195 | +begin | |
| 196 | + C.closedir(d^.d); | |
| 197 | +end; | |
| 198 | + | |
| 199 | +(* Puts the terminal on fd into raw mode, as cfmakeraw makes it: no | |
| 200 | + echo, no lines, no signals, no flow control, no CR to NL, no NL to | |
| 201 | + CR NL; false if fd isn't a terminal. *) | |
| 202 | +proc rawMode*(fd: I64): BOOL; | |
| 203 | +var t: [256]U8; | |
| 204 | +begin | |
| 205 | + if C.tcgetattr(I32(fd), ADR(saved[0])) # 0 then return false; end; | |
| 206 | + t := saved; | |
| 207 | + C.cfmakeraw(ADR(t[0])); | |
| 208 | + return C.tcsetattr(I32(fd), 0, ADR(t[0])) = 0; (* TCSANOW *) | |
| 209 | +end; | |
| 210 | + | |
| 211 | +(* The terminal on fd as rawMode found it. *) | |
| 212 | +proc restoreMode*(fd: I64); | |
| 213 | +begin | |
| 214 | + C.tcsetattr(I32(fd), 0, ADR(saved[0])); | |
| 215 | +end; | |
| 216 | + | |
| 217 | +(* The columns and rows of the terminal on fd; 80 by 24 if it doesn't | |
| 218 | + say. *) | |
| 219 | +proc termSize*(fd: I64): (I64, I64); | |
| 220 | +var w: Winsize; | |
| 221 | +begin | |
| 222 | + if (C.ioctl(I32(fd), TIOCGWINSZ, ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then | |
| 223 | + return 80, 24; | |
| 224 | + end; | |
| 225 | + return I64(w.cols), I64(w.rows); | |
| 226 | +end; | |
| 227 | + | |
| 228 | +(* Whether fd has something to read within ms milliseconds; ms < 0 | |
| 229 | + waits as long as it takes. *) | |
| 230 | +proc ready*(fd, ms: I64): BOOL; | |
| 231 | +var p: Pollfd; | |
| 232 | +begin | |
| 233 | + p := {fd: I32(fd), events: 1}; (* POLLIN *) | |
| 234 | + return C.poll(^U8(ADR(p)), 1, I32(ms)) > 0; | |
| 235 | +end; | |
| 236 | + | |
| 237 | +(* The value of the environment variable name; "" if it isn't set. *) | |
| 238 | +proc env*(name: []U8): []U8; | |
| 239 | +var a: [4096]U8; s: ^U8; n: I64; | |
| 240 | +begin | |
| 241 | + if not cstr(name, a) then return ""; end; | |
| 242 | + s := C.getenv(ADR(a[0])); | |
| 243 | + if s = nil then return ""; end; | |
| 244 | + n := 0; | |
| 245 | + while s[n] # 0 do INC(n); end; | |
| 246 | + return s[0..n]; | |
| 247 | +end; | |
| 248 | + | |
| 249 | +(* The working directory into buf: its length, or minus an errno. *) | |
| 250 | +proc workDir*(var buf: [4096]U8): I64; | |
| 251 | +var n: I64; | |
| 252 | +begin | |
| 253 | + if C.getcwd(ADR(buf[0]), LEN(buf)) = nil then return failed(); end; | |
| 254 | + n := 0; | |
| 255 | + while buf[n] # 0 do INC(n); end; | |
| 256 | + return n; | |
| 257 | +end; | |
| 258 | + | |
| 94 | 259 | proc isTerminal*(fd: I64): BOOL; |
| 95 | 260 | begin |
| 96 | 261 | return C.isatty(I32(fd)) = 1; |
| @@ -107,9 +272,11 @@ | ||
| 107 | 272 | C._exit(I32(status)); |
| 108 | 273 | end; |
| 109 | 274 | |
| 110 | -(* The module's test: a file written, read back and closed. *) | |
| 275 | +(* The module's test, as Linux's Sys has it: a file written, read back | |
| 276 | + and closed; a directory made, listed and emptied; no terminal. *) | |
| 111 | 277 | proc main*; |
| 112 | -var fd, n: I64; buf: [16]U8; p: ^U8; | |
| 278 | +var fd, n, err, w, h: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; | |
| 279 | + dir: [4096]U8; | |
| 113 | 280 | begin |
| 114 | 281 | fd := create("/tmp/raz-sys-test"); |
| 115 | 282 | if fd < 0 then exit(2); end; |
| @@ -125,6 +292,36 @@ | ||
| 125 | 292 | p := mmap(100); |
| 126 | 293 | if (p = nil) or (p[99] # 0) then exit(2); end; |
| 127 | 294 | munmap(p, 100); |
| 295 | + (* files and directories *) | |
| 296 | + remove("/tmp/raz-sys-dir/f"); | |
| 297 | + remove("/tmp/raz-sys-dir/g"); | |
| 298 | + makeDir("/tmp/raz-sys-dir"); | |
| 299 | + fd := create("/tmp/raz-sys-dir/f"); | |
| 300 | + if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end; | |
| 301 | + close(fd); | |
| 302 | + if rename("/tmp/raz-sys-dir/f", "/tmp/raz-sys-dir/g") # 0 then exit(2); end; | |
| 303 | + i, err := info("/tmp/raz-sys-dir/g"); | |
| 304 | + if (err # 0) or (i.size # 3) or i.dir then exit(2); end; | |
| 305 | + i, err := info("/tmp/raz-sys-dir"); | |
| 306 | + if (err # 0) or not i.dir then exit(2); end; | |
| 307 | + i, err := info("/no/such/file"); | |
| 308 | + if err >= 0 then exit(2); end; | |
| 309 | + if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; | |
| 310 | + name, ok := nextEntry(ADR(d)); | |
| 311 | + if not ok or (name # "g") then exit(2); end; | |
| 312 | + name, ok := nextEntry(ADR(d)); | |
| 313 | + if ok then exit(2); end; | |
| 314 | + closeDir(ADR(d)); | |
| 315 | + if (remove("/tmp/raz-sys-dir/g") # 0) or (remove("/tmp/raz-sys-dir/g") >= 0) then exit(2); end; | |
| 316 | + if openDir(ADR(d), "/no/such/dir") >= 0 then exit(2); end; | |
| 317 | + (* the terminal: none here, fd -1 *) | |
| 318 | + if rawMode(-1) or ready(-1, 0) then exit(2); end; | |
| 319 | + w, h := termSize(-1); | |
| 320 | + if (w # 80) or (h # 24) then exit(2); end; | |
| 321 | + (* the environment *) | |
| 322 | + if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; | |
| 323 | + n := workDir(dir); | |
| 324 | + if (n <= 0) or (dir[0] # '/') then exit(2); end; | |
| 128 | 325 | end; |
| 129 | 326 | |
| 130 | 327 | end Sys. |
| @@ -1,11 +1,13 @@ | ||
| 1 | -"""Counts the lines of the compiler's units: all of them, and the lines | |
| 2 | -of code, those that are neither blank nor only comment.""" | |
| 1 | +"""Counts the lines of the compiler's units, or of raz modules: all of | |
| 2 | +them, and the lines of code, those that are neither blank nor only | |
| 3 | +comment. In raz, { and } are literals, not comments.""" | |
| 3 | 4 | import re, sys |
| 4 | 5 | |
| 5 | 6 | total = code = 0 |
| 6 | 7 | for path in sys.argv[1:]: |
| 7 | 8 | text = open(path).read() |
| 8 | - bare = re.sub(r"\{[^}]*\}|\(\*.*?\*\)", "", text, flags=re.S) | |
| 9 | + comment = r"\(\*.*?\*\)" if path.endswith(".raz") else r"\{[^}]*\}|\(\*.*?\*\)" | |
| 10 | + bare = re.sub(comment, "", text, flags=re.S) | |
| 9 | 11 | n = text.count("\n") |
| 10 | 12 | c = sum(1 for line in bare.split("\n") if line.strip()) |
| 11 | 13 | print(f"{path:18} {n:6} {c:6}") |