xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/talk/io.c (revision 672986541be54a7a471bb088e60780c37e371d7e)
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 2005 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 
57 /*
58  * The routine to do the actual talking
59  */
60 
61 void
62 talk()
63 {
64 	int read_template, sockt_mask;
65 	int read_set, nb;
66 	char buf[BUFSIZ];
67 	struct timeval wait;
68 
69 	message(gettext("Connection established"));
70 	beep(); beep(); beep();
71 	current_line = 0;
72 
73 	sockt_mask = (1<<sockt);
74 
75 	/*
76 	 * wait on both the other process (sockt_mask) and
77 	 * standard input ( STDIN_MASK )
78 	 */
79 
80 	read_template = sockt_mask | STDIN_MASK;
81 
82 	forever {
83 
84 		read_set = read_template;
85 
86 		wait.tv_sec = A_LONG_TIME;
87 		wait.tv_usec = 0;
88 
89 		nb = select(32, (fd_set *)&read_set, 0, 0, &wait);
90 
91 		if (nb <= 0) {
92 
93 			/* We may be returning from an interrupt handler */
94 
95 			if (errno == EINTR) {
96 				read_set = read_template;
97 				continue;
98 			} else {
99 				/* panic, we don't know what happened */
100 				p_error(
101 				gettext("Unexpected error from select"));
102 				quit();
103 			}
104 		}
105 
106 		if (read_set & sockt_mask) {
107 
108 			/* There is data on sockt */
109 			nb = read(sockt, buf, sizeof (buf));
110 
111 			if (nb <= 0) {
112 				message(gettext("Connection closed. Exiting"));
113 				pause();	/* wait for Ctrl-C */
114 				quit();
115 			} else {
116 				display(&rem_win, buf, nb);
117 			}
118 		}
119 
120 		if (read_set & STDIN_MASK) {
121 
122 			/*
123 			 * we can't make the tty non_blocking, because
124 			 * curses's output routines would screw up
125 			 */
126 
127 			ioctl(0, FIONREAD, (struct sgttyb *)&nb);
128 			nb = read(0, buf, nb);
129 			display(&my_win, buf, nb);
130 			write(sockt, buf, nb);
131 
132 			/*
133 			 * We might lose data here because sockt is
134 			 * non-blocking
135 			 */
136 		}
137 	}
138 }
139 
140 
141 /*
142  * p_error prints the system error message on the standard location
143  * on the screen and then exits. (i.e. a curses version of perror)
144  */
145 
146 void
147 p_error(char *string)
148 {
149 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
150 	wprintw(my_win.x_win, "[%s : %s]\n", string, strerror(errno));
151 	wrefresh(my_win.x_win);
152 	move(LINES-1, 0);
153 	refresh();
154 	quit();
155 }
156 
157 /* display string in the standard location */
158 
159 void
160 message(char *string)
161 {
162 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
163 	wprintw(my_win.x_win, "[%s]\n", string);
164 	wrefresh(my_win.x_win);
165 }
166