]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ptyfwd: automatically turn off tinting/window title logic on dumb terminals 31162/head
authorLennart Poettering <lennart@poettering.net>
Wed, 7 Feb 2024 16:15:20 +0000 (17:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 9 Feb 2024 17:27:01 +0000 (18:27 +0100)
If we are not talking to a reasonable terminal let's not try to set the
window title or tint the background.

src/shared/ptyfwd.c
src/shared/ptyfwd.h

index f6082580a370018947b95b545aa6c6ed71580c64..654e719180a25ae8dbe7f2c0161e120307b951f2 100644 (file)
@@ -389,6 +389,9 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
         if (!f->background_color)
                 return 0;
 
+        if (FLAGS_SET(f->flags, PTY_FORWARD_DUMB_TERMINAL))
+                return 0;
+
         for (size_t i = offset; i < f->out_buffer_full; i++) {
                 char c = f->out_buffer[i];
 
@@ -484,7 +487,11 @@ static int do_shovel(PTYForward *f) {
 
         assert(f);
 
-        if (f->out_buffer_size == 0) {
+        if (f->out_buffer_size == 0 && !FLAGS_SET(f->flags, PTY_FORWARD_DUMB_TERMINAL)) {
+                /* If the output hasn't been allocated yet, we are at the beginning of the first
+                 * shovelling. Hence, possibly send some initial ANSI sequences. But do so only if we are
+                 * talking to an actual TTY. */
+
                 if (f->background_color) {
                         /* Erase the first line when we start */
                         f->out_buffer = background_color_sequence(f);
@@ -792,6 +799,10 @@ int pty_forward_new(
 
         f->master = master;
 
+        /* Disable color/window title setting unless we talk to a good TTY */
+        if (!isatty_safe(f->output_fd) || get_color_mode() == COLOR_OFF)
+                f->flags |= PTY_FORWARD_DUMB_TERMINAL;
+
         if (ioctl(f->output_fd, TIOCGWINSZ, &ws) < 0)
                 /* If we can't get the resolution from the output fd, then use our internal, regular width/height,
                  * i.e. something derived from $COLUMNS and $LINES if set. */
index 3f0d7811a25fa300bf1b27551d830cdd793ec7ec..f87becd0308b4f1afdf6c7121753d6cacaf1be39 100644 (file)
 typedef struct PTYForward PTYForward;
 
 typedef enum PTYForwardFlags {
-        PTY_FORWARD_READ_ONLY = 1,
+        /* Only output to STDOUT, never try to read from STDIN */
+        PTY_FORWARD_READ_ONLY              = 1 << 0,
 
         /* Continue reading after hangup? */
-        PTY_FORWARD_IGNORE_VHANGUP = 2,
+        PTY_FORWARD_IGNORE_VHANGUP         = 1 << 1,
 
         /* Continue reading after hangup but only if we never read anything else? */
-        PTY_FORWARD_IGNORE_INITIAL_VHANGUP = 4,
+        PTY_FORWARD_IGNORE_INITIAL_VHANGUP = 1 << 2,
+
+        /* Don't tint the background, or set window title */
+        PTY_FORWARD_DUMB_TERMINAL          = 1 << 3,
 } PTYForwardFlags;
 
 typedef int (*PTYForwardHandler)(PTYForward *f, int rcode, void *userdata);