blob: 71717347587f9d97befa25f91c1b8911aa2983f3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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.
Bram Moolenaar7474c7c2012-01-20 21:13:59 +010017 *
18 * This specific version is distibuted under the Vim license (attribution by
19 * Juergen Weigert), the GPL applies to the original version, see the
20 * copyright notice below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 */
22
23/* Copyright (c) 1993
24 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
25 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
26 * Copyright (c) 1987 Oliver Laumann
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2, or (at your option)
31 * any later version.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License
39 * along with this program (see the file COPYING); if not, write to the
40 * Free Software Foundation, Inc.,
41 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42 */
43
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#include "vim.h"
45
Bram Moolenaar613fe7a2017-07-22 21:11:53 +020046#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar6ed80692017-07-22 20:53:21 +020047
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#include <signal.h>
49
50#ifdef __CYGWIN32__
51# include <sys/termios.h>
52#endif
53
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000054#ifdef HAVE_SYS_IOCTL_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# include <sys/ioctl.h>
56#endif
57
58#if HAVE_STROPTS_H
59#include <sys/types.h>
60#ifdef sinix
61#define buf_T __system_buf_t__
62#endif
63#include <stropts.h>
64#ifdef sinix
65#undef buf_T
66#endif
Bram Moolenaara899e6e2016-12-03 16:40:51 +010067# ifdef SUN_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +000068# include <sys/conf.h>
69# endif
70#endif
71
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000072#ifdef HAVE_UNISTD_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000073# include <unistd.h>
74#endif
75
76#if HAVE_TERMIO_H
77# include <termio.h>
78#else
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000079# ifdef HAVE_TERMIOS_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000080# include <termios.h>
81# endif
82#endif
83
84#if HAVE_SYS_STREAM_H
85# include <sys/stream.h>
86#endif
87
88#if HAVE_SYS_PTEM_H
89# include <sys/ptem.h>
90#endif
91
Bram Moolenaard0573012017-10-28 21:11:06 +020092#if !defined(SUN_SYSTEM) && !defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000093# include <sys/ioctl.h>
94#endif
95
Bram Moolenaara899e6e2016-12-03 16:40:51 +010096#if defined(SUN_SYSTEM) && defined(LOCKPTY) && !defined(TIOCEXCL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097# include <sys/ttold.h>
98#endif
99
100#ifdef ISC
101# include <sys/tty.h>
102# include <sys/sioctl.h>
103# include <sys/pty.h>
104#endif
105
106#ifdef sgi
107# include <sys/sysmacros.h>
108#endif
109
110#if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux)
111# define hpux
112#endif
113
114/*
115 * if no PTYRANGE[01] is in the config file, we pick a default
116 */
117#ifndef PTYRANGE0
118# define PTYRANGE0 "qprs"
119#endif
120#ifndef PTYRANGE1
121# define PTYRANGE1 "0123456789abcdef"
122#endif
123
124/* SVR4 pseudo ttys don't seem to work with SCO-5 */
125#ifdef M_UNIX
126# undef HAVE_SVR4_PTYS
127#endif
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129/*
Bram Moolenaarb07269a2011-05-19 13:41:14 +0200130 * Open all ptys with O_NOCTTY, just to be on the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 */
132#ifndef O_NOCTTY
133# define O_NOCTTY 0
134#endif
135
136 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100137initmaster(int f UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138{
139#ifndef VMS
140# ifdef POSIX
141 tcflush(f, TCIOFLUSH);
142# else
143# ifdef TIOCFLUSH
144 (void)ioctl(f, TIOCFLUSH, (char *) 0);
145# endif
146# endif
147# ifdef LOCKPTY
148 (void)ioctl(f, TIOCEXCL, (char *) 0);
149# endif
150#endif
151}
152
153/*
154 * This causes a hang on some systems, but is required for a properly working
155 * pty on others. Needs to be tuned...
156 */
157 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100158SetupSlavePTY(int fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159{
160 if (fd < 0)
161 return 0;
162#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
163# if defined(HAVE_SYS_PTEM_H) || defined(hpux)
164 if (ioctl(fd, I_PUSH, "ptem") != 0)
165 return -1;
166# endif
167 if (ioctl(fd, I_PUSH, "ldterm") != 0)
168 return -1;
Bram Moolenaara899e6e2016-12-03 16:40:51 +0100169# ifdef SUN_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (ioctl(fd, I_PUSH, "ttcompat") != 0)
171 return -1;
172# endif
173#endif
174 return 0;
175}
176
177
178#if defined(OSX) && !defined(PTY_DONE)
179#define PTY_DONE
180 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100181OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
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
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100198OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 char *m, *s;
201 int f;
202 /* used for opening a new pty-pair: */
203 static char PtyName[32];
204 static char TtyName[32];
205
206 if ((f = getpseudotty(&s, &m)) < 0)
207 return -1;
208#ifdef _SEQUENT_
209 fvhangup(s);
210#endif
Bram Moolenaar639304d2011-04-11 14:24:37 +0200211 vim_strncpy((char_u *)PtyName, (char_u *)m, sizeof(PtyName) - 1);
212 vim_strncpy((char_u *)TtyName, (char_u *)s, sizeof(TtyName) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 initmaster(f);
214 *ttyn = TtyName;
215 return f;
216}
217#endif
218
219#if defined(__sgi) && !defined(PTY_DONE)
220#define PTY_DONE
221 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100222OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 int f;
225 char *name;
Bram Moolenaard99df422016-01-29 23:20:40 +0100226 RETSIGTYPE (*sigcld) SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228 /*
229 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
230 * exec() /usr/adm/mkpts
231 */
232 sigcld = signal(SIGCHLD, SIG_DFL);
233 name = _getpty(&f, O_RDWR | O_NONBLOCK | O_EXTRA, 0600, 0);
234 signal(SIGCHLD, sigcld);
235
236 if (name == 0)
237 return -1;
238 initmaster(f);
239 *ttyn = name;
240 return f;
241}
242#endif
243
244#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
245#define PTY_DONE
246 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100247OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 int f;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200250 stat_T buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* used for opening a new pty-pair: */
252 static char TtyName[32];
253
254 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_NONBLOCK | O_EXTRA, 0)) < 0)
255 return -1;
256 if (mch_fstat(f, &buf) < 0)
257 {
258 close(f);
259 return -1;
260 }
261 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
262 initmaster(f);
263 *ttyn = TtyName;
264 return f;
265}
266#endif
267
Bram Moolenaarc958b312017-09-30 20:04:36 +0200268#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) \
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100269 && !(defined(MACOS_X) && !defined(MAC_OS_X_VERSION_10_6))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
Bram Moolenaar95dd9c12008-08-17 21:03:18 +0000271/* NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work!
Bram Moolenaarc958b312017-09-30 20:04:36 +0200272 * Same for Mac OS X Leopard (10.5). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#define PTY_DONE
274 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100275OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
277 int f;
Bram Moolenaareb80f042010-07-24 20:44:27 +0200278 char *m;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100279 char *(ptsname(int));
280 int unlockpt(int);
281 int grantpt(int);
282 RETSIGTYPE (*sigcld) SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 /* used for opening a new pty-pair: */
284 static char TtyName[32];
285
286 if ((f = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
287 return -1;
288
289 /*
290 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
291 * exec()s pt_chmod
292 */
293 sigcld = signal(SIGCHLD, SIG_DFL);
294 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
295 {
296 signal(SIGCHLD, sigcld);
297 close(f);
298 return -1;
299 }
300 signal(SIGCHLD, sigcld);
Bram Moolenaar639304d2011-04-11 14:24:37 +0200301 vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 initmaster(f);
303 *ttyn = TtyName;
304 return f;
305}
306#endif
307
308#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
309#define PTY_DONE
310
311#ifdef _IBMR2
312int aixhack = -1;
313#endif
314
315 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100316OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317{
318 int f;
319 /* used for opening a new pty-pair: */
320 static char TtyName[32];
321
322 /* a dumb looking loop replaced by mycrofts code: */
323 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_EXTRA)) < 0)
324 return -1;
Bram Moolenaar639304d2011-04-11 14:24:37 +0200325 vim_strncpy((char_u *)TtyName, (char_u *)ttyname(f), sizeof(TtyName) - 1);
Bram Moolenaar311d9822007-02-27 15:48:28 +0000326 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {
328 close(f);
329 return -1;
330 }
331 initmaster(f);
332# ifdef _IBMR2
333 if (aixhack >= 0)
334 close(aixhack);
335 if ((aixhack = open(TtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) < 0)
336 {
337 close(f);
338 return -1;
339 }
340# endif
341 *ttyn = TtyName;
342 return f;
343}
344#endif
345
346#ifndef PTY_DONE
347
348# ifdef hpux
349static char PtyProto[] = "/dev/ptym/ptyXY";
350static char TtyProto[] = "/dev/pty/ttyXY";
351# else
352# ifdef __BEOS__
353static char PtyProto[] = "/dev/pt/XY";
354static char TtyProto[] = "/dev/tt/XY";
355# else
356static char PtyProto[] = "/dev/ptyXY";
357static char TtyProto[] = "/dev/ttyXY";
358# endif
359# endif
360
361 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +0100362OpenPTY(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364 char *p, *q, *l, *d;
365 int f;
366 /* used for opening a new pty-pair: */
367 static char PtyName[32];
368 static char TtyName[32];
369
370 strcpy(PtyName, PtyProto);
371 strcpy(TtyName, TtyProto);
372 for (p = PtyName; *p != 'X'; p++)
373 ;
374 for (q = TtyName; *q != 'X'; q++)
375 ;
376 for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
377 {
378 for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 continue;
382 q[0] = *l;
383 q[1] = *d;
Bram Moolenaar311d9822007-02-27 15:48:28 +0000384 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {
386 close(f);
387 continue;
388 }
Bram Moolenaara899e6e2016-12-03 16:40:51 +0100389#if defined(SUN_SYSTEM) && defined(TIOCGPGRP) && !defined(SUNOS3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 /* Hack to ensure that the slave side of the pty is
391 * unused. May not work in anything other than SunOS4.1
392 */
393 {
394 int pgrp;
395
396 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
397 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
398 {
399 close(f);
400 continue;
401 }
402 }
403#endif
404 initmaster(f);
405 *ttyn = TtyName;
406 return f;
407 }
408 }
409 return -1;
410}
411#endif
Bram Moolenaar6ed80692017-07-22 20:53:21 +0200412
413#endif /* FEAT_GUI || FEAT_TERMINAL */