blob: a2162e210866048286aff999fe312fac9d44c01b [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
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +010059# 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>
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +010069# if defined(HAVE_SYS_PTMS_H) && defined(HAVE_SVR4_PTYS)
70# include <sys/ptms.h>
71# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# endif
73#endif
74
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000075#ifdef HAVE_UNISTD_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000076# include <unistd.h>
77#endif
78
79#if HAVE_TERMIO_H
80# include <termio.h>
81#else
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000082# ifdef HAVE_TERMIOS_H
Bram Moolenaar071d4272004-06-13 20:20:40 +000083# include <termios.h>
84# endif
85#endif
86
87#if HAVE_SYS_STREAM_H
88# include <sys/stream.h>
89#endif
90
91#if HAVE_SYS_PTEM_H
92# include <sys/ptem.h>
93#endif
94
Bram Moolenaard0573012017-10-28 21:11:06 +020095#if !defined(SUN_SYSTEM) && !defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096# include <sys/ioctl.h>
97#endif
98
Bram Moolenaara899e6e2016-12-03 16:40:51 +010099#if defined(SUN_SYSTEM) && defined(LOCKPTY) && !defined(TIOCEXCL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100# include <sys/ttold.h>
101#endif
102
103#ifdef ISC
104# include <sys/tty.h>
105# include <sys/sioctl.h>
106# include <sys/pty.h>
107#endif
108
109#ifdef sgi
110# include <sys/sysmacros.h>
111#endif
112
113#if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux)
114# define hpux
115#endif
116
117/*
118 * if no PTYRANGE[01] is in the config file, we pick a default
119 */
120#ifndef PTYRANGE0
121# define PTYRANGE0 "qprs"
122#endif
123#ifndef PTYRANGE1
124# define PTYRANGE1 "0123456789abcdef"
125#endif
126
127/* SVR4 pseudo ttys don't seem to work with SCO-5 */
128#ifdef M_UNIX
129# undef HAVE_SVR4_PTYS
130#endif
131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132/*
Bram Moolenaarb07269a2011-05-19 13:41:14 +0200133 * Open all ptys with O_NOCTTY, just to be on the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
135#ifndef O_NOCTTY
136# define O_NOCTTY 0
137#endif
138
Bram Moolenaar1e449682019-04-28 14:59:59 +0200139#if defined(HAVE_SVR4_PTYS) || defined(HAVE_POSIX_OPENPT)
140// These should be in stdlib.h, but it depends on _XOPEN_SOURCE.
141char *ptsname(int);
142int unlockpt(int);
143int grantpt(int);
Bram Moolenaarab4cece2019-04-28 18:40:03 +0200144int posix_openpt(int flags);
Bram Moolenaar1e449682019-04-28 14:59:59 +0200145#endif
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100148initmaster(int f UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149{
150#ifndef VMS
151# ifdef POSIX
152 tcflush(f, TCIOFLUSH);
153# else
154# ifdef TIOCFLUSH
155 (void)ioctl(f, TIOCFLUSH, (char *) 0);
156# endif
157# endif
158# ifdef LOCKPTY
159 (void)ioctl(f, TIOCEXCL, (char *) 0);
160# endif
161#endif
162}
163
164/*
165 * This causes a hang on some systems, but is required for a properly working
166 * pty on others. Needs to be tuned...
167 */
168 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100169setup_slavepty(int fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
171 if (fd < 0)
172 return 0;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100173#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) \
174 && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175# if defined(HAVE_SYS_PTEM_H) || defined(hpux)
176 if (ioctl(fd, I_PUSH, "ptem") != 0)
177 return -1;
178# endif
179 if (ioctl(fd, I_PUSH, "ldterm") != 0)
180 return -1;
Bram Moolenaara899e6e2016-12-03 16:40:51 +0100181# ifdef SUN_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (ioctl(fd, I_PUSH, "ttcompat") != 0)
183 return -1;
184# endif
185#endif
186 return 0;
187}
188
Bram Moolenaar1e449682019-04-28 14:59:59 +0200189#if defined(HAVE_POSIX_OPENPT) && !defined(PTY_DONE)
190#define PTY_DONE
191 int
192mch_openpty(char **ttyn)
193{
194 int f;
195 char *m;
196 RETSIGTYPE (*sigcld) SIGPROTOARG;
197 static char TtyName[32]; // used for opening a new pty-pair
198
199 if ((f = posix_openpt(O_RDWR | O_NOCTTY | O_EXTRA)) == -1)
200 return -1;
201
202 // SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
203 // exec()s pt_chmod
204 sigcld = signal(SIGCHLD, SIG_DFL);
205 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
206 {
207 signal(SIGCHLD, sigcld);
208 close(f);
209 return -1;
210 }
211 signal(SIGCHLD, sigcld);
212 vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1);
213 initmaster(f);
214 *ttyn = TtyName;
215 return f;
216}
217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219#if defined(OSX) && !defined(PTY_DONE)
220#define PTY_DONE
221 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100222mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 int f;
225 static char TtyName[32];
226
227 if ((f = open_controlling_pty(TtyName)) < 0)
228 return -1;
229 initmaster(f);
230 *ttyn = TtyName;
231 return f;
232}
233#endif
234
235#if (defined(sequent) || defined(_SEQUENT_)) && defined(HAVE_GETPSEUDOTTY) \
236 && !defined(PTY_DONE)
237#define PTY_DONE
238 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100239mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 char *m, *s;
242 int f;
243 /* used for opening a new pty-pair: */
244 static char PtyName[32];
245 static char TtyName[32];
246
247 if ((f = getpseudotty(&s, &m)) < 0)
248 return -1;
249#ifdef _SEQUENT_
250 fvhangup(s);
251#endif
Bram Moolenaar639304d2011-04-11 14:24:37 +0200252 vim_strncpy((char_u *)PtyName, (char_u *)m, sizeof(PtyName) - 1);
253 vim_strncpy((char_u *)TtyName, (char_u *)s, sizeof(TtyName) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 initmaster(f);
255 *ttyn = TtyName;
256 return f;
257}
258#endif
259
260#if defined(__sgi) && !defined(PTY_DONE)
261#define PTY_DONE
262 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100263mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264{
265 int f;
266 char *name;
Bram Moolenaard99df422016-01-29 23:20:40 +0100267 RETSIGTYPE (*sigcld) SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269 /*
270 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
271 * exec() /usr/adm/mkpts
272 */
273 sigcld = signal(SIGCHLD, SIG_DFL);
274 name = _getpty(&f, O_RDWR | O_NONBLOCK | O_EXTRA, 0600, 0);
275 signal(SIGCHLD, sigcld);
276
277 if (name == 0)
278 return -1;
279 initmaster(f);
280 *ttyn = name;
281 return f;
282}
283#endif
284
285#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
286#define PTY_DONE
287 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100288mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289{
290 int f;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200291 stat_T buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 /* used for opening a new pty-pair: */
293 static char TtyName[32];
294
295 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_NONBLOCK | O_EXTRA, 0)) < 0)
296 return -1;
297 if (mch_fstat(f, &buf) < 0)
298 {
299 close(f);
300 return -1;
301 }
302 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
303 initmaster(f);
304 *ttyn = TtyName;
305 return f;
306}
307#endif
308
Bram Moolenaarc958b312017-09-30 20:04:36 +0200309#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) \
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100310 && !(defined(MACOS_X) && !defined(MAC_OS_X_VERSION_10_6))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
Bram Moolenaar95dd9c12008-08-17 21:03:18 +0000312/* NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work!
Bram Moolenaarc958b312017-09-30 20:04:36 +0200313 * Same for Mac OS X Leopard (10.5). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#define PTY_DONE
315 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100316mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317{
318 int f;
Bram Moolenaareb80f042010-07-24 20:44:27 +0200319 char *m;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100320 RETSIGTYPE (*sigcld) SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 /* used for opening a new pty-pair: */
322 static char TtyName[32];
323
324 if ((f = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
325 return -1;
326
327 /*
328 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
329 * exec()s pt_chmod
330 */
331 sigcld = signal(SIGCHLD, SIG_DFL);
332 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
333 {
334 signal(SIGCHLD, sigcld);
335 close(f);
336 return -1;
337 }
338 signal(SIGCHLD, sigcld);
Bram Moolenaar639304d2011-04-11 14:24:37 +0200339 vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 initmaster(f);
341 *ttyn = TtyName;
342 return f;
343}
344#endif
345
346#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
347#define PTY_DONE
348
349#ifdef _IBMR2
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200350static int aixhack = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#endif
352
353 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100354mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
356 int f;
357 /* used for opening a new pty-pair: */
358 static char TtyName[32];
359
360 /* a dumb looking loop replaced by mycrofts code: */
361 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_EXTRA)) < 0)
362 return -1;
Bram Moolenaar639304d2011-04-11 14:24:37 +0200363 vim_strncpy((char_u *)TtyName, (char_u *)ttyname(f), sizeof(TtyName) - 1);
Bram Moolenaar311d9822007-02-27 15:48:28 +0000364 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 {
366 close(f);
367 return -1;
368 }
369 initmaster(f);
370# ifdef _IBMR2
371 if (aixhack >= 0)
372 close(aixhack);
373 if ((aixhack = open(TtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) < 0)
374 {
375 close(f);
376 return -1;
377 }
378# endif
379 *ttyn = TtyName;
380 return f;
381}
382#endif
383
384#ifndef PTY_DONE
385
386# ifdef hpux
387static char PtyProto[] = "/dev/ptym/ptyXY";
388static char TtyProto[] = "/dev/pty/ttyXY";
389# else
390# ifdef __BEOS__
391static char PtyProto[] = "/dev/pt/XY";
392static char TtyProto[] = "/dev/tt/XY";
393# else
394static char PtyProto[] = "/dev/ptyXY";
395static char TtyProto[] = "/dev/ttyXY";
396# endif
397# endif
398
399 int
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100400mch_openpty(char **ttyn)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
402 char *p, *q, *l, *d;
403 int f;
404 /* used for opening a new pty-pair: */
405 static char PtyName[32];
406 static char TtyName[32];
407
408 strcpy(PtyName, PtyProto);
409 strcpy(TtyName, TtyProto);
410 for (p = PtyName; *p != 'X'; p++)
411 ;
412 for (q = TtyName; *q != 'X'; q++)
413 ;
414 for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
415 {
416 for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 continue;
420 q[0] = *l;
421 q[1] = *d;
Bram Moolenaar311d9822007-02-27 15:48:28 +0000422 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
424 close(f);
425 continue;
426 }
Bram Moolenaara899e6e2016-12-03 16:40:51 +0100427#if defined(SUN_SYSTEM) && defined(TIOCGPGRP) && !defined(SUNOS3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 /* Hack to ensure that the slave side of the pty is
429 * unused. May not work in anything other than SunOS4.1
430 */
431 {
432 int pgrp;
433
434 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
435 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
436 {
437 close(f);
438 continue;
439 }
440 }
441#endif
442 initmaster(f);
443 *ttyn = TtyName;
444 return f;
445 }
446 }
447 return -1;
448}
449#endif
Bram Moolenaar6ed80692017-07-22 20:53:21 +0200450
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100451/*
452 * Call isatty(fd), except for SunOS where it's done differently.
453 */
454 int
455mch_isatty(int fd)
456{
457# if defined(I_STR) && defined(HAVE_SYS_PTMS_H) && defined(HAVE_SVR4_PTYS) \
458 && defined(SUN_SYSTEM)
459 // On SunOS, isatty() for /dev/ptmx returns false or sometimes can hang up
460 // in the inner ioctl(), and therefore first determine whether "fd" is a
461 // master device.
462 struct strioctl istr;
463
464 istr.ic_cmd = ISPTM;
465 istr.ic_timout = 0;
466 istr.ic_dp = NULL;
467 istr.ic_len = 0;
468
469 if (ioctl(fd, I_STR, &istr) == 0)
470 // Trick: return 2 in order to advice the caller that "fd" is a master
471 // device. cf. src/os_unix.c:get_tty_fd()
472 return 2;
473# endif
474 return isatty(fd);
475}
476
477#endif /* FEAT_GUI || FEAT_JOB_CHANNEL */