xref: /original-bsd/old/talk/talk/io.c (revision e58c8952)
1 /*-
2  * Copyright (c) 1983, 1985
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)io.c	5.1 (Berkeley) 6/6/85";
10 #endif not lint
11 
12 /*
13  * This file contains the I/O handling and the exchange of
14  * edit characters. This connection itself is established in
15  * ctl.c
16  */
17 
18 #include "talk.h"
19 #include <sys/ioctl.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <sys/time.h>
23 
24 #define A_LONG_TIME 10000000
25 #define STDIN_MASK (1<<fileno(stdin))	/* the bit mask for standard
26 					   input */
27 extern int errno;
28 
29 /*
30  * The routine to do the actual talking
31  */
32 talk()
33 {
34 	register int read_template, sockt_mask;
35 	int read_set, nb;
36 	char buf[BUFSIZ];
37 	struct timeval wait;
38 
39 	message("Connection established\007\007\007");
40 	current_line = 0;
41 	sockt_mask = (1<<sockt);
42 
43 	/*
44 	 * Wait on both the other process (sockt_mask) and
45 	 * standard input ( STDIN_MASK )
46 	 */
47 	read_template = sockt_mask | STDIN_MASK;
48 	forever {
49 		read_set = read_template;
50 		wait.tv_sec = A_LONG_TIME;
51 		wait.tv_usec = 0;
52 		nb = select(32, &read_set, 0, 0, &wait);
53 		if (nb <= 0) {
54 			if (errno == EINTR) {
55 				read_set = read_template;
56 				continue;
57 			}
58 			/* panic, we don't know what happened */
59 			p_error("Unexpected error from select");
60 			quit();
61 		}
62 		if (read_set & sockt_mask) {
63 			/* There is data on sockt */
64 			nb = read(sockt, buf, sizeof buf);
65 			if (nb <= 0) {
66 				message("Connection closed. Exiting");
67 				quit();
68 			}
69 			display(&his_win, buf, nb);
70 		}
71 		if (read_set & STDIN_MASK) {
72 			/*
73 			 * We can't make the tty non_blocking, because
74 			 * curses's output routines would screw up
75 			 */
76 			ioctl(0, FIONREAD, (struct sgttyb *) &nb);
77 			nb = read(0, buf, nb);
78 			display(&my_win, buf, nb);
79 			/* might lose data here because sockt is non-blocking */
80 			write(sockt, buf, nb);
81 		}
82 	}
83 }
84 
85 extern	int errno;
86 extern	int sys_nerr;
87 extern	char *sys_errlist[];
88 
89 /*
90  * p_error prints the system error message on the standard location
91  * on the screen and then exits. (i.e. a curses version of perror)
92  */
93 p_error(string)
94 	char *string;
95 {
96 	char *sys;
97 
98 	sys = "Unknown error";
99 	if (errno < sys_nerr)
100 		sys = sys_errlist[errno];
101 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
102 	wprintw(my_win.x_win, "[%s : %s (%d)]\n", string, sys, errno);
103 	wrefresh(my_win.x_win);
104 	move(LINES-1, 0);
105 	refresh();
106 	quit();
107 }
108 
109 /*
110  * Display string in the standard location
111  */
112 message(string)
113 	char *string;
114 {
115 
116 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
117 	wprintw(my_win.x_win, "[%s]\n", string);
118 	wrefresh(my_win.x_win);
119 }
120