omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / trace / tracecli.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note seed/trace/tracecli.om: offline, read-only trace segment CLI (omega-mv #610), interface version 1. |
| 3 | note Build: cat seed/trace/segment.om seed/trace/tsha.om seed/trace/tracecli.om | seed/out/omc > tracecli |
| 4 | note Gate: sh seed/trace/cli-gate.sh. It scans the exact return line of fn ropen; the flags literal must stay 32768. |
| 5 | note tracecli version |
| 6 | note tracecli verify SEG integrity of one segment |
| 7 | note tracecli show SEG every valid record |
| 8 | note tracecli events SEG [--type T] [--range FROM TO] valid records of type T and FROM <= seq < TO |
| 9 | note tracecli stats SEG [--type T] [--range FROM TO] record count and payload bytes per type |
| 10 | note tracecli export SEG [--type T] [--range FROM TO] CSV with the source SHA-256 and the query |
| 11 | note tracecli compare SEGA SEGB record-by-record comparison |
| 12 | note tracecli list DIR verify every entry of a directory, by name |
| 13 | note Nothing is written except stdout. Segments are opened O_RDONLY|O_NOFOLLOW (32768) and directories |
| 14 | note O_RDONLY|O_DIRECTORY|O_NOFOLLOW (49152); nothing is created, so a symbolic link is refused (ELOOP). |
| 15 | note A damaged segment is never silently shortened: the records before the damage are printed, the verify |
| 16 | note line says "partial" with the reason and the bytes after the valid prefix, and the exit status is 3. |
| 17 | note Every record printed is read again and re-checked (CRC, length, sequence) after the scan; a record |
| 18 | note that no longer checks ends the listing with "changed-during-read at=N" and exit 3. |
| 19 | note Exit status: 0 ok (compare: identical), 1 compare found a difference, 2 usage, 3 partial or |
| 20 | note inconclusive, 4 refused (missing, symlink, bad header, unreadable file or directory), 5 stdout write failed. |
| 21 | global obuf uchar 512 |
| 22 | global on ulong 1 |
| 23 | global wbad ulong 1 |
| 24 | global arec uchar 64 |
| 25 | global argb uchar 64 |
| 26 | global acnt ulong 1 |
| 27 | global moff ulong 96 |
| 28 | global mlen ulong 96 |
| 29 | global qtype ulong 1 |
| 30 | global qlo ulong 1 |
| 31 | global qhi ulong 1 |
| 32 | global wfail ulong 1 |
| 33 | global tcnt ulong 65536 |
| 34 | global tbytes ulong 65536 |
| 35 | global dpath uchar 1024 |
| 36 | global dlen ulong 1 |
| 37 | global dbuf uchar 4096 |
| 38 | global names uchar 16384 |
| 39 | global nlens ulong 64 |
| 40 | global ntype ulong 64 |
| 41 | global nnum ulong 1 |
| 42 | global ntrunc ulong 1 |
| 43 | global nord ulong 64 |
| 44 | note msgs: one message per line; message k is printed by omsg k. Lines 52..57 are the scan reasons |
| 45 | note (sreason 0..5), lines 58..67 the command and option words. |
| 46 | text msgs "usage: tracecli version | verify SEG | show SEG | events SEG [--type T] [--range FROM TO] | stats SEG [--type T] [--range FROM TO] | export SEG [--type T] [--range FROM TO] | compare SEGA SEGB | list DIR\ntracecli 1 segment-format 1\nsegment version=1 recsize=64 hdrsize=16\nverify \nok\npartial\n records=\n bytes=\n lost=\n reason=\nrefused code=\n errno=\nseq=\n type=\n len=\n crc=\n payload=\nquery type=\nany\n seq>=\n seq<\nmatched=\n of=\nchanged-during-read at=\ntype=\n payload-bytes=\nstats records=\n types=\nenergy=unavailable timing=unavailable (segment format 1 carries no typed measurements)\na: \nb: \ncompare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)\ncompare identical records=\ncompare differ at=\ncompare prefix a=\n b=\ncompare inconclusive\n# tracecli 1 export\n# source sha256=\n# \n# \nseq,type,len,crc,payload_hex\nlist \n: \ndirectory\nlist entries=\n ok=\n partial=\n refused=\n directories=\nlist truncated at 64 entries\nlist refused errno=\nclean\ntorn-tail\nbad-length\ncrc-mismatch\nseq-mismatch\nover-capacity\nversion\nverify\nshow\nevents\nstats\nexport\ncompare\nlist\n--type\n--range\n" |
| 47 | text hexd "0123456789abcdef" |
| 48 | const kusage ulong 0 |
| 49 | const kvers ulong 1 |
| 50 | const khdr ulong 2 |
| 51 | const kverify ulong 3 |
| 52 | const kok ulong 4 |
| 53 | const kpart ulong 5 |
| 54 | const krecs ulong 6 |
| 55 | const kbytes ulong 7 |
| 56 | const klost ulong 8 |
| 57 | const kreason ulong 9 |
| 58 | const krefused ulong 10 |
| 59 | const kerrno ulong 11 |
| 60 | const kseq ulong 12 |
| 61 | const ktype ulong 13 |
| 62 | const klen ulong 14 |
| 63 | const kcrc ulong 15 |
| 64 | const kpay ulong 16 |
| 65 | const kquery ulong 17 |
| 66 | const kany ulong 18 |
| 67 | const kseqge ulong 19 |
| 68 | const kseqlt ulong 20 |
| 69 | const kmatched ulong 21 |
| 70 | const kof ulong 22 |
| 71 | const kchanged ulong 23 |
| 72 | const ktypeeq ulong 24 |
| 73 | const kpbytes ulong 25 |
| 74 | const kstats ulong 26 |
| 75 | const ktypes ulong 27 |
| 76 | const kenergy ulong 28 |
| 77 | const ka ulong 29 |
| 78 | const kb ulong 30 |
| 79 | const kbasis ulong 31 |
| 80 | const kident ulong 32 |
| 81 | const kdiffer ulong 33 |
| 82 | const kprefix ulong 34 |
| 83 | const kbn ulong 35 |
| 84 | const kinconc ulong 36 |
| 85 | const kexport ulong 37 |
| 86 | const ksrc ulong 38 |
| 87 | const khash ulong 39 |
| 88 | const kcsvhdr ulong 41 |
| 89 | const klist ulong 42 |
| 90 | const kcolon ulong 43 |
| 91 | const kdir ulong 44 |
| 92 | const kentries ulong 45 |
| 93 | const knok ulong 46 |
| 94 | const knpart ulong 47 |
| 95 | const knref ulong 48 |
| 96 | const kndirs ulong 49 |
| 97 | const ktrunc ulong 50 |
| 98 | const klistref ulong 51 |
| 99 | const kreasons ulong 52 |
| 100 | const wversion ulong 58 |
| 101 | const wverify ulong 59 |
| 102 | const wshow ulong 60 |
| 103 | const wevents ulong 61 |
| 104 | const wstats ulong 62 |
| 105 | const wexport ulong 63 |
| 106 | const wcompare ulong 64 |
| 107 | const wlist ulong 65 |
| 108 | const wtype ulong 66 |
| 109 | const wrange ulong 67 |
| 110 | fn oflush to void |
| 111 | w = write 1 obuf 0 get on 0 |
| 112 | if ne w long get on 0 |
| 113 | put wbad 0 1 |
| 114 | end |
| 115 | put on 0 0 |
| 116 | end |
| 117 | fn obyte b uchar to void |
| 118 | if ge get on 0 512 |
| 119 | oflush |
| 120 | end |
| 121 | put obuf get on 0 b |
| 122 | put on 0 add get on 0 1 |
| 123 | end |
| 124 | fn onl to void |
| 125 | obyte 10 |
| 126 | end |
| 127 | fn onum v ulong to void |
| 128 | d = new uchar 20 0 |
| 129 | x = v |
| 130 | k = ulong 0 |
| 131 | go = true |
| 132 | while go bound 21 |
| 133 | put d k uchar add 48 rem x 10 |
| 134 | k = add k 1 |
| 135 | x = div x 10 |
| 136 | if eq x 0 |
| 137 | go = false |
| 138 | end |
| 139 | end |
| 140 | while gt k 0 bound 21 |
| 141 | k = sub k 1 |
| 142 | obyte get d k |
| 143 | end |
| 144 | end |
| 145 | fn ohex b ulong to void |
| 146 | obyte get hexd and shr b 4 15 |
| 147 | obyte get hexd and b 15 |
| 148 | end |
| 149 | note minit fills moff/mlen with the start and length of every line of msgs (68 lines, 96 slots). |
| 150 | fn minit to void |
| 151 | s = ulong 0 |
| 152 | k = ulong 0 |
| 153 | for i ulong len msgs |
| 154 | if eq get msgs i 10 |
| 155 | put moff k s |
| 156 | put mlen k sub i s |
| 157 | k = add k 1 |
| 158 | s = add i 1 |
| 159 | end |
| 160 | end |
| 161 | end |
| 162 | fn omsg k ulong to void |
| 163 | o = get moff k |
| 164 | for i ulong get mlen k |
| 165 | obyte get msgs add o i |
| 166 | end |
| 167 | end |
| 168 | note omnum k v: message k then the decimal v. |
| 169 | fn omnum k ulong v ulong to void |
| 170 | omsg k |
| 171 | onum v |
| 172 | end |
| 173 | note argword i: argument i into argb[0..acnt); acnt = 0 when it does not fit. |
| 174 | fn argword i ulong to void |
| 175 | put acnt 0 0 |
| 176 | n = argread i argb 0 64 |
| 177 | if ge n 0 |
| 178 | put acnt 0 ulong n |
| 179 | end |
| 180 | end |
| 181 | note isword k: argb[0..acnt) equals message line k. |
| 182 | fn isword k ulong to bool |
| 183 | if ne get acnt 0 get mlen k |
| 184 | ret false |
| 185 | end |
| 186 | o = get moff k |
| 187 | for i ulong get mlen k |
| 188 | if ne get argb i get msgs add o i |
| 189 | ret false |
| 190 | end |
| 191 | end |
| 192 | ret true |
| 193 | end |
| 194 | note argpath i: argument i becomes the segment path (tpath, tlen); 0 on success, 1 when it is empty |
| 195 | note or longer than 1024 bytes. |
| 196 | fn argpath i ulong to ulong |
| 197 | n = argread i tpath 0 1024 |
| 198 | if le n 0 |
| 199 | ret 1 |
| 200 | end |
| 201 | put tlen 0 ulong n |
| 202 | ret 0 |
| 203 | end |
| 204 | note argnum i: decimal argument i (1 to 5 digits, value at most 65535); 65536 when malformed. |
| 205 | fn argnum i ulong to ulong |
| 206 | argword i |
| 207 | n = get acnt 0 |
| 208 | if or eq n 0 gt n 5 |
| 209 | ret 65536 |
| 210 | end |
| 211 | v = ulong 0 |
| 212 | for k ulong n |
| 213 | b = get argb k |
| 214 | if or lt b 48 gt b 57 |
| 215 | ret 65536 |
| 216 | end |
| 217 | v = add mul v 10 sub ulong b 48 |
| 218 | end |
| 219 | if gt v 65535 |
| 220 | ret 65536 |
| 221 | end |
| 222 | ret v |
| 223 | end |
| 224 | note parseq c: options from argument 3 to c-1 into qtype (65536 = any), qlo, qhi (seq range |
| 225 | note qlo <= seq < qhi; default 0..4096, the whole segment). 0 ok, 1 usage error. A repeated option |
| 226 | note replaces the earlier one. |
| 227 | fn parseq c ulong to ulong |
| 228 | put qtype 0 65536 |
| 229 | put qlo 0 0 |
| 230 | put qhi 0 4096 |
| 231 | i = ulong 3 |
| 232 | while lt i c bound 64 |
| 233 | argword i |
| 234 | if isword wtype |
| 235 | i = qopt i c 1 |
| 236 | else |
| 237 | if isword wrange |
| 238 | i = qopt i c 2 |
| 239 | else |
| 240 | ret 1 |
| 241 | end |
| 242 | end |
| 243 | if eq i 0 |
| 244 | ret 1 |
| 245 | end |
| 246 | end |
| 247 | ret 0 |
| 248 | end |
| 249 | note qopt i c k: the option at argument i takes k values (1 --type T, 2 --range FROM TO, FROM <= TO); |
| 250 | note returns the index of the next argument, 0 when the values are missing or malformed. |
| 251 | fn qopt i ulong c ulong k ulong to ulong |
| 252 | if ge add i k c |
| 253 | ret 0 |
| 254 | end |
| 255 | a = argnum add i 1 |
| 256 | if gt a 65535 |
| 257 | ret 0 |
| 258 | end |
| 259 | if eq k 1 |
| 260 | put qtype 0 a |
| 261 | ret add i 2 |
| 262 | end |
| 263 | b = argnum add i 2 |
| 264 | if or gt b 65535 gt a b |
| 265 | ret 0 |
| 266 | end |
| 267 | put qlo 0 a |
| 268 | put qhi 0 b |
| 269 | ret add i 3 |
| 270 | end |
| 271 | fn oquery to void |
| 272 | omsg kquery |
| 273 | if eq get qtype 0 65536 |
| 274 | omsg kany |
| 275 | else |
| 276 | onum get qtype 0 |
| 277 | end |
| 278 | omnum kseqge get qlo 0 |
| 279 | omnum kseqlt get qhi 0 |
| 280 | onl |
| 281 | end |
| 282 | note qmatch: the record in trec satisfies the query. |
| 283 | fn qmatch to bool |
| 284 | t = ld16 0 |
| 285 | s = ld32 4 |
| 286 | if and ne get qtype 0 65536 ne t get qtype 0 |
| 287 | ret false |
| 288 | end |
| 289 | ret and ge s get qlo 0 lt s get qhi 0 |
| 290 | end |
| 291 | fn ousage to void |
| 292 | put on 0 0 |
| 293 | omsg kusage |
| 294 | onl |
| 295 | end |
| 296 | note orefused e: the refusal for scan result e; serrno (-errno) is reported for open failures (10). |
| 297 | fn orefused e ulong to void |
| 298 | omnum krefused e |
| 299 | if eq e 10 |
| 300 | omnum kerrno ulong neg get serrno 0 |
| 301 | end |
| 302 | onl |
| 303 | end |
| 304 | note overify: the verify line for the last segscan. |
| 305 | fn overify to void |
| 306 | omsg kverify |
| 307 | if eq get sreason 0 0 |
| 308 | omsg kok |
| 309 | else |
| 310 | omsg kpart |
| 311 | end |
| 312 | omnum krecs get scount 0 |
| 313 | omnum kbytes get stotal 0 |
| 314 | omnum klost get slost 0 |
| 315 | omsg kreason |
| 316 | omsg add kreasons get sreason 0 |
| 317 | onl |
| 318 | end |
| 319 | note vstatus: exit 3 when the scan stopped on damage (sreason not 0), else 0. |
| 320 | fn vstatus to int |
| 321 | ret partial get sreason 0 |
| 322 | end |
| 323 | note partial r: exit 3 when r is not 0 (damage), else 0. The one place the partial exit status is chosen. |
| 324 | fn partial r ulong to int |
| 325 | if ne r 0 |
| 326 | ret 3 |
| 327 | end |
| 328 | ret 0 |
| 329 | end |
| 330 | fn orecord to void |
| 331 | omnum kseq ld32 4 |
| 332 | omnum ktype ld16 0 |
| 333 | n = ld16 2 |
| 334 | omnum klen n |
| 335 | omnum kcrc ld32 60 |
| 336 | omsg kpay |
| 337 | for i ulong n |
| 338 | ohex ulong get trec add 8 i |
| 339 | end |
| 340 | onl |
| 341 | end |
| 342 | fn ocsv to void |
| 343 | onum ld32 4 |
| 344 | obyte 44 |
| 345 | onum ld16 0 |
| 346 | obyte 44 |
| 347 | n = ld16 2 |
| 348 | onum n |
| 349 | obyte 44 |
| 350 | onum ld32 60 |
| 351 | obyte 44 |
| 352 | for i ulong n |
| 353 | ohex ulong get trec add 8 i |
| 354 | end |
| 355 | onl |
| 356 | end |
| 357 | note ropen: the one open of a segment path by this unit: O_RDONLY|O_NOFOLLOW, nothing created. cli-gate.sh scans this exact return line. |
| 358 | fn ropen to long |
| 359 | ret fopen tpath 0 get tlen 0 32768 0 |
| 360 | end |
| 361 | note rdrec fd i: read record i into trec and check it again (length, CRC, sequence = i). |
| 362 | fn rdrec fd long i ulong to bool |
| 363 | m = pread int fd trec 0 64 long add 16 mul i 64 |
| 364 | if ne m 64 |
| 365 | ret false |
| 366 | end |
| 367 | ret eq recok i 0 |
| 368 | end |
| 369 | note tally: stats accumulation for the record in trec. |
| 370 | fn tally to void |
| 371 | t = ld16 0 |
| 372 | put tcnt t add get tcnt t 1 |
| 373 | put tbytes t add get tbytes t ld16 2 |
| 374 | end |
| 375 | note walk m: the valid records (scount of them, after segscan) that match the query; m 0 prints |
| 376 | note record lines, 1 CSV rows, 2 tallies them. Returns the match count. wfail = index + 1 of the record |
| 377 | note whose re-read failed (0 none). At most 4096 valid records: bound 4097. |
| 378 | fn walk m ulong to ulong |
| 379 | put wfail 0 0 |
| 380 | fd = ropen |
| 381 | if lt fd 0 |
| 382 | put wfail 0 1 |
| 383 | ret 0 |
| 384 | end |
| 385 | hit = ulong 0 |
| 386 | n = get scount 0 |
| 387 | i = ulong 0 |
| 388 | while lt i n bound 4097 |
| 389 | if rdrec fd i |
| 390 | if qmatch |
| 391 | hit = add hit 1 |
| 392 | wone m |
| 393 | end |
| 394 | i = add i 1 |
| 395 | else |
| 396 | put wfail 0 add i 1 |
| 397 | i = n |
| 398 | end |
| 399 | end |
| 400 | shut fd |
| 401 | ret hit |
| 402 | end |
| 403 | fn wone m ulong to void |
| 404 | if eq m 0 |
| 405 | orecord |
| 406 | end |
| 407 | if eq m 1 |
| 408 | ocsv |
| 409 | end |
| 410 | if eq m 2 |
| 411 | tally |
| 412 | end |
| 413 | end |
| 414 | fn ochanged to void |
| 415 | if ne get wfail 0 0 |
| 416 | omnum kchanged sub get wfail 0 1 |
| 417 | onl |
| 418 | end |
| 419 | end |
| 420 | note seg i: argument i becomes the segment path and is scanned; 0 scanned (clean or partial), 4 |
| 421 | note refused (the refusal is printed), 2 usage. |
| 422 | fn seg i ulong to int |
| 423 | if ne argpath i 0 |
| 424 | ret 2 |
| 425 | end |
| 426 | e = segscan |
| 427 | if ne e 0 |
| 428 | orefused e |
| 429 | ret 4 |
| 430 | end |
| 431 | ret 0 |
| 432 | end |
| 433 | note endst: exit 3 when a re-read failed or the scan stopped on damage, else 0. |
| 434 | fn endst to int |
| 435 | if ne get wfail 0 0 |
| 436 | ret partial 1 |
| 437 | end |
| 438 | ret vstatus |
| 439 | end |
| 440 | fn cmdverify to int |
| 441 | e = seg 2 |
| 442 | if ne e 0 |
| 443 | ret e |
| 444 | end |
| 445 | overify |
| 446 | ret vstatus |
| 447 | end |
| 448 | note cmdrecs m c: show/events (m 0) and export (m 1) after parseq; header, query, records, totals. |
| 449 | fn cmdrecs m ulong to int |
| 450 | e = seg 2 |
| 451 | if ne e 0 |
| 452 | ret e |
| 453 | end |
| 454 | if eq m 1 |
| 455 | ret cmdexport |
| 456 | end |
| 457 | omsg khdr |
| 458 | onl |
| 459 | oquery |
| 460 | hit = walk 0 |
| 461 | omnum kmatched hit |
| 462 | omnum kof get scount 0 |
| 463 | onl |
| 464 | ochanged |
| 465 | overify |
| 466 | ret endst |
| 467 | end |
| 468 | fn cmdexport to int |
| 469 | omsg kexport |
| 470 | onl |
| 471 | if ne hashfile 0 |
| 472 | omnum kchanged 0 |
| 473 | onl |
| 474 | ret partial 1 |
| 475 | end |
| 476 | omsg khash |
| 477 | oquery |
| 478 | omsg khash |
| 479 | overify |
| 480 | omsg kcsvhdr |
| 481 | onl |
| 482 | hit = walk 1 |
| 483 | ochanged |
| 484 | ret endst |
| 485 | end |
| 486 | note hashfile: SHA-256 of the first stotal bytes of the segment (the bytes segscan saw), printed as |
| 487 | note "# source sha256=<hex> bytes=<n>". 0 ok, 1 when the file could not be read back. stotal is at |
| 488 | note most 16 + 64*4097 + 63 bytes before segscan stops (over-capacity), so at most 66 chunks of 4096: |
| 489 | note bound 70. |
| 490 | fn hashfile to ulong |
| 491 | st = new ulong 9 0 |
| 492 | blk = new uchar 4096 0 |
| 493 | dig = new uchar 32 0 |
| 494 | shainit st |
| 495 | fd = ropen |
| 496 | if lt fd 0 |
| 497 | ret 1 |
| 498 | end |
| 499 | tot = get stotal 0 |
| 500 | off = ulong 0 |
| 501 | bad = ulong 0 |
| 502 | go = true |
| 503 | while go bound 70 |
| 504 | want = sub tot off |
| 505 | if gt want 4096 |
| 506 | want = 4096 |
| 507 | end |
| 508 | m = pread int fd blk 0 want long off |
| 509 | if ne m long want |
| 510 | bad = 1 |
| 511 | go = false |
| 512 | else |
| 513 | off = add off want |
| 514 | if lt off tot |
| 515 | shaupd st blk want |
| 516 | else |
| 517 | shafin st blk want dig |
| 518 | go = false |
| 519 | end |
| 520 | end |
| 521 | end |
| 522 | shut fd |
| 523 | if ne bad 0 |
| 524 | ret 1 |
| 525 | end |
| 526 | omsg ksrc |
| 527 | for i ulong 32 |
| 528 | ohex ulong get dig i |
| 529 | end |
| 530 | omnum kbytes tot |
| 531 | onl |
| 532 | ret 0 |
| 533 | end |
| 534 | fn cmdstats to int |
| 535 | e = seg 2 |
| 536 | if ne e 0 |
| 537 | ret e |
| 538 | end |
| 539 | omsg khdr |
| 540 | onl |
| 541 | oquery |
| 542 | hit = walk 2 |
| 543 | nt = ulong 0 |
| 544 | pb = ulong 0 |
| 545 | for t ulong 65536 |
| 546 | if ne get tcnt t 0 |
| 547 | nt = add nt 1 |
| 548 | pb = add pb get tbytes t |
| 549 | omnum ktypeeq t |
| 550 | omnum krecs get tcnt t |
| 551 | omnum kpbytes get tbytes t |
| 552 | onl |
| 553 | end |
| 554 | end |
| 555 | omnum kstats hit |
| 556 | omnum ktypes nt |
| 557 | omnum kpbytes pb |
| 558 | onl |
| 559 | omsg kenergy |
| 560 | onl |
| 561 | ochanged |
| 562 | overify |
| 563 | ret endst |
| 564 | end |
| 565 | note cmpside k: scan argument k (2 = a, 3 = b) and print its prefixed verify line; 0 when the scan |
| 566 | note completed (clean or partial), else the refusal status. |
| 567 | fn cmpside k ulong to int |
| 568 | if eq k 2 |
| 569 | omsg ka |
| 570 | else |
| 571 | omsg kb |
| 572 | end |
| 573 | e = seg k |
| 574 | if eq e 2 |
| 575 | ret 2 |
| 576 | end |
| 577 | if ne e 0 |
| 578 | ret e |
| 579 | end |
| 580 | overify |
| 581 | ret 0 |
| 582 | end |
| 583 | note firstdiff fa fb n: index of the first of n records that differs between the two files, n if none, |
| 584 | note n + 1 when a record could not be read back and re-checked. |
| 585 | fn firstdiff fa long fb long n ulong to ulong |
| 586 | at = n |
| 587 | i = ulong 0 |
| 588 | while lt i n bound 4097 |
| 589 | if rdrec fa i |
| 590 | for j ulong 64 |
| 591 | put arec j get trec j |
| 592 | end |
| 593 | if rdrec fb i |
| 594 | if recdiff |
| 595 | at = i |
| 596 | i = n |
| 597 | else |
| 598 | i = add i 1 |
| 599 | end |
| 600 | else |
| 601 | at = add n 1 |
| 602 | i = n |
| 603 | end |
| 604 | else |
| 605 | at = add n 1 |
| 606 | i = n |
| 607 | end |
| 608 | end |
| 609 | ret at |
| 610 | end |
| 611 | note recdiff: the record in arec (side a) and trec (side b) differ in some byte. |
| 612 | fn recdiff to bool |
| 613 | for j ulong 64 |
| 614 | if ne get arec j get trec j |
| 615 | ret true |
| 616 | end |
| 617 | end |
| 618 | ret same |
| 619 | end |
| 620 | note same: recdiff's answer once all 64 bytes matched, so always false (no difference). It is a separate |
| 621 | note function only so that "ret same" is a unique compare-mutant site for cli-gate.sh (mutant: ret true). |
| 622 | fn same to bool |
| 623 | ret false |
| 624 | end |
| 625 | note opn i: open argument i read-only (O_RDONLY|O_NOFOLLOW). |
| 626 | fn opn i ulong to long |
| 627 | e = argpath i |
| 628 | if ne e 0 |
| 629 | ret -1 |
| 630 | end |
| 631 | ret ropen |
| 632 | end |
| 633 | fn cmdcompare to int |
| 634 | omsg kbasis |
| 635 | onl |
| 636 | e = cmpside 2 |
| 637 | pa = get sreason 0 |
| 638 | ca = get scount 0 |
| 639 | e2 = cmpside 3 |
| 640 | pb = get sreason 0 |
| 641 | cb = get scount 0 |
| 642 | if or eq e 2 eq e2 2 |
| 643 | ret 2 |
| 644 | end |
| 645 | if or ne e 0 ne e2 0 |
| 646 | omsg kinconc |
| 647 | onl |
| 648 | ret 4 |
| 649 | end |
| 650 | n = ca |
| 651 | if lt cb n |
| 652 | n = cb |
| 653 | end |
| 654 | fa = opn 2 |
| 655 | fb = opn 3 |
| 656 | at = add n 1 |
| 657 | if and ge fa 0 ge fb 0 |
| 658 | at = firstdiff fa fb n |
| 659 | end |
| 660 | if ge fa 0 |
| 661 | shut fa |
| 662 | end |
| 663 | if ge fb 0 |
| 664 | shut fb |
| 665 | end |
| 666 | ret cmpout pa ca pb cb at n |
| 667 | end |
| 668 | note cmpout: a differing record inside both valid prefixes is a definite difference (1); otherwise |
| 669 | note damage on either side makes the result inconclusive (3); then unequal lengths differ (1). |
| 670 | fn cmpout pa ulong ca ulong pb ulong cb ulong at ulong n ulong to int |
| 671 | if gt at n |
| 672 | omsg kinconc |
| 673 | onl |
| 674 | ret partial 1 |
| 675 | end |
| 676 | if lt at n |
| 677 | omnum kdiffer at |
| 678 | onl |
| 679 | ret 1 |
| 680 | end |
| 681 | if or ne pa 0 ne pb 0 |
| 682 | omsg kinconc |
| 683 | omnum krecs n |
| 684 | onl |
| 685 | ret partial 1 |
| 686 | end |
| 687 | if ne ca cb |
| 688 | omnum kprefix ca |
| 689 | omnum kbn cb |
| 690 | onl |
| 691 | ret 1 |
| 692 | end |
| 693 | omnum kident n |
| 694 | onl |
| 695 | ret 0 |
| 696 | end |
| 697 | note dname e: the 2-byte little-endian reclen at dbuf[e+16]; getdents64 record layout d_ino u64, |
| 698 | note d_off s64, d_reclen u16, d_type u8, d_name NUL-terminated. |
| 699 | fn dreclen e ulong to ulong |
| 700 | ret or ulong get dbuf add e 16 shl ulong get dbuf add e 17 8 |
| 701 | end |
| 702 | note dkeep e r: store the entry at dbuf[e..) (not "." or "..") as name number nnum; ntrunc = 1 when 64 |
| 703 | note names are already stored or the name is longer than 255 bytes (the listing is then inconclusive). |
| 704 | fn dkeep e ulong r ulong to void |
| 705 | o = add e 19 |
| 706 | n = dnlen o r |
| 707 | if dotname o n |
| 708 | ret |
| 709 | end |
| 710 | if or ge get nnum 0 64 gt n 255 |
| 711 | put ntrunc 0 1 |
| 712 | ret |
| 713 | end |
| 714 | k = get nnum 0 |
| 715 | dstore k o n |
| 716 | put ntype k ulong get dbuf add e 18 |
| 717 | put nord k k |
| 718 | put nnum 0 add k 1 |
| 719 | end |
| 720 | note dnlen o r: length of the NUL-terminated name at dbuf[o..r), at most 256 bytes are examined. |
| 721 | fn dnlen o ulong r ulong to ulong |
| 722 | n = ulong 0 |
| 723 | go = true |
| 724 | while go bound 257 |
| 725 | if or ge add o n r gt n 255 |
| 726 | go = false |
| 727 | else |
| 728 | if eq get dbuf add o n 0 |
| 729 | go = false |
| 730 | else |
| 731 | n = add n 1 |
| 732 | end |
| 733 | end |
| 734 | end |
| 735 | ret n |
| 736 | end |
| 737 | fn dstore k ulong o ulong n ulong to void |
| 738 | b = mul k 256 |
| 739 | for i ulong n |
| 740 | put names add b i get dbuf add o i |
| 741 | end |
| 742 | put nlens k n |
| 743 | end |
| 744 | fn dotname o ulong n ulong to bool |
| 745 | if eq n 0 |
| 746 | ret true |
| 747 | end |
| 748 | if ne get dbuf o 46 |
| 749 | ret false |
| 750 | end |
| 751 | if eq n 1 |
| 752 | ret true |
| 753 | end |
| 754 | if eq n 2 |
| 755 | ret eq get dbuf add o 1 46 |
| 756 | end |
| 757 | ret false |
| 758 | end |
| 759 | note dread fd: read every directory entry; 0 ok, else -errno. Every getdnts call that returns more than |
| 760 | note 0 bytes consumes at least one entry, so the bound 100000 traps (exit 104) only for a directory of |
| 761 | note more than 100000 entries. |
| 762 | fn dread fd long to long |
| 763 | go = true |
| 764 | while go bound 100000 |
| 765 | r = getdnts int fd dbuf 0 4096 |
| 766 | if lt r 0 |
| 767 | ret r |
| 768 | end |
| 769 | if eq r 0 |
| 770 | go = false |
| 771 | else |
| 772 | dparse ulong r |
| 773 | end |
| 774 | end |
| 775 | ret 0 |
| 776 | end |
| 777 | fn dparse r ulong to void |
| 778 | e = ulong 0 |
| 779 | while lt e r bound 4097 |
| 780 | rl = dreclen e |
| 781 | if eq rl 0 |
| 782 | e = r |
| 783 | else |
| 784 | dkeep e r |
| 785 | e = add e rl |
| 786 | end |
| 787 | end |
| 788 | end |
| 789 | note nless a b: name a sorts before name b (bytewise, a proper prefix first). |
| 790 | fn nless a ulong b ulong to bool |
| 791 | la = get nlens a |
| 792 | lb = get nlens b |
| 793 | m = la |
| 794 | if lt lb m |
| 795 | m = lb |
| 796 | end |
| 797 | for i ulong m |
| 798 | x = get names add mul a 256 i |
| 799 | y = get names add mul b 256 i |
| 800 | if ne x y |
| 801 | ret lt x y |
| 802 | end |
| 803 | end |
| 804 | ret lt la lb |
| 805 | end |
| 806 | note nsort: insertion sort of nord by name, at most 64 entries. |
| 807 | fn nsort to void |
| 808 | n = get nnum 0 |
| 809 | for i ulong n |
| 810 | j = i |
| 811 | while gt j 0 bound 64 |
| 812 | p = get nord sub j 1 |
| 813 | q = get nord j |
| 814 | if nless q p |
| 815 | put nord j p |
| 816 | put nord sub j 1 q |
| 817 | j = sub j 1 |
| 818 | else |
| 819 | j = 0 |
| 820 | end |
| 821 | end |
| 822 | end |
| 823 | end |
| 824 | note npath k: tpath = DIR "/" name k; 1 when it does not fit 1024 bytes. |
| 825 | fn npath k ulong to ulong |
| 826 | d = get dlen 0 |
| 827 | n = get nlens k |
| 828 | if gt add add d 1 n 1024 |
| 829 | ret 1 |
| 830 | end |
| 831 | for i ulong d |
| 832 | put tpath i get dpath i |
| 833 | end |
| 834 | put tpath d 47 |
| 835 | for i ulong n |
| 836 | put tpath add add d 1 i get names add mul k 256 i |
| 837 | end |
| 838 | put tlen 0 add add d 1 n |
| 839 | ret 0 |
| 840 | end |
| 841 | fn oname k ulong to void |
| 842 | for i ulong get nlens k |
| 843 | obyte get names add mul k 256 i |
| 844 | end |
| 845 | omsg kcolon |
| 846 | end |
| 847 | note lentry k: one line for name k; returns 0 ok, 1 partial, 2 refused, 3 directory. Spelled ret ulong 3 so the ret 3 in partial stays the unique mutant site. |
| 848 | fn lentry k ulong to ulong |
| 849 | oname k |
| 850 | if eq get ntype k 4 |
| 851 | omsg kdir |
| 852 | onl |
| 853 | ret ulong 3 |
| 854 | end |
| 855 | if ne npath k 0 |
| 856 | orefused 10 |
| 857 | ret 2 |
| 858 | end |
| 859 | e = segscan |
| 860 | if ne e 0 |
| 861 | orefused e |
| 862 | ret 2 |
| 863 | end |
| 864 | overify |
| 865 | if ne get sreason 0 0 |
| 866 | ret 1 |
| 867 | end |
| 868 | ret 0 |
| 869 | end |
| 870 | fn cmdlist to int |
| 871 | n = argread 2 dpath 0 1024 |
| 872 | if le n 0 |
| 873 | ret 2 |
| 874 | end |
| 875 | put dlen 0 ulong n |
| 876 | fd = fopen dpath 0 get dlen 0 49152 0 |
| 877 | if lt fd 0 |
| 878 | omnum klistref ulong neg fd |
| 879 | onl |
| 880 | ret 4 |
| 881 | end |
| 882 | e = dread fd |
| 883 | shut fd |
| 884 | if ne e 0 |
| 885 | omnum klistref ulong neg e |
| 886 | onl |
| 887 | ret 4 |
| 888 | end |
| 889 | nsort |
| 890 | ret lall |
| 891 | end |
| 892 | fn lall to int |
| 893 | c = new ulong 4 0 |
| 894 | for i ulong get nnum 0 |
| 895 | r = lentry get nord i |
| 896 | put c r add get c r 1 |
| 897 | end |
| 898 | omnum kentries get nnum 0 |
| 899 | omnum knok get c 0 |
| 900 | omnum knpart get c 1 |
| 901 | omnum knref get c 2 |
| 902 | omnum kndirs get c 3 |
| 903 | onl |
| 904 | if ne get ntrunc 0 0 |
| 905 | omsg ktrunc |
| 906 | onl |
| 907 | ret partial 1 |
| 908 | end |
| 909 | if ne add get c 1 get c 2 0 |
| 910 | ret partial 1 |
| 911 | end |
| 912 | ret 0 |
| 913 | end |
| 914 | note dispatch: the command word decides the argument count; anything else is usage (2). Nothing is |
| 915 | note opened before the count and the options are checked. |
| 916 | fn dispatch to int |
| 917 | c = ulong argcnt |
| 918 | if lt c 2 |
| 919 | ret 2 |
| 920 | end |
| 921 | argword 1 |
| 922 | if isword wversion |
| 923 | if ne c 2 |
| 924 | ret 2 |
| 925 | end |
| 926 | omsg kvers |
| 927 | onl |
| 928 | ret 0 |
| 929 | end |
| 930 | if lt c 3 |
| 931 | ret 2 |
| 932 | end |
| 933 | if isword wcompare |
| 934 | if ne c 4 |
| 935 | ret 2 |
| 936 | end |
| 937 | ret cmdcompare |
| 938 | end |
| 939 | if ne parseq c 0 |
| 940 | ret 2 |
| 941 | end |
| 942 | argword 1 |
| 943 | ret disp1 c |
| 944 | end |
| 945 | fn disp1 c ulong to int |
| 946 | if isword wevents |
| 947 | ret cmdrecs 0 |
| 948 | end |
| 949 | if isword wexport |
| 950 | ret cmdrecs 1 |
| 951 | end |
| 952 | if isword wstats |
| 953 | ret cmdstats |
| 954 | end |
| 955 | if ne c 3 |
| 956 | ret 2 |
| 957 | end |
| 958 | if isword wverify |
| 959 | ret cmdverify |
| 960 | end |
| 961 | if isword wshow |
| 962 | ret cmdrecs 0 |
| 963 | end |
| 964 | if isword wlist |
| 965 | ret cmdlist |
| 966 | end |
| 967 | ret 2 |
| 968 | end |
| 969 | note main: usage (2) discards any unflushed output and prints the usage line on stdout. |
| 970 | fn main to int |
| 971 | minit |
| 972 | r = dispatch |
| 973 | if eq r 2 |
| 974 | ousage |
| 975 | end |
| 976 | oflush |
| 977 | if ne get wbad 0 0 |
| 978 | ret 5 |
| 979 | end |
| 980 | ret r |
| 981 | end |