| | @@ -38,7 +38,7 @@ |
| 38 | 38 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 39 | 39 | <p>This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, <code>doc/raz-lib.md</code>, is the library, which sections 3 and 10 and appendix A name.</p> |
| 40 | 40 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 41 | | -<p>This is version 2026-10.27 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone.</p> |
| 41 | +<p>This is version 2026-10.29 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program.</p> |
| 42 | 42 | <p>The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 43 | 43 | <h1 id="a-first-look">1. A first look</h1> |
| 44 | 44 | <pre><code>module Hello; |
| | @@ -146,6 +146,9 @@ |
| 146 | 146 | ... |
| 147 | 147 | p := Primes[i];</code></pre> |
| 148 | 148 | <p>It is used as a variable of that type is, but can’t be changed: an array constant can be indexed and sliced and its <code>LEN</code> is <code>n</code>, a record constant’s fields can be read, and either can be assigned, compared as its type allows, and passed. Its parts are constant as a string’s bytes are (section 5.10). A part of it isn’t a constant, even at a constant index.</p> |
| 149 | +<p>An array constant may be typed <code>[]T</code>, with no length: its length is then its literal’s, as a string constant’s is the string’s, and it is an array constant of that length, <code>[n]T</code>, in all else. Its literal has at least one element.</p> |
| 150 | +<pre><code>const Wide: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; |
| 151 | + Count = LEN(Wide); (* 3, a constant *)</code></pre> |
| 149 | 152 | <p><strong>Variables</strong> of a module live as long as the program; variables of a procedure live as long as its call. Both start at zero: every byte of them is zero. So a number starts as <code>0</code> (<code>0.0</code> for <code>F32</code> and <code>F64</code>), a <code>BOOL</code> as <code>false</code>, a pointer, procedure or <code>ERROR</code> as <code>nil</code>, a slice as the empty slice (<code>ptr</code> is <code>nil</code>, <code>len</code> is 0), a variant as its first case with its fields zero, a set as the empty set, and an enumeration as its first name. No variable is ever read before it has a value.</p> |
| 150 | 153 | <h1 id="types">5. Types</h1> |
| 151 | 154 | <pre><code>Type = QualIdent |
| | @@ -645,6 +648,9 @@ |
| 645 | 648 | <p>A procedure without parameters may leave out the parentheses, both where it is declared (<code>proc main*;</code>) and where it is called (<code>Out.ln</code>). Used without parentheses in an expression, a procedure is a procedure value, not a call.</p> |
| 646 | 649 | <p><strong>Parameters.</strong> A value parameter is a copy: the procedure may change it, and the caller’s variable stays as it was. A <code>var</code> parameter is the caller’s variable itself; its argument must be a variable of exactly the parameter’s type.</p> |
| 647 | 650 | <p><strong>Results.</strong> A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple assignment, alone as a statement, or alone after <code>return</code> in a procedure whose results are of the same types, in the same order, which then returns them as they are: <code>return divmod(n, 2);</code>. A procedure that can fail has an <code>ERROR</code> as its last result (section 5.8), which <code>try</code> passes on (section 7.8).</p> |
| 651 | +<p>An array a call returns lives to the end of the statement the call is in. A slice of it may be used there, indexed, passed or compared; one kept past the statement, in a variable or anywhere else, points at what is gone, and is the exception of section 10:</p> |
| 652 | +<pre><code>s := f('a')[0..4]; (* f returns a [8]U8: s is undefined after this *) |
| 653 | +Out.str(f('a')[0..4]); (* used in its statement: defined *)</code></pre> |
| 648 | 654 | <pre><code>module Divide; |
| 649 | 655 | import Out; |
| 650 | 656 | |
| | @@ -700,7 +706,7 @@ |
| 700 | 706 | proc exit*(status: INT); |
| 701 | 707 | proc trace*; (* the calls under way, on standard error *)</code></pre> |
| 702 | 708 | <p><code>Rt.trace</code> says on standard error the calls under way, from the one that calls it out, each with its variables, as a program that stops does.</p> |
| 703 | | -<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, a slice kept after the call whose frame holds what it points at (section 7.3), an index out of range in a module that says <code>check off;</code>, writing a string’s bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p> |
| 709 | +<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, a slice kept after the call whose frame holds what it points at (section 7.3), a slice of an array a call returned kept past its statement (section 8), an index out of range in a module that says <code>check off;</code>, writing a string’s bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p> |
| 704 | 710 | <p><strong>Separate compilation.</strong> An importer depends only on a module’s interface: what it exports, as an importer sees it, never its source or its code. An implementation that compiles modules on its own makes the same program as if every module were compiled together (appendix A).</p> |
| 705 | 711 | <p><strong>Errors.</strong> A program that breaks a rule of this report that says it is an error isn’t a raz program, and an implementation must reject it: say what is wrong and make no executable. Such a rule is one whose breach would leave the program with no meaning this report can give: a mistake of types, a name that isn’t declared or is declared twice, an assignment to what is read-only, a call with the wrong number of arguments, a type name or a procedure with no result used as a value, type arguments given to a module that isn’t generic, or too many, an import that can’t be found, a procedure declared <code>forward</code> and never defined, a module that imports itself. A fault that is only a lint isn’t an error: a name declared and never used, or a result that a call standing on its own drops (<code>Sys.write(fd, s);</code>, where <code>_ := Sys.write(fd, s);</code> says it is meant). A program with one is valid, and an implementation may warn of it. The text of its messages, how many it reports and which warnings it gives are its own (appendix A).</p> |
| 706 | 712 | <h1 id="versions">11. Versions</h1> |
| | @@ -836,7 +842,7 @@ |
| 836 | 842 | </tr> |
| 837 | 843 | <tr> |
| 838 | 844 | <td><code>Sys</code></td> |
| 839 | | -<td>system calls: <code>open</code>, <code>openMode</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>syncAll</code>, <code>lock</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>linkInfo</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td> |
| 845 | +<td>system calls: <code>open</code>, <code>openMode</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>syncAll</code>, <code>lock</code>, <code>close</code>, <code>dup</code>, <code>dupTo</code>, <code>dupAbove</code>, <code>keepOnExec</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>access</code>, <code>umask</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>linkInfo</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>entryKind</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>getMode</code>, <code>setMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>envAt</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, <code>spawn</code> and <code>exec</code>, pipes and signals; the user, the machine, time used and limits; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td> |
| 840 | 846 | </tr> |
| 841 | 847 | <tr> |
| 842 | 848 | <td><code>Out</code></td> |
| | @@ -918,7 +924,7 @@ |
| 918 | 924 | const Full*: ERROR = "too many procedures to run at exit"; |
| 919 | 925 | proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) |
| 920 | 926 | proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) |
| 921 | | -proc stop*(msg: []U8); (* msg on standard error, then exit with status 2 *) |
| 927 | +proc stop*(msg: []U8); (* msg, and a line feed, on standard error, then exit with status 2 *) |
| 922 | 928 | proc trace*; (* the calls under way, and their variables, on standard error *)</code></pre> |
| 923 | 929 | <p><code>Rt</code> also holds what the compiler’s checks call when they fail, and the handlers of the signals that stop a program (section 10). <code>exit</code> takes each procedure off the list before it runs it, so it runs once even if it fails or calls <code>exit</code> itself; in a program that uses C, it ends through C’s <code>exit</code>, so that C flushes its buffers too.</p> |
| 924 | 930 | <pre><code>module Mem; |
| | @@ -990,9 +996,13 @@ |
| 990 | 996 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 991 | 997 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 992 | 998 | sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 999 | + sigUsr1* = 10; sigUsr2* = 12; sigAlrm* = 14; |
| 1000 | + (* the limits limit and setLimit take, by their numbers on Linux; each target has its own *) |
| 1001 | + limitCpu* = 0; limitFileSize* = 1; limitData* = 2; limitStack* = 3; |
| 1002 | + limitCore* = 4; limitProcs* = 6; limitFiles* = 7; limitMemory* = 9; |
| 993 | 1003 | |
| 994 | 1004 | type |
| 995 | | - Access* = (reading, writing, creating, exclusive, truncating, appending); |
| 1005 | + Access* = (reading, writing, creating, exclusive, truncating, appending, executing); |
| 996 | 1006 | Accesses* = set of Access; |
| 997 | 1007 | Info* = record |
| 998 | 1008 | size*: I64; (* in bytes *) |
| | @@ -1002,7 +1012,9 @@ |
| 1002 | 1012 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 1003 | 1013 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 1004 | 1014 | ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) |
| 1015 | + dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 1005 | 1016 | end; |
| 1017 | + Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) |
| 1006 | 1018 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 1007 | 1019 | Program* = record |
| 1008 | 1020 | path*: []U8; |
| | @@ -1014,8 +1026,10 @@ |
| 1014 | 1026 | group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) |
| 1015 | 1027 | foreground*: BOOL; (* its group takes the terminal on standard input *) |
| 1016 | 1028 | tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) |
| 1029 | + more*: []INT; (* fds above 2 passed on to it, at the same numbers *) |
| 1017 | 1030 | end; |
| 1018 | 1031 | Status* = (running, exited, killed, stopped); |
| 1032 | + Mode* = record ... end; (* a terminal's modes; its fields are the target's *) |
| 1019 | 1033 | Peer* = record ... end; (* whom a datagram came from *) |
| 1020 | 1034 | |
| 1021 | 1035 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| | @@ -1031,6 +1045,10 @@ |
| 1031 | 1045 | proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *) |
| 1032 | 1046 | proc lock*(fd: INT; wait: BOOL): INT; (* the file's lock, held until fd is closed *) |
| 1033 | 1047 | proc close*(fd: INT); |
| 1048 | +proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) |
| 1049 | +proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) |
| 1050 | +proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *) |
| 1051 | +proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *) |
| 1034 | 1052 | proc isTerminal*(fd: INT): BOOL; |
| 1035 | 1053 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| 1036 | 1054 | proc munmap*(p: ^U8; size: INT); |
| | @@ -1042,6 +1060,8 @@ |
| 1042 | 1060 | proc removeDir*(path: []U8): INT; (* an empty directory *) |
| 1043 | 1061 | proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) |
| 1044 | 1062 | proc chmod*(path: []U8; mode: INT): INT; |
| 1063 | +proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) |
| 1064 | +proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) |
| 1045 | 1065 | proc symlink*(target, path: []U8): INT; (* path made a link to target *) |
| 1046 | 1066 | proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) |
| 1047 | 1067 | proc info*(path: []U8): (Info, INT); (* following a symbolic link *) |
| | @@ -1051,10 +1071,13 @@ |
| 1051 | 1071 | |
| 1052 | 1072 | proc openDir*(d: ^Dir; path: []U8): INT; |
| 1053 | 1073 | proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) |
| 1074 | +proc entryKind*(d: ^Dir): Kind; (* what the name nextEntry gave last is *) |
| 1054 | 1075 | proc closeDir*(d: ^Dir); |
| 1055 | 1076 | |
| 1056 | 1077 | proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) |
| 1057 | 1078 | proc restoreMode*(fd: INT); (* as rawMode found it *) |
| 1079 | +proc getMode*(fd: INT): (Mode, INT); (* the terminal's modes, to set again *) |
| 1080 | +proc setMode*(fd: INT; m: Mode): INT; (* once what was written to it is sent *) |
| 1058 | 1081 | proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) |
| 1059 | 1082 | proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 1060 | 1083 | proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) |
| | @@ -1062,6 +1085,7 @@ |
| 1062 | 1085 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| 1063 | 1086 | |
| 1064 | 1087 | proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) |
| 1088 | +proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) |
| 1065 | 1089 | proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) |
| 1066 | 1090 | proc setWorkDir*(path: []U8): INT; |
| 1067 | 1091 | |
| | @@ -1069,16 +1093,27 @@ |
| 1069 | 1093 | proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *) |
| 1070 | 1094 | proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) |
| 1071 | 1095 | proc spawn*(p: Program): INT; (* a program started: its process id *) |
| 1096 | +proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) |
| 1097 | +proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) |
| 1072 | 1098 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) |
| 1073 | 1099 | proc kill*(pid, signal: INT): INT; |
| 1074 | 1100 | proc pid*: INT; (* this program's process id *) |
| 1101 | +proc group*: INT; (* the process group this program is in *) |
| 1075 | 1102 | proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *) |
| 1076 | 1103 | proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *) |
| 1077 | 1104 | proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) |
| 1105 | +proc uid*: INT; (* the user this program runs as *) |
| 1106 | +proc euid*: INT; (* the user whose rights it has: 0 is root *) |
| 1107 | +proc hostName*: ([]U8, INT); (* this machine's name, good until the next call *) |
| 1108 | +proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *) |
| 1109 | +proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *) |
| 1110 | +proc setLimit*(which: INT; soft, hard: I64): INT; |
| 1078 | 1111 | |
| 1079 | 1112 | proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) |
| 1080 | 1113 | proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) |
| 1081 | 1114 | proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *) |
| 1115 | +proc handle*(sig: INT): INT; (* sig noted, for came; a system call it comes during is -4 *) |
| 1116 | +proc came*(sig: INT): BOOL; (* whether sig came since the last time it was asked *) |
| 1082 | 1117 | |
| 1083 | 1118 | proc clock*: I64; (* nanoseconds, on a clock that only goes forward *) |
| 1084 | 1119 | proc now*: I64; (* nanoseconds since 1970-01-01, UTC *) |
| | @@ -1103,13 +1138,16 @@ |
| 1103 | 1138 | proc memFile*(size: INT): INT; (* a file of size zero bytes, in memory, with no name: its fd *) |
| 1104 | 1139 | proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre> |
| 1105 | 1140 | <p><code>info</code> follows a symbolic link: <code>size</code>, <code>dir</code> and <code>file</code> are those of what it names, and <code>symlink</code> says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is <code>-ENOENT</code>, as a missing file is. <code>file</code> lets a walk open only what holds data: opening a pipe would wait for a program to write to it.</p> |
| 1106 | | -<p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p> |
| 1107 | | -<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock, which one open file holds at a time: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> |
| 1141 | +<p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>entryKind</code> says what the name it gave last is, a file, a directory, a symbolic link (whatever it names) or something else, as the directory itself records it, so that a program walking a tree needn’t ask <code>linkInfo</code> of every name, a system call each; where the file system doesn’t record it, it is <code>unknown</code>, and <code>linkInfo</code> must be asked. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p> |
| 1142 | +<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock, which one open file holds at a time: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.dev</code> is the file system the file is on: two files are one only if both <code>dev</code> and <code>ino</code> agree, which is how a program following links finds that one has led it back into a directory it is already in. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> |
| 1108 | 1143 | <p><strong>Programs.</strong> <code>openPty</code> makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: what it writes to the fd is typed, and what the programs on the terminal write it reads.</p> |
| 1109 | | -<p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "/bin/ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p> |
| 1144 | +<p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. A <code>p.path</code> without a <code>/</code> is looked for as a shell looks for a command, and as C’s <code>execvp</code> does: in each directory of this program’s <code>PATH</code> in turn, an empty one being the working directory, or of <code>/bin:/usr/bin</code> if <code>PATH</code> is unset or empty; the first that runs is the program. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file but those of <code>p.more</code>, each at its own number, as a shell passes <code>3<file</code> on, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p> |
| 1110 | 1145 | <p><code>p.group</code> puts the program in a process group, the programs that a shell’s job is made of: -1 makes it the first of a new group, whose number is its process id, and the other programs of the job join that number. The terminal gives its keys, and the signals of Ctrl-C and Ctrl-Z, to one group, the one in its foreground: <code>p.foreground</code> puts the program’s group there before it starts, and <code>foreground</code> puts any group there, as a shell does to take the terminal back. <code>setGroup</code> moves a program to a group, as the shell does for each program it starts, for whichever of the two runs first; a shell runs in a group of its own, <code>setGroup(0, 0)</code>.</p> |
| 1111 | | -<p><code>wait</code> waits for the program <code>pid</code> to change, or for any of this program’s if <code>pid</code> is -1, and says which one, and how: <code>exited</code>, with its exit status; <code>killed</code>, with the number of the signal that ended it; or <code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. Without <code>block</code> it doesn’t wait, and is 0 and <code>running</code> if no program changed. A program that has nothing to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal, <code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus its number.</p> |
| 1146 | +<p><code>exec</code> runs the program <code>p</code> describes in this program’s place, as a shell’s <code>exec cmd</code> does, found and started as <code>spawn</code> would start it, <code>p.tty</code>, <code>p.group</code>, <code>p.foreground</code>, <code>p.fds</code> and <code>p.dir</code> included, except that it keeps this program’s other open files: each stays open in the program unless <code>keepOnExec(fd, false)</code> was said of it, as it is of <code>pipe</code>’s, and those of <code>p.more</code> are kept. It returns only if the program can’t run: -2 (ENOENT) or -13 (EACCES) if there is none to run, with nothing done; or, if the program is found but won’t start, minus the errno, with what <code>p</code> asks of the terminal, the group, the files and the directory done, and this program’s signals as they were.</p> |
| 1147 | +<p>A shell moves its own files with <code>dup</code> and <code>dupTo</code>, for <code>2>&1</code> and <code>exec >log</code> and around a builtin: <code>dupTo(fd, into)</code> makes <code>into</code> the same open file as <code>fd</code>, closing what <code>into</code> was, and <code>dup</code> gives a new fd for it, the lowest free, as a shell saves a file to give it back. <code>dupAbove(fd, 10)</code> gives one at 10 or above, closed on exec, out of the way of the fds a script names (<code>3>log</code>), as a shell keeps its own. <code>fork</code> copies this program, which goes on from there in both: it is 0 in the copy and the copy’s process id in this one, as a shell runs a builtin or a procedure as a stage of a pipe. Only the thread that calls it is copied, so a program with threads mustn’t; and what <code>Out</code> keeps is copied too, to be written twice, so <code>Out.flush</code> comes first. <code>group</code> is the process group this program is in: a shell starting on a terminal waits until it is <code>foregroundOf(0)</code> before it takes it. <code>access</code> says whether this user, by its real ids, may read, write or execute a file, as <code>test -r</code>, <code>-w</code> and <code>-x</code> ask, which <code>Info.mode</code> alone can’t say; an empty <code>how</code> asks only whether it is there. <code>open</code> refuses <code>executing</code>, with -22 (EINVAL). <code>umask</code> sets the bits taken away from the mode of every file this program, and those it starts, make, and says what they were. <code>envAt</code> gives the environment one variable at a time, from 0, for a shell to list it and to pass it on whole with <code>onlyEnv</code>. <code>getMode</code> reads a terminal’s modes, and <code>setMode</code> sets them again once what was written is sent, so that a shell keeps them for each job: a program stopped by Ctrl-Z with its terminal raw gets it back raw, and the shell its own between. <code>cpuTime</code> says how long the processor ran this program, in its own code and in the system for it, or, if <code>children</code>, the programs it started and has waited for, as <code>time</code> and <code>times</code> say. <code>limit</code> gives a limit, <code>limitFiles</code> say, soft and hard, and <code>setLimit</code> sets them, as <code>ulimit</code> does: a program may raise its soft limit up to the hard one, and lower either. <code>uid</code> is the user this program runs as, <code>euid</code> the one whose rights it has, 0 for root, as a prompt shows <code>#</code>; <code>hostName</code> is the machine’s name.</p> |
| 1148 | +<p><code>wait</code> waits for the program <code>pid</code> to change, for any of this program’s if <code>pid</code> is -1, or for any of the group <code>g</code> if <code>pid</code> is <code>-g</code>, as a shell waits for a job’s programs, and says which one, and how: <code>exited</code>, with its exit status; <code>killed</code>, with the number of the signal that ended it; or <code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. Without <code>block</code> it doesn’t wait, and is 0 and <code>running</code> if no program changed. A program that has nothing to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal, <code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus its number.</p> |
| 1112 | 1149 | <p><strong>Signals.</strong> <code>catch</code> takes the signals <code>sigs</code> away from what they would do, ending the program most of them, and gives an fd that is ready to read when one comes, so that a program waits for signals with <code>ready</code> and <code>readyOf</code> as it waits for keys or a server. <code>caught</code> gives each signal that came, in turn. A later <code>catch</code> adds to the signals caught and gives the same fd. Catching <code>sigKill</code> or <code>sigStop</code>, or the signals <code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL). <code>ignore</code> makes a signal do nothing, as a shell ignores <code>sigTtou</code> so that it can take its terminal back, and <code>ignore(sig, false)</code> gives it back what it does at first. A program that <code>spawn</code> starts begins with neither.</p> |
| 1150 | +<p><code>handle</code> is for a program that must see a signal while it waits in a system call, as a shell waiting for its foreground program, or for a key, sees Ctrl-C: the signal is noted, and the call it comes during, <code>wait</code>, <code>read</code>, <code>ready</code> and the rest, fails with -4 (EINTR) rather than going on; <code>sleep</code> sleeps its time all the same. <code>came(sig)</code> says whether it came since the last time it was asked. Nothing else runs when the signal comes, so the program does what it does after it, in its own time; <code>Out</code> and <code>Files</code> try again after -4. <code>ignore(sig, false)</code> gives the signal back what it does at first, and the signals <code>catch</code> refuses, <code>handle</code> refuses too.</p> |
| 1113 | 1151 | <p><strong>Time.</strong> <code>clock</code> is for measuring how long something takes: it never goes back, and says nothing of the date. <code>now</code> is the date and time, which a person or the network may set back. <code>sleep</code> waits <code>ms</code> milliseconds.</p> |
| 1114 | 1152 | <p><strong>Sockets.</strong> <code>connect</code> is for talking to a server on the same machine, an X server at <code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one across the network, given its address as numbers, <code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an address is a library’s work over these. <code>connectUdp</code> gives a socket of datagrams, each <code>send</code> one message to that address and each <code>read</code> one message from it, as a program asks a name server. What a socket receives comes by <code>read</code>, what it sends goes by <code>send</code>, and <code>close</code> ends it. <code>send</code> is <code>write</code> for a socket, except when the other end has closed it: <code>write</code> would stop the program with the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. <code>listen</code> and <code>listenTcp</code> are the other side: a server’s socket, on <code>path</code>, which must not be there yet, or on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or <code>"::"</code> for every address the machine has; <code>accept</code> gives each connection that comes, as a socket of its own. A server started on port 0 gets a port the system chooses, which <code>localPort</code> says, as a test does that must not take a port in use. <code>connectTcp</code> waits as long as the system does, minutes for a host that never answers; <code>connectTcpWithin</code> gives up after <code>ms</code> milliseconds, with -110 (ETIMEDOUT).</p> |
| 1115 | 1153 | <p><code>listenUdp</code> is the server’s side of <code>connectUdp</code>: a socket of datagrams from anyone, as a name server has. <code>receive</code> gives the next datagram, at most <code>LEN(buf)</code> bytes of it, the rest lost, and a <code>Peer</code> that <code>reply</code> sends an answer to.</p> |
| | @@ -1122,6 +1160,7 @@ |
| 1122 | 1160 | proc eq*(a, b: []U8): BOOL; |
| 1123 | 1161 | proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *) |
| 1124 | 1162 | proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) |
| 1163 | +proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) |
| 1125 | 1164 | proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) |
| 1126 | 1165 | proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) |
| 1127 | 1166 | proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) |
| | @@ -1191,6 +1230,7 @@ |
| 1191 | 1230 | proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) |
| 1192 | 1231 | proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) |
| 1193 | 1232 | proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) |
| 1233 | +proc done*(m: ^Map); (* its memory given back, leaving it empty *) |
| 1194 | 1234 | |
| 1195 | 1235 | module Vec(T); |
| 1196 | 1236 | type Vec* = record len*: INT; ... end; |
| | @@ -1261,6 +1301,7 @@ |
| 1261 | 1301 | <pre><code>module Files; |
| 1262 | 1302 | proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) |
| 1263 | 1303 | proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) |
| 1304 | +proc load*(path: []U8; b: ^Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) |
| 1264 | 1305 | proc write*(path, data: []U8): BOOL; (* the file made to hold data *) |
| 1265 | 1306 | |
| 1266 | 1307 | module Opt; |