summaryrefslogtreecommitdiff
path: root/src/Plumb.zig
blob: e2edef77532c25421fe58d61c71154f67ce1fb05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
//! The plumber (Plan 9's plumber(4), windows/plumb/): the rules, and a
//! message matched against them and sent where they say. The language is
//! plumb(6)'s as rules.c and match.c read it, and its refusals are their
//! words; docs/next-plumber.md is the design. The rules are kept as their
//! text, a step's state as any other (digested, dumped, replicated), and
//! parsed again for each message: a send reads no file, and asks the disk
//! only at `isfile` and `isdir`, through the taped resolve sites Look uses.
const Plumb = @This();
const std = @import("std");
const pardes = @import("pardes.zig");
const fs = @import("fs.zig");
const look = @import("look.zig");
const layout = @import("layout.zig");
const Kind = @import("Kind.zig");
const tape = @import("tape.zig");
const events = @import("ninep/events.zig");
const Regex = @import("regexp.zig").Regex;
const Pardes = pardes.Pardes;

/// The rules in force once any were written, as plumb/rules reads them:
/// what `print` makes of them, an include's text in its place, in the
/// order they were written.
text: std.ArrayList(u8) = .empty,
/// Whether rules were written (or cleared): until then the rules in force
/// are `include basic`'s, so a session starts with the defaults, and so
/// does a dump made before the plumber.
written: bool = false,
/// Every port a rule has named this session, in the order they were
/// named, never dropped (plumber(4)'s directory): plumb/<port>'s files,
/// a port's index its node.
ports: std.ArrayList([]u8) = .empty,

/// The default rules, which `include basic` names, as Plan 9's names
/// /lib/plumb/basic.
pub const basic = @embedFile("plumbing");

/// The most rule text kept (Plan 9's whole basic is about 4 KiB), variables
/// and ports, as Plan 9's NDIR less its fixed files; deeper includes, as
/// rules.c's pushinput.
pub const max_text = 64 * 1024;
const max_vars = 32;
const max_ports = 32;
const max_depth = 10;

/// fsys.c's and match.c's words for a message that went nowhere.
pub const e_bad_msg = "bad plumb message format";
pub const e_no_port = "couldn't find destination for message";
pub const e_no_start = "no start action for plumb message";
pub const e_client = "no port for \"client\" rule";
pub const e_empty = "empty argument list";

pub fn deinit(pl: *Plumb, gpa: std.mem.Allocator) void {
    pl.text.deinit(gpa);
    for (pl.ports.items) |name| gpa.free(name);
    pl.ports.deinit(gpa);
}

// ---- the message ----

/// A plumb message (plumb.h, plumb(6)): six lines of header, then `data`.
pub const Msg = struct {
    src: []const u8 = "",
    dst: []const u8 = "",
    wdir: []const u8 = "",
    type: []const u8 = "",
    /// `name=value` pairs, blank-separated, as they are packed: a value
    /// holding a blank, a quote, `=` or a tab quoted rc's way.
    attr: []const u8 = "",
    data: []const u8 = "",

    /// plumbpack: the header lines, the data's length, the data.
    pub fn pack(m: Msg, w: *std.Io.Writer) std.Io.Writer.Error!void {
        try w.print("{s}\n{s}\n{s}\n{s}\n{s}\n{d}\n{s}", .{ m.src, m.dst, m.wdir, m.type, m.attr, m.data.len, m.data });
    }
};

/// plumbunpackpartial: a whole message, `more` bytes of its data still to
/// come, or one malformed (a header line short, a count the data passes).
pub const Unpacked = union(enum) { msg: Msg, more: usize, bad };

pub fn unpack(bytes: []const u8) Unpacked {
    var lines: [6][]const u8 = undefined;
    var at: usize = 0;
    for (&lines) |*line| {
        const nl = std.mem.indexOfScalarPos(u8, bytes, at, '\n') orelse return .bad;
        line.* = bytes[at..nl];
        at = nl + 1;
    }
    // atoi's: digits after blanks, else 0.
    const count = std.mem.trimStart(u8, lines[5], " \t");
    var n: usize = 0;
    for (count) |c| {
        if (c < '0' or c > '9') break;
        n = std.math.mul(usize, n, 10) catch return .bad;
        n = std.math.add(usize, n, c - '0') catch return .bad;
    }
    const rest = bytes.len - at;
    if (n > rest) return .{ .more = n - rest };
    if (n < rest) return .bad;
    return .{ .msg = .{ .src = lines[0], .dst = lines[1], .wdir = lines[2], .type = lines[3], .attr = lines[4], .data = bytes[at..] } };
}

/// One `name=value` of packed attributes, its value unquoted.
const Attr = struct { name: []const u8, value: []const u8 };

/// The attribute at `at.*` in `attr`, read as plumbunpackattr reads it,
/// its value unquoted into `buf`; null at their end, or at one with no
/// `=`, where Plan 9 stops.
fn nextAttr(attr: []const u8, at: *usize, buf: *[4096]u8) ?Attr {
    var i = at.*;
    while (i < attr.len and (attr[i] == ' ' or attr[i] == '\t')) i += 1;
    if (i >= attr.len or attr[i] == '\n') return null;
    const start = i;
    while (i < attr.len and attr[i] != '=' and attr[i] != ' ' and attr[i] != '\t' and attr[i] != '\n') i += 1;
    if (i >= attr.len or attr[i] != '=') return null;
    const name = attr[start..i];
    i += 1;
    var n: usize = 0;
    var quoting = false;
    while (i < attr.len and attr[i] != '\n' and n < buf.len) {
        const c = attr[i];
        i += 1;
        if (c == '\'') {
            if (!quoting) {
                quoting = true;
                continue;
            }
            if (i < attr.len and attr[i] == '\'') {
                i += 1;
            } else {
                quoting = false;
                continue;
            }
        } else if (!quoting and (c == ' ' or c == '\t')) break;
        buf[n] = c;
        n += 1;
    }
    at.* = i;
    return .{ .name = name, .value = buf[0..n] };
}

/// plumblookup: the first attribute named `name`'s value.
fn attrValue(attr: []const u8, name: []const u8, buf: *[4096]u8) ?[]const u8 {
    var at: usize = 0;
    while (nextAttr(attr, &at, buf)) |a| if (std.mem.eql(u8, a.name, name)) return a.value;
    return null;
}

/// `attr` packed again (plumbpackattr): the first attribute named `drop`
/// left out (plumbdelattr), those of `add` after it (plumbaddattr).
fn repack(arena: std.mem.Allocator, attr: []const u8, drop: ?[]const u8, add: []const u8) error{OutOfMemory}![]const u8 {
    var out: std.Io.Writer.Allocating = .init(arena);
    var dropped = drop == null;
    var buf: [4096]u8 = undefined;
    for ([2][]const u8{ attr, add }) |from| {
        var at: usize = 0;
        while (nextAttr(from, &at, &buf)) |a| {
            if (!dropped and std.mem.eql(u8, a.name, drop.?)) {
                dropped = true;
                continue;
            }
            const w = &out.writer;
            if (out.written().len > 0) w.writeByte(' ') catch return error.OutOfMemory;
            w.print("{s}=", .{a.name}) catch return error.OutOfMemory;
            // mesg.c's quote: a value with a blank, a quote, `=` or a tab.
            if (std.mem.indexOfAny(u8, a.value, " '=\t") == null) {
                w.writeAll(a.value) catch return error.OutOfMemory;
                continue;
            }
            w.writeByte('\'') catch return error.OutOfMemory;
            for (a.value) |c| {
                if (c == '\'') w.writeByte('\'') catch return error.OutOfMemory;
                w.writeByte(c) catch return error.OutOfMemory;
            }
            w.writeByte('\'') catch return error.OutOfMemory;
        }
    }
    return out.written();
}

// ---- the rules ----

/// rules.c's objects and verbs, in its order (`kind` is an old `type`).
const Obj = enum { arg, attr, data, dst, plumb, src, type, wdir };
const Verb = enum { add, client, delete, is, isdir, isfile, matches, set, start, to };

/// A line `object verb arg`: `qarg` is `arg` expanded with the variables
/// alone, its first word, as rules.c's parserule keeps it.
const Rule = struct { obj: Obj, verb: Verb, arg: []const u8, qarg: []const u8 };
const Set = struct { pats: []const Rule, acts: []const Rule, port: ?[]const u8 };
const Var = struct { name: []const u8, value: []const u8, qvalue: []const u8 };

/// The rules as read, each item where it was written: printed back in
/// that order, a variable given again later means what it meant to the
/// rules between (Plan 9's printrules puts every variable first).
const Rules = struct {
    items: std.ArrayList(union(enum) { variable: Var, ports: []const Rule, set: Set }) = .empty,
    vars: std.ArrayList(Var) = .empty,
    /// Every port a rule set or a declaration names, once, in order.
    ports: std.ArrayList([]const u8) = .empty,

    fn print(r: *const Rules, w: *std.Io.Writer) std.Io.Writer.Error!void {
        for (r.items.items) |item| switch (item) {
            .variable => |v| try w.print("{s}={s}\n\n", .{ v.name, v.value }),
            .ports => |acts| {
                for (acts) |a| try printRule(w, a);
                try w.writeByte('\n');
            },
            .set => |set| {
                for (set.pats) |a| try printRule(w, a);
                for (set.acts) |a| try printRule(w, a);
                try w.writeByte('\n');
            },
        };
    }

    fn printRule(w: *std.Io.Writer, r: Rule) std.Io.Writer.Error!void {
        try w.print("{s}\t{s}\t{s}\n", .{ @tagName(r.obj), @tagName(r.verb), r.arg });
    }

    fn named(r: *const Rules, port: []const u8) bool {
        for (r.ports.items) |n| if (std.mem.eql(u8, n, port)) return true;
        return false;
    }
};

