file reorg, pathnames.h, paths.h
[unix-history] / usr / src / usr.bin / talk / io.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
f9ac90b4
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
d0aeaf5a
DF
16 */
17
963ce42e 18#ifndef lint
b36fc510 19static char sccsid[] = "@(#)io.c 5.3 (Berkeley) %G%";
f9ac90b4 20#endif /* not lint */
e851d673 21
963ce42e
MK
22/*
23 * This file contains the I/O handling and the exchange of
24 * edit characters. This connection itself is established in
25 * ctl.c
e851d673
MK
26 */
27
28#include "talk.h"
29#include <stdio.h>
30#include <errno.h>
31#include <sys/time.h>
32
33#define A_LONG_TIME 10000000
34#define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard
35 input */
36extern int errno;
37
38/*
39 * The routine to do the actual talking
40 */
e851d673
MK
41talk()
42{
963ce42e
MK
43 register int read_template, sockt_mask;
44 int read_set, nb;
45 char buf[BUFSIZ];
46 struct timeval wait;
e851d673 47
963ce42e
MK
48 message("Connection established\007\007\007");
49 current_line = 0;
50 sockt_mask = (1<<sockt);
e851d673
MK
51
52 /*
963ce42e 53 * Wait on both the other process (sockt_mask) and
e851d673
MK
54 * standard input ( STDIN_MASK )
55 */
963ce42e
MK
56 read_template = sockt_mask | STDIN_MASK;
57 forever {
e851d673 58 read_set = read_template;
963ce42e
MK
59 wait.tv_sec = A_LONG_TIME;
60 wait.tv_usec = 0;
61 nb = select(32, &read_set, 0, 0, &wait);
62 if (nb <= 0) {
63 if (errno == EINTR) {
64 read_set = read_template;
65 continue;
66 }
67 /* panic, we don't know what happened */
68 p_error("Unexpected error from select");
69 quit();
70 }
71 if (read_set & sockt_mask) {
72 /* There is data on sockt */
73 nb = read(sockt, buf, sizeof buf);
74 if (nb <= 0) {
75 message("Connection closed. Exiting");
76 quit();
77 }
78 display(&his_win, buf, nb);
79 }
80 if (read_set & STDIN_MASK) {
81 /*
82 * We can't make the tty non_blocking, because
83 * curses's output routines would screw up
84 */
85 ioctl(0, FIONREAD, (struct sgttyb *) &nb);
86 nb = read(0, buf, nb);
87 display(&my_win, buf, nb);
88 /* might lose data here because sockt is non-blocking */
89 write(sockt, buf, nb);
90 }
e851d673 91 }
e851d673
MK
92}
93
963ce42e
MK
94extern int errno;
95extern int sys_nerr;
96extern char *sys_errlist[];
e851d673 97
963ce42e
MK
98/*
99 * p_error prints the system error message on the standard location
100 * on the screen and then exits. (i.e. a curses version of perror)
101 */
e851d673 102p_error(string)
963ce42e 103 char *string;
e851d673 104{
963ce42e
MK
105 char *sys;
106
107 sys = "Unknown error";
108 if (errno < sys_nerr)
109 sys = sys_errlist[errno];
110 wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
111 wprintw(my_win.x_win, "[%s : %s (%d)]\n", string, sys, errno);
112 wrefresh(my_win.x_win);
113 move(LINES-1, 0);
114 refresh();
115 quit();
e851d673
MK
116}
117
963ce42e
MK
118/*
119 * Display string in the standard location
120 */
e851d673 121message(string)
963ce42e 122 char *string;
e851d673 123{
963ce42e
MK
124
125 wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
126 wprintw(my_win.x_win, "[%s]\n", string);
127 wrefresh(my_win.x_win);
e851d673 128}