loksh-noxz

[discontinued] a Linux port of OpenBSD's ksh
git clone https://noxz.tech/git/loksh-noxz.git
loksh-noxz

commit: 3bffa5e00a0832c02c15f2297924d286ba41ee0e
parent: b1863a7a8a9384b7c4791336080a1a0b3e768cee
author: Chris Noxz <chris@noxz.tech>
date:   Thu, 13 Jun 2024 16:05:52 +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
@@ -617,6 +617,12 @@ shell(Source *volatile s, volatile int toplevel)
 			j_notify();
 			mcheck();
 			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
@@ -475,6 +475,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