aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-05 01:50:21 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-05 01:50:21 +0200
commit596a41139c08e172459d28fba0584f8ef6f576b5 (patch)
tree07696ce0652413a3b2d64df61db38c35f2ea80e8
parent40e500c009bd65b7305ea95a3e61db88d5886181 (diff)
downloadnoice-596a41139c08e172459d28fba0584f8ef6f576b5.tar.gz
Set title on path change (safely)
-rw-r--r--Makefile6
-rw-r--r--noice.c5
-rw-r--r--safetitle.c104
-rw-r--r--safetitle.h1
-rw-r--r--safetitle.obin0 -> 2452 bytes
5 files changed, 113 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index bb84dd0..461937e 100644
--- a/Makefile
+++ b/Makefile
@@ -7,9 +7,9 @@ MANPREFIX = $(PREFIX)/man
#CFLAGS = -g
LDLIBS = -lcurses
-DISTFILES = noice.c strlcat.c strlcpy.c util.h config.def.h\
+DISTFILES = noice.c strlcat.c strlcpy.c safetitle.h util.h config.def.h\
noice.1 Makefile README LICENSE
-OBJ = noice.o strlcat.o strlcpy.o
+OBJ = noice.o strlcat.o strlcpy.o safetitle.o
BIN = noice
all: $(BIN)
@@ -17,7 +17,7 @@ all: $(BIN)
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(LDLIBS)
-noice.o: util.h config.h
+noice.o: safetitle.h util.h config.h
strlcat.o: util.h
strlcpy.o: util.h
diff --git a/noice.c b/noice.c
index 4340ab7..68485db 100644
--- a/noice.c
+++ b/noice.c
@@ -19,6 +19,7 @@
#include <string.h>
#include <unistd.h>
+#include "safetitle.h"
#include "util.h"
#ifdef DEBUG
@@ -698,6 +699,7 @@ nochange:
/* Save history */
strlcpy(oldpath, path, sizeof(oldpath));
strlcpy(path, dir, sizeof(path));
+ safetitle(path);
/* Reset filter */
strlcpy(fltr, ifilter, sizeof(fltr));
goto begin;
@@ -731,6 +733,7 @@ nochange:
goto nochange;
}
strlcpy(path, newpath, sizeof(path));
+ safetitle(path);
/* Reset filter */
strlcpy(fltr, ifilter, sizeof(fltr));
goto begin;
@@ -801,6 +804,7 @@ nochange:
goto nochange;
}
strlcpy(path, newpath, sizeof(path));
+ safetitle(path);
/* Reset filter */
strlcpy(fltr, ifilter, sizeof(fltr));
DPRINTF_S(path);
@@ -816,6 +820,7 @@ nochange:
goto nochange;
}
strlcpy(path, tmp, sizeof(path));
+ safetitle(path);
/* Reset filter */
strlcpy(fltr, ifilter, sizeof(fltr));
DPRINTF_S(path);
diff --git a/safetitle.c b/safetitle.c
new file mode 100644
index 0000000..b9d8be0
--- /dev/null
+++ b/safetitle.c
@@ -0,0 +1,104 @@
+/*
+ * safetitle sets the title of the terminal window to the given string,
+ * unless the terminal type is blacklisted.
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/select.h>
+#include <termios.h>
+#include <unistd.h>
+
+#define WSCONS_DA (char[]){27,91,62,50,52,59,50,48,59,48,99,0}
+#define SCREEN_DA (char[]){27,91,62,56,51,59,52,48,56,48,48,59,48,99,0}
+
+int screen = 0;
+
+int
+blacklisted(int fd)
+{
+ char *buf, c;
+ fd_set fds;
+ int i, r;
+ struct timeval timeout;
+
+ if((buf = malloc(200*sizeof(char)))==NULL)
+ err(1, "malloc");
+
+ /* check if GNU screen */
+ write(fd, "\033[>c", 4);
+ i = 0;
+ while(r = read(fd, &c, 1)){
+ if(r==-1)
+ return 1;
+ buf[i++] = c;
+ if(c=='c') break;
+ }
+ buf[i] = '\0';
+ screen = strcmp(buf, SCREEN_DA)==0;
+
+ /* ignore strange trailing nul before reading response */
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0;
+ while(select(fd+1, &fds, NULL, NULL, &timeout)>0)
+ read(fd, &c, 1);
+
+ /* check (real) terminal type(s) */
+ if(screen) write(fd, "\033P\033[>c\033\\", 8);
+ else write(fd, "\033[>c", 4);
+
+ i = 0;
+ while(r = read(fd, &c, 1)){
+loop:
+ if(r==-1)
+ return 1;
+ buf[i++] = c;
+ if(c==99) break;
+ }
+ buf[i] = '\0';
+
+ if(strcmp(buf, WSCONS_DA)==0)
+ return 1;
+
+ /* go back and check remaining responses (if any) */
+check:
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 1; /* it takes a millisecond sometimes */
+ if(screen && select(fd+1, &fds, NULL, NULL, &timeout)>0){
+ /* ignore strange trailing nul before reading response */
+ if(read(fd, &c, 1)==-1)
+ return 1;
+ i = 0;
+ if(c==0) goto check;
+ else goto loop;
+ }
+
+ return 0;
+}
+
+void
+safetitle(char *title)
+{
+ int ttyfd;
+
+ if((ttyfd = open("/dev/tty", O_RDWR))==-1)
+ err(1, "open");
+
+ /* get device attributes for real terminal */
+ if(blacklisted(ttyfd))
+ return;
+
+ if(strncmp(title, "/home/", 6)==0)
+ title += 6;
+
+ /* set title */
+ if(screen) dprintf(ttyfd, "\033P\033]2;%s\007\033\\", title);
+ else dprintf(ttyfd, "\033]2;%s\007", title);
+}
diff --git a/safetitle.h b/safetitle.h
new file mode 100644
index 0000000..9d72423
--- /dev/null
+++ b/safetitle.h
@@ -0,0 +1 @@
+void safetitle(char *);
diff --git a/safetitle.o b/safetitle.o
new file mode 100644
index 0000000..a3e7abf
--- /dev/null
+++ b/safetitle.o
Binary files differ