aboutsummaryrefslogtreecommitdiff
path: root/rin.c
blob: 6af10b88907135bfafd0685863ff0f232170c9f7 (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
#include <curses.h>
#include <err.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>

#define MAXBUF 1024

int
main()
{
	fd_set rfds;
	int bufin[MAXBUF], bufout[MAXBUF], c, in, nin, nout, out, startx, y;
	struct timeval tv;
	WINDOW *w;

	w = initscr();
	raw();
	noecho();
	timeout(1);

	if((in = open("/var/tmp/r.in", O_RDWR)) == -1)
		err(1, "/var/tmp/r.in");
	if((out = open("/var/tmp/r.out", O_RDWR)) == -1)
		err(1, "/var/tmp/r.out");

	tv.tv_sec = 0;
	tv.tv_usec = 0;
	FD_ZERO(&rfds);

	startx = 0;
	nin = 0;

	for(;;){
		FD_SET(out, &rfds);
		if(select(out+1, &rfds, NULL, NULL, &tv) > 0){
			nout = read(out, bufout, MAXBUF);
			bufout[nout+1] = 0;
			printw("%s", bufout);
			refresh();

			startx = -1;
		}
		if((c = getch()) != ERR){
			if(startx == -1)
				startx = getcurx(w);
			if(c == 26) /* ^Z */
				break;
			if(c == 21){ /* ^U */
reset:				bufin[0] = nin = 0;
				y = getcury(w);
				move(y, startx);
				refresh();
				continue;
			}

			bufin[nin++] = c;

			if(c == '\n'){
				bufin[nin] = 0;
				dprintf(in, "%s", bufin);
				goto reset;
			}else{
				printw("%c", c);
				refresh();
			}
		}
	}

	echo();
	noraw();
	endwin();
}