| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Files and the command line - More Raz</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Files and the command line - More Raz"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | <li><a href="collections.html">Lists, arrays, maps and sorting</a></li> |
| 25 | <li><a href="files.html" aria-current="page">Files and the command line</a></li> |
| 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| 28 | </ol> |
| 29 | <h3>Standing alone</h3><ol start='7'> |
| 30 | <li><a href="static.html">A program with no run time</a></li> |
| 31 | <li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li> |
| 32 | <li><a href="lifecycle.html">Starting, ending, and signals</a></li> |
| 33 | <li><a href="targets.html">One module, one file per target</a></li> |
| 34 | </ol> |
| 35 | <h3>The machine</h3><ol start='11'> |
| 36 | <li><a href="asm.html">Procedures in assembly</a></li> |
| 37 | <li><a href="syscalls.html">System calls</a></li> |
| 38 | <li><a href="listing.html">What the compiler makes</a></li> |
| 39 | </ol> |
| 40 | <h3>Shared memory</h3><ol start='14'> |
| 41 | <li><a href="volatile.html">Volatile</a></li> |
| 42 | <li><a href="atomics.html">Atomics</a></li> |
| 43 | <li><a href="ordering.html">Ordering and publication</a></li> |
| 44 | <li><a href="lockfree.html">A lock-free stack</a></li> |
| 45 | <li><a href="ring.html">A ring buffer between threads</a></li> |
| 46 | </ol></aside><article><p class="kicker">The library · lesson 4 of 18</p><h1>Files and the command line</h1> |
| 47 | <p><code>Files</code> is for the common case: a whole file read or written in one call. <code>Sys</code> is the system underneath, for everything else. <code>Opt</code> reads a command line from a table, so that the help and the error messages can't drift from the code.</p> |
| 48 | <h2>A whole file</h2> |
| 49 | <p><code>Files.write(path, data)</code> makes the file hold <code>data</code>, and <code>Files.read(path, b)</code> adds the whole file to a <code>Buf</code>. Both say whether they worked.</p> |
| 50 | <div class="file"><span>Notes.raz</span></div><pre class="code"><code><span class="kw">module</span> Notes; |
| 51 | <span class="kw">import</span> Out, Files, Buf, Heap; |
| 52 | |
| 53 | <span class="kw">proc</span> main*; |
| 54 | <span class="kw">var</span> b: Buf.Buf; ok: <span class="ty">BOOL</span>; |
| 55 | <span class="kw">begin</span> |
| 56 | ok := Files.write(<span class="string">"notes.txt"</span>, <span class="string">"first line\nsecond line\n"</span>); |
| 57 | <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">"can't write\n"</span>); <span class="kw">return</span>; <span class="kw">end</span>; |
| 58 | Buf.init(b, Heap.heap); |
| 59 | <span class="kw">defer</span> Buf.done(b); |
| 60 | ok := Files.read(<span class="string">"notes.txt"</span>, b); |
| 61 | <span class="kw">if</span> ok <span class="kw">then</span> |
| 62 | Out.int(b.len); Out.str(<span class="string">" bytes: "</span>); Out.str(Buf.bytes(b)); |
| 63 | <span class="kw">end</span>; |
| 64 | ok := Files.read(<span class="string">"missing.txt"</span>, b); |
| 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>, 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>; |
| 67 | <span class="kw">end</span>; |
| 68 | |
| 69 | <span class="kw">end</span> Notes.</code></pre> |
| 70 | <pre class="output"><code>23 bytes: first line |
| 71 | second line |
| 72 | missing.txt: not there |
| 73 | missing.txt: ENOENT |
| 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> |
| 76 | <h2>Descriptors</h2> |
| 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> |
| 78 | <div class="file"><span>Count.raz</span></div><pre class="code"><code><span class="kw">module</span> Count; |
| 79 | <span class="kw">import</span> Out, Sys; |
| 80 | |
| 81 | <span class="comment">(* the lines of the file at path, or minus an errno *)</span> |
| 82 | <span class="kw">proc</span> lines(path: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>; |
| 83 | <span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>; |
| 84 | <span class="kw">begin</span> |
| 85 | fd := Sys.openRead(path); |
| 86 | <span class="kw">if</span> fd < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> fd; <span class="kw">end</span>; |
| 87 | <span class="kw">defer</span> Sys.close(fd); |
| 88 | n := Sys.read(fd, buf); |
| 89 | <span class="kw">while</span> n > <span class="number">0</span> <span class="kw">do</span> |
| 90 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> n - <span class="number">1</span> <span class="kw">do</span> |
| 91 | <span class="kw">if</span> buf[i] = <span class="string">'\n'</span> <span class="kw">then</span> <span class="ty">INC</span>(count); <span class="kw">end</span>; |
| 92 | <span class="kw">end</span>; |
| 93 | n := Sys.read(fd, buf); |
| 94 | <span class="kw">end</span>; |
| 95 | <span class="kw">if</span> n < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> n; <span class="kw">end</span>; |
| 96 | <span class="kw">return</span> count; |
| 97 | <span class="kw">end</span>; |
| 98 | |
| 99 | <span class="kw">proc</span> main*; |
| 100 | <span class="kw">var</span> fd: <span class="ty">INT</span>; pos: <span class="ty">I64</span>; |
| 101 | <span class="kw">begin</span> |
| 102 | fd := Sys.create(<span class="string">"log.txt"</span>); |
| 103 | _ := Sys.write(fd, <span class="string">"a\nb\nc\n"</span>); |
| 104 | pos := Sys.seek(fd, <span class="number">2</span>, <span class="ty">false</span>); <span class="comment">(* from the start *)</span> |
| 105 | Out.int(pos); Out.ln; |
| 106 | Sys.close(fd); |
| 107 | Out.int(lines(<span class="string">"log.txt"</span>)); Out.str(<span class="string">" lines\n"</span>); |
| 108 | Out.int(lines(<span class="string">"/no/such/file"</span>)); Out.ln; <span class="comment">(* -2: ENOENT *)</span> |
| 109 | fd := Sys.open(<span class="string">"log.txt"</span>, {Sys.writing, Sys.appending}); |
| 110 | _ := Sys.write(fd, <span class="string">"d\n"</span>); |
| 111 | Sys.close(fd); |
| 112 | Out.int(lines(<span class="string">"log.txt"</span>)); Out.str(<span class="string">" lines now\n"</span>); |
| 113 | <span class="kw">end</span>; |
| 114 | |
| 115 | <span class="kw">end</span> Count.</code></pre> |
| 116 | <pre class="output"><code>2 |
| 117 | 3 lines |
| 118 | -2 |
| 119 | 4 lines now |
| 120 | </code></pre> |
| 121 | <h2>Looking at a file, and a directory</h2> |
| 122 | <p><code>Sys.info</code> says a file's size, modification time, permissions, and whether it is a directory, a regular file or a link. <code>Sys.openDir</code> and <code>nextEntry</code> list a directory, in no particular order, so a program that prints one sorts it first. A name from <code>nextEntry</code> is good only until the next call, so the program keeps a copy.</p> |
| 123 | <div class="file"><span>Tree.raz</span></div><pre class="code"><code><span class="kw">module</span> Tree; |
| 124 | <span class="kw">import</span> Out, Sys, Str, Heap, Mem, Buf, Order := Sort([]<span class="ty">U8</span>, before); |
| 125 | <span class="kw">import</span> SYSTEM; |
| 126 | |
| 127 | <span class="kw">proc</span> before(a, b: []<span class="ty">U8</span>): <span class="ty">BOOL</span>; |
| 128 | <span class="kw">begin</span> |
| 129 | <span class="kw">return</span> Str.cmp(a, b) < <span class="number">0</span>; |
| 130 | <span class="kw">end</span>; |
| 131 | |
| 132 | <span class="comment">(* a file made to hold text *)</span> |
| 133 | <span class="kw">proc</span> put(path, text: []<span class="kw">const</span> <span class="ty">U8</span>); |
| 134 | <span class="kw">var</span> fd: <span class="ty">INT</span>; |
| 135 | <span class="kw">begin</span> |
| 136 | fd := Sys.create(path); |
| 137 | _ := Sys.write(fd, text); |
| 138 | Sys.close(fd); |
| 139 | <span class="kw">end</span>; |
| 140 | |
| 141 | <span class="kw">proc</span> main*; |
| 142 | <span class="kw">var</span> |
| 143 | d: Sys.Dir; name: []<span class="kw">const</span> <span class="ty">U8</span>; copy: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; i, n: <span class="ty">INT</span>; |
| 144 | info: Sys.Info; err: <span class="ty">INT</span>; names: [<span class="number">16</span>][]<span class="ty">U8</span>; path: Buf.Buf; |
| 145 | <span class="kw">begin</span> |
| 146 | _ := Sys.makeDir(<span class="string">"proj"</span>); |
| 147 | _ := Sys.makeDir(<span class="string">"proj/src"</span>); |
| 148 | put(<span class="string">"proj/b.txt"</span>, <span class="string">"bb"</span>); |
| 149 | put(<span class="string">"proj/a.txt"</span>, <span class="string">"a"</span>); |
| 150 | <span class="kw">if</span> Sys.openDir(d, <span class="string">"proj"</span>) < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 151 | n := <span class="number">0</span>; |
| 152 | name, more := Sys.nextEntry(d); |
| 153 | <span class="kw">while</span> more <span class="kw">do</span> |
| 154 | copy, ok := Str.copy(Heap.heap, name); |
| 155 | <span class="kw">if</span> ok <span class="kw">and</span> (n < <span class="number">16</span>) <span class="kw">then</span> names[n] := copy; <span class="ty">INC</span>(n); <span class="kw">end</span>; |
| 156 | name, more := Sys.nextEntry(d); |
| 157 | <span class="kw">end</span>; |
| 158 | Sys.closeDir(d); |
| 159 | Order.sort(names[<span class="number">0</span>..n]); |
| 160 | Buf.init(path, Heap.heap); |
| 161 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> n - <span class="number">1</span> <span class="kw">do</span> |
| 162 | Buf.reset(path); |
| 163 | Buf.str(path, <span class="string">"proj/"</span>); |
| 164 | Buf.str(path, names[i]); |
| 165 | info, err := Sys.info(Buf.bytes(path)); |
| 166 | Out.str(names[i]); |
| 167 | <span class="kw">if</span> err < <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">" ?"</span>); |
| 168 | <span class="kw">elsif</span> info.dir <span class="kw">then</span> Out.str(<span class="string">"/"</span>); |
| 169 | <span class="kw">else</span> Out.str(<span class="string">", "</span>); Out.int(info.size); Out.str(<span class="string">" bytes"</span>); |
| 170 | <span class="kw">end</span>; |
| 171 | Out.ln; |
| 172 | Mem.free(Heap.heap, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(names[i][<span class="number">0</span>]))); |
| 173 | <span class="kw">end</span>; |
| 174 | Buf.done(path); |
| 175 | <span class="kw">end</span>; |
| 176 | |
| 177 | <span class="kw">end</span> Tree.</code></pre> |
| 178 | <pre class="output"><code>a.txt, 1 bytes |
| 179 | b.txt, 2 bytes |
| 180 | src/ |
| 181 | </code></pre> |
| 182 | <h2>Modes, links, and a lock</h2> |
| 183 | <p><code>Sys.openMode</code> is <code>open</code> with the permissions a file it makes should have, less those the umask takes away: 1EDH (755) for a script, 124H (444) for a file never to be written. <code>Sys.linkInfo</code> looks at a symbolic link itself, where <code>info</code> follows it, so a link whose target is gone is still seen. <code>Info.ino</code> is the file's inode: a file renamed over another has a new one, though its size and time may match, which is how a program knows a file it cached is still the same.</p> |
| 184 | <p><code>Sys.lock</code> takes the lock on an open file, which one open file holds at a time; another that asks for it waits, or with <code>wait</code> false gets -11 at once. The lock goes when the file is closed or the program ends, however it ends, so it can't go stale as a lock file can. <code>Sys.syncAll</code> puts everything written to a file system on the disk at once, before the one file that names it all is written: a database's or a version control system's commit.</p> |
| 185 | <div class="file"><span>Store.raz</span></div><pre class="code"><code><span class="kw">module</span> Store; |
| 186 | <span class="kw">import</span> Fmt, Sys; |
| 187 | |
| 188 | <span class="kw">proc</span> main*; |
| 189 | <span class="kw">var</span> fd, other: <span class="ty">INT</span>; named, target: Sys.Info; |
| 190 | <span class="kw">begin</span> |
| 191 | fd := Sys.openMode(<span class="string">"run.sh"</span>, {Sys.writing, Sys.creating, Sys.truncating}, <span class="number">1EDH</span>); |
| 192 | _ := Sys.write(fd, <span class="string">"echo hi\n"</span>); |
| 193 | _ := Sys.syncAll(fd); |
| 194 | Sys.close(fd); |
| 195 | target, _ := Sys.info(<span class="string">"run.sh"</span>); |
| 196 | Fmt.print(<span class="string">"its owner may run it: {}\n"</span>, [Fmt.bool(target.mode & <span class="number">40H</span> # <span class="number">0</span>)]); |
| 197 | |
| 198 | _ := Sys.remove(<span class="string">"current"</span>); |
| 199 | _ := Sys.symlink(<span class="string">"run.sh"</span>, <span class="string">"current"</span>); |
| 200 | named, _ := Sys.linkInfo(<span class="string">"current"</span>); |
| 201 | Fmt.print(<span class="string">"a link: {}, of {} bytes\n"</span>, [Fmt.bool(named.symlink), Fmt.int(named.size)]); |
| 202 | named, _ := Sys.info(<span class="string">"current"</span>); |
| 203 | Fmt.print(<span class="string">"the same file: {}\n"</span>, [Fmt.bool(named.ino = target.ino)]); |
| 204 | |
| 205 | fd := Sys.openRead(<span class="string">"run.sh"</span>); |
| 206 | other := Sys.openRead(<span class="string">"run.sh"</span>); |
| 207 | Fmt.print(<span class="string">"{} {}\n"</span>, [Fmt.int(Sys.lock(fd, <span class="ty">false</span>)), Fmt.int(Sys.lock(other, <span class="ty">false</span>))]); <span class="comment">(* -11: held *)</span> |
| 208 | Sys.close(fd); |
| 209 | Fmt.print(<span class="string">"{}\n"</span>, [Fmt.int(Sys.lock(other, <span class="ty">false</span>))]); <span class="comment">(* free again *)</span> |
| 210 | Sys.close(other); |
| 211 | <span class="kw">end</span>; |
| 212 | |
| 213 | <span class="kw">end</span> Store.</code></pre> |
| 214 | <pre class="output"><code>its owner may run it: true |
| 215 | a link: true, of 6 bytes |
| 216 | the same file: true |
| 217 | 0 -11 |
| 218 | 0 |
| 219 | </code></pre> |
| 220 | <h2>A command line, from a table</h2> |
| 221 | <p>A program describes its options once, in a table of <code>Opt.Row</code>s: its synopsis lines, its subcommands and its options. The help and the error messages are made from the table. The parser reads by the rules of the C library's <code>getopt</code> with GNU's long options, so <code>-vn3</code>, <code>-n 3</code>, <code>--count=3</code> and <code>--count 3</code> all work, and <code>--</code> ends the options.</p> |
| 222 | <div class="file"><span>Tool.raz</span></div><pre class="code"><code><span class="kw">module</span> Tool; |
| 223 | <span class="kw">import</span> Out, Fmt, Opt; |
| 224 | |
| 225 | <span class="kw">const</span> count = <span class="number">1</span>; <span class="comment">(* the key of --count, which has no letter *)</span> |
| 226 | |
| 227 | <span class="kw">proc</span> main*; |
| 228 | <span class="kw">var</span> p: Opt.Parser; verbose: <span class="ty">BOOL</span>; n, i: <span class="ty">INT</span>; w: []<span class="kw">const</span> <span class="ty">U8</span>; ok: <span class="ty">BOOL</span>; |
| 229 | <span class="kw">begin</span> |
| 230 | n := <span class="number">1</span>; |
| 231 | Opt.init(p, <span class="string">"tool"</span>, [ |
| 232 | Opt.usage(<span class="string">""</span>, <span class="string">"tool [-v] [--count n] word ..."</span>), |
| 233 | Opt.option(<span class="string">""</span>, <span class="string">'v'</span>, <span class="string">"verbose"</span>, <span class="string">""</span>, <span class="string">"say more"</span>), |
| 234 | Opt.option(<span class="string">""</span>, count, <span class="string">"count"</span>, <span class="string">"n"</span>, <span class="string">"repeat each word n times"</span>)]); |
| 235 | <span class="kw">while</span> Opt.next(p) <span class="kw">do</span> |
| 236 | <span class="kw">if</span> p.letter = <span class="string">'v'</span> <span class="kw">then</span> verbose := <span class="ty">true</span>; |
| 237 | <span class="kw">elsif</span> p.letter = count <span class="kw">then</span> n := <span class="ty">INT</span>(Opt.number(p, <span class="number">1</span>, <span class="number">5</span>)); |
| 238 | <span class="kw">end</span>; |
| 239 | <span class="kw">end</span>; |
| 240 | w, ok := Opt.operand(p); |
| 241 | <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Opt.fail(p, <span class="string">"no words; see tool -h"</span>); <span class="kw">end</span>; |
| 242 | <span class="kw">while</span> ok <span class="kw">do</span> |
| 243 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> n <span class="kw">do</span> |
| 244 | <span class="kw">if</span> verbose <span class="kw">then</span> Out.str(<span class="string">"word: "</span>); <span class="kw">end</span>; |
| 245 | Fmt.print(<span class="string">"{}\n"</span>, [Fmt.str(w)]); |
| 246 | <span class="kw">end</span>; |
| 247 | w, ok := Opt.operand(p); |
| 248 | <span class="kw">end</span>; |
| 249 | <span class="kw">end</span>; |
| 250 | |
| 251 | <span class="kw">end</span> Tool.</code></pre> |
| 252 | <pre class="output term"><code><span class="cmd">$ raz Tool.raz</span> |
| 253 | <span class="cmd">$ ./Tool -h</span> |
| 254 | usage: tool [-v] [--count n] word ... |
| 255 | |
| 256 | -v, --verbose say more |
| 257 | --count n repeat each word n times |
| 258 | -h, --help print this and exit |
| 259 | <span class="cmd">$ ./Tool -v hello world</span> |
| 260 | word: hello |
| 261 | word: world |
| 262 | <span class="cmd">$ ./Tool --count 9 hello; echo "status $?"</span> |
| 263 | tool: --count wants a number from 1 to 5, not '9' |
| 264 | usage: tool [-v] [--count n] word ... |
| 265 | status 2 |
| 266 | <span class="cmd">$ ./Tool -x; echo "status $?"</span> |
| 267 | tool: unknown option -x |
| 268 | usage: tool [-v] [--count n] word ... |
| 269 | status 2 |
| 270 | <span class="cmd">$ ./Tool; echo "status $?"</span> |
| 271 | tool: no words; see tool -h |
| 272 | status 2</code></pre> |
| 273 | <p><code>-h</code> and <code>--help</code> are <code>Opt</code>'s: the program never sees them. A mistake on the command line is a sentence on standard error with the program's name first, then the synopsis, and status 2. Nothing in <code>Tool</code> wrote any of that.</p> |
| 274 | <p>A mistake in the table itself, such as an option given twice, is the program's, and <code>init</code> says so and ends with status 1 before it reads a word.</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="collections.html">← Lists, arrays, maps and sorting</a><a href="index.html">Contents</a><a class="next" href="processes.html">Time, randomness and other programs →</a></div></article></div> |
| 275 | </main> |
| 276 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 277 | </body> |
| 278 | </html> |