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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* Process in-band messages about window title changes.
* The messages are of the form:
*
* \033];xxx\007
*
* where xxx is the new directory. This format was chosen
* because it changes the label on xterm windows.
*/
#include <u.h>
#include <libc.h>
struct {
char *file;
char name[512];
} keep[] = {
{ "/dev/label" },
{ "/dev/wdir" }
};
char *prog = "/bin/rwd";
void
usage(void)
{
fprint(2, "usage: conswdir [/bin/rwd]\n");
exits("usage");
}
void
save(void)
{
int i, fd;
for(i = 0; i < nelem(keep); i++){
*keep[i].name = 0;
if((fd = open(keep[i].file, OREAD)) != -1){
read(fd, keep[i].name, sizeof(keep[i].name));
close(fd);
}
}
}
void
rest(void)
{
int i, fd;
for(i = 0; i < nelem(keep); i++)
if((fd = open(keep[i].file, OWRITE)) != -1){
write(fd, keep[i].name, strlen(keep[i].name));
close(fd);
}
}
void
setpath(char *s)
{
switch(rfork(RFPROC|RFFDG|RFNOWAIT)){
case 0:
execl(prog, prog, s, nil);
_exits(nil);
}
}
enum
{
None,
Esc,
Brack,
Semi,
Bell,
};
int
process(char *buf, int n, int *pn)
{
char *p;
char path[4096];
int start, state;
start = 0;
state = None;
for(p=buf; p<buf+n; p++){
switch(state){
case None:
if(*p == '\033'){
start = p-buf;
state++;
}
break;
case Esc:
if(*p == ']')
state++;
else
state = None;
break;
case Brack:
if(*p == ';')
state++;
else
state = None;
break;
case Semi:
if(*p == '\007')
state++;
else if((uchar)*p < 040)
state = None;
break;
}
if(state == Bell){
memmove(path, buf+start+3, p - (buf+start+3));
path[p-(buf+start+3)] = 0;
p++;
memmove(buf+start, p, n-(p-buf));
n -= p-(buf+start);
p = buf+start;
p--;
start = 0;
state = None;
setpath(path);
}
}
/* give up if we go too long without seeing the close */
*pn = n;
if(state == None || p-(buf+start) >= 2048)
return (p - buf);
else
return start;
}
static void
catchint(void*, char *msg)
{
if(strstr(msg, "interrupt"))
noted(NCONT);
else if(strstr(msg, "kill"))
noted(NDFLT);
else
noted(NCONT);
}
void
main(int argc, char **argv)
{
char buf[4096];
int n, m;
notify(catchint);
ARGBEGIN{
default:
usage();
}ARGEND
if(argc > 1)
usage();
if(argc == 1)
prog = argv[0];
save();
n = 0;
for(;;){
m = read(0, buf+n, sizeof buf-n);
if(m < 0){
rerrstr(buf, sizeof buf);
if(strstr(buf, "interrupt"))
continue;
break;
}
n += m;
m = process(buf, n, &n);
if(m > 0){
write(1, buf, m);
memmove(buf, buf+m, n-m);
n -= m;
}
}
rest();
exits(nil);
}
|