xref: /freebsd/usr.bin/talk/io.c (revision e1b4d8d0746069292d84708b0e11b17a7e1ef5e0)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
3520868061SPhilippe Charnier #if 0
369b50d902SRodney W. Grimes static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
3720868061SPhilippe Charnier #endif
3820868061SPhilippe Charnier static const char rcsid[] =
39c3aac50fSPeter Wemm   "$FreeBSD$";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes /*
439b50d902SRodney W. Grimes  * This file contains the I/O handling and the exchange of
449b50d902SRodney W. Grimes  * edit characters. This connection itself is established in
459b50d902SRodney W. Grimes  * ctl.c
469b50d902SRodney W. Grimes  */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <errno.h>
499b50d902SRodney W. Grimes #include <string.h>
50b3445392SPeter Wemm #include <sys/filio.h>
51e1b4d8d0SSheldon Hearn #include <unistd.h>
529b50d902SRodney W. Grimes #include "talk.h"
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes #define A_LONG_TIME 10000000
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes /*
579b50d902SRodney W. Grimes  * The routine to do the actual talking
589b50d902SRodney W. Grimes  */
5928592604SJoerg Wunsch void
609b50d902SRodney W. Grimes talk()
619b50d902SRodney W. Grimes {
6228592604SJoerg Wunsch 	int nb;
6328592604SJoerg Wunsch 	fd_set read_set, read_template;
649b50d902SRodney W. Grimes 	char buf[BUFSIZ];
659b50d902SRodney W. Grimes 	struct timeval wait;
669b50d902SRodney W. Grimes 
67e151ec23SPeter Wemm 	message("Connection established");
68e151ec23SPeter Wemm 	write(STDOUT_FILENO, "\007\007\007", 3);
69e151ec23SPeter Wemm 
709b50d902SRodney W. Grimes 	current_line = 0;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes 	/*
739b50d902SRodney W. Grimes 	 * Wait on both the other process (sockt_mask) and
749b50d902SRodney W. Grimes 	 * standard input ( STDIN_MASK )
759b50d902SRodney W. Grimes 	 */
7628592604SJoerg Wunsch 	FD_ZERO(&read_template);
7728592604SJoerg Wunsch 	FD_SET(sockt, &read_template);
7828592604SJoerg Wunsch 	FD_SET(fileno(stdin), &read_template);
799b50d902SRodney W. Grimes 	for (;;) {
809b50d902SRodney W. Grimes 		read_set = read_template;
819b50d902SRodney W. Grimes 		wait.tv_sec = A_LONG_TIME;
829b50d902SRodney W. Grimes 		wait.tv_usec = 0;
839b50d902SRodney W. Grimes 		nb = select(32, &read_set, 0, 0, &wait);
849b50d902SRodney W. Grimes 		if (nb <= 0) {
859b50d902SRodney W. Grimes 			if (errno == EINTR) {
869b50d902SRodney W. Grimes 				read_set = read_template;
879b50d902SRodney W. Grimes 				continue;
889b50d902SRodney W. Grimes 			}
899b50d902SRodney W. Grimes 			/* panic, we don't know what happened */
909b50d902SRodney W. Grimes 			p_error("Unexpected error from select");
919b50d902SRodney W. Grimes 			quit();
929b50d902SRodney W. Grimes 		}
9328592604SJoerg Wunsch 		if (FD_ISSET(sockt, &read_set)) {
949b50d902SRodney W. Grimes 			/* There is data on sockt */
959b50d902SRodney W. Grimes 			nb = read(sockt, buf, sizeof buf);
969b50d902SRodney W. Grimes 			if (nb <= 0) {
979b50d902SRodney W. Grimes 				message("Connection closed. Exiting");
989b50d902SRodney W. Grimes 				quit();
999b50d902SRodney W. Grimes 			}
1009b50d902SRodney W. Grimes 			display(&his_win, buf, nb);
1019b50d902SRodney W. Grimes 		}
10228592604SJoerg Wunsch 		if (FD_ISSET(fileno(stdin), &read_set)) {
1039b50d902SRodney W. Grimes 			/*
1049b50d902SRodney W. Grimes 			 * We can't make the tty non_blocking, because
1059b50d902SRodney W. Grimes 			 * curses's output routines would screw up
1069b50d902SRodney W. Grimes 			 */
10711128de9SPaul Saab 			int i;
1089b50d902SRodney W. Grimes 			ioctl(0, FIONREAD, (struct sgttyb *) &nb);
109e1b4d8d0SSheldon Hearn 			nb = read(STDIN_FILENO, buf, nb);
1109b50d902SRodney W. Grimes 			display(&my_win, buf, nb);
1119b50d902SRodney W. Grimes 			/* might lose data here because sockt is non-blocking */
11211128de9SPaul Saab 			for (i = 0; i < nb; ++i)
11311128de9SPaul Saab 				if (buf[i] == '\r')
11411128de9SPaul Saab 					buf[i] = '\n';
1159b50d902SRodney W. Grimes 			write(sockt, buf, nb);
1169b50d902SRodney W. Grimes 		}
1179b50d902SRodney W. Grimes 	}
1189b50d902SRodney W. Grimes }
1199b50d902SRodney W. Grimes 
1209b50d902SRodney W. Grimes /*
1219b50d902SRodney W. Grimes  * p_error prints the system error message on the standard location
1229b50d902SRodney W. Grimes  * on the screen and then exits. (i.e. a curses version of perror)
1239b50d902SRodney W. Grimes  */
12428592604SJoerg Wunsch void
1259b50d902SRodney W. Grimes p_error(string)
1269b50d902SRodney W. Grimes 	char *string;
1279b50d902SRodney W. Grimes {
128b02b5aadSAndrey A. Chernov 	wmove(my_win.x_win, current_line, 0);
1299b50d902SRodney W. Grimes 	wprintw(my_win.x_win, "[%s : %s (%d)]\n",
1309b50d902SRodney W. Grimes 	    string, strerror(errno), errno);
1319b50d902SRodney W. Grimes 	wrefresh(my_win.x_win);
1329b50d902SRodney W. Grimes 	move(LINES-1, 0);
1339b50d902SRodney W. Grimes 	refresh();
1349b50d902SRodney W. Grimes 	quit();
1359b50d902SRodney W. Grimes }
1369b50d902SRodney W. Grimes 
1379b50d902SRodney W. Grimes /*
1389b50d902SRodney W. Grimes  * Display string in the standard location
1399b50d902SRodney W. Grimes  */
14028592604SJoerg Wunsch void
1419b50d902SRodney W. Grimes message(string)
1429b50d902SRodney W. Grimes 	char *string;
1439b50d902SRodney W. Grimes {
144b02b5aadSAndrey A. Chernov 	wmove(my_win.x_win, current_line, 0);
145b02b5aadSAndrey A. Chernov 	wprintw(my_win.x_win, "[%s]\n", string);
146b02b5aadSAndrey A. Chernov 	if (current_line < my_win.x_nlines - 1)
1479b50d902SRodney W. Grimes 		current_line++;
1489b50d902SRodney W. Grimes 	wrefresh(my_win.x_win);
1499b50d902SRodney W. Grimes }
150