site: the tours for v0.16.0 Records: a constant typed []Planet takes its literal's length. Collections: Map.done and Vec's done deferred; Str.free with Str.copy. Files: Files.load says why. Processes: signals, catch and handle.
| @@ -121,6 +121,7 @@ | ||
| 121 | 121 | w, rest: []U8; key: []U8; v: INT; all: []Entry; |
| 122 | 122 | begin |
| 123 | 123 | Count.init(ADR(m), Heap.heap); |
| 124 | + defer Count.done(ADR(m)); | |
| 124 | 125 | w, rest := Str.field(Text); |
| 125 | 126 | while LEN(w) > 0 do |
| 126 | 127 | k, _ := Count.get(ADR(m), w); |
| @@ -128,6 +129,7 @@ | ||
| 128 | 129 | w, rest := Str.field(rest); |
| 129 | 130 | end; |
| 130 | 131 | Entries.init(ADR(entries), Heap.heap); |
| 132 | + defer Entries.done(ADR(entries)); | |
| 131 | 133 | found, key, v := Count.next(ADR(m), 0); |
| 132 | 134 | while found >= 0 do |
| 133 | 135 | Entries.push(ADR(entries), {word: key, n: v}); |
| @@ -143,7 +145,7 @@ | ||
| 143 | 145 | end Freq. |
| 144 | 146 | ``` |
| 145 | 147 | |
| 146 | -The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with `Str.copy`. | |
| 148 | +The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with `Str.copy`, and gives each back with `Str.free` before `Count.done` gives back the map's own memory. | |
| 147 | 149 | |
| 148 | 150 | ## Choosing |
| 149 | 151 |
| @@ -25,10 +25,13 @@ | ||
| 25 | 25 | end; |
| 26 | 26 | ok := Files.read("missing.txt", ADR(b)); |
| 27 | 27 | if not ok then Out.str("missing.txt: not there\n"); end; |
| 28 | + if Files.load("missing.txt", ADR(b)) = -2 then Out.str("missing.txt: ENOENT\n"); end; | |
| 28 | 29 | end; |
| 29 | 30 | |
| 30 | 31 | end Notes. |
| 31 | 32 | ``` |
| 33 | + | |
| 34 | +When the reason matters, `Files.load` says it: 0, or minus the errno of what failed, -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed. | |
| 32 | 35 | |
| 33 | 36 | ## Descriptors |
| 34 | 37 |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | title: Time, randomness and other programs |
| 2 | 2 | part: The library |
| 3 | -summary: Clocks, sleeping, random bytes, and running a program with its output piped back. | |
| 3 | +summary: Clocks, sleeping, random bytes, running a program with its output piped back, and signals. | |
| 4 | 4 | --- |
| 5 | 5 | Everything here is in `Sys`, and is the same procedures on every target raz supports: through system calls on Linux, through the C library elsewhere. |
| 6 | 6 | |
| @@ -153,4 +153,27 @@ | ||
| 153 | 153 | end; |
| 154 | 154 | |
| 155 | 155 | end Where. |
| 156 | +``` | |
| 157 | + | |
| 158 | +## Signals | |
| 159 | + | |
| 160 | +A signal, Ctrl-C's `sigInt` or `sigTerm` from `kill`, ends a program unless it says otherwise. `Sys.catch` takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with `Sys.readyOf`, waits for signals the same way; `Sys.caught` gives each in turn. `Sys.handle` is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for `Sys.came`, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns. | |
| 161 | + | |
| 162 | +```raz Signals run | |
| 163 | +module Signals; | |
| 164 | +import Out, Sys; | |
| 165 | + | |
| 166 | +proc main*; | |
| 167 | +var fd: INT; | |
| 168 | +begin | |
| 169 | + fd := Sys.catch([Sys.sigUsr1]); | |
| 170 | + _ := Sys.kill(Sys.pid(), Sys.sigUsr1); | |
| 171 | + if Sys.ready(fd, 1000) and (Sys.caught(fd) = Sys.sigUsr1) then Out.str("caught sigUsr1\n"); end; | |
| 172 | + _ := Sys.handle(Sys.sigUsr2); | |
| 173 | + _ := Sys.kill(Sys.pid(), Sys.sigUsr2); | |
| 174 | + if Sys.came(Sys.sigUsr2) then Out.str("sigUsr2 came\n"); end; | |
| 175 | + if not Sys.came(Sys.sigUsr2) then Out.str("and was seen once\n"); end; | |
| 176 | +end; | |
| 177 | + | |
| 178 | +end Signals. | |
| 156 | 179 | ``` |
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | |
| 52 | 52 | type Planet = record name: []U8; moons: INT; water: BOOL; end; |
| 53 | 53 | |
| 54 | -const Inner: [4]Planet = [ | |
| 54 | +const Inner: []Planet = [ | |
| 55 | 55 | {name: "Mercury"}, |
| 56 | 56 | {name: "Venus"}, |
| 57 | 57 | {name: "Earth", moons: 1, water: true}, |
| @@ -70,7 +70,7 @@ | ||
| 70 | 70 | end Planets. |
| 71 | 71 | ``` |
| 72 | 72 | |
| 73 | -Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`. | |
| 73 | +Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`. A constant typed `[]Planet` takes its length from its literal, here 4, so a planet added to the list needs no count changed. | |
| 74 | 74 | |
| 75 | 75 | ## Layout |
| 76 | 76 |
| @@ -152,6 +152,7 @@ | ||
| 152 | 152 | w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry; |
| 153 | 153 | <span class="kw">begin</span> |
| 154 | 154 | Count.init(<span class="ty">ADR</span>(m), Heap.heap); |
| 155 | + <span class="kw">defer</span> Count.done(<span class="ty">ADR</span>(m)); | |
| 155 | 156 | w, rest := Str.field(Text); |
| 156 | 157 | <span class="kw">while</span> <span class="ty">LEN</span>(w) > <span class="number">0</span> <span class="kw">do</span> |
| 157 | 158 | k, _ := Count.get(<span class="ty">ADR</span>(m), w); |
| @@ -159,6 +160,7 @@ | ||
| 159 | 160 | w, rest := Str.field(rest); |
| 160 | 161 | <span class="kw">end</span>; |
| 161 | 162 | Entries.init(<span class="ty">ADR</span>(entries), Heap.heap); |
| 163 | + <span class="kw">defer</span> Entries.done(<span class="ty">ADR</span>(entries)); | |
| 162 | 164 | found, key, v := Count.next(<span class="ty">ADR</span>(m), <span class="number">0</span>); |
| 163 | 165 | <span class="kw">while</span> found >= <span class="number">0</span> <span class="kw">do</span> |
| 164 | 166 | Entries.push(<span class="ty">ADR</span>(entries), {word: key, n: v}); |
| @@ -181,7 +183,7 @@ | ||
| 181 | 183 | 1 mat |
| 182 | 184 | 1 on |
| 183 | 185 | </code></pre> |
| 184 | -<p>The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with <code>Str.copy</code>.</p> | |
| 186 | +<p>The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with <code>Str.copy</code>, and gives each back with <code>Str.free</code> before <code>Count.done</code> gives back the map's own memory.</p> | |
| 185 | 187 | <h2>Choosing</h2> |
| 186 | 188 | <p>Use a <code>List</code> when you push and pop at one end and walk it in order. Use a <code>Vec</code> when you add at the end and index, and don't know how many will come; an array when you do. Use a <code>Map</code> when you look things up by key. Use <code>Sort</code> on either's slice when you want an order. When none of them fits, they are each under a hundred lines of raz in <code>lib/</code>, and the advanced tour's allocator lesson shows what is underneath.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="bytes.html">← Strings and bytes</a><a href="index.html">Contents</a><a class="next" href="files.html">Files and the command line →</a></div></article></div> |
| 187 | 189 | </main> |
| @@ -63,13 +63,16 @@ | ||
| 63 | 63 | <span class="kw">end</span>; |
| 64 | 64 | ok := Files.read(<span class="string">"missing.txt"</span>, <span class="ty">ADR</span>(b)); |
| 65 | 65 | <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">"missing.txt: not there\n"</span>); <span class="kw">end</span>; |
| 66 | + <span class="kw">if</span> Files.load(<span class="string">"missing.txt"</span>, <span class="ty">ADR</span>(b)) = -<span class="number">2</span> <span class="kw">then</span> Out.str(<span class="string">"missing.txt: ENOENT\n"</span>); <span class="kw">end</span>; | |
| 66 | 67 | <span class="kw">end</span>; |
| 67 | 68 | |
| 68 | 69 | <span class="kw">end</span> Notes.</code></pre> |
| 69 | 70 | <pre class="output"><code>23 bytes: first line |
| 70 | 71 | second line |
| 71 | 72 | missing.txt: not there |
| 73 | +missing.txt: ENOENT | |
| 72 | 74 | </code></pre> |
| 75 | +<p>When the reason matters, <code>Files.load</code> says it: 0, or minus the errno of what failed, -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed.</p> | |
| 73 | 76 | <h2>Descriptors</h2> |
| 74 | 77 | <p>Underneath, a file is a descriptor, an <code>INT</code>. <code>Sys.open</code> takes the path and the way to open it as a set; <code>read</code>, <code>write</code>, <code>seek</code> and <code>close</code> do what they say. A result that can fail is a number, and minus an errno if it failed, as on Linux itself. Reading in a loop is the pattern for anything too big to hold at once.</p> |
| 75 | 78 | <div class="file"><span>Count.raz</span></div><pre class="code"><code><span class="kw">module</span> Count; |
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | <li><a href="bytes.html">Strings and bytes</a> <span>Copying, hashing and ordering strings, searching bytes many at a time, and reading and writing binary formats.</span></li> |
| 34 | 34 | <li><a href="collections.html">Lists, arrays, maps and sorting</a> <span>List(T), Vec(T), Map(K, V) and Sort(T), put together in a word-frequency program.</span></li> |
| 35 | 35 | <li><a href="files.html">Files and the command line</a> <span>Whole files with Files, descriptors and directories with Sys, and options with Opt.</span></li> |
| 36 | -<li><a href="processes.html">Time, randomness and other programs</a> <span>Clocks, sleeping, random bytes, and running a program with its output piped back.</span></li> | |
| 36 | +<li><a href="processes.html">Time, randomness and other programs</a> <span>Clocks, sleeping, random bytes, running a program with its output piped back, and signals.</span></li> | |
| 37 | 37 | <li><a href="allocators.html">Writing an allocator</a> <span>Mem.Allocator is two procedures; make one over a fixed array, and one that counts.</span></li> |
| 38 | 38 | </ol> |
| 39 | 39 | <h2>Standing alone</h2><ol start='7'> |
| @@ -184,6 +184,28 @@ | ||
| 184 | 184 | <span class="kw">end</span> Where.</code></pre> |
| 185 | 185 | <pre class="output"><code>unset reads as empty |
| 186 | 186 | hello from /tmp |
| 187 | +</code></pre> | |
| 188 | +<h2>Signals</h2> | |
| 189 | +<p>A signal, Ctrl-C's <code>sigInt</code> or <code>sigTerm</code> from <code>kill</code>, ends a program unless it says otherwise. <code>Sys.catch</code> takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with <code>Sys.readyOf</code>, waits for signals the same way; <code>Sys.caught</code> gives each in turn. <code>Sys.handle</code> is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for <code>Sys.came</code>, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns.</p> | |
| 190 | +<div class="file"><span>Signals.raz</span></div><pre class="code"><code><span class="kw">module</span> Signals; | |
| 191 | +<span class="kw">import</span> Out, Sys; | |
| 192 | + | |
| 193 | +<span class="kw">proc</span> main*; | |
| 194 | +<span class="kw">var</span> fd: <span class="ty">INT</span>; | |
| 195 | +<span class="kw">begin</span> | |
| 196 | + fd := Sys.catch([Sys.sigUsr1]); | |
| 197 | + _ := Sys.kill(Sys.pid(), Sys.sigUsr1); | |
| 198 | + <span class="kw">if</span> Sys.ready(fd, <span class="number">1000</span>) <span class="kw">and</span> (Sys.caught(fd) = Sys.sigUsr1) <span class="kw">then</span> Out.str(<span class="string">"caught sigUsr1\n"</span>); <span class="kw">end</span>; | |
| 199 | + _ := Sys.handle(Sys.sigUsr2); | |
| 200 | + _ := Sys.kill(Sys.pid(), Sys.sigUsr2); | |
| 201 | + <span class="kw">if</span> Sys.came(Sys.sigUsr2) <span class="kw">then</span> Out.str(<span class="string">"sigUsr2 came\n"</span>); <span class="kw">end</span>; | |
| 202 | + <span class="kw">if</span> <span class="kw">not</span> Sys.came(Sys.sigUsr2) <span class="kw">then</span> Out.str(<span class="string">"and was seen once\n"</span>); <span class="kw">end</span>; | |
| 203 | +<span class="kw">end</span>; | |
| 204 | + | |
| 205 | +<span class="kw">end</span> Signals.</code></pre> | |
| 206 | +<pre class="output"><code>caught sigUsr1 | |
| 207 | +sigUsr2 came | |
| 208 | +and was seen once | |
| 187 | 209 | </code></pre><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="files.html">← Files and the command line</a><a href="index.html">Contents</a><a class="next" href="allocators.html">Writing an allocator →</a></div></article></div> |
| 188 | 210 | </main> |
| 189 | 211 | <footer>raz is under the ISC license.</footer> |
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | |
| 115 | 115 | <span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>; |
| 116 | 116 | |
| 117 | -<span class="kw">const</span> Inner: [<span class="number">4</span>]Planet = [ | |
| 117 | +<span class="kw">const</span> Inner: []Planet = [ | |
| 118 | 118 | {name: <span class="string">"Mercury"</span>}, |
| 119 | 119 | {name: <span class="string">"Venus"</span>}, |
| 120 | 120 | {name: <span class="string">"Earth"</span>, moons: <span class="number">1</span>, water: <span class="ty">true</span>}, |
| @@ -135,7 +135,7 @@ | ||
| 135 | 135 | Venus has no moon |
| 136 | 136 | 3 moons in all |
| 137 | 137 | </code></pre> |
| 138 | -<p>Fields left out are zero, as in any literal: Mercury's <code>moons</code> is 0 and its <code>water</code> is <code>false</code>.</p> | |
| 138 | +<p>Fields left out are zero, as in any literal: Mercury's <code>moons</code> is 0 and its <code>water</code> is <code>false</code>. A constant typed <code>[]Planet</code> takes its length from its literal, here 4, so a planet added to the list needs no count changed.</p> | |
| 139 | 139 | <h2>Layout</h2> |
| 140 | 140 | <p>A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. <code>SIZE(T)</code> says how many bytes. A <code>packed record</code> has no padding, which is what a file format or a wire protocol wants.</p> |
| 141 | 141 | <div class="file"><span>Layout.raz</span></div><pre class="code"><code><span class="kw">module</span> Layout; |