/// What a message is matched with, beside the rules: the message, and what
/// the rule set's patterns found so far (match.c's Exec).
const Exec = struct {
    m: *Msg,
    /// The last `data matches`'s match, `$0`.
    match0: []const u8 = "",
    /// The span a click match took, which every other `data matches` of
    /// the set must take too.
    span: ?[2]usize = null,
    clearclick: bool = false,
    setdata: bool = false,
    /// What `isfile` and `isdir` found: `$file`, `$dir`.
    file: ?[]const u8 = null,
    dir: ?[]const u8 = null,
};

/// rules.c's expand: the first word of `s` (a blank outside quotes ends
/// it), quotes rc's (`'...'`, `''` a quote), `$name` a variable's value,
/// with a message the message's (`$data`, `$0`, `$file`, ...); a `$` that
/// names nothing stays. Past `out` (Plan 9's 4 KiB ebuf), `TooLong`.
/// Answers the word and how much of `s` it took.
fn expand(r: *const Rules, e: ?*const Exec, s: []const u8, out: *[4096]u8) error{TooLong}!struct { []const u8, usize } {
    var w = std.Io.Writer.fixed(out);
    var i: usize = 0;
    var quoting = false;
    while (i < s.len and (quoting or (s[i] != ' ' and s[i] != '\t'))) {
        if (s[i] == '\'') {
            i += 1;
            if (!quoting) {
                quoting = true;
            } else if (i < s.len and s[i] == '\'') {
                w.writeByte('\'') catch return error.TooLong;
                i += 1;
            } else quoting = false;
            continue;
        }
        if (quoting or s[i] != '$') {
            w.writeByte(s[i]) catch return error.TooLong;
            i += 1;
            continue;
        }
        i += 1;
        const took = dollar(r, e, s[i..], &w) catch return error.TooLong;
        if (took) |n| i += n else w.writeByte('$') catch return error.TooLong;
    }
    return .{ w.buffered(), i };
}

/// rules.c's dollar: the value `$` then `s` names, written to `w`; how
/// much of `s` its name took, or null when it names nothing.
fn dollar(r: *const Rules, e: ?*const Exec, s: []const u8, w: *std.Io.Writer) std.Io.Writer.Error!?usize {
    if (e) |x| if (s.len > 0 and std.ascii.isDigit(s[0])) {
        // `$1`-`$9` were refused at parse (no submatches): `$0` alone.
        if (s[0] == '0') try w.writeAll(x.match0);
        return 1;
    };
    var n: usize = 0;
    while (n < s.len and std.ascii.isAlphanumeric(s[n])) n += 1;
    const name = s[0..n];
    if (e) |x| {
        const m = x.m;
        const field: ?[]const u8 = if (std.mem.eql(u8, name, "src")) m.src else if (std.mem.eql(u8, name, "dst")) m.dst else if (std.mem.eql(u8, name, "attr")) m.attr else if (std.mem.eql(u8, name, "data")) m.data else if (std.mem.eql(u8, name, "type")) m.type else if (std.mem.eql(u8, name, "wdir")) m.wdir else null;
        if (field) |f| {
            try w.writeAll(f);
            return n;
        }
        // rules.c's filename: what isfile or isdir found, else the data
        // made absolute in wdir.
        const found = if (std.mem.eql(u8, name, "file")) x.file else if (std.mem.eql(u8, name, "dir")) x.dir else null;
        if (found != null or std.mem.eql(u8, name, "file") or std.mem.eql(u8, name, "dir")) {
            if (found) |f| {
                try w.writeAll(f);
            } else if ((m.data.len > 0 and m.data[0] == '/') or m.wdir.len == 0) {
                try w.writeAll(m.data);
            } else try w.print("{s}/{s}", .{ m.wdir, m.data });
            return n;
        }
    }
    for (r.vars.items) |v| if (std.mem.eql(u8, v.name, name)) {
        try w.writeAll(v.qvalue);
        return n;
    };
    return null;
}

/// Reads rule text into `rules` (rules.c's readrules): line by line, a
/// blank line (or a comment, or a variable) ending a rule set, an include
/// read in its place. A refusal is `why`: `<file>:<line>: <rules.c's words>`.
const Parser = struct {
    arena: std.mem.Allocator,
    /// Where an include is read from: none, only `basic`.
    p: ?*Pardes,
    /// Rules written, not the ones in force read again: their patterns
    /// compiled and their bounds counted.
    check: bool,
    rules: Rules = .{},
    pats: std.ArrayList(Rule) = .empty,
    acts: std.ArrayList(Rule) = .empty,
    port: ?[]const u8 = null,
    commands: usize = 0,
    file: []const u8 = "",
    line: usize = 0,
    depth: usize = 0,
    why: []const u8 = "",

    const Error = error{ Parse, OutOfMemory };

    fn fail(ps: *Parser, comptime fmt: []const u8, args: anytype) Error {
        ps.why = try std.fmt.allocPrint(ps.arena, "{s}:{d}: " ++ fmt, .{ ps.file, ps.line } ++ args);
        return error.Parse;
    }

    fn feed(ps: *Parser, name: []const u8, text: []const u8) Error!void {
        ps.file = name;
        ps.line = 0;
        const whole = if (std.mem.endsWith(u8, text, "\n")) text[0 .. text.len - 1] else text;
        var lines = std.mem.splitScalar(u8, whole, '\n');
        while (lines.next()) |line| {
            ps.line += 1;
            try ps.take(line);
        }
    }

    /// The text read, ended as its end ends a rule set.
    fn finish(ps: *Parser) Error!void {
        try ps.endSet();
    }

    /// rules.c's readrule, for one line.
    fn take(ps: *Parser, raw: []const u8) Error!void {
        const l = std.mem.trimStart(u8, raw, " \t");
        if (l.len == 0 or l[0] == '#') return ps.endSet();
        if (std.mem.startsWith(u8, l, "include")) return ps.include(l);
        if (try ps.assignment(l)) return;
        const obj_end = std.mem.indexOfAny(u8, l, " \t") orelse return ps.fail("malformed rule", .{});
        const obj_word = l[0..obj_end];
        const obj = std.meta.stringToEnum(Obj, obj_word) orelse
            if (std.mem.eql(u8, obj_word, "kind")) .type else return ps.fail("unknown object {s}", .{obj_word});
        const rest = std.mem.trimStart(u8, l[obj_end..], " \t");
        const verb_end = std.mem.indexOfAny(u8, rest, " \t") orelse return ps.fail("malformed rule", .{});
        const verb = std.meta.stringToEnum(Verb, rest[0..verb_end]) orelse return ps.fail("unknown verb {s}", .{rest[0..verb_end]});
        const arg = std.mem.trimStart(u8, rest[verb_end..], " \t");
        if (arg.len == 0) return ps.fail("malformed rule", .{});
        // parserule.
        const action = verb == .client or verb == .start or verb == .to;
        if ((obj == .plumb) != action or (obj != .attr and (verb == .add or verb == .delete)))
            return ps.fail("{s} not valid verb for object {s}", .{ @tagName(verb), @tagName(obj) });
        // pardes's regular expressions have no capture groups (regexp.zig,
        // mvzr), and the user chose never to have an engine of pardes's
        // own: a submatch is refused, never silently empty.
        var quoting = false;
        for (arg, 0..) |c, i| {
            if (c == '\'') quoting = !quoting;
            if (!quoting and c == '$' and i + 1 < arg.len and arg[i + 1] >= '1' and arg[i + 1] <= '9')
                return ps.fail("${c}: no submatches (the regular expressions have no capture groups): use $0 and several matches", .{arg[i + 1]});
        }
        const r: Rule = .{ .obj = obj, .verb = verb, .arg = arg, .qarg = try ps.word(arg) };
        if (verb == .matches and ps.check) {
            var pat: [4096 + 4]u8 = undefined;
            _ = Regex.compile(anchored(r.qarg, &pat)) catch |err| return ps.fail("{s}", .{Regex.message(err)});
        }
        if (obj != .plumb) return ps.pats.append(ps.arena, r);
        try ps.acts.append(ps.arena, r);
        if (verb != .to) {
            ps.commands += 1;
            return;
        }
        // readruleset's: a declaration names many, a rule set one.
        if (ps.pats.items.len > 0 and ps.port != null) return ps.fail("too many ports", .{});
        for ([_][]const u8{ ".", "..", "send", "rules" }) |bad| if (std.mem.eql(u8, r.qarg, bad))
            return ps.fail("illegal port name {s}", .{r.qarg});
        ps.port = r.qarg;
    }

    /// readruleset's end: a rule set kept, ports declared, or a refusal.
    fn endSet(ps: *Parser) Error!void {
        const pats = ps.pats.items;
        const acts = ps.acts.items;
        const port = ps.port;
        const commands = ps.commands;
        ps.pats = .empty;
        ps.acts = .empty;
        ps.port = null;
        ps.commands = 0;
        if (commands > 1) return ps.fail("ruleset has more than one client or start action", .{});
        if (pats.len == 0 and acts.len == 0) return;
        if (pats.len > 0 and acts.len > 0) {
            try ps.rules.items.append(ps.arena, .{ .set = .{ .pats = pats, .acts = acts, .port = port } });
        } else {
            if (acts.len == 0 or port == null) return ps.fail("ruleset must have patterns and actions", .{});
            for (acts) |a| if (a.verb != .to) return ps.fail("ruleset must have actions", .{});
            try ps.rules.items.append(ps.arena, .{ .ports = acts });
        }
        for (acts) |a| if (a.verb == .to and !ps.rules.named(a.qarg)) {
            if (ps.check and ps.rules.ports.items.len >= max_ports) return ps.fail("too many ports; max {d}", .{max_ports});
            try ps.rules.ports.append(ps.arena, a.qarg);
        };
    }

    /// `s`'s first word expanded with the variables: a slice of `s` when
    /// that changes nothing (no quote, no `$`), as in most rules. What a
    /// parse keeps slices the text it read, which outlives it (the rules in
    /// force, a write's, an include read into the arena, src/plumbing).
    fn word(ps: *Parser, s: []const u8) Error![]const u8 {
        var buf: [4096]u8 = undefined;
        const w, const took = expand(&ps.rules, null, s, &buf) catch return ps.fail("string-too-long", .{});
        return if (std.mem.eql(u8, w, s[0..took])) s[0..took] else ps.arena.dupe(u8, w);
    }

    /// rules.c's assignment: `name=value`, its value expanded once, now;
    /// it ends a rule set as a blank line does.
    fn assignment(ps: *Parser, l: []const u8) Error!bool {
        if (!std.ascii.isAlphabetic(l[0])) return false;
        var i: usize = 0;
        while (i < l.len and std.ascii.isAlphanumeric(l[i])) i += 1;
        const name = l[0..i];
        while (i < l.len and (l[i] == ' ' or l[i] == '\t')) i += 1;
        if (i >= l.len or l[i] != '=') return false;
        try ps.endSet();
        const value = std.mem.trimStart(u8, l[i + 1 ..], " \t");
        const v: Var = .{ .name = name, .value = value, .qvalue = try ps.word(value) };
        try ps.rules.items.append(ps.arena, .{ .variable = v });
        for (ps.rules.vars.items) |*old| if (std.mem.eql(u8, old.name, name)) {
            old.* = v;
            return true;
        };
        if (ps.check and ps.rules.vars.items.len >= max_vars) return ps.fail("too many variables; max {d}", .{max_vars});
        try ps.rules.vars.append(ps.arena, v);
        return true;
    }

    /// rules.c's include: `include basic` is src/plumbing; another name
    /// the configuration directory's file, an absolute one where it says.
    fn include(ps: *Parser, l: []const u8) Error!void {
        var words = std.mem.tokenizeAny(u8, l, " \t");
        const first = words.next().?;
        const name = words.next() orelse "";
        const third = words.next();
        if (!std.mem.eql(u8, first, "include") or name.len == 0 or name[0] == '#' or (third != null and third.?[0] != '#'))
            return ps.fail("malformed include statement", .{});
        if (ps.depth >= max_depth) return ps.fail("include stack too deep; max {d}", .{max_depth});
        const text = if (std.mem.eql(u8, name, "basic")) basic else text: {
            var buf: [4096]u8 = undefined;
            const p = ps.p orelse return ps.fail("can't open {s} for inclusion", .{name});
            const path = if (name[0] == '/') name else std.fmt.bufPrint(&buf, "{s}/{s}", .{ p.configDir() orelse "", name }) catch name;
            break :text fs.readFile(ps.arena, path) catch return ps.fail("can't open {s} for inclusion", .{path});
        };
        const file = ps.file;
        const line = ps.line;
        ps.depth += 1;
        try ps.feed(name, text);
        ps.depth -= 1;
        ps.file = file;
        ps.line = line;
    }
};

