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
|
//! Event streams: per-pane `event` records, the editor-wide `log`, and the
//! bounded queues that park a read until something happens.
const std = @import("std");
const pardes = @import("../pardes.zig");
const exec = @import("../exec.zig");
const look = @import("../look.zig");
const cloud9 = @import("cloud9");
const tree = @import("tree.zig");
const pane_files = @import("pane.zig");
const dump = @import("../dump.zig");
const Pardes = pardes.Pardes;
const Pane = pardes.Pane;
const MAX_PANES = pardes.MAX_PANES;
const Req = tree.Req;
const Reply = tree.Reply;
const E = tree.E;
pub const queue_cap = 64 * 1024;
/// Wall-clock seconds, or zero where the platform has no clock.
pub fn now() u32 {
if (comptime !pardes.hosted) return 0;
var ts: std.c.timespec = undefined;
if (std.c.clock_gettime(.REALTIME, &ts) != 0) return 0;
return std.math.cast(u32, ts.sec) orelse 0;
}
pub const Queue = struct {
buf: std.ArrayList(u8) = .empty,
head: usize = 0,
cap: usize = queue_cap,
/// Records pushed out of the front to make room: the sequence number of
/// the oldest one still held.
dropped: u64 = 0,
pub fn deinit(q: *Queue, gpa: std.mem.Allocator) void {
q.buf.deinit(gpa);
q.head = 0;
}
pub fn push(q: *Queue, gpa: std.mem.Allocator, record: []const u8) void {
if (record.len > std.math.maxInt(u32)) return;
while (q.buf.items.len - q.head + record.len + 4 > q.cap) {
if (q.peek() == null) return;
q.pop();
q.dropped += 1;
}
q.compact();
var head: [4]u8 = undefined;
std.mem.writeInt(u32, &head, @intCast(record.len), .little);
q.buf.appendSlice(gpa, &head) catch return;
q.buf.appendSlice(gpa, record) catch {
q.buf.shrinkRetainingCapacity(q.buf.items.len - 4);
return;
};
}
pub fn peek(q: *const Queue) ?[]const u8 {
const rest = q.buf.items[@min(q.head, q.buf.items.len)..];
if (rest.len < 4) return null;
const len = std.mem.readInt(u32, rest[0..4], .little);
if (rest.len < 4 + len) return null;
return rest[4 .. 4 + len];
}
pub fn pop(q: *Queue) void {
const record = q.peek() orelse return;
q.head += 4 + record.len;
if (q.head == q.buf.items.len) {
q.buf.clearRetainingCapacity();
q.head = 0;
}
}
pub fn popFront(q: *Queue, n: usize) void {
const record = q.peek() orelse return;
if (n >= record.len) return q.pop();
q.head += n;
std.mem.writeInt(u32, q.buf.items[q.head..][0..4], @intCast(record.len - n), .little);
}
fn compact(q: *Queue) void {
if (q.head == 0 or q.head * 2 < q.buf.items.len) return;
const rest = q.buf.items.len - q.head;
std.mem.copyForwards(u8, q.buf.items[0..rest], q.buf.items[q.head..]);
q.buf.shrinkRetainingCapacity(rest);
q.head = 0;
}
pub fn empty(q: *const Queue) bool {
return q.peek() == null;
}
pub fn clearAndFree(q: *Queue, gpa: std.mem.Allocator) void {
q.buf.clearAndFree(gpa);
q.head = 0;
}
};
/// One record per read; `.again` holds the read until a record arrives.
pub fn readQueue(p: *Pardes, req: Req, q: *Queue) Reply {
const record = q.peek() orelse return .{ .tag = req.tag, .status = .again };
if (req.size < record.len) return tree.failText(req.tag, E.INVAL, "invalid read: shorter than the record waiting");
const out = p.fs.stage(p.gpa);
out.appendSlice(p.gpa, record) catch return Reply.fail(req.tag, E.NOMEM);
q.pop();
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
}
// ---- the editor-wide log ----
/// `unsaved`: a pane a refusal (Exit, Restore, Del, Delcol) is about,
/// logged before the write's err so a script reads which, whole.
pub const LogKind = enum { new, del, rename, save, unsaved };
/// A pane leaving, before its state goes with it; one never announced
/// leaves silently.
pub fn noteRetire(p: *Pardes, pane: *Pane) void {
tree.pty.shellGone(p, pane, false);
p.fs.news = true; // a read held on its event or pty/data hears it went
p.fs.listeners -|= pane.fs.readers;
if (pane.fs.unannounced) {
pane.fs.unannounced = false;
return;
}
// A question still standing ends with it, so a follower of its `ask`
// hears the end.
if (pane.prompt == .repl_choice or pane.prompt == .del_side or pane.prompt == .save) {
var rec: [32]u8 = undefined;
notePath(p, "answer", std.fmt.bufPrint(&rec, "{d} -", .{pane.serial}) catch "");
}
noteLog(p, .del, pane);
}
/// Called at the end of every update: reports the panes installed by it.
pub fn announce(p: *Pardes) void {
for (p.panes) |slot| {
const pane = slot orelse continue;
if (!pane.fs.unannounced) continue;
pane.fs.unannounced = false;
noteLog(p, .new, pane);
// A file opened (a look, a Restore, a boot): first in Recent.
if (pane.file) |f| if (f.output == null and pardes.Pardes.recentKeeps(f.path)) p.recent.opened(p.recentGpa(), f.path);
}
}
/// The panes present at boot are the log's first records: something said
/// before the first update ends (an init file's notice) waits for them.
fn bootFirst(p: *Pardes) void {
if (newest(p) == null) announce(p);
}
/// Records `<kind> <serial> <name>`.
pub fn noteLog(p: *Pardes, kind: LogKind, pane: *Pane) void {
var buf: [4 * 4096 + 64]u8 = undefined;
var name_buf: [4 * 4096]u8 = undefined;
// As /index shows it: a newline in the name is `\n`.
const name = shown(pane_files.nameOf(pane), &name_buf);
pushLog(p, std.fmt.bufPrint(&buf, "{s} {d} {s}\n", .{ @tagName(kind), pane.serial, name }) catch return);
}
/// Records `msg <serial> <text>` for what the editor said, `-` for no pane.
/// Text past LoggedMessage.cap bytes is cut between words with an ellipsis,
/// as an err record's reason is.
pub fn noteMessage(p: *Pardes, serial: u32, said: []const u8) void {
var buf: [pardes.Messages.LoggedMessage.cap + 32]u8 = undefined;
const kept = pardes.Messages.clip(said, pardes.Messages.LoggedMessage.cap);
const cut = if (kept.len < said.len) "…" else "";
bootFirst(p);
pushCounting(p, (if (serial == 0)
std.fmt.bufPrint(&buf, "msg - {s}{s}\n", .{ kept, cut })
else
std.fmt.bufPrint(&buf, "msg {d} {s}{s}\n", .{ serial, kept, cut })) catch return, said);
}
/// Records `err <serial|-> <file>: <why>` for a write that was refused or
/// failed: through a mount a shell sees only the errno the
/// kernel mapped the reply to (`Invalid argument`), and here is the reason.
/// The log is the one place for it, as acme's `errors` file takes text and
/// answers nothing: a per-pane readable error file would be a second.
/// `dump <path>` when a Dump is written, `restore <path>` first in a
/// Restore's replacement.
/// `notePath` for an `err` record: the same one again straight after is
/// counted, `(x2)`, as every err is.
pub fn noteErrCounted(p: *Pardes, text: []const u8) void {
bootFirst(p);
var buf: [pardes.memory.limits.host_path_cap + 16]u8 = undefined;
pushCounting(p, std.fmt.bufPrint(&buf, "err {s}\n", .{text}) catch return, text);
}
pub fn notePath(p: *Pardes, what: []const u8, path: []const u8) void {
bootFirst(p);
var buf: [pardes.memory.limits.host_path_cap + 16]u8 = undefined;
pushLog(p, std.fmt.bufPrint(&buf, "{s} {s}\n", .{ what, path }) catch return);
}
pub fn noteError(p: *Pardes, req: Req, reply: Reply) void {
const why = if (reply.ename.len > 0) reply.ename else cloud9.fs.errString(reply.errno);
var name: [32]u8 = undefined;
var serial: []const u8 = "-";
var file: []const u8 = "os";
if (req.node & tree.sources.archive_node != 0) file = "src";
if (req.node != pardes.filesystem.os_root and req.node & pardes.filesystem.os_node == 0 and req.node & tree.sources.archive_node == 0) {
if (tree.Node.target(req.node)) |t| switch (t) {
.top => |f| {
file = f.fileName();
// The root's exec or look ran at the keyboard's pane: its
// failure is that pane's, as a click there would be.
if ((f == .exec or f == .look) and p.fs.ran_at != 0)
serial = std.fmt.bufPrint(&name, "{d}", .{p.fs.ran_at}) catch "-";
},
.col => |c| {
serial = "-";
file = (if (c.file == .dir)
std.fmt.bufPrint(&name, "col/{d}", .{c.serial})
else
std.fmt.bufPrint(&name, "col/{d}/{s}", .{ c.serial, @tagName(c.file) })) catch "col";
},
.pane => |pt| {
serial = std.fmt.bufPrint(&name, "{d}", .{pt.serial}) catch "-";
file = switch (pt.file) {
.pty_ctl => "pty/ctl",
.pty_status => "pty/status",
.pty_data => "pty/data",
.pty_run => "pty/run",
else => pt.file.fileName(),
};
},
};
}
var buf: [256]u8 = undefined;
// A reason that names its file already (`look: no space...` from a
// look) is not prefixed twice: `err 3 look: no space...`.
const reason = if (why.len > file.len + 2 and std.ascii.startsWithIgnoreCase(why, file) and std.mem.startsWith(u8, why[file.len..], ": ")) why[file.len + 2 ..] else why;
// A long reason is cut between words, with an ellipsis, never mid-word.
const kept = pardes.Messages.clip(reason, 200);
const record = std.fmt.bufPrint(&buf, "err {s} {s}: {s}{s}\n", .{ serial, file, kept, if (kept.len < reason.len) "…" else "" }) catch return;
for (record[0 .. record.len - 1]) |*c| if (c.* < ' ') {
c.* = ' ';
};
pushCounting(p, record, reason);
}
/// Drops the newest record when it is a msg saying `text` (of any pane)
/// that no follower has read: a failure its write's err record will say.
pub fn dropMessage(p: *Pardes, text: []const u8) void {
const last = newest(p) orelse return;
if (followerRead(p, last.seq) or !std.mem.startsWith(u8, last.text, "msg ")) return;
const rest = last.text["msg ".len..];
const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse return;
if (std.mem.eql(u8, rest[sp + 1 ..], text)) p.fs.log.buf.shrinkRetainingCapacity(last.at);
}
/// Pushes `record`, or, when it repeats the newest record word for word,
/// counts that one instead: `<record> (x2)`, the count being every time it
/// was said. One a follower has already read is not rewritten: the repeat
/// is a new line carrying the running count, `(x3)`.
/// `full` is the text before any cut: two records that agree only in what
/// was cut to fit are no repeat (`fs.log_full_hash`).
fn pushCounting(p: *Pardes, raw: []u8, full: []const u8) void {
const full_hash = std.hash.Wyhash.hash(0, full);
defer p.fs.log_full_hash = full_hash;
// A client retrying a write that fails the same way would fill the ring
// with one line, so a repeat of the newest record is that record counted,
// `(x3)`, as +Messages counts its repeats; unless a follower has read it
// already and so waits on the repeat as a line of its own.
var clean: [4 * 4096 + 256]u8 = undefined;
const record = sanitize(raw, &clean);
const last = newest(p) orelse return pushLog(p, record);
var said = last.text;
var times: u32 = 1;
if (std.mem.endsWith(u8, said, ")")) if (std.mem.lastIndexOf(u8, said, " (x")) |x| {
if (std.fmt.parseInt(u32, said[x + 3 .. said.len - 1], 10)) |n| {
times = n;
said = said[0..x];
} else |_| {}
};
if (!std.mem.eql(u8, said, record[0 .. record.len - 1]) or p.fs.log_full_hash != full_hash) return pushLog(p, record);
var counted: [pardes.Messages.LoggedMessage.cap + 64]u8 = undefined;
const again = std.fmt.bufPrint(&counted, "{s} (x{d})\n", .{ said, times +| 1 }) catch return;
if (!followerRead(p, last.seq)) p.fs.log.buf.shrinkRetainingCapacity(last.at);
pushLog(p, again);
}
/// The newest record in the ring: where it starts, its sequence number and
/// its text without the newline.
fn newest(p: *Pardes) ?struct { at: usize, seq: u64, text: []const u8 } {
const q = &p.fs.log;
var at = q.head;
var last: ?usize = null;
var seq = q.dropped;
while (at + 4 <= q.buf.items.len) : (seq += 1) {
last = at;
at += 4 + std.mem.readInt(u32, q.buf.items[at..][0..4], .little);
}
const last_at = last orelse return null;
const text = q.buf.items[last_at + 4 ..];
return .{ .at = last_at, .seq = seq - 1, .text = text[0 .. text.len - 1] };
}
/// Whether a follower has read the record `seq`, or is part way through it.
fn followerRead(p: *Pardes, seq: u64) bool {
for (p.fs.opens) |o| if (o.node != 0 and o.what == .log) {
const l = o.what.log;
if (l.follow and (l.next > seq or (l.next == seq and l.part > 0))) return true;
};
return false;
}
/// The log is one ring that records whether or not anyone reads it. A record
/// is one line: a newline in a message or a name would read as two records.
/// A record as one line of text (shown): the record's own newline, last,
/// is kept.
fn sanitize(record: []const u8, out: []u8) []u8 {
// A newline in a message is a space (its words go on); a name's own
// newline is escaped before it gets here (noteLog).
const w = shownAs(record[0 .. record.len - 1], out[0 .. out.len - 1], false).len;
out[w] = '\n';
return out[0 .. w + 1];
}
/// `text` as one line of UTF-8 a reader can split and decode, as /log and
/// /index show names: a backslash is `\\`, a newline `\n` (a name with one
/// stays readable as what it is, not run into the next), any other control
/// character, DEL or C1 control (U+0080-U+009F) a space, and a byte that
/// is not UTF-8 `\xNN`. `out` of 4 bytes a byte of `text` holds it all.
pub fn shown(text: []const u8, out: []u8) []u8 {
return shownAs(text, out, true);
}
fn shownAs(text: []const u8, out: []u8, escape_newline: bool) []u8 {
var w: usize = 0;
var i: usize = 0;
const body = text;
while (i < body.len and w + 4 <= out.len) {
const c = body[i];
// A name's own backslash is `\\`, so `\n` and `\xNN` read back as
// the escapes they are, never as a name that held them.
if (c == '\\' and escape_newline) {
@memcpy(out[w..][0..2], "\\\\");
w += 2;
i += 1;
continue;
}
if (c == '\n' and escape_newline) {
@memcpy(out[w..][0..2], "\\n");
w += 2;
i += 1;
continue;
}
if (c < ' ' or c == 0x7f) {
out[w] = ' ';
w += 1;
i += 1;
continue;
}
const n = std.unicode.utf8ByteSequenceLength(c) catch 0;
if (n == 0 or i + n > body.len or !std.unicode.utf8ValidateSlice(body[i .. i + n])) {
_ = std.fmt.bufPrint(out[w..], "\\x{x:0>2}", .{c}) catch break;
w += 4;
i += 1;
continue;
}
if (n == 2 and c == 0xC2 and body[i + 1] <= 0x9F) {
out[w] = ' ';
w += 1;
i += 2;
continue;
}
if (w + n > out.len) break;
@memcpy(out[w..][0..n], body[i..][0..n]);
w += n;
i += n;
}
return out[0..w];
}
fn pushLog(p: *Pardes, raw: []u8) void {
// A record not counted: the next one is no repeat of a counted one.
p.fs.log_full_hash = 0;
var clean: [4 * 4096 + 256]u8 = undefined;
const record = sanitize(raw, &clean);
// One record larger than the ring would push every other out and then
// not fit itself; cut it to what fits instead, on a character boundary.
var end = @min(record.len, p.fs.log.cap - 4) - 1;
while (end > 0 and record[end] & 0xC0 == 0x80) end -= 1;
record[end] = '\n';
p.fs.log.push(p.gpa, record[0 .. end + 1]);
p.fs.news = true;
}
/// What an open of /log holds.
pub const LogOpen = struct {
/// The ring's text when it was opened.
bytes: []const u8 = &.{},
/// This open waits for records after `bytes`, from `next`.
follow: bool = false,
next: u64 = 0,
/// How much of record `next` a short read already took.
part: u32 = 0,
/// How much of `bytes` this open has read. Reads walk it as a stream once
/// following and ignore their offset, since `exec 3<>log; echo follow
/// >&3` leaves the shared offset past the word it wrote.
pos: u64 = 0,
};
/// An open freezes the ring's text, so `cat log` answers what happened lately
/// and ends; `next` is where a follower picks up after it.
pub fn freezeLog(p: *Pardes) !LogOpen {
var text: std.ArrayList(u8) = .empty;
errdefer text.deinit(p.gpa);
var at = p.fs.log.head;
const items = p.fs.log.buf.items;
var n: u64 = 0;
while (at + 4 <= items.len) : (n += 1) {
const len = std.mem.readInt(u32, items[at..][0..4], .little);
try text.appendSlice(p.gpa, items[at + 4 ..][0..len]);
at += 4 + len;
}
return .{ .bytes = try text.toOwnedSlice(p.gpa), .next = p.fs.log.dropped + n };
}
/// Past the frozen text, a following open reads newer records one per read,
/// held until one arrives; one that fell behind the ring hears `lost N`.
pub fn readLog(p: *Pardes, req: Req) Reply {
const slot = &(tree.openOf(p, req) orelse return Reply.fail(req.tag, E.INVAL)).what.log;
const frozen = slot.bytes;
const out = p.fs.stage(p.gpa);
if (!slot.follow or slot.pos < frozen.len) {
// Until `follow`, the frozen text is a file like any other: `tail -n`
// seeks near its end, `less` seeks back. After it, a stream.
const off = @min(if (slot.follow) slot.pos else req.off, frozen.len);
const len = @min(frozen.len - off, req.size);
out.appendSlice(p.gpa, frozen[off..][0..len]) catch return Reply.fail(req.tag, E.NOMEM);
slot.pos = off + len;
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(len) } };
}
const q = &p.fs.log;
if (slot.next < q.dropped) {
out.print(p.gpa, "lost {d}\n", .{q.dropped - slot.next}) catch return Reply.fail(req.tag, E.NOMEM);
slot.next = q.dropped;
slot.part = 0;
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
}
// ponytail: walks the ring from its oldest record; it holds at most log_bytes.
var at = q.head;
var seq = q.dropped;
while (at + 4 <= q.buf.items.len) : (seq += 1) {
const len = std.mem.readInt(u32, q.buf.items[at..][0..4], .little);
if (seq == slot.next) {
// A short read (a shell's `read` takes a few bytes at a time)
// gets the record in pieces, as pty/data does.
const rest = q.buf.items[at + 4 + slot.part ..][0 .. len - slot.part];
const n = @min(rest.len, req.size);
out.appendSlice(p.gpa, rest[0..n]) catch return Reply.fail(req.tag, E.NOMEM);
slot.part += @intCast(n);
if (slot.part == len) {
slot.next += 1;
slot.part = 0;
}
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } };
}
at += 4 + len;
}
return .{ .tag = req.tag, .status = .again };
}
/// `follow`, written to an open of the log, makes that open wait for newer
/// records instead of ending; closing it is the way back.
pub fn writeLog(p: *Pardes, req: Req) Reply {
const slot = &(tree.openOf(p, req) orelse return Reply.fail(req.tag, E.INVAL)).what.log;
const word = std.mem.trim(u8, req.data, " \t\r\n");
// `follow` reads what the open froze, then waits; `follow new` skips
// what was there and waits for what comes after, as tail -n0 -f does.
if (std.mem.eql(u8, word, "follow new")) {
slot.pos = slot.bytes.len;
} else if (!std.mem.eql(u8, word, "follow")) return tree.failText(req.tag, E.INVAL, "invalid write to log: it takes `follow` or `follow new`");
slot.follow = true;
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
}
/// The length a stat reports: the text an open would freeze now.
pub fn logLen(q: *const Queue) u64 {
var n: u64 = 0;
var at = q.head;
while (at + 4 <= q.buf.items.len) {
const len = std.mem.readInt(u32, q.buf.items[at..][0..4], .little);
n += len;
at += 4 + len;
}
return n;
}
// ---- per-pane event records ----
pub const max_record_text = 256;
pub const Action = enum(u8) {
body_delete = 'D',
tag_delete = 'd',
body_insert = 'I',
tag_insert = 'i',
body_look = 'L',
tag_look = 'l',
body_exec = 'X',
tag_exec = 'x',
pub fn char(a: Action) u8 {
return @intFromEnum(a);
}
pub fn fromChar(c: u8) ?Action {
return std.enums.fromInt(Action, c);
}
pub fn onTag(a: Action) bool {
return @intFromEnum(a) >= 'a';
}
};
pub const flag_builtin: u32 = 1;
pub const flag_expansion: u32 = 2;
pub const flag_filename: u32 = 4;
pub const flag_chorded: u32 = 8;
pub fn formatRecord(
buf: []u8,
origin: u8,
action: Action,
q0: u32,
q1: u32,
flag: u32,
text: []const u8,
) []const u8 {
const sent = if (text.len >= max_record_text) text[0..0] else text;
return std.fmt.bufPrint(buf, "{c}{c}{d} {d} {d} {d} {s}\n", .{
origin,
action.char(),
q0,
q1,
flag,
sent.len,
sent,
}) catch buf[0..0];
}
pub const Span = struct { at: u32, removed: u32, inserted: u32 };
pub fn diffSpan(old: []const u8, new: []const u8) Span {
const both = @min(old.len, new.len);
const stride = 64;
var head: usize = 0;
while (head + stride <= both and
std.mem.eql(u8, old[head..][0..stride], new[head..][0..stride])) head += stride;
while (head < both and old[head] == new[head]) head += 1;
var tail: usize = 0;
const rest = both - head;
while (tail + stride <= rest and std.mem.eql(
u8,
old[old.len - tail - stride ..][0..stride],
new[new.len - tail - stride ..][0..stride],
)) tail += stride;
while (tail < rest and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1;
return .{
.at = @intCast(head),
.removed = @intCast(old.len - tail - head),
.inserted = @intCast(new.len - tail - head),
};
}
pub fn noteReplace(p: *Pardes, id: usize, on_tag: bool, old: []const u8, new: []const u8) void {
const pane = p.panes[id] orelse return;
if (pane.fs.readers == 0) return;
const span = diffSpan(old, new);
if (span.removed == 0 and span.inserted == 0) return;
if (span.removed > 0) _ = noteAction(
p,
id,
if (on_tag) .tag_delete else .body_delete,
span.at,
span.at + span.removed,
0,
"",
);
if (span.inserted > 0) _ = noteAction(
p,
id,
if (on_tag) .tag_insert else .body_insert,
span.at,
span.at + span.inserted,
0,
new[span.at..][0..span.inserted],
);
}
pub fn noteAction(
p: *Pardes,
id: usize,
action: Action,
q0: u32,
q1: u32,
flag: u32,
text: []const u8,
) bool {
const pane = p.panes[id] orelse return false;
if (pane.fs.readers == 0) return false;
var buf: [max_record_text + 64]u8 = undefined;
const record = formatRecord(&buf, p.fs.origin, action, q0, q1, flag, text);
pane.fs.events.push(p.gpa, record);
p.fs.news = true;
return true;
}
/// A record that follows another with no place of its own, `<c>0 0 0 <n>
/// <text>`: a chorded exec's argument and its location (acme exec.c:182).
pub fn noteFollow(p: *Pardes, id: usize, action: Action, text: []const u8) void {
const pane = p.panes[id] orelse return;
if (pane.fs.readers == 0) return;
var buf: [max_record_text + 64]u8 = undefined;
pane.fs.events.push(p.gpa, formatRecord(&buf, p.fs.origin, action, 0, 0, 0, text));
p.fs.news = true;
}
pub fn notePtyOutput(p: *Pardes, id: usize, bytes: []const u8) void {
if (id >= MAX_PANES or bytes.len == 0) return;
const pf = &(p.panes[id] orelse return).fs;
if (!pf.pty_reader) return;
var off: usize = 0;
while (off < bytes.len) {
const n = @min(bytes.len - off, queue_cap / 2);
pf.pty_out.push(p.gpa, bytes[off..][0..n]);
off += n;
}
p.fs.news = true;
}
const EventRecord = struct { origin: u8, action: Action, q0: u32, q1: u32, flag: u32 = 0, text: ?[]const u8 = null };
const EventReader = struct {
data: []const u8,
i: usize = 0,
fn next(er: *EventReader) ?EventRecord {
if (er.i >= er.data.len) return null;
var i = er.i;
if (i + 2 > er.data.len) return null;
const origin = er.data[i];
i += 1;
const action = Action.fromChar(er.data[i]) orelse return null;
i += 1;
const q0 = scanNumber(er.data, &i) orelse return null;
const q1 = scanNumber(er.data, &i) orelse return null;
while (i < er.data.len and er.data[i] == ' ') i += 1;
// acme takes only `origin action q0 q1`; the record whole, as read,
// is taken too, so a click with no place in the text (one written
// to a pane's own look or exec) can be written back and done.
var text: ?[]const u8 = null;
var flag: u32 = 0;
if (i < er.data.len and er.data[i] != '\n') {
flag = scanNumber(er.data, &i) orelse return null;
const n = scanNumber(er.data, &i) orelse return null;
if (i >= er.data.len or er.data[i] != ' ' or er.data.len - (i + 1) < n) return null;
text = er.data[i + 1 ..][0..n];
i += 1 + n;
}
if (i >= er.data.len or er.data[i] != '\n') return null;
er.i = i + 1;
return .{ .origin = origin, .action = action, .q0 = q0, .q1 = q1, .flag = flag, .text = text };
}
};
fn scanNumber(data: []const u8, i: *usize) ?u32 {
while (i.* < data.len and data[i.*] == ' ') i.* += 1;
const s = i.*;
var n: u64 = 0;
while (i.* < data.len and data[i.*] >= '0' and data[i.*] <= '9') : (i.* += 1)
n = @min(n * 10 + (data[i.*] - '0'), std.math.maxInt(u32));
if (i.* == s) return null;
return @intCast(n);
}
/// Writing a Look or Exec record back performs the action it names.
pub fn writeEvent(p: *Pardes, req: Req, id: usize) Reply {
const pane0 = p.panes[id] orelse return Reply.fail(req.tag, E.NOENT);
const serial = pane0.serial;
{
const body = pane_files.bodyOf(pane0);
const tag = pane_files.tagOf(p, pane0);
var check: EventReader = .{ .data = req.data };
while (check.next()) |r| {
switch (r.action) {
.body_look, .tag_look, .body_exec, .tag_exec => {},
else => return tree.failText(req.tag, E.INVAL, tree.e_bad_event ++ ": only a look or exec (L l X x) is written back"),
}
const n = if (r.action.onTag()) tag.len else body.len;
if (r.q0 > r.q1) return tree.failText(req.tag, E.INVAL, tree.e_bad_event ++ ": its range ends before it starts");
// Well formed, but not a range of this text: say which.
if (r.q1 > n) return tree.failText(req.tag, E.INVAL, if (r.action.onTag()) "range past end of tag" else "range past end of body");
}
if (check.i != req.data.len) return tree.failText(req.tag, E.INVAL, tree.e_bad_event ++ ": a record is <origin><action><q0> <q1> <flag> <count> <text>, a line each");
}
var run: EventReader = .{ .data = req.data };
while (run.next()) |r| {
const live = p.paneBySerial(serial) orelse break;
const pane = p.panes[live].?;
const whole = if (r.action.onTag()) pane_files.tagOf(p, pane) else pane_files.bodyOf(pane);
const lo = @min(@as(usize, r.q0), whole.len);
const hi = @max(lo, @min(@as(usize, r.q1), whole.len));
const said = if (r.q0 == r.q1) r.text orelse whole[lo..hi] else whole[lo..hi];
const text = p.scratch.allocator().dupe(u8, said) catch continue;
switch (r.action) {
// A body's click written back is done as the click would have
// been (acme's xfideventwrite, xfid.c:842-844): a REPL bound for
// it takes it. One that came from a write to exec (`F`) was
// never a click, and runs as the command it was.
// The chorded exec a reader was told of runs with its argument
// (mouse.reportGesture), when it is that range written back.
.body_exec, .tag_exec => {
const pf = &pane.fs;
// The two records that follow a chorded exec (its argument
// and where it came from), written back after it, are its:
// consumed, never run as commands of their own.
if (pf.chord_follow > 0 and r.q0 == 0 and r.q1 == 0 and r.flag == 0) {
pf.chord_follow -= 1;
// The argument a command written back alone waited for.
if (pf.chord_pending) |command| {
pf.chord_pending = null;
defer p.gpa.free(command);
const line = exec.withArg(p, command, r.text orelse "");
if (pf.chord_pending_tag) _ = exec.execute(p, live, line) else _ = exec.executeFrom(p, live, line, true);
}
continue;
}
var arg = if (pf.chord_arg) |a| (if (pf.chord_at.q0 == r.q0 and pf.chord_at.q1 == r.q1) a else null) else null;
if (r.flag & flag_chorded != 0) {
// Its argument is the next record when it came in the
// same write; the rest that follow are consumed.
pf.chord_follow = 2;
const save = run.i;
if (run.next()) |following| if (following.q0 == 0 and following.q1 == 0 and following.flag == 0) {
if (following.text) |t| arg = p.scratch.allocator().dupe(u8, t) catch arg;
pf.chord_follow = 1;
} else {
run.i = save;
};
// Alone, with no argument kept: it waits for its own.
if (arg == null) {
if (pf.chord_pending) |old| p.gpa.free(old);
pf.chord_pending = p.gpa.dupe(u8, text) catch null;
pf.chord_pending_tag = r.action == .tag_exec;
continue;
}
}
if (r.action == .tag_exec)
_ = exec.execute(p, live, exec.withArg(p, text, arg))
else
_ = exec.executeFrom(p, live, exec.withArg(p, text, arg), r.origin != 'F');
if (arg != null) if (p.paneBySerial(serial)) |still| {
const spf = &p.panes[still].?.fs;
if (spf.chord_arg) |a| p.gpa.free(a);
spf.chord_arg = null;
};
},
// What it found is what the look file reads back next, as a
// look written there would leave (ctl.run).
.body_look, .tag_look => {
p.look_missed = false;
look.lookAt(p, live, text);
p.fs.results_len = 0;
if (!p.look_missed) if (p.panes[p.active]) |found| {
p.fs.results[0] = found.serial;
p.fs.results_len = 1;
};
},
else => unreachable,
}
}
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
}
// ---- tests ----
const testing = std.testing;
const th = @import("testing.zig");
const call = th.call;
const rd = th.rd;
const wr = th.wr;
const withFile = th.withFile;
const serialOf = th.serialOf;
const Node = tree.Node;
const Status = tree.Status;
test "a stream read at an offset inside its last answer gets the rest of it again, as bash's read seeks back" {
const p = try withFile(testing.allocator, "one\n");
defer p.deinit();
p.update(.tick);
while (p.nextEffect()) |_| {}
const log = @intFromEnum(tree.TopFile.log);
const f = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
const frozen: u64 = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = f, .size = 8192 }).bytes.len;
_ = call(p, .{ .tag = 3, .op = .write, .node = log, .handle = f, .data = "follow" });
notePath(p, "dump", "/a");
notePath(p, "dump", "/b");
const Read = struct {
fn at(pp: *Pardes, h: u32, off: u64) []const u8 {
return call(pp, .{ .tag = 4, .op = .read, .node = @intFromEnum(tree.TopFile.log), .handle = h, .off = off, .size = 128 }).bytes;
}
};
try testing.expectEqualStrings("dump /a\n", Read.at(p, f, frozen));
// bash kept "dump" and seeked back to just past it.
try testing.expectEqualStrings(" /a\n", Read.at(p, f, frozen + 4));
// At the end of what it was given: the next record.
try testing.expectEqualStrings("dump /b\n", Read.at(p, f, frozen + 8));
// A client that reads every record at one offset still gets new ones.
notePath(p, "dump", "/c");
try testing.expectEqualStrings("dump /c\n", Read.at(p, f, frozen + 8));
}
test "event records are acme's bytes, one per read, and .again when empty" {
const gpa = testing.allocator;
const p = try withFile(gpa, "Msg fs-ran\n");
defer p.deinit();
const serial = serialOf(p);
const event = Node.of(serial, .event);
_ = noteAction(p, 0, .body_exec, 1, 4, flag_builtin, "sg ");
try testing.expect(p.panes[0].?.fs.events.empty());
const h = call(p, .{ .tag = 10, .op = .open, .node = event });
try testing.expect(h.reply.handle != 0);
try testing.expectEqual(@as(u16, 1), p.fs.listeners);
try testing.expectEqual(Status.again, rd(p, event, 0, 4096).reply.status);
p.fs.origin = 'M';
_ = noteAction(p, 0, .body_exec, 1, 4, flag_builtin, "ell");
_ = noteAction(p, 0, .body_delete, 0, 3, 0, "");
try testing.expectEqualStrings("MX1 4 1 3 ell\n", rd(p, event, 0, 4096).bytes);
try testing.expectEqualStrings("MD0 3 0 0 \n", rd(p, event, 0, 4096).bytes);
try testing.expectEqual(Status.again, rd(p, event, 0, 4096).reply.status);
_ = noteAction(p, 0, .body_look, 0, 3, flag_filename, "one");
try testing.expectEqual(E.INVAL, rd(p, event, 0, 4).errno());
try testing.expectEqualStrings("ML0 3 4 3 one\n", rd(p, event, 0, 4096).bytes);
const big = "z" ** max_record_text;
_ = noteAction(p, 0, .body_exec, 0, max_record_text, 0, big);
try testing.expectEqualStrings("MX0 256 0 0 \n", rd(p, event, 0, 4096).bytes);
_ = call(p, .{ .tag = 11, .op = .release, .node = event, .handle = h.reply.handle });
try testing.expectEqual(@as(u16, 0), p.fs.listeners);
}
fn drainEvents(p: *Pardes, node: u64, store: []u8, out: [][]const u8) [][]const u8 {
var used: usize = 0;
var n: usize = 0;
while (n < out.len) {
const a = rd(p, node, 0, 4096);
if (a.reply.status != .ok) break;
@memcpy(store[used..][0..a.bytes.len], a.bytes);
out[n] = store[used..][0..a.bytes.len];
used += a.bytes.len;
n += 1;
}
return out[0..n];
}
test "a write through the filesystem is reported once, attributed to the file it came through" {
const gpa = testing.allocator;
const p = try withFile(gpa, "one\ntwo\n");
defer p.deinit();
const serial = serialOf(p);
const event = Node.of(serial, .event);
_ = call(p, .{ .tag = 40, .op = .open, .node = event });
var store: [4096]u8 = undefined;
var slots: [16][]const u8 = undefined;
_ = drainEvents(p, event, &store, &slots);
_ = wr(p, Node.of(serial, .body), "three\n");
const body_recs = drainEvents(p, event, &store, &slots);
try testing.expect(body_recs.len >= 1);
try testing.expectEqualStrings("EI8 14 0 6 three\n\n", body_recs[0]);
for (body_recs[1..]) |r| {
try testing.expectEqual(@as(u8, 'E'), r[0]);
try testing.expect(Action.fromChar(r[1]).?.onTag());
}
_ = wr(p, Node.of(serial, .addr), "1");
_ = wr(p, Node.of(serial, .data), "ONE\n");
const data_recs = drainEvents(p, event, &store, &slots);
try testing.expectEqual(@as(usize, 2), data_recs.len);
try testing.expectEqualStrings("FD0 3 0 0 \n", data_recs[0]);
try testing.expectEqualStrings("FI0 3 0 3 ONE\n", data_recs[1]);
try testing.expectEqual(Status.again, rd(p, event, 0, 4096).reply.status);
}
test "one event reader at a time, and a writer still holds the pane" {
const gpa = testing.allocator;
const p = try withFile(gpa, "x\n");
defer p.deinit();
const event = Node.of(serialOf(p), .event);
const reader = call(p, .{ .tag = 12, .op = .open, .node = event });
try testing.expectEqual(Status.ok, reader.reply.status);
// Reads consume, so a second reader would see half the clicks: refused.
const second = call(p, .{ .tag = 13, .op = .open, .node = event, .omode = 2 });
try testing.expectEqual(Status.err, second.reply.status);
try testing.expectEqualStrings(tree.e_in_use, second.reply.ename);
const writer = call(p, .{ .tag = 13, .op = .open, .node = event, .omode = 1 });
try testing.expectEqual(Status.ok, writer.reply.status);
try testing.expectEqual(@as(u16, 2), p.panes[0].?.fs.readers);
try testing.expectEqual(@as(u16, 2), p.fs.listeners);
_ = call(p, .{ .tag = 14, .op = .release, .node = event, .handle = writer.reply.handle });
try testing.expectEqual(@as(u16, 1), p.panes[0].?.fs.readers);
try testing.expect(p.panes[0].?.fs.readers != 0);
_ = call(p, .{ .tag = 15, .op = .release, .node = Node.of(serialOf(p), .body) });
try testing.expectEqual(@as(u16, 1), p.fs.listeners);
_ = call(p, .{ .tag = 16, .op = .release, .node = event, .handle = reader.reply.handle });
try testing.expectEqual(@as(u16, 0), p.fs.listeners);
try testing.expect(p.panes[0].?.fs.readers == 0);
_ = call(p, .{ .tag = 17, .op = .release, .node = event });
try testing.expectEqual(@as(u16, 0), p.fs.listeners);
// Once it closed, the next reader gets in.
const next = call(p, .{ .tag = 18, .op = .open, .node = event });
try testing.expectEqual(Status.ok, next.reply.status);
_ = call(p, .{ .tag = 19, .op = .release, .node = event, .handle = next.reply.handle });
}
test "a pane deleted while its event file is open leaves no suppression behind" {
const gpa = testing.allocator;
const p = try withFile(gpa, "x\n");
defer p.deinit();
const serial = serialOf(p);
const event = Node.of(serial, .event);
_ = try th.newPane(p);
const a = call(p, .{ .tag = 18, .op = .open, .node = event });
const b = call(p, .{ .tag = 19, .op = .open, .node = event, .omode = 1 });
try testing.expectEqual(@as(u16, 2), p.fs.listeners);
// Through ctl: a line written to its exec would go to the reader.
_ = wr(p, Node.of(serial, .ctl), "Del\n");
try testing.expect(p.paneBySerial(serial) == null);
try testing.expectEqual(@as(u16, 0), p.fs.listeners);
// The reader's next read hears what acme says of a window gone under it.
const shut = call(p, .{ .tag = 19, .op = .read, .node = event, .handle = a.reply.handle, .size = 64 });
try testing.expectEqual(E.NOENT, shut.errno());
try testing.expectEqualStrings(tree.e_shut_down, shut.reply.ename);
_ = call(p, .{ .tag = 20, .op = .release, .node = event, .handle = a.reply.handle });
_ = call(p, .{ .tag = 21, .op = .release, .node = event, .handle = b.reply.handle });
try testing.expectEqual(@as(u16, 0), p.fs.listeners);
try testing.expectEqual(E.NOENT, rd(p, event, 0, 64).errno());
try testing.expectEqual(E.NOENT, rd(p, Node.of(serial, .body), 0, 64).errno());
try testing.expectEqual(E.NOENT, wr(p, Node.of(serial, .dirty), "0\n").errno());
try testing.expectEqual(E.NOENT, call(p, .{ .tag = 22, .op = .open, .node = event }).errno());
}
test "writing an event record back performs the action it names" {
const gpa = testing.allocator;
const p = try withFile(gpa, "Msg fs-ran\n");
defer p.deinit();
const serial = serialOf(p);
const event = Node.of(serial, .event);
const pane = p.panes[0].?;
const w = wr(p, event, "FX0 10\n");
try testing.expectEqual(Status.ok, w.reply.status);
try testing.expectEqualStrings("fs-ran", pane.msg[0..pane.msg_len]);
pane.msg_len = 0;
try testing.expectEqual(Status.ok, wr(p, event, "FX0 10\nFX0 10\n").reply.status);
try testing.expectEqualStrings("fs-ran", pane.msg[0..pane.msg_len]);
pane.msg_len = 0;
for ([_][]const u8{
"FX0 10\nFQ0 1\n", // unknown type character
"FX0 999\n", // out of range
"FX0 10", // no newline
"FX5 1\n", // q0 > q1
"FD0 3\n", // a report, not a request
"F\n",
}) |bad| {
try testing.expectEqual(E.INVAL, wr(p, event, bad).errno());
try testing.expectEqual(@as(usize, 0), pane.msg_len);
}
// A look written back sets what look reads back.
_ = wr(p, event, "FL0 3\n");
var want_serial: [16]u8 = undefined;
try testing.expectEqualStrings(try std.fmt.bufPrint(&want_serial, "{d}\n", .{serial}), rd(p, @intFromEnum(tree.TopFile.look), 0, 64).bytes);
// Well formed but past the text: said so, not "bad event syntax".
try testing.expectEqualStrings("range past end of body", wr(p, event, "MX0 999\n").reply.ename);
_ = call(p, .{ .tag = 23, .op = .open, .node = event });
p.fs.origin = 'K';
_ = wr(p, event, "KX0 10\n");
try testing.expectEqual(@as(u8, 'F'), p.fs.origin);
}
test "the log records whether or not anyone reads, and an open that follows waits" {
const gpa = testing.allocator;
const p = try withFile(gpa, "logged\n");
defer p.deinit();
const log = @intFromEnum(tree.TopFile.log);
var expected: [4200]u8 = undefined;
// Nobody holds /log open, and the pane is recorded anyway.
const early = try th.newPane(p);
const early_line = try std.fmt.bufPrint(&expected, "new {d} {s}\n", .{ early, pane_files.nameOf(p.panes[p.paneBySerial(early).?].?) });
const early_owned = try gpa.dupe(u8, early_line);
defer gpa.free(early_owned);
// A plain open answers what happened so far, and ends: `cat log` never hangs.
const plain = call(p, .{ .tag = 1, .op = .open, .node = log });
try testing.expectEqual(Status.ok, plain.reply.status);
const first = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = plain.reply.handle, .size = 8192 });
try testing.expect(std.mem.endsWith(u8, first.bytes, early_owned));
const past = call(p, .{ .tag = 3, .op = .read, .node = log, .handle = plain.reply.handle, .off = first.bytes.len, .size = 8192 });
try testing.expectEqual(Status.ok, past.reply.status);
try testing.expectEqual(@as(usize, 0), past.bytes.len);
const tail = call(p, .{ .tag = 3, .op = .read, .node = log, .handle = plain.reply.handle, .off = first.bytes.len - early_owned.len, .size = 8192 });
try testing.expectEqualStrings(early_owned, tail.bytes);
_ = call(p, .{ .tag = 4, .op = .release, .node = log, .handle = plain.reply.handle });
// `follow` on an open makes it wait past what it froze, one record a read.
const f = call(p, .{ .tag = 5, .op = .open, .node = log });
const fh = f.reply.handle;
const frozen = call(p, .{ .tag = 6, .op = .read, .node = log, .handle = fh, .size = 8192 }).bytes.len;
try testing.expectEqual(Status.err, call(p, .{ .tag = 7, .op = .write, .node = log, .handle = fh, .data = "tail\n" }).reply.status);
try testing.expectEqual(Status.ok, call(p, .{ .tag = 8, .op = .write, .node = log, .handle = fh, .data = "follow\n" }).reply.status);
// A stream: after `echo follow >&3` a shell reads at offset 7, and gets
// what comes next, not the frozen text from byte 7. Before `follow` the
// offset is honoured, so `tail -n` works (checked below on `plain`).
const rdf = struct {
fn next(pp: *Pardes, node: u64, h: u32, off: u64) th.Answer {
return call(pp, .{ .tag = 9, .op = .read, .node = node, .handle = h, .off = off, .size = 8192 });
}
};
// The word it refused is on record, and then there is nothing new.
try testing.expect(std.mem.startsWith(u8, rdf.next(p, log, fh, frozen).bytes, "err - log: "));
try testing.expectEqual(Status.again, rdf.next(p, log, fh, frozen).reply.status);
const serial = try th.newPane(p);
const id = p.paneBySerial(serial).?;
try testing.expectEqualStrings(
try std.fmt.bufPrint(&expected, "new {d} {s}\n", .{ serial, pane_files.nameOf(p.panes[id].?) }),
rdf.next(p, log, fh, frozen).bytes,
);
try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .name), "/tmp/logged.txt\n").reply.status);
try testing.expectEqualStrings(
try std.fmt.bufPrint(&expected, "rename {d} /tmp/logged.txt\n", .{serial}),
rdf.next(p, log, fh, frozen).bytes,
);
// What the editor says is in the same stream, one line even when the
// message had several.
p.setMessage(id, "save: two\nlines");
try testing.expectEqualStrings(
try std.fmt.bufPrint(&expected, "msg {d} save: two lines\n", .{serial}),
rdf.next(p, log, fh, frozen).bytes,
);
// A builtin announces itself on the message row only: the log has
// what it did.
try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Del\n").reply.status);
try testing.expectEqualStrings(
try std.fmt.bufPrint(&expected, "del {d} /tmp/logged.txt\n", .{serial}),
rdf.next(p, log, fh, frozen).bytes,
);
try testing.expectEqual(Status.again, rdf.next(p, log, fh, frozen).reply.status);
// A shell's `read` asks for a few bytes at a time: the record comes in
// pieces rather than failing. A repeat the follower waits on, having
// read the first, is its own line.
const said_by = p.panes[0].?.serial;
p.setMessage(0, "same failure");
var pieced: [64]u8 = undefined;
var got: usize = 0;
while (got == 0 or pieced[got - 1] != '\n') {
const piece = call(p, .{ .tag = 9, .op = .read, .node = log, .handle = fh, .off = frozen, .size = 3 }).bytes;
@memcpy(pieced[got..][0..piece.len], piece);
got += piece.len;
}
try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "msg {d} same failure\n", .{said_by}), pieced[0..got]);
p.setMessage(0, "same failure");
try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "msg {d} same failure (x2)\n", .{said_by}), rdf.next(p, log, fh, frozen).bytes);
// A follower the ring outran hears how much it missed, then carries on.
var filler: [200]u8 = @splat('x');
for (0..p.fs.log.cap / filler.len + 8) |i| {
_ = std.fmt.bufPrint(&filler, "{d:0>6}", .{i}) catch unreachable; // a repeat would collapse
p.setMessage(0, &filler);
}
const lost = rdf.next(p, log, fh, frozen).bytes;
try testing.expect(std.mem.startsWith(u8, lost, "lost "));
try testing.expect(std.mem.startsWith(u8, rdf.next(p, log, fh, frozen).bytes, "msg "));
_ = call(p, .{ .tag = 10, .op = .release, .node = log, .handle = fh });
for (p.fs.opens) |o| try testing.expect(o.node == 0);
// A record bigger than the whole ring is cut to fit, not dropped with
// everything else pushed out ahead of it.
p.fs.log.cap = 128;
var huge: [300]u8 = @splat('y');
p.setMessage(0, &huge);
const cut = call(p, .{ .tag = 11, .op = .open, .node = log });
const kept = call(p, .{ .tag = 12, .op = .read, .node = log, .handle = cut.reply.handle, .size = 512 }).bytes;
try testing.expectEqual(@as(usize, 124), kept.len);
try testing.expect(std.mem.startsWith(u8, kept, "msg ") and kept[kept.len - 1] == '\n');
_ = call(p, .{ .tag = 13, .op = .release, .node = log, .handle = cut.reply.handle });
}
test "a refused or failed write is an err record in the log, saying which file and why" {
const p = try withFile(testing.allocator, "one\n");
defer p.deinit();
p.update(.tick); // the pane's `new` is logged before the log is opened
while (p.nextEffect()) |_| {}
const serial = serialOf(p);
const log = @intFromEnum(tree.TopFile.log);
const f = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
const frozen = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = f, .size = 1 << 16 }).bytes.len;
_ = call(p, .{ .tag = 3, .op = .write, .node = log, .handle = f, .data = "follow" });
var want: [128]u8 = undefined;
const next = struct {
fn record(pp: *Pardes, h: u32, off: usize) []const u8 {
return call(pp, .{ .tag = 4, .op = .read, .node = @intFromEnum(tree.TopFile.log), .handle = h, .off = off, .size = 4096 }).bytes;
}
};
_ = wr(p, Node.of(serial, .addr), "/nowhere/");
try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "err {d} addr: no match for regexp\n", .{serial}), next.record(p, f, frozen));
_ = wr(p, @intFromEnum(tree.TopFile.ctl), "Bogus");
try testing.expectEqualStrings("err - ctl: unknown control message \"Bogus\"\n", next.record(p, f, frozen));
_ = wr(p, @intFromEnum(tree.TopFile.focus), "99999");
try testing.expectEqualStrings("err - focus: no such pane\n", next.record(p, f, frozen));
// A write that succeeds says nothing.
_ = wr(p, Node.of(serial, .addr), "#0");
try testing.expectEqual(Status.again, call(p, .{ .tag = 5, .op = .read, .node = log, .handle = f, .off = frozen, .size = 4096 }).reply.status);
// A follower that has read an err waits on its repeat: a line of its
// own, with the running count.
_ = wr(p, Node.of(serial, .addr), "/nowhere/");
try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "err {d} addr: no match for regexp\n", .{serial}), next.record(p, f, frozen));
_ = wr(p, Node.of(serial, .addr), "/nowhere/");
try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "err {d} addr: no match for regexp (x2)\n", .{serial}), next.record(p, f, frozen));
_ = call(p, .{ .tag = 6, .op = .release, .node = log, .handle = f });
// With the follower gone, repeats are the newest record counted on:
// its two, and three more.
for (0..3) |_| _ = wr(p, Node.of(serial, .addr), "/elsewhere/");
const g = call(p, .{ .tag = 7, .op = .open, .node = log }).reply.handle;
const text = call(p, .{ .tag = 8, .op = .read, .node = log, .handle = g, .size = 1 << 16 }).bytes;
try testing.expect(std.mem.endsWith(u8, text, try std.fmt.bufPrint(&want, "regexp\nerr {d} addr: no match for regexp (x5)\n", .{serial})));
_ = call(p, .{ .tag = 9, .op = .release, .node = log, .handle = g });
}
test "a Dump written and a Restore made are in the log, with their files" {
const p = try withFile(testing.allocator, "one\n");
defer p.deinit();
p.setLastDump("/tmp/pardes.dump.zon");
try dump.dumpState(p);
const restored = try dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon");
defer restored.deinit();
for ([_]*Pardes{ p, restored }, [_][]const u8{ "dump", "restore" }) |core, what| {
const log = try freezeLog(core);
defer core.gpa.free(log.bytes);
var want: [64]u8 = undefined;
try testing.expect(std.mem.indexOf(u8, log.bytes, try std.fmt.bufPrint(&want, "{s} /tmp/pardes.dump.zon\n", .{what})) != null);
}
// The replacement's last says which serial each pane had.
const log = try freezeLog(restored);
defer restored.gpa.free(log.bytes);
var want: [64]u8 = undefined;
try testing.expect(std.mem.indexOf(u8, log.bytes, try std.fmt.bufPrint(&want, "restore /tmp/pardes.dump.zon\nrestored {d} {d}\n", .{ p.panes[0].?.serial, restored.panes[0].?.serial })) != null);
// ...and each column's: restoredcol <old> <new>.
try testing.expect(std.mem.endsWith(u8, log.bytes, try std.fmt.bufPrint(&want, "restoredcol {d} {d}\n", .{ pardes.layout.columnSerial(p, 0), pardes.layout.columnSerial(restored, 0) })));
}
test "opens of the log share the open records, and a closed one frees its record" {
const gpa = testing.allocator;
const p = try withFile(gpa, "x\n");
defer p.deinit();
const log = @intFromEnum(tree.TopFile.log);
var handles: [tree.open_slots]u32 = undefined;
for (&handles) |*h| h.* = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
try testing.expectEqual(E.NFILE, call(p, .{ .tag = 2, .op = .open, .node = log }).errno());
_ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = handles[5] });
const again = call(p, .{ .tag = 4, .op = .open, .node = log });
try testing.expectEqual(Status.ok, again.reply.status);
handles[5] = again.reply.handle;
for (handles) |h| _ = call(p, .{ .tag = 5, .op = .release, .node = log, .handle = h });
for (p.fs.opens) |o| try testing.expect(o.node == 0);
}
test "a click in the body reports the offsets of the word it takes, so the record can be written back" {
const config = @import("../config.zig");
const gpa = testing.allocator;
const text = "alpha qty beta\n qty\n\tqty\ndef f(qty): return qty.x\n" ++ "w" ** 100 ++ " qty\n";
const p = try withFile(gpa, text);
defer p.deinit();
// A scratch made over 9P, clicked while another pane has the keyboard.
const scratch = try th.newPane(p);
_ = wr(p, Node.of(scratch, .body), text);
for ([_]u32{ serialOf(p), scratch }) |serial| {
const id = p.paneBySerial(serial).?;
p.active = p.paneBySerial(if (serial == scratch) serialOf(p) else scratch).?;
const event = Node.of(serial, .event);
const h = call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle;
var frame = std.heap.ArenaAllocator.init(gpa);
defer frame.deinit();
_ = try p.render(frame.allocator());
const rect = p.rects[id];
const x0 = rect.x + config.GUTTER + config.PREFIX_W;
const y0 = if (p.settings.tag_bottom) rect.y else rect.y + p.panes[id].?.tag_rows;
const tab: u16 = config.tab_width;
const Click = struct { x: u16, y: u16, look: []const u8, exec: []const u8, scroll: i32 = 0 };
// Where the wrapped line's `qty` landed, as drawn.
const surface = try p.render(frame.allocator());
var wrap_col: u16 = 0;
while (wrap_col < rect.w and !std.mem.eql(u8, surface.at(x0 + wrap_col, y0 + 5).grapheme(), "q")) wrap_col += 1;
for ([_]Click{
.{ .x = x0 + 7, .y = y0, .look = "ML6 9 0 3 qty\n", .exec = "MX6 9 0 3 qty\n" },
.{ .x = x0 + 5, .y = y0 + 1, .look = "ML19 22 0 3 qty\n", .exec = "MX19 22 0 3 qty\n" },
.{ .x = x0 + tab + 1, .y = y0 + 2, .look = "ML24 27 0 3 qty\n", .exec = "MX24 27 0 3 qty\n" },
.{ .x = x0 + 7, .y = y0 + 3, .look = "ML34 37 0 3 qty\n", .exec = "MX34 37 0 3 qty\n" },
// Scrolled a line, the first row shows the second.
.{ .x = x0 + 5, .y = y0, .look = "ML19 22 0 3 qty\n", .exec = "MX19 22 0 3 qty\n", .scroll = 1 },
// Wrapped: the word on the continuation row.
.{ .x = x0 + wrap_col, .y = y0 + 5, .look = "ML154 157 0 3 qty\n", .exec = "MX154 157 0 3 qty\n" },
}) |c| for ([_]pardes.Mouse.Button{ config.look_button, config.exec_button }, [_][]const u8{ c.look, c.exec }) |button, want| {
// The pointer rests on the word first, as a hand's does, and
// the hover preview comes up under it.
p.panes[id].?.scrollBy(c.scroll);
_ = try p.render(frame.allocator());
p.update(.{ .mouse = .{ .button = .none, .kind = .motion, .col = c.x, .row = c.y } });
for (0..200) |_| p.update(.tick);
p.update(.{ .mouse = .{ .button = button, .kind = .press, .col = c.x, .row = c.y } });
p.update(.{ .mouse = .{ .button = button, .kind = .release, .col = c.x, .row = c.y } });
try testing.expectEqualStrings(want, call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).bytes);
p.panes[id].?.scrollBy(-c.scroll);
};
// A click on nothing expands to nothing, and acme then says
// nothing (look.c:37, exec.c:141).
p.update(.{ .mouse = .{ .button = config.look_button, .kind = .press, .col = x0 + 60, .row = y0 + 1 } });
p.update(.{ .mouse = .{ .button = config.look_button, .kind = .release, .col = x0 + 60, .row = y0 + 1 } });
try testing.expectEqual(Status.again, call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).reply.status);
_ = call(p, .{ .tag = 3, .op = .release, .node = event, .handle = h });
}
}
test "follow new skips what the log held and waits for what comes after" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
p.update(.tick); // the pane's `new` is logged first
while (p.nextEffect()) |_| {}
p.setMessage(0, "before the open");
const log = @intFromEnum(tree.TopFile.log);
const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = log, .handle = h, .data = "follow new\n" }).reply.status);
try testing.expectEqual(Status.again, call(p, .{ .tag = 3, .op = .read, .node = log, .handle = h, .size = 4096 }).reply.status);
p.setMessage(0, "after it");
try testing.expect(std.mem.endsWith(u8, call(p, .{ .tag = 4, .op = .read, .node = log, .handle = h, .size = 4096 }).bytes, " after it\n"));
_ = call(p, .{ .tag = 5, .op = .release, .node = log, .handle = h });
}
test "a long msg record is cut between words at its cap, with an ellipsis" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const long = "word " ** 60; // 300 bytes
_ = wr(p, Node.of(serialOf(p), .ctl), "Msg " ++ long ++ "\n");
const log = @intFromEnum(tree.TopFile.log);
const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes;
const at = std.mem.lastIndexOf(u8, text, "msg ").?;
const record = text[at .. std.mem.indexOfScalarPos(u8, text, at, '\n').? + 1];
try testing.expect(std.mem.endsWith(u8, record, " word…\n"));
try testing.expect(record.len <= "msg 1 ".len + pardes.Messages.LoggedMessage.cap + "…\n".len);
_ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h });
}
test "a record is one line of UTF-8: DEL and C1 are spaces, bytes not UTF-8 are escaped" {
var out: [64]u8 = undefined;
var name: [64]u8 = undefined;
try testing.expectEqualStrings("/tmp/two\\nlines", shown("/tmp/two\nlines", &name));
// A backslash of the name's own is doubled: `\n` in a name differs from
// a newline in one.
try testing.expectEqualStrings("/tmp/a\\\\nb", shown("/tmp/a\\nb", &name));
try testing.expectEqualStrings("msg - a b c \\xff d\n", sanitize("msg - a\x7fb\xc2\x85c \xff d\n", &out));
try testing.expectEqualStrings("msg - caf\xc3\xa9\n", sanitize("msg - caf\xc3\xa9\n", &out));
}
test "a long err record keeps its end, its middle given up to an ellipsis" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
_ = wr(p, @intFromEnum(tree.TopFile.ctl), "Bogus " ++ "abcdefgh " ** 40 ++ "\n");
const log = @intFromEnum(tree.TopFile.log);
const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes;
const at = std.mem.lastIndexOf(u8, text, "err - ctl: ").?;
const record = text[at .. std.mem.indexOfScalarPos(u8, text, at, '\n').? + 1];
try testing.expect(std.mem.indexOf(u8, record, "…") != null);
try testing.expect(std.mem.startsWith(u8, record, "err - ctl: "));
try testing.expect(std.mem.endsWith(u8, record, "abcdefgh\"\n") or std.mem.endsWith(u8, record, "abcdefgh \"\n"));
_ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h });
}
test "two failures that agree only in what their records cut are two records, not one counted" {
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
const long = "why " ** 80;
p.setMessage(0, long ++ "first");
p.setMessage(0, long ++ "second");
try testing.expect(!th.logHas(p, "(x2)"));
p.setMessage(0, long ++ "second");
try testing.expect(th.logHas(p, "(x2)"));
}
|