122395Sdist /*
2*62303Sbostic * Copyright (c) 1983, 1993
3*62303Sbostic * The Regents of the University of California. All rights reserved.
434373Sbostic *
542770Sbostic * %sccs.include.redist.c%
622395Sdist */
722395Sdist
816364Skarels #ifndef lint
9*62303Sbostic static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 06/06/93";
1034373Sbostic #endif /* not lint */
1116351Skarels
1216364Skarels /*
1316364Skarels * This file contains the I/O handling and the exchange of
1416364Skarels * edit characters. This connection itself is established in
1516364Skarels * ctl.c
1616351Skarels */
1716351Skarels
1856104Selan #include <sys/ioctl.h>
1942418Sbostic #include <sys/time.h>
2016351Skarels #include <stdio.h>
2116351Skarels #include <errno.h>
2242418Sbostic #include <string.h>
2356104Selan #include "talk.h"
2416351Skarels
2516351Skarels #define A_LONG_TIME 10000000
2616351Skarels #define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard
2716351Skarels input */
2816351Skarels
2916351Skarels /*
3016351Skarels * The routine to do the actual talking
3116351Skarels */
talk()3216351Skarels talk()
3316351Skarels {
3416364Skarels register int read_template, sockt_mask;
3516364Skarels int read_set, nb;
3616364Skarels char buf[BUFSIZ];
3716364Skarels struct timeval wait;
3816351Skarels
3916364Skarels message("Connection established\007\007\007");
4016364Skarels current_line = 0;
4116364Skarels sockt_mask = (1<<sockt);
4216351Skarels
4316351Skarels /*
4416364Skarels * Wait on both the other process (sockt_mask) and
4516351Skarels * standard input ( STDIN_MASK )
4616351Skarels */
4716364Skarels read_template = sockt_mask | STDIN_MASK;
4846858Sbostic for (;;) {
4916351Skarels read_set = read_template;
5016364Skarels wait.tv_sec = A_LONG_TIME;
5116364Skarels wait.tv_usec = 0;
5216364Skarels nb = select(32, &read_set, 0, 0, &wait);
5316364Skarels if (nb <= 0) {
5416364Skarels if (errno == EINTR) {
5516364Skarels read_set = read_template;
5616364Skarels continue;
5716364Skarels }
5816364Skarels /* panic, we don't know what happened */
5916364Skarels p_error("Unexpected error from select");
6016364Skarels quit();
6116364Skarels }
6216364Skarels if (read_set & sockt_mask) {
6316364Skarels /* There is data on sockt */
6416364Skarels nb = read(sockt, buf, sizeof buf);
6516364Skarels if (nb <= 0) {
6616364Skarels message("Connection closed. Exiting");
6716364Skarels quit();
6816364Skarels }
6916364Skarels display(&his_win, buf, nb);
7016364Skarels }
7116364Skarels if (read_set & STDIN_MASK) {
7216364Skarels /*
7316364Skarels * We can't make the tty non_blocking, because
7416364Skarels * curses's output routines would screw up
7516364Skarels */
7616364Skarels ioctl(0, FIONREAD, (struct sgttyb *) &nb);
7716364Skarels nb = read(0, buf, nb);
7816364Skarels display(&my_win, buf, nb);
7916364Skarels /* might lose data here because sockt is non-blocking */
8016364Skarels write(sockt, buf, nb);
8116364Skarels }
8216351Skarels }
8316351Skarels }
8416351Skarels
8516364Skarels extern int errno;
8616364Skarels extern int sys_nerr;
8716351Skarels
8816364Skarels /*
8916364Skarels * p_error prints the system error message on the standard location
9016364Skarels * on the screen and then exits. (i.e. a curses version of perror)
9116364Skarels */
p_error(string)9216351Skarels p_error(string)
9316364Skarels char *string;
9416351Skarels {
9516364Skarels wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
9642418Sbostic wprintw(my_win.x_win, "[%s : %s (%d)]\n",
9742418Sbostic string, strerror(errno), errno);
9816364Skarels wrefresh(my_win.x_win);
9916364Skarels move(LINES-1, 0);
10016364Skarels refresh();
10116364Skarels quit();
10216351Skarels }
10316351Skarels
10416364Skarels /*
10516364Skarels * Display string in the standard location
10616364Skarels */
message(string)10716351Skarels message(string)
10816364Skarels char *string;
10916351Skarels {
11057852Selan wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
11157852Selan wprintw(my_win.x_win, "[%s]", string);
11257852Selan wclrtoeol(my_win.x_win);
11357852Selan current_line++;
11457852Selan wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
11516364Skarels wrefresh(my_win.x_win);
11616351Skarels }
117