| | @@ -921,7 +921,7 @@ |
| 921 | 921 | | Module | What | |
| 922 | 922 | |---|---| |
| 923 | 923 | | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail | |
| 924 | | -| `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 | |
| 924 | +| `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; and sockets. One file per target, and `Sys.raz` over the C library for the others | |
| 925 | 925 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 926 | 926 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 927 | 927 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| | @@ -929,11 +929,13 @@ |
| 929 | 929 | | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | |
| 930 | 930 | | `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program | |
| 931 | 931 | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | |
| 932 | | -| `Str` | operations on `[]U8` strings | |
| 932 | +| `Str` | operations on `[]U8` strings: comparing, hashing, finding, UTF-8, reading a number | |
| 933 | 933 | | `Buf` | a growable byte buffer | |
| 934 | | -| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])`, or into a `Buf` with `Fmt.write` | |
| 934 | +| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` | |
| 935 | 935 | | `List` | a generic singly linked list, `List(T)` | |
| 936 | 936 | | `Map` | a generic hash map, `Map(K, V; hash, eq)` | |
| 937 | +| `Sort` | sorting a slice in place, `Sort(T; less)` | |
| 938 | +| `Math` | `sqrt`, `floor`, `sin`, `cos`, `atan2`, `exp`, `ln` and `pi`, for `F64` | |
| 937 | 939 | |
| 938 | 940 | ``` |
| 939 | 941 | module Mem; |
| | @@ -952,18 +954,50 @@ |
| 952 | 954 | number, minus an errno, as on Linux; 0 is success. |
| 953 | 955 | |
| 954 | 956 | ``` |
| 957 | +const |
| 958 | + stdin* = 0; stdout* = 1; stderr* = 2; |
| 959 | + (* signals, by their numbers on Linux; each target has its own *) |
| 960 | + sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 961 | + sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 962 | + sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 963 | + |
| 955 | 964 | type |
| 965 | + Access* = (reading, writing, creating, exclusive, truncating, appending); |
| 966 | + Accesses* = set of Access; |
| 956 | 967 | Info* = record |
| 957 | 968 | size*: I64; (* in bytes *) |
| 969 | + mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) |
| 970 | + mode*: I64; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *) |
| 958 | 971 | dir*: BOOL; (* a directory *) |
| 959 | 972 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 960 | 973 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 961 | 974 | end; |
| 962 | 975 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 976 | + Program* = record |
| 977 | + path*: []U8; |
| 978 | + args*: [][]U8; (* the first is its name *) |
| 979 | + env*: [][]U8; (* NAME=value, each added to this program's environment *) |
| 980 | + onlyEnv*: BOOL; (* env is its whole environment *) |
| 981 | + dir*: []U8; (* its working directory; "" for this program's *) |
| 982 | + fds*: [3]I64; (* its standard input, output and error; 0 for this program's own *) |
| 983 | + group*: I64; (* the process group it joins: 0 for this program's, -1 a new one it leads *) |
| 984 | + foreground*: BOOL; (* its group takes the terminal on standard input *) |
| 985 | + tty*: I64; (* a pseudo-terminal's fd, to start a session on; 0 for none *) |
| 986 | + end; |
| 987 | + Status* = (running, exited, killed, stopped); |
| 963 | 988 | |
| 989 | +proc open*(path: []U8; how: Accesses): I64; (* its fd *) |
| 990 | +proc openRead*(path: []U8): I64; (* open(path, {reading}) *) |
| 991 | +proc create*(path: []U8): I64; (* open(path, {writing, creating, truncating}) *) |
| 992 | +proc seek*(fd, pos: I64; fromEnd: BOOL): I64; (* the new position *) |
| 993 | + |
| 964 | 994 | proc rename*(old, new: []U8): I64; (* replaces what new names *) |
| 965 | 995 | proc remove*(path: []U8): I64; (* a file, not a directory *) |
| 996 | +proc removeDir*(path: []U8): I64; (* an empty directory *) |
| 966 | 997 | proc makeDir*(path: []U8): I64; (* readable by all, writable by its owner *) |
| 998 | +proc chmod*(path: []U8; mode: I64): I64; |
| 999 | +proc symlink*(target, path: []U8): I64; (* path made a link to target *) |
| 1000 | +proc readLink*(path: []U8): ([]U8, I64); (* what the link names, good until the next call *) |
| 967 | 1001 | proc info*(path: []U8): (Info, I64); (* following a symbolic link *) |
| 968 | 1002 | |
| 969 | 1003 | proc openDir*(d: ^Dir; path: []U8): I64; |
| | @@ -974,19 +1008,39 @@ |
| 974 | 1008 | proc restoreMode*(fd: I64); (* as rawMode found it *) |
| 975 | 1009 | proc termSize*(fd: I64): (I64, I64); (* columns and rows; 80 and 24 if unknown *) |
| 976 | 1010 | proc ready*(fd: I64; ms: I64): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 1011 | +proc readyOf*(fds: []I64; ms: I64): I64; (* the first of fds with something to read within ms, or -1 *) |
| 977 | 1012 | |
| 978 | 1013 | proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) |
| 979 | 1014 | proc workDir*: ([]U8, I64); (* the working directory, good until the next call *) |
| 1015 | +proc setWorkDir*(path: []U8): I64; |
| 980 | 1016 | |
| 981 | | -proc connect*(path: []U8): I64; (* a stream socket, connected to the Unix socket at path: its fd *) |
| 982 | | -proc send*(fd: I64; s: []U8): I64; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) |
| 983 | | -proc readyOf*(fds: []I64; ms: I64): I64; (* the first of fds with something to read within ms, or -1 *) |
| 984 | | - |
| 1017 | +proc pipe*: (I64, I64); (* the fd to read and the fd to write; or minus an errno and -1 *) |
| 985 | 1018 | proc openPty*: I64; (* a pseudo-terminal: the fd of its controlling side *) |
| 986 | 1019 | proc setTermSize*(fd, cols, rows: I64): I64; (* the size its programs are told of *) |
| 987 | | -proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64; (* a program started: its process id *) |
| 988 | | -proc wait*(pid: I64; block: BOOL): (I64, I64); (* its status, and 1 if it has ended, 0 if not *) |
| 1020 | +proc spawn*(p: Program): I64; (* a program started: its process id *) |
| 1021 | +proc wait*(pid: I64; block: BOOL): (I64, Status, I64); (* which program, how, and its code *) |
| 989 | 1022 | proc kill*(pid, signal: I64): I64; |
| 1023 | +proc pid*: I64; (* this program's process id *) |
| 1024 | +proc setGroup*(pid, group: I64): I64; (* pid 0 is this program; group 0 a new one pid leads *) |
| 1025 | +proc foreground*(fd, group: I64): I64; (* the terminal on fd gives its keys to group *) |
| 1026 | +proc foregroundOf*(fd: I64): I64; (* the group the terminal on fd gives its keys to *) |
| 1027 | + |
| 1028 | +proc catch*(sigs: []I64): I64; (* sigs caught: an fd to wait on *) |
| 1029 | +proc caught*(fd: I64): I64; (* the next signal caught, or 0 if none is waiting *) |
| 1030 | +proc ignore*(sig: I64; yes: BOOL): I64; (* sig ignored; or, if not yes, back to what it does at first *) |
| 1031 | + |
| 1032 | +proc clock*: I64; (* nanoseconds, on a clock that only goes forward *) |
| 1033 | +proc now*: I64; (* nanoseconds since 1970-01-01, UTC *) |
| 1034 | +proc sleep*(ms: I64); |
| 1035 | +proc random*(b: []U8); (* b filled with random bytes *) |
| 1036 | + |
| 1037 | +proc connect*(path: []U8): I64; (* a stream socket, connected to the Unix socket at path: its fd *) |
| 1038 | +proc connectTcp*(addr: []U8; port: I64): I64; (* addr an IPv4 or IPv6 address, as numbers *) |
| 1039 | +proc connectUdp*(addr: []U8; port: I64): I64; |
| 1040 | +proc listen*(path: []U8): I64; (* a Unix socket made at path, taking connections *) |
| 1041 | +proc listenTcp*(addr: []U8; port: I64): I64; |
| 1042 | +proc accept*(fd: I64): I64; (* the next connection, waiting for one *) |
| 1043 | +proc send*(fd: I64; s: []U8): I64; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) |
| 990 | 1044 | ``` |
| 991 | 1045 | |
| 992 | 1046 | `info` follows a symbolic link: `size`, `dir` and `file` are those of |
| | @@ -1014,20 +1068,152 @@ |
| 1014 | 1068 | since a read of it won't wait. It looks at the first 64 of `fds`, and |
| 1015 | 1069 | `connect` takes a path of at most 107 bytes, as `struct sockaddr_un` |
| 1016 | 1070 | holds; a longer one is -36 (ENAMETOOLONG). |
| 1071 | + |
| 1072 | +**Files.** `open` gives a file for what `how` asks: `reading`, |
| 1073 | +`writing` or both; `creating` it if it is missing, readable by all and |
| 1074 | +writable by its owner; with `exclusive`, failing with -17 (EEXIST) if |
| 1075 | +it is there, as a program does that makes a lock or a file of its |
| 1076 | +own; `truncating` it to nothing; `appending`, each write going to its |
| 1077 | +end, however other programs write it too. `seek` moves to `pos` from |
| 1078 | +the start of the file, or from its end if `fromEnd`, where a negative |
| 1079 | +`pos` reads back from the end. `Info.mtime` is what a program checks |
| 1080 | +to see whether a file changed under it; `Info.mode` holds the nine |
| 1081 | +bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, |
| 1082 | +1A4H (644) for data. |
| 1083 | + |
| 1084 | +**Programs.** `openPty` makes a pseudo-terminal, a terminal that a |
| 1085 | +program, a terminal emulator, plays the screen and the keyboard of: |
| 1086 | +what it writes to the fd is typed, and what the programs on the |
| 1087 | +terminal write it reads. |
| 1017 | 1088 | |
| 1018 | | -`openPty` makes a pseudo-terminal, a terminal that a program, a |
| 1019 | | -terminal emulator, plays the screen and the keyboard of: what it |
| 1020 | | -writes to the fd is typed, and what the programs on the terminal |
| 1021 | | -write it reads. `spawn` starts the program at `path`, with `args` as |
| 1022 | | -its arguments, the first being its name, and the environment of this |
| 1023 | | -one with each `NAME=value` of `env` added or put in place of the old |
| 1024 | | -value. If `tty` is a pseudo-terminal's fd, the program runs on that |
| 1025 | | -terminal: it is its controlling terminal, and the program's standard |
| 1026 | | -input, output and error. The program gets no other open file. `wait` |
| 1027 | | -says whether the program has ended, waiting for it if `block`, and how: |
| 1028 | | -its exit status, or 128 and the number of the signal that stopped it. |
| 1029 | | -`kill` sends it a signal, 15 to end it. |
| 1089 | +`spawn` starts the program at `p.path`, with `p.args` as its |
| 1090 | +arguments, the first being its name. Its environment is this |
| 1091 | +program's with each `NAME=value` of `p.env` added or put in place of |
| 1092 | +the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in |
| 1093 | +`p.dir`. Its standard input, output and error are `p.fds`; a 0 gives |
| 1094 | +it this program's own, so `{path: "/bin/ls", args: ["ls"]}` runs |
| 1095 | +`ls` as a shell runs it, with every other field zero. It gets no |
| 1096 | +other open file, every signal does what it does at first, and none |
| 1097 | +is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a |
| 1098 | +session on that terminal: it is its controlling terminal, and its |
| 1099 | +standard input, output and error, and `p.fds` and `p.group` are not |
| 1100 | +used. |
| 1030 | 1101 | |
| 1102 | +`p.group` puts the program in a process group, the programs that a |
| 1103 | +shell's job is made of: -1 makes it the first of a new group, whose |
| 1104 | +number is its process id, and the other programs of the job join that |
| 1105 | +number. The terminal gives its keys, and the signals of Ctrl-C and |
| 1106 | +Ctrl-Z, to one group, the one in its foreground: `p.foreground` puts |
| 1107 | +the program's group there before it starts, and `foreground` puts any |
| 1108 | +group there, as a shell does to take the terminal back. `setGroup` |
| 1109 | +moves a program to a group, as the shell does for each program it |
| 1110 | +starts, for whichever of the two runs first; a shell runs in a group |
| 1111 | +of its own, `setGroup(0, 0)`. |
| 1112 | + |
| 1113 | +`wait` waits for the program `pid` to change, or for any of this |
| 1114 | +program's if `pid` is -1, and says which one, and how: `exited`, with |
| 1115 | +its exit status; `killed`, with the number of the signal that ended |
| 1116 | +it; or `stopped`, by Ctrl-Z or a signal, with the signal's number. |
| 1117 | +Without `block` it doesn't wait, and is 0 and `running` if no program |
| 1118 | +changed. A program that has nothing to wait for is -10 (ECHILD). |
| 1119 | +`kill` sends a program a signal, `sigTerm` to end it; to a group, if |
| 1120 | +`pid` is minus its number. |
| 1121 | + |
| 1122 | +**Signals.** `catch` takes the signals `sigs` away from what they |
| 1123 | +would do, ending the program most of them, and gives an fd that is |
| 1124 | +ready to read when one comes, so that a program waits for signals with |
| 1125 | +`ready` and `readyOf` as it waits for keys or a server. `caught` gives |
| 1126 | +each signal that came, in turn. A later `catch` adds to the signals |
| 1127 | +caught and gives the same fd. Catching `sigKill` or `sigStop`, or the |
| 1128 | +signals `Rt` reports, SIGSEGV and SIGFPE, is -22 (EINVAL). `ignore` |
| 1129 | +makes a signal do nothing, as a shell ignores `sigTtou` so that it can |
| 1130 | +take its terminal back, and `ignore(sig, false)` gives it back what it |
| 1131 | +does at first. A program that `spawn` starts begins with neither. |
| 1132 | + |
| 1133 | +**Time.** `clock` is for measuring how long something takes: it never |
| 1134 | +goes back, and says nothing of the date. `now` is the date and time, |
| 1135 | +which a person or the network may set back. `sleep` waits `ms` |
| 1136 | +milliseconds. |
| 1137 | + |
| 1138 | +**Sockets.** `connect` is for talking to a server on the same machine, |
| 1139 | +an X server at `/tmp/.X11-unix/X0` say; `connectTcp` for one across |
| 1140 | +the network, given its address as numbers, `"127.0.0.1"` or `"::1"`: |
| 1141 | +turning a name into an address is a library's work over these. |
| 1142 | +`connectUdp` gives a socket of datagrams, each `send` one message to |
| 1143 | +that address and each `read` one message from it, as a program asks a |
| 1144 | +name server. What a socket receives comes by `read`, what it sends goes |
| 1145 | +by `send`, and `close` ends it. `send` is `write` for a socket, except |
| 1146 | +when the other end has closed it: `write` would stop the program with |
| 1147 | +the signal SIGPIPE, where `send` returns -32 (EPIPE), so that a |
| 1148 | +program learns its server has gone and can end as it wants. |
| 1149 | +`listen` and `listenTcp` are the other side: a server's socket, on |
| 1150 | +`path`, which must not be there yet, or on `port` at `addr`, |
| 1151 | +`"0.0.0.0"` or `"::"` for every address the machine has; `accept` |
| 1152 | +gives each connection that comes, as a socket of its own. |
| 1153 | + |
| 1154 | +`readyOf` waits on several file descriptors at once, as a program does |
| 1155 | +that listens to a server and a terminal both; a descriptor at its end, |
| 1156 | +or failed, counts as ready, since a read of it won't wait. It looks at |
| 1157 | +the first 64 of `fds`, and `connect` and `listen` take a path of at |
| 1158 | +most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36 |
| 1159 | +(ENAMETOOLONG). |
| 1160 | + |
| 1161 | +**Str, Map, Sort, Math and Fmt** gain these: |
| 1162 | + |
| 1163 | +``` |
| 1164 | +module Str; |
| 1165 | +proc encode*(c: I64): ([4]U8, I64); (* c in UTF-8: its bytes, and how many *) |
| 1166 | +proc decode*(s: []U8): (I64, I64); (* the character s starts with, and its length in bytes *) |
| 1167 | +proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) |
| 1168 | + |
| 1169 | +module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 1170 | +proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) |
| 1171 | +proc next*(m: ^Map; i: I64): (I64, K, V); (* the entry after i *) |
| 1172 | + |
| 1173 | +module Sort(T; less: proc(a, b: T): BOOL); |
| 1174 | +proc sort*(s: []T); |
| 1175 | + |
| 1176 | +module Math; |
| 1177 | +const pi* = 3.141592653589793; |
| 1178 | +proc sqrt*(x: F64): F64; |
| 1179 | +proc floor*(x: F64): F64; |
| 1180 | +proc sin*(x: F64): F64; |
| 1181 | +proc cos*(x: F64): F64; |
| 1182 | +proc atan2*(y, x: F64): F64; |
| 1183 | +proc exp*(x: F64): F64; |
| 1184 | +proc ln*(x: F64): F64; |
| 1185 | + |
| 1186 | +module Fmt; |
| 1187 | +type Arg* = variant ... real(r: F64; places: I64); end; |
| 1188 | +``` |
| 1189 | + |
| 1190 | +`Str` reads and writes UTF-8. `encode` writes a character, of at most |
| 1191 | +10FFFFH, in one to four bytes; a number that isn't one becomes FFFDH, |
| 1192 | +the character that stands for a wrong one. `decode` reads the first |
| 1193 | +character of `s`, and gives FFFDH and 1 for a byte that doesn't start |
| 1194 | +one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal |
| 1195 | +number, with a `-` or `+` before it if any, and nothing else. |
| 1196 | + |
| 1197 | +`Map.next` walks a map: from 0, it gives the next entry and the number |
| 1198 | +to pass for the one after it, and -1 when there are no more. A walk |
| 1199 | +sees each entry once if the map doesn't change during it: |
| 1200 | + |
| 1201 | +``` |
| 1202 | +i, k, v := Names.next(ADR(names), 0); |
| 1203 | +while i >= 0 do |
| 1204 | + Fmt.print("{} is {}\n", [Fmt.str(k), Fmt.int(v)]); |
| 1205 | + i, k, v := Names.next(ADR(names), i); |
| 1206 | +end; |
| 1207 | +``` |
| 1208 | + |
| 1209 | +`Sort.sort` puts `s` in the order `less` gives, in place, without |
| 1210 | +memory of its own, in time `n log n` at most; two elements equal by |
| 1211 | +`less` may change places. `Math`'s procedures are as C's `math.h` |
| 1212 | +has them, to within the last bit or so. `Fmt.real(x, places)` writes |
| 1213 | +`x` with `places` digits after the point, at most 9, rounded to the |
| 1214 | +nearest; from 1e15 on, with an exponent, as `1.50e17`; and `nan`, |
| 1215 | +`inf` and `-inf` as themselves. |
| 1216 | + |
| 1031 | 1217 | **Out and C's stdio.** `Out` keeps a buffer of 4 KB. It writes it out |
| 1032 | 1218 | when it is full, at each line feed when standard output is a terminal, |
| 1033 | 1219 | at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio` |
| | @@ -1085,3 +1271,11 @@ |
| 1085 | 1271 | (section 11). |
| 1086 | 1272 | - **2026-10.8.** `Sys.workDir` returns the directory, with no `var` |
| 1087 | 1273 | parameter (section 11). |
| 1274 | +- **2026-10.9**, under way: what follows is in the report, but not |
| 1275 | + yet all in the library. `Sys` gains opening a file for any use, seeking, |
| 1276 | + links, permissions and times; pipes, process groups and the |
| 1277 | + terminal's foreground; signals caught on a file descriptor; clocks, |
| 1278 | + sleeping and random bytes; and network sockets. `Sys.spawn` takes a |
| 1279 | + `Program`, and `Sys.wait` says which program changed and how. `Str` |
| 1280 | + gains UTF-8 and reading a number, `Map` removing and walking, `Fmt` |
| 1281 | + reals; `Sort` and `Math` are new (section 11). |