/// `pat` matching only the whole of what it is run over: `^(pat)$` (its
/// own anchors dropped, mvzr taking `^` only first), in `out`.
fn anchored(pat: []const u8, out: *[4096 + 4]u8) []const u8 {
    var core = pat;
    if (core.len > 0 and core[0] == '^') core = core[1..];
    if (core.len > 1 and core[core.len - 1] == '$' and core[core.len - 2] != '\\') core = core[0 .. core.len - 1];
    return std.fmt.bufPrint(out, "^({s})$", .{core}) catch pat;
}

/// The rules in force as text to read again.
fn source(p: *Pardes) []const u8 {
    return if (p.plumber.written) p.plumber.text.items else "include basic\n";
}

/// The rules in force read, for a send.
fn rulesOf(p: *Pardes, arena: std.mem.Allocator) Parser.Error!Rules {
    var ps: Parser = .{ .arena = arena, .p = p, .check = false };
    try ps.feed("plumb/rules", source(p));
    try ps.finish();
    return ps.rules;
}

/// The rules in force as plumb/rules reads them, the defaults printed
/// (in the step's scratch) while none were written.
pub fn inForce(p: *Pardes) []const u8 {
    if (p.plumber.written) return p.plumber.text.items;
    const arena = p.scratch.allocator();
    const rules = rulesOf(p, arena) catch return "";
    var out: std.Io.Writer.Allocating = .init(arena);
    rules.print(&out.writer) catch return "";
    return out.written();
}

/// What the `Plumb` word says to a rule written after it.
pub const e_word = "Plumb takes no rule now: rules are plumb(6) rule sets, in <config>/plumbing (Plumb opens it) or written to plumb/rules";

/// The plumbing file, the user's rules beside init (plumber(4)'s
/// $home/lib/plumbing), in `buf`; null with no configuration directory.
pub fn filePath(p: *Pardes, buf: *[4096]u8) ?[]const u8 {
    const dir = p.configDir() orelse return null;
    return std.fmt.bufPrint(buf, "{s}/plumbing", .{std.mem.trimEnd(u8, dir, "/")}) catch null;
}

/// At boot, the plumbing file's rules in force, if it is there (the
/// plumber reading $home/lib/plumbing); a refusal is said, as an init
/// line that fails is, and the defaults stay. Either way the ports the
/// rules in force name have their files.
pub fn boot(p: *Pardes) void {
    if (rulesOf(p, p.scratch.allocator())) |rules| notePorts(p, rules.ports.items) catch {} else |_| {}
    var buf: [4096]u8 = undefined;
    const path = filePath(p, &buf) orelse return;
    const text = fs.read(p, path) catch return;
    defer p.gpa.free(text);
    if (apply(p, false, "plumbing", text)) |why| p.reportFailure(p.view().active, why);
}

/// A file saved from pane `pane`: when it is the plumbing file, its rules
/// in force now, or the save says why not and the rules stay as they were
/// (plumb.ms's `cp lib/plumbing /mnt/plumb/rules`, done for you).
pub fn saved(p: *Pardes, pane: usize, path: []const u8, text: []const u8) void {
    var buf: [4096]u8 = undefined;
    const mine = filePath(p, &buf) orelse return;
    if (!std.mem.eql(u8, mine, path)) return;
    if (apply(p, false, "plumbing", text)) |why| p.reportFailure(pane, why);
}

/// `names` added to the session's ports, those it has not yet, in order;
/// none when they would be more than max_ports.
fn notePorts(p: *Pardes, names: []const []const u8) error{ TooMany, OutOfMemory }!void {
    var new: usize = 0;
    for (names) |n| new += @intFromBool(portIndex(p, n) == null);
    if (p.plumber.ports.items.len + new > max_ports) return error.TooMany;
    try p.plumber.ports.ensureUnusedCapacity(p.gpa, new);
    for (names) |n| if (portIndex(p, n) == null) p.plumber.ports.appendAssumeCapacity(try p.gpa.dupe(u8, n));
}

/// The port named `name`'s index, its file's node.
pub fn portIndex(p: *const Pardes, name: []const u8) ?u8 {
    for (p.plumber.ports.items, 0..) |n, i| if (std.mem.eql(u8, n, name)) return @intCast(i);
    return null;
}

pub fn portName(p: *const Pardes, index: u8) ?[]const u8 {
    return if (index < p.plumber.ports.items.len) p.plumber.ports.items[index] else null;
}

/// Whether a program reads port `index` (an open of its file): the
/// authority's 9P state, asked through the tape.
fn portRead(p: *Pardes, index: u8) bool {
    for (&p.fs.opens) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) return true;
    return false;
}

/// `m` packed onto `q` (a reader's, the held ones'), after `lead` (a
/// held message's port); false when it is over what a queue holds.
fn enqueue(p: *Pardes, q: *events.Queue, lead: []const u8, m: *const Msg) bool {
    var out: std.Io.Writer.Allocating = .init(p.scratch.allocator());
    out.writer.writeAll(lead) catch return false;
    m.pack(&out.writer) catch return false;
    if (out.written().len + 4 > q.cap) return false;
    q.push(p.gpa, out.written());
    return true;
}

