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