summaryrefslogtreecommitdiff
path: root/sys/src/cmd/dial/pass.c
blob: b5b740bf814d77589dfb2d83aa731fc600e4b5d6 (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
#include <u.h>
#include <libc.h>

int alarmed;
int done;

void
usage(void)
{
	fprint(2, "usage: %s [-q]\n", argv0);
	exits("usage");
}

void
ding(void*, char *s)
{
	if(strstr(s, "alarm")){
		alarmed = 1;
		noted(NCONT);
	} else
		noted(NDFLT);
}


void
main(int argc, char **argv)
{
	int fd, cfd;
	int i;
	char buf[1];
	int quiet = 0;

	ARGBEGIN {
	case 'q':
		quiet = 1;
		break;
	} ARGEND;

	notify(ding);

	fd = open("/dev/cons", ORDWR);
	if(fd < 0)
		sysfatal("opening /dev/cons: %r");
	cfd = open("/dev/consctl", OWRITE);
	if(cfd >= 0)
		fprint(cfd, "rawon");

	switch(rfork(RFPROC|RFFDG|RFMEM)){
	case -1:
		sysfatal("forking: %r");
	default:
		// read until we're done writing or
		// we get an end of line
		while(!done){
			alarmed = 0;
			alarm(250);
			i = read(0, buf, 1);
			alarm(0);

			if(i == 0)
				break;
			if(i < 0){
				if(alarmed)
					continue;
				else
					break;
			}
			if(!quiet && write(fd, buf, 1) < 1)
				break;
			if(buf[0] == '\n' || buf[0] == '\r')
				break;
		}	
		break;
	case 0:
		// pass one character at a time till end of line
		for(;;){
			if(read(fd, buf, 1) <= 0)
				break;
			if(write(1, buf, 1) < 0)
				break;
			if(buf[0] == '\n' || buf[0] == '\r')
				break;
		}

		// tell reader to give up after next char
		done = 1;
		break;
	}
	exits(0);
}