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
|
//! OSC 3008: Hierarchical Context Signalling (UAPI spec)
//! Specification: https://uapi-group.org/specifications/specs/osc_context/
//!
//! OSC 3008 allows programs to signal context changes to the terminal emulator.
//! Each context has an identifier and metadata fields. Contexts are hierarchical
//! and form a stack.
const std = @import("std");
const Parser = @import("../../osc.zig").Parser;
const OSCCommand = @import("../../osc.zig").Command;
const log = std.log.scoped(.osc_context_signal);
/// Maximum length of a context identifier (per spec).
const max_context_id_len = 64;
/// A single OSC 3008 context signal command.
pub const Command = struct {
pub const C = void;
action: Action,
/// The context identifier. Must be 1-64 characters in the 32..126 byte range.
id: []const u8,
/// Raw unparsed metadata fields after the context ID.
/// Fields are semicolon-separated key=value pairs.
/// Parsed lazily via `readOption`.
metadata: []const u8,
pub const Action = enum {
/// OSC 3008;start=<id> — initiates, updates, or returns to a context.
start,
/// OSC 3008;end=<id> — terminates a context.
end,
};
/// Read a metadata field value from the raw fields string.
/// Returns null if the field is not present or malformed.
pub fn readOption(
self: Command,
comptime option: Field,
) option.Type() {
return option.read(self.metadata);
}
};
/// Context types defined by the specification.
pub const ContextType = enum {
boot,
container,
vm,
elevate,
chpriv,
subcontext,
remote,
shell,
command,
app,
service,
session,
pub fn parse(value: []const u8) ?ContextType {
return std.meta.stringToEnum(ContextType, value);
}
};
/// Exit status for the `exit` end-sequence field.
pub const ExitStatus = enum {
success,
failure,
crash,
interrupt,
pub fn parse(value: []const u8) ?ExitStatus {
return std.meta.stringToEnum(ExitStatus, value);
}
};
/// Metadata fields that can appear in OSC 3008 sequences.
/// Fields are read lazily from the raw string using the `read` method.
pub const Field = enum {
// Start sequence fields
type,
user,
hostname,
machineid,
bootid,
pid,
pidfdid,
comm,
cwd,
cmdline,
vm,
container,
targetuser,
targethost,
sessionid,
// End sequence fields
exit,
status,
signal,
pub fn Type(comptime self: Field) type {
return switch (self) {
.type => ?ContextType,
.exit => ?ExitStatus,
.pid, .pidfdid => ?u64,
.status => ?u64,
// All other fields are string values
.user,
.hostname,
.machineid,
.bootid,
.comm,
.cwd,
.cmdline,
.vm,
.container,
.targetuser,
.targethost,
.sessionid,
.signal,
=> ?[]const u8,
};
}
fn key(comptime self: Field) []const u8 {
return @tagName(self);
}
/// Read the field value from the raw fields string.
///
/// The raw fields string contains semicolon-separated key=value pairs
/// e.g. "type=container;user=lennart;hostname=zeta".
///
/// Unknown or malformed fields are ignored per the specification.
pub fn read(
comptime self: Field,
raw: []const u8,
) self.Type() {
var it = std.mem.splitScalar(u8, raw, ';');
while (it.next()) |full| {
// Parse key=value
const value = value: {
if (std.mem.indexOfScalar(
u8,
full,
'=',
)) |eql_idx| {
if (std.mem.eql(
u8,
full[0..eql_idx],
self.key(),
)) {
break :value full[eql_idx + 1 ..];
}
}
continue;
};
return switch (self) {
.type => .parse(value),
.exit => .parse(value),
.pid, .pidfdid, .status => value: {
for (value) |c| {
if (c < '0' or c > '9') break :value null;
}
break :value std.fmt.parseInt(
u64,
value,
10,
) catch null;
},
// String fields
.user,
.hostname,
.machineid,
.bootid,
.comm,
.cwd,
.cmdline,
.vm,
.container,
.targetuser,
.targethost,
.sessionid,
.signal,
=> if (value.len > 0) value else null,
};
}
// Not found
return null;
}
};
/// Parse OSC 3008: hierarchical context signalling.
///
/// Expected data format (after "3008;" prefix has been consumed by the state machine):
/// start=<id>[;<field>=<value>]*
/// end=<id>[;<field>=<value>]*
pub fn parse(parser: *Parser, _: ?u8) ?*OSCCommand {
const cap = if (parser.capture) |*c| c else {
parser.state = .invalid;
return null;
};
const data = cap.trailing();
if (data.len == 0) {
parser.state = .invalid;
return null;
}
// Determine the action (start= or end=)
const action: Command.Action = action: {
if (std.mem.startsWith(u8, data, "start=")) break :action .start;
if (std.mem.startsWith(u8, data, "end=")) break :action .end;
log.warn("OSC 3008: expected 'start=' or 'end=' prefix, got: {s}", .{
data[0..@min(data.len, 10)],
});
parser.state = .invalid;
return null;
};
// Skip past the "start=" or "end=" prefix
const prefix_len: usize = switch (action) {
.start => "start=".len,
.end => "end=".len,
};
const rest = data[prefix_len..];
if (rest.len == 0) {
log.warn("OSC 3008: missing context ID", .{});
parser.state = .invalid;
return null;
}
// Extract the context ID (up to the first semicolon or end of data)
const id_end = std.mem.indexOfScalar(u8, rest, ';') orelse rest.len;
const id = rest[0..id_end];
// Validate context ID length (1-64 chars per spec)
if (id.len == 0 or id.len > max_context_id_len) {
log.warn("OSC 3008: context ID length {d} out of range (1-{d})", .{
id.len,
max_context_id_len,
});
parser.state = .invalid;
return null;
}
// Validate context ID characters (32..126 byte range per spec)
for (id) |c| {
if (c < 0x20 or c > 0x7e) {
log.warn("OSC 3008: invalid character 0x{x:0>2} in context ID", .{c});
parser.state = .invalid;
return null;
}
}
// Extract raw metadata fields (everything after the ID)
const metadata = if (id_end < rest.len) rest[id_end + 1 ..] else "";
parser.command = .{
.context_signal = .{
.action = action,
.id = id,
.metadata = metadata,
},
};
return &parser.command;
}
// ============================================================================
// Tests
// ============================================================================
test "OSC 3008: basic start command" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=abc123";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .start);
try testing.expectEqualStrings("abc123", cmd.context_signal.id);
try testing.expectEqualStrings("", cmd.context_signal.metadata);
}
test "OSC 3008: basic end command" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;end=abc123";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .end);
try testing.expectEqualStrings("abc123", cmd.context_signal.id);
try testing.expectEqualStrings("", cmd.context_signal.metadata);
}
test "OSC 3008: start with metadata fields" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=bed86fab93af4328bbed0a1224af6d40;type=container;user=lennart;hostname=zeta";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .start);
try testing.expectEqualStrings("bed86fab93af4328bbed0a1224af6d40", cmd.context_signal.id);
// Read individual fields
try testing.expect(cmd.context_signal.readOption(.type).? == .container);
try testing.expectEqualStrings("lennart", cmd.context_signal.readOption(.user).?);
try testing.expectEqualStrings("zeta", cmd.context_signal.readOption(.hostname).?);
}
test "OSC 3008: start with all common fields" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=myctx;type=shell;user=root;hostname=myhost;machineid=3deb5353d3ba43d08201c136a47ead7b;bootid=d4a3d0fdf2e24fdea6d971ce73f4fbf2;pid=1062862;pidfdid=1063162;comm=bash";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.readOption(.type).? == .shell);
try testing.expectEqualStrings("root", cmd.context_signal.readOption(.user).?);
try testing.expectEqualStrings("myhost", cmd.context_signal.readOption(.hostname).?);
try testing.expectEqualStrings("3deb5353d3ba43d08201c136a47ead7b", cmd.context_signal.readOption(.machineid).?);
try testing.expectEqualStrings("d4a3d0fdf2e24fdea6d971ce73f4fbf2", cmd.context_signal.readOption(.bootid).?);
try testing.expectEqual(@as(u64, 1062862), cmd.context_signal.readOption(.pid).?);
try testing.expectEqual(@as(u64, 1063162), cmd.context_signal.readOption(.pidfdid).?);
try testing.expectEqualStrings("bash", cmd.context_signal.readOption(.comm).?);
}
test "OSC 3008: end with exit metadata" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;end=myctx;exit=success;status=0";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .end);
try testing.expectEqualStrings("myctx", cmd.context_signal.id);
try testing.expect(cmd.context_signal.readOption(.exit).? == .success);
try testing.expectEqual(@as(u64, 0), cmd.context_signal.readOption(.status).?);
}
test "OSC 3008: end with failure exit" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;end=myctx;exit=failure;status=1;signal=SIGKILL";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.readOption(.exit).? == .failure);
try testing.expectEqual(@as(u64, 1), cmd.context_signal.readOption(.status).?);
try testing.expectEqualStrings("SIGKILL", cmd.context_signal.readOption(.signal).?);
}
test "OSC 3008: unknown fields are ignored" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=myctx;type=shell;unknownfield=value;user=root";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.readOption(.type).? == .shell);
try testing.expectEqualStrings("root", cmd.context_signal.readOption(.user).?);
}
test "OSC 3008: missing field returns null" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=myctx;user=lennart";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.readOption(.type) == null);
try testing.expect(cmd.context_signal.readOption(.hostname) == null);
try testing.expect(cmd.context_signal.readOption(.pid) == null);
}
test "OSC 3008: invalid prefix" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;bogus=abc123";
for (input) |ch| p.next(ch);
try testing.expect(p.end(null) == null);
}
test "OSC 3008: empty data" {
const testing = std.testing;
// Can't really produce empty data after "3008;" because the state machine
// won't write a writer for that case, but we test the edge case where
// only "start=" is present with no ID.
var p: Parser = .init(null);
const input = "3008;start=";
for (input) |ch| p.next(ch);
try testing.expect(p.end(null) == null);
}
test "OSC 3008: max length context ID" {
const testing = std.testing;
var p: Parser = .init(null);
const id = "a" ** 64;
const input = "3008;start=" ++ id;
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expectEqualStrings(id, cmd.context_signal.id);
}
test "OSC 3008: over-length context ID" {
const testing = std.testing;
var p: Parser = .init(null);
const id = "a" ** 65;
const input = "3008;start=" ++ id;
for (input) |ch| p.next(ch);
try testing.expect(p.end(null) == null);
}
test "OSC 3008: context type enum coverage" {
const testing = std.testing;
const types = [_]struct { str: []const u8, expected: ContextType }{
.{ .str = "boot", .expected = .boot },
.{ .str = "container", .expected = .container },
.{ .str = "vm", .expected = .vm },
.{ .str = "elevate", .expected = .elevate },
.{ .str = "chpriv", .expected = .chpriv },
.{ .str = "subcontext", .expected = .subcontext },
.{ .str = "remote", .expected = .remote },
.{ .str = "shell", .expected = .shell },
.{ .str = "command", .expected = .command },
.{ .str = "app", .expected = .app },
.{ .str = "service", .expected = .service },
.{ .str = "session", .expected = .session },
};
for (types) |t| {
try testing.expectEqual(t.expected, ContextType.parse(t.str).?);
}
try testing.expect(ContextType.parse("invalid") == null);
}
test "OSC 3008: exit status enum coverage" {
const testing = std.testing;
try testing.expect(ExitStatus.parse("success").? == .success);
try testing.expect(ExitStatus.parse("failure").? == .failure);
try testing.expect(ExitStatus.parse("crash").? == .crash);
try testing.expect(ExitStatus.parse("interrupt").? == .interrupt);
try testing.expect(ExitStatus.parse("invalid") == null);
}
test "OSC 3008: spec example - container start" {
const testing = std.testing;
// From the spec: a new container "foobar" invoked by user "lennart" on host "zeta"
var p: Parser = .init(null);
const input = "3008;start=bed86fab93af4328bbed0a1224af6d40;type=container;user=lennart;hostname=zeta;machineid=3deb5353d3ba43d08201c136a47ead7b;bootid=d4a3d0fdf2e24fdea6d971ce73f4fbf2;pid=1062862;pidfdid=1063162;comm=systemd-nspawn;container=foobar";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .start);
try testing.expectEqualStrings("bed86fab93af4328bbed0a1224af6d40", cmd.context_signal.id);
try testing.expect(cmd.context_signal.readOption(.type).? == .container);
try testing.expectEqualStrings("lennart", cmd.context_signal.readOption(.user).?);
try testing.expectEqualStrings("zeta", cmd.context_signal.readOption(.hostname).?);
try testing.expectEqualStrings("systemd-nspawn", cmd.context_signal.readOption(.comm).?);
try testing.expectEqualStrings("foobar", cmd.context_signal.readOption(.container).?);
try testing.expectEqual(@as(u64, 1062862), cmd.context_signal.readOption(.pid).?);
}
test "OSC 3008: spec example - context end" {
const testing = std.testing;
// From the spec: context end
var p: Parser = .init(null);
const input = "3008;end=bed86fab93af4328bbed0a1224af6d40";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .end);
try testing.expectEqualStrings("bed86fab93af4328bbed0a1224af6d40", cmd.context_signal.id);
}
test "OSC 3008: cwd and cmdline fields" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=myctx;type=command;cwd=/home/user;cmdline=ls -la";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expectEqualStrings("/home/user", cmd.context_signal.readOption(.cwd).?);
try testing.expectEqualStrings("ls -la", cmd.context_signal.readOption(.cmdline).?);
}
test "OSC 3008: start command with no fields" {
const testing = std.testing;
var p: Parser = .init(null);
const input = "3008;start=simpleid";
for (input) |ch| p.next(ch);
const cmd = p.end(null).?.*;
try testing.expect(cmd == .context_signal);
try testing.expect(cmd.context_signal.action == .start);
try testing.expectEqualStrings("simpleid", cmd.context_signal.id);
try testing.expect(cmd.context_signal.readOption(.type) == null);
try testing.expect(cmd.context_signal.readOption(.user) == null);
try testing.expect(cmd.context_signal.readOption(.exit) == null);
}
|