xref: /titanic_41/usr/src/cmd/cmd-inet/usr.bin/talk/io.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * this file contains the I/O handling and the exchange of
44  * edit characters. This connection itself is established in ctl.c
45  */
46 
47 #include "talk.h"
48 #include <stdio.h>
49 #include <errno.h>
50 #include <sys/time.h>
51 #include <sys/filio.h>
52 #include <libintl.h>
53 
54 #define	A_LONG_TIME 10000000
55 #define	STDIN_MASK (1<<fileno(stdin))	/* the bit mask for standard input */
56 extern int	errno;
57 extern int	sys_nerr;
58 
59 
60 /*
61  * The routine to do the actual talking
62  */
63 
64 void
65 talk()
66 {
67 	register int read_template, sockt_mask;
68 	int read_set, nb;
69 	char buf[BUFSIZ];
70 	struct timeval wait;
71 
72 	message(gettext("Connection established"));
73 	beep(); beep(); beep();
74 	current_line = 0;
75 
76 	sockt_mask = (1<<sockt);
77 
78 	/*
79 	 * wait on both the other process (sockt_mask) and
80 	 * standard input ( STDIN_MASK )
81 	 */
82 
83 	read_template = sockt_mask | STDIN_MASK;
84 
85 	forever {
86 
87 		read_set = read_template;
88 
89 		wait.tv_sec = A_LONG_TIME;
90 		wait.tv_usec = 0;
91 
92 		nb = select(32, (fd_set *)&read_set, 0, 0, &wait);
93 
94 		if (nb <= 0) {
95 
96 			/* We may be returning from an interrupt handler */
97 
98 			if (errno == EINTR) {
99 				read_set = read_template;
100 				continue;
101 			} else {
102 				/* panic, we don't know what happened */
103 				p_error(
104 				gettext("Unexpected error from select"));
105 				quit();
106 			}
107 		}
108 
109 		if (read_set & sockt_mask) {
110 
111 			/* There is data on sockt */
112 			nb = read(sockt, buf, sizeof (buf));
113 
114 			if (nb <= 0) {
115 				message(gettext("Connection closed. Exiting"));
116 				pause();	/* wait for Ctrl-C */
117 				quit();
118 			} else {
119 				display(&rem_win, buf, nb);
120 			}
121 		}
122 
123 		if (read_set & STDIN_MASK) {
124 
125 		/*
126 		 * we can't make the tty non_blocking, because
127 		 * curses's output routines would screw up
128 		 */
129 
130 			ioctl(0, FIONREAD, (struct sgttyb *)&nb);
131 			nb = read(0, buf, nb);
132 			display(&my_win, buf, nb);
133 			write(sockt, buf, nb);
134 
135 		/* We might lose data here because sockt is non-blocking */
136 
137 		}
138 	}
139 }
140 
141 
142 	/*
143 	 * p_error prints the system error message on the standard location
144 	 * on the screen and then exits. (i.e. a curses version of perror)
145 	 */
146 
147 void
148 p_error(string)
149 char *string;
150 {
151 	char *sys;
152 
153 	if (errno < sys_nerr) {
154 		sys = strerror(errno);
155 	} else {
156 		sys = gettext("Unknown error");
157 	}
158 
159 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
160 	wprintw(my_win.x_win, "[%s : %s (%d)]\n", string, sys, errno);
161 	wrefresh(my_win.x_win);
162 	move(LINES-1, 0);
163 	refresh();
164 	quit();
165 }
166 
167 	/* display string in the standard location */
168 
169 void
170 message(string)
171 char *string;
172 {
173 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
174 	wprintw(my_win.x_win, "[%s]\n", string);
175 	wrefresh(my_win.x_win);
176 }
177