1/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved		by Bram Moolenaar
4 *
5 * Do ":help uganda"  in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * The stuff in this file mostly comes from the "screen" program.
11 * Included with permission from Juergen Weigert.
12 * Copied from "pty.c".  "putenv.c" was used for putenv() in misc2.c.
13 *
14 * It has been modified to work better with Vim.
15 * The parts that are not used in Vim have been deleted.
16 * See the "screen" sources for the complete stuff.
17 */
18
19/* Copyright (c) 1993
20 *	Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
21 *	Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
22 * Copyright (c) 1987 Oliver Laumann
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program (see the file COPYING); if not, write to the
36 * Free Software Foundation, Inc.,
37 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
38 */
39
40#include "vim.h"
41
42#include <signal.h>
43
44#ifdef __CYGWIN32__
45# include <sys/termios.h>
46#endif
47
48#ifdef HAVE_SYS_IOCTL_H
49# include <sys/ioctl.h>
50#endif
51
52#if HAVE_STROPTS_H
53#include <sys/types.h>
54#ifdef sinix
55#define buf_T __system_buf_t__
56#endif
57#include <stropts.h>
58#ifdef sinix
59#undef buf_T
60#endif
61# ifdef sun
62#  include <sys/conf.h>
63# endif
64#endif
65
66#ifdef HAVE_UNISTD_H
67# include <unistd.h>
68#endif
69
70#if HAVE_TERMIO_H
71# include <termio.h>
72#else
73# ifdef HAVE_TERMIOS_H
74#  include <termios.h>
75# endif
76#endif
77
78#if HAVE_SYS_STREAM_H
79# include <sys/stream.h>
80#endif
81
82#if HAVE_SYS_PTEM_H
83# include <sys/ptem.h>
84#endif
85
86#if !defined(sun) && !defined(VMS) && !defined(MACOS)
87# include <sys/ioctl.h>
88#endif
89
90#if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
91# include <sys/ttold.h>
92#endif
93
94#ifdef ISC
95# include <sys/tty.h>
96# include <sys/sioctl.h>
97# include <sys/pty.h>
98#endif
99
100#ifdef sgi
101# include <sys/sysmacros.h>
102#endif
103
104#if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux)
105# define hpux
106#endif
107
108/*
109 * if no PTYRANGE[01] is in the config file, we pick a default
110 */
111#ifndef PTYRANGE0
112# define PTYRANGE0 "qprs"
113#endif
114#ifndef PTYRANGE1
115# define PTYRANGE1 "0123456789abcdef"
116#endif
117
118/* SVR4 pseudo ttys don't seem to work with SCO-5 */
119#ifdef M_UNIX
120# undef HAVE_SVR4_PTYS
121#endif
122
123static void initmaster __ARGS((int));
124
125/*
126 *  Open all ptys with O_NOCTTY, just to be on the safe side
127 *  (RISCos mips breaks otherwise)
128 */
129#ifndef O_NOCTTY
130# define O_NOCTTY 0
131#endif
132
133    static void
134initmaster(f)
135    int f UNUSED;
136{
137#ifndef VMS
138# ifdef POSIX
139    tcflush(f, TCIOFLUSH);
140# else
141#  ifdef TIOCFLUSH
142    (void)ioctl(f, TIOCFLUSH, (char *) 0);
143#  endif
144# endif
145# ifdef LOCKPTY
146    (void)ioctl(f, TIOCEXCL, (char *) 0);
147# endif
148#endif
149}
150
151/*
152 * This causes a hang on some systems, but is required for a properly working
153 * pty on others.  Needs to be tuned...
154 */
155    int
156SetupSlavePTY(fd)
157    int fd;
158{
159    if (fd < 0)
160	return 0;
161#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
162# if defined(HAVE_SYS_PTEM_H) || defined(hpux)
163    if (ioctl(fd, I_PUSH, "ptem") != 0)
164	return -1;
165# endif
166    if (ioctl(fd, I_PUSH, "ldterm") != 0)
167	return -1;
168# ifdef sun
169    if (ioctl(fd, I_PUSH, "ttcompat") != 0)
170	return -1;
171# endif
172#endif
173    return 0;
174}
175
176
177#if defined(OSX) && !defined(PTY_DONE)
178#define PTY_DONE
179    int
180OpenPTY(ttyn)
181    char **ttyn;
182{
183    int		f;
184    static char TtyName[32];
185
186    if ((f = open_controlling_pty(TtyName)) < 0)
187	return -1;
188    initmaster(f);
189    *ttyn = TtyName;
190    return f;
191}
192#endif
193
194#if (defined(sequent) || defined(_SEQUENT_)) && defined(HAVE_GETPSEUDOTTY) \
195	&& !defined(PTY_DONE)
196#define PTY_DONE
197    int
198OpenPTY(ttyn)
199    char **ttyn;
200{
201    char	*m, *s;
202    int		f;
203    /* used for opening a new pty-pair: */
204    static char PtyName[32];
205    static char TtyName[32];
206
207    if ((f = getpseudotty(&s, &m)) < 0)
208	return -1;
209#ifdef _SEQUENT_
210    fvhangup(s);
211#endif
212    strncpy(PtyName, m, sizeof(PtyName));
213    strncpy(TtyName, s, sizeof(TtyName));
214    initmaster(f);
215    *ttyn = TtyName;
216    return f;
217}
218#endif
219
220#if defined(__sgi) && !defined(PTY_DONE)
221#define PTY_DONE
222    int
223OpenPTY(ttyn)
224    char **ttyn;
225{
226    int f;
227    char *name;
228    RETSIGTYPE (*sigcld)__ARGS(SIGPROTOARG);
229
230    /*
231     * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
232     * exec() /usr/adm/mkpts
233     */
234    sigcld = signal(SIGCHLD, SIG_DFL);
235    name = _getpty(&f, O_RDWR | O_NONBLOCK | O_EXTRA, 0600, 0);
236    signal(SIGCHLD, sigcld);
237
238    if (name == 0)
239	return -1;
240    initmaster(f);
241    *ttyn = name;
242    return f;
243}
244#endif
245
246#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
247#define PTY_DONE
248    int
249OpenPTY(ttyn)
250    char **ttyn;
251{
252    int		f;
253    struct stat buf;
254    /* used for opening a new pty-pair: */
255    static char TtyName[32];
256
257    if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_NONBLOCK | O_EXTRA, 0)) < 0)
258	return -1;
259    if (mch_fstat(f, &buf) < 0)
260    {
261	close(f);
262	return -1;
263    }
264    sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
265    initmaster(f);
266    *ttyn = TtyName;
267    return f;
268}
269#endif
270
271#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) && !defined(MACOS_X)
272
273/* NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work!
274 * Same for Mac OS X Leopard. */
275#define PTY_DONE
276    int
277OpenPTY(ttyn)
278    char **ttyn;
279{
280    int		f;
281    char	*m;
282    char	*(ptsname __ARGS((int)));
283    int		unlockpt __ARGS((int));
284    int		grantpt __ARGS((int));
285    RETSIGTYPE (*sigcld)__ARGS(SIGPROTOARG);
286    /* used for opening a new pty-pair: */
287    static char TtyName[32];
288
289    if ((f = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
290	return -1;
291
292    /*
293     * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
294     * exec()s pt_chmod
295     */
296    sigcld = signal(SIGCHLD, SIG_DFL);
297    if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
298    {
299	signal(SIGCHLD, sigcld);
300	close(f);
301	return -1;
302    }
303    signal(SIGCHLD, sigcld);
304    strncpy(TtyName, m, sizeof(TtyName));
305    initmaster(f);
306    *ttyn = TtyName;
307    return f;
308}
309#endif
310
311#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
312#define PTY_DONE
313
314#ifdef _IBMR2
315int aixhack = -1;
316#endif
317
318    int
319OpenPTY(ttyn)
320    char **ttyn;
321{
322    int		f;
323    /* used for opening a new pty-pair: */
324    static char TtyName[32];
325
326    /* a dumb looking loop replaced by mycrofts code: */
327    if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_EXTRA)) < 0)
328	return -1;
329    strncpy(TtyName, ttyname(f), sizeof(TtyName));
330    if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
331    {
332	close(f);
333	return -1;
334    }
335    initmaster(f);
336# ifdef _IBMR2
337    if (aixhack >= 0)
338	close(aixhack);
339    if ((aixhack = open(TtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) < 0)
340    {
341	close(f);
342	return -1;
343    }
344# endif
345    *ttyn = TtyName;
346    return f;
347}
348#endif
349
350#ifndef PTY_DONE
351
352# ifdef hpux
353static char PtyProto[] = "/dev/ptym/ptyXY";
354static char TtyProto[] = "/dev/pty/ttyXY";
355# else
356#  ifdef __BEOS__
357static char PtyProto[] = "/dev/pt/XY";
358static char TtyProto[] = "/dev/tt/XY";
359#  else
360static char PtyProto[] = "/dev/ptyXY";
361static char TtyProto[] = "/dev/ttyXY";
362#  endif
363# endif
364
365    int
366OpenPTY(ttyn)
367    char **ttyn;
368{
369    char	*p, *q, *l, *d;
370    int		f;
371    /* used for opening a new pty-pair: */
372    static char PtyName[32];
373    static char TtyName[32];
374
375    strcpy(PtyName, PtyProto);
376    strcpy(TtyName, TtyProto);
377    for (p = PtyName; *p != 'X'; p++)
378	;
379    for (q = TtyName; *q != 'X'; q++)
380	;
381    for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
382    {
383	for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
384	{
385#if !defined(MACOS) || defined(USE_CARBONIZED)
386	    if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
387#else
388	    if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA)) == -1)
389#endif
390		continue;
391	    q[0] = *l;
392	    q[1] = *d;
393#ifndef MACOS
394	    if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
395	    {
396		close(f);
397		continue;
398	    }
399#endif
400#if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
401	    /* Hack to ensure that the slave side of the pty is
402	     * unused. May not work in anything other than SunOS4.1
403	     */
404	    {
405		int pgrp;
406
407		/* tcgetpgrp does not work (uses TIOCGETPGRP)! */
408		if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
409		{
410		    close(f);
411		    continue;
412		}
413	    }
414#endif
415	    initmaster(f);
416	    *ttyn = TtyName;
417	    return f;
418	}
419    }
420    return -1;
421}
422#endif
423