aboutsummaryrefslogtreecommitdiff
path: root/safetitle.c
blob: b1e49b41f8375e90a9c582cefa5bcd321cd2c34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 *  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 0
#define SCREEN 1

#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 screen = 0;

int
blacklisted(int fd)
{
	char *buf, c;
	fd_set fds;
	int i, r;
	struct timeval timeout;

	/* setup */
	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){
			warn("read");
			return 1;
		}
		buf[i++] = c;
		if(c=='c') break;
	}
	buf[i] = '\0';
	screen = strcmp(buf, SCREEN_DA)==0;

	FD_ZERO(&fds);
	FD_SET(fd, &fds);
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;

	/* ignore strange trailing nul */
	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){
			warn("read");
			return 1;
		}
		buf[i++] = c;
		if(c==99) break;
	}
	buf[i] = '\0';

	if(strcmp(buf, WSCONS_DA)==0){
		if(debug) fprintf(stderr, "blacklisted terminal type\n");
		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;
	if(screen && select(fd+1, &fds, NULL, NULL, &timeout)>0){
		i = 0;
		if(read(fd, &c, 1)==-1){
			warn("read");
			return 1;
		}
		if(c==0) goto check; /* ignore strange trailing nul */
		else goto loop;
	}

	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;
}