8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / talk / io.c
blob36b9865a7fc40e5257a98fed18356084453bd06d
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * this file contains the I/O handling and the exchange of
44 * edit characters. This connection itself is established in ctl.c
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>
54 #define A_LONG_TIME 10000000
55 #define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard input */
58 * The routine to do the actual talking
61 void
62 talk()
64 int read_template, sockt_mask;
65 int read_set, nb;
66 char buf[BUFSIZ];
67 struct timeval wait;
69 message(gettext("Connection established"));
70 beep(); beep(); beep();
71 current_line = 0;
73 sockt_mask = (1<<sockt);
76 * wait on both the other process (sockt_mask) and
77 * standard input ( STDIN_MASK )
80 read_template = sockt_mask | STDIN_MASK;
82 forever {
84 read_set = read_template;
86 wait.tv_sec = A_LONG_TIME;
87 wait.tv_usec = 0;
89 nb = select(32, (fd_set *)&read_set, 0, 0, &wait);
91 if (nb <= 0) {
93 /* We may be returning from an interrupt handler */
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();
106 if (read_set & sockt_mask) {
108 /* There is data on sockt */
109 nb = read(sockt, buf, sizeof (buf));
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);
120 if (read_set & STDIN_MASK) {
123 * we can't make the tty non_blocking, because
124 * curses's output routines would screw up
127 ioctl(0, FIONREAD, (struct sgttyb *)&nb);
128 nb = read(0, buf, nb);
129 display(&my_win, buf, nb);
130 write(sockt, buf, nb);
133 * We might lose data here because sockt is
134 * non-blocking
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)
146 void
147 p_error(char *string)
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();
157 /* display string in the standard location */
159 void
160 message(char *string)
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);