xref: /original-bsd/usr.bin/talk/io.c (revision 7323bcb8)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)io.c	5.3 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 /*
23  * This file contains the I/O handling and the exchange of
24  * edit characters. This connection itself is established in
25  * ctl.c
26  */
27 
28 #include "talk.h"
29 #include <stdio.h>
30 #include <errno.h>
31 #include <sys/time.h>
32 
33 #define A_LONG_TIME 10000000
34 #define STDIN_MASK (1<<fileno(stdin))	/* the bit mask for standard
35 					   input */
36 extern int errno;
37 
38 /*
39  * The routine to do the actual talking
40  */
41 talk()
42 {
43 	register int read_template, sockt_mask;
44 	int read_set, nb;
45 	char buf[BUFSIZ];
46 	struct timeval wait;
47 
48 	message("Connection established\007\007\007");
49 	current_line = 0;
50 	sockt_mask = (1<<sockt);
51 
52 	/*
53 	 * Wait on both the other process (sockt_mask) and
54 	 * standard input ( STDIN_MASK )
55 	 */
56 	read_template = sockt_mask | STDIN_MASK;
57 	forever {
58 		read_set = read_template;
59 		wait.tv_sec = A_LONG_TIME;
60 		wait.tv_usec = 0;
61 		nb = select(32, &read_set, 0, 0, &wait);
62 		if (nb <= 0) {
63 			if (errno == EINTR) {
64 				read_set = read_template;
65 				continue;
66 			}
67 			/* panic, we don't know what happened */
68 			p_error("Unexpected error from select");
69 			quit();
70 		}
71 		if (read_set & sockt_mask) {
72 			/* There is data on sockt */
73 			nb = read(sockt, buf, sizeof buf);
74 			if (nb <= 0) {
75 				message("Connection closed. Exiting");
76 				quit();
77 			}
78 			display(&his_win, buf, nb);
79 		}
80 		if (read_set & STDIN_MASK) {
81 			/*
82 			 * We can't make the tty non_blocking, because
83 			 * curses's output routines would screw up
84 			 */
85 			ioctl(0, FIONREAD, (struct sgttyb *) &nb);
86 			nb = read(0, buf, nb);
87 			display(&my_win, buf, nb);
88 			/* might lose data here because sockt is non-blocking */
89 			write(sockt, buf, nb);
90 		}
91 	}
92 }
93 
94 extern	int errno;
95 extern	int sys_nerr;
96 extern	char *sys_errlist[];
97 
98 /*
99  * p_error prints the system error message on the standard location
100  * on the screen and then exits. (i.e. a curses version of perror)
101  */
102 p_error(string)
103 	char *string;
104 {
105 	char *sys;
106 
107 	sys = "Unknown error";
108 	if (errno < sys_nerr)
109 		sys = sys_errlist[errno];
110 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
111 	wprintw(my_win.x_win, "[%s : %s (%d)]\n", string, sys, errno);
112 	wrefresh(my_win.x_win);
113 	move(LINES-1, 0);
114 	refresh();
115 	quit();
116 }
117 
118 /*
119  * Display string in the standard location
120  */
121 message(string)
122 	char *string;
123 {
124 
125 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
126 	wprintw(my_win.x_win, "[%s]\n", string);
127 	wrefresh(my_win.x_win);
128 }
129