xref: /netbsd-src/usr.bin/talk/io.c (revision 68168df687839776c137f90832075bcd1dead31d)
1*68168df6Sjoerg /*	$NetBSD: io.c,v 1.14 2011/09/06 18:32:03 joerg Exp $	*/
251207773Sjtc 
361f28255Scgd /*
451207773Sjtc  * Copyright (c) 1983, 1993
551207773Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3232a9e65fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3451207773Sjtc #if 0
3551207773Sjtc static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
3651207773Sjtc #endif
37*68168df6Sjoerg __RCSID("$NetBSD: io.c,v 1.14 2011/09/06 18:32:03 joerg Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd 
4061f28255Scgd /*
4161f28255Scgd  * This file contains the I/O handling and the exchange of
4261f28255Scgd  * edit characters. This connection itself is established in
4361f28255Scgd  * ctl.c
4461f28255Scgd  */
4561f28255Scgd 
4632a9e65fSlukem #include "talk.h"
4765caf7fdSmycroft #include <sys/ioctl.h>
4851207773Sjtc #include <sys/time.h>
4961f28255Scgd #include <stdio.h>
5061f28255Scgd #include <errno.h>
5161f28255Scgd #include <string.h>
5232a9e65fSlukem #include <unistd.h>
536248b528Sitojun #include <poll.h>
5461f28255Scgd 
55675b884cSmycroft #define A_LONG_TIME 1000000
5661f28255Scgd 
5761f28255Scgd /*
5861f28255Scgd  * The routine to do the actual talking
5961f28255Scgd  */
6032a9e65fSlukem void
talk(void)61*68168df6Sjoerg talk(void)
6261f28255Scgd {
63675b884cSmycroft 	struct pollfd set[2];
6432a9e65fSlukem 	int nb;
6561f28255Scgd 	char buf[BUFSIZ];
6661f28255Scgd 
6761f28255Scgd 	message("Connection established\007\007\007");
6861f28255Scgd 	current_line = 0;
6961f28255Scgd 
7061f28255Scgd 	/*
7161f28255Scgd 	 * Wait on both the other process (sockt_mask) and
7261f28255Scgd 	 * standard input ( STDIN_MASK )
7361f28255Scgd 	 */
74675b884cSmycroft 	set[0].fd = sockt;
75675b884cSmycroft 	set[0].events = POLLIN;
76675b884cSmycroft 	set[1].fd = fileno(stdin);
77675b884cSmycroft 	set[1].events = POLLIN;
7861f28255Scgd 	for (;;) {
79675b884cSmycroft 		nb = poll(set, 2, A_LONG_TIME * 1000);
8061f28255Scgd 		if (nb <= 0) {
81675b884cSmycroft 			if (errno == EINTR)
8261f28255Scgd 				continue;
8361f28255Scgd 			/* panic, we don't know what happened */
841232ea27Selad 			p_error("Unexpected error from poll");
8561f28255Scgd 			quit();
8661f28255Scgd 		}
87675b884cSmycroft 		if (set[0].revents & POLLIN) {
8861f28255Scgd 			/* There is data on sockt */
8961f28255Scgd 			nb = read(sockt, buf, sizeof buf);
9061f28255Scgd 			if (nb <= 0) {
9161f28255Scgd 				message("Connection closed. Exiting");
9261f28255Scgd 				quit();
9361f28255Scgd 			}
9461f28255Scgd 			display(&his_win, buf, nb);
9561f28255Scgd 		}
96675b884cSmycroft 		if (set[1].revents & POLLIN) {
9761f28255Scgd 			/*
9861f28255Scgd 			 * We can't make the tty non_blocking, because
9961f28255Scgd 			 * curses's output routines would screw up
10061f28255Scgd 			 */
10129d4b591Sthorpej 			ioctl(0, FIONREAD, (void *) &nb);
10261f28255Scgd 			nb = read(0, buf, nb);
10361f28255Scgd 			display(&my_win, buf, nb);
10461f28255Scgd 			/* might lose data here because sockt is non-blocking */
105ccae083cSginsbach 			if (nb > 0)
10661f28255Scgd 				write(sockt, buf, nb);
10761f28255Scgd 		}
10861f28255Scgd 	}
10961f28255Scgd }
11061f28255Scgd 
11161f28255Scgd /*
11261f28255Scgd  * p_error prints the system error message on the standard location
11361f28255Scgd  * on the screen and then exits. (i.e. a curses version of perror)
11461f28255Scgd  */
11532a9e65fSlukem void
p_error(const char * string)116*68168df6Sjoerg p_error(const char *string)
11761f28255Scgd {
11861f28255Scgd 	wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
11961f28255Scgd 	wprintw(my_win.x_win, "[%s : %s (%d)]\n",
12061f28255Scgd 	    string, strerror(errno), errno);
12161f28255Scgd 	wrefresh(my_win.x_win);
12261f28255Scgd 	move(LINES-1, 0);
12361f28255Scgd 	refresh();
12461f28255Scgd 	quit();
12561f28255Scgd }
12661f28255Scgd 
12761f28255Scgd /*
12861f28255Scgd  * Display string in the standard location
12961f28255Scgd  */
13032a9e65fSlukem void
message(const char * string)131*68168df6Sjoerg message(const char *string)
13261f28255Scgd {
13361f28255Scgd 	wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
13451207773Sjtc 	wprintw(my_win.x_win, "[%s]", string);
13551207773Sjtc 	wclrtoeol(my_win.x_win);
13651207773Sjtc 	current_line++;
13751207773Sjtc 	wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
13861f28255Scgd 	wrefresh(my_win.x_win);
13961f28255Scgd }
140