Adjusted alternative joining method to obey keepLineBreaksICS
[xboard.git] / childio.c
1 /*
2  * childio.c -- set up communication with child processes 
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts. 
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009 Free Software Foundation, Inc.
9  *
10  * The following terms apply to Digital Equipment Corporation's copyright
11  * interest in XBoard:
12  * ------------------------------------------------------------------------
13  * All Rights Reserved
14  *
15  * Permission to use, copy, modify, and distribute this software and its
16  * documentation for any purpose and without fee is hereby granted,
17  * provided that the above copyright notice appear in all copies and that
18  * both that copyright notice and this permission notice appear in
19  * supporting documentation, and that the name of Digital not be
20  * used in advertising or publicity pertaining to distribution of the
21  * software without specific, written prior permission.
22  *
23  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
25  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
26  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
28  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29  * SOFTWARE.
30  * ------------------------------------------------------------------------
31  *
32  * The following terms apply to the enhanced version of XBoard
33  * distributed by the Free Software Foundation:
34  * ------------------------------------------------------------------------
35  *
36  * GNU XBoard is free software: you can redistribute it and/or modify
37  * it under the terms of the GNU General Public License as published by
38  * the Free Software Foundation, either version 3 of the License, or (at
39  * your option) any later version.
40  *
41  * GNU XBoard is distributed in the hope that it will be useful, but
42  * WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44  * General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program. If not, see http://www.gnu.org/licenses/.  *
48  *
49  *------------------------------------------------------------------------
50  ** See the file ChangeLog for a revision history.  */
51
52 /* This file splits into two entirely different pieces of code
53    depending on whether USE_PTYS is 1.  The whole reason for all
54    the pty nonsense is that select() does not work on pipes in System-V
55    derivatives (at least some of them).  This is a problem because
56    XtAppAddInput works by adding its argument to a select that is done
57    deep inside Xlib.
58 */
59
60 #include "config.h"
61
62 #include <signal.h>
63 #if HAVE_UNISTD_H
64 # include <unistd.h>
65 #endif
66
67 #include "common.h"
68 #include "frontend.h"
69
70 #if !USE_PTYS
71 /* This code is for systems where pipes work properly */
72
73 void SetUpChildIO(to_prog, from_prog)
74      int to_prog[2], from_prog[2];
75 {
76     signal(SIGPIPE, SIG_IGN);
77     pipe(to_prog);
78     pipe(from_prog);
79 }
80
81 #else /* USE_PTYS == 1 */
82 /* This code is for all systems where we must use ptys */
83
84 #include <errno.h>
85 #include <sys/stat.h>
86 #include <sys/ioctl.h>
87 #if HAVE_STROPTS_H
88 # include <stropts.h>
89 #endif /* HAVE_STROPTS_H */
90 #if HAVE_SYS_FCNTL_H
91 # include <sys/fcntl.h>
92 #else /* not HAVE_SYS_FCNTL_H */
93 # if HAVE_FCNTL_H
94 #  include <fcntl.h>
95 # endif /* HAVE_FCNTL_H */
96 #endif /* not HAVE_SYS_FCNTL_H */
97
98 int PseudoTTY P((char pty_name[]));
99
100 int SetUpChildIO(to_prog, from_prog)
101      int to_prog[2], from_prog[2];
102 {
103     char pty_name[MSG_SIZ];
104
105     if ((to_prog[1] = PseudoTTY(pty_name)) == -1) {
106         DisplayFatalError("Can't open pseudo-tty", errno, 1);
107         ExitEvent(1);
108     }
109     from_prog[0] = to_prog[1];
110     to_prog[0] = from_prog[1] = open(pty_name, O_RDWR, 0);
111
112 #if HAVE_STROPTS_H /* do we really need this??  pipe-like behavior is fine */
113     if (ioctl (to_prog[0], I_PUSH, "ptem") == -1 ||
114         ioctl (to_prog[0], I_PUSH, "ldterm") == -1 ||
115         ioctl (to_prog[0], I_PUSH, "ttcompat") == -1) {
116 # ifdef NOTDEF /* seems some systems don't have or need ptem and ttcompat */
117         DisplayFatalError("Can't ioctl pseudo-tty", errno, 1);
118         ExitEvent(1);
119 # endif /*NOTDEF*/
120     }
121 #endif /* HAVE_STROPTS_H */
122
123 }
124
125 #if HAVE_GRANTPT
126 /* This code is for SVR4 */
127
128 int PseudoTTY(pty_name)
129      char pty_name[];
130 {
131     extern char *ptsname();
132     char *ptss;
133     int fd;
134     
135     fd = open("/dev/ptmx", O_RDWR);
136     if (fd < 0) return fd;
137     if (grantpt(fd) == -1) return -1;
138     if (unlockpt(fd) == -1) return -1;
139     if (!(ptss = ptsname(fd))) return -1;
140     strcpy(pty_name, ptss);
141     return fd;
142 }
143
144 #else /* not HAVE_GRANTPT */
145 #if HAVE__GETPTY
146 /* This code is for IRIX */
147
148 int PseudoTTY(pty_name)
149      char pty_name[];
150 {
151     int fd;
152     char *ptyn;
153
154     ptyn = _getpty(&fd, O_RDWR, 0600, 0);
155     if (ptyn == NULL) return -1;
156     strcpy(pty_name, ptyn);
157     return fd;
158 }
159
160 #else /* not HAVE__GETPTY */
161 #if HAVE_LIBSEQ
162 /* This code is for Sequent DYNIX/ptx.  Untested. --tpm */
163
164 int PseudoTTY(pty_name)
165      char pty_name[];
166 {
167     int fd;
168     char *slave, *master;
169
170     fd = getpseudotty(&slave, &master);
171     if (fd < 0) return fd;
172     strcpy(pty_name, slave);
173     return fd;
174 }
175
176 #else /* not HAVE_LIBSEQ */
177 /* This code is for all other systems */
178 /* The code is adapted from GNU Emacs 19.24 */
179
180 #ifndef FIRST_PTY_LETTER
181 #define FIRST_PTY_LETTER 'p'
182 #endif
183 #ifndef LAST_PTY_LETTER
184 #define LAST_PTY_LETTER 'z'
185 #endif
186
187 int PseudoTTY(pty_name)
188      char pty_name[];
189 {
190   struct stat stb;
191   register c, i;
192   int fd;
193
194   /* Some systems name their pseudoterminals so that there are gaps in
195      the usual sequence - for example, on HP9000/S700 systems, there
196      are no pseudoterminals with names ending in 'f'.  So we wait for
197      three failures in a row before deciding that we've reached the
198      end of the ptys.  */
199   int failed_count = 0;
200
201 #ifdef PTY_ITERATION
202   PTY_ITERATION
203 #else
204   for (c = FIRST_PTY_LETTER; c <= LAST_PTY_LETTER; c++)
205     for (i = 0; i < 16; i++)
206 #endif
207       {
208 #ifdef PTY_NAME_SPRINTF
209         PTY_NAME_SPRINTF
210 #else
211         sprintf (pty_name, "/dev/pty%c%x", c, i);
212 #endif /* no PTY_NAME_SPRINTF */
213
214 #ifdef PTY_OPEN
215         PTY_OPEN;
216 #else /* no PTY_OPEN */
217         if (stat (pty_name, &stb) < 0)
218           {
219             failed_count++;
220             if (failed_count >= 3)
221               return -1;
222           }
223         else
224           failed_count = 0;
225         fd = open (pty_name, O_RDWR, 0);
226 #endif /* no PTY_OPEN */
227
228         if (fd >= 0)
229           {
230             /* check to make certain that both sides are available
231                this avoids a nasty yet stupid bug in rlogins */
232 #ifdef PTY_TTY_NAME_SPRINTF
233             PTY_TTY_NAME_SPRINTF
234 #else
235             sprintf (pty_name, "/dev/tty%c%x", c, i);
236 #endif /* no PTY_TTY_NAME_SPRINTF */
237 #ifndef UNIPLUS
238             if (access (pty_name, 6) != 0)
239               {
240                 close (fd);
241                 continue;
242               }
243 #endif /* not UNIPLUS */
244 #ifdef IBMRTAIX
245               /* On AIX, the parent gets SIGHUP when a pty attached
246                  child dies.  So, we ignore SIGHUP once we've started
247                  a child on a pty.  Note that this may cause xboard
248                  not to die when it should, i.e., when its own
249                  controlling tty goes away.
250               */
251               signal(SIGHUP, SIG_IGN);
252 #endif /* IBMRTAIX */
253             return fd;
254           }
255       }
256   return -1;
257 }
258
259 #endif /* not HAVE_LIBSEQ */
260 #endif /* not HAVE__GETPTY */
261 #endif /* not HAVE_GRANTPT */
262 #endif /* USE_PTYS */