/// A copy of `m` to every reader of port `index` (plumber(4)'s queuesend:
/// every fid open when it was sent), each read held for one answered.
fn fanOut(p: *Pardes, index: u8, m: *const Msg) void {
    for (&p.fs.opens) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) {
        if (!enqueue(p, &o.what.port.queue, "", m)) {
            var said: Kind.Buf(160) = .{};
            events.notePath(p, "err -", said.print("plumb/{s}: a message of {d} bytes is over a reader's {d} KiB, not sent to it", .{ portName(p, index).?, m.data.len, events.queue_cap / 1024 }));
        }
    };
    p.fs.news = true;
}

/// A port's file opened (tree.zig): what `client` rules held for it, the
/// opener's (plumber(4)'s queueheld).
pub fn opened(p: *Pardes, index: u8, q: *events.Queue) void {
    const held = &p.fs.plumb_held;
    var kept: events.Queue = .{};
    while (held.peek()) |record| {
        if (record[0] == index) {
            q.push(p.gpa, record[1..]);
        } else kept.push(p.gpa, record);
        held.pop();
    }
    held.deinit(p.gpa);
    held.* = kept;
}

/// No rules at all: a `>` to plumb/rules, before what it writes.
pub fn clear(p: *Pardes) void {
    p.plumber.text.clearRetainingCapacity();
    p.plumber.written = true;
}

/// `more` (read from `name`) read after the rules in force (`keep`) or in
/// their place, and what that makes kept as the rules: plumb/rules' write
/// and its close's rest, the plumbing file saved or read at boot. Null
/// when taken, else why not, in the step's scratch, the rules as they were.
pub fn apply(p: *Pardes, keep: bool, name: []const u8, more: []const u8) ?[]const u8 {
    const arena = p.scratch.allocator();
    var ps: Parser = .{ .arena = arena, .p = p, .check = true };
    const said = said: {
        if (keep) {
            ps.feed("plumb/rules", source(p)) catch break :said ps.why;
            ps.finish() catch break :said ps.why;
        }
        ps.feed(name, more) catch break :said ps.why;
        ps.finish() catch break :said ps.why;
        var out: std.Io.Writer.Allocating = .init(arena);
        ps.rules.print(&out.writer) catch break :said "out of memory";
        if (out.written().len > max_text) break :said std.fmt.comptimePrint("rules too long; max {d} KiB", .{max_text / 1024});
        notePorts(p, ps.rules.ports.items) catch |err| break :said switch (err) {
            error.TooMany => std.fmt.comptimePrint("too many ports; max {d}", .{max_ports}),
            error.OutOfMemory => "out of memory",
        };
        p.plumber.text.clearRetainingCapacity();
        p.plumber.text.appendSlice(p.gpa, out.written()) catch break :said "out of memory";
        p.plumber.written = true;
        return null;
    };
    return if (said.len > 0) said else "out of memory";
}

// ---- matching ----

/// One message's match (fsys.c's write of `send` through dispose): the
/// rules, and what the disk said of a name, asked once however many rules
/// ask it.
const Send = struct {
    p: *Pardes,
    /// The pane a look sent from, whose jump list's directories a name is
    /// looked for in too; none from plumb/send.
    from: ?usize,
    arena: std.mem.Allocator,
    rules: Rules,
    /// The data as a look sent it (`Seen`): a message to `edit` that still
    /// holds it is Look's to open.
    looked: ?[]const u8 = null,
    memo_name: ?[]const u8 = null,
    memo_wdir: []const u8 = "",
    memo: ?fs.Resolved = null,
    memo_buf: [4096]u8 = undefined,

    /// Where a name is, as Look finds it: the place written after it
    /// (`:12`, `:12:5`) no part of it, then the message's directory, the
    /// jump list's panes' (from a look), then Incl's (look.zig's
    /// resolveLookTarget). Each stat is a taped resolve site; a name is
    /// asked once a send.
    fn resolve(s: *Send, wdir: []const u8, raw: []const u8) ?fs.Resolved {
        const name = look.parsePathLine(std.mem.trim(u8, raw, " \t\r\n")).path;
        if (s.memo_name) |n| if (std.mem.eql(u8, n, name) and std.mem.eql(u8, s.memo_wdir, wdir)) return s.memo;
        s.memo_name = s.arena.dupe(u8, name) catch return null;
        s.memo_wdir = s.arena.dupe(u8, wdir) catch return null;
        s.memo = s.search(wdir, name);
        return s.memo;
    }

    fn search(s: *Send, wdir: []const u8, name: []const u8) ?fs.Resolved {
        if (name.len == 0) return null;
        var dirs: look.Dirs = .init(s.p, s.from, wdir);
        while (dirs.next()) |dir| {
            if (fs.resolve(s.p, name, dir, &s.memo_buf)) |found| return found;
            if (name[0] == '/' or fs.dotRelative(name)) return null;
        }
        for (s.p.incl.items) |dir| if (fs.resolve(s.p, name, dir, &s.memo_buf)) |found| return found;
        return null;
    }

    /// match.c's matchruleset: the message tried against `set`, rewritten
    /// in place by its patterns (a `set` stays though a later one fails).
    fn matchSet(s: *Send, set: Set, e: *Exec) error{OutOfMemory}!bool {
        const m = e.m;
        if (m.dst.len > 0 and set.port != null and !std.mem.eql(u8, m.dst, set.port.?)) return false;
        for (set.pats) |r| if (!try s.pattern(e, r)) return false;
        if (set.port) |port| if (m.dst.len == 0) {
            m.dst = port;
        };
        // match.c's rewrite: a click match's span is the data now.
        if (e.clearclick) {
            m.attr = try repack(s.arena, m.attr, "click", "");
            if (e.setdata) m.data = e.match0;
        }
        return true;
    }

    /// match.c's matchpat.
    fn pattern(s: *Send, e: *Exec, r: Rule) error{OutOfMemory}!bool {
        const m = e.m;
        const field: ?*[]const u8 = switch (r.obj) {
            .data => &m.data,
            .dst => &m.dst,
            .type => &m.type,
            .wdir => &m.wdir,
            .src => &m.src,
            .arg, .attr, .plumb => null,
        };
        var buf: [4096]u8 = undefined;
        switch (r.verb) {
            .is => return if (field) |f| std.mem.eql(u8, f.*, r.qarg) else false,
            .set => {
                const f = field orelse return true;
                const value = (expand(&s.rules, e, r.arg, &buf) catch return false)[0];
                f.* = try s.arena.dupe(u8, value);
                if (r.obj == .data) {
                    e.span = null;
                    e.setdata = false;
                }
                return true;
            },
            .add, .delete => {
                if (r.obj != .attr) return false;
                const value = (expand(&s.rules, e, r.arg, &buf) catch return false)[0];
                if (r.verb == .add) {
                    m.attr = try repack(s.arena, m.attr, null, value);
                    return true;
                }
                // The code's, not the man page's: an absent one fails.
                var vbuf: [4096]u8 = undefined;
                if (attrValue(m.attr, value, &vbuf) == null) return false;
                m.attr = try repack(s.arena, m.attr, value, "");
                return true;
            },
            .isfile, .isdir => {
                const name = switch (r.obj) {
                    .arg => (expand(&s.rules, e, r.arg, &buf) catch return false)[0],
                    .data => m.data,
                    .wdir => m.wdir,
                    else => return false,
                };
                const found = s.resolve(m.wdir, name) orelse return false;
                if (found.dir != (r.verb == .isdir)) return false;
                const path = try s.arena.dupe(u8, found.path);
                if (r.verb == .isdir) e.dir = path else e.file = path;
                return true;
            },
            .matches => {
                const text = (field orelse return false).*;
                var pat: [4096 + 4]u8 = undefined;
                var click_buf: [4096]u8 = undefined;
                const click = if (r.obj == .data) attrValue(m.attr, "click", &click_buf) else null;
                if (click == null) {
                    // The whole of it, as match.c asks of regexec's answer.
                    var rx = Regex.compile(anchored(r.qarg, &pat)) catch return false;
                    const hit = (rx.find(text, 0, 0, text.len) catch return false) orelse return false;
                    if (hit.start != 0 or hit.end != text.len) return false;
                    e.match0 = text;
                    return true;
                }
                // clickmatch: the first match from each start up to the
                // click that holds it, the click counted in runes.
                const runes = std.fmt.parseInt(usize, std.mem.trim(u8, click.?, " \t"), 10) catch 0;
                var at: usize = 0;
                var n: usize = 0;
                while (n < runes and at < text.len) : (n += 1) at += std.unicode.utf8ByteSequenceLength(text[at]) catch 1;
                at = @min(at, text.len);
                var rx = Regex.compile(r.qarg) catch return false;
                var from: usize = 0;
                const hit = while (from <= at) {
                    const h = (rx.find(text, from, at, text.len) catch return false) orelse return false;
                    if (h.start <= at and at <= h.end) break h;
                    // Every start before the next match finds this one.
                    from = h.start + 1;
                    while (from < text.len and text[from] & 0xC0 == 0x80) from += 1;
                } else return false;
                if (e.span) |sp| if (sp[0] != hit.start or sp[1] != hit.end) return false;
                e.span = .{ hit.start, hit.end };
                e.clearclick = true;
                e.setdata = true;
                e.match0 = text[hit.start..hit.end];
                return true;
            },
            .client, .start, .to => return false,
        }
    }
};

