From 3c2dfd4a842f839beffe003032f98a6d9a9283c5 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 9 Jan 2024 10:07:32 -0600 Subject: [PATCH] change name of config entry and variables, add lock for safety during config update --- docs/sequences/enq.md | 4 ++-- src/config/Config.zig | 2 +- src/termio/Exec.zig | 36 ++++++++++++++++++++++++------------ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/docs/sequences/enq.md b/docs/sequences/enq.md index 15ccaf957..f7d3adfdb 100644 --- a/docs/sequences/enq.md +++ b/docs/sequences/enq.md @@ -10,8 +10,8 @@ operator. ## Implementation Details -The answerback can be configured in the config file using the `enquiry-string` -configuration setting or on the command line using the `--enquiry-string` +The answerback can be configured in the config file using the `enquiry-response` +configuration setting or on the command line using the `--enquiry-response` parameter. The default is to send an empty string (`""`). ## References diff --git a/src/config/Config.zig b/src/config/Config.zig index 86c7e0c44..61b3f5b6c 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -898,7 +898,7 @@ term: []const u8 = "xterm-ghostty", /// String to send when we receive ENQ (0x05) from the command that we are /// running. Defaults to "" if not set. -@"enquiry-string": ?[]const u8 = null, +@"enquiry-response": ?[]const u8 = null, /// This is set by the CLI parser for deinit. _arena: ?ArenaAllocator = null, diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 66754f003..561eb76eb 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -51,7 +51,7 @@ abnormal_runtime_threshold_ms: u32, /// Equiry string - the string to send back when we receive a ENQ (0x05) /// from the process. -enquiry_string: ?[]const u8 = null, +enquiry_response: ?[]const u8 = null, /// The terminal emulator internal state. This is the abstract "terminal" /// that manages input, grid updating, etc. and is renderer-agnostic. It @@ -116,7 +116,7 @@ pub const DerivedConfig = struct { term: []const u8, grapheme_width_method: configpkg.Config.GraphemeWidthMethod, abnormal_runtime_threshold_ms: u32, - enquiry_string: ?[]const u8, + enquiry_response: ?[]const u8, pub fn init( alloc_gpa: Allocator, @@ -136,7 +136,7 @@ pub const DerivedConfig = struct { .term = config.term, .grapheme_width_method = config.@"grapheme-width-method", .abnormal_runtime_threshold_ms = config.@"abnormal-command-exit-runtime", - .enquiry_string = config.@"enquiry-string", + .enquiry_response = config.@"enquiry-response", }; } @@ -216,7 +216,7 @@ pub fn init(alloc: Allocator, opts: termio.Options) !Exec { .osc_color_report_format = config.osc_color_report_format, .data = null, .abnormal_runtime_threshold_ms = opts.config.abnormal_runtime_threshold_ms, - .enquiry_string = if (opts.config.enquiry_string) |s| try alloc.dupe(u8, s) else null, + .enquiry_response = if (opts.config.enquiry_response) |s| try alloc.dupe(u8, s) else null, }; } @@ -226,7 +226,7 @@ pub fn deinit(self: *Exec) void { // Clean up our other members self.terminal.deinit(self.alloc); - if (self.enquiry_string) |s| self.alloc.free(s); + if (self.enquiry_response) |s| self.alloc.free(s); } pub fn threadEnter(self: *Exec, thread: *termio.Thread) !ThreadData { @@ -306,7 +306,8 @@ pub fn threadEnter(self: *Exec, thread: *termio.Thread) !ThreadData { .foreground_color = self.foreground_color, .background_color = self.background_color, .osc_color_report_format = self.osc_color_report_format, - .enquiry_string = if (self.enquiry_string) |s| try self.alloc.dupe(u8, s) else null, + .enquiry_response = if (self.enquiry_response) |s| try self.alloc.dupe(u8, s) else null, + .enquiry_response_lock = .{}, }, .parser = .{ @@ -441,8 +442,12 @@ pub fn changeConfig(self: *Exec, config: *DerivedConfig) !void { config.cursor_style, config.cursor_blink, ); - if (data.terminal_stream.handler.enquiry_string) |s| data.terminal_stream.handler.alloc.free(s); - data.terminal_stream.handler.enquiry_string = if (config.enquiry_string) |s| try data.terminal_stream.handler.alloc.dupe(u8, s) else null; + { + data.terminal_stream.handler.enquiry_response_lock.lock(); + defer data.terminal_stream.handler.enquiry_response_lock.unlock(); + if (data.terminal_stream.handler.enquiry_response) |s| data.terminal_stream.handler.alloc.free(s); + data.terminal_stream.handler.enquiry_response = if (config.enquiry_response) |s| try data.terminal_stream.handler.alloc.dupe(u8, s) else null; + } } // Set the image size limits @@ -1707,12 +1712,13 @@ const StreamHandler = struct { osc_color_report_format: configpkg.Config.OSCColorReportFormat, - enquiry_string: ?[]const u8 = null, + enquiry_response: ?[]const u8, + enquiry_response_lock: std.Thread.RwLock, pub fn deinit(self: *StreamHandler) void { self.apc.deinit(); self.dcs.deinit(); - if (self.enquiry_string) |s| self.alloc.free(s); + if (self.enquiry_response) |s| self.alloc.free(s); } inline fn queueRender(self: *StreamHandler) !void { @@ -2405,8 +2411,14 @@ const StreamHandler = struct { } pub fn enquiry(self: *StreamHandler) !void { - log.debug("sending equiry string '{any}''", .{self.enquiry_string orelse ""}); - self.messageWriter(.{ .write_stable = self.enquiry_string orelse "" }); + self.enquiry_response_lock.lockShared(); + defer self.enquiry_response_lock.unlock(); + log.debug("sending enquiry response={any}", .{self.enquiry_response orelse ""}); + if (self.enquiry_response) |r| { + self.messageWriter(try termio.Message.writeReq(self.alloc, r)); + } else { + self.messageWriter(.{ .write_stable = "" }); + } } pub fn scrollDown(self: *StreamHandler, count: usize) !void {