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
|
#include <u.h>
#include <libc.h>
int c;
int
alarmed(void *a, char *msg)
{
USED(a);
USED(msg);
if(!c)
exits("timedout");
noted(NCONT);
return 1;
}
void
readline(int fd, char *buf, int nbuf)
{
int i, n;
i = 0;
while(i < nbuf-1){
n = read(fd, &c, sizeof c);
alarm(0);
c &= 0xff;
write(fd, &c, 1);
if(n != 1 || c == '\04' || c == '\177'){
i = 0;
break;
} else if(c == '\n')
break;
else if(c == '\b' && i > 0)
--i;
else if(c == ('u' & 037)){
c = '\b';
for(n=0; n <= i; n++)
write(fd, &c, 1);
i = 0;
} else
buf[i++] = c;
}
buf[i] = 0;
}
void
main(int argc, char *argv[])
{
int fd, ctl, i;
char buf[256];
long n;
if(argc < 2)
sysfatal("usage: tread timeout");
atnotify(alarmed, 1);
fd = open("/dev/cons", ORDWR);
if(fd < 0)
sysfatal("open cons: %r");
ctl = open("/dev/consctl", OWRITE);
if(ctl < 0)
sysfatal("open consctl: %r");
write(ctl, "rawon", 5);
alarm(atoi(argv[1])*1000);
readline(fd, buf, sizeof(buf));
close(ctl);
close(fd);
print("%s", buf);
exits(nil);
}
|