/// The ports pardes reads itself, as acme reads `edit`; and every loaded
/// kind by its name.
const Port = enum {
    /// A file or directory opened as Look opens one, at the place written
    /// after its name or given as `addr=`.
    edit,
    /// A URL given to the host's browser (xdg-open, open on macOS).
    web,
};

/// What a look found of the name it sends (look.zig's resolveLookTarget:
/// in its pane's directory, the jump list's, Incl's), which `isfile` and
/// `isdir` take as found rather than ask the disk again: the same answer,
/// and Look's own readings of a name kept (a URL, `//`: no file).
pub const Seen = struct { name: []const u8, found: ?fs.Resolved };

/// Where a send went.
pub const Sent = union(enum) {
    /// Taken: started, or given to the port that reads it.
    done,
    /// To `edit` as the look sent it (`Seen`): Look opens what it found,
    /// as acme's own look opens a file.
    look,
    /// Taken by nothing, in Plan 9's words; or what a rule started
    /// declined it (`declined`).
    refused: []const u8,
};

/// What a send a rule's started word declined says (Pardes.plumb_declined):
/// fsys.c's words for a message nothing takes.
pub const e_declined = "no matching plumb rule";

/// A message matched against the rules and sent where they say (fsys.c's
/// write of `send`, then dispose), from pane `from` (a look's) or from
/// plumb/send (null: the pane with the keyboard runs what it starts).
/// `seen` is a look's, what it found of the name it sends. A refusal is in
/// the step's scratch.
pub fn send(p: *Pardes, from: ?usize, msg: Msg, seen: ?Seen) Sent {
    const arena = p.scratch.allocator();
    var s: Send = .{ .p = p, .from = from, .arena = arena, .rules = rulesOf(p, arena) catch return .{ .refused = "plumb/rules: the rules in force do not read" } };
    if (seen) |look_seen| {
        s.looked = msg.data;
        s.memo_name = look_seen.name;
        s.memo_wdir = msg.wdir;
        s.memo = look_seen.found;
    }
    var m = msg;
    var e: Exec = .{ .m = &m };
    const set: ?Set = for (s.rules.items.items) |item| {
        const candidate = switch (item) {
            .set => |x| x,
            else => continue,
        };
        e = .{ .m = &m };
        if (s.matchSet(candidate, &e) catch return .{ .refused = "out of memory" }) break candidate;
    } else null;
    const id = from orelse p.view().active;
    // dispose.
    if (m.dst.len == 0) return startup(&s, id, set orelse return .{ .refused = e_no_port }, &e);
    const port = portIndex(p, m.dst);
    // A port pardes reads itself: its readers' copies (a program reading
    // plumb/edit sees every open, plumber(4)'s fan-out), the authority's
    // alone to hand out, so asked of nobody; then pardes's own.
    if (std.meta.stringToEnum(Port, m.dst)) |own| {
        if (port) |i| fanOut(p, i, &m);
        return deliver(&s, id, own, &m);
    }
    if (p.kinds.byName(m.dst)) |k| {
        if (port) |i| fanOut(p, i, &m);
        openInKind(&s, id, k, &m);
        return .done;
    }
    // One no rule ever named is none, and the message goes nowhere, as
    // fsys.c's dispose finds no Dirtab for it. Another goes to its readers,
    // whom only the authority knows (its 9P opens); with none, what the
    // rule set starts.
    const i = port orelse return .done;
    if (tape.authority(portRead, .{ p, i })) {
        fanOut(p, i, &m);
        return .done;
    }
    return startup(&s, id, set orelse return .{ .refused = e_no_start }, &e);
}

/// match.c's startup: the rule set's `start` or `client`, its words
/// expanded, run.
fn startup(s: *Send, id: usize, set: Set, e: *const Exec) Sent {
    const act = for (set.acts) |a| {
        if (a.verb == .start) break a;
        if (a.verb == .client) {
            if (e.m.dst.len == 0) return .{ .refused = e_client };
            break a;
        }
    } else return .{ .refused = e_no_start };
    // buildargv: one expansion a word.
    var argv: std.ArrayList([]const u8) = .empty;
    var rest = act.arg;
    while (true) {
        rest = std.mem.trimStart(u8, rest, " \t");
        if (rest.len == 0) break;
        var buf: [4096]u8 = undefined;
        const word, const took = expand(&s.rules, e, rest, &buf) catch return .{ .refused = "string-too-long" };
        argv.append(s.arena, s.arena.dupe(u8, word) catch return .{ .refused = "out of memory" }) catch return .{ .refused = "out of memory" };
        rest = rest[took..];
    }
    if (argv.items.len == 0) return .{ .refused = e_empty };
    if (run(s.p, id, e.m.wdir, argv.items)) |why| return .{ .refused = why };
    // `client`: the message waits for the port's first reader, the program
    // it started perhaps (plumber(4)'s hold); held messages past what the
    // queue keeps push out the oldest.
    if (act.verb == .client) if (portIndex(s.p, e.m.dst)) |i| {
        if (!enqueue(s.p, &s.p.fs.plumb_held, &.{i}, e.m)) return .{ .refused = "plumb: message too big to hold for a client" };
    };
    return .done;
}

/// What a `plumb start` or a `plumb client` runs:
/// the one place that decides it (question 4 of docs/next-plumber.md).
/// A builtin or a kind's word runs in place, from the message's pane, as
/// a line of it would; one that cannot do what the message asks declines
/// (Pardes.plumb_declined), saying nothing, and the send is refused, so a
/// look looks itself. Any other is a program, started as Plan 9's plumber
/// starts one (match.c's startup): detached, no pane, its argv exec'd in
/// `wdir` with no shell (a rule that wants one says `sh -c`), by the host
/// (Effect.start), which says a failure to start in the log. Null when it
/// ran, else why not.
fn run(p: *Pardes, id: usize, wdir: []const u8, argv: []const []const u8) ?[]const u8 {
    const arena = p.scratch.allocator();
    const line = std.mem.join(arena, " ", argv) catch return "out of memory";
    const outer = p.plumb_declined;
    p.plumb_declined = false;
    defer p.plumb_declined = outer;
    // Started, not clicked: no name said for it under Verbose.
    const announce = p.announce;
    p.announce = false;
    defer p.announce = announce;
    if (p.executeBuiltinLine(id, line)) return if (p.plumb_declined.?) e_declined else null;
    // Its directory, then its words, each ended by a NUL.
    var start: pardes.Effect.Buf(pardes.effect_path_cap) = .{};
    for ([_][]const []const u8{ &.{wdir}, argv }) |words| for (words) |w| {
        if (std.mem.indexOfScalar(u8, w, 0) != null) return "plumb start: a NUL in a word";
        if (start.len + w.len + 1 > start.data.len) return std.fmt.comptimePrint("plumb start: over {d} bytes", .{pardes.effect_path_cap});
        @memcpy(start.data[start.len..][0..w.len], w);
        start.data[start.len + w.len] = 0;
        start.len += @intCast(w.len + 1);
    };
    p.emit(.{ .start = .{ .pane = @intCast(id), .line = start } });
    return null;
}

/// What pardes does with a message to a port it reads. Like a message
/// queued to a reader in Plan 9, the send is done: what goes wrong after
/// is said as a look's failure is.
fn deliver(s: *Send, id: usize, port: Port, m: *const Msg) Sent {
    const p = s.p;
    switch (port) {
        .web => {
            if (m.data.len > 256) p.reportFailure(id, "plumb: web: a URL is at most 256 bytes") else p.emit(.{ .open_link = .from(m.data) });
        },
        .edit => {
            var buf: [4096]u8 = undefined;
            const addr = attrValue(m.attr, "addr", &buf) orelse "";
            // A look's own name, as it sent it: its own look opens it.
            if (s.looked) |data| if (addr.len == 0 and std.mem.eql(u8, data, m.data)) return .look;
            // acme's plumblook: the data a name in wdir, at its addr,
            // opened as Look opens one and not sent again.
            const relative = m.data.len > 0 and m.data[0] != '/' and m.data[0] != '~' and m.wdir.len > 0;
            const text = std.fmt.allocPrint(s.arena, "{s}{s}{s}{s}{s}", .{
                if (relative) m.wdir else "", if (relative) "/" else "", m.data,
                if (addr.len > 0) ":" else "",                         addr,
            }) catch {
                p.reportError(id, "plumb", error.OutOfMemory);
                return .done;
            };
            const was = p.look_text;
            p.look_text = true;
            defer p.look_text = was;
            look.lookAt(p, id, text);
        },
    }
    return .done;
}

