| | @@ -78,7 +78,7 @@ |
| 78 | 78 | <p>writes <code>"usage: tool [-v] file ...\n -v say what is done\n"</code>. Each line of an indented raw string must start with the closing line’s blanks, the same spaces and tabs in the same order, or it is an error; a line holding only blanks is an empty line of the text. A raw string that isn’t indented is the text between its backquotes as it stands.</p> |
| 79 | 79 | <p>A raw string can’t hold a backquote; a text that needs one joins it with <code>+</code>, as <code>"`"</code>. Everywhere else a raw string is a string, as one in double quotes is.</p> |
| 80 | 80 | <p>Messages give the line and the column, counted in characters (section 10).</p> |
| 81 | | -<p><strong>Comments</strong> are <code>(*</code> … <code>*)</code> and nest. By convention, the comment just before an exported declaration is its documentation, and the one before <code>module</code> is the module’s.</p> |
| 81 | +<p><strong>Comments</strong> are <code>(*</code> … <code>*)</code> and nest. A comment ends at the <code>*)</code> that closes it, wherever that stands, so one that quotes <code>*)</code>, as in <code>x*)</code> of a pattern, writes it apart: <code>x* )</code>. By convention, the comment just before an exported declaration is its documentation, and the one before <code>module</code> is the module’s.</p> |
| 82 | 82 | <p><strong>Operators and delimiters:</strong></p> |
| 83 | 83 | <pre><code>+ - * / & | ~ = # < <= > >= := : ; , . .. ... ( ) [ ] { } ^</code></pre> |
| 84 | 84 | <p><code>|</code> is also what starts an arm of <code>match</code> (section 7.4); an arm comes where a statement could, never after an operand, so the two don’t mix.</p> |
| | @@ -869,7 +869,7 @@ |
| 869 | 869 | </tr> |
| 870 | 870 | <tr> |
| 871 | 871 | <td><code>Sys</code></td> |
| 872 | | -<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> |
| 872 | +<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>setEnv</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>; what an errno means, <code>errorText</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> |
| 873 | 873 | </tr> |
| 874 | 874 | <tr> |
| 875 | 875 | <td><code>Out</code></td> |
| | @@ -913,7 +913,7 @@ |
| 913 | 913 | </tr> |
| 914 | 914 | <tr> |
| 915 | 915 | <td><code>Fmt</code></td> |
| 916 | | -<td>formatted output: <code>Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])</code>, or into a <code>Buf</code> with <code>Fmt.write</code></td> |
| 916 | +<td>formatted output: <code>Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])</code>, into a <code>Buf</code> with <code>Fmt.write</code>, or to standard error with <code>Fmt.eprint</code></td> |
| 917 | 917 | </tr> |
| 918 | 918 | <tr> |
| 919 | 919 | <td><code>List</code></td> |
| | @@ -1115,8 +1115,11 @@ |
| 1115 | 1115 | proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) |
| 1116 | 1116 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| 1117 | 1117 | |
| 1118 | +proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *) |
| 1119 | + |
| 1118 | 1120 | proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) |
| 1119 | 1121 | proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) |
| 1122 | +proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *) |
| 1120 | 1123 | proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) |
| 1121 | 1124 | proc setWorkDir*(path: []U8): INT; |
| 1122 | 1125 | |
| | @@ -1175,7 +1178,7 @@ |
| 1175 | 1178 | <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> |
| 1176 | 1179 | <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> |
| 1177 | 1180 | <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> |
| 1178 | | -<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> |
| 1181 | +<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>setEnv</code> refuses a name that is empty or holds an <code>=</code>, and a 0 in either, with -22; as C’s <code>setenv</code>, it isn’t safe while another thread reads the environment, and what <code>env</code> gave for a variable may be gone once it sets that variable again. <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> |
| 1179 | 1182 | <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> |
| 1180 | 1183 | <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> |
| 1181 | 1184 | <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> |
| | @@ -1194,6 +1197,7 @@ |
| 1194 | 1197 | proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) |
| 1195 | 1198 | proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) |
| 1196 | 1199 | proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) |
| 1200 | +proc findFrom*(s, sub: []U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *) |
| 1197 | 1201 | proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) |
| 1198 | 1202 | proc hasPrefix*(s, p: []U8): BOOL; |
| 1199 | 1203 | proc hasSuffix*(s, x: []U8): BOOL; |
| | @@ -1202,6 +1206,11 @@ |
| 1202 | 1206 | proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *) |
| 1203 | 1207 | proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *) |
| 1204 | 1208 | proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *) |
| 1209 | +proc isDigit*(c: U8): BOOL; (* 0 to 9 *) |
| 1210 | +proc isLetter*(c: U8): BOOL; (* A to Z, a to z *) |
| 1211 | +proc isSpace*(c: U8): BOOL; (* a space, as trim takes it *) |
| 1212 | +proc isPunct*(c: U8): BOOL; (* printable, and neither a letter, a digit nor the blank *) |
| 1213 | +proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *) |
| 1205 | 1214 | proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) |
| 1206 | 1215 | proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) |
| 1207 | 1216 | proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) |
| | @@ -1242,6 +1251,7 @@ |
| 1242 | 1251 | real(r: F64; places: INT); |
| 1243 | 1252 | end; |
| 1244 | 1253 | proc print*(f: []U8; args: []Arg); (* to standard output *) |
| 1254 | +proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) |
| 1245 | 1255 | proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) |
| 1246 | 1256 | |
| 1247 | 1257 | module List(T); |
| | @@ -1269,6 +1279,8 @@ |
| 1269 | 1279 | proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) |
| 1270 | 1280 | proc push*(v: ^Vec; x: T); (* x added at the end *) |
| 1271 | 1281 | proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) |
| 1282 | +proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) |
| 1283 | +proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) |
| 1272 | 1284 | proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *) |
| 1273 | 1285 | proc all*(v: ^Vec): []T; (* the elements, until it next grows *) |
| 1274 | 1286 | proc reset*(v: ^Vec); (* emptied, its memory kept *) |
| | @@ -1306,8 +1318,8 @@ |
| 1306 | 1318 | ... (* c is a character of s *) |
| 1307 | 1319 | c, i := Str.next(s, i); |
| 1308 | 1320 | end;</code></pre> |
| 1309 | | -<p>A string is bytes, and <code>Str</code> takes no more of Unicode than UTF-8: <code>lower</code>, <code>upper</code> and <code>eqFold</code> change and match ASCII letters alone, and nothing says how wide a character is on a terminal, nor which spellings of a text are the same.</p> |
| 1310 | | -<p>A space, to <code>trim</code>, <code>trimLeft</code>, <code>trimRight</code> and <code>field</code>, is a blank, a tab, a line feed, a carriage return, a vertical tab or a form feed. <code>cut</code> gives what comes before the first <code>sep</code> in <code>s</code>, what comes after it, and <code>true</code>; or <code>s</code>, an empty string and <code>false</code> if <code>s</code> has no <code>sep</code>. <code>field</code> passes the spaces at the start of <code>s</code> and gives the bytes up to the next space or the end, and the rest of <code>s</code> from there; two empty strings if <code>s</code> holds only spaces. They take a string apart without memory of their own, each part a slice of <code>s</code>:</p> |
| 1321 | +<p>A string is bytes, and <code>Str</code> takes no more of Unicode than UTF-8: <code>lower</code>, <code>upper</code> and <code>eqFold</code> change and match ASCII letters alone, the character tests know ASCII alone, and nothing says how wide a character is on a terminal, nor which spellings of a text are the same.</p> |
| 1322 | +<p>A space, to <code>isSpace</code>, <code>trim</code>, <code>trimLeft</code>, <code>trimRight</code> and <code>field</code>, is a blank, a tab, a line feed, a carriage return, a vertical tab or a form feed. <code>cut</code> gives what comes before the first <code>sep</code> in <code>s</code>, what comes after it, and <code>true</code>; or <code>s</code>, an empty string and <code>false</code> if <code>s</code> has no <code>sep</code>. <code>field</code> passes the spaces at the start of <code>s</code> and gives the bytes up to the next space or the end, and the rest of <code>s</code> from there; two empty strings if <code>s</code> holds only spaces. They take a string apart without memory of their own, each part a slice of <code>s</code>:</p> |
| 1311 | 1323 | <pre><code>line, rest, more := Str.cut(text, "\n"); (* the first line, and the others *) |
| 1312 | 1324 | key, val, ok := Str.cut(line, "="); |
| 1313 | 1325 | w, rest := Str.field(line); (* the words of a line *) |