1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1983, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
32caa64801SMark Murray
339b50d902SRodney W. Grimes
349b50d902SRodney W. Grimes /*
359b50d902SRodney W. Grimes * This file contains the I/O handling and the exchange of
369b50d902SRodney W. Grimes * edit characters. This connection itself is established in
379b50d902SRodney W. Grimes * ctl.c
389b50d902SRodney W. Grimes */
399b50d902SRodney W. Grimes
40caa64801SMark Murray #include <sys/filio.h>
41caa64801SMark Murray
429b50d902SRodney W. Grimes #include <errno.h>
43ec8ffccbSOlivier Houchard #include <signal.h>
44e896c344SDima Dorfman #include <netdb.h>
457880fc11SPedro F. Giffuni #include <poll.h>
4605b0160eSGleb Smirnoff #include <stdio.h>
47e896c344SDima Dorfman #include <stdlib.h>
489b50d902SRodney W. Grimes #include <string.h>
49e1b4d8d0SSheldon Hearn #include <unistd.h>
5005b0160eSGleb Smirnoff #define _XOPEN_SOURCE_EXTENDED
5105b0160eSGleb Smirnoff #include <curses.h>
52caa64801SMark Murray
539b50d902SRodney W. Grimes #include "talk.h"
54e896c344SDima Dorfman #include "talk_ctl.h"
559b50d902SRodney W. Grimes
5605b0160eSGleb Smirnoff extern void display(xwin_t *, wchar_t *);
579b50d902SRodney W. Grimes
58ec8ffccbSOlivier Houchard volatile sig_atomic_t gotwinch = 0;
59ec8ffccbSOlivier Houchard
609b50d902SRodney W. Grimes /*
619b50d902SRodney W. Grimes * The routine to do the actual talking
629b50d902SRodney W. Grimes */
6328592604SJoerg Wunsch void
talk(void)64b6d9e1f3SXin LI talk(void)
659b50d902SRodney W. Grimes {
66e896c344SDima Dorfman struct hostent *hp, *hp2;
677880fc11SPedro F. Giffuni struct pollfd fds[2];
6828592604SJoerg Wunsch int nb;
6905b0160eSGleb Smirnoff wchar_t buf[BUFSIZ];
7005b0160eSGleb Smirnoff char **addr, *his_machine_name;
7105b0160eSGleb Smirnoff FILE *sockfp;
729b50d902SRodney W. Grimes
73e896c344SDima Dorfman his_machine_name = NULL;
74e896c344SDima Dorfman hp = gethostbyaddr((const char *)&his_machine_addr.s_addr,
75e896c344SDima Dorfman sizeof(his_machine_addr.s_addr), AF_INET);
76e896c344SDima Dorfman if (hp != NULL) {
77e896c344SDima Dorfman hp2 = gethostbyname(hp->h_name);
78e896c344SDima Dorfman if (hp2 != NULL && hp2->h_addrtype == AF_INET &&
79e896c344SDima Dorfman hp2->h_length == sizeof(his_machine_addr))
80e896c344SDima Dorfman for (addr = hp2->h_addr_list; *addr != NULL; addr++)
81e896c344SDima Dorfman if (memcmp(*addr, &his_machine_addr,
82e896c344SDima Dorfman sizeof(his_machine_addr)) == 0) {
83e896c344SDima Dorfman his_machine_name = strdup(hp->h_name);
84e896c344SDima Dorfman break;
85e896c344SDima Dorfman }
86e896c344SDima Dorfman }
87e896c344SDima Dorfman if (his_machine_name == NULL)
88e896c344SDima Dorfman his_machine_name = strdup(inet_ntoa(his_machine_addr));
8905b0160eSGleb Smirnoff snprintf((char *)buf, sizeof(buf), "Connection established with %s@%s.",
90e896c344SDima Dorfman msg.r_name, his_machine_name);
91e896c344SDima Dorfman free(his_machine_name);
9205b0160eSGleb Smirnoff message((char *)buf);
93e151ec23SPeter Wemm write(STDOUT_FILENO, "\007\007\007", 3);
94e151ec23SPeter Wemm
959b50d902SRodney W. Grimes current_line = 0;
969b50d902SRodney W. Grimes
9705b0160eSGleb Smirnoff if ((sockfp = fdopen(sockt, "w+")) == NULL)
9805b0160eSGleb Smirnoff p_error("fdopen");
9905b0160eSGleb Smirnoff
10005b0160eSGleb Smirnoff setvbuf(sockfp, NULL, _IONBF, 0);
10105b0160eSGleb Smirnoff setvbuf(stdin, NULL, _IONBF, 0);
10205b0160eSGleb Smirnoff
1039b50d902SRodney W. Grimes /*
10405b0160eSGleb Smirnoff * Wait on both the other process (sockt) and standard input.
1059b50d902SRodney W. Grimes */
1069b50d902SRodney W. Grimes for (;;) {
1077880fc11SPedro F. Giffuni fds[0].fd = fileno(stdin);
1087880fc11SPedro F. Giffuni fds[0].events = POLLIN;
1097880fc11SPedro F. Giffuni fds[1].fd = sockt;
1107880fc11SPedro F. Giffuni fds[1].events = POLLIN;
1117880fc11SPedro F. Giffuni nb = poll(fds, 2, INFTIM);
112ec8ffccbSOlivier Houchard if (gotwinch) {
113ec8ffccbSOlivier Houchard resize_display();
114ec8ffccbSOlivier Houchard gotwinch = 0;
115ec8ffccbSOlivier Houchard }
1169b50d902SRodney W. Grimes if (nb <= 0) {
11705b0160eSGleb Smirnoff if (errno == EINTR)
1189b50d902SRodney W. Grimes continue;
11905b0160eSGleb Smirnoff /* Panic, we don't know what happened. */
1207880fc11SPedro F. Giffuni p_error("Unexpected error from poll");
1219b50d902SRodney W. Grimes quit();
1229b50d902SRodney W. Grimes }
1237880fc11SPedro F. Giffuni if (fds[1].revents & POLLIN) {
12405b0160eSGleb Smirnoff wint_t w;
12505b0160eSGleb Smirnoff
12605b0160eSGleb Smirnoff /* There is data on sockt. */
12705b0160eSGleb Smirnoff w = fgetwc(sockfp);
12805b0160eSGleb Smirnoff if (w == WEOF) {
1299b50d902SRodney W. Grimes message("Connection closed. Exiting");
1309b50d902SRodney W. Grimes quit();
1319b50d902SRodney W. Grimes }
13205b0160eSGleb Smirnoff display(&his_win, &w);
1339b50d902SRodney W. Grimes }
1347880fc11SPedro F. Giffuni if (fds[0].revents & POLLIN) {
13505b0160eSGleb Smirnoff wint_t w;
13605b0160eSGleb Smirnoff
13705b0160eSGleb Smirnoff if ((w = getwchar()) != WEOF) {
13805b0160eSGleb Smirnoff display(&my_win, &w);
13905b0160eSGleb Smirnoff (void )fputwc(w, sockfp);
14005b0160eSGleb Smirnoff (void )fflush(sockfp);
14105b0160eSGleb Smirnoff }
1429b50d902SRodney W. Grimes }
1439b50d902SRodney W. Grimes }
1449b50d902SRodney W. Grimes }
1459b50d902SRodney W. Grimes
1469b50d902SRodney W. Grimes /*
1479b50d902SRodney W. Grimes * p_error prints the system error message on the standard location
1489b50d902SRodney W. Grimes * on the screen and then exits. (i.e. a curses version of perror)
1499b50d902SRodney W. Grimes */
15028592604SJoerg Wunsch void
p_error(const char * string)151b6d9e1f3SXin LI p_error(const char *string)
1529b50d902SRodney W. Grimes {
153b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0);
1549b50d902SRodney W. Grimes wprintw(my_win.x_win, "[%s : %s (%d)]\n",
1559b50d902SRodney W. Grimes string, strerror(errno), errno);
1569b50d902SRodney W. Grimes wrefresh(my_win.x_win);
1579b50d902SRodney W. Grimes move(LINES-1, 0);
1589b50d902SRodney W. Grimes refresh();
1599b50d902SRodney W. Grimes quit();
1609b50d902SRodney W. Grimes }
1619b50d902SRodney W. Grimes
1629b50d902SRodney W. Grimes /*
1639b50d902SRodney W. Grimes * Display string in the standard location
1649b50d902SRodney W. Grimes */
16528592604SJoerg Wunsch void
message(const char * string)166b6d9e1f3SXin LI message(const char *string)
1679b50d902SRodney W. Grimes {
168b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0);
169b02b5aadSAndrey A. Chernov wprintw(my_win.x_win, "[%s]\n", string);
170b02b5aadSAndrey A. Chernov if (current_line < my_win.x_nlines - 1)
1719b50d902SRodney W. Grimes current_line++;
1729b50d902SRodney W. Grimes wrefresh(my_win.x_win);
1739b50d902SRodney W. Grimes }
174