diff options
-rw-r--r-- | safetitle.c | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/safetitle.c b/safetitle.c index bf302d3..7c24ab2 100644 --- a/safetitle.c +++ b/safetitle.c @@ -17,9 +17,11 @@ #include <termios.h> #include <unistd.h> -#define XTERM_DA (char[]){27,91,62,52,49,59,51,51,48,59,48,99,0} +#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 debug = 0; + int da(int fd, char *buf, int real) { @@ -33,13 +35,13 @@ da(int fd, char *buf, int real) timeout.tv_sec = 0; timeout.tv_usec = 0; responses = 0; - i = 0; + if(real) write(fd, "\033P\033[>c\033\\", 8); else write(fd, "\033[>c", 4); -read: while(r = read(fd, &c, 1)){ +loop: if(r==-1){ warn("read"); return -1; @@ -51,10 +53,15 @@ read: responses++; /* ensure there is only one real terminal */ - if(real && select(fd+1, &fds, NULL, NULL, &timeout)>0) - goto read; - if(responses>1) - buf[0] = '\0'; +check: + if(real && select(fd+1, &fds, NULL, NULL, &timeout)>0){ + if ((r = read(fd, &c, 1)) && c==0) goto check; /* extra nul */ + else goto loop; + } + if(responses>1){ + if(debug) fprintf(stderr, "more than one attached terminal\n"); + return -1; + } return i; } @@ -63,13 +70,12 @@ int main(int argc, char *argv[]) { char *ap, *buf; - int debug, r, screen, ttyfd; + int r, screen, ttyfd; struct termios term, restore; r = 1; /* parse arguments */ - debug = 0; if(argc==2) ap = argv[1]; else if(argc==3){ @@ -88,30 +94,30 @@ main(int argc, char *argv[]) tcgetattr(ttyfd, &restore); tcgetattr(ttyfd, &term); term.c_lflag &= ~(ICANON|ECHO); - tcsetattr(ttyfd, TCSANOW, &term); + tcsetattr(ttyfd, TCSAFLUSH, &term); /* get device attributes for real terminal */ - if(da(ttyfd, buf, 0)==-1) goto end; - if(strcmp(buf, SCREEN_DA)==0) - screen = 1; - else - screen = 0; - if(da(ttyfd, buf, screen)==-1) goto end; + if(da(ttyfd, buf, 0)==-1){ + goto end; + } + screen = strcmp(buf, SCREEN_DA)==0; + if(screen && da(ttyfd, buf, screen)==-1){ + goto end; + } /* set title */ - if(strcmp(buf, XTERM_DA)==0){ - if(screen) dprintf(ttyfd, "\033P\033]2;%s\007\033\\", ap); - else dprintf(ttyfd, "\033]2;%s\007", ap); - } - else{ + if(strcmp(buf, WSCONS_DA)==0){ if(debug) fprintf(stderr, "wrong type of terminal\n"); - r = 2; goto end; } + else{ + if(screen) dprintf(ttyfd, "\033P\033]2;%s\007\033\\", ap); + else dprintf(ttyfd, "\033]2;%s\007", ap); + } r = 0; end: - tcsetattr(ttyfd, TCSANOW, &restore); + tcsetattr(ttyfd, TCSAFLUSH, &restore); return r; usage: fprintf(stderr, "usage: %s [-d] title\n", argv[0]); |