]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/dial/pass.c
Import sources from 2011-03-30 iso image
[plan9front.git] / sys / src / cmd / dial / pass.c
1 #include <u.h>
2 #include <libc.h>
3
4 int alarmed;
5 int done;
6
7 void
8 usage(void)
9 {
10         fprint(2, "usage: %s [-q]\n", argv0);
11         exits("usage");
12 }
13
14 void
15 ding(void*, char *s)
16 {
17         if(strstr(s, "alarm")){
18                 alarmed = 1;
19                 noted(NCONT);
20         } else
21                 noted(NDFLT);
22 }
23
24
25 void
26 main(int argc, char **argv)
27 {
28         int fd, cfd;
29         int i;
30         char buf[1];
31         int quiet = 0;
32
33         ARGBEGIN {
34         case 'q':
35                 quiet = 1;
36                 break;
37         } ARGEND;
38
39         notify(ding);
40
41         fd = open("/dev/cons", ORDWR);
42         if(fd < 0)
43                 sysfatal("opening /dev/cons: %r");
44         cfd = open("/dev/consctl", OWRITE);
45         if(cfd >= 0)
46                 fprint(cfd, "rawon");
47
48         switch(rfork(RFPROC|RFFDG|RFMEM)){
49         case -1:
50                 sysfatal("forking: %r");
51         default:
52                 // read until we're done writing or
53                 // we get an end of line
54                 while(!done){
55                         alarmed = 0;
56                         alarm(250);
57                         i = read(0, buf, 1);
58                         alarm(0);
59
60                         if(i == 0)
61                                 break;
62                         if(i < 0){
63                                 if(alarmed)
64                                         continue;
65                                 else
66                                         break;
67                         }
68                         if(!quiet && write(fd, buf, 1) < 1)
69                                 break;
70                         if(buf[0] == '\n' || buf[0] == '\r')
71                                 break;
72                 }       
73                 break;
74         case 0:
75                 // pass one character at a time till end of line
76                 for(;;){
77                         if(read(fd, buf, 1) <= 0)
78                                 break;
79                         if(write(1, buf, 1) < 0)
80                                 break;
81                         if(buf[0] == '\n' || buf[0] == '\r')
82                                 break;
83                 }
84
85                 // tell reader to give up after next char
86                 done = 1;
87                 break;
88         }
89         exits(0);
90 }