/// A message to a loaded kind's port: its data, a file, opened in that kind
/// (or its pane given the keyboard, when one shows it).
fn openInKind(s: *Send, id: usize, k: u8, m: *const Msg) void {
    const p = s.p;
    const found = s.resolve(m.wdir, m.data) orelse return p.reportFailure(id, "plumb: no such file");
    if (found.dir) return p.reportFailure(id, "plumb: a directory is no kind's");
    const path = s.arena.dupe(u8, found.path) catch return;
    for (p.panes, 0..) |slot, i| if (slot) |pn| if (pn.addon) |a| if (std.mem.eql(u8, a.path, path)) {
        p.view().active = i;
        return;
    };
    const free = p.freeSlot() orelse return p.reportError(id, "plumb", error.NoPaneSlots);
    const made = Kind.openFile(p, free, k, path) catch |err| return p.reportError(id, path, err);
    if (made.addon.?.refused.len > 0) {
        made.vweight = 0;
        p.unplaced.set(free);
        return p.reportFailure(id, s.arena.dupe(u8, made.addon.?.refused.slice()) catch "plumb: refused");
    }
    layout.placeNew(p, id, id, free, .doc);
}

/// Where rule text written in pieces is whole: through its last blank line
/// (rules.c's morerules), 0 when none is.
pub fn wholeSets(text: []const u8) usize {
    return if (std.mem.lastIndexOf(u8, text, "\n\n")) |at| at + 2 else 0;
}

// ---- tests ----

const testing = std.testing;

/// The rules `text` read as written ones are, printed; or the refusal.
fn reread(arena: std.mem.Allocator, text: []const u8) ![]const u8 {
    var ps: Parser = .{ .arena = arena, .p = null, .check = true };
    ps.feed("t", text) catch return ps.why;
    ps.finish() catch return ps.why;
    var out: std.Io.Writer.Allocating = .init(arena);
    try ps.rules.print(&out.writer);
    return out.written();
}

test "plumb: a message packs as plumb(6)'s six lines and its data, and unpacks whole, short of its data, or malformed" {
    var buf: [256]u8 = undefined;
    var w = std.Io.Writer.fixed(&buf);
    const m: Msg = .{ .src = "pardes", .wdir = "/tmp", .type = "text", .attr = "click=3 addr=12", .data = "a b\nc" };
    try m.pack(&w);
    try testing.expectEqualStrings("pardes\n\n/tmp\ntext\nclick=3 addr=12\n5\na b\nc", w.buffered());
    const back = unpack(w.buffered()).msg;
    inline for (.{ "src", "dst", "wdir", "type", "attr", "data" }) |f| try testing.expectEqualStrings(@field(m, f), @field(back, f));
    try testing.expectEqual(@as(usize, 2), unpack(w.buffered()[0 .. w.buffered().len - 2]).more);
    // A header line short is no message at all, as is data past its count.
    try testing.expect(unpack("pardes\n\n/tmp\ntext\n") == .bad);
    try testing.expect(unpack("a\nb\nc\nd\ne\n1\nxy") == .bad);
    try testing.expect(unpack("a\nb\nc\nd\ne\nzz\n") == .msg);
}

test "plumb: attributes read rc's quotes, a value that needs them is quoted again, delete takes the first, add appends" {
    var arena: std.heap.ArenaAllocator = .init(testing.allocator);
    defer arena.deinit();
    const a = arena.allocator();
    var buf: [4096]u8 = undefined;
    const attr = "action=showdata filename='/a b/it''s' click=4";
    try testing.expectEqualStrings("/a b/it's", attrValue(attr, "filename", &buf).?);
    try testing.expectEqualStrings("4", attrValue(attr, "click", &buf).?);
    try testing.expect(attrValue(attr, "addr", &buf) == null);
    try testing.expectEqualStrings("action=showdata filename='/a b/it''s'", try repack(a, attr, "click", ""));
    try testing.expectEqualStrings("action=showdata filename='/a b/it''s' click=4 addr='x=1'", try repack(a, attr, null, "addr='x=1'"));
    // One with no `=` ends them, as plumbunpackattr stops there.
    try testing.expect(attrValue("a=1 bogus b=2", "b", &buf) == null);
}

test "plumb: every parse error in rules.c's words, with its line" {
    var arena: std.heap.ArenaAllocator = .init(testing.allocator);
    defer arena.deinit();
    const a = arena.allocator();
    for ([_][2][]const u8{
        .{ "type is text\nbogus\n", "t:2: malformed rule" },
        .{ "type is\n", "t:1: malformed rule" },
        .{ "type   \n", "t:1: malformed rule" },
        .{ "colour is red\n", "t:1: unknown object colour" },
        .{ "type equals text\n", "t:1: unknown verb equals" },
        .{ "data to edit\n", "t:1: to not valid verb for object data" },
        .{ "type start x\n", "t:1: start not valid verb for object type" },
        .{ "data add x=1\n", "t:1: add not valid verb for object data" },
        .{ "plumb is edit\n", "t:1: is not valid verb for object plumb" },
        .{ "type is text\nplumb to a\nplumb to b\n", "t:3: too many ports" },
        .{ "type is text\nplumb to send\n", "t:2: illegal port name send" },
        .{ "plumb to ..\n", "t:1: illegal port name .." },
        .{ "plumb to rules\n", "t:1: illegal port name rules" },
        .{ "type is text\nplumb start a\nplumb client b\n", "t:3: ruleset has more than one client or start action" },
        .{ "type is text\n\n", "t:2: ruleset must have patterns and actions" },
        .{ "plumb start x\n", "t:1: ruleset must have patterns and actions" },
        .{ "plumb to a\nplumb start x\n\n", "t:3: ruleset must have actions" },
        .{ "include\n", "t:1: malformed include statement" },
        .{ "include a b\n", "t:1: malformed include statement" },
        .{ "includes x\n", "t:1: malformed include statement" },
        .{ "include nowhere\n", "t:1: can't open nowhere for inclusion" },
        .{ "data matches '[a-'\nplumb to edit\n", "t:1: bad regular expression" },
        .{ "data matches '([a-z]+)\\.c'\narg isfile $1\nplumb to edit\n", "t:2: $1: no submatches (the regular expressions have no capture groups): use $0 and several matches" },
    }) |c| try testing.expectEqualStrings(c[1], try reread(a, c[0]));
    // Inside quotes a `$1` is the text `$1`.
    try testing.expectEqualStrings("data\tis\t'$1'\nplumb\tto\tedit\n\n", try reread(a, "data is '$1'\nplumb to edit\n"));
    // The most variables, and ports, are bounded.
    var many: std.Io.Writer.Allocating = .init(a);
    for (0..max_vars + 1) |i| try many.writer.print("v{d}=x\n", .{i});
    try testing.expectEqualStrings("t:33: too many variables; max 32", try reread(a, many.written()));
    many.clearRetainingCapacity();
    for (0..max_ports + 1) |i| try many.writer.print("plumb to p{d}\n", .{i});
    try testing.expectEqualStrings("t:33: too many ports; max 32", try reread(a, many.written()));
}

test "plumb: rules print as rule sets of tab-separated lines, variables where they were set, and read back the same" {
    var arena: std.heap.ArenaAllocator = .init(testing.allocator);
    defer arena.deinit();
    const a = arena.allocator();
    const text =
        \\# a comment ends nothing it is not in
        \\x = '[a-z]+'
        \\plumb to seemail
        \\plumb to showmail
        \\
        \\kind is text
        \\data matches $x
        \\plumb to edit
        \\x=other
        \\type is text
        \\data set $x
        \\plumb start Msg $data
    ;
    const printed = try reread(a, text);
    try testing.expectEqualStrings("x='[a-z]+'\n\nplumb\tto\tseemail\nplumb\tto\tshowmail\n\ntype\tis\ttext\ndata\tmatches\t$x\nplumb\tto\tedit\n\nx=other\n\ntype\tis\ttext\ndata\tset\t$x\nplumb\tstart\tMsg $data\n\n", printed);
    try testing.expectEqualStrings(printed, try reread(a, printed));
    // The default rules read, and print as they read.
    const defaults = try reread(a, "include basic\n");
    try testing.expect(std.mem.indexOf(u8, defaults, "data\tmatches\t'https?://.*'\nplumb\tto\tweb\n") != null);
    try testing.expectEqualStrings(defaults, try reread(a, defaults));
}

test "plumb: rc's quoting, variables and the message's in an expansion, the first word alone" {
    var arena: std.heap.ArenaAllocator = .init(testing.allocator);
    defer arena.deinit();
    var ps: Parser = .{ .arena = arena.allocator(), .p = null, .check = true };
    try ps.feed("t", "v='a b'\nw=x$v'y'\n");
    var m: Msg = .{ .src = "pardes", .wdir = "/w", .data = "f.c" };
    var e: Exec = .{ .m = &m, .match0 = "zero" };
    var buf: [4096]u8 = undefined;
    for ([_][2][]const u8{
        .{ "'it''s' rest", "it's" },
        .{ "pre$v.post", "prea b.post" },
        .{ "$w", "xa by" },
        .{ "'$v'", "$v" },
        .{ "$nothere$", "$nothere$" },
        .{ "$src:$wdir:$data:$0:$file", "pardes:/w:f.c:zero:/w/f.c" },
    }) |c| try testing.expectEqualStrings(c[1], (try expand(&ps.rules, &e, c[0], &buf))[0]);
    e.file = "/real/f.c";
    try testing.expectEqualStrings("/real/f.c", (try expand(&ps.rules, &e, "$file", &buf))[0]);
    // Past 4 KiB: too long, never cut.
    const long = try arena.allocator().alloc(u8, 5000);
    @memset(long, 'a');
    m.data = long;
    try testing.expectError(error.TooLong, expand(&ps.rules, &e, "$data", &buf));
}

