oksh-noxz

[fork] Portable OpenBSD ksh, based on the Public Domain Korn Shell (pdksh).
git clone https://noxz.tech/git/oksh-noxz.git
oksh-noxz

commit: 920be4e0a0306a903328d259682741f2c77b29db
parent: 38a023f241e3c8a7dbc070c6a7415aad37a24b76
author: Chris Noxz <chris@noxz.tech>
date:   Tue, 9 Jul 2024 20:38:00 +0200
Add feature to set term title
Mio.c14++++++++++++++
Mlex.c13+++++++++++++
Mmain.c6++++++
Msh.h2++
4 files changed, 35 insertions(+)
diff --git a/io.c b/io.c
@@ -149,6 +149,20 @@ shellf(const char *fmt, ...)
 	shf_flush(shl_out);
 }
 
+/* printf to shl_out (stderr) without flush */
+void
+shellnof(const char *fmt, ...)
+{
+	va_list va;
+
+	if (!initio_done) /* shl_out may not be set up yet... */
+		return;
+	va_start(va, fmt);
+	shf_vfprintf(shl_out, fmt, va);
+	va_end(va);
+}
+
+
 /* printf to shl_stdout (stdout) */
 void
 shprintf(const char *fmt, ...)
diff --git a/lex.c b/lex.c
@@ -1157,6 +1157,19 @@ getsc_line(Source *s)
 			s->line++;
 			histsave(s->line, s->str, 1);
 		}
+		/* Set term title */
+		char *d = str_val(global("DISPLAY"));
+		char str[41];
+		memset(str, '\0', 41);
+		for (int i = 0; i < 40; i++) {
+			str[i] = p[i];
+			if ((str[i] > 0x7e) || (str[i] < 0x20)) {
+				str[i] = '\0';
+				break;
+			}
+		}
+		if (d[0])
+			shellf("%c]0;%s%c", '\033', str, '\007');
 	}
 	if (interactive)
 		set_prompt(PS2);
diff --git a/main.c b/main.c
@@ -627,6 +627,12 @@ shell(Source *volatile s, volatile int toplevel)
 			mcheck();
 #endif /* SMALL */
 			set_prompt(PS1);
+			/* Reset term title */
+			char *d = str_val(global("DISPLAY"));
+			if(d[0] > 0) {
+				shellnof("%c]0;ksh (%s)%c",
+					'\033', current_wd, '\007');
+			}
 		}
 
 		t = compile(s);
diff --git a/sh.h b/sh.h
@@ -474,6 +474,8 @@ void	internal_warningf(const char *, ...)
 void	error_prefix(int);
 void	shellf(const char *, ...)
 	    __attribute__((__format__ (printf, 1, 2)));
+void	shellnof(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));
 void	shprintf(const char *, ...)
 	    __attribute__((__format__ (printf, 1, 2)));
 #ifdef KSH_DEBUG