blob: f11a22dcd8577e608f0df546af03cd8423d4b1e7 [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 *
Bram Moolenaar32aa1022019-11-02 22:54:41 +010018 * This specific version is distributed under the Vim license (attribution by
Bram Moolenaar7474c7c2012-01-20 21:13:59 +010019 * Juergen Weigert), the GPL applies to the original version, see the
20 * copyright notice below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 */
22
Bram Moolenaar63d9e732019-12-05 21:10:38 +010023// 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#include "vim.h"
44
Bram Moolenaar613fe7a2017-07-22 21:11:53 +020045#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar6ed80692017-07-22 20:53:21 +020046
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#include <signal.h>
48
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000049#ifdef HAVE_SYS_IOCTL_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# include <sys/ioctl.h>
51#endif
52
53#if HAVE_STROPTS_H
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +010054# include <sys/types.h>
55# ifdef sinix
56# define buf_T __system_buf_t__
57# endif
58# include <stropts.h>
59# ifdef sinix
60# undef buf_T
61# endif
Bram Moolenaara899e6e2016-12-03 16:40:51 +010062# ifdef SUN_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# include <sys/conf.h>
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +010064# if defined(HAVE_SYS_PTMS_H) && defined(HAVE_SVR4_PTYS)
65# include <sys/ptms.h>
66# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067# endif
68#endif
69
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000070#ifdef HAVE_UNISTD_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000071# include <unistd.h>
72#endif
73
74#if HAVE_TERMIO_H
75# include <termio.h>
76#else
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000077# ifdef HAVE_TERMIOS_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000078# include <termios.h>
79# endif
80#endif
81
82#if HAVE_SYS_STREAM_H
83# include <sys/stream.h>
84#endif
85
86#if HAVE_SYS_PTEM_H
87# include <sys/ptem.h>
88#endif
89
Bram Moolenaard0573012017-10-28 21:11:06 +020090#if !defined(SUN_SYSTEM) && !defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091# include <sys/ioctl.h>
92#endif
93
Bram Moolenaara899e6e2016-12-03 16:40:51 +010094#if defined(SUN_SYSTEM) && defined(LOCKPTY) && !defined(TIOCEXCL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000095# include <sys/ttold.h>
96#endif
97
98#ifdef ISC
99# include <sys/tty.h>
100# include <sys/sioctl.h>
101# include <sys/pty.h>
102#endif
103
104#ifdef sgi
105# include <sys/sysmacros.h>
106#endif
107
108#if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux)
109# define hpux
110#endif
111
112/*
113 * if no PTYRANGE[01] is in the config file, we pick a default
114 */
115#ifndef PTYRANGE0
116# define PTYRANGE0 "qprs"
117#endif
118#ifndef PTYRANGE1
119# define PTYRANGE1 "0123456789abcdef"
120#endif
121
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100122// SVR4 pseudo ttys don't seem to work with SCO-5
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#ifdef M_UNIX
124# undef HAVE_SVR4_PTYS
125#endif
126
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127/*
Bram Moolenaarb07269a2011-05-19 13:41:14 +0200128 * Open all ptys with O_NOCTTY, just to be on the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 */
130#ifndef O_NOCTTY
131# define O_NOCTTY 0
132#endif
133
Bram Moolenaar1e449682019-04-28 14:59:59 +0200134#if defined(HAVE_SVR4_PTYS) || defined(HAVE_POSIX_OPENPT)
135// These should be in stdlib.h, but it depends on _XOPEN_SOURCE.
136char *ptsname(int);
137int unlockpt(int);
138int grantpt(int);
Bram Moolenaarab4cece2019-04-28 18:40:03 +0200139int posix_openpt(int flags);
Bram Moolenaar1e449682019-04-28 14:59:59 +0200140#endif
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100143initmaster(int f UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145#ifndef VMS
146# ifdef POSIX
147 tcflush(f, TCIOFLUSH);
148# else
149# ifdef TIOCFLUSH
150 (void)ioctl(f, TIOCFLUSH, (char *) 0);
151# endif
152# endif
153# ifdef LOCKPTY
154 (void)ioctl(f, TIOCEXCL, (char *) 0);
155# endif
156#endif
157}
158
159/*
160 * This causes a hang on some systems, but is required for a properly working
161 * pty on others. Needs to be tuned...
162 */
163 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100164setup_slavepty(int fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165{
166 if (fd < 0)
167 return 0;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100168#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) \
169 && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# if defined(HAVE_SYS_PTEM_H) || defined(hpux)
171 if (ioctl(fd, I_PUSH, "ptem") != 0)
172 return -1;
173# endif
174 if (ioctl(fd, I_PUSH, "ldterm") != 0)
175 return -1;
Bram Moolenaara899e6e2016-12-03 16:40:51 +0100176# ifdef SUN_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 if (ioctl(fd, I_PUSH, "ttcompat") != 0)
178 return -1;
179# endif
180#endif
181 return 0;
182}
183
Bram Moolenaar1e449682019-04-28 14:59:59 +0200184#if defined(HAVE_POSIX_OPENPT) && !defined(PTY_DONE)
185#define PTY_DONE
186 int
187mch_openpty(char **ttyn)
188{
ichizok378447f2023-05-11 22:25:42 +0100189 int f;
190 char *m;
191 sighandler_T sigcld;
Bram Moolenaar1e449682019-04-28 14:59:59 +0200192 static char TtyName[32]; // used for opening a new pty-pair
193
194 if ((f = posix_openpt(O_RDWR | O_NOCTTY | O_EXTRA)) == -1)
195 return -1;
196
197 // SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
198 // exec()s pt_chmod
ichizok378447f2023-05-11 22:25:42 +0100199 sigcld = mch_signal(SIGCHLD, SIG_DFL);
Bram Moolenaar1e449682019-04-28 14:59:59 +0200200 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
201 {
ichizok378447f2023-05-11 22:25:42 +0100202 mch_signal(SIGCHLD, sigcld);
Bram Moolenaar1e449682019-04-28 14:59:59 +0200203 close(f);
204 return -1;
205 }
ichizok378447f2023-05-11 22:25:42 +0100206 mch_signal(SIGCHLD, sigcld);
Bram Moolenaar1e449682019-04-28 14:59:59 +0200207 vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1);
208 initmaster(f);
209 *ttyn = TtyName;
210 return f;
211}
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214#if defined(OSX) && !defined(PTY_DONE)
215#define PTY_DONE
216 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100217mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
219 int f;
220 static char TtyName[32];
221
222 if ((f = open_controlling_pty(TtyName)) < 0)
223 return -1;
224 initmaster(f);
225 *ttyn = TtyName;
226 return f;
227}
228#endif
229
230#if (defined(sequent) || defined(_SEQUENT_)) && defined(HAVE_GETPSEUDOTTY) \
231 && !defined(PTY_DONE)
232#define PTY_DONE
233 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100234mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 char *m, *s;
237 int f;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100238 // used for opening a new pty-pair:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 static char PtyName[32];
240 static char TtyName[32];
241
242 if ((f = getpseudotty(&s, &m)) < 0)
243 return -1;
244#ifdef _SEQUENT_
245 fvhangup(s);
246#endif
Bram Moolenaar639304d2011-04-11 14:24:37 +0200247 vim_strncpy((char_u *)PtyName, (char_u *)m, sizeof(PtyName) - 1);
248 vim_strncpy((char_u *)TtyName, (char_u *)s, sizeof(TtyName) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 initmaster(f);
250 *ttyn = TtyName;
251 return f;
252}
253#endif
254
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
256#define PTY_DONE
257 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100258mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 int f;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200261 stat_T buf;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100262 // used for opening a new pty-pair:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 static char TtyName[32];
264
265 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_NONBLOCK | O_EXTRA, 0)) < 0)
266 return -1;
267 if (mch_fstat(f, &buf) < 0)
268 {
269 close(f);
270 return -1;
271 }
272 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
273 initmaster(f);
274 *ttyn = TtyName;
275 return f;
276}
277#endif
278
Bram Moolenaarc958b312017-09-30 20:04:36 +0200279#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) \
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100280 && !(defined(MACOS_X) && !defined(MAC_OS_X_VERSION_10_6))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100282// NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work!
283// Same for Mac OS X Leopard (10.5).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#define PTY_DONE
285 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100286mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287{
ichizok378447f2023-05-11 22:25:42 +0100288 int f;
289 char *m;
290 sighandler_T sigcld;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100291 // used for opening a new pty-pair:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 static char TtyName[32];
293
294 if ((f = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
295 return -1;
296
297 /*
298 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
299 * exec()s pt_chmod
300 */
ichizok378447f2023-05-11 22:25:42 +0100301 sigcld = mch_signal(SIGCHLD, SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
303 {
ichizok378447f2023-05-11 22:25:42 +0100304 mch_signal(SIGCHLD, sigcld);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 close(f);
306 return -1;
307 }
ichizok378447f2023-05-11 22:25:42 +0100308 mch_signal(SIGCHLD, sigcld);
Bram Moolenaar639304d2011-04-11 14:24:37 +0200309 vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 initmaster(f);
311 *ttyn = TtyName;
312 return f;
313}
314#endif
315
316#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
317#define PTY_DONE
318
319#ifdef _IBMR2
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200320static int aixhack = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#endif
322
323 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100324mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 int f;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100327 // used for opening a new pty-pair:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 static char TtyName[32];
329
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100330 // a dumb looking loop replaced by mycrofts code:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_EXTRA)) < 0)
332 return -1;
Bram Moolenaar639304d2011-04-11 14:24:37 +0200333 vim_strncpy((char_u *)TtyName, (char_u *)ttyname(f), sizeof(TtyName) - 1);
Bram Moolenaar311d9822007-02-27 15:48:28 +0000334 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
336 close(f);
337 return -1;
338 }
339 initmaster(f);
340# ifdef _IBMR2
341 if (aixhack >= 0)
342 close(aixhack);
343 if ((aixhack = open(TtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) < 0)
344 {
345 close(f);
346 return -1;
347 }
348# endif
349 *ttyn = TtyName;
350 return f;
351}
352#endif
353
354#ifndef PTY_DONE
355
356# ifdef hpux
357static char PtyProto[] = "/dev/ptym/ptyXY";
358static char TtyProto[] = "/dev/pty/ttyXY";
359# else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100360# ifdef __HAIKU__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361static char PtyProto[] = "/dev/pt/XY";
362static char TtyProto[] = "/dev/tt/XY";
363# else
364static char PtyProto[] = "/dev/ptyXY";
365static char TtyProto[] = "/dev/ttyXY";
366# endif
367# endif
368
369 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100370mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
372 char *p, *q, *l, *d;
373 int f;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100374 // used for opening a new pty-pair:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 static char PtyName[32];
376 static char TtyName[32];
377
378 strcpy(PtyName, PtyProto);
379 strcpy(TtyName, TtyProto);
380 for (p = PtyName; *p != 'X'; p++)
381 ;
382 for (q = TtyName; *q != 'X'; q++)
383 ;
384 for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
385 {
386 for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 continue;
390 q[0] = *l;
391 q[1] = *d;
Bram Moolenaar311d9822007-02-27 15:48:28 +0000392 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {
394 close(f);
395 continue;
396 }
Bram Moolenaara899e6e2016-12-03 16:40:51 +0100397#if defined(SUN_SYSTEM) && defined(TIOCGPGRP) && !defined(SUNOS3)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100398 // Hack to ensure that the slave side of the pty is
399 // unused. May not work in anything other than SunOS4.1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {
401 int pgrp;
402
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100403 // tcgetpgrp does not work (uses TIOCGETPGRP)!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
405 {
406 close(f);
407 continue;
408 }
409 }
410#endif
411 initmaster(f);
412 *ttyn = TtyName;
413 return f;
414 }
415 }
416 return -1;
417}
418#endif
Bram Moolenaar6ed80692017-07-22 20:53:21 +0200419
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100420/*
421 * Call isatty(fd), except for SunOS where it's done differently.
422 */
423 int
424mch_isatty(int fd)
425{
426# if defined(I_STR) && defined(HAVE_SYS_PTMS_H) && defined(HAVE_SVR4_PTYS) \
427 && defined(SUN_SYSTEM)
428 // On SunOS, isatty() for /dev/ptmx returns false or sometimes can hang up
429 // in the inner ioctl(), and therefore first determine whether "fd" is a
430 // master device.
431 struct strioctl istr;
432
433 istr.ic_cmd = ISPTM;
434 istr.ic_timout = 0;
435 istr.ic_dp = NULL;
436 istr.ic_len = 0;
437
438 if (ioctl(fd, I_STR, &istr) == 0)
439 // Trick: return 2 in order to advice the caller that "fd" is a master
440 // device. cf. src/os_unix.c:get_tty_fd()
441 return 2;
442# endif
443 return isatty(fd);
444}
445
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100446#endif // FEAT_GUI || FEAT_JOB_CHANNEL