/// A send against `text`'s rules on a session of its own: the message
/// after its match, the rule set that took it (its index among the sets,
/// null for none) and what dispose said.
const Tried = struct { m: Msg, set: ?usize };

fn tryRules(p: *Pardes, text: []const u8, msg: Msg) !Tried {
    try testing.expect(apply(p, false, "plumb/rules", text) == null);
    const arena = p.scratch.allocator();
    var s: Send = .{ .p = p, .from = null, .arena = arena, .rules = try rulesOf(p, arena) };
    var m = msg;
    var e: Exec = .{ .m = &m };
    var n: usize = 0;
    for (s.rules.items.items) |item| {
        const set = switch (item) {
            .set => |x| x,
            else => continue,
        };
        e = .{ .m = &m };
        if (try s.matchSet(set, &e)) return .{ .m = m, .set = n };
        n += 1;
    }
    return .{ .m = m, .set = null };
}

test "plumb: matches takes the whole text, or with a click the run around it: Pike's horse.gift is no gif" {
    const p = try Pardes.init(testing.allocator, .{ .cols = 80, .rows = 24 });
    defer p.deinit();
    const rules =
        \\type is text
        \\data matches '[a-zA-Z0-9_\-./]+'
        \\data matches '([a-zA-Z0-9_\-./]+)\.(gif|jpe?g)'
        \\plumb to image
        \\
    ;
    try testing.expectEqual(@as(?usize, 0), (try tryRules(p, rules, .{ .type = "text", .data = "horse.gif" })).set);
    try testing.expect((try tryRules(p, rules, .{ .type = "text", .data = "horse.gift" })).set == null);
    try testing.expect((try tryRules(p, rules, .{ .type = "text", .data = "see horse.gif" })).set == null);
    // A click inside the name: the run of file characters around it, the
    // data cut to it and the click gone.
    const hit = try tryRules(p, rules, .{ .type = "text", .attr = "click=7", .data = "see horse.gif now" });
    try testing.expectEqual(@as(?usize, 0), hit.set);
    try testing.expectEqualStrings("horse.gif", hit.m.data);
    try testing.expectEqualStrings("", hit.m.attr);
    try testing.expectEqualStrings("image", hit.m.dst);
    try testing.expect((try tryRules(p, rules, .{ .type = "text", .attr = "click=7", .data = "see horse.gift now" })).set == null);
    // The click counts runes: two bytes of é before it.
    try testing.expectEqualStrings("horse.gif", (try tryRules(p, rules, .{ .type = "text", .attr = "click=3", .data = "é horse.gif" })).m.data);
    // Each object a rule matches whole.
    const src = "src\tmatches\t'par.*'\ntype\tmatches\t'te(x|y)t'\nplumb\tto\tedit\n";
    try testing.expectEqual(@as(?usize, 0), (try tryRules(p, src, .{ .src = "pardes", .type = "text" })).set);
    try testing.expect((try tryRules(p, src, .{ .src = "pardes", .type = "texts" })).set == null);
    // The default rules' URL: http and https, whole.
    try testing.expectEqual(@as(?usize, 0), (try tryRules(p, basic, .{ .type = "text", .data = "https://ziglang.org/x?y" })).set);
    try testing.expect((try tryRules(p, basic, .{ .type = "text", .data = "see https://ziglang.org" })).set == null);
}

test "plumb: a set rewrites the message for good, attr delete of one absent fails, a message to a port tries only rule sets to it" {
    const p = try Pardes.init(testing.allocator, .{ .cols = 80, .rows = 24 });
    defer p.deinit();
    const rules =
        \\data set changed
        \\type is never
        \\plumb to a
        \\
        \\attr delete action
        \\attr add 'action=showdiff filename=x'
        \\plumb to b
        \\
        \\data is changed
        \\plumb to c
        \\
    ;
    // The first set fails after its `set`: the data stays changed; the
    // second fails at its delete; the third sees the changed data.
    const t = try tryRules(p, rules, .{ .type = "text", .data = "orig" });
    try testing.expectEqual(@as(?usize, 2), t.set);
    try testing.expectEqualStrings("changed", t.m.data);
    try testing.expectEqualStrings("c", t.m.dst);
    const d = try tryRules(p, rules, .{ .type = "text", .attr = "action=showdata", .data = "x" });
    try testing.expectEqual(@as(?usize, 1), d.set);
    try testing.expectEqualStrings("action=showdiff filename=x", d.m.attr);
    // dst names a port: rule sets to another are passed over.
    const to_a = try tryRules(p, "data\tis\tx\nplumb\tto\ta\n\ndata\tis\tx\nplumb\tto\tb\n\n", .{ .dst = "b", .data = "x" });
    try testing.expectEqual(@as(?usize, 1), to_a.set);
}

test "plumb: isfile and isdir find a name as Look does, past its place, in the message's directory then Incl's; $file is where" {
    if (!fs.platform_has_fs) return;
    var tmp = testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(testing.io, .{ .sub_path = "it.zig", .data = "x\n" });
    try tmp.dir.createDir(testing.io, "sub", .default_dir);
    var dir_buf: [4096]u8 = undefined;
    const root = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
    const p = try Pardes.init(testing.allocator, .{ .cols = 80, .rows = 24 });
    defer p.deinit();
    const rules = "arg\tisfile\t$data\ndata\tset\t$file\nplumb\tto\tedit\n\narg\tisdir\t$data\ndata\tset\t$dir\nplumb\tto\tedit\n\n";
    const file = try tryRules(p, rules, .{ .wdir = root, .data = "it.zig:3:2" });
    try testing.expectEqual(@as(?usize, 0), file.set);
    try testing.expect(std.mem.endsWith(u8, file.m.data, "/it.zig"));
    const dir = try tryRules(p, rules, .{ .wdir = root, .data = "sub" });
    try testing.expectEqual(@as(?usize, 1), dir.set);
    try testing.expect(std.mem.endsWith(u8, dir.m.data, "/sub"));
    try testing.expect((try tryRules(p, rules, .{ .wdir = root, .data = "nothere.zig" })).set == null);
    // Not in wdir: Incl's directories, as acme's includename.
    try p.incl.append(p.gpa, try p.gpa.dupe(u8, root));
    try testing.expectEqual(@as(?usize, 0), (try tryRules(p, rules, .{ .wdir = "/", .data = "it.zig" })).set);
    try testing.expect((try tryRules(p, rules, .{ .wdir = "/", .data = "./it.zig" })).set == null);
}

test "plumb: dispose: no rule and no dst, a start, the web port, a port no rule names" {
    const p = try Pardes.init(testing.allocator, .{ .cols = 80, .rows = 24 });
    defer p.deinit();
    _ = try p.setTestFile("x\n");
    try testing.expect(apply(p, false, "plumb/rules", "include basic\n\ntype\tis\tmsg\nplumb\tstart\tMsg $data\n\ntype\tis\tnone\nplumb\tto\tnobody\n\ntype\tis\tclient\nplumb\tclient\tMsg x\n\n") == null);
    try testing.expectEqualStrings(e_no_port, send(p, null, .{ .type = "text", .data = "no such thing" }, null).refused);
    // A start of a builtin runs it in place.
    try testing.expect(send(p, null, .{ .type = "msg", .data = "hello" }, null) == .done);
    const pane = p.panes[p.view().active].?;
    try testing.expectEqualStrings("hello", pane.msg[0..pane.msg_len]);
    try testing.expectEqualStrings(e_client, send(p, null, .{ .type = "client" }, null).refused);
    try testing.expectEqualStrings(e_no_start, send(p, null, .{ .type = "none" }, null).refused);
    try testing.expectEqualStrings(e_no_start, send(p, null, .{ .dst = "nobody", .type = "x" }, null).refused);
    // A port no rule names goes nowhere, said nothing (fsys.c's dispose).
    try testing.expect(send(p, null, .{ .dst = "elsewhere", .data = "x" }, null) == .done);
    // A URL to the web port: the host's opener.
    while (p.nextEffect()) |_| {}
    try testing.expect(send(p, null, .{ .type = "text", .data = "https://ziglang.org" }, null) == .done);
    var linked = false;
    while (p.nextEffect()) |e| switch (e) {
        .open_link => |u| {
            try testing.expectEqualStrings("https://ziglang.org", u.slice());
            linked = true;
        },
        else => {},
    };
    try testing.expect(linked);
}

test "plumb: rules written replace or add, a refusal keeps them as they were, and a dump keeps them" {
    const p = try Pardes.init(testing.allocator, .{ .cols = 80, .rows = 24 });
    defer p.deinit();
    // Every session starts with the defaults.
    try testing.expect(std.mem.indexOf(u8, inForce(p), "plumb\tto\tweb") != null);
    try testing.expect(!p.plumber.written);
    try testing.expect(apply(p, false, "plumb/rules", "type is a\nplumb to edit\n") == null);
    try testing.expect(apply(p, true, "plumb/rules", "type is b\nplumb to edit\n") == null);
    try testing.expectEqualStrings("type\tis\ta\nplumb\tto\tedit\n\ntype\tis\tb\nplumb\tto\tedit\n\n", p.plumber.text.items);
    try testing.expectEqualStrings("plumb/rules:1: unknown verb isnt", apply(p, true, "plumb/rules", "type isnt c\n").?);
    try testing.expectEqualStrings("type\tis\ta\nplumb\tto\tedit\n\ntype\tis\tb\nplumb\tto\tedit\n\n", p.plumber.text.items);
    try pardes.dump.dumpState(p);
    const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon");
    defer restored.deinit();
    try testing.expectEqualStrings(p.plumber.text.items, restored.plumber.text.items);
    try testing.expect(restored.plumber.written);
}

