aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--safetitle.c92
1 files 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 <err.h>
@@ -18,10 +18,54 @@
#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)
{
char *buf, c;
@@ -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;
-}