xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/talk/io.c (revision 634:263e46dc023d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*634Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * this file contains the I/O handling and the exchange of
440Sstevel@tonic-gate  * edit characters. This connection itself is established in ctl.c
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include "talk.h"
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <sys/time.h>
510Sstevel@tonic-gate #include <sys/filio.h>
520Sstevel@tonic-gate #include <libintl.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	A_LONG_TIME 10000000
550Sstevel@tonic-gate #define	STDIN_MASK (1<<fileno(stdin))	/* the bit mask for standard input */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * The routine to do the actual talking
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate void
talk()620Sstevel@tonic-gate talk()
630Sstevel@tonic-gate {
64*634Sdp 	int read_template, sockt_mask;
650Sstevel@tonic-gate 	int read_set, nb;
660Sstevel@tonic-gate 	char buf[BUFSIZ];
670Sstevel@tonic-gate 	struct timeval wait;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	message(gettext("Connection established"));
700Sstevel@tonic-gate 	beep(); beep(); beep();
710Sstevel@tonic-gate 	current_line = 0;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	sockt_mask = (1<<sockt);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * wait on both the other process (sockt_mask) and
770Sstevel@tonic-gate 	 * standard input ( STDIN_MASK )
780Sstevel@tonic-gate 	 */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	read_template = sockt_mask | STDIN_MASK;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	forever {
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 		read_set = read_template;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 		wait.tv_sec = A_LONG_TIME;
870Sstevel@tonic-gate 		wait.tv_usec = 0;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 		nb = select(32, (fd_set *)&read_set, 0, 0, &wait);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		if (nb <= 0) {
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 			/* We may be returning from an interrupt handler */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 			if (errno == EINTR) {
960Sstevel@tonic-gate 				read_set = read_template;
970Sstevel@tonic-gate 				continue;
980Sstevel@tonic-gate 			} else {
990Sstevel@tonic-gate 				/* panic, we don't know what happened */
1000Sstevel@tonic-gate 				p_error(
1010Sstevel@tonic-gate 				gettext("Unexpected error from select"));
1020Sstevel@tonic-gate 				quit();
1030Sstevel@tonic-gate 			}
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		if (read_set & sockt_mask) {
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 			/* There is data on sockt */
1090Sstevel@tonic-gate 			nb = read(sockt, buf, sizeof (buf));
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 			if (nb <= 0) {
1120Sstevel@tonic-gate 				message(gettext("Connection closed. Exiting"));
1130Sstevel@tonic-gate 				pause();	/* wait for Ctrl-C */
1140Sstevel@tonic-gate 				quit();
1150Sstevel@tonic-gate 			} else {
1160Sstevel@tonic-gate 				display(&rem_win, buf, nb);
1170Sstevel@tonic-gate 			}
1180Sstevel@tonic-gate 		}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		if (read_set & STDIN_MASK) {
1210Sstevel@tonic-gate 
122*634Sdp 			/*
123*634Sdp 			 * we can't make the tty non_blocking, because
124*634Sdp 			 * curses's output routines would screw up
125*634Sdp 			 */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 			ioctl(0, FIONREAD, (struct sgttyb *)&nb);
1280Sstevel@tonic-gate 			nb = read(0, buf, nb);
1290Sstevel@tonic-gate 			display(&my_win, buf, nb);
1300Sstevel@tonic-gate 			write(sockt, buf, nb);
1310Sstevel@tonic-gate 
132*634Sdp 			/*
133*634Sdp 			 * We might lose data here because sockt is
134*634Sdp 			 * non-blocking
135*634Sdp 			 */
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 
141*634Sdp /*
142*634Sdp  * p_error prints the system error message on the standard location
143*634Sdp  * on the screen and then exits. (i.e. a curses version of perror)
144*634Sdp  */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate void
p_error(char * string)147*634Sdp p_error(char *string)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
150*634Sdp 	wprintw(my_win.x_win, "[%s : %s]\n", string, strerror(errno));
1510Sstevel@tonic-gate 	wrefresh(my_win.x_win);
1520Sstevel@tonic-gate 	move(LINES-1, 0);
1530Sstevel@tonic-gate 	refresh();
1540Sstevel@tonic-gate 	quit();
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
157*634Sdp /* display string in the standard location */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate void
message(char * string)160*634Sdp message(char *string)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
1630Sstevel@tonic-gate 	wprintw(my_win.x_win, "[%s]\n", string);
1640Sstevel@tonic-gate 	wrefresh(my_win.x_win);
1650Sstevel@tonic-gate }
166