test "plumb: plan9port's basic parses, its ¡-￿ classes UTF-8's bytes and its submatch rule sets left out, and its rules match" {
    var arena: std.heap.ArenaAllocator = .init(testing.allocator);
    defer arena.deinit();
    // plan9port's plumb/basic and fileaddr; `¡-￿` is `\x80-\xf4`, the bytes
    // of UTF-8's multi-byte runes (a run of them is whole runes: UTF-8 is
    // self-synchronising), and the rule sets that need `$1`-`$5` are gone.
    const p9p =
        \\addrelem='((#?[0-9]+)|(/[A-Za-z0-9_\^]+/?)|[.$])'
        \\addr=:($addrelem([,;+\-]$addrelem)*)
        \\
        \\plumb to seemail
        \\plumb to showmail
        \\
        \\type is text
        \\data matches '(https?|ftp|file|gopher|mailto|news|nntp|telnet|wais|prospero)://[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-]+)*/?[a-zA-Z0-9_?,%#~&/\-+=]+([:.][a-zA-Z0-9_?,%#~&/\-+=]+)*'
        \\plumb to web
        \\plumb start web $0
        \\
        \\type is text
        \\data matches '[a-zA-Z\x80-\xf40-9_\-./]+'
        \\data matches '([a-zA-Z\x80-\xf40-9_\-./]+)\.(doc|rtf)'
        \\arg isfile $0
        \\plumb to msword
        \\plumb start wdoc2txt $file
        \\
        \\type is text
        \\dst is msword
        \\plumb to msword
        \\plumb start wdoc2txt $file
        \\
        \\type is text
        \\data matches '[a-zA-Z\x80-\xf40-9_\-./@]+'
        \\data matches '([a-zA-Z\x80-\xf40-9_\-./@]+)\.(jpe?g|JPE?G|gif|GIF|tiff?|TIFF?|ppm|bit|png|PNG)'
        \\arg isfile $0
        \\plumb to image
        \\plumb start 9 page $file
        \\
        \\type is text
        \\data matches '[a-zA-Z\x80-\xf40-9_\-./@]+'
        \\data matches '([a-zA-Z\x80-\xf40-9_\-./@]+)\.(ps|PS|eps|EPS|pdf|PDF|dvi|DVI)'
        \\arg isfile $0
        \\plumb to postscript
        \\plumb start 9 page $file
        \\
        \\data matches '[a-zA-Z\x80-\xf40-9_\-./@]+'
        \\data matches '([a-zA-Z\x80-\xf40-9_\-./@]+)\.([Ss][XxTt][CcDdIiGgMmWw]|[Dd][Oo][Cc]|[Xx][Ll][Ss]|[Pp][Pp][Tt])'
        \\arg isfile $0
        \\plumb to openoffice
        \\plumb start openoffice $file
        \\
        \\type is text
        \\data matches '[a-zA-Z\x80-\xf40-9_\-./]+'
        \\data matches '/mail/fs/[a-zA-Z\x80-\xf40-9/]+/[0-9]+'
        \\plumb to showmail
        \\plumb start window -r 4 120 750 600 upas/nedmail -s $0
        \\
        \\type is text
        \\data matches '[a-zA-Z0-9_+.\-]+@[a-zA-Z0-9_+.\-]*'
        \\plumb to sendmail
        \\plumb start wmail $0
        \\
        \\dst is image
        \\arg isfile $data
        \\plumb to image
        \\plumb start 9 page $data
        \\
        \\dst is postscript
        \\arg isfile $data
        \\plumb start 9 page $data
        \\
    ;
    const printed = try reread(arena.allocator(), p9p);
    try testing.expect(std.mem.startsWith(u8, printed, "addrelem='((#?[0-9]+)|(/[A-Za-z0-9_\\^]+/?)|[.$])'\n\naddr=:($addrelem([,;+\\-]$addrelem)*)\n\nplumb\tto\tseemail\n"));
    try testing.expectEqualStrings(printed, try reread(arena.allocator(), printed));
    const p = try Pardes.init(testing.allocator, .{ .cols = 80, .rows = 24 });
    defer p.deinit();
    // Clicked in a line: the URL's run, the mail address's, and the
    // faces' path, whole runes of a name that is not ASCII.
    const url = try tryRules(p, p9p, .{ .type = "text", .attr = "click=10", .data = "see https://9fans.github.io/plan9port/ now" });
    try testing.expectEqual(@as(?usize, 0), url.set);
    try testing.expectEqualStrings("https://9fans.github.io/plan9port/", url.m.data);
    try testing.expectEqualStrings("web", url.m.dst);
    const mail = try tryRules(p, p9p, .{ .type = "text", .attr = "click=12", .data = "write to [email protected]." });
    try testing.expectEqualStrings("sendmail", mail.m.dst);
    try testing.expectEqualStrings("[email protected].", mail.m.data);
    const face = try tryRules(p, p9p, .{ .type = "text", .attr = "click=3", .data = "x /mail/fs/café/12 y" });
    try testing.expectEqualStrings("showmail", face.m.dst);
    try testing.expectEqualStrings("/mail/fs/café/12", face.m.data);
    // An image named in UTF-8, and Pike's horse.gift, by the image set's
    // two patterns alone.
    const pics = "data matches '[a-zA-Z\\x80-\\xf40-9_\\-./@]+'\ndata matches '([a-zA-Z\\x80-\\xf40-9_\\-./@]+)\\.(jpe?g|gif|png)'\nplumb to image\n";
    try testing.expectEqualStrings("Ångström.png", (try tryRules(p, pics, .{ .attr = "click=2", .data = "Ångström.png" })).m.data);
    try testing.expect((try tryRules(p, pics, .{ .attr = "click=2", .data = "horse.gift" })).set == null);
    // The rune range as written is refused, in regexp.zig's words.
    try testing.expectEqualStrings("t:1: bad regular expression: a range of runes wider than 256 in [...] is not supported", try reread(arena.allocator(), "data matches '[a-zA-Z¡-￿0-9_\\-./]+'\nplumb to edit\n"));
}

test "plumb: the Plumb word opens the plumbing file, include basic until one is saved; saved, or read at boot, its rules are in force, a bad one said with its line" {
    if (!fs.platform_has_fs) return;
    var tmp = testing.tmpDir(.{});
    defer tmp.cleanup();
    var dir_buf: [4096]u8 = undefined;
    const root = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
    const p = try Pardes.init(testing.allocator, .{ .cols = 100, .rows = 30, .config_dir = root });
    defer p.deinit();
    _ = try p.setTestFile("x\n");
    var path_buf: [4096]u8 = undefined;
    const path = filePath(p, &path_buf).?;
    try testing.expect(std.mem.endsWith(u8, path, "/plumbing"));
    // Not there yet: a pane of its name holding `include basic`.
    try testing.expect(p.executeBuiltinLine(0, "Plumb"));
    const made = p.panes[p.view().active].?;
    try testing.expectEqualStrings(path, made.file.?.path);
    try testing.expectEqualStrings("include basic\n", made.file.?.content);
    // A rule after the word is refused, in words.
    try testing.expect(p.executeBuiltinLine(0, "Plumb suffix .svg to image"));
    const first = p.panes[0].?;
    try testing.expectEqualStrings(e_word, first.msg[0..first.msg_len]);
    // Saved: its rules, or why not, the rules as they were.
    saved(p, 0, path, "type is text\ndata matches 'x.*'\nplumb to web\n");
    try testing.expectEqualStrings("type\tis\ttext\ndata\tmatches\t'x.*'\nplumb\tto\tweb\n\n", inForce(p));
    saved(p, 0, path, "type is text\ndata isnt x\nplumb to web\n");
    try testing.expectEqualStrings("plumbing:2: unknown verb isnt", first.msg[0..first.msg_len]);
    try testing.expectEqualStrings("type\tis\ttext\ndata\tmatches\t'x.*'\nplumb\tto\tweb\n\n", inForce(p));
    // Another file saved is no plumbing.
    saved(p, 0, "/elsewhere/plumbing", "type is text\nplumb to edit\n");
    try testing.expect(std.mem.indexOf(u8, inForce(p), "'x.*'") != null);
    // At boot: the file's rules.
    try tmp.dir.writeFile(testing.io, .{ .sub_path = "plumbing", .data = "type is text\ndata matches 'y.*'\nplumb to web\n" });
    const q = try Pardes.init(testing.allocator, .{ .cols = 100, .rows = 30, .config_dir = root });
    defer q.deinit();
    try testing.expectEqualStrings("type\tis\ttext\ndata\tmatches\t'y.*'\nplumb\tto\tweb\n\n", inForce(q));
}