xref: /freebsd/usr.bin/talk/io.c (revision 285926046a95179bfe85b70b3487f13ca791f8db)
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
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes /*
399b50d902SRodney W. Grimes  * This file contains the I/O handling and the exchange of
409b50d902SRodney W. Grimes  * edit characters. This connection itself is established in
419b50d902SRodney W. Grimes  * ctl.c
429b50d902SRodney W. Grimes  */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/ioctl.h>
459b50d902SRodney W. Grimes #include <sys/time.h>
469b50d902SRodney W. Grimes #include <stdio.h>
479b50d902SRodney W. Grimes #include <errno.h>
489b50d902SRodney W. Grimes #include <string.h>
499b50d902SRodney W. Grimes #include "talk.h"
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #define A_LONG_TIME 10000000
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes /*
549b50d902SRodney W. Grimes  * The routine to do the actual talking
559b50d902SRodney W. Grimes  */
5628592604SJoerg Wunsch void
579b50d902SRodney W. Grimes talk()
589b50d902SRodney W. Grimes {
5928592604SJoerg Wunsch 	int nb;
6028592604SJoerg Wunsch 	fd_set read_set, read_template;
619b50d902SRodney W. Grimes 	char buf[BUFSIZ];
629b50d902SRodney W. Grimes 	struct timeval wait;
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes 	message("Connection established\007\007\007");
659b50d902SRodney W. Grimes 	current_line = 0;
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes 	/*
689b50d902SRodney W. Grimes 	 * Wait on both the other process (sockt_mask) and
699b50d902SRodney W. Grimes 	 * standard input ( STDIN_MASK )
709b50d902SRodney W. Grimes 	 */
7128592604SJoerg Wunsch 	FD_ZERO(&read_template);
7228592604SJoerg Wunsch 	FD_SET(sockt, &read_template);
7328592604SJoerg Wunsch 	FD_SET(fileno(stdin), &read_template);
749b50d902SRodney W. Grimes 	for (;;) {
759b50d902SRodney W. Grimes 		read_set = read_template;
769b50d902SRodney W. Grimes 		wait.tv_sec = A_LONG_TIME;
779b50d902SRodney W. Grimes 		wait.tv_usec = 0;
789b50d902SRodney W. Grimes 		nb = select(32, &read_set, 0, 0, &wait);
799b50d902SRodney W. Grimes 		if (nb <= 0) {
809b50d902SRodney W. Grimes 			if (errno == EINTR) {
819b50d902SRodney W. Grimes 				read_set = read_template;
829b50d902SRodney W. Grimes 				continue;
839b50d902SRodney W. Grimes 			}
849b50d902SRodney W. Grimes 			/* panic, we don't know what happened */
859b50d902SRodney W. Grimes 			p_error("Unexpected error from select");
869b50d902SRodney W. Grimes 			quit();
879b50d902SRodney W. Grimes 		}
8828592604SJoerg Wunsch 		if (FD_ISSET(sockt, &read_set)) {
899b50d902SRodney W. Grimes 			/* There is data on sockt */
909b50d902SRodney W. Grimes 			nb = read(sockt, buf, sizeof buf);
919b50d902SRodney W. Grimes 			if (nb <= 0) {
929b50d902SRodney W. Grimes 				message("Connection closed. Exiting");
939b50d902SRodney W. Grimes 				quit();
949b50d902SRodney W. Grimes 			}
959b50d902SRodney W. Grimes 			display(&his_win, buf, nb);
969b50d902SRodney W. Grimes 		}
9728592604SJoerg Wunsch 		if (FD_ISSET(fileno(stdin), &read_set)) {
989b50d902SRodney W. Grimes 			/*
999b50d902SRodney W. Grimes 			 * We can't make the tty non_blocking, because
1009b50d902SRodney W. Grimes 			 * curses's output routines would screw up
1019b50d902SRodney W. Grimes 			 */
1029b50d902SRodney W. Grimes 			ioctl(0, FIONREAD, (struct sgttyb *) &nb);
1039b50d902SRodney W. Grimes 			nb = read(0, buf, nb);
1049b50d902SRodney W. Grimes 			display(&my_win, buf, nb);
1059b50d902SRodney W. Grimes 			/* might lose data here because sockt is non-blocking */
1069b50d902SRodney W. Grimes 			write(sockt, buf, nb);
1079b50d902SRodney W. Grimes 		}
1089b50d902SRodney W. Grimes 	}
1099b50d902SRodney W. Grimes }
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes extern	int errno;
1129b50d902SRodney W. Grimes extern	int sys_nerr;
1139b50d902SRodney W. Grimes 
1149b50d902SRodney W. Grimes /*
1159b50d902SRodney W. Grimes  * p_error prints the system error message on the standard location
1169b50d902SRodney W. Grimes  * on the screen and then exits. (i.e. a curses version of perror)
1179b50d902SRodney W. Grimes  */
11828592604SJoerg Wunsch void
1199b50d902SRodney W. Grimes p_error(string)
1209b50d902SRodney W. Grimes 	char *string;
1219b50d902SRodney W. Grimes {
122b02b5aadSAndrey A. Chernov 	wmove(my_win.x_win, current_line, 0);
1239b50d902SRodney W. Grimes 	wprintw(my_win.x_win, "[%s : %s (%d)]\n",
1249b50d902SRodney W. Grimes 	    string, strerror(errno), errno);
1259b50d902SRodney W. Grimes 	wrefresh(my_win.x_win);
1269b50d902SRodney W. Grimes 	move(LINES-1, 0);
1279b50d902SRodney W. Grimes 	refresh();
1289b50d902SRodney W. Grimes 	quit();
1299b50d902SRodney W. Grimes }
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes /*
1329b50d902SRodney W. Grimes  * Display string in the standard location
1339b50d902SRodney W. Grimes  */
13428592604SJoerg Wunsch void
1359b50d902SRodney W. Grimes message(string)
1369b50d902SRodney W. Grimes 	char *string;
1379b50d902SRodney W. Grimes {
138b02b5aadSAndrey A. Chernov 	wmove(my_win.x_win, current_line, 0);
139b02b5aadSAndrey A. Chernov 	wprintw(my_win.x_win, "[%s]\n", string);
140b02b5aadSAndrey A. Chernov 	if (current_line < my_win.x_nlines - 1)
1419b50d902SRodney W. Grimes 		current_line++;
1429b50d902SRodney W. Grimes 	wrefresh(my_win.x_win);
1439b50d902SRodney W. Grimes }
144