From 80b395c1a40d093128826b638d355665645af8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 24 Jul 2021 23:21:10 +0200 Subject: Move blacklisted function to end --- safetitle.c | 92 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/safetitle.c b/safetitle.c index 03a1456..3f92bee 100644 --- a/safetitle.c +++ b/safetitle.c @@ -1,6 +1,6 @@ /* - * safetitle sets the title of the terminal window to the given string, - * unless the terminal type is blacklisted. + * safetitle sets the title of the terminal window to the given string, + * unless the terminal type is blacklisted. */ #include @@ -18,9 +18,53 @@ #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 blacklisted(int); + int debug = 0; int screen = 0; +int +main(int argc, char *argv[]) +{ + char *ap; + int ttyfd; + struct termios term, restore; + + if(argc==2) + ap = argv[1]; + else if(argc==3){ + ap = argv[2]; + if(strcmp(argv[1], "-d")==0) + debug = 1; + else + goto usage; + }else{ +usage: fprintf(stderr, "usage: %s [-d] title\n", argv[0]); + return 1; + } + + if((ttyfd = open("/dev/tty", O_RDWR))==-1) + err(1, "open"); + + tcgetattr(ttyfd, &restore); + tcgetattr(ttyfd, &term); + term.c_lflag &= ~(ICANON|ECHO); + tcsetattr(ttyfd, TCSANOW, &term); + + if(blacklisted(ttyfd)) + goto end; + + /* set title */ + if(screen) dprintf(ttyfd, "\033P\033]2;%s\007\033\\", ap); + else dprintf(ttyfd, "\033]2;%s\007", ap); + + tcsetattr(ttyfd, TCSANOW, &restore); + return 0; +end: + tcsetattr(ttyfd, TCSANOW, &restore); + return 1; +} + int blacklisted(int fd) { @@ -94,47 +138,3 @@ check: return 0; } - -int -main(int argc, char *argv[]) -{ - char *ap; - int ttyfd; - struct termios term, restore; - - /* parse arguments */ - if(argc==2) - ap = argv[1]; - else if(argc==3){ - ap = argv[2]; - if(strcmp(argv[1], "-d")==0) debug = 1; - else goto usage; - } - else goto usage; - - if((ttyfd = open("/dev/tty", O_RDWR))==-1) - err(1, "open"); - - /* enter "raw" terminal mode */ - tcgetattr(ttyfd, &restore); - tcgetattr(ttyfd, &term); - term.c_lflag &= ~(ICANON|ECHO); - tcsetattr(ttyfd, TCSANOW, &term); - - /* get device attributes for real terminal */ - if(blacklisted(ttyfd)) - goto end; - - /* set title */ - if(screen) dprintf(ttyfd, "\033P\033]2;%s\007\033\\", ap); - else dprintf(ttyfd, "\033]2;%s\007", ap); - - tcsetattr(ttyfd, TCSANOW, &restore); - return 0; -end: - tcsetattr(ttyfd, TCSANOW, &restore); - return 1; -usage: - fprintf(stderr, "usage: %s [-d] title\n", argv[0]); - return 1; -} -- cgit v1.2.3