NetBSD/usr.bin/talk/io.c

142 lines
3.9 KiB
C
Raw Normal View History

/* $NetBSD: io.c,v 1.12 2006/10/07 17:27:57 elad Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: io.c,v 1.12 2006/10/07 17:27:57 elad Exp $");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* This file contains the I/O handling and the exchange of
* edit characters. This connection itself is established in
* ctl.c
*/
#include "talk.h"
#include <sys/ioctl.h>
#include <sys/time.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
2002-09-23 07:29:10 +04:00
#include <poll.h>
1993-03-21 12:45:37 +03:00
2002-09-18 23:16:09 +04:00
#define A_LONG_TIME 1000000
1993-03-21 12:45:37 +03:00
/*
* The routine to do the actual talking
*/
void
1993-03-21 12:45:37 +03:00
talk()
{
2002-09-18 23:16:09 +04:00
struct pollfd set[2];
int nb;
1993-03-21 12:45:37 +03:00
char buf[BUFSIZ];
message("Connection established\007\007\007");
current_line = 0;
/*
* Wait on both the other process (sockt_mask) and
* standard input ( STDIN_MASK )
*/
2002-09-18 23:16:09 +04:00
set[0].fd = sockt;
set[0].events = POLLIN;
set[1].fd = fileno(stdin);
set[1].events = POLLIN;
1993-03-21 12:45:37 +03:00
for (;;) {
2002-09-18 23:16:09 +04:00
nb = poll(set, 2, A_LONG_TIME * 1000);
1993-03-21 12:45:37 +03:00
if (nb <= 0) {
2002-09-18 23:16:09 +04:00
if (errno == EINTR)
1993-03-21 12:45:37 +03:00
continue;
/* panic, we don't know what happened */
p_error("Unexpected error from poll");
1993-03-21 12:45:37 +03:00
quit();
}
2002-09-18 23:16:09 +04:00
if (set[0].revents & POLLIN) {
1993-03-21 12:45:37 +03:00
/* There is data on sockt */
nb = read(sockt, buf, sizeof buf);
if (nb <= 0) {
message("Connection closed. Exiting");
quit();
}
display(&his_win, buf, nb);
}
2002-09-18 23:16:09 +04:00
if (set[1].revents & POLLIN) {
1993-03-21 12:45:37 +03:00
/*
* We can't make the tty non_blocking, because
* curses's output routines would screw up
*/
2002-12-06 06:21:43 +03:00
ioctl(0, FIONREAD, (void *) &nb);
1993-03-21 12:45:37 +03:00
nb = read(0, buf, nb);
display(&my_win, buf, nb);
/* might lose data here because sockt is non-blocking */
if (nb > 0)
write(sockt, buf, nb);
1993-03-21 12:45:37 +03:00
}
}
}
/*
* p_error prints the system error message on the standard location
* on the screen and then exits. (i.e. a curses version of perror)
*/
void
1993-03-21 12:45:37 +03:00
p_error(string)
char *string;
{
wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
wprintw(my_win.x_win, "[%s : %s (%d)]\n",
string, strerror(errno), errno);
wrefresh(my_win.x_win);
move(LINES-1, 0);
refresh();
quit();
}
/*
* Display string in the standard location
*/
void
1993-03-21 12:45:37 +03:00
message(string)
char *string;
{
wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
wprintw(my_win.x_win, "[%s]", string);
wclrtoeol(my_win.x_win);
current_line++;
wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
1993-03-21 12:45:37 +03:00
wrefresh(my_win.x_win);
}