blob: 765cd57e7abebed8917737760e193bf2a84f838b [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 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
15 * Also for BeOS and Atari MiNT.
16 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#include "os_unixx.h" /* unix includes for os_unix.c only */
28
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaar643b6142018-09-12 20:29:09 +020046#ifdef __BEOS__
Bram Moolenaar311d9822007-02-27 15:48:28 +000047# undef select
Bram Moolenaar643b6142018-09-12 20:29:09 +020048# define select beos_select
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
Bram Moolenaara2442432007-04-26 14:26:37 +000051#ifdef __CYGWIN__
52# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000053# include <cygwin/version.h>
54# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
55 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010056# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
57# define WIN32_LEAN_AND_MEAN
58# include <windows.h>
59# include "winclip.pro"
60# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000061# endif
62#endif
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_MOUSE_GPM
65# include <gpm.h>
66/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
67 * I just copied relevant defines here. A cleaner solution would be to put gpm
68 * code into separate file and include there linux/keyboard.h
69 */
70/* #include <linux/keyboard.h> */
71# define KG_SHIFT 0
72# define KG_CTRL 2
73# define KG_ALT 3
74# define KG_ALTGR 1
75# define KG_SHIFTL 4
76# define KG_SHIFTR 5
77# define KG_CTRLL 6
78# define KG_CTRLR 7
79# define KG_CAPSSHIFT 8
80
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static void gpm_close(void);
82static int gpm_open(void);
83static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
85
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000086#ifdef FEAT_SYSMOUSE
87# include <sys/consio.h>
88# include <sys/fbio.h>
89
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int sysmouse_open(void);
91static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010092static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000093#endif
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * end of autoconf section. To be extended...
97 */
98
99/* Are the following #ifdefs still required? And why? Is that for X11? */
100
101#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
102# ifdef SIGWINCH
103# undef SIGWINCH
104# endif
105# ifdef TIOCGWINSZ
106# undef TIOCGWINSZ
107# endif
108#endif
109
110#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
111# define SIGWINCH SIGWINDOW
112#endif
113
114#ifdef FEAT_X11
115# include <X11/Xlib.h>
116# include <X11/Xutil.h>
117# include <X11/Xatom.h>
118# ifdef FEAT_XCLIPBOARD
119# include <X11/Intrinsic.h>
120# include <X11/Shell.h>
121# include <X11/StringDefs.h>
122static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void clip_update(void);
124static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# endif
126
127# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
128Window x11_window = 0;
129# endif
130Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
133#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200137static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static int did_set_title = FALSE;
139static char_u *oldicon = NULL;
140static int did_set_icon = FALSE;
141#endif
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar205b8862011-09-07 15:04:31 +0200145#ifdef HAVE_UNION_WAIT
146typedef union wait waitstatus;
147#else
148typedef int waitstatus;
149#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200150static int WaitForChar(long msec, int *interrupted, int ignore_input);
151static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100152#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200153int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200155static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157
158#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000160# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#endif
162
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
165#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100166static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100169static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100172static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174#if defined(SIGALRM) && defined(FEAT_X11) \
175 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
176# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100177static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000178/* volatile because it is used in signal handler sig_alarm(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200179static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100181static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static void catch_int_signal(void);
184static void set_signals(void);
185static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200186#ifdef HAVE_SIGPROCMASK
187# define SIGSET_DECL(set) sigset_t set;
188# define BLOCK_SIGNALS(set) block_signals(set)
189# define UNBLOCK_SIGNALS(set) unblock_signals(set)
190#else
191# define SIGSET_DECL(set)
192# define BLOCK_SIGNALS(set) do { /**/ } while (0)
193# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
194#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100195static int have_wildcard(int, char_u **);
196static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100198static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000201# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000204/* volatile because it is used in signal handler sig_winch(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200205static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206static char_u *extra_shell_arg = NULL;
207static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000208/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200209static volatile sig_atomic_t deadly_signal = 0; /* The signal we caught */
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000210/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200211static volatile sig_atomic_t in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100213#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
214static int dont_check_job_ended = 0;
215#endif
216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
218
219#ifdef USE_XSMP
220typedef struct
221{
222 SmcConn smcconn; /* The SM connection ID */
223 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200224 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 Bool save_yourself; /* If we're in the middle of a save_yourself */
226 Bool shutdown; /* If we're in shutdown mode */
227} xsmp_config_T;
228
229static xsmp_config_T xsmp;
230#endif
231
232#ifdef SYS_SIGLIST_DECLARED
233/*
234 * I have seen
235 * extern char *_sys_siglist[NSIG];
236 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
237 * that describe the signals. That is nearly what we want here. But
238 * autoconf does only check for sys_siglist (without the underscore), I
239 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100240 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242#endif
243
244static struct signalinfo
245{
246 int sig; /* Signal number, eg. SIGSEGV etc */
247 char *name; /* Signal name (not char_u!). */
248 char deadly; /* Catch as a deadly signal? */
249} signal_info[] =
250{
251#ifdef SIGHUP
252 {SIGHUP, "HUP", TRUE},
253#endif
254#ifdef SIGQUIT
255 {SIGQUIT, "QUIT", TRUE},
256#endif
257#ifdef SIGILL
258 {SIGILL, "ILL", TRUE},
259#endif
260#ifdef SIGTRAP
261 {SIGTRAP, "TRAP", TRUE},
262#endif
263#ifdef SIGABRT
264 {SIGABRT, "ABRT", TRUE},
265#endif
266#ifdef SIGEMT
267 {SIGEMT, "EMT", TRUE},
268#endif
269#ifdef SIGFPE
270 {SIGFPE, "FPE", TRUE},
271#endif
272#ifdef SIGBUS
273 {SIGBUS, "BUS", TRUE},
274#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100275#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
276 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {SIGSEGV, "SEGV", TRUE},
278#endif
279#ifdef SIGSYS
280 {SIGSYS, "SYS", TRUE},
281#endif
282#ifdef SIGALRM
283 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
284#endif
285#ifdef SIGTERM
286 {SIGTERM, "TERM", TRUE},
287#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100288#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGVTALRM, "VTALRM", TRUE},
290#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000291#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
292 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
293 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {SIGPROF, "PROF", TRUE},
295#endif
296#ifdef SIGXCPU
297 {SIGXCPU, "XCPU", TRUE},
298#endif
299#ifdef SIGXFSZ
300 {SIGXFSZ, "XFSZ", TRUE},
301#endif
302#ifdef SIGUSR1
303 {SIGUSR1, "USR1", TRUE},
304#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000305#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
306 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGUSR2, "USR2", TRUE},
308#endif
309#ifdef SIGINT
310 {SIGINT, "INT", FALSE},
311#endif
312#ifdef SIGWINCH
313 {SIGWINCH, "WINCH", FALSE},
314#endif
315#ifdef SIGTSTP
316 {SIGTSTP, "TSTP", FALSE},
317#endif
318#ifdef SIGPIPE
319 {SIGPIPE, "PIPE", FALSE},
320#endif
321 {-1, "Unknown!", FALSE}
322};
323
Bram Moolenaar25724922009-07-14 15:38:41 +0000324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000326{
327 if (p_verbose >= 5)
328 {
329 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100330 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000331 verbose_leave();
332 }
333# ifdef VMS
334 return chdir(vms_fixfilename(path));
335# else
336 return chdir(path);
337# endif
338}
339
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200340/* Why is NeXT excluded here (and not in os_unixx.h)? */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100341#if defined(ECHOE) && defined(ICANON) \
342 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
343 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200344# define NEW_TTY_SYSTEM
345#endif
346
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100348 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100351mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200353 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100355 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356}
357
358/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000359 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 * Get a characters from the keyboard.
361 * Return the number of characters that are available.
362 * If wtime == 0 do not wait for characters.
363 * If wtime == n wait a short time for characters.
364 * If wtime == -1 wait forever for characters.
365 */
366 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100367mch_inchar(
368 char_u *buf,
369 int maxlen,
370 long wtime, /* don't use "time", MIPS cannot handle it */
371 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
373 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200374 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200375 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200376 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200377 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100378#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +0100379 elapsed_T start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200380
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100381 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200384 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200385 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200387 /* Check if window changed size while we were busy, perhaps the ":set
388 * columns=99" command was used. */
389 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200391
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200392#ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +0200393 // Only process messages when waiting.
394 if (wtime != 0)
395 {
396 parse_queued_messages();
397 // If input was put directly in typeahead buffer bail out here.
398 if (typebuf_changed(tb_change_cnt))
399 return 0;
400 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200401#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200402 if (wtime < 0 && did_start_blocking)
403 /* blocking and already waited for p_ut */
404 wait_time = -1;
405 else
406 {
407 if (wtime >= 0)
408 wait_time = wtime;
409 else
410 /* going to block after p_ut */
411 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100412#ifdef ELAPSED_FUNC
413 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200414#endif
415 wait_time -= elapsed_time;
416 if (wait_time < 0)
417 {
418 if (wtime >= 0)
419 /* no character available within "wtime" */
420 return 0;
421
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200422 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200423 {
424 /* no character available within 'updatetime' */
425 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200426 if (trigger_cursorhold() && maxlen >= 3
427 && !typebuf_changed(tb_change_cnt))
428 {
429 buf[0] = K_SPECIAL;
430 buf[1] = KS_EXTRA;
431 buf[2] = (int)KE_CURSORHOLD;
432 return 3;
433 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200434 /*
435 * If there is no character available within 'updatetime'
436 * seconds flush all the swap files to disk.
437 * Also done when interrupted by SIGWINCH.
438 */
439 before_blocking();
440 continue;
441 }
442 }
443 }
444
445#ifdef FEAT_JOB_CHANNEL
446 /* Checking if a job ended requires polling. Do this every 100 msec. */
447 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
448 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100449 /* If there is readahead then parse_queued_messages() timed out and we
450 * should call it again soon. */
451 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
452 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200453#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100454#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100455 if (p_beval && wait_time > 100L)
456 /* The 'balloonexpr' may indirectly invoke a callback while waiting
457 * for a character, need to check often. */
458 wait_time = 100L;
459#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200460
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200462 * We want to be interrupted by the winch signal
463 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200465 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200466 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200467 /* If input was put directly in typeahead buffer bail out here. */
468 if (typebuf_changed(tb_change_cnt))
469 return 0;
470
471 /*
472 * For some terminals we only get one character at a time.
473 * We want the get all available characters, so we could keep on
474 * trying until none is available
475 * For some other terminals this is quite slow, that's why we don't
476 * do it.
477 */
478 len = read_from_input_buf(buf, (long)maxlen);
479 if (len > 0)
480 return len;
481 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200484 /* no character available */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +0100485#ifndef ELAPSED_FUNC
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200486 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100487 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200490 if (do_resize /* interrupted by SIGWINCH signal */
491#ifdef FEAT_CLIENTSERVER
492 || server_waiting()
493#endif
494#ifdef MESSAGE_QUEUE
495 || interrupted
496#endif
497 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100498 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200499 continue;
500
501 /* no character available or interrupted */
502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200504 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505}
506
507 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100508handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 do_resize = FALSE;
511 shell_resized();
512}
513
514/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200515 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 */
517 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100518mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200520 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521}
522
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200523#if defined(FEAT_TERMINAL) || defined(PROTO)
524/*
525 * Check for any pending input or messages.
526 */
527 int
528mch_check_messages(void)
529{
530 return WaitForChar(0L, NULL, TRUE);
531}
532#endif
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
535# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000536# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537# endif
538# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
539# include <sys/sysctl.h>
540# endif
541# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
542# include <sys/sysinfo.h>
543# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100544# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100545# include <mach/mach_host.h>
546# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000550 * Return total amount of memory available in Kbyte.
551 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100554mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000557 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar362dc332018-03-05 21:59:37 +0100559# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100560 {
561 /* Mac (Darwin) way of getting the amount of RAM available */
562 mach_port_t host = mach_host_self();
563 kern_return_t kret;
564# ifdef HOST_VM_INFO64
565 struct vm_statistics64 vm_stat;
566 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
Bram Moolenaar988615f2018-02-27 17:58:20 +0100568 kret = host_statistics64(host, HOST_VM_INFO64,
569 (host_info64_t)&vm_stat, &count);
570# else
571 struct vm_statistics vm_stat;
572 natural_t count = HOST_VM_INFO_COUNT;
573
574 kret = host_statistics(host, HOST_VM_INFO,
575 (host_info_t)&vm_stat, &count);
576# endif
577 if (kret == KERN_SUCCESS)
578 /* get the amount of user memory by summing each usage */
579 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
580 + vm_stat.inactive_count
581# ifdef MAC_OS_X_VERSION_10_9
582 + vm_stat.compressor_page_count
583# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100584 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100585 mach_port_deallocate(mach_task_self(), host);
586 }
587# endif
588
589# ifdef HAVE_SYSCTL
590 if (mem == 0)
591 {
592 /* BSD way of getting the amount of RAM available. */
593 int mib[2];
594 size_t len = sizeof(long_u);
595# ifdef HW_USERMEM64
596 long_u physmem;
597# else
598 /* sysctl() may return 32 bit or 64 bit, accept both */
599 union {
600 int_u u32;
601 long_u u64;
602 } physmem;
603# endif
604
605 mib[0] = CTL_HW;
606# ifdef HW_USERMEM64
607 mib[1] = HW_USERMEM64;
608# else
609 mib[1] = HW_USERMEM;
610# endif
611 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
612 {
613# ifdef HW_USERMEM64
614 mem = (long_u)physmem;
615# else
616 if (len == sizeof(physmem.u64))
617 mem = (long_u)physmem.u64;
618 else
619 mem = (long_u)physmem.u32;
620# endif
621 }
622 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200625# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (mem == 0)
627 {
628 struct sysinfo sinfo;
629
630 /* Linux way of getting amount of RAM available */
631 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000632 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200633# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000634 /* avoid overflow as much as possible */
635 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
636 {
637 sinfo.mem_unit = sinfo.mem_unit >> 1;
638 --shiftright;
639 }
640 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200641# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200643# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (mem == 0)
650 {
651 long pagesize, pagecount;
652
653 /* Solaris way of getting amount of RAM available */
654 pagesize = sysconf(_SC_PAGESIZE);
655 pagecount = sysconf(_SC_PHYS_PAGES);
656 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000657 {
658 /* avoid overflow as much as possible */
659 while (shiftright > 0 && (pagesize & 1) == 0)
660 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000661 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000662 --shiftright;
663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 /* Return the minimum of the physical memory and the user limit, because
670 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200671# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
673 struct rlimit rlp;
674
675 if (getrlimit(RLIMIT_DATA, &rlp) == 0
676 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200677# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200679# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000682 {
683 mem = (long_u)rlp.rlim_cur;
684 shiftright = 10;
685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000690 return mem >> shiftright;
691 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692}
693#endif
694
695 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100696mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
698 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000699#ifdef FEAT_MZSCHEME
700 long total = msec; /* remember original value */
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703 if (ignoreinput)
704 {
705 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000706 * here. But we don't want QUIT to kill us (CTRL-\ used in a
707 * shell may produce SIGQUIT). */
708 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 old_tmode = curr_tmode;
710 if (curr_tmode == TMODE_RAW)
711 settmode(TMODE_SLEEP);
712
713 /*
714 * Everybody sleeps in a different way...
715 * Prefer nanosleep(), some versions of usleep() can only sleep up to
716 * one second.
717 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000718#ifdef FEAT_MZSCHEME
719 do
720 {
721 /* if total is large enough, wait by portions in p_mzq */
722 if (total > p_mzq)
723 msec = p_mzq;
724 else
725 msec = total;
726 total -= msec;
727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#ifdef HAVE_NANOSLEEP
729 {
730 struct timespec ts;
731
732 ts.tv_sec = msec / 1000;
733 ts.tv_nsec = (msec % 1000) * 1000000;
734 (void)nanosleep(&ts, NULL);
735 }
736#else
737# ifdef HAVE_USLEEP
738 while (msec >= 1000)
739 {
740 usleep((unsigned int)(999 * 1000));
741 msec -= 999;
742 }
743 usleep((unsigned int)(msec * 1000));
744# else
745# ifndef HAVE_SELECT
746 poll(NULL, 0, (int)msec);
747# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
749 struct timeval tv;
750
751 tv.tv_sec = msec / 1000;
752 tv.tv_usec = (msec % 1000) * 1000;
753 /*
754 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
755 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
756 */
757 select(0, NULL, NULL, NULL, &tv);
758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759# endif /* HAVE_SELECT */
760# endif /* HAVE_NANOSLEEP */
761#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000762#ifdef FEAT_MZSCHEME
763 }
764 while (total > 0);
765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
767 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000768 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 }
770 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200771 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772}
773
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000774#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
776# define HAVE_CHECK_STACK_GROWTH
777/*
778 * Support for checking for an almost-out-of-stack-space situation.
779 */
780
781/*
782 * Return a pointer to an item on the stack. Used to find out if the stack
783 * grows up or down.
784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785static int stack_grows_downwards;
786
787/*
788 * Find out if the stack grows upwards or downwards.
789 * "p" points to a variable on the stack of the caller.
790 */
791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100792check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 int i;
795
796 stack_grows_downwards = (p > (char *)&i);
797}
798#endif
799
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000800#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801static char *stack_limit = NULL;
802
803#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
804# include <pthread.h>
805# include <pthread_np.h>
806#endif
807
808/*
809 * Find out until how var the stack can grow without getting into trouble.
810 * Called when starting up and when switching to the signal stack in
811 * deathtrap().
812 */
813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100814get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 struct rlimit rlp;
817 int i;
818 long lim;
819
820 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
821 * limit doesn't fit in a long (rlim_cur might be "long long"). */
822 if (getrlimit(RLIMIT_STACK, &rlp) == 0
823 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
824# ifdef RLIM_INFINITY
825 && rlp.rlim_cur != RLIM_INFINITY
826# endif
827 )
828 {
829 lim = (long)rlp.rlim_cur;
830#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
831 {
832 pthread_attr_t attr;
833 size_t size;
834
835 /* On FreeBSD the initial thread always has a fixed stack size, no
836 * matter what the limits are set to. Normally it's 1 Mbyte. */
837 pthread_attr_init(&attr);
838 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
839 {
840 pthread_attr_getstacksize(&attr, &size);
841 if (lim > (long)size)
842 lim = (long)size;
843 }
844 pthread_attr_destroy(&attr);
845 }
846#endif
847 if (stack_grows_downwards)
848 {
849 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
850 if (stack_limit >= (char *)&i)
851 /* overflow, set to 1/16 of current stack position */
852 stack_limit = (char *)((long)&i / 16L);
853 }
854 else
855 {
856 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
857 if (stack_limit <= (char *)&i)
858 stack_limit = NULL; /* overflow */
859 }
860 }
861}
862
863/*
864 * Return FAIL when running out of stack space.
865 * "p" must point to any variable local to the caller that's on the stack.
866 */
867 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100868mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 if (stack_limit != NULL)
871 {
872 if (stack_grows_downwards)
873 {
874 if (p < stack_limit)
875 return FAIL;
876 }
877 else if (p > stack_limit)
878 return FAIL;
879 }
880 return OK;
881}
882#endif
883
884#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
885/*
886 * Support for using the signal stack.
887 * This helps when we run out of stack space, which causes a SIGSEGV. The
888 * signal handler then must run on another stack, since the normal stack is
889 * completely full.
890 */
891
892#ifndef SIGSTKSZ
893# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
894#endif
895
896# ifdef HAVE_SIGALTSTACK
897static stack_t sigstk; /* for sigaltstack() */
898# else
899static struct sigstack sigstk; /* for sigstack() */
900# endif
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902static char *signal_stack;
903
904 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 if (signal_stack != NULL)
908 {
909# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910# ifdef HAVE_SS_BASE
911 sigstk.ss_base = signal_stack;
912# else
913 sigstk.ss_sp = signal_stack;
914# endif
915 sigstk.ss_size = SIGSTKSZ;
916 sigstk.ss_flags = 0;
917 (void)sigaltstack(&sigstk, NULL);
918# else
919 sigstk.ss_sp = signal_stack;
920 if (stack_grows_downwards)
921 sigstk.ss_sp += SIGSTKSZ - 1;
922 sigstk.ss_onstack = 0;
923 (void)sigstack(&sigstk, NULL);
924# endif
925 }
926}
927#endif
928
929/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000930 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * will barf when the second argument to signal() is ``wrong''.
932 * Let me try it with a few tricky defines from my own osdef.h (jw).
933 */
934#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 static RETSIGTYPE
936sig_winch SIGDEFARG(sigarg)
937{
938 /* this is not required on all systems, but it doesn't hurt anybody */
939 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
940 do_resize = TRUE;
941 SIGRETURN;
942}
943#endif
944
945#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 static RETSIGTYPE
947catch_sigint SIGDEFARG(sigarg)
948{
949 /* this is not required on all systems, but it doesn't hurt anybody */
950 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
951 got_int = TRUE;
952 SIGRETURN;
953}
954#endif
955
956#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 static RETSIGTYPE
958catch_sigpwr SIGDEFARG(sigarg)
959{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000960 /* this is not required on all systems, but it doesn't hurt anybody */
961 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 /*
963 * I'm not sure we get the SIGPWR signal when the system is really going
964 * down or when the batteries are almost empty. Just preserve the swap
965 * files and don't exit, that can't do any harm.
966 */
967 ml_sync_all(FALSE, FALSE);
968 SIGRETURN;
969}
970#endif
971
972#ifdef SET_SIG_ALARM
973/*
974 * signal function for alarm().
975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 static RETSIGTYPE
977sig_alarm SIGDEFARG(sigarg)
978{
979 /* doesn't do anything, just to break a system call */
980 sig_alarm_called = TRUE;
981 SIGRETURN;
982}
983#endif
984
Bram Moolenaaredce7422019-01-20 18:39:30 +0100985#if (defined(HAVE_SETJMP_H) \
986 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
987 || defined(FEAT_LIBCALL))) \
988 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100989# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100990
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100991// argument to SETJMP()
992static JMP_BUF lc_jump_env;
993
994# ifdef SIGHASARG
995// Caught signal number, 0 when no was signal caught; used for mch_libcall().
996// Volatile because it is used in signal handlers.
997static volatile sig_atomic_t lc_signal;
998# endif
999
1000// TRUE when lc_jump_env is valid.
1001// Volatile because it is used in signal handler deathtrap().
1002static volatile sig_atomic_t lc_active INIT(= FALSE);
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004/*
1005 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001006 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001008 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 * Usage:
1010 * mch_startjmp();
1011 * if (SETJMP(lc_jump_env) != 0)
1012 * {
1013 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001014 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * }
1016 * else
1017 * {
1018 * do_the_work;
1019 * mch_endjmp();
1020 * }
1021 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1022 * not return before the saved environment is used.
1023 * Returns OK for normal return, FAIL when the protected code caused a
1024 * problem and LONGJMP() was used.
1025 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001027mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001029# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 lc_active = TRUE;
1033}
1034
Bram Moolenaar113e1072019-01-20 15:30:40 +01001035 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001036mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
1038 lc_active = FALSE;
1039}
1040
Bram Moolenaar113e1072019-01-20 15:30:40 +01001041 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001042mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001045 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1046 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 init_signal_stack();
1048# endif
1049}
1050#endif
1051
1052/*
1053 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001054 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001056 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1057 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
1059 static RETSIGTYPE
1060deathtrap SIGDEFARG(sigarg)
1061{
1062 static int entered = 0; /* count the number of times we got here.
1063 Note: when memory has been corrupted
1064 this may get an arbitrary value! */
1065#ifdef SIGHASARG
1066 int i;
1067#endif
1068
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001069#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 /*
1071 * Catch a crash in protected code.
1072 * Restores the environment saved in lc_jump_env, which looks like
1073 * SETJMP() returns 1.
1074 */
1075 if (lc_active)
1076 {
1077# if defined(SIGHASARG)
1078 lc_signal = sigarg;
1079# endif
1080 lc_active = FALSE; /* don't jump again */
1081 LONGJMP(lc_jump_env, 1);
1082 /* NOTREACHED */
1083 }
1084#endif
1085
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001086#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001087# ifdef SIGQUIT
1088 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1089 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1090 * pressing CTRL-\, but we don't want Vim to exit then. */
1091 if (in_mch_delay && sigarg == SIGQUIT)
1092 SIGRETURN;
1093# endif
1094
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001095 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1096 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1097 * free(). Calling free() again may then cause a crash. */
1098 if (entered == 0
1099 && (0
1100# ifdef SIGHUP
1101 || sigarg == SIGHUP
1102# endif
1103# ifdef SIGQUIT
1104 || sigarg == SIGQUIT
1105# endif
1106# ifdef SIGTERM
1107 || sigarg == SIGTERM
1108# endif
1109# ifdef SIGPWR
1110 || sigarg == SIGPWR
1111# endif
1112# ifdef SIGUSR1
1113 || sigarg == SIGUSR1
1114# endif
1115# ifdef SIGUSR2
1116 || sigarg == SIGUSR2
1117# endif
1118 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001119 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001120 SIGRETURN;
1121#endif
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 /* Remember how often we have been called. */
1124 ++entered;
1125
Bram Moolenaare429e702016-06-10 19:49:14 +02001126 /* Executing autocommands is likely to use more stack space than we have
1127 * available in the signal stack. */
1128 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#ifdef FEAT_EVAL
1131 /* Set the v:dying variable. */
1132 set_vim_var_nr(VV_DYING, (long)entered);
1133#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001134 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001136#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 /* Since we are now using the signal stack, need to reset the stack
1138 * limit. Otherwise using a regexp will fail. */
1139 get_stack_limit();
1140#endif
1141
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001142#if 0
1143 /* This is for opening gdb the moment Vim crashes.
1144 * You need to manually adjust the file name and Vim executable name.
1145 * Suggested by SungHyun Nam. */
1146 {
1147# define VI_GDB_FILE "/tmp/vimgdb"
1148# define VIM_NAME "/usr/bin/vim"
1149 FILE *fp = fopen(VI_GDB_FILE, "w");
1150 if (fp)
1151 {
1152 fprintf(fp,
1153 "file %s\n"
1154 "attach %d\n"
1155 "set height 1000\n"
1156 "bt full\n"
1157 , VIM_NAME, getpid());
1158 fclose(fp);
1159 system("xterm -e gdb -x "VI_GDB_FILE);
1160 unlink(VI_GDB_FILE);
1161 }
1162 }
1163#endif
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#ifdef SIGHASARG
1166 /* try to find the name of this signal */
1167 for (i = 0; signal_info[i].sig != -1; i++)
1168 if (sigarg == signal_info[i].sig)
1169 break;
1170 deadly_signal = sigarg;
1171#endif
1172
1173 full_screen = FALSE; /* don't write message to the GUI, it might be
1174 * part of the problem... */
1175 /*
1176 * If something goes wrong after entering here, we may get here again.
1177 * When this happens, give a message and try to exit nicely (resetting the
1178 * terminal mode, etc.)
1179 * When this happens twice, just exit, don't even try to give a message,
1180 * stack may be corrupt or something weird.
1181 * When this still happens again (or memory was corrupted in such a way
1182 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1183 */
1184 if (entered >= 3)
1185 {
1186 reset_signals(); /* don't catch any signals anymore */
1187 may_core_dump();
1188 if (entered >= 4)
1189 _exit(8);
1190 exit(7);
1191 }
1192 if (entered == 2)
1193 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001194 /* No translation, it may call malloc(). */
1195 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 out_flush();
1197 getout(1);
1198 }
1199
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001200 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001202 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 signal_info[i].name);
1204#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001205 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001207
1208 /* Preserve files and exit. This sets the really_exiting flag to prevent
1209 * calling free(). */
1210 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaare429e702016-06-10 19:49:14 +02001212 /* NOTREACHED */
1213
Bram Moolenaar009b2592004-10-24 19:18:58 +00001214#ifdef NBDEBUG
1215 reset_signals();
1216 may_core_dump();
1217 abort();
1218#endif
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 SIGRETURN;
1221}
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001224 * Invoked after receiving SIGCONT. We don't know what happened while
1225 * sleeping, deal with part of that.
1226 */
1227 static void
1228after_sigcont(void)
1229{
1230# ifdef FEAT_TITLE
1231 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1232 // again later.
1233 oldtitle_outdated = TRUE;
1234# endif
1235 settmode(TMODE_RAW);
1236 need_check_timestamps = TRUE;
1237 did_check_timestamps = FALSE;
1238}
1239
1240#if defined(SIGCONT)
1241static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001242static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001243
1244/*
1245 * With multi-threading, suspending might not work immediately. Catch the
1246 * SIGCONT signal, which will be used as an indication whether the suspending
1247 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001248 *
1249 * On Linux, signal is not always handled immediately either.
1250 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001251 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001252 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001253 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001255static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001256static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258/*
1259 * signal handler for SIGCONT
1260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 static RETSIGTYPE
1262sigcont_handler SIGDEFARG(sigarg)
1263{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001264 if (in_mch_suspend)
1265 {
1266 sigcont_received = TRUE;
1267 }
1268 else
1269 {
1270 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1271 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1272 // back to a sane mode. We should redraw, but we can't really do that
1273 // in a signal handler, do a redraw later.
1274 after_sigcont();
1275 redraw_later(CLEAR);
1276 cursor_on_force();
1277 out_flush();
1278 }
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 SIGRETURN;
1281}
1282#endif
1283
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001284#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001285# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001286static void *clip_star_save = NULL;
1287static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001288# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001289
1290/*
1291 * Called when Vim is going to sleep or execute a shell command.
1292 * We can't respond to requests for the X selections. Lose them, otherwise
1293 * other applications will hang. But first copy the text to cut buffer 0.
1294 */
1295 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001296loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001297{
1298 if (clip_star.owned || clip_plus.owned)
1299 {
1300 x11_export_final_selection();
1301 if (clip_star.owned)
1302 clip_lose_selection(&clip_star);
1303 if (clip_plus.owned)
1304 clip_lose_selection(&clip_plus);
1305 if (x11_display != NULL)
1306 XFlush(x11_display);
1307 }
1308}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001310# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001311/*
1312 * Save clipboard text to restore later.
1313 */
1314 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001315save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001316{
1317 if (clip_star.owned)
1318 clip_star_save = get_register('*', TRUE);
1319 if (clip_plus.owned)
1320 clip_plus_save = get_register('+', TRUE);
1321}
1322
1323/*
1324 * Restore clipboard text if no one own the X selection.
1325 */
1326 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001327restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001328{
1329 if (clip_star_save != NULL)
1330 {
1331 if (!clip_gen_owner_exists(&clip_star))
1332 put_register('*', clip_star_save);
1333 else
1334 free_register(clip_star_save);
1335 clip_star_save = NULL;
1336 }
1337 if (clip_plus_save != NULL)
1338 {
1339 if (!clip_gen_owner_exists(&clip_plus))
1340 put_register('+', clip_plus_save);
1341 else
1342 free_register(clip_plus_save);
1343 clip_plus_save = NULL;
1344 }
1345}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001346# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001347#endif
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349/*
1350 * If the machine has job control, use it to suspend the program,
1351 * otherwise fake it by starting a new shell.
1352 */
1353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001354mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 /* BeOS does have SIGTSTP, but it doesn't work. */
1357#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001358 in_mch_suspend = TRUE;
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 out_flush(); /* needed to make cursor visible on some systems */
1361 settmode(TMODE_COOK);
1362 out_flush(); /* needed to disable mouse on some systems */
1363
1364# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001365 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001367# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 sigcont_received = FALSE;
1369# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001372
1373# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001374 /*
1375 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001376 * immediately, but somehow not all the time, probably because it's handled
1377 * in another thread. Do not call pause() because there would be race
1378 * condition which would hang Vim if signal happened in between the test of
1379 * sigcont_received and the call to pause(). If signal is not yet received,
1380 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1381 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001382 */
1383 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001384 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001385
Bram Moolenaar262735e2009-07-14 10:20:22 +00001386 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001387 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001390 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaar2e310482018-08-21 13:09:10 +02001392 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393#else
1394 suspend_shell();
1395#endif
1396}
1397
1398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001399mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 Columns = 80;
1402 Rows = 24;
1403
1404 out_flush();
1405 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001406
Bram Moolenaar56718732006-03-15 22:53:57 +00001407#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001408 mac_conv_init();
1409#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001410#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1411 win_clip_init();
1412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413}
1414
1415 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001416set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418#if defined(SIGWINCH)
1419 /*
1420 * WINDOW CHANGE signal is handled with sig_winch().
1421 */
1422 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1423#endif
1424
1425 /*
1426 * We want the STOP signal to work, to make mch_suspend() work.
1427 * For "rvim" the STOP signal is ignored.
1428 */
1429#ifdef SIGTSTP
1430 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1431#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001432#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 signal(SIGCONT, sigcont_handler);
1434#endif
1435
1436 /*
1437 * We want to ignore breaking of PIPEs.
1438 */
1439#ifdef SIGPIPE
1440 signal(SIGPIPE, SIG_IGN);
1441#endif
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001444 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445#endif
1446
1447 /*
1448 * Ignore alarm signals (Perl's alarm() generates it).
1449 */
1450#ifdef SIGALRM
1451 signal(SIGALRM, SIG_IGN);
1452#endif
1453
1454 /*
1455 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1456 * work will be lost.
1457 */
1458#ifdef SIGPWR
1459 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1460#endif
1461
1462 /*
1463 * Arrange for other signals to gracefully shutdown Vim.
1464 */
1465 catch_signals(deathtrap, SIG_ERR);
1466
1467#if defined(FEAT_GUI) && defined(SIGHUP)
1468 /*
1469 * When the GUI is running, ignore the hangup signal.
1470 */
1471 if (gui.in_use)
1472 signal(SIGHUP, SIG_IGN);
1473#endif
1474}
1475
Bram Moolenaardf177f62005-02-22 08:39:57 +00001476#if defined(SIGINT) || defined(PROTO)
1477/*
1478 * Catch CTRL-C (only works while in Cooked mode).
1479 */
1480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001481catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001482{
1483 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1484}
1485#endif
1486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001488reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001491#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 /* SIGCONT isn't in the list, because its default action is ignore */
1493 signal(SIGCONT, SIG_DFL);
1494#endif
1495}
1496
1497 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001498catch_signals(
1499 RETSIGTYPE (*func_deadly)(),
1500 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 int i;
1503
1504 for (i = 0; signal_info[i].sig != -1; i++)
1505 if (signal_info[i].deadly)
1506 {
1507#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1508 struct sigaction sa;
1509
1510 /* Setup to use the alternate stack for the signal function. */
1511 sa.sa_handler = func_deadly;
1512 sigemptyset(&sa.sa_mask);
1513# if defined(__linux__) && defined(_REENTRANT)
1514 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1515 * thread handling in combination with using the alternate stack:
1516 * pthread library functions try to use the stack pointer to
1517 * identify the current thread, causing a SEGV signal, which
1518 * recursively calls deathtrap() and hangs. */
1519 sa.sa_flags = 0;
1520# else
1521 sa.sa_flags = SA_ONSTACK;
1522# endif
1523 sigaction(signal_info[i].sig, &sa, NULL);
1524#else
1525# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1526 struct sigvec sv;
1527
1528 /* Setup to use the alternate stack for the signal function. */
1529 sv.sv_handler = func_deadly;
1530 sv.sv_mask = 0;
1531 sv.sv_flags = SV_ONSTACK;
1532 sigvec(signal_info[i].sig, &sv, NULL);
1533# else
1534 signal(signal_info[i].sig, func_deadly);
1535# endif
1536#endif
1537 }
1538 else if (func_other != SIG_ERR)
1539 signal(signal_info[i].sig, func_other);
1540}
1541
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001542#ifdef HAVE_SIGPROCMASK
1543 static void
1544block_signals(sigset_t *set)
1545{
1546 sigset_t newset;
1547 int i;
1548
1549 sigemptyset(&newset);
1550
1551 for (i = 0; signal_info[i].sig != -1; i++)
1552 sigaddset(&newset, signal_info[i].sig);
1553
Bram Moolenaar2e310482018-08-21 13:09:10 +02001554# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001555 /* SIGCONT isn't in the list, because its default action is ignore */
1556 sigaddset(&newset, SIGCONT);
1557# endif
1558
1559 sigprocmask(SIG_BLOCK, &newset, set);
1560}
1561
1562 static void
1563unblock_signals(sigset_t *set)
1564{
1565 sigprocmask(SIG_SETMASK, set, NULL);
1566}
1567#endif
1568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001570 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001571 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1572 * return TRUE
1573 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1574 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001575 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001576 * Returns TRUE when Vim should exit.
1577 */
1578 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001579vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001580{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001581 static int got_signal = 0;
1582 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001583
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001584 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001585 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001586 case SIGNAL_BLOCK: blocked = TRUE;
1587 break;
1588
1589 case SIGNAL_UNBLOCK: blocked = FALSE;
1590 if (got_signal != 0)
1591 {
1592 kill(getpid(), got_signal);
1593 got_signal = 0;
1594 }
1595 break;
1596
1597 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001598 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001599 got_signal = sig;
1600#ifdef SIGPWR
1601 if (sig != SIGPWR)
1602#endif
1603 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001604 break;
1605 }
1606 return FALSE;
1607}
1608
1609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * Check_win checks whether we have an interactive stdout.
1611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001613mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (isatty(1))
1616 return OK;
1617 return FAIL;
1618}
1619
1620/*
1621 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1622 */
1623 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001624mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 if (isatty(read_cmd_fd))
1627 return TRUE;
1628 return FALSE;
1629}
1630
1631#ifdef FEAT_X11
1632
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001633# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1635
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636/*
1637 * Give a message about the elapsed time for opening the X window.
1638 */
1639 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001640xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643}
1644# endif
1645#endif
1646
1647#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1648/*
1649 * A few functions shared by X11 title and clipboard code.
1650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
1652static int got_x_error = FALSE;
1653
1654/*
1655 * X Error handler, otherwise X just exits! (very rude) -- webb
1656 */
1657 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001658x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001660 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 STRCAT(IObuff, _("\nVim: Got X error\n"));
1662
1663 /* We cannot print a message and continue, because no X calls are allowed
1664 * here (causes my system to hang). Silently continuing might be an
1665 * alternative... */
1666 preserve_exit(); /* preserve files and exit */
1667
1668 return 0; /* NOTREACHED */
1669}
1670
1671/*
1672 * Another X Error handler, just used to check for errors.
1673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001675x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 got_x_error = TRUE;
1678 return 0;
1679}
1680
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001681/*
1682 * Return TRUE when connection to the X server is desired.
1683 */
1684 static int
1685x_connect_to_server(void)
1686{
1687 // No point in connecting if we are exiting or dying.
1688 if (exiting || v_dying)
1689 return FALSE;
1690
1691#if defined(FEAT_CLIENTSERVER)
1692 if (x_force_connect)
1693 return TRUE;
1694#endif
1695 if (x_no_connect)
1696 return FALSE;
1697
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001698 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001699 if (clip_exclude_prog != NULL)
1700 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001701 // Just in case we get called recursively, return FALSE. This could
1702 // happen if vpeekc() is used while executing the prog and it causes a
1703 // related callback to be invoked.
1704 if (regprog_in_use(clip_exclude_prog))
1705 return FALSE;
1706
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001707 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1708 return FALSE;
1709 }
1710 return TRUE;
1711}
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001714# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715/*
1716 * An X IO Error handler, used to catch error while opening the display.
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001719x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 /* This function should not return, it causes exit(). Longjump instead. */
1722 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001723# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001724 return 0; /* avoid the compiler complains about missing return value */
1725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727# endif
1728
1729/*
1730 * An X IO Error handler, used to catch terminal errors.
1731 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001732static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001735x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001738 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 x11_window = 0;
1740 x11_display = NULL;
1741 xterm_Shell = (Widget)0;
1742
1743 /* This function should not return, it causes exit(). Longjump instead. */
1744 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001745# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001746 return 0; /* avoid the compiler complains about missing return value */
1747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001749
1750/*
1751 * If the X11 connection was lost try to restore it.
1752 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001753 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001754 */
1755 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001756may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001757{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001758 // No point in restoring the connecting if we are exiting or dying.
1759 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001760 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001761 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001762
1763# ifndef LESSTIF_VERSION
1764 /* This has been reported to avoid Vim getting stuck. */
1765 if (app_context != (XtAppContext)NULL)
1766 {
1767 XtDestroyApplicationContext(app_context);
1768 app_context = (XtAppContext)NULL;
1769 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1770 }
1771# endif
1772
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001773 setup_term_clip();
1774 get_x11_title(FALSE);
1775 }
1776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
1778
1779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 * Test if "dpy" and x11_window are valid by getting the window title.
1781 * I don't actually want it yet, so there may be a simpler call to use, but
1782 * this will cause the error handler x_error_check() to be called if anything
1783 * is wrong, such as the window pointer being invalid (as can happen when the
1784 * user changes his DISPLAY, but not his WINDOWID) -- webb
1785 */
1786 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001787test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 int (*old_handler)();
1790 XTextProperty text_prop;
1791
1792 old_handler = XSetErrorHandler(x_error_check);
1793 got_x_error = FALSE;
1794 if (XGetWMName(dpy, x11_window, &text_prop))
1795 XFree((void *)text_prop.value);
1796 XSync(dpy, False);
1797 (void)XSetErrorHandler(old_handler);
1798
1799 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001800 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 return (got_x_error ? FAIL : OK);
1803}
1804#endif
1805
1806#ifdef FEAT_TITLE
1807
1808#ifdef FEAT_X11
1809
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001810static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812/*
1813 * try to get x11 window and display
1814 *
1815 * return FAIL for failure, OK otherwise
1816 */
1817 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001818get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
1820 char *winid;
1821 static int result = -1;
1822#define XD_NONE 0 /* x11_display not set here */
1823#define XD_HERE 1 /* x11_display opened here */
1824#define XD_GUI 2 /* x11_display used from gui.dpy */
1825#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1826 static int x11_display_from = XD_NONE;
1827 static int did_set_error_handler = FALSE;
1828
1829 if (!did_set_error_handler)
1830 {
1831 /* X just exits if it finds an error otherwise! */
1832 (void)XSetErrorHandler(x_error_handler);
1833 did_set_error_handler = TRUE;
1834 }
1835
Bram Moolenaar9372a112005-12-06 19:59:18 +00001836#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (gui.in_use)
1838 {
1839 /*
1840 * If the X11 display was opened here before, for the window where Vim
1841 * was started, close that one now to avoid a memory leak.
1842 */
1843 if (x11_display_from == XD_HERE && x11_display != NULL)
1844 {
1845 XCloseDisplay(x11_display);
1846 x11_display_from = XD_NONE;
1847 }
1848 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1849 {
1850 x11_display_from = XD_GUI;
1851 return OK;
1852 }
1853 x11_display = NULL;
1854 return FAIL;
1855 }
1856 else if (x11_display_from == XD_GUI)
1857 {
1858 /* GUI must have stopped somehow, clear x11_display */
1859 x11_window = 0;
1860 x11_display = NULL;
1861 x11_display_from = XD_NONE;
1862 }
1863#endif
1864
1865 /* When started with the "-X" argument, don't try connecting. */
1866 if (!x_connect_to_server())
1867 return FAIL;
1868
1869 /*
1870 * If WINDOWID not set, should try another method to find out
1871 * what the current window number is. The only code I know for
1872 * this is very complicated.
1873 * We assume that zero is invalid for WINDOWID.
1874 */
1875 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1876 x11_window = (Window)atol(winid);
1877
1878#ifdef FEAT_XCLIPBOARD
1879 if (xterm_dpy != NULL && x11_window != 0)
1880 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001881 /* We may have checked it already, but Gnome terminal can move us to
1882 * another window, so we need to check every time. */
1883 if (x11_display_from != XD_XTERM)
1884 {
1885 /*
1886 * If the X11 display was opened here before, for the window where
1887 * Vim was started, close that one now to avoid a memory leak.
1888 */
1889 if (x11_display_from == XD_HERE && x11_display != NULL)
1890 XCloseDisplay(x11_display);
1891 x11_display = xterm_dpy;
1892 x11_display_from = XD_XTERM;
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (test_x11_window(x11_display) == FAIL)
1895 {
1896 /* probably bad $WINDOWID */
1897 x11_window = 0;
1898 x11_display = NULL;
1899 x11_display_from = XD_NONE;
1900 return FAIL;
1901 }
1902 return OK;
1903 }
1904#endif
1905
1906 if (x11_window == 0 || x11_display == NULL)
1907 result = -1;
1908
1909 if (result != -1) /* Have already been here and set this */
1910 return result; /* Don't do all these X calls again */
1911
1912 if (x11_window != 0 && x11_display == NULL)
1913 {
1914#ifdef SET_SIG_ALARM
1915 RETSIGTYPE (*sig_save)();
1916#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001917#ifdef ELAPSED_FUNC
1918 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001921 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922#endif
1923
1924#ifdef SET_SIG_ALARM
1925 /*
1926 * Opening the Display may hang if the DISPLAY setting is wrong, or
1927 * the network connection is bad. Set an alarm timer to get out.
1928 */
1929 sig_alarm_called = FALSE;
1930 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1931 (RETSIGTYPE (*)())sig_alarm);
1932 alarm(2);
1933#endif
1934 x11_display = XOpenDisplay(NULL);
1935
1936#ifdef SET_SIG_ALARM
1937 alarm(0);
1938 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1939 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001940 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#endif
1942 if (x11_display != NULL)
1943 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001944# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001946 {
1947 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001948 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001949 verbose_leave();
1950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951# endif
1952 if (test_x11_window(x11_display) == FAIL)
1953 {
1954 /* Maybe window id is bad */
1955 x11_window = 0;
1956 XCloseDisplay(x11_display);
1957 x11_display = NULL;
1958 }
1959 else
1960 x11_display_from = XD_HERE;
1961 }
1962 }
1963 if (x11_window == 0 || x11_display == NULL)
1964 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001965
1966# ifdef FEAT_EVAL
1967 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1968# endif
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 return (result = OK);
1971}
1972
1973/*
1974 * Determine original x11 Window Title
1975 */
1976 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001977get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001979 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980}
1981
1982/*
1983 * Determine original x11 Window icon
1984 */
1985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001986get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 int retval = FALSE;
1989
1990 retval = get_x11_thing(FALSE, test_only);
1991
1992 /* could not get old icon, use terminal name */
1993 if (oldicon == NULL && !test_only)
1994 {
1995 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001996 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001998 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000
2001 return retval;
2002}
2003
2004 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002005get_x11_thing(
2006 int get_title, /* get title string */
2007 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 XTextProperty text_prop;
2010 int retval = FALSE;
2011 Status status;
2012
2013 if (get_x11_windis() == OK)
2014 {
2015 /* Get window/icon name if any */
2016 if (get_title)
2017 status = XGetWMName(x11_display, x11_window, &text_prop);
2018 else
2019 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2020
2021 /*
2022 * If terminal is xterm, then x11_window may be a child window of the
2023 * outer xterm window that actually contains the window/icon name, so
2024 * keep traversing up the tree until a window with a title/icon is
2025 * found.
2026 */
2027 /* Previously this was only done for xterm and alikes. I don't see a
2028 * reason why it would fail for other terminal emulators.
2029 * if (term_is_xterm) */
2030 {
2031 Window root;
2032 Window parent;
2033 Window win = x11_window;
2034 Window *children;
2035 unsigned int num_children;
2036
2037 while (!status || text_prop.value == NULL)
2038 {
2039 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2040 &num_children))
2041 break;
2042 if (children)
2043 XFree((void *)children);
2044 if (parent == root || parent == 0)
2045 break;
2046
2047 win = parent;
2048 if (get_title)
2049 status = XGetWMName(x11_display, win, &text_prop);
2050 else
2051 status = XGetWMIconName(x11_display, win, &text_prop);
2052 }
2053 }
2054 if (status && text_prop.value != NULL)
2055 {
2056 retval = TRUE;
2057 if (!test_only)
2058 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01002059 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (get_title)
2062 oldtitle = vim_strsave((char_u *)text_prop.value);
2063 else
2064 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066 else
2067 {
2068 char **cl;
2069 Status transform_status;
2070 int n = 0;
2071
2072 transform_status = XmbTextPropertyToTextList(x11_display,
2073 &text_prop,
2074 &cl, &n);
2075 if (transform_status >= Success && n > 0 && cl[0])
2076 {
2077 if (get_title)
2078 oldtitle = vim_strsave((char_u *) cl[0]);
2079 else
2080 oldicon = vim_strsave((char_u *) cl[0]);
2081 XFreeStringList(cl);
2082 }
2083 else
2084 {
2085 if (get_title)
2086 oldtitle = vim_strsave((char_u *)text_prop.value);
2087 else
2088 oldicon = vim_strsave((char_u *)text_prop.value);
2089 }
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092 XFree((void *)text_prop.value);
2093 }
2094 }
2095 return retval;
2096}
2097
Bram Moolenaarb2148f52019-01-20 23:43:57 +01002098/* Xutf8 functions are not available on older systems. Note that on some
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002099 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2100 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2101 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01002102#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002103# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104# define USE_UTF8_STRING
2105# endif
2106#endif
2107
2108/*
2109 * Set x11 Window Title
2110 *
2111 * get_x11_windis() must be called before this and have returned OK
2112 */
2113 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002114set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2117 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2118 * supported everywhere and STRING doesn't work for multi-byte titles.
2119 */
2120#ifdef USE_UTF8_STRING
2121 if (enc_utf8)
2122 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2123 NULL, NULL, 0, NULL, NULL, NULL);
2124 else
2125#endif
2126 {
2127#if XtSpecificationRelease >= 4
2128# ifdef FEAT_XFONTSET
2129 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2130 NULL, NULL, 0, NULL, NULL, NULL);
2131# else
2132 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002133 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002136 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 XSetWMProperties(x11_display, x11_window, &text_prop,
2138 NULL, NULL, 0, NULL, NULL, NULL);
2139# endif
2140#else
2141 XStoreName(x11_display, x11_window, (char *)title);
2142#endif
2143 }
2144 XFlush(x11_display);
2145}
2146
2147/*
2148 * Set x11 Window icon
2149 *
2150 * get_x11_windis() must be called before this and have returned OK
2151 */
2152 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002153set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 /* See above for comments about using X*SetWMProperties(). */
2156#ifdef USE_UTF8_STRING
2157 if (enc_utf8)
2158 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2159 NULL, 0, NULL, NULL, NULL);
2160 else
2161#endif
2162 {
2163#if XtSpecificationRelease >= 4
2164# ifdef FEAT_XFONTSET
2165 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2166 NULL, 0, NULL, NULL, NULL);
2167# else
2168 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002169 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002171 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2173 NULL, 0, NULL, NULL, NULL);
2174# endif
2175#else
2176 XSetIconName(x11_display, x11_window, (char *)icon);
2177#endif
2178 }
2179 XFlush(x11_display);
2180}
2181
2182#else /* FEAT_X11 */
2183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002185get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 return FALSE;
2188}
2189
2190 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002191get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 if (!test_only)
2194 {
2195 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002196 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002198 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200 return FALSE;
2201}
2202
2203#endif /* FEAT_X11 */
2204
2205 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002206mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208 return get_x11_title(TRUE);
2209}
2210
2211 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002212mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
2214 return get_x11_icon(TRUE);
2215}
2216
2217/*
2218 * Set the window title and icon.
2219 */
2220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002221mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 int type = 0;
2224 static int recursive = 0;
2225
2226 if (T_NAME == NULL) /* no terminal name (yet) */
2227 return;
2228 if (title == NULL && icon == NULL) /* nothing to do */
2229 return;
2230
2231 /* When one of the X11 functions causes a deadly signal, we get here again
2232 * recursively. Avoid hanging then (something is probably locked). */
2233 if (recursive)
2234 return;
2235 ++recursive;
2236
2237 /*
2238 * if the window ID and the display is known, we may use X11 calls
2239 */
2240#ifdef FEAT_X11
2241 if (get_x11_windis() == OK)
2242 type = 1;
2243#else
2244# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2245 if (gui.in_use)
2246 type = 1;
2247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#endif
2249
2250 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002251 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * than x11 calls, because the x11 calls don't always work
2253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if ((type || *T_TS != NUL) && title != NULL)
2255 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002256 if (oldtitle_outdated)
2257 {
2258 oldtitle_outdated = FALSE;
2259 VIM_CLEAR(oldtitle);
2260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (oldtitle == NULL
2262#ifdef FEAT_GUI
2263 && !gui.in_use
2264#endif
2265 ) /* first call but not in GUI, save title */
2266 (void)get_x11_title(FALSE);
2267
2268 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2269 term_settitle(title);
2270#ifdef FEAT_X11
2271 else
2272# ifdef FEAT_GUI_GTK
2273 if (!gui.in_use) /* don't do this if GTK+ is running */
2274# endif
2275 set_x11_title(title); /* x11 */
2276#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002277#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2279 else
2280 gui_mch_settitle(title, icon);
2281#endif
2282 did_set_title = TRUE;
2283 }
2284
2285 if ((type || *T_CIS != NUL) && icon != NULL)
2286 {
2287 if (oldicon == NULL
2288#ifdef FEAT_GUI
2289 && !gui.in_use
2290#endif
2291 ) /* first call, save icon */
2292 get_x11_icon(FALSE);
2293
2294 if (*T_CIS != NUL)
2295 {
2296 out_str(T_CIS); /* set icon start */
2297 out_str_nf(icon);
2298 out_str(T_CIE); /* set icon end */
2299 out_flush();
2300 }
2301#ifdef FEAT_X11
2302 else
2303# ifdef FEAT_GUI_GTK
2304 if (!gui.in_use) /* don't do this if GTK+ is running */
2305# endif
2306 set_x11_icon(icon); /* x11 */
2307#endif
2308 did_set_icon = TRUE;
2309 }
2310 --recursive;
2311}
2312
2313/*
2314 * Restore the window/icon title.
2315 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002316 * SAVE_RESTORE_TITLE only restore title
2317 * SAVE_RESTORE_ICON only restore icon
2318 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
2320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002321mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002324 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002326 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2327
2328 // pop and push from/to the stack
2329 term_pop_title(which);
2330 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331}
2332
2333#endif /* FEAT_TITLE */
2334
2335/*
2336 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002337 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 */
2339 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002340vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342 if (name == NULL)
2343 return FALSE;
2344 return (STRNICMP(name, "xterm", 5) == 0
2345 || STRNICMP(name, "nxterm", 6) == 0
2346 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002347 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002349 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 || STRCMP(name, "builtin_xterm") == 0);
2351}
2352
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002353#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2354/*
2355 * Return TRUE if "name" appears to be that of a terminal
2356 * known to support the xterm-style mouse protocol.
2357 * Relies on term_is_xterm having been set to its correct value.
2358 */
2359 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002360use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002361{
2362 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002363 && (term_is_xterm
2364 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002365 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002366 || STRICMP(name, "st") == 0
2367 || STRNICMP(name, "st-", 3) == 0
2368 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002369}
2370#endif
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2373/*
2374 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2375 * Return 1 for "xterm".
2376 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002377 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002378 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 */
2380 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002381use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002383 if (ttym_flags == TTYM_SGR)
2384 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002385 if (ttym_flags == TTYM_URXVT)
2386 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (ttym_flags == TTYM_XTERM2)
2388 return 2;
2389 if (ttym_flags == TTYM_XTERM)
2390 return 1;
2391 return 0;
2392}
2393#endif
2394
2395 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002396vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 if (name == NULL)
2399 return FALSE;
2400 return (STRNICMP(name, "iris-ansi", 9) == 0
2401 || STRCMP(name, "builtin_iris-ansi") == 0);
2402}
2403
2404 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002405vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 if (name == NULL)
2408 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002409 /* catch VT100 - VT5xx */
2410 return ((STRNICMP(name, "vt", 2) == 0
2411 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 || STRCMP(name, "builtin_vt320") == 0);
2413}
2414
2415/*
2416 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2417 * This should include all windowed terminal emulators.
2418 */
2419 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002420vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 if (name == NULL)
2423 return FALSE;
2424 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2425 return TRUE;
2426 return ( STRNICMP(name, "hpterm", 6) == 0
2427 || STRNICMP(name, "sun-cmd", 7) == 0
2428 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002429 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 || STRNICMP(name, "dtterm", 6) == 0);
2431}
2432
2433/*
2434 * Insert user name in s[len].
2435 * Return OK if a name found.
2436 */
2437 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002438mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002441 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 return OK;
2443#else
2444 return mch_get_uname(getuid(), s, len);
2445#endif
2446}
2447
2448/*
2449 * Insert user name for "uid" in s[len].
2450 * Return OK if a name found.
2451 */
2452 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002453mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2456 struct passwd *pw;
2457
2458 if ((pw = getpwuid(uid)) != NULL
2459 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2460 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002461 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 return OK;
2463 }
2464#endif
2465 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2466 return FAIL; /* a number is not a name */
2467}
2468
2469/*
2470 * Insert host name is s[len].
2471 */
2472
2473#ifdef HAVE_SYS_UTSNAME_H
2474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002475mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 struct utsname vutsname;
2478
2479 if (uname(&vutsname) < 0)
2480 *s = NUL;
2481 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002482 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483}
2484#else /* HAVE_SYS_UTSNAME_H */
2485
2486# ifdef HAVE_SYS_SYSTEMINFO_H
2487# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2488# endif
2489
2490 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002491mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493# ifdef VAXC
2494 vaxc$gethostname((char *)s, len);
2495# else
2496 gethostname((char *)s, len);
2497# endif
2498 s[len - 1] = NUL; /* make sure it's terminated */
2499}
2500#endif /* HAVE_SYS_UTSNAME_H */
2501
2502/*
2503 * return process ID
2504 */
2505 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002506mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 return (long)getpid();
2509}
2510
2511#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002513strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 extern int sys_nerr;
2516 extern char *sys_errlist[];
2517 static char er[20];
2518
2519 if (err > 0 && err < sys_nerr)
2520 return (sys_errlist[err]);
2521 sprintf(er, "Error %d", err);
2522 return er;
2523}
2524#endif
2525
2526/*
2527 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2528 * Return OK for success, FAIL for failure.
2529 */
2530 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002531mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533#if defined(USE_GETCWD)
2534 if (getcwd((char *)buf, len) == NULL)
2535 {
2536 STRCPY(buf, strerror(errno));
2537 return FAIL;
2538 }
2539 return OK;
2540#else
2541 return (getwd((char *)buf) != NULL ? OK : FAIL);
2542#endif
2543}
2544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002546 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 *
2548 * return FAIL for failure, OK for success
2549 */
2550 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002551mch_FullName(
2552 char_u *fname,
2553 char_u *buf,
2554 int len,
2555 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
2557 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002558#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 int fd = -1;
2560 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 char_u olddir[MAXPATHL];
2563 char_u *p;
2564 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002565#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002566 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2567 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002568#endif
2569
Bram Moolenaar38323e42007-03-06 19:22:53 +00002570#ifdef VMS
2571 fname = vms_fixfilename(fname);
2572#endif
2573
Bram Moolenaara2442432007-04-26 14:26:37 +00002574#ifdef __CYGWIN__
2575 /*
2576 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2577 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002578# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002579 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2580 * a forward slash. */
2581 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2582 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002583# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002584 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002585# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002586 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002587#endif
2588
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002589 /* Expand it if forced or not an absolute path.
2590 * Do not do it for "/file", the result is always "/". */
2591 if ((force || !mch_isFullName(fname))
2592 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 /*
2595 * If the file name has a path, change to that directory for a moment,
2596 * and then do the getwd() (and get back to where we were).
2597 * This will get the correct path name with "../" things.
2598 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002599 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002601#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 /*
2603 * Use fchdir() if possible, it's said to be faster and more
2604 * reliable. But on SunOS 4 it might not work. Check this by
2605 * doing a fchdir() right now.
2606 */
2607 if (!dont_fchdir)
2608 {
2609 fd = open(".", O_RDONLY | O_EXTRA, 0);
2610 if (fd >= 0 && fchdir(fd) < 0)
2611 {
2612 close(fd);
2613 fd = -1;
2614 dont_fchdir = TRUE; /* don't try again */
2615 }
2616 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /* Only change directory when we are sure we can return to where
2620 * we are now. After doing "su" chdir(".") might not work. */
2621 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002622#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 (mch_dirname(olddir, MAXPATHL) == FAIL
2626 || mch_chdir((char *)olddir) != 0))
2627 {
2628 p = NULL; /* can't get current dir: don't chdir */
2629 retval = FAIL;
2630 }
2631 else
2632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /* The directory is copied into buf[], to be able to remove
2634 * the file name without changing it (could be a string in
2635 * read-only memory) */
2636 if (p - fname >= len)
2637 retval = FAIL;
2638 else
2639 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002640 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (mch_chdir((char *)buf))
2642 retval = FAIL;
2643 else
2644 fname = p + 1;
2645 *buf = NUL;
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 }
2649 if (mch_dirname(buf, len) == FAIL)
2650 {
2651 retval = FAIL;
2652 *buf = NUL;
2653 }
2654 if (p != NULL)
2655 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002656#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (fd >= 0)
2658 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002659 if (p_verbose >= 5)
2660 {
2661 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002662 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002663 verbose_leave();
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 l = fchdir(fd);
2666 close(fd);
2667 }
2668 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 l = mch_chdir((char *)olddir);
2671 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002672 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002676 if (l >= len - 1)
2677 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002678#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002679 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002681 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002686 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return FAIL;
2688
2689 /* Do not append ".", "/dir/." is equal to "/dir". */
2690 if (STRCMP(fname, ".") != 0)
2691 STRCAT(buf, fname);
2692
2693 return OK;
2694}
2695
2696/*
2697 * Return TRUE if "fname" does not depend on the current directory.
2698 */
2699 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002700mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002702#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return ( fname[0] == '/' || fname[0] == '.' ||
2704 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2705 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2706 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002707#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#endif
2710}
2711
Bram Moolenaar24552be2005-12-10 20:17:30 +00002712#if defined(USE_FNAME_CASE) || defined(PROTO)
2713/*
2714 * Set the case of the file name, if it already exists. This will cause the
2715 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002716 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002717 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002718 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002719fname_case(
2720 char_u *name,
2721 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002722{
2723 struct stat st;
2724 char_u *slash, *tail;
2725 DIR *dirp;
2726 struct dirent *dp;
2727
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002728 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002729 {
2730 /* Open the directory where the file is located. */
2731 slash = vim_strrchr(name, '/');
2732 if (slash == NULL)
2733 {
2734 dirp = opendir(".");
2735 tail = name;
2736 }
2737 else
2738 {
2739 *slash = NUL;
2740 dirp = opendir((char *)name);
2741 *slash = '/';
2742 tail = slash + 1;
2743 }
2744
2745 if (dirp != NULL)
2746 {
2747 while ((dp = readdir(dirp)) != NULL)
2748 {
2749 /* Only accept names that differ in case and are the same byte
2750 * length. TODO: accept different length name. */
2751 if (STRICMP(tail, dp->d_name) == 0
2752 && STRLEN(tail) == STRLEN(dp->d_name))
2753 {
2754 char_u newname[MAXPATHL + 1];
2755 struct stat st2;
2756
2757 /* Verify the inode is equal. */
2758 vim_strncpy(newname, name, MAXPATHL);
2759 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2760 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002761 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002762 && st.st_ino == st2.st_ino
2763 && st.st_dev == st2.st_dev)
2764 {
2765 STRCPY(tail, dp->d_name);
2766 break;
2767 }
2768 }
2769 }
2770
2771 closedir(dirp);
2772 }
2773 }
2774}
2775#endif
2776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777/*
2778 * Get file permissions for 'name'.
2779 * Returns -1 when it doesn't exist.
2780 */
2781 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002782mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 struct stat statb;
2785
2786 /* Keep the #ifdef outside of stat(), it may be a macro. */
2787#ifdef VMS
2788 if (stat((char *)vms_fixfilename(name), &statb))
2789#else
2790 if (stat((char *)name, &statb))
2791#endif
2792 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002793#ifdef __INTERIX
2794 /* The top bit makes the value negative, which means the file doesn't
2795 * exist. Remove the bit, we don't use it. */
2796 return statb.st_mode & ~S_ADDACE;
2797#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800}
2801
2802/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002803 * Set file permission for "name" to "perm".
2804 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 */
2806 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002807mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 return (chmod((char *)
2810#ifdef VMS
2811 vms_fixfilename(name),
2812#else
2813 name,
2814#endif
2815 (mode_t)perm) == 0 ? OK : FAIL);
2816}
2817
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002818#if defined(HAVE_FCHMOD) || defined(PROTO)
2819/*
2820 * Set file permission for open file "fd" to "perm".
2821 * Return FAIL for failure, OK otherwise.
2822 */
2823 int
2824mch_fsetperm(int fd, long perm)
2825{
2826 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2827}
2828#endif
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#if defined(HAVE_ACL) || defined(PROTO)
2831# ifdef HAVE_SYS_ACL_H
2832# include <sys/acl.h>
2833# endif
2834# ifdef HAVE_SYS_ACCESS_H
2835# include <sys/access.h>
2836# endif
2837
2838# ifdef HAVE_SOLARIS_ACL
2839typedef struct vim_acl_solaris_T {
2840 int acl_cnt;
2841 aclent_t *acl_entry;
2842} vim_acl_solaris_T;
2843# endif
2844
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002845#if defined(HAVE_SELINUX) || defined(PROTO)
2846/*
2847 * Copy security info from "from_file" to "to_file".
2848 */
2849 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002850mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002851{
2852 if (from_file == NULL)
2853 return;
2854
2855 if (selinux_enabled == -1)
2856 selinux_enabled = is_selinux_enabled();
2857
2858 if (selinux_enabled > 0)
2859 {
2860 security_context_t from_context = NULL;
2861 security_context_t to_context = NULL;
2862
2863 if (getfilecon((char *)from_file, &from_context) < 0)
2864 {
2865 /* If the filesystem doesn't support extended attributes,
2866 the original had no special security context and the
2867 target cannot have one either. */
2868 if (errno == EOPNOTSUPP)
2869 return;
2870
Bram Moolenaar32526b32019-01-19 17:43:09 +01002871 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002872 msg_outtrans(from_file);
2873 msg_putchar('\n');
2874 return;
2875 }
2876 if (getfilecon((char *)to_file, &to_context) < 0)
2877 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002878 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002879 msg_outtrans(to_file);
2880 msg_putchar('\n');
2881 freecon (from_context);
2882 return ;
2883 }
2884 if (strcmp(from_context, to_context) != 0)
2885 {
2886 if (setfilecon((char *)to_file, from_context) < 0)
2887 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002888 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002889 msg_outtrans(to_file);
2890 msg_putchar('\n');
2891 }
2892 }
2893 freecon(to_context);
2894 freecon(from_context);
2895 }
2896}
2897#endif /* HAVE_SELINUX */
2898
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002899#if defined(HAVE_SMACK) && !defined(PROTO)
2900/*
2901 * Copy security info from "from_file" to "to_file".
2902 */
2903 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002904mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002905{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002906 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002907 {
2908 XATTR_NAME_SMACK,
2909 XATTR_NAME_SMACKEXEC,
2910 XATTR_NAME_SMACKMMAP
2911 };
2912
2913 char buffer[SMACK_LABEL_LEN];
2914 const char *name;
2915 int index;
2916 int ret;
2917 ssize_t size;
2918
2919 if (from_file == NULL)
2920 return;
2921
2922 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2923 / sizeof(smack_copied_attributes)[0]) ; index++)
2924 {
2925 /* get the name of the attribute to copy */
2926 name = smack_copied_attributes[index];
2927
2928 /* get the value of the attribute in buffer */
2929 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2930 if (size >= 0)
2931 {
2932 /* copy the attribute value of buffer */
2933 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2934 if (ret < 0)
2935 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002936 vim_snprintf((char *)IObuff, IOSIZE,
2937 _("Could not set security context %s for %s"),
2938 name, to_file);
2939 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002940 msg_putchar('\n');
2941 }
2942 }
2943 else
2944 {
2945 /* what reason of not having the attribute value? */
2946 switch (errno)
2947 {
2948 case ENOTSUP:
2949 /* extended attributes aren't supported or enabled */
2950 /* should a message be echoed? not sure... */
Bram Moolenaarb2148f52019-01-20 23:43:57 +01002951 return; /* leave because it isn't useful to continue */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002952
2953 case ERANGE:
2954 default:
2955 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002956 vim_snprintf((char *)IObuff, IOSIZE,
2957 _("Could not get security context %s for %s. Removing it!"),
2958 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002959 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002960 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002961 /* FALLTHROUGH to remove the attribute */
2962
2963 case ENODATA:
2964 /* no attribute of this name */
2965 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002966 /* Silently ignore errors, apparently this happens when
2967 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002968 break;
2969 }
2970 }
2971 }
2972}
2973#endif /* HAVE_SMACK */
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975/*
2976 * Return a pointer to the ACL of file "fname" in allocated memory.
2977 * Return NULL if the ACL is not available for whatever reason.
2978 */
2979 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002980mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 vim_acl_T ret = NULL;
2983#ifdef HAVE_POSIX_ACL
2984 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2985#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002986#ifdef HAVE_SOLARIS_ZFS_ACL
2987 acl_t *aclent;
2988
2989 if (acl_get((char *)fname, 0, &aclent) < 0)
2990 return NULL;
2991 ret = (vim_acl_T)aclent;
2992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993#ifdef HAVE_SOLARIS_ACL
2994 vim_acl_solaris_T *aclent;
2995
2996 aclent = malloc(sizeof(vim_acl_solaris_T));
2997 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2998 {
2999 free(aclent);
3000 return NULL;
3001 }
3002 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3003 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3004 {
3005 free(aclent->acl_entry);
3006 free(aclent);
3007 return NULL;
3008 }
3009 ret = (vim_acl_T)aclent;
3010#else
3011#if defined(HAVE_AIX_ACL)
3012 int aclsize;
3013 struct acl *aclent;
3014
3015 aclsize = sizeof(struct acl);
3016 aclent = malloc(aclsize);
3017 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3018 {
3019 if (errno == ENOSPC)
3020 {
3021 aclsize = aclent->acl_len;
3022 aclent = realloc(aclent, aclsize);
3023 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3024 {
3025 free(aclent);
3026 return NULL;
3027 }
3028 }
3029 else
3030 {
3031 free(aclent);
3032 return NULL;
3033 }
3034 }
3035 ret = (vim_acl_T)aclent;
3036#endif /* HAVE_AIX_ACL */
3037#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003038#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#endif /* HAVE_POSIX_ACL */
3040 return ret;
3041}
3042
3043/*
3044 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3045 */
3046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003047mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 if (aclent == NULL)
3050 return;
3051#ifdef HAVE_POSIX_ACL
3052 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3053#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003054#ifdef HAVE_SOLARIS_ZFS_ACL
3055 acl_set((char *)fname, (acl_t *)aclent);
3056#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057#ifdef HAVE_SOLARIS_ACL
3058 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3059 ((vim_acl_solaris_T *)aclent)->acl_entry);
3060#else
3061#ifdef HAVE_AIX_ACL
3062 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3063#endif /* HAVE_AIX_ACL */
3064#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003065#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#endif /* HAVE_POSIX_ACL */
3067}
3068
3069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003070mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 if (aclent == NULL)
3073 return;
3074#ifdef HAVE_POSIX_ACL
3075 acl_free((acl_t)aclent);
3076#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003077#ifdef HAVE_SOLARIS_ZFS_ACL
3078 acl_free((acl_t *)aclent);
3079#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#ifdef HAVE_SOLARIS_ACL
3081 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3082 free(aclent);
3083#else
3084#ifdef HAVE_AIX_ACL
3085 free(aclent);
3086#endif /* HAVE_AIX_ACL */
3087#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003088#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089#endif /* HAVE_POSIX_ACL */
3090}
3091#endif
3092
3093/*
3094 * Set hidden flag for "name".
3095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003097mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 /* can't hide a file */
3100}
3101
3102/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003103 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * return FALSE if "name" is not a directory
3105 * return FALSE for error
3106 */
3107 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003108mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 struct stat statb;
3111
3112 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3113 return FALSE;
3114 if (stat((char *)name, &statb))
3115 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117}
3118
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003119/*
3120 * return TRUE if "name" is a directory, NOT a symlink to a directory
3121 * return FALSE if "name" is not a directory
3122 * return FALSE for error
3123 */
3124 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003125mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003126{
3127 struct stat statb;
3128
3129 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3130 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003131 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003132 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003133 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003134}
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136/*
3137 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3138 */
3139 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003140executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 struct stat st;
3143
3144 if (stat((char *)name, &st))
3145 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003146#ifdef VMS
3147 /* Like on Unix system file can have executable rights but not necessarily
3148 * be an executable, but on Unix is not a default for an ordianry file to
3149 * have an executable flag - on VMS it is in most cases.
3150 * Therefore, this check does not have any sense - let keep us to the
3151 * conventions instead:
3152 * *.COM and *.EXE files are the executables - the rest are not. This is
3153 * not ideal but better then it was.
3154 */
3155 int vms_executable = 0;
3156 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3157 {
3158 if (strstr(vms_tolower((char*)name),".exe") != NULL
3159 || strstr(vms_tolower((char*)name),".com")!= NULL)
3160 vms_executable = 1;
3161 }
3162 return vms_executable;
3163#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166}
3167
3168/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003169 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003170 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 * Return -1 if unknown.
3172 */
3173 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003174mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
3176 char_u *buf;
3177 char_u *p, *e;
3178 int retval;
3179
Bram Moolenaarb5971142015-03-21 17:32:19 +01003180 /* When "use_path" is false and if it's an absolute or relative path don't
3181 * need to use $PATH. */
3182 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3183 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003184 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003185 /* There must be a path separator, files in the current directory
3186 * can't be executed. */
3187 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003188 {
3189 if (path != NULL)
3190 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003191 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003192 *path = FullName_save(name, TRUE);
3193 else
3194 *path = vim_strsave(name);
3195 }
3196 return TRUE;
3197 }
3198 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 p = (char_u *)getenv("PATH");
3202 if (p == NULL || *p == NUL)
3203 return -1;
3204 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3205 if (buf == NULL)
3206 return -1;
3207
3208 /*
3209 * Walk through all entries in $PATH to check if "name" exists there and
3210 * is an executable file.
3211 */
3212 for (;;)
3213 {
3214 e = (char_u *)strchr((char *)p, ':');
3215 if (e == NULL)
3216 e = p + STRLEN(p);
3217 if (e - p <= 1) /* empty entry means current dir */
3218 STRCPY(buf, "./");
3219 else
3220 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003221 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 add_pathsep(buf);
3223 }
3224 STRCAT(buf, name);
3225 retval = executable_file(buf);
3226 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003227 {
3228 if (path != NULL)
3229 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003230 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003231 *path = FullName_save(buf, TRUE);
3232 else
3233 *path = vim_strsave(buf);
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 if (*e != ':')
3239 break;
3240 p = e + 1;
3241 }
3242
3243 vim_free(buf);
3244 return retval;
3245}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
3247/*
3248 * Check what "name" is:
3249 * NODE_NORMAL: file or directory (or doesn't exist)
3250 * NODE_WRITABLE: writable device, socket, fifo, etc.
3251 * NODE_OTHER: non-writable things
3252 */
3253 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003254mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 struct stat st;
3257
3258 if (stat((char *)name, &st))
3259 return NODE_NORMAL;
3260 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3261 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3263 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 /* Everything else is writable? */
3265 return NODE_WRITABLE;
3266}
3267
3268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003269mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271#ifdef HAVE_CHECK_STACK_GROWTH
3272 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 check_stack_growth((char *)&i);
3275
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003276# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 get_stack_limit();
3278# endif
3279
3280#endif
3281
3282 /*
3283 * Setup an alternative stack for signals. Helps to catch signals when
3284 * running out of stack space.
3285 * Use of sigaltstack() is preferred, it's more portable.
3286 * Ignore any errors.
3287 */
3288#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003289 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 init_signal_stack();
3291#endif
3292}
3293
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003294#if defined(EXITFREE) || defined(PROTO)
3295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003296mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003297{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3299 if (clip_star.owned)
3300 clip_lose_selection(&clip_star);
3301 if (clip_plus.owned)
3302 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003303# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003304# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003305 if (xterm_Shell != (Widget)0)
3306 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003307# ifndef LESSTIF_VERSION
3308 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003309 if (xterm_dpy != NULL)
3310 XtCloseDisplay(xterm_dpy);
3311 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003312 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003313 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003314# ifdef FEAT_X11
3315 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3316# endif
3317 }
3318# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003319# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003320# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003321 if (x11_display != NULL
3322# ifdef FEAT_XCLIPBOARD
3323 && x11_display != xterm_dpy
3324# endif
3325 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 XCloseDisplay(x11_display);
3327# endif
3328# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003329 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003330# endif
3331# ifdef FEAT_TITLE
3332 vim_free(oldtitle);
3333 vim_free(oldicon);
3334# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003335}
3336#endif
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * Output a newline when exiting.
3340 * Make sure the newline goes to the same stream as the text.
3341 */
3342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003343exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003345 if (silent_mode)
3346 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (newline_on_exit || msg_didout)
3348 {
3349 if (msg_use_printf())
3350 {
3351 if (info_message)
3352 mch_msg("\n");
3353 else
3354 mch_errmsg("\r\n");
3355 }
3356 else
3357 out_char('\n');
3358 }
3359 else
3360 {
3361 restore_cterm_colors(); /* get original colors back */
3362 msg_clr_eos_force(); /* clear the rest of the display */
3363 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3364 }
3365}
3366
3367 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003368mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 exiting = TRUE;
3371
3372#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3373 x11_export_final_selection();
3374#endif
3375
3376#ifdef FEAT_GUI
3377 if (!gui.in_use)
3378#endif
3379 {
3380 settmode(TMODE_COOK);
3381#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003382 // restore xterm title and icon name
3383 mch_restore_title(SAVE_RESTORE_BOTH);
3384 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#endif
3386 /*
3387 * When t_ti is not empty but it doesn't cause swapping terminal
3388 * pages, need to output a newline when msg_didout is set. But when
3389 * t_ti does swap pages it should not go to the shell page. Do this
3390 * before stoptermcap().
3391 */
3392 if (swapping_screen() && !newline_on_exit)
3393 exit_scroll();
3394
3395 /* Stop termcap: May need to check for T_CRV response, which
3396 * requires RAW mode. */
3397 stoptermcap();
3398
3399 /*
3400 * A newline is only required after a message in the alternate screen.
3401 * This is set to TRUE by wait_return().
3402 */
3403 if (!swapping_screen() || newline_on_exit)
3404 exit_scroll();
3405
3406 /* Cursor may have been switched off without calling starttermcap()
3407 * when doing "vim -u vimrc" and vimrc contains ":q". */
3408 if (full_screen)
3409 cursor_on();
3410 }
3411 out_flush();
3412 ml_close_all(TRUE); /* remove all memfiles */
3413 may_core_dump();
3414#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 gui_exit(r);
3417#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003418
Bram Moolenaar56718732006-03-15 22:53:57 +00003419#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003420 mac_conv_cleanup();
3421#endif
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#ifdef __QNX__
3424 /* A core dump won't be created if the signal handler
3425 * doesn't return, so we can't call exit() */
3426 if (deadly_signal != 0)
3427 return;
3428#endif
3429
Bram Moolenaar009b2592004-10-24 19:18:58 +00003430#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003431 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003432#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003433
3434#ifdef EXITFREE
3435 free_all_mem();
3436#endif
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 exit(r);
3439}
3440
3441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003442may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 if (deadly_signal != 0)
3445 {
3446 signal(deadly_signal, SIG_DFL);
3447 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3448 }
3449}
3450
3451#ifndef VMS
3452
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003453/*
3454 * Get the file descriptor to use for tty operations.
3455 */
3456 static int
3457get_tty_fd(int fd)
3458{
3459 int tty_fd = fd;
3460
3461#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3462 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3463 // "fd" when it is a master device.
3464 if (mch_isatty(fd) > 1)
3465 {
3466 char *name;
3467
3468 name = ptsname(fd);
3469 if (name == NULL)
3470 return -1;
3471
3472 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3473 if (tty_fd < 0)
3474 return -1;
3475 }
3476#endif
3477 return tty_fd;
3478}
3479
3480 static int
3481mch_tcgetattr(int fd, void *term)
3482{
3483 int tty_fd;
3484 int retval = -1;
3485
3486 tty_fd = get_tty_fd(fd);
3487 if (tty_fd >= 0)
3488 {
3489#ifdef NEW_TTY_SYSTEM
3490# ifdef HAVE_TERMIOS_H
3491 retval = tcgetattr(tty_fd, (struct termios *)term);
3492# else
3493 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3494# endif
3495#else
3496 // for "old" tty systems
3497 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3498#endif
3499 if (tty_fd != fd)
3500 close(tty_fd);
3501 }
3502 return retval;
3503}
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003506mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 static int first = TRUE;
3509
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003510#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511# ifdef HAVE_TERMIOS_H
3512 static struct termios told;
3513 struct termios tnew;
3514# else
3515 static struct termio told;
3516 struct termio tnew;
3517# endif
3518
3519 if (first)
3520 {
3521 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003522 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524
3525 tnew = told;
3526 if (tmode == TMODE_RAW)
3527 {
3528 /*
3529 * ~ICRNL enables typing ^V^M
3530 */
3531 tnew.c_iflag &= ~ICRNL;
3532 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3533# if defined(IEXTEN) && !defined(__MINT__)
3534 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3535 /* but it breaks function keys on MINT */
3536# endif
3537 );
3538# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3539 tnew.c_oflag &= ~ONLCR;
3540# endif
3541 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3542 tnew.c_cc[VTIME] = 0; /* don't wait */
3543 }
3544 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003545 {
3546 /* Also reset ICANON here, otherwise on Solaris select() won't see
3547 * typeahead characters. */
3548 tnew.c_lflag &= ~(ICANON | ECHO);
3549 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3550 tnew.c_cc[VTIME] = 0; /* don't wait */
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
3553# if defined(HAVE_TERMIOS_H)
3554 {
3555 int n = 10;
3556
3557 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3558 * few times. */
3559 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3560 && errno == EINTR && n > 0)
3561 --n;
3562 }
3563# else
3564 ioctl(read_cmd_fd, TCSETA, &tnew);
3565# endif
3566
3567#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 /*
3569 * for "old" tty systems
3570 */
3571# ifndef TIOCSETN
3572# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3573# endif
3574 static struct sgttyb ttybold;
3575 struct sgttyb ttybnew;
3576
3577 if (first)
3578 {
3579 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003580 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582
3583 ttybnew = ttybold;
3584 if (tmode == TMODE_RAW)
3585 {
3586 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3587 ttybnew.sg_flags |= RAW;
3588 }
3589 else if (tmode == TMODE_SLEEP)
3590 ttybnew.sg_flags &= ~(ECHO);
3591 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3592#endif
3593 curr_tmode = tmode;
3594}
3595
3596/*
3597 * Try to get the code for "t_kb" from the stty setting
3598 *
3599 * Even if termcap claims a backspace key, the user's setting *should*
3600 * prevail. stty knows more about reality than termcap does, and if
3601 * somebody's usual erase key is DEL (which, for most BSD users, it will
3602 * be), they're going to get really annoyed if their erase key starts
3603 * doing forward deletes for no reason. (Eric Fischer)
3604 */
3605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003606get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003608 ttyinfo_T info;
3609 char_u buf[2];
3610 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003612 if (get_tty_info(read_cmd_fd, &info) == OK)
3613 {
3614 intr_char = info.interrupt;
3615 buf[0] = info.backspace;
3616 buf[1] = NUL;
3617 add_termcode((char_u *)"kb", buf, FALSE);
3618
3619 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3620 p = find_termcode((char_u *)"kD");
3621 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3622 do_fixdel(NULL);
3623 }
3624}
3625
3626/*
3627 * Obtain the characters that Backspace and Enter produce on "fd".
3628 * Returns OK or FAIL.
3629 */
3630 int
3631get_tty_info(int fd, ttyinfo_T *info)
3632{
3633#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634# ifdef HAVE_TERMIOS_H
3635 struct termios keys;
3636# else
3637 struct termio keys;
3638# endif
3639
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003640 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003642 info->backspace = keys.c_cc[VERASE];
3643 info->interrupt = keys.c_cc[VINTR];
3644 if (keys.c_iflag & ICRNL)
3645 info->enter = NL;
3646 else
3647 info->enter = CAR;
3648 if (keys.c_oflag & ONLCR)
3649 info->nl_does_cr = TRUE;
3650 else
3651 info->nl_does_cr = FALSE;
3652 return OK;
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#else
3655 /* for "old" tty systems */
3656 struct sgttyb keys;
3657
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003658 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003660 info->backspace = keys.sg_erase;
3661 info->interrupt = keys.sg_kill;
3662 info->enter = CAR;
3663 info->nl_does_cr = TRUE;
3664 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668}
3669
3670#endif /* VMS */
3671
3672#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003673static int mouse_ison = FALSE;
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675/*
3676 * Set mouse clicks on or off.
3677 */
3678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003679mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003681# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003682 static int bevalterm_ison = FALSE;
3683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 int xterm_mouse_vers;
3685
Bram Moolenaara06afc72018-08-27 23:24:16 +02003686# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3687 if (!on)
3688 // Make sure not tracing mouse movements. Important when a button-down
3689 // was received but no release yet.
3690 stop_xterm_trace();
3691# endif
3692
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003693 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003694# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003695 && p_bevalterm == bevalterm_ison
3696# endif
3697 )
3698 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 return;
3700
3701 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003702
3703# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003704 if (ttym_flags == TTYM_URXVT)
3705 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003706 out_str_nf((char_u *)
3707 (on
3708 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3709 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003710 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003711 }
3712# endif
3713
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003714# ifdef FEAT_MOUSE_SGR
3715 if (ttym_flags == TTYM_SGR)
3716 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003717 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003718 out_str_nf((char_u *)
3719 (on
3720 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3721 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003722 mouse_ison = on;
3723 }
3724# endif
3725
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003726# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 if (bevalterm_ison != (p_bevalterm && on))
3728 {
3729 bevalterm_ison = (p_bevalterm && on);
3730 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3731 /* disable mouse movement events, enabling is below */
3732 out_str_nf((char_u *)
3733 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003734 }
3735# endif
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (xterm_mouse_vers > 0)
3738 {
3739 if (on) /* enable mouse events, use mouse tracking if available */
3740 out_str_nf((char_u *)
3741 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003742 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003743# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003744 bevalterm_ison
3745 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3746# endif
3747 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3749 else /* disable mouse events, could probably always send the same */
3750 out_str_nf((char_u *)
3751 (xterm_mouse_vers > 1
3752 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3753 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756
3757# ifdef FEAT_MOUSE_DEC
3758 else if (ttym_flags == TTYM_DEC)
3759 {
3760 if (on) /* enable mouse events */
3761 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3762 else /* disable mouse events */
3763 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003764 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766# endif
3767
3768# ifdef FEAT_MOUSE_GPM
3769 else
3770 {
3771 if (on)
3772 {
3773 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003774 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 else
3777 {
3778 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003779 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781 }
3782# endif
3783
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003784# ifdef FEAT_SYSMOUSE
3785 else
3786 {
3787 if (on)
3788 {
3789 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003791 }
3792 else
3793 {
3794 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003795 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003796 }
3797 }
3798# endif
3799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800# ifdef FEAT_MOUSE_JSB
3801 else
3802 {
3803 if (on)
3804 {
3805 /* D - Enable Mouse up/down messages
3806 * L - Enable Left Button Reporting
3807 * M - Enable Middle Button Reporting
3808 * R - Enable Right Button Reporting
3809 * K - Enable SHIFT and CTRL key Reporting
3810 * + - Enable Advanced messaging of mouse moves and up/down messages
3811 * Q - Quiet No Ack
3812 * # - Numeric value of mouse pointer required
3813 * 0 = Multiview 2000 cursor, used as standard
3814 * 1 = Windows Arrow
3815 * 2 = Windows I Beam
3816 * 3 = Windows Hour Glass
3817 * 4 = Windows Cross Hair
3818 * 5 = Windows UP Arrow
3819 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003820# ifdef JSBTERM_MOUSE_NONADVANCED
3821 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3823 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003824# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3826 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003827# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003828 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 else
3831 {
3832 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3833 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003834 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 }
3837# endif
3838# ifdef FEAT_MOUSE_PTERM
3839 else
3840 {
3841 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3842 if (on)
3843 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3844 else
3845 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003846 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848# endif
3849}
3850
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003851#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003852/*
3853 * Called when 'balloonevalterm' changed.
3854 */
3855 void
3856mch_bevalterm_changed(void)
3857{
3858 mch_setmouse(mouse_ison);
3859}
3860#endif
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
3863 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3864 */
3865 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003866check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868# ifdef FEAT_MOUSE_XTERM
3869 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003870# ifdef FEAT_MOUSE_URXVT
3871 && use_xterm_mouse() != 3
3872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873# ifdef FEAT_GUI
3874 && !gui.in_use
3875# endif
3876 )
3877 {
3878 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003879 ? IF_EB("\233M", CSI_STR "M")
3880 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (*p_mouse != NUL)
3882 {
3883 /* force mouse off and maybe on to send possibly new mouse
3884 * activation sequence to the xterm, with(out) drag tracing. */
3885 mch_setmouse(FALSE);
3886 setmouse();
3887 }
3888 }
3889 else
3890 del_mouse_termcode(KS_MOUSE);
3891# endif
3892
3893# ifdef FEAT_MOUSE_GPM
3894 if (!use_xterm_mouse()
3895# ifdef FEAT_GUI
3896 && !gui.in_use
3897# endif
3898 )
3899 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3900# endif
3901
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003902# ifdef FEAT_SYSMOUSE
3903 if (!use_xterm_mouse()
3904# ifdef FEAT_GUI
3905 && !gui.in_use
3906# endif
3907 )
3908 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3909# endif
3910
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003912 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (!use_xterm_mouse()
3914# ifdef FEAT_GUI
3915 && !gui.in_use
3916# endif
3917 )
3918 set_mouse_termcode(KS_JSBTERM_MOUSE,
3919 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3920 else
3921 del_mouse_termcode(KS_JSBTERM_MOUSE);
3922# endif
3923
3924# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003925 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 * define it in the GUI or when using an xterm. */
3927 if (!use_xterm_mouse()
3928# ifdef FEAT_GUI
3929 && !gui.in_use
3930# endif
3931 )
3932 set_mouse_termcode(KS_NETTERM_MOUSE,
3933 (char_u *)IF_EB("\033}", ESC_STR "}"));
3934 else
3935 del_mouse_termcode(KS_NETTERM_MOUSE);
3936# endif
3937
3938# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003939 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003940 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941# ifdef FEAT_GUI
3942 && !gui.in_use
3943# endif
3944 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003945 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3946 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
3948 del_mouse_termcode(KS_DEC_MOUSE);
3949# endif
3950# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003951 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003952 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953# ifdef FEAT_GUI
3954 && !gui.in_use
3955# endif
3956 )
3957 set_mouse_termcode(KS_PTERM_MOUSE,
3958 (char_u *) IF_EB("\033[", ESC_STR "["));
3959 else
3960 del_mouse_termcode(KS_PTERM_MOUSE);
3961# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003962# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003963 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003964# ifdef FEAT_GUI
3965 && !gui.in_use
3966# endif
3967 )
3968 {
3969 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003970 ? IF_EB("\233*M", CSI_STR "*M")
3971 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003972
3973 if (*p_mouse != NUL)
3974 {
3975 mch_setmouse(FALSE);
3976 setmouse();
3977 }
3978 }
3979 else
3980 del_mouse_termcode(KS_URXVT_MOUSE);
3981# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003982# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003983 if (use_xterm_mouse() == 4
3984# ifdef FEAT_GUI
3985 && !gui.in_use
3986# endif
3987 )
3988 {
3989 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003990 ? IF_EB("\233<*M", CSI_STR "<*M")
3991 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3992
3993 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3994 ? IF_EB("\233<*m", CSI_STR "<*m")
3995 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003996
3997 if (*p_mouse != NUL)
3998 {
3999 mch_setmouse(FALSE);
4000 setmouse();
4001 }
4002 }
4003 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02004004 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004005 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02004006 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
4007 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009}
4010#endif
4011
4012/*
4013 * set screen mode, always fails.
4014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004016mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004018 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020}
4021
4022#ifndef VMS
4023
4024/*
4025 * Try to get the current window size:
4026 * 1. with an ioctl(), most accurate method
4027 * 2. from the environment variables LINES and COLUMNS
4028 * 3. from the termcap
4029 * 4. keep using the old values
4030 * Return OK when size could be determined, FAIL otherwise.
4031 */
4032 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004033mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
4035 long rows = 0;
4036 long columns = 0;
4037 char_u *p;
4038
4039 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 * 1. try using an ioctl. It is the most accurate method.
4041 *
4042 * Try using TIOCGWINSZ first, some systems that have it also define
4043 * TIOCGSIZE but don't have a struct ttysize.
4044 */
4045# ifdef TIOCGWINSZ
4046 {
4047 struct winsize ws;
4048 int fd = 1;
4049
4050 /* When stdout is not a tty, use stdin for the ioctl(). */
4051 if (!isatty(fd) && isatty(read_cmd_fd))
4052 fd = read_cmd_fd;
4053 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4054 {
4055 columns = ws.ws_col;
4056 rows = ws.ws_row;
4057 }
4058 }
4059# else /* TIOCGWINSZ */
4060# ifdef TIOCGSIZE
4061 {
4062 struct ttysize ts;
4063 int fd = 1;
4064
4065 /* When stdout is not a tty, use stdin for the ioctl(). */
4066 if (!isatty(fd) && isatty(read_cmd_fd))
4067 fd = read_cmd_fd;
4068 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4069 {
4070 columns = ts.ts_cols;
4071 rows = ts.ts_lines;
4072 }
4073 }
4074# endif /* TIOCGSIZE */
4075# endif /* TIOCGWINSZ */
4076
4077 /*
4078 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004079 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4080 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004082 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
4084 if ((p = (char_u *)getenv("LINES")))
4085 rows = atoi((char *)p);
4086 if ((p = (char_u *)getenv("COLUMNS")))
4087 columns = atoi((char *)p);
4088 }
4089
4090#ifdef HAVE_TGETENT
4091 /*
4092 * 3. try reading "co" and "li" entries from termcap
4093 */
4094 if (columns == 0 || rows == 0)
4095 getlinecol(&columns, &rows);
4096#endif
4097
4098 /*
4099 * 4. If everything fails, use the old values
4100 */
4101 if (columns <= 0 || rows <= 0)
4102 return FAIL;
4103
4104 Rows = rows;
4105 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004106 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return OK;
4108}
4109
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004110#if defined(FEAT_TERMINAL) || defined(PROTO)
4111/*
4112 * Report the windows size "rows" and "cols" to tty "fd".
4113 */
4114 int
4115mch_report_winsize(int fd, int rows, int cols)
4116{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004117 int tty_fd;
4118 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004119
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004120 tty_fd = get_tty_fd(fd);
4121 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004122 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004123# if defined(TIOCSWINSZ)
4124 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004125
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004126 ws.ws_col = cols;
4127 ws.ws_row = rows;
4128 ws.ws_xpixel = cols * 5;
4129 ws.ws_ypixel = rows * 10;
4130 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4131 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4132 retval == 0 ? "success" : "failed");
4133# elif defined(TIOCSSIZE)
4134 struct ttysize ts;
4135
4136 ts.ts_cols = cols;
4137 ts.ts_lines = rows;
4138 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4139 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4140 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004141# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004142 if (tty_fd != fd)
4143 close(tty_fd);
4144 }
4145 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004146}
4147#endif
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149/*
4150 * Try to set the window size to Rows and Columns.
4151 */
4152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004153mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155 if (*T_CWS)
4156 {
4157 /*
4158 * NOTE: if you get an error here that term_set_winsize() is
4159 * undefined, check the output of configure. It could probably not
4160 * find a ncurses, termcap or termlib library.
4161 */
4162 term_set_winsize((int)Rows, (int)Columns);
4163 out_flush();
4164 screen_start(); /* don't know where cursor is now */
4165 }
4166}
4167
4168#endif /* VMS */
4169
4170/*
4171 * Rows and/or Columns has changed.
4172 */
4173 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004174mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 /* Nothing to do. */
4177}
4178
Bram Moolenaar205b8862011-09-07 15:04:31 +02004179/*
4180 * Wait for process "child" to end.
4181 * Return "child" if it exited properly, <= 0 on error.
4182 */
4183 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004184wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004185{
4186 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004187 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004188
4189 while (wait_pid != child)
4190 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004191 /* When compiled with Python threads are probably used, in which case
4192 * wait() sometimes hangs for no obvious reason. Use waitpid()
4193 * instead and loop (like the GUI). Also needed for other interfaces,
4194 * they might call system(). */
4195# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004196 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004197# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004198 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004199# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004200 if (wait_pid == 0)
4201 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004202 /* Wait for 1 to 10 msec before trying again. */
4203 mch_delay(delay_msec, TRUE);
4204 if (++delay_msec > 10)
4205 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004206 continue;
4207 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004208 if (wait_pid <= 0
4209# ifdef ECHILD
4210 && errno == ECHILD
4211# endif
4212 )
4213 break;
4214 }
4215 return wait_pid;
4216}
4217
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004218#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004219/*
4220 * Set the environment for a child process.
4221 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004223set_child_environment(
4224 long rows,
4225 long columns,
4226 char *term,
4227 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004228{
4229# ifdef HAVE_SETENV
4230 char envbuf[50];
4231# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004232 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004234 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004235 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004236 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004237# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004238 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004239# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004240# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004241 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004242# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004243# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004244 long colors =
4245# ifdef FEAT_GUI
4246 gui.in_use ? 256*256*256 :
4247# endif
4248 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004250# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004251 setenv("TERM", term, 1);
4252 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004255 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004256 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004257 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004258 sprintf((char *)envbuf, "%ld", colors);
4259 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004260# ifdef FEAT_TERMINAL
4261 if (is_terminal)
4262 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004263 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004264 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4265 }
4266# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004267# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004268 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004269# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270# else
4271 /*
4272 * Putenv does not copy the string, it has to remain valid.
4273 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004274 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004275 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004276 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4277 putenv(envbuf_Term);
4278 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004279 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004280 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4281 putenv(envbuf_Lines);
4282 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4283 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004284 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004285 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4286 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004287# ifdef FEAT_TERMINAL
4288 if (is_terminal)
4289 {
4290 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004291 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004292 putenv(envbuf_Version);
4293 }
4294# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004295# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004296 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4297 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4298 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004299# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004300# endif
4301}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004302
4303 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004304set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004305{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004306 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004307}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004308#endif
4309
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004310#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004311/*
4312 * Open a PTY, with FD for the master and slave side.
4313 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4314 * When successful both file descriptors are stored.
4315 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004316 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004317open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004318{
4319 char *tty_name;
4320
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004321 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004322 if (*pty_master_fd >= 0)
4323 {
4324 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4325 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4326 * adding O_NOCTTY always works when defined. */
4327#ifdef O_NOCTTY
4328 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4329#else
4330 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4331#endif
4332 if (*pty_slave_fd < 0)
4333 {
4334 close(*pty_master_fd);
4335 *pty_master_fd = -1;
4336 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004337 else if (namep != NULL)
4338 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339 }
4340}
4341#endif
4342
Bram Moolenaarfae42832017-08-01 22:24:26 +02004343/*
4344 * Send SIGINT to a child process if "c" is an interrupt character.
4345 */
4346 void
4347may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4348{
4349# ifdef SIGINT
4350 if (c == Ctrl_C || c == intr_char)
4351 {
4352# ifdef HAVE_SETSID
4353 kill(-pid, SIGINT);
4354# else
4355 kill(0, SIGINT);
4356# endif
4357 if (wpid > 0)
4358 kill(wpid, SIGINT);
4359 }
4360# endif
4361}
4362
Bram Moolenaar13568252018-03-16 20:46:58 +01004363#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4364
4365 static int
4366build_argv(
4367 char_u *cmd,
4368 char ***argvp,
4369 char_u **sh_tofree,
4370 char_u **shcf_tofree)
4371{
4372 char **argv = NULL;
4373 int argc;
4374
4375 *sh_tofree = vim_strsave(p_sh);
4376 if (*sh_tofree == NULL) /* out of memory */
4377 return FAIL;
4378
4379 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4380 return FAIL;
4381 *argvp = argv;
4382
4383 if (cmd != NULL)
4384 {
4385 char_u *s;
4386 char_u *p;
4387
4388 if (extra_shell_arg != NULL)
4389 argv[argc++] = (char *)extra_shell_arg;
4390
4391 /* Break 'shellcmdflag' into white separated parts. This doesn't
4392 * handle quoted strings, they are very unlikely to appear. */
4393 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4394 if (*shcf_tofree == NULL) /* out of memory */
4395 return FAIL;
4396 s = *shcf_tofree;
4397 p = p_shcf;
4398 while (*p != NUL)
4399 {
4400 argv[argc++] = (char *)s;
4401 while (*p && *p != ' ' && *p != TAB)
4402 *s++ = *p++;
4403 *s++ = NUL;
4404 p = skipwhite(p);
4405 }
4406
4407 argv[argc++] = (char *)cmd;
4408 }
4409 argv[argc] = NULL;
4410 return OK;
4411}
4412#endif
4413
4414#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4415/*
4416 * Use a terminal window to run a shell command in.
4417 */
4418 static int
4419mch_call_shell_terminal(
4420 char_u *cmd,
4421 int options UNUSED) /* SHELL_*, see vim.h */
4422{
4423 jobopt_T opt;
4424 char **argv = NULL;
4425 char_u *tofree1 = NULL;
4426 char_u *tofree2 = NULL;
4427 int retval = -1;
4428 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004429 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004430 aco_save_T aco;
4431 oparg_T oa; /* operator arguments */
4432
4433 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4434 goto theend;
4435
4436 init_job_options(&opt);
4437 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4438 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004439 if (buf == NULL)
4440 goto theend;
4441
4442 job = term_getjob(buf->b_term);
4443 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004444
4445 /* Find a window to make "buf" curbuf. */
4446 aucmd_prepbuf(&aco, buf);
4447
4448 clear_oparg(&oa);
4449 while (term_use_loop())
4450 {
4451 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4452 {
4453 /* If terminal_loop() returns OK we got a key that is handled
4454 * in Normal model. We don't do redrawing anyway. */
4455 if (terminal_loop(TRUE) == OK)
4456 normal_cmd(&oa, TRUE);
4457 }
4458 else
4459 normal_cmd(&oa, TRUE);
4460 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004461 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004462 ch_log(NULL, "system command finished");
4463
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004464 job_unref(job);
4465
Bram Moolenaar13568252018-03-16 20:46:58 +01004466 /* restore curwin/curbuf and a few other things */
4467 aucmd_restbuf(&aco);
4468
4469 wait_return(TRUE);
4470 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4471
4472theend:
4473 vim_free(argv);
4474 vim_free(tofree1);
4475 vim_free(tofree2);
4476 return retval;
4477}
4478#endif
4479
4480#ifdef USE_SYSTEM
4481/*
4482 * Use system() to start the shell: simple but slow.
4483 */
4484 static int
4485mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004486 char_u *cmd,
4487 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489#ifdef VMS
4490 char *ifn = NULL;
4491 char *ofn = NULL;
4492#endif
4493 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004494 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004495 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 out_flush();
4498
4499 if (options & SHELL_COOKED)
4500 settmode(TMODE_COOK); /* set to normal mode */
4501
Bram Moolenaar62b42182010-09-21 22:09:37 +02004502# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004503 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004504 loose_clipboard();
4505# endif
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (cmd == NULL)
4508 x = system((char *)p_sh);
4509 else
4510 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004511# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (ofn = strchr((char *)cmd, '>'))
4513 *ofn++ = '\0';
4514 if (ifn = strchr((char *)cmd, '<'))
4515 {
4516 char *p;
4517
4518 *ifn++ = '\0';
4519 p = strchr(ifn,' '); /* chop off any trailing spaces */
4520 if (p)
4521 *p = '\0';
4522 }
4523 if (ofn)
4524 x = vms_sys((char *)cmd, ofn, ifn);
4525 else
4526 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004527# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 newcmd = lalloc(STRLEN(p_sh)
4529 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4530 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4531 if (newcmd == NULL)
4532 x = 0;
4533 else
4534 {
4535 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4536 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4537 (char *)p_shcf,
4538 (char *)cmd);
4539 x = system((char *)newcmd);
4540 vim_free(newcmd);
4541 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544# ifdef VMS
4545 x = vms_sys_status(x);
4546# endif
4547 if (emsg_silent)
4548 ;
4549 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else if (x && !(options & SHELL_SILENT))
4552 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004553 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 msg_outnum((long)x);
4555 msg_putchar('\n');
4556 }
4557
4558 if (tmode == TMODE_RAW)
4559 settmode(TMODE_RAW); /* set to raw mode */
4560# ifdef FEAT_TITLE
4561 resettitle();
4562# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004563# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4564 restore_clipboard();
4565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar13568252018-03-16 20:46:58 +01004569#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaardf177f62005-02-22 08:39:57 +00004571# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4572 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004573# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaar13568252018-03-16 20:46:58 +01004575/*
4576 * Don't use system(), use fork()/exec().
4577 */
4578 static int
4579mch_call_shell_fork(
4580 char_u *cmd,
4581 int options) /* SHELL_*, see vim.h */
4582{
4583 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004585 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 pid_t wait_pid = 0;
4587# ifdef HAVE_UNION_WAIT
4588 union wait status;
4589# else
4590 int status = -1;
4591# endif
4592 int retval = -1;
4593 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004594 char_u *tofree1 = NULL;
4595 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004600# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004601 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 int fd_fromshell[2];
4603 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004604 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 out_flush();
4607 if (options & SHELL_COOKED)
4608 settmode(TMODE_COOK); /* set to normal mode */
4609
Bram Moolenaar13568252018-03-16 20:46:58 +01004610 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004611 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614 * For the GUI, when writing the output into the buffer and when reading
4615 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4616 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004618 if ((options & (SHELL_READ|SHELL_WRITE))
4619# ifdef FEAT_GUI
4620 || (gui.in_use && show_shell_mess)
4621# endif
4622 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004624# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 /*
4626 * Try to open a master pty.
4627 * If this works, open the slave pty.
4628 * If the slave can't be opened, close the master pty.
4629 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004630 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004631 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 /*
4633 * If not opening a pty or it didn't work, try using pipes.
4634 */
4635 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 pipe_error = (pipe(fd_toshell) < 0);
4639 if (!pipe_error) /* pipe create OK */
4640 {
4641 pipe_error = (pipe(fd_fromshell) < 0);
4642 if (pipe_error) /* pipe create failed */
4643 {
4644 close(fd_toshell[0]);
4645 close(fd_toshell[1]);
4646 }
4647 }
4648 if (pipe_error)
4649 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004650 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 out_flush();
4652 }
4653 }
4654 }
4655
4656 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004658 SIGSET_DECL(curset)
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660# ifdef __BEOS__
4661 beos_cleanup_read_thread();
4662# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004663
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004664 BLOCK_SIGNALS(&curset);
4665 pid = fork(); /* maybe we should use vfork() */
4666 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004668 UNBLOCK_SIGNALS(&curset);
4669
Bram Moolenaar32526b32019-01-19 17:43:09 +01004670 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004671 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004673 || (gui.in_use && show_shell_mess)
4674# endif
4675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004677# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (pty_master_fd >= 0) /* close the pseudo tty */
4679 {
4680 close(pty_master_fd);
4681 close(pty_slave_fd);
4682 }
4683 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
4686 close(fd_toshell[0]);
4687 close(fd_toshell[1]);
4688 close(fd_fromshell[0]);
4689 close(fd_fromshell[1]);
4690 }
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693 else if (pid == 0) /* child */
4694 {
4695 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004696 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
Bram Moolenaar819524702018-02-27 19:10:00 +01004698# ifdef FEAT_JOB_CHANNEL
4699 if (ch_log_active())
4700 /* close the log file in the child */
4701 ch_logfile((char_u *)"", (char_u *)"");
4702# endif
4703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (!show_shell_mess || (options & SHELL_EXPAND))
4705 {
4706 int fd;
4707
4708 /*
4709 * Don't want to show any message from the shell. Can't just
4710 * close stdout and stderr though, because some systems will
4711 * break if you try to write to them after that, so we must
4712 * use dup() to replace them with something else -- webb
4713 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4714 * waiting for input.
4715 */
4716 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4717 fclose(stdin);
4718 fclose(stdout);
4719 fclose(stderr);
4720
4721 /*
4722 * If any of these open()'s and dup()'s fail, we just continue
4723 * anyway. It's not fatal, and on most systems it will make
4724 * no difference at all. On a few it will cause the execvp()
4725 * to exit with a non-zero status even when the completion
4726 * could be done, which is nothing too serious. If the open()
4727 * or dup() failed we'd just do the same thing ourselves
4728 * anyway -- webb
4729 */
4730 if (fd >= 0)
4731 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004732 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4733 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4734 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736 /* Don't need this now that we've duplicated it */
4737 close(fd);
4738 }
4739 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004740 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004742 || gui.in_use
4743# endif
4744 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
4746
Bram Moolenaardf177f62005-02-22 08:39:57 +00004747# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004748 /* Create our own process group, so that the child and all its
4749 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004750 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004751 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004752 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004753 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004754# if defined(SIGHUP)
4755 /* When doing "!xterm&" and 'shell' is bash: the shell
4756 * will exit and send SIGHUP to all processes in its
4757 * group, killing the just started process. Ignore SIGHUP
4758 * to avoid that. (suggested by Simon Schubert)
4759 */
4760 signal(SIGHUP, SIG_IGN);
4761# endif
4762 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004763# endif
4764# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004765 if (pty_slave_fd >= 0)
4766 {
4767 /* push stream discipline modules */
4768 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004769 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004771 /* Try to become controlling tty (probably doesn't work,
4772 * unless run by root) */
4773 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004775 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004777 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
Bram Moolenaara5792f52005-11-23 21:25:05 +00004779 /*
4780 * stderr is only redirected when using the GUI, so that a
4781 * program like gpg can still access the terminal to get a
4782 * passphrase using stderr.
4783 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004784# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 if (pty_master_fd >= 0)
4786 {
4787 close(pty_master_fd); /* close master side of pty */
4788
4789 /* set up stdin/stdout/stderr for the child */
4790 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004791 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004793 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004794 if (gui.in_use)
4795 {
4796 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004797 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800 close(pty_slave_fd); /* has been dupped, close it now */
4801 }
4802 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 /* set up stdin for the child */
4806 close(fd_toshell[1]);
4807 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004808 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 close(fd_toshell[0]);
4810
4811 /* set up stdout for the child */
4812 close(fd_fromshell[0]);
4813 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004814 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 close(fd_fromshell[1]);
4816
Bram Moolenaara5792f52005-11-23 21:25:05 +00004817# ifdef FEAT_GUI
4818 if (gui.in_use)
4819 {
4820 /* set up stderr for the child */
4821 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004822 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004823 }
4824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 /*
4829 * There is no type cast for the argv, because the type may be
4830 * different on different machines. This may cause a warning
4831 * message with strict compilers, don't worry about it.
4832 * Call _exit() instead of exit() to avoid closing the connection
4833 * to the X server (esp. with GTK, which uses atexit()).
4834 */
4835 execvp(argv[0], argv);
4836 _exit(EXEC_FAILED); /* exec failed, return failure code */
4837 }
4838 else /* parent */
4839 {
4840 /*
4841 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 */
4844 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004846 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004847# ifdef FEAT_JOB_CHANNEL
4848 ++dont_check_job_ended;
4849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 /*
4851 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004852 * This is also used to pipe stdin/stdout to/from the external
4853 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855 if ((options & (SHELL_READ|SHELL_WRITE))
4856# ifdef FEAT_GUI
4857 || (gui.in_use && show_shell_mess)
4858# endif
4859 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004861# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 char_u buffer[BUFLEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4865 int ta_len = 0; /* valid bytes in ta_buf[] */
4866 int len;
4867 int p_more_save;
4868 int old_State;
4869 int c;
4870 int toshell_fd;
4871 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004872 garray_T ga;
4873 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004874# ifdef ELAPSED_FUNC
4875 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
Bram Moolenaardf177f62005-02-22 08:39:57 +00004878# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 if (pty_master_fd >= 0)
4880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 fromshell_fd = pty_master_fd;
4882 toshell_fd = dup(pty_master_fd);
4883 }
4884 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
4887 close(fd_toshell[0]);
4888 close(fd_fromshell[1]);
4889 toshell_fd = fd_toshell[1];
4890 fromshell_fd = fd_fromshell[0];
4891 }
4892
4893 /*
4894 * Write to the child if there are typed characters.
4895 * Read from the child if there are characters available.
4896 * Repeat the reading a few times if more characters are
4897 * available. Need to check for typed keys now and then, but
4898 * not too often (delays when no chars are available).
4899 * This loop is quit if no characters can be read from the pty
4900 * (WaitForChar detected special condition), or there are no
4901 * characters available and the child has exited.
4902 * Only check if the child has exited when there is no more
4903 * output. The child may exit before all the output has
4904 * been printed.
4905 *
4906 * Currently this busy loops!
4907 * This can probably dead-lock when the write blocks!
4908 */
4909 p_more_save = p_more;
4910 p_more = FALSE;
4911 old_State = State;
4912 State = EXTERNCMD; /* don't redraw at window resize */
4913
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004914 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004915 {
4916 /* Fork a process that will write the lines to the
4917 * external program. */
4918 if ((wpid = fork()) == -1)
4919 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004920 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004922 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 {
4924 linenr_T lnum = curbuf->b_op_start.lnum;
4925 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004926 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 size_t l;
4928
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004929 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 for (;;)
4931 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004932 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004933 if (l == 0)
4934 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004935 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004936 /* NL -> NUL translation */
4937 len = write(toshell_fd, "", (size_t)1);
4938 else
4939 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004940 char_u *s = vim_strchr(lp + written, NL);
4941
Bram Moolenaar89d40322006-08-29 15:30:07 +00004942 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004943 s == NULL ? l
4944 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004945 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004946 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004947 {
4948 /* Finished a line, add a NL, unless this line
4949 * should not have one. */
4950 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004951 || (!curbuf->b_p_bin
4952 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004953 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004954 && (lnum !=
4955 curbuf->b_ml.ml_line_count
4956 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004957 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004958 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004959 ++lnum;
4960 if (lnum > curbuf->b_op_end.lnum)
4961 {
4962 /* finished all the lines, close pipe */
4963 close(toshell_fd);
4964 toshell_fd = -1;
4965 break;
4966 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004967 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004968 written = 0;
4969 }
4970 else if (len > 0)
4971 written += len;
4972 }
4973 _exit(0);
4974 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004975 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004976 {
4977 close(toshell_fd);
4978 toshell_fd = -1;
4979 }
4980 }
4981
4982 if (options & SHELL_READ)
4983 ga_init2(&ga, 1, BUFLEN);
4984
4985 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004986# ifdef ELAPSED_FUNC
4987 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 for (;;)
4990 {
4991 /*
4992 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004993 * if there are any.
4994 * Don't do this if we are expanding wild cards (would eat
4995 * typeahead).
4996 * Don't do this when filtering and terminal is in cooked
4997 * mode, the shell command will handle the I/O. Avoids
4998 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004999 * Don't get characters when the child has already
5000 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00005001 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005002 * while (noread_cnt > 4), avoids that ":r !ls" eats
5003 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 */
5005 len = 0;
5006 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005007 && ((options &
5008 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5009 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005010# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005011 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005012# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005013 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005014 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005015 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005017 if (ta_len == 0)
5018 {
5019 /* Get extra characters when we don't have any.
5020 * Reset the counter and timer. */
5021 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005022# ifdef ELAPSED_FUNC
5023 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005024# endif
5025 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5026 }
5027 if (ta_len > 0 || len > 0)
5028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 /*
5030 * For pipes:
5031 * Check for CTRL-C: send interrupt signal to child.
5032 * Check for CTRL-D: EOF, close pipe to child.
5033 */
5034 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 /*
5037 * Send SIGINT to the child's group or all
5038 * processes in our group.
5039 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005040 may_send_sigint(ta_buf[ta_len], pid, wpid);
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (pty_master_fd < 0 && toshell_fd >= 0
5043 && ta_buf[ta_len] == Ctrl_D)
5044 {
5045 close(toshell_fd);
5046 toshell_fd = -1;
5047 }
5048 }
5049
5050 /* replace K_BS by <BS> and K_DEL by <DEL> */
5051 for (i = ta_len; i < ta_len + len; ++i)
5052 {
5053 if (ta_buf[i] == CSI && len - i > 2)
5054 {
5055 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5056 if (c == K_DEL || c == K_KDEL || c == K_BS)
5057 {
5058 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5059 (size_t)(len - i - 2));
5060 if (c == K_DEL || c == K_KDEL)
5061 ta_buf[i] = DEL;
5062 else
5063 ta_buf[i] = Ctrl_H;
5064 len -= 2;
5065 }
5066 }
5067 else if (ta_buf[i] == '\r')
5068 ta_buf[i] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005070 i += (*mb_ptr2len_len)(ta_buf + i,
5071 ta_len + len - i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073
5074 /*
5075 * For pipes: echo the typed characters.
5076 * For a pty this does not seem to work.
5077 */
5078 if (pty_master_fd < 0)
5079 {
5080 for (i = ta_len; i < ta_len + len; ++i)
5081 {
5082 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5083 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 else if (has_mbyte)
5085 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005086 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 msg_outtrans_len(ta_buf + i, l);
5089 i += l - 1;
5090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 else
5092 msg_outtrans_len(ta_buf + i, 1);
5093 }
5094 windgoto(msg_row, msg_col);
5095 out_flush();
5096 }
5097
5098 ta_len += len;
5099
5100 /*
5101 * Write the characters to the child, unless EOF has
5102 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005103 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 * When writing buffer lines, drop the typed
5105 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005107 if (options & SHELL_WRITE)
5108 ta_len = 0;
5109 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5112 if (len > 0)
5113 {
5114 ta_len -= len;
5115 mch_memmove(ta_buf, ta_buf + len, ta_len);
5116 }
5117 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
Bram Moolenaardf177f62005-02-22 08:39:57 +00005121 if (got_int)
5122 {
5123 /* CTRL-C sends a signal to the child, we ignore it
5124 * ourselves */
5125# ifdef HAVE_SETSID
5126 kill(-pid, SIGINT);
5127# else
5128 kill(0, SIGINT);
5129# endif
5130 if (wpid > 0)
5131 kill(wpid, SIGINT);
5132 got_int = FALSE;
5133 }
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /*
5136 * Check if the child has any characters to be printed.
5137 * Read them and write them to our window. Repeat this as
5138 * long as there is something to do, avoid the 10ms wait
5139 * for mch_inchar(), or sending typeahead characters to
5140 * the external process.
5141 * TODO: This should handle escape sequences, compatible
5142 * to some terminal (vt52?).
5143 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005144 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005145 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005147 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 );
5150 if (len <= 0) /* end of file or error */
5151 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005152
5153 noread_cnt = 0;
5154 if (options & SHELL_READ)
5155 {
5156 /* Do NUL -> NL translation, append NL separated
5157 * lines to the current buffer. */
5158 for (i = 0; i < len; ++i)
5159 {
5160 if (buffer[i] == NL)
5161 append_ga_line(&ga);
5162 else if (buffer[i] == NUL)
5163 ga_append(&ga, NL);
5164 else
5165 ga_append(&ga, buffer[i]);
5166 }
5167 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005168 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
5170 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005171 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
Bram Moolenaardf177f62005-02-22 08:39:57 +00005173 len += buffer_off;
5174 buffer[len] = NUL;
5175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 /* Check if the last character in buffer[] is
5177 * incomplete, keep these bytes for the next
5178 * round. */
5179 for (p = buffer; p < buffer + len; p += l)
5180 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005181 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (l == 0)
5183 l = 1; /* NUL byte? */
5184 else if (MB_BYTE2LEN(*p) != l)
5185 break;
5186 }
5187 if (p == buffer) /* no complete character */
5188 {
5189 /* avoid getting stuck at an illegal byte */
5190 if (len >= 12)
5191 ++p;
5192 else
5193 {
5194 buffer_off = len;
5195 continue;
5196 }
5197 }
5198 c = *p;
5199 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005200 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (p < buffer + len)
5202 {
5203 *p = c;
5204 buffer_off = (buffer + len) - p;
5205 mch_memmove(buffer, p, buffer_off);
5206 continue;
5207 }
5208 buffer_off = 0;
5209 }
5210 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
5212 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005213 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215
5216 windgoto(msg_row, msg_col);
5217 cursor_on();
5218 out_flush();
5219 if (got_int)
5220 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005221
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005222# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005223 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005224 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005225 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005226
5227 /* Avoid that we keep looping here without
5228 * checking for a CTRL-C for a long time. Don't
5229 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005230 if (msec > 2000)
5231 {
5232 noread_cnt = 5;
5233 break;
5234 }
5235 }
5236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005239 /* If we already detected the child has finished, continue
5240 * reading output for a short while. Some text may be
5241 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005242 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005243 {
5244 if (noread_cnt < 5)
5245 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005246 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005247 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /*
5250 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005251 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005253# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005254 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005255# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5259 || (wait_pid == pid && WIFEXITED(status)))
5260 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005261 /* Don't break the loop yet, try reading more
5262 * characters from "fromshell_fd" first. When using
5263 * pipes there might still be something to read and
5264 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005267 else
5268 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005269
Bram Moolenaar95a51352013-03-21 22:53:50 +01005270# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005271 /* Handle any X events, e.g. serving the clipboard. */
5272 clip_update();
5273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275finished:
5276 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005277 if (options & SHELL_READ)
5278 {
5279 if (ga.ga_len > 0)
5280 {
5281 append_ga_line(&ga);
5282 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005283 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005284 }
5285 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005286 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005287 ga_clear(&ga);
5288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 /*
5291 * Give all typeahead that wasn't used back to ui_inchar().
5292 */
5293 if (ta_len)
5294 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 State = old_State;
5296 if (toshell_fd >= 0)
5297 close(toshell_fd);
5298 close(fromshell_fd);
5299 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005300# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005301 else
5302 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005303 long delay_msec = 1;
5304
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005305 /*
5306 * Similar to the loop above, but only handle X events, no
5307 * I/O.
5308 */
5309 for (;;)
5310 {
5311 if (got_int)
5312 {
5313 /* CTRL-C sends a signal to the child, we ignore it
5314 * ourselves */
5315# ifdef HAVE_SETSID
5316 kill(-pid, SIGINT);
5317# else
5318 kill(0, SIGINT);
5319# endif
5320 got_int = FALSE;
5321 }
5322# ifdef __NeXT__
5323 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5324# else
5325 wait_pid = waitpid(pid, &status, WNOHANG);
5326# endif
5327 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5328 || (wait_pid == pid && WIFEXITED(status)))
5329 {
5330 wait_pid = pid;
5331 break;
5332 }
5333
5334 /* Handle any X events, e.g. serving the clipboard. */
5335 clip_update();
5336
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005337 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5338 * less time. */
5339 mch_delay(delay_msec, TRUE);
5340 if (++delay_msec > 10)
5341 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005342 }
5343 }
5344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 /*
5347 * Wait until our child has exited.
5348 * Ignore wait() returning pids of other children and returning
5349 * because of some signal like SIGWINCH.
5350 * Don't wait if wait_pid was already set above, indicating the
5351 * child already exited.
5352 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005353 if (wait_pid != pid)
5354 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
Bram Moolenaar624891f2010-10-13 16:22:09 +02005356# ifdef FEAT_GUI
5357 /* Close slave side of pty. Only do this after the child has
5358 * exited, otherwise the child may hang when it tries to write on
5359 * the pty. */
5360 if (pty_master_fd >= 0)
5361 close(pty_slave_fd);
5362# endif
5363
Bram Moolenaardf177f62005-02-22 08:39:57 +00005364 /* Make sure the child that writes to the external program is
5365 * dead. */
5366 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005367 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005368 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005369 wait4pid(wpid, NULL);
5370 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005371
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005372# ifdef FEAT_JOB_CHANNEL
5373 --dont_check_job_ended;
5374# endif
5375
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 /*
5377 * Set to raw mode right now, otherwise a CTRL-C after
5378 * catch_signals() will kill Vim.
5379 */
5380 if (tmode == TMODE_RAW)
5381 settmode(TMODE_RAW);
5382 did_settmode = TRUE;
5383 set_signals();
5384
5385 if (WIFEXITED(status))
5386 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005387 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005389 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 if (retval == EXEC_FAILED)
5392 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005393 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 msg_outtrans(p_sh);
5395 msg_putchar('\n');
5396 }
5397 else if (!(options & SHELL_SILENT))
5398 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005399 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 msg_outnum((long)retval);
5401 msg_putchar('\n');
5402 }
5403 }
5404 }
5405 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005406 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410error:
5411 if (!did_settmode)
5412 if (tmode == TMODE_RAW)
5413 settmode(TMODE_RAW); /* set to raw mode */
5414# ifdef FEAT_TITLE
5415 resettitle();
5416# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005417 vim_free(argv);
5418 vim_free(tofree1);
5419 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
5421 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005424
5425 int
5426mch_call_shell(
5427 char_u *cmd,
5428 int options) /* SHELL_*, see vim.h */
5429{
5430#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5431 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5432 return mch_call_shell_terminal(cmd, options);
5433#endif
5434#ifdef USE_SYSTEM
5435 return mch_call_shell_system(cmd, options);
5436#else
5437 return mch_call_shell_fork(cmd, options);
5438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439}
5440
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005441#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005442 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005443mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005444{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005445 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005446 int fd_in[2] = {-1, -1}; /* for stdin */
5447 int fd_out[2] = {-1, -1}; /* for stdout */
5448 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005449 int pty_master_fd = -1;
5450 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005451 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005452 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5453 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5454 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005455 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005456 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5457 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005458 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005459 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005460 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005461
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005462 if (use_out_for_err && use_null_for_out)
5463 use_null_for_err = TRUE;
5464
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005465 /* default is to fail */
5466 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005467
Bram Moolenaarb2412082017-08-20 18:09:14 +02005468 if (options->jo_pty
5469 && (!(use_file_for_in || use_null_for_in)
5470 || !(use_file_for_in || use_null_for_out)
5471 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005472 {
5473 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5474 if (job->jv_tty_out != NULL)
5475 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5476 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005477
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005478 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005479 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005480 if (use_file_for_in)
5481 {
5482 char_u *fname = options->jo_io_name[PART_IN];
5483
5484 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5485 if (fd_in[0] < 0)
5486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005487 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005488 goto failed;
5489 }
5490 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005491 else
5492 /* When writing buffer lines to the input don't use the pty, so that
5493 * the pipe can be closed when all lines were written. */
5494 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5495 && pipe(fd_in) < 0)
5496 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005497
5498 if (use_file_for_out)
5499 {
5500 char_u *fname = options->jo_io_name[PART_OUT];
5501
5502 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5503 if (fd_out[1] < 0)
5504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005505 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005506 goto failed;
5507 }
5508 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005509 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005510 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005511
5512 if (use_file_for_err)
5513 {
5514 char_u *fname = options->jo_io_name[PART_ERR];
5515
5516 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5517 if (fd_err[1] < 0)
5518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005519 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005520 goto failed;
5521 }
5522 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005523 else if (!use_out_for_err && !use_null_for_err
5524 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005525 goto failed;
5526
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005527 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5528 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005529 if (options->jo_set & JO_CHANNEL)
5530 {
5531 channel = options->jo_channel;
5532 if (channel != NULL)
5533 ++channel->ch_refcount;
5534 }
5535 else
5536 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005537 if (channel == NULL)
5538 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005539 if (job->jv_tty_out != NULL)
5540 ch_log(channel, "using pty %s on fd %d",
5541 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005542 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005543
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005544 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005545 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005546 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005547 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005548 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005549 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005550 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005551 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005552 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005553 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005554 int null_fd = -1;
5555 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005556
Bram Moolenaar835dc632016-02-07 14:27:38 +01005557 /* child */
5558 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005559 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005560
Bram Moolenaar819524702018-02-27 19:10:00 +01005561# ifdef FEAT_JOB_CHANNEL
5562 if (ch_log_active())
5563 /* close the log file in the child */
5564 ch_logfile((char_u *)"", (char_u *)"");
5565# endif
5566
Bram Moolenaar835dc632016-02-07 14:27:38 +01005567# ifdef HAVE_SETSID
5568 /* Create our own process group, so that the child and all its
5569 * children can be kill()ed. Don't do this when using pipes,
5570 * because stdin is not a tty, we would lose /dev/tty. */
5571 (void)setsid();
5572# endif
5573
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005574# ifdef FEAT_TERMINAL
5575 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005576 {
5577 char *term = (char *)T_NAME;
5578
5579#ifdef FEAT_GUI
5580 if (term_is_gui(T_NAME))
5581 /* In the GUI 'term' is not what we want, use $TERM. */
5582 term = getenv("TERM");
5583#endif
5584 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5585 * back to "xterm". */
5586 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5587 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005588 set_child_environment(
5589 (long)options->jo_term_rows,
5590 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005591 term,
5592 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005593 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005594 else
5595# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005596 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005597
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005598 if (options->jo_env != NULL)
5599 {
5600 dict_T *dict = options->jo_env;
5601 hashitem_T *hi;
5602 int todo = (int)dict->dv_hashtab.ht_used;
5603
5604 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5605 if (!HASHITEM_EMPTY(hi))
5606 {
5607 typval_T *item = &dict_lookup(hi)->di_tv;
5608
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005610 --todo;
5611 }
5612 }
5613
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005614 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005615 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005616 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005617 if (null_fd < 0)
5618 {
5619 perror("opening /dev/null failed");
5620 _exit(OPEN_NULL_FAILED);
5621 }
5622 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005623
Bram Moolenaar223896d2017-08-02 22:33:28 +02005624 if (pty_slave_fd >= 0)
5625 {
5626 /* push stream discipline modules */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005627 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005628# ifdef TIOCSCTTY
5629 /* Try to become controlling tty (probably doesn't work,
5630 * unless run by root) */
5631 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5632# endif
5633 }
5634
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005635 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005636 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005637 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005638 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005639 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005640 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005641 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005642 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005643
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005644 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005645 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005646 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005647 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005648 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005649 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005650 }
5651 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005652 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005653 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005654 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005655 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005656 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005657
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005658 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005659 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005660 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005661 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005662 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005663 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005664 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005665 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005666
5667 if (fd_in[0] >= 0)
5668 close(fd_in[0]);
5669 if (fd_in[1] >= 0)
5670 close(fd_in[1]);
5671 if (fd_out[0] >= 0)
5672 close(fd_out[0]);
5673 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005674 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005675 if (fd_err[0] >= 0)
5676 close(fd_err[0]);
5677 if (fd_err[1] >= 0)
5678 close(fd_err[1]);
5679 if (pty_master_fd >= 0)
5680 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005681 close(pty_master_fd); /* not used in the child */
5682 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005683 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005684
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005685 if (null_fd >= 0)
5686 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005687
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005688 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5689 _exit(EXEC_FAILED);
5690
Bram Moolenaar835dc632016-02-07 14:27:38 +01005691 /* See above for type of argv. */
5692 execvp(argv[0], argv);
5693
Bram Moolenaar4694a172016-04-21 14:05:23 +02005694 if (stderr_works)
5695 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005696# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005697 /* calling free_all_mem() here causes problems. Ignore valgrind
5698 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005699# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005700 _exit(EXEC_FAILED); /* exec failed, return failure code */
5701 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702
5703 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005704 UNBLOCK_SIGNALS(&curset);
5705
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706 job->jv_pid = pid;
5707 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005708 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005709
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005710 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005711 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005712 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005713 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005714 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005715 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005716 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005717 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005718 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005719 if (channel != NULL)
5720 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005721 int in_fd = use_file_for_in || use_null_for_in
5722 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5723 int out_fd = use_file_for_out || use_null_for_out
5724 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005725 /* When using pty_master_fd only set it for stdout, do not duplicate it
5726 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005727 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005728 ? INVALID_FD
5729 : fd_err[0] >= 0
5730 ? fd_err[0]
5731 : (out_fd == pty_master_fd
5732 ? INVALID_FD
5733 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005734
5735 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005736 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005737 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005738 else
5739 {
5740 if (fd_in[1] >= 0)
5741 close(fd_in[1]);
5742 if (fd_out[0] >= 0)
5743 close(fd_out[0]);
5744 if (fd_err[0] >= 0)
5745 close(fd_err[0]);
5746 if (pty_master_fd >= 0)
5747 close(pty_master_fd);
5748 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005749
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005750 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 return;
5752
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005753failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005754 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005755 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005756 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005757 if (fd_in[1] >= 0)
5758 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005759 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005760 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005761 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005762 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005763 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005764 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005765 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005766 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005767 if (pty_master_fd >= 0)
5768 close(pty_master_fd);
5769 if (pty_slave_fd >= 0)
5770 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005771}
5772
5773 char *
5774mch_job_status(job_T *job)
5775{
5776# ifdef HAVE_UNION_WAIT
5777 union wait status;
5778# else
5779 int status = -1;
5780# endif
5781 pid_t wait_pid = 0;
5782
5783# ifdef __NeXT__
5784 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5785# else
5786 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5787# endif
5788 if (wait_pid == -1)
5789 {
5790 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005791 if (job->jv_status < JOB_ENDED)
5792 ch_log(job->jv_channel, "Job no longer exists: %s",
5793 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005794 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005795 }
5796 if (wait_pid == 0)
5797 return "run";
5798 if (WIFEXITED(status))
5799 {
5800 /* LINTED avoid "bitwise operation on signed value" */
5801 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005802 if (job->jv_status < JOB_ENDED)
5803 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005804 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005805 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005806 if (WIFSIGNALED(status))
5807 {
5808 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005809 if (job->jv_status < JOB_ENDED)
5810 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005811 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005812 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005813 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005814
5815return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005816 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005817 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005818 return "dead";
5819}
5820
5821 job_T *
5822mch_detect_ended_job(job_T *job_list)
5823{
5824# ifdef HAVE_UNION_WAIT
5825 union wait status;
5826# else
5827 int status = -1;
5828# endif
5829 pid_t wait_pid = 0;
5830 job_T *job;
5831
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005832# ifndef USE_SYSTEM
5833 /* Do not do this when waiting for a shell command to finish, we would get
5834 * the exit value here (and discard it), the exit value obtained there
5835 * would then be wrong. */
5836 if (dont_check_job_ended > 0)
5837 return NULL;
5838# endif
5839
Bram Moolenaar97792de2016-10-15 18:36:49 +02005840# ifdef __NeXT__
5841 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5842# else
5843 wait_pid = waitpid(-1, &status, WNOHANG);
5844# endif
5845 if (wait_pid <= 0)
5846 /* no process ended */
5847 return NULL;
5848 for (job = job_list; job != NULL; job = job->jv_next)
5849 {
5850 if (job->jv_pid == wait_pid)
5851 {
5852 if (WIFEXITED(status))
5853 /* LINTED avoid "bitwise operation on signed value" */
5854 job->jv_exitval = WEXITSTATUS(status);
5855 else if (WIFSIGNALED(status))
5856 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005857 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005858 {
5859 ch_log(job->jv_channel, "Job ended");
5860 job->jv_status = JOB_ENDED;
5861 }
5862 return job;
5863 }
5864 }
5865 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866}
5867
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005868/*
5869 * Send a (deadly) signal to "job".
5870 * Return FAIL if "how" is not a valid name.
5871 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005872 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005873mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005874{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005875 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005876
Bram Moolenaar923d9262016-02-25 20:56:01 +01005877 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005879 else if (STRCMP(how, "hup") == 0)
5880 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005881 else if (STRCMP(how, "quit") == 0)
5882 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005883 else if (STRCMP(how, "int") == 0)
5884 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005885 else if (STRCMP(how, "kill") == 0)
5886 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005887#ifdef SIGWINCH
5888 else if (STRCMP(how, "winch") == 0)
5889 sig = SIGWINCH;
5890#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005891 else if (isdigit(*how))
5892 sig = atoi((char *)how);
5893 else
5894 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005895
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005896 // Never kill ourselves!
5897 if (job->jv_pid != 0)
5898 {
5899 // TODO: have an option to only kill the process, not the group?
5900 kill(-job->jv_pid, sig);
5901 kill(job->jv_pid, sig);
5902 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005903
Bram Moolenaar835dc632016-02-07 14:27:38 +01005904 return OK;
5905}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005906
5907/*
5908 * Clear the data related to "job".
5909 */
5910 void
5911mch_clear_job(job_T *job)
5912{
5913 /* call waitpid because child process may become zombie */
5914# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005915 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005916# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005917 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005918# endif
5919}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005920#endif
5921
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005922#if defined(FEAT_TERMINAL) || defined(PROTO)
5923 int
5924mch_create_pty_channel(job_T *job, jobopt_T *options)
5925{
5926 int pty_master_fd = -1;
5927 int pty_slave_fd = -1;
5928 channel_T *channel;
5929
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005930 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5931 if (job->jv_tty_out != NULL)
5932 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005933 close(pty_slave_fd);
5934
5935 channel = add_channel();
5936 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005937 {
5938 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005939 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005940 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005941 if (job->jv_tty_out != NULL)
5942 ch_log(channel, "using pty %s on fd %d",
5943 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005944 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5945 channel->ch_keep_open = TRUE;
5946
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005947 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5948 * it only needs to be read once. */
5949 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005950 channel_set_job(channel, job, options);
5951 return OK;
5952}
5953#endif
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955/*
5956 * Check for CTRL-C typed by reading all available characters.
5957 * In cooked mode we should get SIGINT, no need to check.
5958 */
5959 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005960mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005962 if ((curr_tmode == TMODE_RAW || force)
5963 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 fill_input_buf(FALSE);
5965}
5966
5967/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005968 * Wait "msec" msec until a character is available from the mouse, keyboard,
5969 * from inbuf[].
5970 * "msec" == -1 will block forever.
5971 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005972 * When "ignore_input" is TRUE even check for pending input when input is
5973 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005974 * "interrupted" (if not NULL) is set to TRUE when no character is available
5975 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005976 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005977 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 */
5979 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005980WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005982#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005983 return ui_wait_for_chars_or_timer(
5984 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005985#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005986 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005987#endif
5988}
5989
5990/*
5991 * Wait "msec" msec until a character is available from the mouse or keyboard
5992 * or from inbuf[].
5993 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005994 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005995 * "interrupted" (if not NULL) is set to TRUE when no character is available
5996 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005997 * When a GUI is being used, this will never get called -- webb
5998 */
5999 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006000WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006001{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#ifdef FEAT_MOUSE_GPM
6003 int gpm_process_wanted;
6004#endif
6005#ifdef FEAT_XCLIPBOARD
6006 int rest;
6007#endif
6008 int avail;
6009
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006010 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 return 1;
6012
6013#if defined(FEAT_MOUSE_DEC)
6014 /* May need to query the mouse position. */
6015 if (WantQueryMouse)
6016 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006017 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
6019 }
6020#endif
6021
6022 /*
6023 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6024 * events. This is a bit complicated, because they might both be defined.
6025 */
6026#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6027# ifdef FEAT_XCLIPBOARD
6028 rest = 0;
6029 if (do_xterm_trace())
6030 rest = msec;
6031# endif
6032 do
6033 {
6034# ifdef FEAT_XCLIPBOARD
6035 if (rest != 0)
6036 {
6037 msec = XT_TRACE_DELAY;
6038 if (rest >= 0 && rest < XT_TRACE_DELAY)
6039 msec = rest;
6040 if (rest >= 0)
6041 rest -= msec;
6042 }
6043# endif
6044# ifdef FEAT_MOUSE_GPM
6045 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006046 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006047 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006049 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050# endif
6051 if (!avail)
6052 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006053 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 return 1;
6055# ifdef FEAT_XCLIPBOARD
6056 if (rest == 0 || !do_xterm_trace())
6057# endif
6058 break;
6059 }
6060 }
6061 while (FALSE
6062# ifdef FEAT_MOUSE_GPM
6063 || (gpm_process_wanted && mch_gpm_process() == 0)
6064# endif
6065# ifdef FEAT_XCLIPBOARD
6066 || (!avail && rest != 0)
6067# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006068 )
6069 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006072 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
6074 return avail;
6075}
6076
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006077#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078/*
6079 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006080 * "msec" == 0 will check for characters once.
6081 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006084 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006085 * "interrupted" (if not NULL) is set to TRUE when no character is available
6086 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088#if defined(__BEOS__)
6089 int
6090#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006091 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006093RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006096 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006097#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 static int busy = FALSE;
6099
6100 /* May retry getting characters after an event was handled. */
6101# define MAY_LOOP
6102
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006103# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 /* Remember at what time we started, so that we know how much longer we
6105 * should wait after being interrupted. */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006106 long start_msec = msec;
6107 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006109 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006110 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111# endif
6112
6113 /* Handle being called recursively. This may happen for the session
6114 * manager stuff, it may save the file, which does a breakcheck. */
6115 if (busy)
6116 return 0;
6117#endif
6118
6119#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006120 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121#endif
6122 {
6123#ifdef MAY_LOOP
6124 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006125# ifdef FEAT_MZSCHEME
6126 int mzquantum_used = FALSE;
6127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128#endif
6129#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006130 /* each channel may use in, out and err */
6131 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 int nfd;
6133# ifdef FEAT_XCLIPBOARD
6134 int xterm_idx = -1;
6135# endif
6136# ifdef FEAT_MOUSE_GPM
6137 int gpm_idx = -1;
6138# endif
6139# ifdef USE_XSMP
6140 int xsmp_idx = -1;
6141# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006142 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006144# ifdef FEAT_MZSCHEME
6145 mzvim_check_threads();
6146 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6147 {
6148 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6149 mzquantum_used = TRUE;
6150 }
6151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 fds[0].fd = fd;
6153 fds[0].events = POLLIN;
6154 nfd = 1;
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006157 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (xterm_Shell != (Widget)0)
6159 {
6160 xterm_idx = nfd;
6161 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6162 fds[nfd].events = POLLIN;
6163 nfd++;
6164 }
6165# endif
6166# ifdef FEAT_MOUSE_GPM
6167 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6168 {
6169 gpm_idx = nfd;
6170 fds[nfd].fd = gpm_fd;
6171 fds[nfd].events = POLLIN;
6172 nfd++;
6173 }
6174# endif
6175# ifdef USE_XSMP
6176 if (xsmp_icefd != -1)
6177 {
6178 xsmp_idx = nfd;
6179 fds[nfd].fd = xsmp_icefd;
6180 fds[nfd].events = POLLIN;
6181 nfd++;
6182 }
6183# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006184#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006185 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006186#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006187 if (interrupted != NULL)
6188 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006190 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006191
6192 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006193 if (result == 0 && interrupted != NULL && ret > 0)
6194 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006195
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006196# ifdef FEAT_MZSCHEME
6197 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006198 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006199 finished = FALSE;
6200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202# ifdef FEAT_XCLIPBOARD
6203 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6204 {
6205 xterm_update(); /* Maybe we should hand out clipboard */
6206 if (--ret == 0 && !input_available())
6207 /* Try again */
6208 finished = FALSE;
6209 }
6210# endif
6211# ifdef FEAT_MOUSE_GPM
6212 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6213 {
6214 *check_for_gpm = 1;
6215 }
6216# endif
6217# ifdef USE_XSMP
6218 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6219 {
6220 if (fds[xsmp_idx].revents & POLLIN)
6221 {
6222 busy = TRUE;
6223 xsmp_handle_requests();
6224 busy = FALSE;
6225 }
6226 else if (fds[xsmp_idx].revents & POLLHUP)
6227 {
6228 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006229 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 xsmp_close();
6231 }
6232 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006233 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
6235# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006236#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006237 /* also call when ret == 0, we may be polling a keep-open channel */
6238 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006239 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#else /* HAVE_SELECT */
6243
6244 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006245 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006246 // These are static because they can take 8 Kbyte each and cause the
6247 // signal stack to run out with -O3.
6248 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006250 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006252# ifdef FEAT_MZSCHEME
6253 mzvim_check_threads();
6254 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6255 {
6256 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6257 mzquantum_used = TRUE;
6258 }
6259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006261 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006263 tv.tv_sec = towait / 1000;
6264 tv.tv_usec = (towait % 1000) * (1000000/1000);
6265 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006267 else
6268 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 /*
6271 * Select on ready for reading and exceptional condition (end of file).
6272 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006273select_eintr:
6274 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006275 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 FD_ZERO(&efds);
6277 FD_SET(fd, &rfds);
6278# if !defined(__QNX__) && !defined(__CYGWIN32__)
6279 /* For QNX select() always returns 1 if this is set. Why? */
6280 FD_SET(fd, &efds);
6281# endif
6282 maxfd = fd;
6283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006285 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (xterm_Shell != (Widget)0)
6287 {
6288 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6289 if (maxfd < ConnectionNumber(xterm_dpy))
6290 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006291
6292 /* An event may have already been read but not handled. In
6293 * particulary, XFlush may cause this. */
6294 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 }
6296# endif
6297# ifdef FEAT_MOUSE_GPM
6298 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6299 {
6300 FD_SET(gpm_fd, &rfds);
6301 FD_SET(gpm_fd, &efds);
6302 if (maxfd < gpm_fd)
6303 maxfd = gpm_fd;
6304 }
6305# endif
6306# ifdef USE_XSMP
6307 if (xsmp_icefd != -1)
6308 {
6309 FD_SET(xsmp_icefd, &rfds);
6310 FD_SET(xsmp_icefd, &efds);
6311 if (maxfd < xsmp_icefd)
6312 maxfd = xsmp_icefd;
6313 }
6314# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006315# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006316 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006317# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006318 if (interrupted != NULL)
6319 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
Bram Moolenaar643b6142018-09-12 20:29:09 +02006321 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6322 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006323 result = ret > 0 && FD_ISSET(fd, &rfds);
6324 if (result)
6325 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006326 else if (interrupted != NULL && ret > 0)
6327 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006328
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006329# ifdef EINTR
6330 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006331 {
6332 /* Check whether window has been resized, EINTR may be caused by
6333 * SIGWINCH. */
6334 if (do_resize)
6335 handle_resize();
6336
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006337 /* Interrupted by a signal, need to try again. We ignore msec
6338 * here, because we do want to check even after a timeout if
6339 * characters are available. Needed for reading output of an
6340 * external command after the process has finished. */
6341 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006342 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006343# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006344# ifdef __TANDEM
6345 if (ret == -1 && errno == ENOTSUP)
6346 {
6347 FD_ZERO(&rfds);
6348 FD_ZERO(&efds);
6349 ret = 0;
6350 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006351# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006352# ifdef FEAT_MZSCHEME
6353 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006354 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006355 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356# endif
6357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358# ifdef FEAT_XCLIPBOARD
6359 if (ret > 0 && xterm_Shell != (Widget)0
6360 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6361 {
6362 xterm_update(); /* Maybe we should hand out clipboard */
6363 /* continue looping when we only got the X event and the input
6364 * buffer is empty */
6365 if (--ret == 0 && !input_available())
6366 {
6367 /* Try again */
6368 finished = FALSE;
6369 }
6370 }
6371# endif
6372# ifdef FEAT_MOUSE_GPM
6373 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6374 {
6375 if (FD_ISSET(gpm_fd, &efds))
6376 gpm_close();
6377 else if (FD_ISSET(gpm_fd, &rfds))
6378 *check_for_gpm = 1;
6379 }
6380# endif
6381# ifdef USE_XSMP
6382 if (ret > 0 && xsmp_icefd != -1)
6383 {
6384 if (FD_ISSET(xsmp_icefd, &efds))
6385 {
6386 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006387 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 xsmp_close();
6389 if (--ret == 0)
6390 finished = FALSE; /* keep going if event was only one */
6391 }
6392 else if (FD_ISSET(xsmp_icefd, &rfds))
6393 {
6394 busy = TRUE;
6395 xsmp_handle_requests();
6396 busy = FALSE;
6397 if (--ret == 0)
6398 finished = FALSE; /* keep going if event was only one */
6399 }
6400 }
6401# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006402#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006403 /* also call when ret == 0, we may be polling a keep-open channel */
6404 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006405 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408#endif /* HAVE_SELECT */
6409
6410#ifdef MAY_LOOP
6411 if (finished || msec == 0)
6412 break;
6413
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006414# ifdef FEAT_CLIENTSERVER
6415 if (server_waiting())
6416 break;
6417# endif
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 /* We're going to loop around again, find out for how long */
6420 if (msec > 0)
6421 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006422# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006424 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425# else
6426 /* Guess we got interrupted halfway. */
6427 msec = msec / 2;
6428# endif
6429 if (msec <= 0)
6430 break; /* waited long enough */
6431 }
6432#endif
6433 }
6434
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006435 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436}
6437
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006440 * Expand a path into all matching files and/or directories. Handles "*",
6441 * "?", "[a-z]", "**", etc.
6442 * "path" has backslashes before chars that are not to be expanded.
6443 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 */
6445 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006446mch_expandpath(
6447 garray_T *gap,
6448 char_u *path,
6449 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
Bram Moolenaar02743632005-07-25 20:42:36 +00006451 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452}
6453#endif
6454
6455/*
6456 * mch_expand_wildcards() - this code does wild-card pattern matching using
6457 * the shell
6458 *
6459 * return OK for success, FAIL for error (you may lose some memory) and put
6460 * an error message in *file.
6461 *
6462 * num_pat is number of input patterns
6463 * pat is array of pointers to input patterns
6464 * num_file is pointer to number of matched file names
6465 * file is pointer to array of pointers to matched file names
6466 */
6467
6468#ifndef SEEK_SET
6469# define SEEK_SET 0
6470#endif
6471#ifndef SEEK_END
6472# define SEEK_END 2
6473#endif
6474
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006475#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006478mch_expand_wildcards(
6479 int num_pat,
6480 char_u **pat,
6481 int *num_file,
6482 char_u ***file,
6483 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 int i;
6486 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006487 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 char_u *p;
6489 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
Bram Moolenaarc7247912008-01-13 12:54:11 +00006491 /*
6492 * This is the non-OS/2 implementation (really Unix).
6493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 int j;
6495 char_u *tempname;
6496 char_u *command;
6497 FILE *fd;
6498 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006499#define STYLE_ECHO 0 /* use "echo", the default */
6500#define STYLE_GLOB 1 /* use "glob", for csh */
6501#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6502#define STYLE_PRINT 3 /* use "print -N", for zsh */
6503#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6504 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 int shell_style = STYLE_ECHO;
6506 int check_spaces;
6507 static int did_find_nul = FALSE;
6508 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006509 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006510 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 *num_file = 0; /* default: no files found */
6513 *file = NULL;
6514
6515 /*
6516 * If there are no wildcards, just copy the names to allocated memory.
6517 * Saves a lot of time, because we don't have to start a new shell.
6518 */
6519 if (!have_wildcard(num_pat, pat))
6520 return save_patterns(num_pat, pat, num_file, file);
6521
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006522# ifdef HAVE_SANDBOX
6523 /* Don't allow any shell command in the sandbox. */
6524 if (sandbox != 0 && check_secure())
6525 return FAIL;
6526# endif
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 /*
6529 * Don't allow the use of backticks in secure and restricted mode.
6530 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006531 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 for (i = 0; i < num_pat; ++i)
6533 if (vim_strchr(pat[i], '`') != NULL
6534 && (check_restricted() || check_secure()))
6535 return FAIL;
6536
6537 /*
6538 * get a name for the temp file
6539 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006540 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006542 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 return FAIL;
6544 }
6545
6546 /*
6547 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006548 * file.
6549 * STYLE_BT: NL separated
6550 * If expanding `cmd` execute it directly.
6551 * STYLE_GLOB: NUL separated
6552 * If we use *csh, "glob" will work better than "echo".
6553 * STYLE_PRINT: NL or NUL separated
6554 * If we use *zsh, "print -N" will work better than "glob".
6555 * STYLE_VIMGLOB: NL separated
6556 * If we use *sh*, we define "vimglob()".
6557 * STYLE_ECHO: space separated.
6558 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 */
6560 if (num_pat == 1 && *pat[0] == '`'
6561 && (len = STRLEN(pat[0])) > 2
6562 && *(pat[0] + len - 1) == '`')
6563 shell_style = STYLE_BT;
6564 else if ((len = STRLEN(p_sh)) >= 3)
6565 {
6566 if (STRCMP(p_sh + len - 3, "csh") == 0)
6567 shell_style = STYLE_GLOB;
6568 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6569 shell_style = STYLE_PRINT;
6570 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006571 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6572 "sh") != NULL)
6573 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaarc7247912008-01-13 12:54:11 +00006575 /* Compute the length of the command. We need 2 extra bytes: for the
6576 * optional '&' and for the NUL.
6577 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006579 if (shell_style == STYLE_VIMGLOB)
6580 len += STRLEN(sh_vimglob_func);
6581
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006582 for (i = 0; i < num_pat; ++i)
6583 {
6584 /* Count the length of the patterns in the same way as they are put in
6585 * "command" below. */
6586#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006588#else
6589 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006590 for (j = 0; pat[i][j] != NUL; ++j)
6591 {
6592 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6593 ++len; /* may add a backslash */
6594 ++len;
6595 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006596#endif
6597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 command = alloc(len);
6599 if (command == NULL)
6600 {
6601 /* out of memory */
6602 vim_free(tempname);
6603 return FAIL;
6604 }
6605
6606 /*
6607 * Build the shell command:
6608 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6609 * recognizes this).
6610 * - Add the shell command to print the expanded names.
6611 * - Add the temp file name.
6612 * - Add the file name patterns.
6613 */
6614 if (shell_style == STYLE_BT)
6615 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006616 /* change `command; command& ` to (command; command ) */
6617 STRCPY(command, "(");
6618 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006620 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006621 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 --p;
6623 if (*p == '&') /* remove trailing '&' */
6624 {
6625 ampersent = TRUE;
6626 *p = ' ';
6627 }
6628 STRCAT(command, ">");
6629 }
6630 else
6631 {
6632 if (flags & EW_NOTFOUND)
6633 STRCPY(command, "set nonomatch; ");
6634 else
6635 STRCPY(command, "unset nonomatch; ");
6636 if (shell_style == STYLE_GLOB)
6637 STRCAT(command, "glob >");
6638 else if (shell_style == STYLE_PRINT)
6639 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006640 else if (shell_style == STYLE_VIMGLOB)
6641 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 else
6643 STRCAT(command, "echo >");
6644 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (shell_style != STYLE_BT)
6649 for (i = 0; i < num_pat; ++i)
6650 {
6651 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006652 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006653 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654#ifdef USE_SYSTEM
6655 STRCAT(command, " \"");
6656 STRCAT(command, pat[i]);
6657 STRCAT(command, "\"");
6658#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006659 int intick = FALSE;
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 p = command + STRLEN(command);
6662 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006663 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006664 {
6665 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006666 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006667 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6668 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006669 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006670 * backslash inside backticks, before a special character
6671 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006672 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006673 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6674 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006675 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006676 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006677 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006678 else if (!intick
6679 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6680 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006681 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006682 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6683 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006684 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006685
6686 /* Copy one character. */
6687 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 *p = NUL;
6690#endif
6691 }
6692 if (flags & EW_SILENT)
6693 show_shell_mess = FALSE;
6694 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006695 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 /*
6698 * Using zsh -G: If a pattern has no matches, it is just deleted from
6699 * the argument list, otherwise zsh gives an error message and doesn't
6700 * expand any other pattern.
6701 */
6702 if (shell_style == STYLE_PRINT)
6703 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6704
6705 /*
6706 * If we use -f then shell variables set in .cshrc won't get expanded.
6707 * vi can do it, so we will too, but it is only necessary if there is a "$"
6708 * in one of the patterns, otherwise we can still use the fast option.
6709 */
6710 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6711 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6712
6713 /*
6714 * execute the shell command
6715 */
6716 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6717
6718 /* When running in the background, give it some time to create the temp
6719 * file, but don't wait for it to finish. */
6720 if (ampersent)
6721 mch_delay(10L, TRUE);
6722
6723 extra_shell_arg = NULL; /* cleanup */
6724 show_shell_mess = TRUE;
6725 vim_free(command);
6726
Bram Moolenaarc7247912008-01-13 12:54:11 +00006727 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
6729 mch_remove(tempname);
6730 vim_free(tempname);
6731 /*
6732 * With interactive completion, the error message is not printed.
6733 * However with USE_SYSTEM, I don't know how to turn off error messages
6734 * from the shell, so screen may still get messed up -- webb.
6735 */
6736#ifndef USE_SYSTEM
6737 if (!(flags & EW_SILENT))
6738#endif
6739 {
6740 redraw_later_clear(); /* probably messed up screen */
6741 msg_putchar('\n'); /* clear bottom line quickly */
6742 cmdline_row = Rows - 1; /* continue on last line */
6743#ifdef USE_SYSTEM
6744 if (!(flags & EW_SILENT))
6745#endif
6746 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006747 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 msg_start(); /* don't overwrite this message */
6749 }
6750 }
6751 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6752 * EW_NOTFOUND is given */
6753 if (shell_style == STYLE_BT)
6754 return FAIL;
6755 goto notfound;
6756 }
6757
6758 /*
6759 * read the names from the file into memory
6760 */
6761 fd = fopen((char *)tempname, READBIN);
6762 if (fd == NULL)
6763 {
6764 /* Something went wrong, perhaps a file name with a special char. */
6765 if (!(flags & EW_SILENT))
6766 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006767 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 msg_start(); /* don't overwrite this message */
6769 }
6770 vim_free(tempname);
6771 goto notfound;
6772 }
6773 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006774 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006776 if (llen < 0)
6777 /* just in case ftell() would fail */
6778 buffer = NULL;
6779 else
6780 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (buffer == NULL)
6782 {
6783 /* out of memory */
6784 mch_remove(tempname);
6785 vim_free(tempname);
6786 fclose(fd);
6787 return FAIL;
6788 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006789 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 i = fread((char *)buffer, 1, len, fd);
6791 fclose(fd);
6792 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006793 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
6795 /* unexpected read error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006796 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 vim_free(tempname);
6798 vim_free(buffer);
6799 return FAIL;
6800 }
6801 vim_free(tempname);
6802
Bram Moolenaarc7247912008-01-13 12:54:11 +00006803# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6805 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006806 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6808 *p++ = buffer[i];
6809 len = p - buffer;
6810# endif
6811
6812
6813 /* file names are separated with Space */
6814 if (shell_style == STYLE_ECHO)
6815 {
6816 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6817 p = buffer;
6818 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6819 {
6820 while (*p != ' ' && *p != '\n')
6821 ++p;
6822 p = skipwhite(p); /* skip to next entry */
6823 }
6824 }
6825 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006826 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
6828 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6829 p = buffer;
6830 for (i = 0; *p != NUL; ++i) /* count number of entries */
6831 {
6832 while (*p != '\n' && *p != NUL)
6833 ++p;
6834 if (*p != NUL)
6835 ++p;
6836 p = skipwhite(p); /* skip leading white space */
6837 }
6838 }
6839 /* file names are separated with NUL */
6840 else
6841 {
6842 /*
6843 * Some versions of zsh use spaces instead of NULs to separate
6844 * results. Only do this when there is no NUL before the end of the
6845 * buffer, otherwise we would never be able to use file names with
6846 * embedded spaces when zsh does use NULs.
6847 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6848 * don't check for spaces again.
6849 */
6850 check_spaces = FALSE;
6851 if (shell_style == STYLE_PRINT && !did_find_nul)
6852 {
6853 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006854 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006855 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 did_find_nul = TRUE;
6857 else
6858 check_spaces = TRUE;
6859 }
6860
6861 /*
6862 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6863 * already is one, for STYLE_GLOB it needs to be added.
6864 */
6865 if (len && buffer[len - 1] == NUL)
6866 --len;
6867 else
6868 buffer[len] = NUL;
6869 i = 0;
6870 for (p = buffer; p < buffer + len; ++p)
6871 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6872 {
6873 ++i;
6874 *p = NUL;
6875 }
6876 if (len)
6877 ++i; /* count last entry */
6878 }
6879 if (i == 0)
6880 {
6881 /*
6882 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6883 * /bin/sh will happily expand it to nothing rather than returning an
6884 * error; and hey, it's good to check anyway -- webb.
6885 */
6886 vim_free(buffer);
6887 goto notfound;
6888 }
6889 *num_file = i;
6890 *file = (char_u **)alloc(sizeof(char_u *) * i);
6891 if (*file == NULL)
6892 {
6893 /* out of memory */
6894 vim_free(buffer);
6895 return FAIL;
6896 }
6897
6898 /*
6899 * Isolate the individual file names.
6900 */
6901 p = buffer;
6902 for (i = 0; i < *num_file; ++i)
6903 {
6904 (*file)[i] = p;
6905 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006906 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6907 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006909 while (!(shell_style == STYLE_ECHO && *p == ' ')
6910 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 ++p;
6912 if (p == buffer + len) /* last entry */
6913 *p = NUL;
6914 else
6915 {
6916 *p++ = NUL;
6917 p = skipwhite(p); /* skip to next entry */
6918 }
6919 }
6920 else /* NUL separates */
6921 {
6922 while (*p && p < buffer + len) /* skip entry */
6923 ++p;
6924 ++p; /* skip NUL */
6925 }
6926 }
6927
6928 /*
6929 * Move the file names to allocated memory.
6930 */
6931 for (j = 0, i = 0; i < *num_file; ++i)
6932 {
6933 /* Require the files to exist. Helps when using /bin/sh */
6934 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6935 continue;
6936
6937 /* check if this entry should be included */
6938 dir = (mch_isdir((*file)[i]));
6939 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6940 continue;
6941
Bram Moolenaara2031822006-03-07 22:29:51 +00006942 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006943 if (!dir && (flags & EW_EXEC)
6944 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006945 continue;
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6948 if (p)
6949 {
6950 STRCPY(p, (*file)[i]);
6951 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006952 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 (*file)[j++] = p;
6954 }
6955 }
6956 vim_free(buffer);
6957 *num_file = j;
6958
6959 if (*num_file == 0) /* rejected all entries */
6960 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006961 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 goto notfound;
6963 }
6964
6965 return OK;
6966
6967notfound:
6968 if (flags & EW_NOTFOUND)
6969 return save_patterns(num_pat, pat, num_file, file);
6970 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971}
6972
6973#endif /* VMS */
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006976save_patterns(
6977 int num_pat,
6978 char_u **pat,
6979 int *num_file,
6980 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006983 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6986 if (*file == NULL)
6987 return FAIL;
6988 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006989 {
6990 s = vim_strsave(pat[i]);
6991 if (s != NULL)
6992 /* Be compatible with expand_filename(): halve the number of
6993 * backslashes. */
6994 backslash_halve(s);
6995 (*file)[i] = s;
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 *num_file = num_pat;
6998 return OK;
6999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001/*
7002 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7003 * expand.
7004 */
7005 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007006mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007008 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (*p == '\\' && p[1] != NUL)
7011 ++p;
7012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (vim_strchr((char_u *)
7014#ifdef VMS
7015 "*?%"
7016#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#endif
7019 , *p) != NULL)
7020 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022 return FALSE;
7023}
7024
7025/*
7026 * Return TRUE if the string "p" contains a wildcard.
7027 * Don't recognize '~' at the end as a wildcard.
7028 */
7029 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007030mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007032 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (*p == '\\' && p[1] != NUL)
7035 ++p;
7036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 if (vim_strchr((char_u *)
7038#ifdef VMS
7039 "*?%$"
7040#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042#endif
7043 , *p) != NULL
7044 || (*p == '~' && p[1] != NUL))
7045 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 return FALSE;
7048}
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007051have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 int i;
7054
7055 for (i = 0; i < num; i++)
7056 if (mch_has_wildcard(file[i]))
7057 return 1;
7058 return 0;
7059}
7060
7061 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007062have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 int i;
7065
7066 for (i = 0; i < num; i++)
7067 if (vim_strchr(file[i], '$') != NULL)
7068 return TRUE;
7069 return FALSE;
7070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007072#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073/*
7074 * Scaled-down version of rename(), which is missing in Xenix.
7075 * This version can only move regular files and will fail if the
7076 * destination exists.
7077 */
7078 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007079mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
7081 struct stat st;
7082
7083 if (stat(dest, &st) >= 0) /* fail if destination exists */
7084 return -1;
7085 if (link(src, dest) != 0) /* link file to new name */
7086 return -1;
7087 if (mch_remove(src) == 0) /* delete link to old name */
7088 return 0;
7089 return -1;
7090}
7091#endif /* !HAVE_RENAME */
7092
7093#ifdef FEAT_MOUSE_GPM
7094/*
7095 * Initializes connection with gpm (if it isn't already opened)
7096 * Return 1 if succeeded (or connection already opened), 0 if failed
7097 */
7098 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007099gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7102
7103 if (!gpm_flag)
7104 {
7105 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7106 gpm_connect.defaultMask = ~GPM_HARD;
7107 /* Default handling for mouse move*/
7108 gpm_connect.minMod = 0; /* Handle any modifier keys */
7109 gpm_connect.maxMod = 0xffff;
7110 if (Gpm_Open(&gpm_connect, 0) > 0)
7111 {
7112 /* gpm library tries to handling TSTP causes
7113 * problems. Anyways, we close connection to Gpm whenever
7114 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007115 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007117# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 return 1; /* succeed */
7121 }
7122 if (gpm_fd == -2)
7123 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7124 return 0;
7125 }
7126 return 1; /* already open */
7127}
7128
7129/*
7130 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 */
7132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007133gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
7135 if (gpm_flag && gpm_fd >= 0) /* if Open */
7136 Gpm_Close();
7137}
7138
7139/* Reads gpm event and adds special keys to input buf. Returns length of
7140 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007141 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 */
7143 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007144mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
7146 int button;
7147 static Gpm_Event gpm_event;
7148 char_u string[6];
7149 int_u vim_modifiers;
7150 int row,col;
7151 unsigned char buttons_mask;
7152 unsigned char gpm_modifiers;
7153 static unsigned char old_buttons = 0;
7154
7155 Gpm_GetEvent(&gpm_event);
7156
7157#ifdef FEAT_GUI
7158 /* Don't put events in the input queue now. */
7159 if (hold_gui_events)
7160 return 0;
7161#endif
7162
7163 row = gpm_event.y - 1;
7164 col = gpm_event.x - 1;
7165
7166 string[0] = ESC; /* Our termcode */
7167 string[1] = 'M';
7168 string[2] = 'G';
7169 switch (GPM_BARE_EVENTS(gpm_event.type))
7170 {
7171 case GPM_DRAG:
7172 string[3] = MOUSE_DRAG;
7173 break;
7174 case GPM_DOWN:
7175 buttons_mask = gpm_event.buttons & ~old_buttons;
7176 old_buttons = gpm_event.buttons;
7177 switch (buttons_mask)
7178 {
7179 case GPM_B_LEFT:
7180 button = MOUSE_LEFT;
7181 break;
7182 case GPM_B_MIDDLE:
7183 button = MOUSE_MIDDLE;
7184 break;
7185 case GPM_B_RIGHT:
7186 button = MOUSE_RIGHT;
7187 break;
7188 default:
7189 return 0;
7190 /*Don't know what to do. Can more than one button be
7191 * reported in one event? */
7192 }
7193 string[3] = (char_u)(button | 0x20);
7194 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7195 break;
7196 case GPM_UP:
7197 string[3] = MOUSE_RELEASE;
7198 old_buttons &= ~gpm_event.buttons;
7199 break;
7200 default:
7201 return 0;
7202 }
7203 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7204 gpm_modifiers = gpm_event.modifiers;
7205 vim_modifiers = 0x0;
7206 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7207 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7208 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7209 */
7210 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7211 vim_modifiers |= MOUSE_SHIFT;
7212
7213 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7214 vim_modifiers |= MOUSE_CTRL;
7215 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7216 vim_modifiers |= MOUSE_ALT;
7217 string[3] |= vim_modifiers;
7218 string[4] = (char_u)(col + ' ' + 1);
7219 string[5] = (char_u)(row + ' ' + 1);
7220 add_to_input_buf(string, 6);
7221 return 6;
7222}
7223#endif /* FEAT_MOUSE_GPM */
7224
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007225#ifdef FEAT_SYSMOUSE
7226/*
7227 * Initialize connection with sysmouse.
7228 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7229 * output, any sysmouse output than will be processed via sig_sysmouse().
7230 * Return OK if succeeded, FAIL if failed.
7231 */
7232 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007233sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007234{
7235 struct mouse_info mouse;
7236
7237 mouse.operation = MOUSE_MODE;
7238 mouse.u.mode.mode = 0;
7239 mouse.u.mode.signal = SIGUSR2;
7240 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7241 {
7242 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7243 mouse.operation = MOUSE_SHOW;
7244 ioctl(1, CONS_MOUSECTL, &mouse);
7245 return OK;
7246 }
7247 return FAIL;
7248}
7249
7250/*
7251 * Stop processing SIGUSR2 signals, and also make sure that
7252 * virtual console do not send us any sysmouse related signal.
7253 */
7254 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007255sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007256{
7257 struct mouse_info mouse;
7258
7259 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7260 mouse.operation = MOUSE_MODE;
7261 mouse.u.mode.mode = 0;
7262 mouse.u.mode.signal = 0;
7263 ioctl(1, CONS_MOUSECTL, &mouse);
7264}
7265
7266/*
7267 * Gets info from sysmouse and adds special keys to input buf.
7268 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007269 static RETSIGTYPE
7270sig_sysmouse SIGDEFARG(sigarg)
7271{
7272 struct mouse_info mouse;
7273 struct video_info video;
7274 char_u string[6];
7275 int row, col;
7276 int button;
7277 int buttons;
7278 static int oldbuttons = 0;
7279
7280#ifdef FEAT_GUI
7281 /* Don't put events in the input queue now. */
7282 if (hold_gui_events)
7283 return;
7284#endif
7285
7286 mouse.operation = MOUSE_GETINFO;
7287 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7288 && ioctl(1, FBIO_MODEINFO, &video) != -1
7289 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7290 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7291 {
7292 row = mouse.u.data.y / video.vi_cheight;
7293 col = mouse.u.data.x / video.vi_cwidth;
7294 buttons = mouse.u.data.buttons;
7295 string[0] = ESC; /* Our termcode */
7296 string[1] = 'M';
7297 string[2] = 'S';
7298 if (oldbuttons == buttons && buttons != 0)
7299 {
7300 button = MOUSE_DRAG;
7301 }
7302 else
7303 {
7304 switch (buttons)
7305 {
7306 case 0:
7307 button = MOUSE_RELEASE;
7308 break;
7309 case 1:
7310 button = MOUSE_LEFT;
7311 break;
7312 case 2:
7313 button = MOUSE_MIDDLE;
7314 break;
7315 case 4:
7316 button = MOUSE_RIGHT;
7317 break;
7318 default:
7319 return;
7320 }
7321 oldbuttons = buttons;
7322 }
7323 string[3] = (char_u)(button);
7324 string[4] = (char_u)(col + ' ' + 1);
7325 string[5] = (char_u)(row + ' ' + 1);
7326 add_to_input_buf(string, 6);
7327 }
7328 return;
7329}
7330#endif /* FEAT_SYSMOUSE */
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007333typedef char_u * (*STRPROCSTR)(char_u *);
7334typedef char_u * (*INTPROCSTR)(int);
7335typedef int (*STRPROCINT)(char_u *);
7336typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338/*
7339 * Call a DLL routine which takes either a string or int param
7340 * and returns an allocated string.
7341 */
7342 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007343mch_libcall(
7344 char_u *libname,
7345 char_u *funcname,
7346 char_u *argstring, /* NULL when using a argint */
7347 int argint,
7348 char_u **string_result,/* NULL when using number_result */
7349 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351# if defined(USE_DLOPEN)
7352 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007353 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354# else
7355 shl_t hinstLib;
7356# endif
7357 STRPROCSTR ProcAdd;
7358 INTPROCSTR ProcAddI;
7359 char_u *retval_str = NULL;
7360 int retval_int = 0;
7361 int success = FALSE;
7362
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007363 /*
7364 * Get a handle to the DLL module.
7365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007367 /* First clear any error, it's not cleared by the dlopen() call. */
7368 (void)dlerror();
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 hinstLib = dlopen((char *)libname, RTLD_LAZY
7371# ifdef RTLD_LOCAL
7372 | RTLD_LOCAL
7373# endif
7374 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007375 if (hinstLib == NULL)
7376 {
7377 /* "dlerr" must be used before dlclose() */
7378 dlerr = (char *)dlerror();
7379 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007380 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382# else
7383 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7384# endif
7385
7386 /* If the handle is valid, try to get the function address. */
7387 if (hinstLib != NULL)
7388 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007389# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 /*
7391 * Catch a crash when calling the library function. For example when
7392 * using a number where a string pointer is expected.
7393 */
7394 mch_startjmp();
7395 if (SETJMP(lc_jump_env) != 0)
7396 {
7397 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007398# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 mch_didjmp();
7402 }
7403 else
7404# endif
7405 {
7406 retval_str = NULL;
7407 retval_int = 0;
7408
7409 if (argstring != NULL)
7410 {
7411# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007412 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007413 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414# else
7415 if (shl_findsym(&hinstLib, (const char *)funcname,
7416 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7417 ProcAdd = NULL;
7418# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007419 if ((success = (ProcAdd != NULL
7420# if defined(USE_DLOPEN)
7421 && dlerr == NULL
7422# endif
7423 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
7425 if (string_result == NULL)
7426 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7427 else
7428 retval_str = (ProcAdd)(argstring);
7429 }
7430 }
7431 else
7432 {
7433# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007434 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007435 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436# else
7437 if (shl_findsym(&hinstLib, (const char *)funcname,
7438 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7439 ProcAddI = NULL;
7440# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007441 if ((success = (ProcAddI != NULL
7442# if defined(USE_DLOPEN)
7443 && dlerr == NULL
7444# endif
7445 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 if (string_result == NULL)
7448 retval_int = ((INTPROCINT)ProcAddI)(argint);
7449 else
7450 retval_str = (ProcAddI)(argint);
7451 }
7452 }
7453
7454 /* Save the string before we free the library. */
7455 /* Assume that a "1" or "-1" result is an illegal pointer. */
7456 if (string_result == NULL)
7457 *number_result = retval_int;
7458 else if (retval_str != NULL
7459 && retval_str != (char_u *)1
7460 && retval_str != (char_u *)-1)
7461 *string_result = vim_strsave(retval_str);
7462 }
7463
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007464# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 mch_endjmp();
7466# ifdef SIGHASARG
7467 if (lc_signal != 0)
7468 {
7469 int i;
7470
7471 /* try to find the name of this signal */
7472 for (i = 0; signal_info[i].sig != -1; i++)
7473 if (lc_signal == signal_info[i].sig)
7474 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477# endif
7478# endif
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007481 /* "dlerr" must be used before dlclose() */
7482 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007483 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007484
7485 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 (void)dlclose(hinstLib);
7487# else
7488 (void)shl_unload(hinstLib);
7489# endif
7490 }
7491
7492 if (!success)
7493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return FAIL;
7496 }
7497
7498 return OK;
7499}
7500#endif
7501
7502#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7503static int xterm_trace = -1; /* default: disabled */
7504static int xterm_button;
7505
7506/*
7507 * Setup a dummy window for X selections in a terminal.
7508 */
7509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007510setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 int z = 0;
7513 char *strp = "";
7514 Widget AppShell;
7515
7516 if (!x_connect_to_server())
7517 return;
7518
7519 open_app_context();
7520 if (app_context != NULL && xterm_Shell == (Widget)0)
7521 {
7522 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007523# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007525# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007526# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007527 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007530 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531# endif
7532
7533 /* Ignore X errors while opening the display */
7534 oldhandler = XSetErrorHandler(x_error_check);
7535
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007536# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 /* Ignore X IO errors while opening the display */
7538 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7539 mch_startjmp();
7540 if (SETJMP(lc_jump_env) != 0)
7541 {
7542 mch_didjmp();
7543 xterm_dpy = NULL;
7544 }
7545 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {
7548 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7549 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007550 if (xterm_dpy != NULL)
7551 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007552# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
7556
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007557# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 /* Now handle X IO errors normally. */
7559 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 /* Now handle X errors normally. */
7562 (void)XSetErrorHandler(oldhandler);
7563
7564 if (xterm_dpy == NULL)
7565 {
7566 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007567 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 return;
7569 }
7570
7571 /* Catch terminating error of the X server connection. */
7572 (void)XSetIOErrorHandler(x_IOerror_handler);
7573
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007574# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007576 {
7577 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007578 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007579 verbose_leave();
7580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581# endif
7582
7583 /* Create a Shell to make converters work. */
7584 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7585 applicationShellWidgetClass, xterm_dpy,
7586 NULL);
7587 if (AppShell == (Widget)0)
7588 return;
7589 xterm_Shell = XtVaCreatePopupShell("VIM",
7590 topLevelShellWidgetClass, AppShell,
7591 XtNmappedWhenManaged, 0,
7592 XtNwidth, 1,
7593 XtNheight, 1,
7594 NULL);
7595 if (xterm_Shell == (Widget)0)
7596 return;
7597
7598 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007599 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (x11_display == NULL)
7601 x11_display = xterm_dpy;
7602
7603 XtRealizeWidget(xterm_Shell);
7604 XSync(xterm_dpy, False);
7605 xterm_update();
7606 }
7607 if (xterm_Shell != (Widget)0)
7608 {
7609 clip_init(TRUE);
7610 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7611 x11_window = (Window)atol(strp);
7612 /* Check if $WINDOWID is valid. */
7613 if (test_x11_window(xterm_dpy) == FAIL)
7614 x11_window = 0;
7615 if (x11_window != 0)
7616 xterm_trace = 0;
7617 }
7618}
7619
7620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007621start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
7623 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7624 return;
7625 xterm_trace = 1;
7626 xterm_button = button;
7627 do_xterm_trace();
7628}
7629
7630
7631 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007632stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 if (xterm_trace < 0)
7635 return;
7636 xterm_trace = 0;
7637}
7638
7639/*
7640 * Query the xterm pointer and generate mouse termcodes if necessary
7641 * return TRUE if dragging is active, else FALSE
7642 */
7643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007644do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 Window root, child;
7647 int root_x, root_y;
7648 int win_x, win_y;
7649 int row, col;
7650 int_u mask_return;
7651 char_u buf[50];
7652 char_u *strp;
7653 long got_hints;
7654 static char_u *mouse_code;
7655 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7656 static int prev_row = 0, prev_col = 0;
7657 static XSizeHints xterm_hints;
7658
7659 if (xterm_trace <= 0)
7660 return FALSE;
7661
7662 if (xterm_trace == 1)
7663 {
7664 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007665 * have changed recently. */
7666 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7667 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 || xterm_hints.width_inc <= 1
7669 || xterm_hints.height_inc <= 1)
7670 {
7671 xterm_trace = -1; /* Not enough data -- disable tracing */
7672 return FALSE;
7673 }
7674
7675 /* Rely on the same mouse code for the duration of this */
7676 mouse_code = find_termcode(mouse_name);
7677 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007678 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 xterm_trace = 2;
7680
7681 /* Find the offset of the chars, there might be a scrollbar on the
7682 * left of the window and/or a menu on the top (eterm etc.) */
7683 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7684 &win_x, &win_y, &mask_return);
7685 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7686 - (xterm_hints.height_inc / 2);
7687 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7688 xterm_hints.y = 2;
7689 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7690 - (xterm_hints.width_inc / 2);
7691 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7692 xterm_hints.x = 2;
7693 return TRUE;
7694 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007695 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
7697 xterm_trace = 0;
7698 return FALSE;
7699 }
7700
7701 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7702 &win_x, &win_y, &mask_return);
7703
7704 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7705 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7706 if (row == prev_row && col == prev_col)
7707 return TRUE;
7708
7709 STRCPY(buf, mouse_code);
7710 strp = buf + STRLEN(buf);
7711 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7712 *strp++ = (char_u)(col + ' ' + 1);
7713 *strp++ = (char_u)(row + ' ' + 1);
7714 *strp = 0;
7715 add_to_input_buf(buf, STRLEN(buf));
7716
7717 prev_row = row;
7718 prev_col = col;
7719 return TRUE;
7720}
7721
7722# if defined(FEAT_GUI) || defined(PROTO)
7723/*
7724 * Destroy the display, window and app_context. Required for GTK.
7725 */
7726 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007727clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 if (xterm_Shell != (Widget)0)
7730 {
7731 XtDestroyWidget(xterm_Shell);
7732 xterm_Shell = (Widget)0;
7733 }
7734 if (xterm_dpy != NULL)
7735 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007736# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 /* Lesstif and Solaris crash here, lose some memory */
7738 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (x11_display == xterm_dpy)
7741 x11_display = NULL;
7742 xterm_dpy = NULL;
7743 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007744# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 if (app_context != (XtAppContext)NULL)
7746 {
7747 /* Lesstif and Solaris crash here, lose some memory */
7748 XtDestroyApplicationContext(app_context);
7749 app_context = (XtAppContext)NULL;
7750 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
7753# endif
7754
7755/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007756 * Catch up with GUI or X events.
7757 */
7758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007759clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007760{
7761# ifdef FEAT_GUI
7762 if (gui.in_use)
7763 gui_mch_update();
7764 else
7765# endif
7766 if (xterm_Shell != (Widget)0)
7767 xterm_update();
7768}
7769
7770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 * Catch up with any queued X events. This may put keyboard input into the
7772 * input buffer, call resize call-backs, trigger timers etc. If there is
7773 * nothing in the X event queue (& no timers pending), then we return
7774 * immediately.
7775 */
7776 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007777xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 XEvent event;
7780
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007781 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007783 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007785 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007786 break;
7787
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007788 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007789 {
7790 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007791 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007792#ifdef FEAT_CLIENTSERVER
7793 {
7794 XPropertyEvent *e = (XPropertyEvent *)&event;
7795
7796 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007798 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007801 XtDispatchEvent(&event);
7802 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007803 else
7804 {
7805 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007806 XtAppProcessEvent(app_context, mask);
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809}
7810
7811 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007812clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 if (xterm_Shell != (Widget)0)
7815 return clip_x11_own_selection(xterm_Shell, cbd);
7816 return FAIL;
7817}
7818
7819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007820clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 if (xterm_Shell != (Widget)0)
7823 clip_x11_lose_selection(xterm_Shell, cbd);
7824}
7825
7826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007827clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 if (xterm_Shell != (Widget)0)
7830 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7831}
7832
7833 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007834clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 clip_x11_set_selection(cbd);
7837}
7838#endif
7839
7840
7841#if defined(USE_XSMP) || defined(PROTO)
7842/*
7843 * Code for X Session Management Protocol.
7844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847/*
7848 * This is our chance to ask the user if they want to save,
7849 * or abort the logout
7850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007852xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854 cmdmod_T save_cmdmod;
7855 int cancel_shutdown = False;
7856
7857 save_cmdmod = cmdmod;
7858 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007859 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 /* Mustn't logout */
7861 cancel_shutdown = True;
7862 cmdmod = save_cmdmod;
7863 setcursor(); /* position cursor */
7864 out_flush();
7865
7866 /* Done interaction */
7867 SmcInteractDone(smc_conn, cancel_shutdown);
7868
7869 /* Finish off
7870 * Only end save-yourself here if we're not cancelling shutdown;
7871 * we'll get a cancelled callback later in which we'll end it.
7872 * Hopefully get around glitchy SMs (like GNOME-1)
7873 */
7874 if (!cancel_shutdown)
7875 {
7876 xsmp.save_yourself = False;
7877 SmcSaveYourselfDone(smc_conn, True);
7878 }
7879}
7880# endif
7881
7882/*
7883 * Callback that starts save-yourself.
7884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007886xsmp_handle_save_yourself(
7887 SmcConn smc_conn,
7888 SmPointer client_data UNUSED,
7889 int save_type UNUSED,
7890 Bool shutdown,
7891 int interact_style UNUSED,
7892 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
7894 /* Handle already being in saveyourself */
7895 if (xsmp.save_yourself)
7896 SmcSaveYourselfDone(smc_conn, True);
7897 xsmp.save_yourself = True;
7898 xsmp.shutdown = shutdown;
7899
7900 /* First up, preserve all files */
7901 out_flush();
7902 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7903
7904 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007905 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
7907# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7908 /* Now see if we can ask about unsaved files */
7909 if (shutdown && !fast && gui.in_use)
7910 /* Need to interact with user, but need SM's permission */
7911 SmcInteractRequest(smc_conn, SmDialogError,
7912 xsmp_handle_interaction, client_data);
7913 else
7914# endif
7915 {
7916 /* Can stop the cycle here */
7917 SmcSaveYourselfDone(smc_conn, True);
7918 xsmp.save_yourself = False;
7919 }
7920}
7921
7922
7923/*
7924 * Callback to warn us of imminent death.
7925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007927xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 xsmp_close();
7930
7931 /* quit quickly leaving swapfiles for modified buffers behind */
7932 getout_preserve_modified(0);
7933}
7934
7935
7936/*
7937 * Callback to tell us that save-yourself has completed.
7938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007940xsmp_save_complete(
7941 SmcConn smc_conn UNUSED,
7942 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 xsmp.save_yourself = False;
7945}
7946
7947
7948/*
7949 * Callback to tell us that an instigated shutdown was cancelled
7950 * (maybe even by us)
7951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007953xsmp_shutdown_cancelled(
7954 SmcConn smc_conn,
7955 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 if (xsmp.save_yourself)
7958 SmcSaveYourselfDone(smc_conn, True);
7959 xsmp.save_yourself = False;
7960 xsmp.shutdown = False;
7961}
7962
7963
7964/*
7965 * Callback to tell us that a new ICE connection has been established.
7966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007968xsmp_ice_connection(
7969 IceConn iceConn,
7970 IcePointer clientData UNUSED,
7971 Bool opening,
7972 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 /* Intercept creation of ICE connection fd */
7975 if (opening)
7976 {
7977 xsmp_icefd = IceConnectionNumber(iceConn);
7978 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7979 }
7980}
7981
7982
7983/* Handle any ICE processing that's required; return FAIL if SM lost */
7984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007985xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 Bool rep;
7988
7989 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7990 == IceProcessMessagesIOError)
7991 {
7992 /* Lost ICE */
7993 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007994 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 xsmp_close();
7996 return FAIL;
7997 }
7998 else
7999 return OK;
8000}
8001
8002static int dummy;
8003
8004/* Set up X Session Management Protocol */
8005 void
8006xsmp_init(void)
8007{
8008 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 SmcCallbacks smcallbacks;
8010#if 0
8011 SmPropValue smname;
8012 SmProp smnameprop;
8013 SmProp *smprops[1];
8014#endif
8015
8016 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008017 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019 xsmp.save_yourself = xsmp.shutdown = False;
8020
8021 /* Set up SM callbacks - must have all, even if they're not used */
8022 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8023 smcallbacks.save_yourself.client_data = NULL;
8024 smcallbacks.die.callback = xsmp_die;
8025 smcallbacks.die.client_data = NULL;
8026 smcallbacks.save_complete.callback = xsmp_save_complete;
8027 smcallbacks.save_complete.client_data = NULL;
8028 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8029 smcallbacks.shutdown_cancelled.client_data = NULL;
8030
8031 /* Set up a watch on ICE connection creations. The "dummy" argument is
8032 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8033 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8034 {
8035 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008036 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 return;
8038 }
8039
8040 /* Create an SM connection */
8041 xsmp.smcconn = SmcOpenConnection(
8042 NULL,
8043 NULL,
8044 SmProtoMajor,
8045 SmProtoMinor,
8046 SmcSaveYourselfProcMask | SmcDieProcMask
8047 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8048 &smcallbacks,
8049 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008050 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008051 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 errorstring);
8053 if (xsmp.smcconn == NULL)
8054 {
8055 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008056
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008058 {
8059 vim_snprintf(errorreport, sizeof(errorreport),
8060 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008061 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 return;
8064 }
8065 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8066
8067#if 0
8068 /* ID ourselves */
8069 smname.value = "vim";
8070 smname.length = 3;
8071 smnameprop.name = "SmProgram";
8072 smnameprop.type = "SmARRAY8";
8073 smnameprop.num_vals = 1;
8074 smnameprop.vals = &smname;
8075
8076 smprops[0] = &smnameprop;
8077 SmcSetProperties(xsmp.smcconn, 1, smprops);
8078#endif
8079}
8080
8081
8082/* Shut down XSMP comms. */
8083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008084xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 if (xsmp_icefd != -1)
8087 {
8088 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008089 if (xsmp.clientid != NULL)
8090 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008091 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 xsmp_icefd = -1;
8093 }
8094}
8095#endif /* USE_XSMP */
8096
8097
8098#ifdef EBCDIC
8099/* Translate character to its CTRL- value */
8100char CtrlTable[] =
8101{
8102/* 00 - 5E */
8103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8109/* ^ */ 0x1E,
8110/* - */ 0x1F,
8111/* 61 - 6C */
8112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8113/* _ */ 0x1F,
8114/* 6E - 80 */
8115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8116/* a */ 0x01,
8117/* b */ 0x02,
8118/* c */ 0x03,
8119/* d */ 0x37,
8120/* e */ 0x2D,
8121/* f */ 0x2E,
8122/* g */ 0x2F,
8123/* h */ 0x16,
8124/* i */ 0x05,
8125/* 8A - 90 */
8126 0, 0, 0, 0, 0, 0, 0,
8127/* j */ 0x15,
8128/* k */ 0x0B,
8129/* l */ 0x0C,
8130/* m */ 0x0D,
8131/* n */ 0x0E,
8132/* o */ 0x0F,
8133/* p */ 0x10,
8134/* q */ 0x11,
8135/* r */ 0x12,
8136/* 9A - A1 */
8137 0, 0, 0, 0, 0, 0, 0, 0,
8138/* s */ 0x13,
8139/* t */ 0x3C,
8140/* u */ 0x3D,
8141/* v */ 0x32,
8142/* w */ 0x26,
8143/* x */ 0x18,
8144/* y */ 0x19,
8145/* z */ 0x3F,
8146/* AA - AC */
8147 0, 0, 0,
8148/* [ */ 0x27,
8149/* AE - BC */
8150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8151/* ] */ 0x1D,
8152/* BE - C0 */ 0, 0, 0,
8153/* A */ 0x01,
8154/* B */ 0x02,
8155/* C */ 0x03,
8156/* D */ 0x37,
8157/* E */ 0x2D,
8158/* F */ 0x2E,
8159/* G */ 0x2F,
8160/* H */ 0x16,
8161/* I */ 0x05,
8162/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8163/* J */ 0x15,
8164/* K */ 0x0B,
8165/* L */ 0x0C,
8166/* M */ 0x0D,
8167/* N */ 0x0E,
8168/* O */ 0x0F,
8169/* P */ 0x10,
8170/* Q */ 0x11,
8171/* R */ 0x12,
8172/* DA - DF */ 0, 0, 0, 0, 0, 0,
8173/* \ */ 0x1C,
8174/* E1 */ 0,
8175/* S */ 0x13,
8176/* T */ 0x3C,
8177/* U */ 0x3D,
8178/* V */ 0x32,
8179/* W */ 0x26,
8180/* X */ 0x18,
8181/* Y */ 0x19,
8182/* Z */ 0x3F,
8183/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8185};
8186
8187char MetaCharTable[]=
8188{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8189 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8190 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8191 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8192 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8193};
8194
8195
8196/* TODO: Use characters NOT numbers!!! */
8197char CtrlCharTable[]=
8198{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8199 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8200 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8201 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8202 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8203};
8204
8205
8206#endif