blob: 3757dc5593acd6f357f19a6c8bdbfa7928092cc3 [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)? */
341#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
342# define NEW_TTY_SYSTEM
343#endif
344
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000345/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100346 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100349mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200351 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100353 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000357 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 * Get a characters from the keyboard.
359 * Return the number of characters that are available.
360 * If wtime == 0 do not wait for characters.
361 * If wtime == n wait a short time for characters.
362 * If wtime == -1 wait forever for characters.
363 */
364 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100365mch_inchar(
366 char_u *buf,
367 int maxlen,
368 long wtime, /* don't use "time", MIPS cannot handle it */
369 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
371 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200372 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200373 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200374 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200375 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100376#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +0100377 elapsed_T start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200378
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100379 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200382 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200383 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200385 /* Check if window changed size while we were busy, perhaps the ":set
386 * columns=99" command was used. */
387 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200389
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200390#ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +0200391 // Only process messages when waiting.
392 if (wtime != 0)
393 {
394 parse_queued_messages();
395 // If input was put directly in typeahead buffer bail out here.
396 if (typebuf_changed(tb_change_cnt))
397 return 0;
398 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200399#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200400 if (wtime < 0 && did_start_blocking)
401 /* blocking and already waited for p_ut */
402 wait_time = -1;
403 else
404 {
405 if (wtime >= 0)
406 wait_time = wtime;
407 else
408 /* going to block after p_ut */
409 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100410#ifdef ELAPSED_FUNC
411 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200412#endif
413 wait_time -= elapsed_time;
414 if (wait_time < 0)
415 {
416 if (wtime >= 0)
417 /* no character available within "wtime" */
418 return 0;
419
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200420 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200421 {
422 /* no character available within 'updatetime' */
423 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200424 if (trigger_cursorhold() && maxlen >= 3
425 && !typebuf_changed(tb_change_cnt))
426 {
427 buf[0] = K_SPECIAL;
428 buf[1] = KS_EXTRA;
429 buf[2] = (int)KE_CURSORHOLD;
430 return 3;
431 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200432 /*
433 * If there is no character available within 'updatetime'
434 * seconds flush all the swap files to disk.
435 * Also done when interrupted by SIGWINCH.
436 */
437 before_blocking();
438 continue;
439 }
440 }
441 }
442
443#ifdef FEAT_JOB_CHANNEL
444 /* Checking if a job ended requires polling. Do this every 100 msec. */
445 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
446 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100447 /* If there is readahead then parse_queued_messages() timed out and we
448 * should call it again soon. */
449 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
450 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200451#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100452#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100453 if (p_beval && wait_time > 100L)
454 /* The 'balloonexpr' may indirectly invoke a callback while waiting
455 * for a character, need to check often. */
456 wait_time = 100L;
457#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200460 * We want to be interrupted by the winch signal
461 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200463 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200464 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200465 /* If input was put directly in typeahead buffer bail out here. */
466 if (typebuf_changed(tb_change_cnt))
467 return 0;
468
469 /*
470 * For some terminals we only get one character at a time.
471 * We want the get all available characters, so we could keep on
472 * trying until none is available
473 * For some other terminals this is quite slow, that's why we don't
474 * do it.
475 */
476 len = read_from_input_buf(buf, (long)maxlen);
477 if (len > 0)
478 return len;
479 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200482 /* no character available */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +0100483#ifndef ELAPSED_FUNC
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200484 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100485 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200488 if (do_resize /* interrupted by SIGWINCH signal */
489#ifdef FEAT_CLIENTSERVER
490 || server_waiting()
491#endif
492#ifdef MESSAGE_QUEUE
493 || interrupted
494#endif
495 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100496 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200497 continue;
498
499 /* no character available or interrupted */
500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200502 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
505 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100506handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 do_resize = FALSE;
509 shell_resized();
510}
511
512/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200513 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
515 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100516mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200518 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200521#if defined(FEAT_TERMINAL) || defined(PROTO)
522/*
523 * Check for any pending input or messages.
524 */
525 int
526mch_check_messages(void)
527{
528 return WaitForChar(0L, NULL, TRUE);
529}
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
533# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000534# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535# endif
536# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
537# include <sys/sysctl.h>
538# endif
539# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
540# include <sys/sysinfo.h>
541# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100542# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100543# include <mach/mach_host.h>
544# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
547/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000548 * Return total amount of memory available in Kbyte.
549 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100552mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000555 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar362dc332018-03-05 21:59:37 +0100557# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100558 {
559 /* Mac (Darwin) way of getting the amount of RAM available */
560 mach_port_t host = mach_host_self();
561 kern_return_t kret;
562# ifdef HOST_VM_INFO64
563 struct vm_statistics64 vm_stat;
564 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar988615f2018-02-27 17:58:20 +0100566 kret = host_statistics64(host, HOST_VM_INFO64,
567 (host_info64_t)&vm_stat, &count);
568# else
569 struct vm_statistics vm_stat;
570 natural_t count = HOST_VM_INFO_COUNT;
571
572 kret = host_statistics(host, HOST_VM_INFO,
573 (host_info_t)&vm_stat, &count);
574# endif
575 if (kret == KERN_SUCCESS)
576 /* get the amount of user memory by summing each usage */
577 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
578 + vm_stat.inactive_count
579# ifdef MAC_OS_X_VERSION_10_9
580 + vm_stat.compressor_page_count
581# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100582 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100583 mach_port_deallocate(mach_task_self(), host);
584 }
585# endif
586
587# ifdef HAVE_SYSCTL
588 if (mem == 0)
589 {
590 /* BSD way of getting the amount of RAM available. */
591 int mib[2];
592 size_t len = sizeof(long_u);
593# ifdef HW_USERMEM64
594 long_u physmem;
595# else
596 /* sysctl() may return 32 bit or 64 bit, accept both */
597 union {
598 int_u u32;
599 long_u u64;
600 } physmem;
601# endif
602
603 mib[0] = CTL_HW;
604# ifdef HW_USERMEM64
605 mib[1] = HW_USERMEM64;
606# else
607 mib[1] = HW_USERMEM;
608# endif
609 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
610 {
611# ifdef HW_USERMEM64
612 mem = (long_u)physmem;
613# else
614 if (len == sizeof(physmem.u64))
615 mem = (long_u)physmem.u64;
616 else
617 mem = (long_u)physmem.u32;
618# endif
619 }
620 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200623# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (mem == 0)
625 {
626 struct sysinfo sinfo;
627
628 /* Linux way of getting amount of RAM available */
629 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000630 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200631# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000632 /* avoid overflow as much as possible */
633 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
634 {
635 sinfo.mem_unit = sinfo.mem_unit >> 1;
636 --shiftright;
637 }
638 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200639# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200641# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if (mem == 0)
648 {
649 long pagesize, pagecount;
650
651 /* Solaris way of getting amount of RAM available */
652 pagesize = sysconf(_SC_PAGESIZE);
653 pagecount = sysconf(_SC_PHYS_PAGES);
654 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
656 /* avoid overflow as much as possible */
657 while (shiftright > 0 && (pagesize & 1) == 0)
658 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000659 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000660 --shiftright;
661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667 /* Return the minimum of the physical memory and the user limit, because
668 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200669# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
671 struct rlimit rlp;
672
673 if (getrlimit(RLIMIT_DATA, &rlp) == 0
674 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200675# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200677# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000678 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 {
681 mem = (long_u)rlp.rlim_cur;
682 shiftright = 10;
683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
687 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000688 return mem >> shiftright;
689 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691#endif
692
693 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100694mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000697#ifdef FEAT_MZSCHEME
698 long total = msec; /* remember original value */
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701 if (ignoreinput)
702 {
703 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000704 * here. But we don't want QUIT to kill us (CTRL-\ used in a
705 * shell may produce SIGQUIT). */
706 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 old_tmode = curr_tmode;
708 if (curr_tmode == TMODE_RAW)
709 settmode(TMODE_SLEEP);
710
711 /*
712 * Everybody sleeps in a different way...
713 * Prefer nanosleep(), some versions of usleep() can only sleep up to
714 * one second.
715 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000716#ifdef FEAT_MZSCHEME
717 do
718 {
719 /* if total is large enough, wait by portions in p_mzq */
720 if (total > p_mzq)
721 msec = p_mzq;
722 else
723 msec = total;
724 total -= msec;
725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#ifdef HAVE_NANOSLEEP
727 {
728 struct timespec ts;
729
730 ts.tv_sec = msec / 1000;
731 ts.tv_nsec = (msec % 1000) * 1000000;
732 (void)nanosleep(&ts, NULL);
733 }
734#else
735# ifdef HAVE_USLEEP
736 while (msec >= 1000)
737 {
738 usleep((unsigned int)(999 * 1000));
739 msec -= 999;
740 }
741 usleep((unsigned int)(msec * 1000));
742# else
743# ifndef HAVE_SELECT
744 poll(NULL, 0, (int)msec);
745# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 struct timeval tv;
748
749 tv.tv_sec = msec / 1000;
750 tv.tv_usec = (msec % 1000) * 1000;
751 /*
752 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
753 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
754 */
755 select(0, NULL, NULL, NULL, &tv);
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757# endif /* HAVE_SELECT */
758# endif /* HAVE_NANOSLEEP */
759#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000760#ifdef FEAT_MZSCHEME
761 }
762 while (total > 0);
763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000766 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200769 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000772#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
774# define HAVE_CHECK_STACK_GROWTH
775/*
776 * Support for checking for an almost-out-of-stack-space situation.
777 */
778
779/*
780 * Return a pointer to an item on the stack. Used to find out if the stack
781 * grows up or down.
782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783static int stack_grows_downwards;
784
785/*
786 * Find out if the stack grows upwards or downwards.
787 * "p" points to a variable on the stack of the caller.
788 */
789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100790check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 int i;
793
794 stack_grows_downwards = (p > (char *)&i);
795}
796#endif
797
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000798#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799static char *stack_limit = NULL;
800
801#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
802# include <pthread.h>
803# include <pthread_np.h>
804#endif
805
806/*
807 * Find out until how var the stack can grow without getting into trouble.
808 * Called when starting up and when switching to the signal stack in
809 * deathtrap().
810 */
811 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100812get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 struct rlimit rlp;
815 int i;
816 long lim;
817
818 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
819 * limit doesn't fit in a long (rlim_cur might be "long long"). */
820 if (getrlimit(RLIMIT_STACK, &rlp) == 0
821 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
822# ifdef RLIM_INFINITY
823 && rlp.rlim_cur != RLIM_INFINITY
824# endif
825 )
826 {
827 lim = (long)rlp.rlim_cur;
828#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
829 {
830 pthread_attr_t attr;
831 size_t size;
832
833 /* On FreeBSD the initial thread always has a fixed stack size, no
834 * matter what the limits are set to. Normally it's 1 Mbyte. */
835 pthread_attr_init(&attr);
836 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
837 {
838 pthread_attr_getstacksize(&attr, &size);
839 if (lim > (long)size)
840 lim = (long)size;
841 }
842 pthread_attr_destroy(&attr);
843 }
844#endif
845 if (stack_grows_downwards)
846 {
847 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
848 if (stack_limit >= (char *)&i)
849 /* overflow, set to 1/16 of current stack position */
850 stack_limit = (char *)((long)&i / 16L);
851 }
852 else
853 {
854 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
855 if (stack_limit <= (char *)&i)
856 stack_limit = NULL; /* overflow */
857 }
858 }
859}
860
861/*
862 * Return FAIL when running out of stack space.
863 * "p" must point to any variable local to the caller that's on the stack.
864 */
865 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100866mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 if (stack_limit != NULL)
869 {
870 if (stack_grows_downwards)
871 {
872 if (p < stack_limit)
873 return FAIL;
874 }
875 else if (p > stack_limit)
876 return FAIL;
877 }
878 return OK;
879}
880#endif
881
882#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
883/*
884 * Support for using the signal stack.
885 * This helps when we run out of stack space, which causes a SIGSEGV. The
886 * signal handler then must run on another stack, since the normal stack is
887 * completely full.
888 */
889
890#ifndef SIGSTKSZ
891# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
892#endif
893
894# ifdef HAVE_SIGALTSTACK
895static stack_t sigstk; /* for sigaltstack() */
896# else
897static struct sigstack sigstk; /* for sigstack() */
898# endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900static char *signal_stack;
901
902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100903init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 if (signal_stack != NULL)
906 {
907# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908# ifdef HAVE_SS_BASE
909 sigstk.ss_base = signal_stack;
910# else
911 sigstk.ss_sp = signal_stack;
912# endif
913 sigstk.ss_size = SIGSTKSZ;
914 sigstk.ss_flags = 0;
915 (void)sigaltstack(&sigstk, NULL);
916# else
917 sigstk.ss_sp = signal_stack;
918 if (stack_grows_downwards)
919 sigstk.ss_sp += SIGSTKSZ - 1;
920 sigstk.ss_onstack = 0;
921 (void)sigstack(&sigstk, NULL);
922# endif
923 }
924}
925#endif
926
927/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000928 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * will barf when the second argument to signal() is ``wrong''.
930 * Let me try it with a few tricky defines from my own osdef.h (jw).
931 */
932#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 static RETSIGTYPE
934sig_winch SIGDEFARG(sigarg)
935{
936 /* this is not required on all systems, but it doesn't hurt anybody */
937 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
938 do_resize = TRUE;
939 SIGRETURN;
940}
941#endif
942
943#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 static RETSIGTYPE
945catch_sigint SIGDEFARG(sigarg)
946{
947 /* this is not required on all systems, but it doesn't hurt anybody */
948 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
949 got_int = TRUE;
950 SIGRETURN;
951}
952#endif
953
954#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 static RETSIGTYPE
956catch_sigpwr SIGDEFARG(sigarg)
957{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000958 /* this is not required on all systems, but it doesn't hurt anybody */
959 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /*
961 * I'm not sure we get the SIGPWR signal when the system is really going
962 * down or when the batteries are almost empty. Just preserve the swap
963 * files and don't exit, that can't do any harm.
964 */
965 ml_sync_all(FALSE, FALSE);
966 SIGRETURN;
967}
968#endif
969
970#ifdef SET_SIG_ALARM
971/*
972 * signal function for alarm().
973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 static RETSIGTYPE
975sig_alarm SIGDEFARG(sigarg)
976{
977 /* doesn't do anything, just to break a system call */
978 sig_alarm_called = TRUE;
979 SIGRETURN;
980}
981#endif
982
Bram Moolenaaredce7422019-01-20 18:39:30 +0100983#if (defined(HAVE_SETJMP_H) \
984 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
985 || defined(FEAT_LIBCALL))) \
986 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100987# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100988
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100989// argument to SETJMP()
990static JMP_BUF lc_jump_env;
991
992# ifdef SIGHASARG
993// Caught signal number, 0 when no was signal caught; used for mch_libcall().
994// Volatile because it is used in signal handlers.
995static volatile sig_atomic_t lc_signal;
996# endif
997
998// TRUE when lc_jump_env is valid.
999// Volatile because it is used in signal handler deathtrap().
1000static volatile sig_atomic_t lc_active INIT(= FALSE);
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001004 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001006 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Usage:
1008 * mch_startjmp();
1009 * if (SETJMP(lc_jump_env) != 0)
1010 * {
1011 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001012 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * }
1014 * else
1015 * {
1016 * do_the_work;
1017 * mch_endjmp();
1018 * }
1019 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1020 * not return before the saved environment is used.
1021 * Returns OK for normal return, FAIL when the protected code caused a
1022 * problem and LONGJMP() was used.
1023 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001024 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001025mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001027# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 lc_active = TRUE;
1031}
1032
Bram Moolenaar113e1072019-01-20 15:30:40 +01001033 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001034mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036 lc_active = FALSE;
1037}
1038
Bram Moolenaar113e1072019-01-20 15:30:40 +01001039 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001040mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001043 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1044 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 init_signal_stack();
1046# endif
1047}
1048#endif
1049
1050/*
1051 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001052 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001054 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1055 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
1057 static RETSIGTYPE
1058deathtrap SIGDEFARG(sigarg)
1059{
1060 static int entered = 0; /* count the number of times we got here.
1061 Note: when memory has been corrupted
1062 this may get an arbitrary value! */
1063#ifdef SIGHASARG
1064 int i;
1065#endif
1066
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001067#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 /*
1069 * Catch a crash in protected code.
1070 * Restores the environment saved in lc_jump_env, which looks like
1071 * SETJMP() returns 1.
1072 */
1073 if (lc_active)
1074 {
1075# if defined(SIGHASARG)
1076 lc_signal = sigarg;
1077# endif
1078 lc_active = FALSE; /* don't jump again */
1079 LONGJMP(lc_jump_env, 1);
1080 /* NOTREACHED */
1081 }
1082#endif
1083
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001084#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001085# ifdef SIGQUIT
1086 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1087 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1088 * pressing CTRL-\, but we don't want Vim to exit then. */
1089 if (in_mch_delay && sigarg == SIGQUIT)
1090 SIGRETURN;
1091# endif
1092
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001093 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1094 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1095 * free(). Calling free() again may then cause a crash. */
1096 if (entered == 0
1097 && (0
1098# ifdef SIGHUP
1099 || sigarg == SIGHUP
1100# endif
1101# ifdef SIGQUIT
1102 || sigarg == SIGQUIT
1103# endif
1104# ifdef SIGTERM
1105 || sigarg == SIGTERM
1106# endif
1107# ifdef SIGPWR
1108 || sigarg == SIGPWR
1109# endif
1110# ifdef SIGUSR1
1111 || sigarg == SIGUSR1
1112# endif
1113# ifdef SIGUSR2
1114 || sigarg == SIGUSR2
1115# endif
1116 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001117 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001118 SIGRETURN;
1119#endif
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 /* Remember how often we have been called. */
1122 ++entered;
1123
Bram Moolenaare429e702016-06-10 19:49:14 +02001124 /* Executing autocommands is likely to use more stack space than we have
1125 * available in the signal stack. */
1126 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128#ifdef FEAT_EVAL
1129 /* Set the v:dying variable. */
1130 set_vim_var_nr(VV_DYING, (long)entered);
1131#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001132 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001134#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /* Since we are now using the signal stack, need to reset the stack
1136 * limit. Otherwise using a regexp will fail. */
1137 get_stack_limit();
1138#endif
1139
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001140#if 0
1141 /* This is for opening gdb the moment Vim crashes.
1142 * You need to manually adjust the file name and Vim executable name.
1143 * Suggested by SungHyun Nam. */
1144 {
1145# define VI_GDB_FILE "/tmp/vimgdb"
1146# define VIM_NAME "/usr/bin/vim"
1147 FILE *fp = fopen(VI_GDB_FILE, "w");
1148 if (fp)
1149 {
1150 fprintf(fp,
1151 "file %s\n"
1152 "attach %d\n"
1153 "set height 1000\n"
1154 "bt full\n"
1155 , VIM_NAME, getpid());
1156 fclose(fp);
1157 system("xterm -e gdb -x "VI_GDB_FILE);
1158 unlink(VI_GDB_FILE);
1159 }
1160 }
1161#endif
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163#ifdef SIGHASARG
1164 /* try to find the name of this signal */
1165 for (i = 0; signal_info[i].sig != -1; i++)
1166 if (sigarg == signal_info[i].sig)
1167 break;
1168 deadly_signal = sigarg;
1169#endif
1170
1171 full_screen = FALSE; /* don't write message to the GUI, it might be
1172 * part of the problem... */
1173 /*
1174 * If something goes wrong after entering here, we may get here again.
1175 * When this happens, give a message and try to exit nicely (resetting the
1176 * terminal mode, etc.)
1177 * When this happens twice, just exit, don't even try to give a message,
1178 * stack may be corrupt or something weird.
1179 * When this still happens again (or memory was corrupted in such a way
1180 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1181 */
1182 if (entered >= 3)
1183 {
1184 reset_signals(); /* don't catch any signals anymore */
1185 may_core_dump();
1186 if (entered >= 4)
1187 _exit(8);
1188 exit(7);
1189 }
1190 if (entered == 2)
1191 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001192 /* No translation, it may call malloc(). */
1193 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 out_flush();
1195 getout(1);
1196 }
1197
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001198 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001200 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 signal_info[i].name);
1202#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001203 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001205
1206 /* Preserve files and exit. This sets the really_exiting flag to prevent
1207 * calling free(). */
1208 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaare429e702016-06-10 19:49:14 +02001210 /* NOTREACHED */
1211
Bram Moolenaar009b2592004-10-24 19:18:58 +00001212#ifdef NBDEBUG
1213 reset_signals();
1214 may_core_dump();
1215 abort();
1216#endif
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 SIGRETURN;
1219}
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001222 * Invoked after receiving SIGCONT. We don't know what happened while
1223 * sleeping, deal with part of that.
1224 */
1225 static void
1226after_sigcont(void)
1227{
1228# ifdef FEAT_TITLE
1229 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1230 // again later.
1231 oldtitle_outdated = TRUE;
1232# endif
1233 settmode(TMODE_RAW);
1234 need_check_timestamps = TRUE;
1235 did_check_timestamps = FALSE;
1236}
1237
1238#if defined(SIGCONT)
1239static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001240static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001241
1242/*
1243 * With multi-threading, suspending might not work immediately. Catch the
1244 * SIGCONT signal, which will be used as an indication whether the suspending
1245 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001246 *
1247 * On Linux, signal is not always handled immediately either.
1248 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001249 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001250 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001251 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001253static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001254static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256/*
1257 * signal handler for SIGCONT
1258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 static RETSIGTYPE
1260sigcont_handler SIGDEFARG(sigarg)
1261{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001262 if (in_mch_suspend)
1263 {
1264 sigcont_received = TRUE;
1265 }
1266 else
1267 {
1268 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1269 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1270 // back to a sane mode. We should redraw, but we can't really do that
1271 // in a signal handler, do a redraw later.
1272 after_sigcont();
1273 redraw_later(CLEAR);
1274 cursor_on_force();
1275 out_flush();
1276 }
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 SIGRETURN;
1279}
1280#endif
1281
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001282#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001283# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001284static void *clip_star_save = NULL;
1285static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001286# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001287
1288/*
1289 * Called when Vim is going to sleep or execute a shell command.
1290 * We can't respond to requests for the X selections. Lose them, otherwise
1291 * other applications will hang. But first copy the text to cut buffer 0.
1292 */
1293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001294loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001295{
1296 if (clip_star.owned || clip_plus.owned)
1297 {
1298 x11_export_final_selection();
1299 if (clip_star.owned)
1300 clip_lose_selection(&clip_star);
1301 if (clip_plus.owned)
1302 clip_lose_selection(&clip_plus);
1303 if (x11_display != NULL)
1304 XFlush(x11_display);
1305 }
1306}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001307
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001308# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309/*
1310 * Save clipboard text to restore later.
1311 */
1312 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001313save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001314{
1315 if (clip_star.owned)
1316 clip_star_save = get_register('*', TRUE);
1317 if (clip_plus.owned)
1318 clip_plus_save = get_register('+', TRUE);
1319}
1320
1321/*
1322 * Restore clipboard text if no one own the X selection.
1323 */
1324 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001325restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001326{
1327 if (clip_star_save != NULL)
1328 {
1329 if (!clip_gen_owner_exists(&clip_star))
1330 put_register('*', clip_star_save);
1331 else
1332 free_register(clip_star_save);
1333 clip_star_save = NULL;
1334 }
1335 if (clip_plus_save != NULL)
1336 {
1337 if (!clip_gen_owner_exists(&clip_plus))
1338 put_register('+', clip_plus_save);
1339 else
1340 free_register(clip_plus_save);
1341 clip_plus_save = NULL;
1342 }
1343}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001344# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001345#endif
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347/*
1348 * If the machine has job control, use it to suspend the program,
1349 * otherwise fake it by starting a new shell.
1350 */
1351 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001352mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 /* BeOS does have SIGTSTP, but it doesn't work. */
1355#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001356 in_mch_suspend = TRUE;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 out_flush(); /* needed to make cursor visible on some systems */
1359 settmode(TMODE_COOK);
1360 out_flush(); /* needed to disable mouse on some systems */
1361
1362# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001363 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001365# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 sigcont_received = FALSE;
1367# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001370
1371# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001372 /*
1373 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001374 * immediately, but somehow not all the time, probably because it's handled
1375 * in another thread. Do not call pause() because there would be race
1376 * condition which would hang Vim if signal happened in between the test of
1377 * sigcont_received and the call to pause(). If signal is not yet received,
1378 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1379 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001380 */
1381 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001382 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383
Bram Moolenaar262735e2009-07-14 10:20:22 +00001384 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001385 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar2e310482018-08-21 13:09:10 +02001390 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#else
1392 suspend_shell();
1393#endif
1394}
1395
1396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 Columns = 80;
1400 Rows = 24;
1401
1402 out_flush();
1403 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001404
Bram Moolenaar56718732006-03-15 22:53:57 +00001405#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001406 mac_conv_init();
1407#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001408#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1409 win_clip_init();
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411}
1412
1413 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001414set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416#if defined(SIGWINCH)
1417 /*
1418 * WINDOW CHANGE signal is handled with sig_winch().
1419 */
1420 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1421#endif
1422
1423 /*
1424 * We want the STOP signal to work, to make mch_suspend() work.
1425 * For "rvim" the STOP signal is ignored.
1426 */
1427#ifdef SIGTSTP
1428 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1429#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001430#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 signal(SIGCONT, sigcont_handler);
1432#endif
1433
1434 /*
1435 * We want to ignore breaking of PIPEs.
1436 */
1437#ifdef SIGPIPE
1438 signal(SIGPIPE, SIG_IGN);
1439#endif
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001442 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#endif
1444
1445 /*
1446 * Ignore alarm signals (Perl's alarm() generates it).
1447 */
1448#ifdef SIGALRM
1449 signal(SIGALRM, SIG_IGN);
1450#endif
1451
1452 /*
1453 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1454 * work will be lost.
1455 */
1456#ifdef SIGPWR
1457 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1458#endif
1459
1460 /*
1461 * Arrange for other signals to gracefully shutdown Vim.
1462 */
1463 catch_signals(deathtrap, SIG_ERR);
1464
1465#if defined(FEAT_GUI) && defined(SIGHUP)
1466 /*
1467 * When the GUI is running, ignore the hangup signal.
1468 */
1469 if (gui.in_use)
1470 signal(SIGHUP, SIG_IGN);
1471#endif
1472}
1473
Bram Moolenaardf177f62005-02-22 08:39:57 +00001474#if defined(SIGINT) || defined(PROTO)
1475/*
1476 * Catch CTRL-C (only works while in Cooked mode).
1477 */
1478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001479catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001480{
1481 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1482}
1483#endif
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001486reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001489#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 /* SIGCONT isn't in the list, because its default action is ignore */
1491 signal(SIGCONT, SIG_DFL);
1492#endif
1493}
1494
1495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001496catch_signals(
1497 RETSIGTYPE (*func_deadly)(),
1498 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 int i;
1501
1502 for (i = 0; signal_info[i].sig != -1; i++)
1503 if (signal_info[i].deadly)
1504 {
1505#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1506 struct sigaction sa;
1507
1508 /* Setup to use the alternate stack for the signal function. */
1509 sa.sa_handler = func_deadly;
1510 sigemptyset(&sa.sa_mask);
1511# if defined(__linux__) && defined(_REENTRANT)
1512 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1513 * thread handling in combination with using the alternate stack:
1514 * pthread library functions try to use the stack pointer to
1515 * identify the current thread, causing a SEGV signal, which
1516 * recursively calls deathtrap() and hangs. */
1517 sa.sa_flags = 0;
1518# else
1519 sa.sa_flags = SA_ONSTACK;
1520# endif
1521 sigaction(signal_info[i].sig, &sa, NULL);
1522#else
1523# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1524 struct sigvec sv;
1525
1526 /* Setup to use the alternate stack for the signal function. */
1527 sv.sv_handler = func_deadly;
1528 sv.sv_mask = 0;
1529 sv.sv_flags = SV_ONSTACK;
1530 sigvec(signal_info[i].sig, &sv, NULL);
1531# else
1532 signal(signal_info[i].sig, func_deadly);
1533# endif
1534#endif
1535 }
1536 else if (func_other != SIG_ERR)
1537 signal(signal_info[i].sig, func_other);
1538}
1539
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001540#ifdef HAVE_SIGPROCMASK
1541 static void
1542block_signals(sigset_t *set)
1543{
1544 sigset_t newset;
1545 int i;
1546
1547 sigemptyset(&newset);
1548
1549 for (i = 0; signal_info[i].sig != -1; i++)
1550 sigaddset(&newset, signal_info[i].sig);
1551
Bram Moolenaar2e310482018-08-21 13:09:10 +02001552# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001553 /* SIGCONT isn't in the list, because its default action is ignore */
1554 sigaddset(&newset, SIGCONT);
1555# endif
1556
1557 sigprocmask(SIG_BLOCK, &newset, set);
1558}
1559
1560 static void
1561unblock_signals(sigset_t *set)
1562{
1563 sigprocmask(SIG_SETMASK, set, NULL);
1564}
1565#endif
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001568 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001569 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1570 * return TRUE
1571 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1572 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001573 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001574 * Returns TRUE when Vim should exit.
1575 */
1576 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001577vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001579 static int got_signal = 0;
1580 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001581
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001582 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001583 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001584 case SIGNAL_BLOCK: blocked = TRUE;
1585 break;
1586
1587 case SIGNAL_UNBLOCK: blocked = FALSE;
1588 if (got_signal != 0)
1589 {
1590 kill(getpid(), got_signal);
1591 got_signal = 0;
1592 }
1593 break;
1594
1595 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001596 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001597 got_signal = sig;
1598#ifdef SIGPWR
1599 if (sig != SIGPWR)
1600#endif
1601 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001602 break;
1603 }
1604 return FALSE;
1605}
1606
1607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 * Check_win checks whether we have an interactive stdout.
1609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001611mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (isatty(1))
1614 return OK;
1615 return FAIL;
1616}
1617
1618/*
1619 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1620 */
1621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001622mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 if (isatty(read_cmd_fd))
1625 return TRUE;
1626 return FALSE;
1627}
1628
1629#ifdef FEAT_X11
1630
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001631# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634/*
1635 * Give a message about the elapsed time for opening the X window.
1636 */
1637 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001638xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001640 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641}
1642# endif
1643#endif
1644
1645#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1646/*
1647 * A few functions shared by X11 title and clipboard code.
1648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650static int got_x_error = FALSE;
1651
1652/*
1653 * X Error handler, otherwise X just exits! (very rude) -- webb
1654 */
1655 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001656x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001658 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 STRCAT(IObuff, _("\nVim: Got X error\n"));
1660
1661 /* We cannot print a message and continue, because no X calls are allowed
1662 * here (causes my system to hang). Silently continuing might be an
1663 * alternative... */
1664 preserve_exit(); /* preserve files and exit */
1665
1666 return 0; /* NOTREACHED */
1667}
1668
1669/*
1670 * Another X Error handler, just used to check for errors.
1671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001673x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 got_x_error = TRUE;
1676 return 0;
1677}
1678
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001679/*
1680 * Return TRUE when connection to the X server is desired.
1681 */
1682 static int
1683x_connect_to_server(void)
1684{
1685 // No point in connecting if we are exiting or dying.
1686 if (exiting || v_dying)
1687 return FALSE;
1688
1689#if defined(FEAT_CLIENTSERVER)
1690 if (x_force_connect)
1691 return TRUE;
1692#endif
1693 if (x_no_connect)
1694 return FALSE;
1695
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001696 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001697 if (clip_exclude_prog != NULL)
1698 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001699 // Just in case we get called recursively, return FALSE. This could
1700 // happen if vpeekc() is used while executing the prog and it causes a
1701 // related callback to be invoked.
1702 if (regprog_in_use(clip_exclude_prog))
1703 return FALSE;
1704
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001705 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1706 return FALSE;
1707 }
1708 return TRUE;
1709}
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001712# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713/*
1714 * An X IO Error handler, used to catch error while opening the display.
1715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001717x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 /* This function should not return, it causes exit(). Longjump instead. */
1720 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001721# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001722 return 0; /* avoid the compiler complains about missing return value */
1723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724}
1725# endif
1726
1727/*
1728 * An X IO Error handler, used to catch terminal errors.
1729 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001730static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001733x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001736 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 x11_window = 0;
1738 x11_display = NULL;
1739 xterm_Shell = (Widget)0;
1740
1741 /* This function should not return, it causes exit(). Longjump instead. */
1742 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001743# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001744 return 0; /* avoid the compiler complains about missing return value */
1745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001747
1748/*
1749 * If the X11 connection was lost try to restore it.
1750 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001751 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001752 */
1753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001754may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001755{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001756 // No point in restoring the connecting if we are exiting or dying.
1757 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001760
1761# ifndef LESSTIF_VERSION
1762 /* This has been reported to avoid Vim getting stuck. */
1763 if (app_context != (XtAppContext)NULL)
1764 {
1765 XtDestroyApplicationContext(app_context);
1766 app_context = (XtAppContext)NULL;
1767 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1768 }
1769# endif
1770
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001771 setup_term_clip();
1772 get_x11_title(FALSE);
1773 }
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 * Test if "dpy" and x11_window are valid by getting the window title.
1779 * I don't actually want it yet, so there may be a simpler call to use, but
1780 * this will cause the error handler x_error_check() to be called if anything
1781 * is wrong, such as the window pointer being invalid (as can happen when the
1782 * user changes his DISPLAY, but not his WINDOWID) -- webb
1783 */
1784 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001785test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 int (*old_handler)();
1788 XTextProperty text_prop;
1789
1790 old_handler = XSetErrorHandler(x_error_check);
1791 got_x_error = FALSE;
1792 if (XGetWMName(dpy, x11_window, &text_prop))
1793 XFree((void *)text_prop.value);
1794 XSync(dpy, False);
1795 (void)XSetErrorHandler(old_handler);
1796
1797 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001798 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 return (got_x_error ? FAIL : OK);
1801}
1802#endif
1803
1804#ifdef FEAT_TITLE
1805
1806#ifdef FEAT_X11
1807
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001808static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810/*
1811 * try to get x11 window and display
1812 *
1813 * return FAIL for failure, OK otherwise
1814 */
1815 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001816get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 char *winid;
1819 static int result = -1;
1820#define XD_NONE 0 /* x11_display not set here */
1821#define XD_HERE 1 /* x11_display opened here */
1822#define XD_GUI 2 /* x11_display used from gui.dpy */
1823#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1824 static int x11_display_from = XD_NONE;
1825 static int did_set_error_handler = FALSE;
1826
1827 if (!did_set_error_handler)
1828 {
1829 /* X just exits if it finds an error otherwise! */
1830 (void)XSetErrorHandler(x_error_handler);
1831 did_set_error_handler = TRUE;
1832 }
1833
Bram Moolenaar9372a112005-12-06 19:59:18 +00001834#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (gui.in_use)
1836 {
1837 /*
1838 * If the X11 display was opened here before, for the window where Vim
1839 * was started, close that one now to avoid a memory leak.
1840 */
1841 if (x11_display_from == XD_HERE && x11_display != NULL)
1842 {
1843 XCloseDisplay(x11_display);
1844 x11_display_from = XD_NONE;
1845 }
1846 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1847 {
1848 x11_display_from = XD_GUI;
1849 return OK;
1850 }
1851 x11_display = NULL;
1852 return FAIL;
1853 }
1854 else if (x11_display_from == XD_GUI)
1855 {
1856 /* GUI must have stopped somehow, clear x11_display */
1857 x11_window = 0;
1858 x11_display = NULL;
1859 x11_display_from = XD_NONE;
1860 }
1861#endif
1862
1863 /* When started with the "-X" argument, don't try connecting. */
1864 if (!x_connect_to_server())
1865 return FAIL;
1866
1867 /*
1868 * If WINDOWID not set, should try another method to find out
1869 * what the current window number is. The only code I know for
1870 * this is very complicated.
1871 * We assume that zero is invalid for WINDOWID.
1872 */
1873 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1874 x11_window = (Window)atol(winid);
1875
1876#ifdef FEAT_XCLIPBOARD
1877 if (xterm_dpy != NULL && x11_window != 0)
1878 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001879 /* We may have checked it already, but Gnome terminal can move us to
1880 * another window, so we need to check every time. */
1881 if (x11_display_from != XD_XTERM)
1882 {
1883 /*
1884 * If the X11 display was opened here before, for the window where
1885 * Vim was started, close that one now to avoid a memory leak.
1886 */
1887 if (x11_display_from == XD_HERE && x11_display != NULL)
1888 XCloseDisplay(x11_display);
1889 x11_display = xterm_dpy;
1890 x11_display_from = XD_XTERM;
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (test_x11_window(x11_display) == FAIL)
1893 {
1894 /* probably bad $WINDOWID */
1895 x11_window = 0;
1896 x11_display = NULL;
1897 x11_display_from = XD_NONE;
1898 return FAIL;
1899 }
1900 return OK;
1901 }
1902#endif
1903
1904 if (x11_window == 0 || x11_display == NULL)
1905 result = -1;
1906
1907 if (result != -1) /* Have already been here and set this */
1908 return result; /* Don't do all these X calls again */
1909
1910 if (x11_window != 0 && x11_display == NULL)
1911 {
1912#ifdef SET_SIG_ALARM
1913 RETSIGTYPE (*sig_save)();
1914#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001915#ifdef ELAPSED_FUNC
1916 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001919 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
1921
1922#ifdef SET_SIG_ALARM
1923 /*
1924 * Opening the Display may hang if the DISPLAY setting is wrong, or
1925 * the network connection is bad. Set an alarm timer to get out.
1926 */
1927 sig_alarm_called = FALSE;
1928 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1929 (RETSIGTYPE (*)())sig_alarm);
1930 alarm(2);
1931#endif
1932 x11_display = XOpenDisplay(NULL);
1933
1934#ifdef SET_SIG_ALARM
1935 alarm(0);
1936 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1937 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001938 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939#endif
1940 if (x11_display != NULL)
1941 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001942# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001944 {
1945 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001946 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001947 verbose_leave();
1948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949# endif
1950 if (test_x11_window(x11_display) == FAIL)
1951 {
1952 /* Maybe window id is bad */
1953 x11_window = 0;
1954 XCloseDisplay(x11_display);
1955 x11_display = NULL;
1956 }
1957 else
1958 x11_display_from = XD_HERE;
1959 }
1960 }
1961 if (x11_window == 0 || x11_display == NULL)
1962 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001963
1964# ifdef FEAT_EVAL
1965 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1966# endif
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 return (result = OK);
1969}
1970
1971/*
1972 * Determine original x11 Window Title
1973 */
1974 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001975get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001977 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978}
1979
1980/*
1981 * Determine original x11 Window icon
1982 */
1983 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001984get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
1986 int retval = FALSE;
1987
1988 retval = get_x11_thing(FALSE, test_only);
1989
1990 /* could not get old icon, use terminal name */
1991 if (oldicon == NULL && !test_only)
1992 {
1993 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001994 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001996 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998
1999 return retval;
2000}
2001
2002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002003get_x11_thing(
2004 int get_title, /* get title string */
2005 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 XTextProperty text_prop;
2008 int retval = FALSE;
2009 Status status;
2010
2011 if (get_x11_windis() == OK)
2012 {
2013 /* Get window/icon name if any */
2014 if (get_title)
2015 status = XGetWMName(x11_display, x11_window, &text_prop);
2016 else
2017 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2018
2019 /*
2020 * If terminal is xterm, then x11_window may be a child window of the
2021 * outer xterm window that actually contains the window/icon name, so
2022 * keep traversing up the tree until a window with a title/icon is
2023 * found.
2024 */
2025 /* Previously this was only done for xterm and alikes. I don't see a
2026 * reason why it would fail for other terminal emulators.
2027 * if (term_is_xterm) */
2028 {
2029 Window root;
2030 Window parent;
2031 Window win = x11_window;
2032 Window *children;
2033 unsigned int num_children;
2034
2035 while (!status || text_prop.value == NULL)
2036 {
2037 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2038 &num_children))
2039 break;
2040 if (children)
2041 XFree((void *)children);
2042 if (parent == root || parent == 0)
2043 break;
2044
2045 win = parent;
2046 if (get_title)
2047 status = XGetWMName(x11_display, win, &text_prop);
2048 else
2049 status = XGetWMIconName(x11_display, win, &text_prop);
2050 }
2051 }
2052 if (status && text_prop.value != NULL)
2053 {
2054 retval = TRUE;
2055 if (!test_only)
2056 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002057#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2058 if (text_prop.encoding == XA_STRING
2059# ifdef FEAT_MBYTE
2060 && !has_mbyte
2061# endif
2062 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064#endif
2065 if (get_title)
2066 oldtitle = vim_strsave((char_u *)text_prop.value);
2067 else
2068 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002069#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 else
2072 {
2073 char **cl;
2074 Status transform_status;
2075 int n = 0;
2076
2077 transform_status = XmbTextPropertyToTextList(x11_display,
2078 &text_prop,
2079 &cl, &n);
2080 if (transform_status >= Success && n > 0 && cl[0])
2081 {
2082 if (get_title)
2083 oldtitle = vim_strsave((char_u *) cl[0]);
2084 else
2085 oldicon = vim_strsave((char_u *) cl[0]);
2086 XFreeStringList(cl);
2087 }
2088 else
2089 {
2090 if (get_title)
2091 oldtitle = vim_strsave((char_u *)text_prop.value);
2092 else
2093 oldicon = vim_strsave((char_u *)text_prop.value);
2094 }
2095 }
2096#endif
2097 }
2098 XFree((void *)text_prop.value);
2099 }
2100 }
2101 return retval;
2102}
2103
Bram Moolenaarb2148f52019-01-20 23:43:57 +01002104/* Xutf8 functions are not available on older systems. Note that on some
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002105 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2106 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2107 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002109# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110# define USE_UTF8_STRING
2111# endif
2112#endif
2113
2114/*
2115 * Set x11 Window Title
2116 *
2117 * get_x11_windis() must be called before this and have returned OK
2118 */
2119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002120set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2123 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2124 * supported everywhere and STRING doesn't work for multi-byte titles.
2125 */
2126#ifdef USE_UTF8_STRING
2127 if (enc_utf8)
2128 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2129 NULL, NULL, 0, NULL, NULL, NULL);
2130 else
2131#endif
2132 {
2133#if XtSpecificationRelease >= 4
2134# ifdef FEAT_XFONTSET
2135 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2136 NULL, NULL, 0, NULL, NULL, NULL);
2137# else
2138 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002139 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002142 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 XSetWMProperties(x11_display, x11_window, &text_prop,
2144 NULL, NULL, 0, NULL, NULL, NULL);
2145# endif
2146#else
2147 XStoreName(x11_display, x11_window, (char *)title);
2148#endif
2149 }
2150 XFlush(x11_display);
2151}
2152
2153/*
2154 * Set x11 Window icon
2155 *
2156 * get_x11_windis() must be called before this and have returned OK
2157 */
2158 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002159set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 /* See above for comments about using X*SetWMProperties(). */
2162#ifdef USE_UTF8_STRING
2163 if (enc_utf8)
2164 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2165 NULL, 0, NULL, NULL, NULL);
2166 else
2167#endif
2168 {
2169#if XtSpecificationRelease >= 4
2170# ifdef FEAT_XFONTSET
2171 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2172 NULL, 0, NULL, NULL, NULL);
2173# else
2174 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002175 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002177 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2179 NULL, 0, NULL, NULL, NULL);
2180# endif
2181#else
2182 XSetIconName(x11_display, x11_window, (char *)icon);
2183#endif
2184 }
2185 XFlush(x11_display);
2186}
2187
2188#else /* FEAT_X11 */
2189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002191get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 return FALSE;
2194}
2195
2196 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002197get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 if (!test_only)
2200 {
2201 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002202 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002204 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206 return FALSE;
2207}
2208
2209#endif /* FEAT_X11 */
2210
2211 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002212mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
2214 return get_x11_title(TRUE);
2215}
2216
2217 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002218mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 return get_x11_icon(TRUE);
2221}
2222
2223/*
2224 * Set the window title and icon.
2225 */
2226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002227mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 int type = 0;
2230 static int recursive = 0;
2231
2232 if (T_NAME == NULL) /* no terminal name (yet) */
2233 return;
2234 if (title == NULL && icon == NULL) /* nothing to do */
2235 return;
2236
2237 /* When one of the X11 functions causes a deadly signal, we get here again
2238 * recursively. Avoid hanging then (something is probably locked). */
2239 if (recursive)
2240 return;
2241 ++recursive;
2242
2243 /*
2244 * if the window ID and the display is known, we may use X11 calls
2245 */
2246#ifdef FEAT_X11
2247 if (get_x11_windis() == OK)
2248 type = 1;
2249#else
2250# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2251 if (gui.in_use)
2252 type = 1;
2253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#endif
2255
2256 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002257 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * than x11 calls, because the x11 calls don't always work
2259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if ((type || *T_TS != NUL) && title != NULL)
2261 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002262 if (oldtitle_outdated)
2263 {
2264 oldtitle_outdated = FALSE;
2265 VIM_CLEAR(oldtitle);
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (oldtitle == NULL
2268#ifdef FEAT_GUI
2269 && !gui.in_use
2270#endif
2271 ) /* first call but not in GUI, save title */
2272 (void)get_x11_title(FALSE);
2273
2274 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2275 term_settitle(title);
2276#ifdef FEAT_X11
2277 else
2278# ifdef FEAT_GUI_GTK
2279 if (!gui.in_use) /* don't do this if GTK+ is running */
2280# endif
2281 set_x11_title(title); /* x11 */
2282#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002283#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2285 else
2286 gui_mch_settitle(title, icon);
2287#endif
2288 did_set_title = TRUE;
2289 }
2290
2291 if ((type || *T_CIS != NUL) && icon != NULL)
2292 {
2293 if (oldicon == NULL
2294#ifdef FEAT_GUI
2295 && !gui.in_use
2296#endif
2297 ) /* first call, save icon */
2298 get_x11_icon(FALSE);
2299
2300 if (*T_CIS != NUL)
2301 {
2302 out_str(T_CIS); /* set icon start */
2303 out_str_nf(icon);
2304 out_str(T_CIE); /* set icon end */
2305 out_flush();
2306 }
2307#ifdef FEAT_X11
2308 else
2309# ifdef FEAT_GUI_GTK
2310 if (!gui.in_use) /* don't do this if GTK+ is running */
2311# endif
2312 set_x11_icon(icon); /* x11 */
2313#endif
2314 did_set_icon = TRUE;
2315 }
2316 --recursive;
2317}
2318
2319/*
2320 * Restore the window/icon title.
2321 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002322 * SAVE_RESTORE_TITLE only restore title
2323 * SAVE_RESTORE_ICON only restore icon
2324 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
2326 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002327mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002330 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002332 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2333
2334 // pop and push from/to the stack
2335 term_pop_title(which);
2336 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337}
2338
2339#endif /* FEAT_TITLE */
2340
2341/*
2342 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002343 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 */
2345 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002346vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348 if (name == NULL)
2349 return FALSE;
2350 return (STRNICMP(name, "xterm", 5) == 0
2351 || STRNICMP(name, "nxterm", 6) == 0
2352 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002353 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002355 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 || STRCMP(name, "builtin_xterm") == 0);
2357}
2358
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002359#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2360/*
2361 * Return TRUE if "name" appears to be that of a terminal
2362 * known to support the xterm-style mouse protocol.
2363 * Relies on term_is_xterm having been set to its correct value.
2364 */
2365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002366use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002367{
2368 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002369 && (term_is_xterm
2370 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002371 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002372 || STRICMP(name, "st") == 0
2373 || STRNICMP(name, "st-", 3) == 0
2374 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002375}
2376#endif
2377
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2379/*
2380 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2381 * Return 1 for "xterm".
2382 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002383 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002384 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 */
2386 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002387use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002389 if (ttym_flags == TTYM_SGR)
2390 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002391 if (ttym_flags == TTYM_URXVT)
2392 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (ttym_flags == TTYM_XTERM2)
2394 return 2;
2395 if (ttym_flags == TTYM_XTERM)
2396 return 1;
2397 return 0;
2398}
2399#endif
2400
2401 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002402vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
2404 if (name == NULL)
2405 return FALSE;
2406 return (STRNICMP(name, "iris-ansi", 9) == 0
2407 || STRCMP(name, "builtin_iris-ansi") == 0);
2408}
2409
2410 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002411vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 if (name == NULL)
2414 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002415 /* catch VT100 - VT5xx */
2416 return ((STRNICMP(name, "vt", 2) == 0
2417 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 || STRCMP(name, "builtin_vt320") == 0);
2419}
2420
2421/*
2422 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2423 * This should include all windowed terminal emulators.
2424 */
2425 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002426vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 if (name == NULL)
2429 return FALSE;
2430 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2431 return TRUE;
2432 return ( STRNICMP(name, "hpterm", 6) == 0
2433 || STRNICMP(name, "sun-cmd", 7) == 0
2434 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002435 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 || STRNICMP(name, "dtterm", 6) == 0);
2437}
2438
2439/*
2440 * Insert user name in s[len].
2441 * Return OK if a name found.
2442 */
2443 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002444mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002447 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 return OK;
2449#else
2450 return mch_get_uname(getuid(), s, len);
2451#endif
2452}
2453
2454/*
2455 * Insert user name for "uid" in s[len].
2456 * Return OK if a name found.
2457 */
2458 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002459mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2462 struct passwd *pw;
2463
2464 if ((pw = getpwuid(uid)) != NULL
2465 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2466 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002467 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 return OK;
2469 }
2470#endif
2471 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2472 return FAIL; /* a number is not a name */
2473}
2474
2475/*
2476 * Insert host name is s[len].
2477 */
2478
2479#ifdef HAVE_SYS_UTSNAME_H
2480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002481mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 struct utsname vutsname;
2484
2485 if (uname(&vutsname) < 0)
2486 *s = NUL;
2487 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002488 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489}
2490#else /* HAVE_SYS_UTSNAME_H */
2491
2492# ifdef HAVE_SYS_SYSTEMINFO_H
2493# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2494# endif
2495
2496 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002497mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499# ifdef VAXC
2500 vaxc$gethostname((char *)s, len);
2501# else
2502 gethostname((char *)s, len);
2503# endif
2504 s[len - 1] = NUL; /* make sure it's terminated */
2505}
2506#endif /* HAVE_SYS_UTSNAME_H */
2507
2508/*
2509 * return process ID
2510 */
2511 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002512mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514 return (long)getpid();
2515}
2516
2517#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002519strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 extern int sys_nerr;
2522 extern char *sys_errlist[];
2523 static char er[20];
2524
2525 if (err > 0 && err < sys_nerr)
2526 return (sys_errlist[err]);
2527 sprintf(er, "Error %d", err);
2528 return er;
2529}
2530#endif
2531
2532/*
2533 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2534 * Return OK for success, FAIL for failure.
2535 */
2536 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002537mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539#if defined(USE_GETCWD)
2540 if (getcwd((char *)buf, len) == NULL)
2541 {
2542 STRCPY(buf, strerror(errno));
2543 return FAIL;
2544 }
2545 return OK;
2546#else
2547 return (getwd((char *)buf) != NULL ? OK : FAIL);
2548#endif
2549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002552 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 *
2554 * return FAIL for failure, OK for success
2555 */
2556 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002557mch_FullName(
2558 char_u *fname,
2559 char_u *buf,
2560 int len,
2561 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002564#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 int fd = -1;
2566 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 char_u olddir[MAXPATHL];
2569 char_u *p;
2570 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002571#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002572 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2573 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002574#endif
2575
Bram Moolenaar38323e42007-03-06 19:22:53 +00002576#ifdef VMS
2577 fname = vms_fixfilename(fname);
2578#endif
2579
Bram Moolenaara2442432007-04-26 14:26:37 +00002580#ifdef __CYGWIN__
2581 /*
2582 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2583 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002584# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002585 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2586 * a forward slash. */
2587 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2588 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002589# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002590 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002591# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002592 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002593#endif
2594
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002595 /* Expand it if forced or not an absolute path.
2596 * Do not do it for "/file", the result is always "/". */
2597 if ((force || !mch_isFullName(fname))
2598 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 /*
2601 * If the file name has a path, change to that directory for a moment,
2602 * and then do the getwd() (and get back to where we were).
2603 * This will get the correct path name with "../" things.
2604 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002605 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002607#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 /*
2609 * Use fchdir() if possible, it's said to be faster and more
2610 * reliable. But on SunOS 4 it might not work. Check this by
2611 * doing a fchdir() right now.
2612 */
2613 if (!dont_fchdir)
2614 {
2615 fd = open(".", O_RDONLY | O_EXTRA, 0);
2616 if (fd >= 0 && fchdir(fd) < 0)
2617 {
2618 close(fd);
2619 fd = -1;
2620 dont_fchdir = TRUE; /* don't try again */
2621 }
2622 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 /* Only change directory when we are sure we can return to where
2626 * we are now. After doing "su" chdir(".") might not work. */
2627 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002628#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 (mch_dirname(olddir, MAXPATHL) == FAIL
2632 || mch_chdir((char *)olddir) != 0))
2633 {
2634 p = NULL; /* can't get current dir: don't chdir */
2635 retval = FAIL;
2636 }
2637 else
2638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 /* The directory is copied into buf[], to be able to remove
2640 * the file name without changing it (could be a string in
2641 * read-only memory) */
2642 if (p - fname >= len)
2643 retval = FAIL;
2644 else
2645 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002646 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (mch_chdir((char *)buf))
2648 retval = FAIL;
2649 else
2650 fname = p + 1;
2651 *buf = NUL;
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654 }
2655 if (mch_dirname(buf, len) == FAIL)
2656 {
2657 retval = FAIL;
2658 *buf = NUL;
2659 }
2660 if (p != NULL)
2661 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002662#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (fd >= 0)
2664 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002665 if (p_verbose >= 5)
2666 {
2667 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002668 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002669 verbose_leave();
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 l = fchdir(fd);
2672 close(fd);
2673 }
2674 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 l = mch_chdir((char *)olddir);
2677 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680
2681 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002682 if (l >= len - 1)
2683 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002684#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002685 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002687 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002690
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002692 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 return FAIL;
2694
2695 /* Do not append ".", "/dir/." is equal to "/dir". */
2696 if (STRCMP(fname, ".") != 0)
2697 STRCAT(buf, fname);
2698
2699 return OK;
2700}
2701
2702/*
2703 * Return TRUE if "fname" does not depend on the current directory.
2704 */
2705 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002706mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002708#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return ( fname[0] == '/' || fname[0] == '.' ||
2710 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2711 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2712 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002713#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#endif
2716}
2717
Bram Moolenaar24552be2005-12-10 20:17:30 +00002718#if defined(USE_FNAME_CASE) || defined(PROTO)
2719/*
2720 * Set the case of the file name, if it already exists. This will cause the
2721 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002722 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002723 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002724 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002725fname_case(
2726 char_u *name,
2727 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002728{
2729 struct stat st;
2730 char_u *slash, *tail;
2731 DIR *dirp;
2732 struct dirent *dp;
2733
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002734 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002735 {
2736 /* Open the directory where the file is located. */
2737 slash = vim_strrchr(name, '/');
2738 if (slash == NULL)
2739 {
2740 dirp = opendir(".");
2741 tail = name;
2742 }
2743 else
2744 {
2745 *slash = NUL;
2746 dirp = opendir((char *)name);
2747 *slash = '/';
2748 tail = slash + 1;
2749 }
2750
2751 if (dirp != NULL)
2752 {
2753 while ((dp = readdir(dirp)) != NULL)
2754 {
2755 /* Only accept names that differ in case and are the same byte
2756 * length. TODO: accept different length name. */
2757 if (STRICMP(tail, dp->d_name) == 0
2758 && STRLEN(tail) == STRLEN(dp->d_name))
2759 {
2760 char_u newname[MAXPATHL + 1];
2761 struct stat st2;
2762
2763 /* Verify the inode is equal. */
2764 vim_strncpy(newname, name, MAXPATHL);
2765 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2766 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002767 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002768 && st.st_ino == st2.st_ino
2769 && st.st_dev == st2.st_dev)
2770 {
2771 STRCPY(tail, dp->d_name);
2772 break;
2773 }
2774 }
2775 }
2776
2777 closedir(dirp);
2778 }
2779 }
2780}
2781#endif
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Get file permissions for 'name'.
2785 * Returns -1 when it doesn't exist.
2786 */
2787 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002788mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 struct stat statb;
2791
2792 /* Keep the #ifdef outside of stat(), it may be a macro. */
2793#ifdef VMS
2794 if (stat((char *)vms_fixfilename(name), &statb))
2795#else
2796 if (stat((char *)name, &statb))
2797#endif
2798 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002799#ifdef __INTERIX
2800 /* The top bit makes the value negative, which means the file doesn't
2801 * exist. Remove the bit, we don't use it. */
2802 return statb.st_mode & ~S_ADDACE;
2803#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806}
2807
2808/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002809 * Set file permission for "name" to "perm".
2810 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 */
2812 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002813mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
2815 return (chmod((char *)
2816#ifdef VMS
2817 vms_fixfilename(name),
2818#else
2819 name,
2820#endif
2821 (mode_t)perm) == 0 ? OK : FAIL);
2822}
2823
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002824#if defined(HAVE_FCHMOD) || defined(PROTO)
2825/*
2826 * Set file permission for open file "fd" to "perm".
2827 * Return FAIL for failure, OK otherwise.
2828 */
2829 int
2830mch_fsetperm(int fd, long perm)
2831{
2832 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2833}
2834#endif
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836#if defined(HAVE_ACL) || defined(PROTO)
2837# ifdef HAVE_SYS_ACL_H
2838# include <sys/acl.h>
2839# endif
2840# ifdef HAVE_SYS_ACCESS_H
2841# include <sys/access.h>
2842# endif
2843
2844# ifdef HAVE_SOLARIS_ACL
2845typedef struct vim_acl_solaris_T {
2846 int acl_cnt;
2847 aclent_t *acl_entry;
2848} vim_acl_solaris_T;
2849# endif
2850
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002851#if defined(HAVE_SELINUX) || defined(PROTO)
2852/*
2853 * Copy security info from "from_file" to "to_file".
2854 */
2855 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002856mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002857{
2858 if (from_file == NULL)
2859 return;
2860
2861 if (selinux_enabled == -1)
2862 selinux_enabled = is_selinux_enabled();
2863
2864 if (selinux_enabled > 0)
2865 {
2866 security_context_t from_context = NULL;
2867 security_context_t to_context = NULL;
2868
2869 if (getfilecon((char *)from_file, &from_context) < 0)
2870 {
2871 /* If the filesystem doesn't support extended attributes,
2872 the original had no special security context and the
2873 target cannot have one either. */
2874 if (errno == EOPNOTSUPP)
2875 return;
2876
Bram Moolenaar32526b32019-01-19 17:43:09 +01002877 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002878 msg_outtrans(from_file);
2879 msg_putchar('\n');
2880 return;
2881 }
2882 if (getfilecon((char *)to_file, &to_context) < 0)
2883 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002884 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002885 msg_outtrans(to_file);
2886 msg_putchar('\n');
2887 freecon (from_context);
2888 return ;
2889 }
2890 if (strcmp(from_context, to_context) != 0)
2891 {
2892 if (setfilecon((char *)to_file, from_context) < 0)
2893 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002894 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002895 msg_outtrans(to_file);
2896 msg_putchar('\n');
2897 }
2898 }
2899 freecon(to_context);
2900 freecon(from_context);
2901 }
2902}
2903#endif /* HAVE_SELINUX */
2904
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002905#if defined(HAVE_SMACK) && !defined(PROTO)
2906/*
2907 * Copy security info from "from_file" to "to_file".
2908 */
2909 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002910mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002911{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002912 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002913 {
2914 XATTR_NAME_SMACK,
2915 XATTR_NAME_SMACKEXEC,
2916 XATTR_NAME_SMACKMMAP
2917 };
2918
2919 char buffer[SMACK_LABEL_LEN];
2920 const char *name;
2921 int index;
2922 int ret;
2923 ssize_t size;
2924
2925 if (from_file == NULL)
2926 return;
2927
2928 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2929 / sizeof(smack_copied_attributes)[0]) ; index++)
2930 {
2931 /* get the name of the attribute to copy */
2932 name = smack_copied_attributes[index];
2933
2934 /* get the value of the attribute in buffer */
2935 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2936 if (size >= 0)
2937 {
2938 /* copy the attribute value of buffer */
2939 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2940 if (ret < 0)
2941 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002942 vim_snprintf((char *)IObuff, IOSIZE,
2943 _("Could not set security context %s for %s"),
2944 name, to_file);
2945 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002946 msg_putchar('\n');
2947 }
2948 }
2949 else
2950 {
2951 /* what reason of not having the attribute value? */
2952 switch (errno)
2953 {
2954 case ENOTSUP:
2955 /* extended attributes aren't supported or enabled */
2956 /* should a message be echoed? not sure... */
Bram Moolenaarb2148f52019-01-20 23:43:57 +01002957 return; /* leave because it isn't useful to continue */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002958
2959 case ERANGE:
2960 default:
2961 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002962 vim_snprintf((char *)IObuff, IOSIZE,
2963 _("Could not get security context %s for %s. Removing it!"),
2964 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002965 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002966 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002967 /* FALLTHROUGH to remove the attribute */
2968
2969 case ENODATA:
2970 /* no attribute of this name */
2971 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002972 /* Silently ignore errors, apparently this happens when
2973 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002974 break;
2975 }
2976 }
2977 }
2978}
2979#endif /* HAVE_SMACK */
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981/*
2982 * Return a pointer to the ACL of file "fname" in allocated memory.
2983 * Return NULL if the ACL is not available for whatever reason.
2984 */
2985 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002986mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 vim_acl_T ret = NULL;
2989#ifdef HAVE_POSIX_ACL
2990 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2991#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002992#ifdef HAVE_SOLARIS_ZFS_ACL
2993 acl_t *aclent;
2994
2995 if (acl_get((char *)fname, 0, &aclent) < 0)
2996 return NULL;
2997 ret = (vim_acl_T)aclent;
2998#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#ifdef HAVE_SOLARIS_ACL
3000 vim_acl_solaris_T *aclent;
3001
3002 aclent = malloc(sizeof(vim_acl_solaris_T));
3003 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3004 {
3005 free(aclent);
3006 return NULL;
3007 }
3008 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3009 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3010 {
3011 free(aclent->acl_entry);
3012 free(aclent);
3013 return NULL;
3014 }
3015 ret = (vim_acl_T)aclent;
3016#else
3017#if defined(HAVE_AIX_ACL)
3018 int aclsize;
3019 struct acl *aclent;
3020
3021 aclsize = sizeof(struct acl);
3022 aclent = malloc(aclsize);
3023 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3024 {
3025 if (errno == ENOSPC)
3026 {
3027 aclsize = aclent->acl_len;
3028 aclent = realloc(aclent, aclsize);
3029 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3030 {
3031 free(aclent);
3032 return NULL;
3033 }
3034 }
3035 else
3036 {
3037 free(aclent);
3038 return NULL;
3039 }
3040 }
3041 ret = (vim_acl_T)aclent;
3042#endif /* HAVE_AIX_ACL */
3043#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003044#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045#endif /* HAVE_POSIX_ACL */
3046 return ret;
3047}
3048
3049/*
3050 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3051 */
3052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003053mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 if (aclent == NULL)
3056 return;
3057#ifdef HAVE_POSIX_ACL
3058 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3059#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003060#ifdef HAVE_SOLARIS_ZFS_ACL
3061 acl_set((char *)fname, (acl_t *)aclent);
3062#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#ifdef HAVE_SOLARIS_ACL
3064 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3065 ((vim_acl_solaris_T *)aclent)->acl_entry);
3066#else
3067#ifdef HAVE_AIX_ACL
3068 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3069#endif /* HAVE_AIX_ACL */
3070#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003071#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#endif /* HAVE_POSIX_ACL */
3073}
3074
3075 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003076mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 if (aclent == NULL)
3079 return;
3080#ifdef HAVE_POSIX_ACL
3081 acl_free((acl_t)aclent);
3082#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003083#ifdef HAVE_SOLARIS_ZFS_ACL
3084 acl_free((acl_t *)aclent);
3085#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#ifdef HAVE_SOLARIS_ACL
3087 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3088 free(aclent);
3089#else
3090#ifdef HAVE_AIX_ACL
3091 free(aclent);
3092#endif /* HAVE_AIX_ACL */
3093#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003094#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#endif /* HAVE_POSIX_ACL */
3096}
3097#endif
3098
3099/*
3100 * Set hidden flag for "name".
3101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003103mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 /* can't hide a file */
3106}
3107
3108/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003109 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 * return FALSE if "name" is not a directory
3111 * return FALSE for error
3112 */
3113 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003114mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 struct stat statb;
3117
3118 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3119 return FALSE;
3120 if (stat((char *)name, &statb))
3121 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003125/*
3126 * return TRUE if "name" is a directory, NOT a symlink to a directory
3127 * return FALSE if "name" is not a directory
3128 * return FALSE for error
3129 */
3130 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003131mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003132{
3133 struct stat statb;
3134
3135 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3136 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003137 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003138 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003139 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003140}
3141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142/*
3143 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3144 */
3145 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003146executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
3148 struct stat st;
3149
3150 if (stat((char *)name, &st))
3151 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003152#ifdef VMS
3153 /* Like on Unix system file can have executable rights but not necessarily
3154 * be an executable, but on Unix is not a default for an ordianry file to
3155 * have an executable flag - on VMS it is in most cases.
3156 * Therefore, this check does not have any sense - let keep us to the
3157 * conventions instead:
3158 * *.COM and *.EXE files are the executables - the rest are not. This is
3159 * not ideal but better then it was.
3160 */
3161 int vms_executable = 0;
3162 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3163 {
3164 if (strstr(vms_tolower((char*)name),".exe") != NULL
3165 || strstr(vms_tolower((char*)name),".com")!= NULL)
3166 vms_executable = 1;
3167 }
3168 return vms_executable;
3169#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172}
3173
3174/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003175 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003176 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * Return -1 if unknown.
3178 */
3179 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003180mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 char_u *buf;
3183 char_u *p, *e;
3184 int retval;
3185
Bram Moolenaarb5971142015-03-21 17:32:19 +01003186 /* When "use_path" is false and if it's an absolute or relative path don't
3187 * need to use $PATH. */
3188 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3189 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003190 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003191 /* There must be a path separator, files in the current directory
3192 * can't be executed. */
3193 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003194 {
3195 if (path != NULL)
3196 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003197 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003198 *path = FullName_save(name, TRUE);
3199 else
3200 *path = vim_strsave(name);
3201 }
3202 return TRUE;
3203 }
3204 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 p = (char_u *)getenv("PATH");
3208 if (p == NULL || *p == NUL)
3209 return -1;
3210 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3211 if (buf == NULL)
3212 return -1;
3213
3214 /*
3215 * Walk through all entries in $PATH to check if "name" exists there and
3216 * is an executable file.
3217 */
3218 for (;;)
3219 {
3220 e = (char_u *)strchr((char *)p, ':');
3221 if (e == NULL)
3222 e = p + STRLEN(p);
3223 if (e - p <= 1) /* empty entry means current dir */
3224 STRCPY(buf, "./");
3225 else
3226 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003227 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 add_pathsep(buf);
3229 }
3230 STRCAT(buf, name);
3231 retval = executable_file(buf);
3232 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003233 {
3234 if (path != NULL)
3235 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003236 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003237 *path = FullName_save(buf, TRUE);
3238 else
3239 *path = vim_strsave(buf);
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 if (*e != ':')
3245 break;
3246 p = e + 1;
3247 }
3248
3249 vim_free(buf);
3250 return retval;
3251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253/*
3254 * Check what "name" is:
3255 * NODE_NORMAL: file or directory (or doesn't exist)
3256 * NODE_WRITABLE: writable device, socket, fifo, etc.
3257 * NODE_OTHER: non-writable things
3258 */
3259 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003260mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 struct stat st;
3263
3264 if (stat((char *)name, &st))
3265 return NODE_NORMAL;
3266 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3267 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3269 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 /* Everything else is writable? */
3271 return NODE_WRITABLE;
3272}
3273
3274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003275mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277#ifdef HAVE_CHECK_STACK_GROWTH
3278 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 check_stack_growth((char *)&i);
3281
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003282# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 get_stack_limit();
3284# endif
3285
3286#endif
3287
3288 /*
3289 * Setup an alternative stack for signals. Helps to catch signals when
3290 * running out of stack space.
3291 * Use of sigaltstack() is preferred, it's more portable.
3292 * Ignore any errors.
3293 */
3294#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003295 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 init_signal_stack();
3297#endif
3298}
3299
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003300#if defined(EXITFREE) || defined(PROTO)
3301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003302mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003303{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003304# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3305 if (clip_star.owned)
3306 clip_lose_selection(&clip_star);
3307 if (clip_plus.owned)
3308 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003309# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003310# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003311 if (xterm_Shell != (Widget)0)
3312 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003313# ifndef LESSTIF_VERSION
3314 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003315 if (xterm_dpy != NULL)
3316 XtCloseDisplay(xterm_dpy);
3317 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003318 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003319 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003320# ifdef FEAT_X11
3321 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3322# endif
3323 }
3324# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003325# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003326# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003327 if (x11_display != NULL
3328# ifdef FEAT_XCLIPBOARD
3329 && x11_display != xterm_dpy
3330# endif
3331 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003332 XCloseDisplay(x11_display);
3333# endif
3334# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003335 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003336# endif
3337# ifdef FEAT_TITLE
3338 vim_free(oldtitle);
3339 vim_free(oldicon);
3340# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003341}
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * Output a newline when exiting.
3346 * Make sure the newline goes to the same stream as the text.
3347 */
3348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003349exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003351 if (silent_mode)
3352 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (newline_on_exit || msg_didout)
3354 {
3355 if (msg_use_printf())
3356 {
3357 if (info_message)
3358 mch_msg("\n");
3359 else
3360 mch_errmsg("\r\n");
3361 }
3362 else
3363 out_char('\n');
3364 }
3365 else
3366 {
3367 restore_cterm_colors(); /* get original colors back */
3368 msg_clr_eos_force(); /* clear the rest of the display */
3369 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3370 }
3371}
3372
3373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003374mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 exiting = TRUE;
3377
3378#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3379 x11_export_final_selection();
3380#endif
3381
3382#ifdef FEAT_GUI
3383 if (!gui.in_use)
3384#endif
3385 {
3386 settmode(TMODE_COOK);
3387#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003388 // restore xterm title and icon name
3389 mch_restore_title(SAVE_RESTORE_BOTH);
3390 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#endif
3392 /*
3393 * When t_ti is not empty but it doesn't cause swapping terminal
3394 * pages, need to output a newline when msg_didout is set. But when
3395 * t_ti does swap pages it should not go to the shell page. Do this
3396 * before stoptermcap().
3397 */
3398 if (swapping_screen() && !newline_on_exit)
3399 exit_scroll();
3400
3401 /* Stop termcap: May need to check for T_CRV response, which
3402 * requires RAW mode. */
3403 stoptermcap();
3404
3405 /*
3406 * A newline is only required after a message in the alternate screen.
3407 * This is set to TRUE by wait_return().
3408 */
3409 if (!swapping_screen() || newline_on_exit)
3410 exit_scroll();
3411
3412 /* Cursor may have been switched off without calling starttermcap()
3413 * when doing "vim -u vimrc" and vimrc contains ":q". */
3414 if (full_screen)
3415 cursor_on();
3416 }
3417 out_flush();
3418 ml_close_all(TRUE); /* remove all memfiles */
3419 may_core_dump();
3420#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 gui_exit(r);
3423#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003424
Bram Moolenaar56718732006-03-15 22:53:57 +00003425#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003426 mac_conv_cleanup();
3427#endif
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#ifdef __QNX__
3430 /* A core dump won't be created if the signal handler
3431 * doesn't return, so we can't call exit() */
3432 if (deadly_signal != 0)
3433 return;
3434#endif
3435
Bram Moolenaar009b2592004-10-24 19:18:58 +00003436#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003437 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003438#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003439
3440#ifdef EXITFREE
3441 free_all_mem();
3442#endif
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 exit(r);
3445}
3446
3447 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003448may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 if (deadly_signal != 0)
3451 {
3452 signal(deadly_signal, SIG_DFL);
3453 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3454 }
3455}
3456
3457#ifndef VMS
3458
3459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003460mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 static int first = TRUE;
3463
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003464#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465# ifdef HAVE_TERMIOS_H
3466 static struct termios told;
3467 struct termios tnew;
3468# else
3469 static struct termio told;
3470 struct termio tnew;
3471# endif
3472
3473 if (first)
3474 {
3475 first = FALSE;
3476# if defined(HAVE_TERMIOS_H)
3477 tcgetattr(read_cmd_fd, &told);
3478# else
3479 ioctl(read_cmd_fd, TCGETA, &told);
3480# endif
3481 }
3482
3483 tnew = told;
3484 if (tmode == TMODE_RAW)
3485 {
3486 /*
3487 * ~ICRNL enables typing ^V^M
3488 */
3489 tnew.c_iflag &= ~ICRNL;
3490 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3491# if defined(IEXTEN) && !defined(__MINT__)
3492 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3493 /* but it breaks function keys on MINT */
3494# endif
3495 );
3496# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3497 tnew.c_oflag &= ~ONLCR;
3498# endif
3499 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3500 tnew.c_cc[VTIME] = 0; /* don't wait */
3501 }
3502 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003503 {
3504 /* Also reset ICANON here, otherwise on Solaris select() won't see
3505 * typeahead characters. */
3506 tnew.c_lflag &= ~(ICANON | ECHO);
3507 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3508 tnew.c_cc[VTIME] = 0; /* don't wait */
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511# if defined(HAVE_TERMIOS_H)
3512 {
3513 int n = 10;
3514
3515 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3516 * few times. */
3517 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3518 && errno == EINTR && n > 0)
3519 --n;
3520 }
3521# else
3522 ioctl(read_cmd_fd, TCSETA, &tnew);
3523# endif
3524
3525#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /*
3527 * for "old" tty systems
3528 */
3529# ifndef TIOCSETN
3530# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3531# endif
3532 static struct sgttyb ttybold;
3533 struct sgttyb ttybnew;
3534
3535 if (first)
3536 {
3537 first = FALSE;
3538 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3539 }
3540
3541 ttybnew = ttybold;
3542 if (tmode == TMODE_RAW)
3543 {
3544 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3545 ttybnew.sg_flags |= RAW;
3546 }
3547 else if (tmode == TMODE_SLEEP)
3548 ttybnew.sg_flags &= ~(ECHO);
3549 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3550#endif
3551 curr_tmode = tmode;
3552}
3553
3554/*
3555 * Try to get the code for "t_kb" from the stty setting
3556 *
3557 * Even if termcap claims a backspace key, the user's setting *should*
3558 * prevail. stty knows more about reality than termcap does, and if
3559 * somebody's usual erase key is DEL (which, for most BSD users, it will
3560 * be), they're going to get really annoyed if their erase key starts
3561 * doing forward deletes for no reason. (Eric Fischer)
3562 */
3563 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003564get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003566 ttyinfo_T info;
3567 char_u buf[2];
3568 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003570 if (get_tty_info(read_cmd_fd, &info) == OK)
3571 {
3572 intr_char = info.interrupt;
3573 buf[0] = info.backspace;
3574 buf[1] = NUL;
3575 add_termcode((char_u *)"kb", buf, FALSE);
3576
3577 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3578 p = find_termcode((char_u *)"kD");
3579 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3580 do_fixdel(NULL);
3581 }
3582}
3583
3584/*
3585 * Obtain the characters that Backspace and Enter produce on "fd".
3586 * Returns OK or FAIL.
3587 */
3588 int
3589get_tty_info(int fd, ttyinfo_T *info)
3590{
3591#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592# ifdef HAVE_TERMIOS_H
3593 struct termios keys;
3594# else
3595 struct termio keys;
3596# endif
3597
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003598 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003600 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003602 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003604 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003606 info->backspace = keys.c_cc[VERASE];
3607 info->interrupt = keys.c_cc[VINTR];
3608 if (keys.c_iflag & ICRNL)
3609 info->enter = NL;
3610 else
3611 info->enter = CAR;
3612 if (keys.c_oflag & ONLCR)
3613 info->nl_does_cr = TRUE;
3614 else
3615 info->nl_does_cr = FALSE;
3616 return OK;
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#else
3619 /* for "old" tty systems */
3620 struct sgttyb keys;
3621
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003622 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003624 info->backspace = keys.sg_erase;
3625 info->interrupt = keys.sg_kill;
3626 info->enter = CAR;
3627 info->nl_does_cr = TRUE;
3628 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003631 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632}
3633
3634#endif /* VMS */
3635
3636#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003637static int mouse_ison = FALSE;
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639/*
3640 * Set mouse clicks on or off.
3641 */
3642 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003643mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003645# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003646 static int bevalterm_ison = FALSE;
3647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 int xterm_mouse_vers;
3649
Bram Moolenaara06afc72018-08-27 23:24:16 +02003650# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3651 if (!on)
3652 // Make sure not tracing mouse movements. Important when a button-down
3653 // was received but no release yet.
3654 stop_xterm_trace();
3655# endif
3656
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003657 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003658# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003659 && p_bevalterm == bevalterm_ison
3660# endif
3661 )
3662 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return;
3664
3665 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003666
3667# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003668 if (ttym_flags == TTYM_URXVT)
3669 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003670 out_str_nf((char_u *)
3671 (on
3672 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3673 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003674 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003675 }
3676# endif
3677
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003678# ifdef FEAT_MOUSE_SGR
3679 if (ttym_flags == TTYM_SGR)
3680 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003681 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003682 out_str_nf((char_u *)
3683 (on
3684 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3685 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686 mouse_ison = on;
3687 }
3688# endif
3689
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003690# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003691 if (bevalterm_ison != (p_bevalterm && on))
3692 {
3693 bevalterm_ison = (p_bevalterm && on);
3694 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3695 /* disable mouse movement events, enabling is below */
3696 out_str_nf((char_u *)
3697 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003698 }
3699# endif
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 if (xterm_mouse_vers > 0)
3702 {
3703 if (on) /* enable mouse events, use mouse tracking if available */
3704 out_str_nf((char_u *)
3705 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003706 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003707# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003708 bevalterm_ison
3709 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3710# endif
3711 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3713 else /* disable mouse events, could probably always send the same */
3714 out_str_nf((char_u *)
3715 (xterm_mouse_vers > 1
3716 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3717 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003718 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720
3721# ifdef FEAT_MOUSE_DEC
3722 else if (ttym_flags == TTYM_DEC)
3723 {
3724 if (on) /* enable mouse events */
3725 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3726 else /* disable mouse events */
3727 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003728 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730# endif
3731
3732# ifdef FEAT_MOUSE_GPM
3733 else
3734 {
3735 if (on)
3736 {
3737 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003738 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 else
3741 {
3742 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003743 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746# endif
3747
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003748# ifdef FEAT_SYSMOUSE
3749 else
3750 {
3751 if (on)
3752 {
3753 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003755 }
3756 else
3757 {
3758 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003760 }
3761 }
3762# endif
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764# ifdef FEAT_MOUSE_JSB
3765 else
3766 {
3767 if (on)
3768 {
3769 /* D - Enable Mouse up/down messages
3770 * L - Enable Left Button Reporting
3771 * M - Enable Middle Button Reporting
3772 * R - Enable Right Button Reporting
3773 * K - Enable SHIFT and CTRL key Reporting
3774 * + - Enable Advanced messaging of mouse moves and up/down messages
3775 * Q - Quiet No Ack
3776 * # - Numeric value of mouse pointer required
3777 * 0 = Multiview 2000 cursor, used as standard
3778 * 1 = Windows Arrow
3779 * 2 = Windows I Beam
3780 * 3 = Windows Hour Glass
3781 * 4 = Windows Cross Hair
3782 * 5 = Windows UP Arrow
3783 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003784# ifdef JSBTERM_MOUSE_NONADVANCED
3785 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3787 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003788# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3790 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003791# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003792 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 else
3795 {
3796 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3797 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003798 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 }
3801# endif
3802# ifdef FEAT_MOUSE_PTERM
3803 else
3804 {
3805 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3806 if (on)
3807 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3808 else
3809 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003810 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812# endif
3813}
3814
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003815#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003816/*
3817 * Called when 'balloonevalterm' changed.
3818 */
3819 void
3820mch_bevalterm_changed(void)
3821{
3822 mch_setmouse(mouse_ison);
3823}
3824#endif
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826/*
3827 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3828 */
3829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003830check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832# ifdef FEAT_MOUSE_XTERM
3833 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003834# ifdef FEAT_MOUSE_URXVT
3835 && use_xterm_mouse() != 3
3836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837# ifdef FEAT_GUI
3838 && !gui.in_use
3839# endif
3840 )
3841 {
3842 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003843 ? IF_EB("\233M", CSI_STR "M")
3844 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (*p_mouse != NUL)
3846 {
3847 /* force mouse off and maybe on to send possibly new mouse
3848 * activation sequence to the xterm, with(out) drag tracing. */
3849 mch_setmouse(FALSE);
3850 setmouse();
3851 }
3852 }
3853 else
3854 del_mouse_termcode(KS_MOUSE);
3855# endif
3856
3857# ifdef FEAT_MOUSE_GPM
3858 if (!use_xterm_mouse()
3859# ifdef FEAT_GUI
3860 && !gui.in_use
3861# endif
3862 )
3863 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3864# endif
3865
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003866# ifdef FEAT_SYSMOUSE
3867 if (!use_xterm_mouse()
3868# ifdef FEAT_GUI
3869 && !gui.in_use
3870# endif
3871 )
3872 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3873# endif
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003876 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (!use_xterm_mouse()
3878# ifdef FEAT_GUI
3879 && !gui.in_use
3880# endif
3881 )
3882 set_mouse_termcode(KS_JSBTERM_MOUSE,
3883 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3884 else
3885 del_mouse_termcode(KS_JSBTERM_MOUSE);
3886# endif
3887
3888# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003889 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * define it in the GUI or when using an xterm. */
3891 if (!use_xterm_mouse()
3892# ifdef FEAT_GUI
3893 && !gui.in_use
3894# endif
3895 )
3896 set_mouse_termcode(KS_NETTERM_MOUSE,
3897 (char_u *)IF_EB("\033}", ESC_STR "}"));
3898 else
3899 del_mouse_termcode(KS_NETTERM_MOUSE);
3900# endif
3901
3902# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003903 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003904 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905# ifdef FEAT_GUI
3906 && !gui.in_use
3907# endif
3908 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003909 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3910 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 else
3912 del_mouse_termcode(KS_DEC_MOUSE);
3913# endif
3914# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003915 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003916 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917# ifdef FEAT_GUI
3918 && !gui.in_use
3919# endif
3920 )
3921 set_mouse_termcode(KS_PTERM_MOUSE,
3922 (char_u *) IF_EB("\033[", ESC_STR "["));
3923 else
3924 del_mouse_termcode(KS_PTERM_MOUSE);
3925# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003926# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003927 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003928# ifdef FEAT_GUI
3929 && !gui.in_use
3930# endif
3931 )
3932 {
3933 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003934 ? IF_EB("\233*M", CSI_STR "*M")
3935 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003936
3937 if (*p_mouse != NUL)
3938 {
3939 mch_setmouse(FALSE);
3940 setmouse();
3941 }
3942 }
3943 else
3944 del_mouse_termcode(KS_URXVT_MOUSE);
3945# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003946# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003947 if (use_xterm_mouse() == 4
3948# ifdef FEAT_GUI
3949 && !gui.in_use
3950# endif
3951 )
3952 {
3953 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003954 ? IF_EB("\233<*M", CSI_STR "<*M")
3955 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3956
3957 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3958 ? IF_EB("\233<*m", CSI_STR "<*m")
3959 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003960
3961 if (*p_mouse != NUL)
3962 {
3963 mch_setmouse(FALSE);
3964 setmouse();
3965 }
3966 }
3967 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003968 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003969 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003970 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3971 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974#endif
3975
3976/*
3977 * set screen mode, always fails.
3978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003980mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003982 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return FAIL;
3984}
3985
3986#ifndef VMS
3987
3988/*
3989 * Try to get the current window size:
3990 * 1. with an ioctl(), most accurate method
3991 * 2. from the environment variables LINES and COLUMNS
3992 * 3. from the termcap
3993 * 4. keep using the old values
3994 * Return OK when size could be determined, FAIL otherwise.
3995 */
3996 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003997mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 long rows = 0;
4000 long columns = 0;
4001 char_u *p;
4002
4003 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * 1. try using an ioctl. It is the most accurate method.
4005 *
4006 * Try using TIOCGWINSZ first, some systems that have it also define
4007 * TIOCGSIZE but don't have a struct ttysize.
4008 */
4009# ifdef TIOCGWINSZ
4010 {
4011 struct winsize ws;
4012 int fd = 1;
4013
4014 /* When stdout is not a tty, use stdin for the ioctl(). */
4015 if (!isatty(fd) && isatty(read_cmd_fd))
4016 fd = read_cmd_fd;
4017 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4018 {
4019 columns = ws.ws_col;
4020 rows = ws.ws_row;
4021 }
4022 }
4023# else /* TIOCGWINSZ */
4024# ifdef TIOCGSIZE
4025 {
4026 struct ttysize ts;
4027 int fd = 1;
4028
4029 /* When stdout is not a tty, use stdin for the ioctl(). */
4030 if (!isatty(fd) && isatty(read_cmd_fd))
4031 fd = read_cmd_fd;
4032 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4033 {
4034 columns = ts.ts_cols;
4035 rows = ts.ts_lines;
4036 }
4037 }
4038# endif /* TIOCGSIZE */
4039# endif /* TIOCGWINSZ */
4040
4041 /*
4042 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004043 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4044 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004046 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
4048 if ((p = (char_u *)getenv("LINES")))
4049 rows = atoi((char *)p);
4050 if ((p = (char_u *)getenv("COLUMNS")))
4051 columns = atoi((char *)p);
4052 }
4053
4054#ifdef HAVE_TGETENT
4055 /*
4056 * 3. try reading "co" and "li" entries from termcap
4057 */
4058 if (columns == 0 || rows == 0)
4059 getlinecol(&columns, &rows);
4060#endif
4061
4062 /*
4063 * 4. If everything fails, use the old values
4064 */
4065 if (columns <= 0 || rows <= 0)
4066 return FAIL;
4067
4068 Rows = rows;
4069 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004070 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return OK;
4072}
4073
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004074#if defined(FEAT_TERMINAL) || defined(PROTO)
4075/*
4076 * Report the windows size "rows" and "cols" to tty "fd".
4077 */
4078 int
4079mch_report_winsize(int fd, int rows, int cols)
4080{
4081# ifdef TIOCSWINSZ
4082 struct winsize ws;
4083
4084 ws.ws_col = cols;
4085 ws.ws_row = rows;
4086 ws.ws_xpixel = cols * 5;
4087 ws.ws_ypixel = rows * 10;
4088 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4089 {
4090 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4091 return OK;
4092 }
4093 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4094# else
4095# ifdef TIOCSSIZE
4096 struct ttysize ts;
4097
4098 ts.ts_cols = cols;
4099 ts.ts_lines = rows;
4100 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4101 {
4102 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4103 return OK;
4104 }
4105 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4106# endif
4107# endif
4108 return FAIL;
4109}
4110#endif
4111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112/*
4113 * Try to set the window size to Rows and Columns.
4114 */
4115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004116mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 if (*T_CWS)
4119 {
4120 /*
4121 * NOTE: if you get an error here that term_set_winsize() is
4122 * undefined, check the output of configure. It could probably not
4123 * find a ncurses, termcap or termlib library.
4124 */
4125 term_set_winsize((int)Rows, (int)Columns);
4126 out_flush();
4127 screen_start(); /* don't know where cursor is now */
4128 }
4129}
4130
4131#endif /* VMS */
4132
4133/*
4134 * Rows and/or Columns has changed.
4135 */
4136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004137mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 /* Nothing to do. */
4140}
4141
Bram Moolenaar205b8862011-09-07 15:04:31 +02004142/*
4143 * Wait for process "child" to end.
4144 * Return "child" if it exited properly, <= 0 on error.
4145 */
4146 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004147wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004148{
4149 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004150 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004151
4152 while (wait_pid != child)
4153 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004154 /* When compiled with Python threads are probably used, in which case
4155 * wait() sometimes hangs for no obvious reason. Use waitpid()
4156 * instead and loop (like the GUI). Also needed for other interfaces,
4157 * they might call system(). */
4158# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004159 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004160# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004161 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004162# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004163 if (wait_pid == 0)
4164 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004165 /* Wait for 1 to 10 msec before trying again. */
4166 mch_delay(delay_msec, TRUE);
4167 if (++delay_msec > 10)
4168 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004169 continue;
4170 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004171 if (wait_pid <= 0
4172# ifdef ECHILD
4173 && errno == ECHILD
4174# endif
4175 )
4176 break;
4177 }
4178 return wait_pid;
4179}
4180
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004181#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004182/*
4183 * Set the environment for a child process.
4184 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004185 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004186set_child_environment(
4187 long rows,
4188 long columns,
4189 char *term,
4190 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004191{
4192# ifdef HAVE_SETENV
4193 char envbuf[50];
4194# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004195 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004196 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004197 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004198 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004199 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004200# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004201 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004202# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004203# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004204 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004205# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004206# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004207 long colors =
4208# ifdef FEAT_GUI
4209 gui.in_use ? 256*256*256 :
4210# endif
4211 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004212
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004213# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004214 setenv("TERM", term, 1);
4215 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004216 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004217 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004218 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004219 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004220 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004221 sprintf((char *)envbuf, "%ld", colors);
4222 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004223# ifdef FEAT_TERMINAL
4224 if (is_terminal)
4225 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004226 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004227 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4228 }
4229# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004230# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004231 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004232# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233# else
4234 /*
4235 * Putenv does not copy the string, it has to remain valid.
4236 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004237 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004238 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004239 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4240 putenv(envbuf_Term);
4241 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004242 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004243 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4244 putenv(envbuf_Lines);
4245 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4246 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004248 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4249 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004250# ifdef FEAT_TERMINAL
4251 if (is_terminal)
4252 {
4253 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004254 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004255 putenv(envbuf_Version);
4256 }
4257# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004258# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004259 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4260 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4261 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004262# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004263# endif
4264}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004265
4266 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004267set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004268{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004269 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004270}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004271#endif
4272
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004273#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004274/*
4275 * Open a PTY, with FD for the master and slave side.
4276 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4277 * When successful both file descriptors are stored.
4278 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004279 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004280open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004281{
4282 char *tty_name;
4283
4284 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4285 if (*pty_master_fd >= 0)
4286 {
4287 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4288 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4289 * adding O_NOCTTY always works when defined. */
4290#ifdef O_NOCTTY
4291 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4292#else
4293 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4294#endif
4295 if (*pty_slave_fd < 0)
4296 {
4297 close(*pty_master_fd);
4298 *pty_master_fd = -1;
4299 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004300 else if (namep != NULL)
4301 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004302 }
4303}
4304#endif
4305
Bram Moolenaarfae42832017-08-01 22:24:26 +02004306/*
4307 * Send SIGINT to a child process if "c" is an interrupt character.
4308 */
4309 void
4310may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4311{
4312# ifdef SIGINT
4313 if (c == Ctrl_C || c == intr_char)
4314 {
4315# ifdef HAVE_SETSID
4316 kill(-pid, SIGINT);
4317# else
4318 kill(0, SIGINT);
4319# endif
4320 if (wpid > 0)
4321 kill(wpid, SIGINT);
4322 }
4323# endif
4324}
4325
Bram Moolenaar13568252018-03-16 20:46:58 +01004326#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4327
4328 static int
4329build_argv(
4330 char_u *cmd,
4331 char ***argvp,
4332 char_u **sh_tofree,
4333 char_u **shcf_tofree)
4334{
4335 char **argv = NULL;
4336 int argc;
4337
4338 *sh_tofree = vim_strsave(p_sh);
4339 if (*sh_tofree == NULL) /* out of memory */
4340 return FAIL;
4341
4342 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4343 return FAIL;
4344 *argvp = argv;
4345
4346 if (cmd != NULL)
4347 {
4348 char_u *s;
4349 char_u *p;
4350
4351 if (extra_shell_arg != NULL)
4352 argv[argc++] = (char *)extra_shell_arg;
4353
4354 /* Break 'shellcmdflag' into white separated parts. This doesn't
4355 * handle quoted strings, they are very unlikely to appear. */
4356 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4357 if (*shcf_tofree == NULL) /* out of memory */
4358 return FAIL;
4359 s = *shcf_tofree;
4360 p = p_shcf;
4361 while (*p != NUL)
4362 {
4363 argv[argc++] = (char *)s;
4364 while (*p && *p != ' ' && *p != TAB)
4365 *s++ = *p++;
4366 *s++ = NUL;
4367 p = skipwhite(p);
4368 }
4369
4370 argv[argc++] = (char *)cmd;
4371 }
4372 argv[argc] = NULL;
4373 return OK;
4374}
4375#endif
4376
4377#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4378/*
4379 * Use a terminal window to run a shell command in.
4380 */
4381 static int
4382mch_call_shell_terminal(
4383 char_u *cmd,
4384 int options UNUSED) /* SHELL_*, see vim.h */
4385{
4386 jobopt_T opt;
4387 char **argv = NULL;
4388 char_u *tofree1 = NULL;
4389 char_u *tofree2 = NULL;
4390 int retval = -1;
4391 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004392 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004393 aco_save_T aco;
4394 oparg_T oa; /* operator arguments */
4395
4396 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4397 goto theend;
4398
4399 init_job_options(&opt);
4400 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4401 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004402 if (buf == NULL)
4403 goto theend;
4404
4405 job = term_getjob(buf->b_term);
4406 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004407
4408 /* Find a window to make "buf" curbuf. */
4409 aucmd_prepbuf(&aco, buf);
4410
4411 clear_oparg(&oa);
4412 while (term_use_loop())
4413 {
4414 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4415 {
4416 /* If terminal_loop() returns OK we got a key that is handled
4417 * in Normal model. We don't do redrawing anyway. */
4418 if (terminal_loop(TRUE) == OK)
4419 normal_cmd(&oa, TRUE);
4420 }
4421 else
4422 normal_cmd(&oa, TRUE);
4423 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004424 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004425 ch_log(NULL, "system command finished");
4426
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004427 job_unref(job);
4428
Bram Moolenaar13568252018-03-16 20:46:58 +01004429 /* restore curwin/curbuf and a few other things */
4430 aucmd_restbuf(&aco);
4431
4432 wait_return(TRUE);
4433 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4434
4435theend:
4436 vim_free(argv);
4437 vim_free(tofree1);
4438 vim_free(tofree2);
4439 return retval;
4440}
4441#endif
4442
4443#ifdef USE_SYSTEM
4444/*
4445 * Use system() to start the shell: simple but slow.
4446 */
4447 static int
4448mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004449 char_u *cmd,
4450 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452#ifdef VMS
4453 char *ifn = NULL;
4454 char *ofn = NULL;
4455#endif
4456 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004457 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004458 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 out_flush();
4461
4462 if (options & SHELL_COOKED)
4463 settmode(TMODE_COOK); /* set to normal mode */
4464
Bram Moolenaar62b42182010-09-21 22:09:37 +02004465# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004466 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004467 loose_clipboard();
4468# endif
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (cmd == NULL)
4471 x = system((char *)p_sh);
4472 else
4473 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004474# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (ofn = strchr((char *)cmd, '>'))
4476 *ofn++ = '\0';
4477 if (ifn = strchr((char *)cmd, '<'))
4478 {
4479 char *p;
4480
4481 *ifn++ = '\0';
4482 p = strchr(ifn,' '); /* chop off any trailing spaces */
4483 if (p)
4484 *p = '\0';
4485 }
4486 if (ofn)
4487 x = vms_sys((char *)cmd, ofn, ifn);
4488 else
4489 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004490# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 newcmd = lalloc(STRLEN(p_sh)
4492 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4493 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4494 if (newcmd == NULL)
4495 x = 0;
4496 else
4497 {
4498 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4499 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4500 (char *)p_shcf,
4501 (char *)cmd);
4502 x = system((char *)newcmd);
4503 vim_free(newcmd);
4504 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507# ifdef VMS
4508 x = vms_sys_status(x);
4509# endif
4510 if (emsg_silent)
4511 ;
4512 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004513 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 else if (x && !(options & SHELL_SILENT))
4515 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004516 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 msg_outnum((long)x);
4518 msg_putchar('\n');
4519 }
4520
4521 if (tmode == TMODE_RAW)
4522 settmode(TMODE_RAW); /* set to raw mode */
4523# ifdef FEAT_TITLE
4524 resettitle();
4525# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004526# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4527 restore_clipboard();
4528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004530}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar13568252018-03-16 20:46:58 +01004532#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
Bram Moolenaardf177f62005-02-22 08:39:57 +00004534# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4535 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004536# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar13568252018-03-16 20:46:58 +01004538/*
4539 * Don't use system(), use fork()/exec().
4540 */
4541 static int
4542mch_call_shell_fork(
4543 char_u *cmd,
4544 int options) /* SHELL_*, see vim.h */
4545{
4546 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004548 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 pid_t wait_pid = 0;
4550# ifdef HAVE_UNION_WAIT
4551 union wait status;
4552# else
4553 int status = -1;
4554# endif
4555 int retval = -1;
4556 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004557 char_u *tofree1 = NULL;
4558 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004561# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004563# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004564 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 int fd_fromshell[2];
4566 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004567 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 out_flush();
4570 if (options & SHELL_COOKED)
4571 settmode(TMODE_COOK); /* set to normal mode */
4572
Bram Moolenaar13568252018-03-16 20:46:58 +01004573 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004574 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577 * For the GUI, when writing the output into the buffer and when reading
4578 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4579 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004581 if ((options & (SHELL_READ|SHELL_WRITE))
4582# ifdef FEAT_GUI
4583 || (gui.in_use && show_shell_mess)
4584# endif
4585 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004587# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 /*
4589 * Try to open a master pty.
4590 * If this works, open the slave pty.
4591 * If the slave can't be opened, close the master pty.
4592 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004593 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004594 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /*
4596 * If not opening a pty or it didn't work, try using pipes.
4597 */
4598 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
4601 pipe_error = (pipe(fd_toshell) < 0);
4602 if (!pipe_error) /* pipe create OK */
4603 {
4604 pipe_error = (pipe(fd_fromshell) < 0);
4605 if (pipe_error) /* pipe create failed */
4606 {
4607 close(fd_toshell[0]);
4608 close(fd_toshell[1]);
4609 }
4610 }
4611 if (pipe_error)
4612 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004613 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 out_flush();
4615 }
4616 }
4617 }
4618
4619 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004621 SIGSET_DECL(curset)
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623# ifdef __BEOS__
4624 beos_cleanup_read_thread();
4625# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004626
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004627 BLOCK_SIGNALS(&curset);
4628 pid = fork(); /* maybe we should use vfork() */
4629 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004631 UNBLOCK_SIGNALS(&curset);
4632
Bram Moolenaar32526b32019-01-19 17:43:09 +01004633 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004634 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636 || (gui.in_use && show_shell_mess)
4637# endif
4638 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (pty_master_fd >= 0) /* close the pseudo tty */
4642 {
4643 close(pty_master_fd);
4644 close(pty_slave_fd);
4645 }
4646 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
4649 close(fd_toshell[0]);
4650 close(fd_toshell[1]);
4651 close(fd_fromshell[0]);
4652 close(fd_fromshell[1]);
4653 }
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 else if (pid == 0) /* child */
4657 {
4658 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004659 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaar819524702018-02-27 19:10:00 +01004661# ifdef FEAT_JOB_CHANNEL
4662 if (ch_log_active())
4663 /* close the log file in the child */
4664 ch_logfile((char_u *)"", (char_u *)"");
4665# endif
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (!show_shell_mess || (options & SHELL_EXPAND))
4668 {
4669 int fd;
4670
4671 /*
4672 * Don't want to show any message from the shell. Can't just
4673 * close stdout and stderr though, because some systems will
4674 * break if you try to write to them after that, so we must
4675 * use dup() to replace them with something else -- webb
4676 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4677 * waiting for input.
4678 */
4679 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4680 fclose(stdin);
4681 fclose(stdout);
4682 fclose(stderr);
4683
4684 /*
4685 * If any of these open()'s and dup()'s fail, we just continue
4686 * anyway. It's not fatal, and on most systems it will make
4687 * no difference at all. On a few it will cause the execvp()
4688 * to exit with a non-zero status even when the completion
4689 * could be done, which is nothing too serious. If the open()
4690 * or dup() failed we'd just do the same thing ourselves
4691 * anyway -- webb
4692 */
4693 if (fd >= 0)
4694 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004695 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4696 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4697 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
4699 /* Don't need this now that we've duplicated it */
4700 close(fd);
4701 }
4702 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004703 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705 || gui.in_use
4706# endif
4707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709
Bram Moolenaardf177f62005-02-22 08:39:57 +00004710# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004711 /* Create our own process group, so that the child and all its
4712 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004713 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004714 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004715 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004716 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004717# if defined(SIGHUP)
4718 /* When doing "!xterm&" and 'shell' is bash: the shell
4719 * will exit and send SIGHUP to all processes in its
4720 * group, killing the just started process. Ignore SIGHUP
4721 * to avoid that. (suggested by Simon Schubert)
4722 */
4723 signal(SIGHUP, SIG_IGN);
4724# endif
4725 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726# endif
4727# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004728 if (pty_slave_fd >= 0)
4729 {
4730 /* push stream discipline modules */
4731 if (options & SHELL_COOKED)
4732 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004734 /* Try to become controlling tty (probably doesn't work,
4735 * unless run by root) */
4736 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004738 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004739# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004740 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
Bram Moolenaara5792f52005-11-23 21:25:05 +00004742 /*
4743 * stderr is only redirected when using the GUI, so that a
4744 * program like gpg can still access the terminal to get a
4745 * passphrase using stderr.
4746 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004747# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (pty_master_fd >= 0)
4749 {
4750 close(pty_master_fd); /* close master side of pty */
4751
4752 /* set up stdin/stdout/stderr for the child */
4753 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004754 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004756 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004757 if (gui.in_use)
4758 {
4759 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004760 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 close(pty_slave_fd); /* has been dupped, close it now */
4764 }
4765 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
4768 /* set up stdin for the child */
4769 close(fd_toshell[1]);
4770 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004771 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 close(fd_toshell[0]);
4773
4774 /* set up stdout for the child */
4775 close(fd_fromshell[0]);
4776 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004777 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 close(fd_fromshell[1]);
4779
Bram Moolenaara5792f52005-11-23 21:25:05 +00004780# ifdef FEAT_GUI
4781 if (gui.in_use)
4782 {
4783 /* set up stderr for the child */
4784 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004785 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004786 }
4787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 /*
4792 * There is no type cast for the argv, because the type may be
4793 * different on different machines. This may cause a warning
4794 * message with strict compilers, don't worry about it.
4795 * Call _exit() instead of exit() to avoid closing the connection
4796 * to the X server (esp. with GTK, which uses atexit()).
4797 */
4798 execvp(argv[0], argv);
4799 _exit(EXEC_FAILED); /* exec failed, return failure code */
4800 }
4801 else /* parent */
4802 {
4803 /*
4804 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004805 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004809 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004810# ifdef FEAT_JOB_CHANNEL
4811 ++dont_check_job_ended;
4812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 /*
4814 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815 * This is also used to pipe stdin/stdout to/from the external
4816 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004818 if ((options & (SHELL_READ|SHELL_WRITE))
4819# ifdef FEAT_GUI
4820 || (gui.in_use && show_shell_mess)
4821# endif
4822 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004824# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4830 int ta_len = 0; /* valid bytes in ta_buf[] */
4831 int len;
4832 int p_more_save;
4833 int old_State;
4834 int c;
4835 int toshell_fd;
4836 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004837 garray_T ga;
4838 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004839# ifdef ELAPSED_FUNC
4840 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
Bram Moolenaardf177f62005-02-22 08:39:57 +00004843# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (pty_master_fd >= 0)
4845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 fromshell_fd = pty_master_fd;
4847 toshell_fd = dup(pty_master_fd);
4848 }
4849 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
4852 close(fd_toshell[0]);
4853 close(fd_fromshell[1]);
4854 toshell_fd = fd_toshell[1];
4855 fromshell_fd = fd_fromshell[0];
4856 }
4857
4858 /*
4859 * Write to the child if there are typed characters.
4860 * Read from the child if there are characters available.
4861 * Repeat the reading a few times if more characters are
4862 * available. Need to check for typed keys now and then, but
4863 * not too often (delays when no chars are available).
4864 * This loop is quit if no characters can be read from the pty
4865 * (WaitForChar detected special condition), or there are no
4866 * characters available and the child has exited.
4867 * Only check if the child has exited when there is no more
4868 * output. The child may exit before all the output has
4869 * been printed.
4870 *
4871 * Currently this busy loops!
4872 * This can probably dead-lock when the write blocks!
4873 */
4874 p_more_save = p_more;
4875 p_more = FALSE;
4876 old_State = State;
4877 State = EXTERNCMD; /* don't redraw at window resize */
4878
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004879 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004880 {
4881 /* Fork a process that will write the lines to the
4882 * external program. */
4883 if ((wpid = fork()) == -1)
4884 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004885 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004886 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004887 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004888 {
4889 linenr_T lnum = curbuf->b_op_start.lnum;
4890 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004891 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004892 size_t l;
4893
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004894 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004895 for (;;)
4896 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004897 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898 if (l == 0)
4899 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004900 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 /* NL -> NUL translation */
4902 len = write(toshell_fd, "", (size_t)1);
4903 else
4904 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004905 char_u *s = vim_strchr(lp + written, NL);
4906
Bram Moolenaar89d40322006-08-29 15:30:07 +00004907 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004908 s == NULL ? l
4909 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004910 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004911 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004912 {
4913 /* Finished a line, add a NL, unless this line
4914 * should not have one. */
4915 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004916 || (!curbuf->b_p_bin
4917 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004918 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 && (lnum !=
4920 curbuf->b_ml.ml_line_count
4921 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004922 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004923 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004924 ++lnum;
4925 if (lnum > curbuf->b_op_end.lnum)
4926 {
4927 /* finished all the lines, close pipe */
4928 close(toshell_fd);
4929 toshell_fd = -1;
4930 break;
4931 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004932 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004933 written = 0;
4934 }
4935 else if (len > 0)
4936 written += len;
4937 }
4938 _exit(0);
4939 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004940 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004941 {
4942 close(toshell_fd);
4943 toshell_fd = -1;
4944 }
4945 }
4946
4947 if (options & SHELL_READ)
4948 ga_init2(&ga, 1, BUFLEN);
4949
4950 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004951# ifdef ELAPSED_FUNC
4952 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 for (;;)
4955 {
4956 /*
4957 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004958 * if there are any.
4959 * Don't do this if we are expanding wild cards (would eat
4960 * typeahead).
4961 * Don't do this when filtering and terminal is in cooked
4962 * mode, the shell command will handle the I/O. Avoids
4963 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004964 * Don't get characters when the child has already
4965 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004966 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004967 * while (noread_cnt > 4), avoids that ":r !ls" eats
4968 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 */
4970 len = 0;
4971 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004972 && ((options &
4973 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4974 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004975# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004976 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004977# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004978 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004979 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004980 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004982 if (ta_len == 0)
4983 {
4984 /* Get extra characters when we don't have any.
4985 * Reset the counter and timer. */
4986 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004987# ifdef ELAPSED_FUNC
4988 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004989# endif
4990 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4991 }
4992 if (ta_len > 0 || len > 0)
4993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 /*
4995 * For pipes:
4996 * Check for CTRL-C: send interrupt signal to child.
4997 * Check for CTRL-D: EOF, close pipe to child.
4998 */
4999 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 /*
5002 * Send SIGINT to the child's group or all
5003 * processes in our group.
5004 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005005 may_send_sigint(ta_buf[ta_len], pid, wpid);
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (pty_master_fd < 0 && toshell_fd >= 0
5008 && ta_buf[ta_len] == Ctrl_D)
5009 {
5010 close(toshell_fd);
5011 toshell_fd = -1;
5012 }
5013 }
5014
5015 /* replace K_BS by <BS> and K_DEL by <DEL> */
5016 for (i = ta_len; i < ta_len + len; ++i)
5017 {
5018 if (ta_buf[i] == CSI && len - i > 2)
5019 {
5020 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5021 if (c == K_DEL || c == K_KDEL || c == K_BS)
5022 {
5023 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5024 (size_t)(len - i - 2));
5025 if (c == K_DEL || c == K_KDEL)
5026 ta_buf[i] = DEL;
5027 else
5028 ta_buf[i] = Ctrl_H;
5029 len -= 2;
5030 }
5031 }
5032 else if (ta_buf[i] == '\r')
5033 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005034# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005036 i += (*mb_ptr2len_len)(ta_buf + i,
5037 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040
5041 /*
5042 * For pipes: echo the typed characters.
5043 * For a pty this does not seem to work.
5044 */
5045 if (pty_master_fd < 0)
5046 {
5047 for (i = ta_len; i < ta_len + len; ++i)
5048 {
5049 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5050 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005051# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 else if (has_mbyte)
5053 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005054 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 msg_outtrans_len(ta_buf + i, l);
5057 i += l - 1;
5058 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else
5061 msg_outtrans_len(ta_buf + i, 1);
5062 }
5063 windgoto(msg_row, msg_col);
5064 out_flush();
5065 }
5066
5067 ta_len += len;
5068
5069 /*
5070 * Write the characters to the child, unless EOF has
5071 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005072 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005073 * When writing buffer lines, drop the typed
5074 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076 if (options & SHELL_WRITE)
5077 ta_len = 0;
5078 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5081 if (len > 0)
5082 {
5083 ta_len -= len;
5084 mch_memmove(ta_buf, ta_buf + len, ta_len);
5085 }
5086 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089
Bram Moolenaardf177f62005-02-22 08:39:57 +00005090 if (got_int)
5091 {
5092 /* CTRL-C sends a signal to the child, we ignore it
5093 * ourselves */
5094# ifdef HAVE_SETSID
5095 kill(-pid, SIGINT);
5096# else
5097 kill(0, SIGINT);
5098# endif
5099 if (wpid > 0)
5100 kill(wpid, SIGINT);
5101 got_int = FALSE;
5102 }
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 /*
5105 * Check if the child has any characters to be printed.
5106 * Read them and write them to our window. Repeat this as
5107 * long as there is something to do, avoid the 10ms wait
5108 * for mch_inchar(), or sending typeahead characters to
5109 * the external process.
5110 * TODO: This should handle escape sequences, compatible
5111 * to some terminal (vt52?).
5112 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005113 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005114 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005116 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005117# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005119# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 );
5123 if (len <= 0) /* end of file or error */
5124 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005125
5126 noread_cnt = 0;
5127 if (options & SHELL_READ)
5128 {
5129 /* Do NUL -> NL translation, append NL separated
5130 * lines to the current buffer. */
5131 for (i = 0; i < len; ++i)
5132 {
5133 if (buffer[i] == NL)
5134 append_ga_line(&ga);
5135 else if (buffer[i] == NUL)
5136 ga_append(&ga, NL);
5137 else
5138 ga_append(&ga, buffer[i]);
5139 }
5140 }
5141# ifdef FEAT_MBYTE
5142 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
5144 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005145 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaardf177f62005-02-22 08:39:57 +00005147 len += buffer_off;
5148 buffer[len] = NUL;
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /* Check if the last character in buffer[] is
5151 * incomplete, keep these bytes for the next
5152 * round. */
5153 for (p = buffer; p < buffer + len; p += l)
5154 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005155 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (l == 0)
5157 l = 1; /* NUL byte? */
5158 else if (MB_BYTE2LEN(*p) != l)
5159 break;
5160 }
5161 if (p == buffer) /* no complete character */
5162 {
5163 /* avoid getting stuck at an illegal byte */
5164 if (len >= 12)
5165 ++p;
5166 else
5167 {
5168 buffer_off = len;
5169 continue;
5170 }
5171 }
5172 c = *p;
5173 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005174 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (p < buffer + len)
5176 {
5177 *p = c;
5178 buffer_off = (buffer + len) - p;
5179 mch_memmove(buffer, p, buffer_off);
5180 continue;
5181 }
5182 buffer_off = 0;
5183 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005184# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
5187 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005188 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190
5191 windgoto(msg_row, msg_col);
5192 cursor_on();
5193 out_flush();
5194 if (got_int)
5195 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005196
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005197# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005198 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005199 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005200 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005201
5202 /* Avoid that we keep looping here without
5203 * checking for a CTRL-C for a long time. Don't
5204 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005205 if (msec > 2000)
5206 {
5207 noread_cnt = 5;
5208 break;
5209 }
5210 }
5211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005214 /* If we already detected the child has finished, continue
5215 * reading output for a short while. Some text may be
5216 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005217 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005218 {
5219 if (noread_cnt < 5)
5220 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005221 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005222 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 /*
5225 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005226 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005228# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005229 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005230# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5234 || (wait_pid == pid && WIFEXITED(status)))
5235 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005236 /* Don't break the loop yet, try reading more
5237 * characters from "fromshell_fd" first. When using
5238 * pipes there might still be something to read and
5239 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005242 else
5243 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005244
Bram Moolenaar95a51352013-03-21 22:53:50 +01005245# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005246 /* Handle any X events, e.g. serving the clipboard. */
5247 clip_update();
5248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250finished:
5251 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005252 if (options & SHELL_READ)
5253 {
5254 if (ga.ga_len > 0)
5255 {
5256 append_ga_line(&ga);
5257 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005258 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005259 }
5260 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005261 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005262 ga_clear(&ga);
5263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 /*
5266 * Give all typeahead that wasn't used back to ui_inchar().
5267 */
5268 if (ta_len)
5269 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 State = old_State;
5271 if (toshell_fd >= 0)
5272 close(toshell_fd);
5273 close(fromshell_fd);
5274 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005275# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005276 else
5277 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005278 long delay_msec = 1;
5279
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005280 /*
5281 * Similar to the loop above, but only handle X events, no
5282 * I/O.
5283 */
5284 for (;;)
5285 {
5286 if (got_int)
5287 {
5288 /* CTRL-C sends a signal to the child, we ignore it
5289 * ourselves */
5290# ifdef HAVE_SETSID
5291 kill(-pid, SIGINT);
5292# else
5293 kill(0, SIGINT);
5294# endif
5295 got_int = FALSE;
5296 }
5297# ifdef __NeXT__
5298 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5299# else
5300 wait_pid = waitpid(pid, &status, WNOHANG);
5301# endif
5302 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5303 || (wait_pid == pid && WIFEXITED(status)))
5304 {
5305 wait_pid = pid;
5306 break;
5307 }
5308
5309 /* Handle any X events, e.g. serving the clipboard. */
5310 clip_update();
5311
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005312 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5313 * less time. */
5314 mch_delay(delay_msec, TRUE);
5315 if (++delay_msec > 10)
5316 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005317 }
5318 }
5319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
5321 /*
5322 * Wait until our child has exited.
5323 * Ignore wait() returning pids of other children and returning
5324 * because of some signal like SIGWINCH.
5325 * Don't wait if wait_pid was already set above, indicating the
5326 * child already exited.
5327 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005328 if (wait_pid != pid)
5329 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaar624891f2010-10-13 16:22:09 +02005331# ifdef FEAT_GUI
5332 /* Close slave side of pty. Only do this after the child has
5333 * exited, otherwise the child may hang when it tries to write on
5334 * the pty. */
5335 if (pty_master_fd >= 0)
5336 close(pty_slave_fd);
5337# endif
5338
Bram Moolenaardf177f62005-02-22 08:39:57 +00005339 /* Make sure the child that writes to the external program is
5340 * dead. */
5341 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005342 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005343 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005344 wait4pid(wpid, NULL);
5345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005346
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005347# ifdef FEAT_JOB_CHANNEL
5348 --dont_check_job_ended;
5349# endif
5350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 /*
5352 * Set to raw mode right now, otherwise a CTRL-C after
5353 * catch_signals() will kill Vim.
5354 */
5355 if (tmode == TMODE_RAW)
5356 settmode(TMODE_RAW);
5357 did_settmode = TRUE;
5358 set_signals();
5359
5360 if (WIFEXITED(status))
5361 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005362 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005364 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
5366 if (retval == EXEC_FAILED)
5367 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005368 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 msg_outtrans(p_sh);
5370 msg_putchar('\n');
5371 }
5372 else if (!(options & SHELL_SILENT))
5373 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005374 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 msg_outnum((long)retval);
5376 msg_putchar('\n');
5377 }
5378 }
5379 }
5380 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005381 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385error:
5386 if (!did_settmode)
5387 if (tmode == TMODE_RAW)
5388 settmode(TMODE_RAW); /* set to raw mode */
5389# ifdef FEAT_TITLE
5390 resettitle();
5391# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005392 vim_free(argv);
5393 vim_free(tofree1);
5394 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
5396 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005399
5400 int
5401mch_call_shell(
5402 char_u *cmd,
5403 int options) /* SHELL_*, see vim.h */
5404{
5405#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5406 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5407 return mch_call_shell_terminal(cmd, options);
5408#endif
5409#ifdef USE_SYSTEM
5410 return mch_call_shell_system(cmd, options);
5411#else
5412 return mch_call_shell_fork(cmd, options);
5413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414}
5415
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005416#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005417 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005418mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005419{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005420 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005421 int fd_in[2] = {-1, -1}; /* for stdin */
5422 int fd_out[2] = {-1, -1}; /* for stdout */
5423 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005424 int pty_master_fd = -1;
5425 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005426 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005427 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5428 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5429 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005430 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005431 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5432 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005433 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005434 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005435 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005436
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005437 if (use_out_for_err && use_null_for_out)
5438 use_null_for_err = TRUE;
5439
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005440 /* default is to fail */
5441 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005442
Bram Moolenaarb2412082017-08-20 18:09:14 +02005443 if (options->jo_pty
5444 && (!(use_file_for_in || use_null_for_in)
5445 || !(use_file_for_in || use_null_for_out)
5446 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005447 {
5448 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5449 if (job->jv_tty_out != NULL)
5450 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5451 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005452
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005453 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005454 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005455 if (use_file_for_in)
5456 {
5457 char_u *fname = options->jo_io_name[PART_IN];
5458
5459 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5460 if (fd_in[0] < 0)
5461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005462 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005463 goto failed;
5464 }
5465 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005466 else
5467 /* When writing buffer lines to the input don't use the pty, so that
5468 * the pipe can be closed when all lines were written. */
5469 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5470 && pipe(fd_in) < 0)
5471 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005472
5473 if (use_file_for_out)
5474 {
5475 char_u *fname = options->jo_io_name[PART_OUT];
5476
5477 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5478 if (fd_out[1] < 0)
5479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005480 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005481 goto failed;
5482 }
5483 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005484 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005485 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005486
5487 if (use_file_for_err)
5488 {
5489 char_u *fname = options->jo_io_name[PART_ERR];
5490
5491 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5492 if (fd_err[1] < 0)
5493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005494 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005495 goto failed;
5496 }
5497 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005498 else if (!use_out_for_err && !use_null_for_err
5499 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005500 goto failed;
5501
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005502 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5503 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005504 if (options->jo_set & JO_CHANNEL)
5505 {
5506 channel = options->jo_channel;
5507 if (channel != NULL)
5508 ++channel->ch_refcount;
5509 }
5510 else
5511 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005512 if (channel == NULL)
5513 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005514 if (job->jv_tty_out != NULL)
5515 ch_log(channel, "using pty %s on fd %d",
5516 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005517 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005518
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005519 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005520 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005521 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005522 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005523 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005524 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005525 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005526 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005527 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005528 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005529 int null_fd = -1;
5530 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005531
Bram Moolenaar835dc632016-02-07 14:27:38 +01005532 /* child */
5533 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005534 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535
Bram Moolenaar819524702018-02-27 19:10:00 +01005536# ifdef FEAT_JOB_CHANNEL
5537 if (ch_log_active())
5538 /* close the log file in the child */
5539 ch_logfile((char_u *)"", (char_u *)"");
5540# endif
5541
Bram Moolenaar835dc632016-02-07 14:27:38 +01005542# ifdef HAVE_SETSID
5543 /* Create our own process group, so that the child and all its
5544 * children can be kill()ed. Don't do this when using pipes,
5545 * because stdin is not a tty, we would lose /dev/tty. */
5546 (void)setsid();
5547# endif
5548
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005549# ifdef FEAT_TERMINAL
5550 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005551 {
5552 char *term = (char *)T_NAME;
5553
5554#ifdef FEAT_GUI
5555 if (term_is_gui(T_NAME))
5556 /* In the GUI 'term' is not what we want, use $TERM. */
5557 term = getenv("TERM");
5558#endif
5559 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5560 * back to "xterm". */
5561 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5562 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005563 set_child_environment(
5564 (long)options->jo_term_rows,
5565 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005566 term,
5567 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005568 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005569 else
5570# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005571 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005572
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005573 if (options->jo_env != NULL)
5574 {
5575 dict_T *dict = options->jo_env;
5576 hashitem_T *hi;
5577 int todo = (int)dict->dv_hashtab.ht_used;
5578
5579 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5580 if (!HASHITEM_EMPTY(hi))
5581 {
5582 typval_T *item = &dict_lookup(hi)->di_tv;
5583
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005584 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005585 --todo;
5586 }
5587 }
5588
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005589 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005590 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005591 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005592 if (null_fd < 0)
5593 {
5594 perror("opening /dev/null failed");
5595 _exit(OPEN_NULL_FAILED);
5596 }
5597 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005598
Bram Moolenaar223896d2017-08-02 22:33:28 +02005599 if (pty_slave_fd >= 0)
5600 {
5601 /* push stream discipline modules */
5602 SetupSlavePTY(pty_slave_fd);
5603# ifdef TIOCSCTTY
5604 /* Try to become controlling tty (probably doesn't work,
5605 * unless run by root) */
5606 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5607# endif
5608 }
5609
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005610 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005611 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005612 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005613 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005614 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005615 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005616 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005617 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005618
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005619 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005620 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005621 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005622 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005623 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005624 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005625 }
5626 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005627 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005628 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005629 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005630 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005631 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005632
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005633 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005634 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005635 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005636 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005637 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005638 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005639 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005640 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005641
5642 if (fd_in[0] >= 0)
5643 close(fd_in[0]);
5644 if (fd_in[1] >= 0)
5645 close(fd_in[1]);
5646 if (fd_out[0] >= 0)
5647 close(fd_out[0]);
5648 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005649 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005650 if (fd_err[0] >= 0)
5651 close(fd_err[0]);
5652 if (fd_err[1] >= 0)
5653 close(fd_err[1]);
5654 if (pty_master_fd >= 0)
5655 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005656 close(pty_master_fd); /* not used in the child */
5657 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005658 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005659
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660 if (null_fd >= 0)
5661 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005662
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005663 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5664 _exit(EXEC_FAILED);
5665
Bram Moolenaar835dc632016-02-07 14:27:38 +01005666 /* See above for type of argv. */
5667 execvp(argv[0], argv);
5668
Bram Moolenaar4694a172016-04-21 14:05:23 +02005669 if (stderr_works)
5670 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005671# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005672 /* calling free_all_mem() here causes problems. Ignore valgrind
5673 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005674# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005675 _exit(EXEC_FAILED); /* exec failed, return failure code */
5676 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005677
5678 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005679 UNBLOCK_SIGNALS(&curset);
5680
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005681 job->jv_pid = pid;
5682 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005683 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005684
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005685 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005686 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005687 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005688 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005689 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005690 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005691 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005692 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005693 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005694 if (channel != NULL)
5695 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005696 int in_fd = use_file_for_in || use_null_for_in
5697 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5698 int out_fd = use_file_for_out || use_null_for_out
5699 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005700 /* When using pty_master_fd only set it for stdout, do not duplicate it
5701 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005702 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005703 ? INVALID_FD
5704 : fd_err[0] >= 0
5705 ? fd_err[0]
5706 : (out_fd == pty_master_fd
5707 ? INVALID_FD
5708 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005709
5710 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005711 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005712 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005713 else
5714 {
5715 if (fd_in[1] >= 0)
5716 close(fd_in[1]);
5717 if (fd_out[0] >= 0)
5718 close(fd_out[0]);
5719 if (fd_err[0] >= 0)
5720 close(fd_err[0]);
5721 if (pty_master_fd >= 0)
5722 close(pty_master_fd);
5723 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005724
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005725 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726 return;
5727
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005728failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005729 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005730 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005731 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005732 if (fd_in[1] >= 0)
5733 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005735 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005736 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005737 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005738 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005739 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005740 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005742 if (pty_master_fd >= 0)
5743 close(pty_master_fd);
5744 if (pty_slave_fd >= 0)
5745 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005746}
5747
5748 char *
5749mch_job_status(job_T *job)
5750{
5751# ifdef HAVE_UNION_WAIT
5752 union wait status;
5753# else
5754 int status = -1;
5755# endif
5756 pid_t wait_pid = 0;
5757
5758# ifdef __NeXT__
5759 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5760# else
5761 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5762# endif
5763 if (wait_pid == -1)
5764 {
5765 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005766 if (job->jv_status < JOB_ENDED)
5767 ch_log(job->jv_channel, "Job no longer exists: %s",
5768 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005769 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005770 }
5771 if (wait_pid == 0)
5772 return "run";
5773 if (WIFEXITED(status))
5774 {
5775 /* LINTED avoid "bitwise operation on signed value" */
5776 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005777 if (job->jv_status < JOB_ENDED)
5778 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005779 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005780 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005781 if (WIFSIGNALED(status))
5782 {
5783 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005784 if (job->jv_status < JOB_ENDED)
5785 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005786 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005787 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005788 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005789
5790return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005791 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005792 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005793 return "dead";
5794}
5795
5796 job_T *
5797mch_detect_ended_job(job_T *job_list)
5798{
5799# ifdef HAVE_UNION_WAIT
5800 union wait status;
5801# else
5802 int status = -1;
5803# endif
5804 pid_t wait_pid = 0;
5805 job_T *job;
5806
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005807# ifndef USE_SYSTEM
5808 /* Do not do this when waiting for a shell command to finish, we would get
5809 * the exit value here (and discard it), the exit value obtained there
5810 * would then be wrong. */
5811 if (dont_check_job_ended > 0)
5812 return NULL;
5813# endif
5814
Bram Moolenaar97792de2016-10-15 18:36:49 +02005815# ifdef __NeXT__
5816 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5817# else
5818 wait_pid = waitpid(-1, &status, WNOHANG);
5819# endif
5820 if (wait_pid <= 0)
5821 /* no process ended */
5822 return NULL;
5823 for (job = job_list; job != NULL; job = job->jv_next)
5824 {
5825 if (job->jv_pid == wait_pid)
5826 {
5827 if (WIFEXITED(status))
5828 /* LINTED avoid "bitwise operation on signed value" */
5829 job->jv_exitval = WEXITSTATUS(status);
5830 else if (WIFSIGNALED(status))
5831 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005832 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005833 {
5834 ch_log(job->jv_channel, "Job ended");
5835 job->jv_status = JOB_ENDED;
5836 }
5837 return job;
5838 }
5839 }
5840 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841}
5842
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005843/*
5844 * Send a (deadly) signal to "job".
5845 * Return FAIL if "how" is not a valid name.
5846 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005848mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005850 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851
Bram Moolenaar923d9262016-02-25 20:56:01 +01005852 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005854 else if (STRCMP(how, "hup") == 0)
5855 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 else if (STRCMP(how, "quit") == 0)
5857 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005858 else if (STRCMP(how, "int") == 0)
5859 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005860 else if (STRCMP(how, "kill") == 0)
5861 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005862#ifdef SIGWINCH
5863 else if (STRCMP(how, "winch") == 0)
5864 sig = SIGWINCH;
5865#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866 else if (isdigit(*how))
5867 sig = atoi((char *)how);
5868 else
5869 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005870
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005871 // Never kill ourselves!
5872 if (job->jv_pid != 0)
5873 {
5874 // TODO: have an option to only kill the process, not the group?
5875 kill(-job->jv_pid, sig);
5876 kill(job->jv_pid, sig);
5877 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005878
Bram Moolenaar835dc632016-02-07 14:27:38 +01005879 return OK;
5880}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881
5882/*
5883 * Clear the data related to "job".
5884 */
5885 void
5886mch_clear_job(job_T *job)
5887{
5888 /* call waitpid because child process may become zombie */
5889# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005890 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005892 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005893# endif
5894}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895#endif
5896
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005897#if defined(FEAT_TERMINAL) || defined(PROTO)
5898 int
5899mch_create_pty_channel(job_T *job, jobopt_T *options)
5900{
5901 int pty_master_fd = -1;
5902 int pty_slave_fd = -1;
5903 channel_T *channel;
5904
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005905 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5906 if (job->jv_tty_out != NULL)
5907 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005908 close(pty_slave_fd);
5909
5910 channel = add_channel();
5911 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005912 {
5913 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005914 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005915 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005916 if (job->jv_tty_out != NULL)
5917 ch_log(channel, "using pty %s on fd %d",
5918 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005919 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5920 channel->ch_keep_open = TRUE;
5921
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005922 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5923 * it only needs to be read once. */
5924 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005925 channel_set_job(channel, job, options);
5926 return OK;
5927}
5928#endif
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930/*
5931 * Check for CTRL-C typed by reading all available characters.
5932 * In cooked mode we should get SIGINT, no need to check.
5933 */
5934 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005935mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005937 if ((curr_tmode == TMODE_RAW || force)
5938 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 fill_input_buf(FALSE);
5940}
5941
5942/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005943 * Wait "msec" msec until a character is available from the mouse, keyboard,
5944 * from inbuf[].
5945 * "msec" == -1 will block forever.
5946 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005947 * When "ignore_input" is TRUE even check for pending input when input is
5948 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005949 * "interrupted" (if not NULL) is set to TRUE when no character is available
5950 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005951 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005952 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 */
5954 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005955WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005957#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005958 return ui_wait_for_chars_or_timer(
5959 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005960#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005961 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005962#endif
5963}
5964
5965/*
5966 * Wait "msec" msec until a character is available from the mouse or keyboard
5967 * or from inbuf[].
5968 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005969 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005970 * "interrupted" (if not NULL) is set to TRUE when no character is available
5971 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005972 * When a GUI is being used, this will never get called -- webb
5973 */
5974 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005975WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005976{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#ifdef FEAT_MOUSE_GPM
5978 int gpm_process_wanted;
5979#endif
5980#ifdef FEAT_XCLIPBOARD
5981 int rest;
5982#endif
5983 int avail;
5984
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005985 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 return 1;
5987
5988#if defined(FEAT_MOUSE_DEC)
5989 /* May need to query the mouse position. */
5990 if (WantQueryMouse)
5991 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005992 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5994 }
5995#endif
5996
5997 /*
5998 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5999 * events. This is a bit complicated, because they might both be defined.
6000 */
6001#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6002# ifdef FEAT_XCLIPBOARD
6003 rest = 0;
6004 if (do_xterm_trace())
6005 rest = msec;
6006# endif
6007 do
6008 {
6009# ifdef FEAT_XCLIPBOARD
6010 if (rest != 0)
6011 {
6012 msec = XT_TRACE_DELAY;
6013 if (rest >= 0 && rest < XT_TRACE_DELAY)
6014 msec = rest;
6015 if (rest >= 0)
6016 rest -= msec;
6017 }
6018# endif
6019# ifdef FEAT_MOUSE_GPM
6020 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006021 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006022 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006024 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025# endif
6026 if (!avail)
6027 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006028 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 return 1;
6030# ifdef FEAT_XCLIPBOARD
6031 if (rest == 0 || !do_xterm_trace())
6032# endif
6033 break;
6034 }
6035 }
6036 while (FALSE
6037# ifdef FEAT_MOUSE_GPM
6038 || (gpm_process_wanted && mch_gpm_process() == 0)
6039# endif
6040# ifdef FEAT_XCLIPBOARD
6041 || (!avail && rest != 0)
6042# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006043 )
6044 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006047 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#endif
6049 return avail;
6050}
6051
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006052#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053/*
6054 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006055 * "msec" == 0 will check for characters once.
6056 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006059 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006060 * "interrupted" (if not NULL) is set to TRUE when no character is available
6061 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063#if defined(__BEOS__)
6064 int
6065#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006066 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006068RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
6070 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006071 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006072#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 static int busy = FALSE;
6074
6075 /* May retry getting characters after an event was handled. */
6076# define MAY_LOOP
6077
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006078# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 /* Remember at what time we started, so that we know how much longer we
6080 * should wait after being interrupted. */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006081 long start_msec = msec;
6082 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006084 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006085 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086# endif
6087
6088 /* Handle being called recursively. This may happen for the session
6089 * manager stuff, it may save the file, which does a breakcheck. */
6090 if (busy)
6091 return 0;
6092#endif
6093
6094#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006095 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096#endif
6097 {
6098#ifdef MAY_LOOP
6099 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006100# ifdef FEAT_MZSCHEME
6101 int mzquantum_used = FALSE;
6102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103#endif
6104#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006105 /* each channel may use in, out and err */
6106 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 int nfd;
6108# ifdef FEAT_XCLIPBOARD
6109 int xterm_idx = -1;
6110# endif
6111# ifdef FEAT_MOUSE_GPM
6112 int gpm_idx = -1;
6113# endif
6114# ifdef USE_XSMP
6115 int xsmp_idx = -1;
6116# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006117 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006119# ifdef FEAT_MZSCHEME
6120 mzvim_check_threads();
6121 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6122 {
6123 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6124 mzquantum_used = TRUE;
6125 }
6126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 fds[0].fd = fd;
6128 fds[0].events = POLLIN;
6129 nfd = 1;
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006132 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (xterm_Shell != (Widget)0)
6134 {
6135 xterm_idx = nfd;
6136 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6137 fds[nfd].events = POLLIN;
6138 nfd++;
6139 }
6140# endif
6141# ifdef FEAT_MOUSE_GPM
6142 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6143 {
6144 gpm_idx = nfd;
6145 fds[nfd].fd = gpm_fd;
6146 fds[nfd].events = POLLIN;
6147 nfd++;
6148 }
6149# endif
6150# ifdef USE_XSMP
6151 if (xsmp_icefd != -1)
6152 {
6153 xsmp_idx = nfd;
6154 fds[nfd].fd = xsmp_icefd;
6155 fds[nfd].events = POLLIN;
6156 nfd++;
6157 }
6158# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006159#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006160 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006161#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006162 if (interrupted != NULL)
6163 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006165 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006166
6167 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006168 if (result == 0 && interrupted != NULL && ret > 0)
6169 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006170
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006171# ifdef FEAT_MZSCHEME
6172 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006173 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006174 finished = FALSE;
6175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177# ifdef FEAT_XCLIPBOARD
6178 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6179 {
6180 xterm_update(); /* Maybe we should hand out clipboard */
6181 if (--ret == 0 && !input_available())
6182 /* Try again */
6183 finished = FALSE;
6184 }
6185# endif
6186# ifdef FEAT_MOUSE_GPM
6187 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6188 {
6189 *check_for_gpm = 1;
6190 }
6191# endif
6192# ifdef USE_XSMP
6193 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6194 {
6195 if (fds[xsmp_idx].revents & POLLIN)
6196 {
6197 busy = TRUE;
6198 xsmp_handle_requests();
6199 busy = FALSE;
6200 }
6201 else if (fds[xsmp_idx].revents & POLLHUP)
6202 {
6203 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006204 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 xsmp_close();
6206 }
6207 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006208 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006211#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006212 /* also call when ret == 0, we may be polling a keep-open channel */
6213 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006214 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217#else /* HAVE_SELECT */
6218
6219 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006220 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006221 // These are static because they can take 8 Kbyte each and cause the
6222 // signal stack to run out with -O3.
6223 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006225 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006227# ifdef FEAT_MZSCHEME
6228 mzvim_check_threads();
6229 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6230 {
6231 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6232 mzquantum_used = TRUE;
6233 }
6234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006236 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006238 tv.tv_sec = towait / 1000;
6239 tv.tv_usec = (towait % 1000) * (1000000/1000);
6240 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006242 else
6243 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 /*
6246 * Select on ready for reading and exceptional condition (end of file).
6247 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006248select_eintr:
6249 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006250 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 FD_ZERO(&efds);
6252 FD_SET(fd, &rfds);
6253# if !defined(__QNX__) && !defined(__CYGWIN32__)
6254 /* For QNX select() always returns 1 if this is set. Why? */
6255 FD_SET(fd, &efds);
6256# endif
6257 maxfd = fd;
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006260 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if (xterm_Shell != (Widget)0)
6262 {
6263 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6264 if (maxfd < ConnectionNumber(xterm_dpy))
6265 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006266
6267 /* An event may have already been read but not handled. In
6268 * particulary, XFlush may cause this. */
6269 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271# endif
6272# ifdef FEAT_MOUSE_GPM
6273 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6274 {
6275 FD_SET(gpm_fd, &rfds);
6276 FD_SET(gpm_fd, &efds);
6277 if (maxfd < gpm_fd)
6278 maxfd = gpm_fd;
6279 }
6280# endif
6281# ifdef USE_XSMP
6282 if (xsmp_icefd != -1)
6283 {
6284 FD_SET(xsmp_icefd, &rfds);
6285 FD_SET(xsmp_icefd, &efds);
6286 if (maxfd < xsmp_icefd)
6287 maxfd = xsmp_icefd;
6288 }
6289# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006290# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006291 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006292# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006293 if (interrupted != NULL)
6294 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
Bram Moolenaar643b6142018-09-12 20:29:09 +02006296 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6297 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006298 result = ret > 0 && FD_ISSET(fd, &rfds);
6299 if (result)
6300 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006301 else if (interrupted != NULL && ret > 0)
6302 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006303
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006304# ifdef EINTR
6305 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006306 {
6307 /* Check whether window has been resized, EINTR may be caused by
6308 * SIGWINCH. */
6309 if (do_resize)
6310 handle_resize();
6311
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006312 /* Interrupted by a signal, need to try again. We ignore msec
6313 * here, because we do want to check even after a timeout if
6314 * characters are available. Needed for reading output of an
6315 * external command after the process has finished. */
6316 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006317 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006318# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006319# ifdef __TANDEM
6320 if (ret == -1 && errno == ENOTSUP)
6321 {
6322 FD_ZERO(&rfds);
6323 FD_ZERO(&efds);
6324 ret = 0;
6325 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006326# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006327# ifdef FEAT_MZSCHEME
6328 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006329 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006330 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331# endif
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333# ifdef FEAT_XCLIPBOARD
6334 if (ret > 0 && xterm_Shell != (Widget)0
6335 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6336 {
6337 xterm_update(); /* Maybe we should hand out clipboard */
6338 /* continue looping when we only got the X event and the input
6339 * buffer is empty */
6340 if (--ret == 0 && !input_available())
6341 {
6342 /* Try again */
6343 finished = FALSE;
6344 }
6345 }
6346# endif
6347# ifdef FEAT_MOUSE_GPM
6348 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6349 {
6350 if (FD_ISSET(gpm_fd, &efds))
6351 gpm_close();
6352 else if (FD_ISSET(gpm_fd, &rfds))
6353 *check_for_gpm = 1;
6354 }
6355# endif
6356# ifdef USE_XSMP
6357 if (ret > 0 && xsmp_icefd != -1)
6358 {
6359 if (FD_ISSET(xsmp_icefd, &efds))
6360 {
6361 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006362 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 xsmp_close();
6364 if (--ret == 0)
6365 finished = FALSE; /* keep going if event was only one */
6366 }
6367 else if (FD_ISSET(xsmp_icefd, &rfds))
6368 {
6369 busy = TRUE;
6370 xsmp_handle_requests();
6371 busy = FALSE;
6372 if (--ret == 0)
6373 finished = FALSE; /* keep going if event was only one */
6374 }
6375 }
6376# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006378 /* also call when ret == 0, we may be polling a keep-open channel */
6379 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006380 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383#endif /* HAVE_SELECT */
6384
6385#ifdef MAY_LOOP
6386 if (finished || msec == 0)
6387 break;
6388
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006389# ifdef FEAT_CLIENTSERVER
6390 if (server_waiting())
6391 break;
6392# endif
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 /* We're going to loop around again, find out for how long */
6395 if (msec > 0)
6396 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006397# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006399 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400# else
6401 /* Guess we got interrupted halfway. */
6402 msec = msec / 2;
6403# endif
6404 if (msec <= 0)
6405 break; /* waited long enough */
6406 }
6407#endif
6408 }
6409
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006410 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411}
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006415 * Expand a path into all matching files and/or directories. Handles "*",
6416 * "?", "[a-z]", "**", etc.
6417 * "path" has backslashes before chars that are not to be expanded.
6418 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 */
6420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006421mch_expandpath(
6422 garray_T *gap,
6423 char_u *path,
6424 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
Bram Moolenaar02743632005-07-25 20:42:36 +00006426 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427}
6428#endif
6429
6430/*
6431 * mch_expand_wildcards() - this code does wild-card pattern matching using
6432 * the shell
6433 *
6434 * return OK for success, FAIL for error (you may lose some memory) and put
6435 * an error message in *file.
6436 *
6437 * num_pat is number of input patterns
6438 * pat is array of pointers to input patterns
6439 * num_file is pointer to number of matched file names
6440 * file is pointer to array of pointers to matched file names
6441 */
6442
6443#ifndef SEEK_SET
6444# define SEEK_SET 0
6445#endif
6446#ifndef SEEK_END
6447# define SEEK_END 2
6448#endif
6449
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006450#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006453mch_expand_wildcards(
6454 int num_pat,
6455 char_u **pat,
6456 int *num_file,
6457 char_u ***file,
6458 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 int i;
6461 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006462 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 char_u *p;
6464 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
Bram Moolenaarc7247912008-01-13 12:54:11 +00006466 /*
6467 * This is the non-OS/2 implementation (really Unix).
6468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 int j;
6470 char_u *tempname;
6471 char_u *command;
6472 FILE *fd;
6473 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006474#define STYLE_ECHO 0 /* use "echo", the default */
6475#define STYLE_GLOB 1 /* use "glob", for csh */
6476#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6477#define STYLE_PRINT 3 /* use "print -N", for zsh */
6478#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6479 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 int shell_style = STYLE_ECHO;
6481 int check_spaces;
6482 static int did_find_nul = FALSE;
6483 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006484 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006485 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487 *num_file = 0; /* default: no files found */
6488 *file = NULL;
6489
6490 /*
6491 * If there are no wildcards, just copy the names to allocated memory.
6492 * Saves a lot of time, because we don't have to start a new shell.
6493 */
6494 if (!have_wildcard(num_pat, pat))
6495 return save_patterns(num_pat, pat, num_file, file);
6496
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006497# ifdef HAVE_SANDBOX
6498 /* Don't allow any shell command in the sandbox. */
6499 if (sandbox != 0 && check_secure())
6500 return FAIL;
6501# endif
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 /*
6504 * Don't allow the use of backticks in secure and restricted mode.
6505 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006506 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 for (i = 0; i < num_pat; ++i)
6508 if (vim_strchr(pat[i], '`') != NULL
6509 && (check_restricted() || check_secure()))
6510 return FAIL;
6511
6512 /*
6513 * get a name for the temp file
6514 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006515 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006517 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 return FAIL;
6519 }
6520
6521 /*
6522 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006523 * file.
6524 * STYLE_BT: NL separated
6525 * If expanding `cmd` execute it directly.
6526 * STYLE_GLOB: NUL separated
6527 * If we use *csh, "glob" will work better than "echo".
6528 * STYLE_PRINT: NL or NUL separated
6529 * If we use *zsh, "print -N" will work better than "glob".
6530 * STYLE_VIMGLOB: NL separated
6531 * If we use *sh*, we define "vimglob()".
6532 * STYLE_ECHO: space separated.
6533 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 */
6535 if (num_pat == 1 && *pat[0] == '`'
6536 && (len = STRLEN(pat[0])) > 2
6537 && *(pat[0] + len - 1) == '`')
6538 shell_style = STYLE_BT;
6539 else if ((len = STRLEN(p_sh)) >= 3)
6540 {
6541 if (STRCMP(p_sh + len - 3, "csh") == 0)
6542 shell_style = STYLE_GLOB;
6543 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6544 shell_style = STYLE_PRINT;
6545 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006546 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6547 "sh") != NULL)
6548 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaarc7247912008-01-13 12:54:11 +00006550 /* Compute the length of the command. We need 2 extra bytes: for the
6551 * optional '&' and for the NUL.
6552 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006554 if (shell_style == STYLE_VIMGLOB)
6555 len += STRLEN(sh_vimglob_func);
6556
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006557 for (i = 0; i < num_pat; ++i)
6558 {
6559 /* Count the length of the patterns in the same way as they are put in
6560 * "command" below. */
6561#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006563#else
6564 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006565 for (j = 0; pat[i][j] != NUL; ++j)
6566 {
6567 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6568 ++len; /* may add a backslash */
6569 ++len;
6570 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006571#endif
6572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 command = alloc(len);
6574 if (command == NULL)
6575 {
6576 /* out of memory */
6577 vim_free(tempname);
6578 return FAIL;
6579 }
6580
6581 /*
6582 * Build the shell command:
6583 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6584 * recognizes this).
6585 * - Add the shell command to print the expanded names.
6586 * - Add the temp file name.
6587 * - Add the file name patterns.
6588 */
6589 if (shell_style == STYLE_BT)
6590 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006591 /* change `command; command& ` to (command; command ) */
6592 STRCPY(command, "(");
6593 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006595 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006596 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 --p;
6598 if (*p == '&') /* remove trailing '&' */
6599 {
6600 ampersent = TRUE;
6601 *p = ' ';
6602 }
6603 STRCAT(command, ">");
6604 }
6605 else
6606 {
6607 if (flags & EW_NOTFOUND)
6608 STRCPY(command, "set nonomatch; ");
6609 else
6610 STRCPY(command, "unset nonomatch; ");
6611 if (shell_style == STYLE_GLOB)
6612 STRCAT(command, "glob >");
6613 else if (shell_style == STYLE_PRINT)
6614 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006615 else if (shell_style == STYLE_VIMGLOB)
6616 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 else
6618 STRCAT(command, "echo >");
6619 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006620
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006622
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (shell_style != STYLE_BT)
6624 for (i = 0; i < num_pat; ++i)
6625 {
6626 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006627 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006628 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#ifdef USE_SYSTEM
6630 STRCAT(command, " \"");
6631 STRCAT(command, pat[i]);
6632 STRCAT(command, "\"");
6633#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006634 int intick = FALSE;
6635
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 p = command + STRLEN(command);
6637 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006638 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006639 {
6640 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006641 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006642 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6643 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006644 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006645 * backslash inside backticks, before a special character
6646 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006647 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006648 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6649 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006650 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006651 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006652 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006653 else if (!intick
6654 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6655 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006656 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006657 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6658 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006659 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006660
6661 /* Copy one character. */
6662 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 *p = NUL;
6665#endif
6666 }
6667 if (flags & EW_SILENT)
6668 show_shell_mess = FALSE;
6669 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006670 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 /*
6673 * Using zsh -G: If a pattern has no matches, it is just deleted from
6674 * the argument list, otherwise zsh gives an error message and doesn't
6675 * expand any other pattern.
6676 */
6677 if (shell_style == STYLE_PRINT)
6678 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6679
6680 /*
6681 * If we use -f then shell variables set in .cshrc won't get expanded.
6682 * vi can do it, so we will too, but it is only necessary if there is a "$"
6683 * in one of the patterns, otherwise we can still use the fast option.
6684 */
6685 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6686 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6687
6688 /*
6689 * execute the shell command
6690 */
6691 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6692
6693 /* When running in the background, give it some time to create the temp
6694 * file, but don't wait for it to finish. */
6695 if (ampersent)
6696 mch_delay(10L, TRUE);
6697
6698 extra_shell_arg = NULL; /* cleanup */
6699 show_shell_mess = TRUE;
6700 vim_free(command);
6701
Bram Moolenaarc7247912008-01-13 12:54:11 +00006702 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {
6704 mch_remove(tempname);
6705 vim_free(tempname);
6706 /*
6707 * With interactive completion, the error message is not printed.
6708 * However with USE_SYSTEM, I don't know how to turn off error messages
6709 * from the shell, so screen may still get messed up -- webb.
6710 */
6711#ifndef USE_SYSTEM
6712 if (!(flags & EW_SILENT))
6713#endif
6714 {
6715 redraw_later_clear(); /* probably messed up screen */
6716 msg_putchar('\n'); /* clear bottom line quickly */
6717 cmdline_row = Rows - 1; /* continue on last line */
6718#ifdef USE_SYSTEM
6719 if (!(flags & EW_SILENT))
6720#endif
6721 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006722 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 msg_start(); /* don't overwrite this message */
6724 }
6725 }
6726 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6727 * EW_NOTFOUND is given */
6728 if (shell_style == STYLE_BT)
6729 return FAIL;
6730 goto notfound;
6731 }
6732
6733 /*
6734 * read the names from the file into memory
6735 */
6736 fd = fopen((char *)tempname, READBIN);
6737 if (fd == NULL)
6738 {
6739 /* Something went wrong, perhaps a file name with a special char. */
6740 if (!(flags & EW_SILENT))
6741 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006742 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 msg_start(); /* don't overwrite this message */
6744 }
6745 vim_free(tempname);
6746 goto notfound;
6747 }
6748 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006749 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006751 if (llen < 0)
6752 /* just in case ftell() would fail */
6753 buffer = NULL;
6754 else
6755 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (buffer == NULL)
6757 {
6758 /* out of memory */
6759 mch_remove(tempname);
6760 vim_free(tempname);
6761 fclose(fd);
6762 return FAIL;
6763 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006764 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 i = fread((char *)buffer, 1, len, fd);
6766 fclose(fd);
6767 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006768 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
6770 /* unexpected read error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006771 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 vim_free(tempname);
6773 vim_free(buffer);
6774 return FAIL;
6775 }
6776 vim_free(tempname);
6777
Bram Moolenaarc7247912008-01-13 12:54:11 +00006778# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6780 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006781 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6783 *p++ = buffer[i];
6784 len = p - buffer;
6785# endif
6786
6787
6788 /* file names are separated with Space */
6789 if (shell_style == STYLE_ECHO)
6790 {
6791 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6792 p = buffer;
6793 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6794 {
6795 while (*p != ' ' && *p != '\n')
6796 ++p;
6797 p = skipwhite(p); /* skip to next entry */
6798 }
6799 }
6800 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006801 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
6803 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6804 p = buffer;
6805 for (i = 0; *p != NUL; ++i) /* count number of entries */
6806 {
6807 while (*p != '\n' && *p != NUL)
6808 ++p;
6809 if (*p != NUL)
6810 ++p;
6811 p = skipwhite(p); /* skip leading white space */
6812 }
6813 }
6814 /* file names are separated with NUL */
6815 else
6816 {
6817 /*
6818 * Some versions of zsh use spaces instead of NULs to separate
6819 * results. Only do this when there is no NUL before the end of the
6820 * buffer, otherwise we would never be able to use file names with
6821 * embedded spaces when zsh does use NULs.
6822 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6823 * don't check for spaces again.
6824 */
6825 check_spaces = FALSE;
6826 if (shell_style == STYLE_PRINT && !did_find_nul)
6827 {
6828 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006829 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006830 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 did_find_nul = TRUE;
6832 else
6833 check_spaces = TRUE;
6834 }
6835
6836 /*
6837 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6838 * already is one, for STYLE_GLOB it needs to be added.
6839 */
6840 if (len && buffer[len - 1] == NUL)
6841 --len;
6842 else
6843 buffer[len] = NUL;
6844 i = 0;
6845 for (p = buffer; p < buffer + len; ++p)
6846 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6847 {
6848 ++i;
6849 *p = NUL;
6850 }
6851 if (len)
6852 ++i; /* count last entry */
6853 }
6854 if (i == 0)
6855 {
6856 /*
6857 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6858 * /bin/sh will happily expand it to nothing rather than returning an
6859 * error; and hey, it's good to check anyway -- webb.
6860 */
6861 vim_free(buffer);
6862 goto notfound;
6863 }
6864 *num_file = i;
6865 *file = (char_u **)alloc(sizeof(char_u *) * i);
6866 if (*file == NULL)
6867 {
6868 /* out of memory */
6869 vim_free(buffer);
6870 return FAIL;
6871 }
6872
6873 /*
6874 * Isolate the individual file names.
6875 */
6876 p = buffer;
6877 for (i = 0; i < *num_file; ++i)
6878 {
6879 (*file)[i] = p;
6880 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006881 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6882 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006884 while (!(shell_style == STYLE_ECHO && *p == ' ')
6885 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 ++p;
6887 if (p == buffer + len) /* last entry */
6888 *p = NUL;
6889 else
6890 {
6891 *p++ = NUL;
6892 p = skipwhite(p); /* skip to next entry */
6893 }
6894 }
6895 else /* NUL separates */
6896 {
6897 while (*p && p < buffer + len) /* skip entry */
6898 ++p;
6899 ++p; /* skip NUL */
6900 }
6901 }
6902
6903 /*
6904 * Move the file names to allocated memory.
6905 */
6906 for (j = 0, i = 0; i < *num_file; ++i)
6907 {
6908 /* Require the files to exist. Helps when using /bin/sh */
6909 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6910 continue;
6911
6912 /* check if this entry should be included */
6913 dir = (mch_isdir((*file)[i]));
6914 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6915 continue;
6916
Bram Moolenaara2031822006-03-07 22:29:51 +00006917 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006918 if (!dir && (flags & EW_EXEC)
6919 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006920 continue;
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6923 if (p)
6924 {
6925 STRCPY(p, (*file)[i]);
6926 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006927 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 (*file)[j++] = p;
6929 }
6930 }
6931 vim_free(buffer);
6932 *num_file = j;
6933
6934 if (*num_file == 0) /* rejected all entries */
6935 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006936 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 goto notfound;
6938 }
6939
6940 return OK;
6941
6942notfound:
6943 if (flags & EW_NOTFOUND)
6944 return save_patterns(num_pat, pat, num_file, file);
6945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946}
6947
6948#endif /* VMS */
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006951save_patterns(
6952 int num_pat,
6953 char_u **pat,
6954 int *num_file,
6955 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
6957 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006958 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
6960 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6961 if (*file == NULL)
6962 return FAIL;
6963 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006964 {
6965 s = vim_strsave(pat[i]);
6966 if (s != NULL)
6967 /* Be compatible with expand_filename(): halve the number of
6968 * backslashes. */
6969 backslash_halve(s);
6970 (*file)[i] = s;
6971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 *num_file = num_pat;
6973 return OK;
6974}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976/*
6977 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6978 * expand.
6979 */
6980 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006981mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006983 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (*p == '\\' && p[1] != NUL)
6986 ++p;
6987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (vim_strchr((char_u *)
6989#ifdef VMS
6990 "*?%"
6991#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993#endif
6994 , *p) != NULL)
6995 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997 return FALSE;
6998}
6999
7000/*
7001 * Return TRUE if the string "p" contains a wildcard.
7002 * Don't recognize '~' at the end as a wildcard.
7003 */
7004 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007005mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007007 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (*p == '\\' && p[1] != NUL)
7010 ++p;
7011 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if (vim_strchr((char_u *)
7013#ifdef VMS
7014 "*?%$"
7015#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017#endif
7018 , *p) != NULL
7019 || (*p == '~' && p[1] != NUL))
7020 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022 return FALSE;
7023}
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007026have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 int i;
7029
7030 for (i = 0; i < num; i++)
7031 if (mch_has_wildcard(file[i]))
7032 return 1;
7033 return 0;
7034}
7035
7036 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007037have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 int i;
7040
7041 for (i = 0; i < num; i++)
7042 if (vim_strchr(file[i], '$') != NULL)
7043 return TRUE;
7044 return FALSE;
7045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007047#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048/*
7049 * Scaled-down version of rename(), which is missing in Xenix.
7050 * This version can only move regular files and will fail if the
7051 * destination exists.
7052 */
7053 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007054mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 struct stat st;
7057
7058 if (stat(dest, &st) >= 0) /* fail if destination exists */
7059 return -1;
7060 if (link(src, dest) != 0) /* link file to new name */
7061 return -1;
7062 if (mch_remove(src) == 0) /* delete link to old name */
7063 return 0;
7064 return -1;
7065}
7066#endif /* !HAVE_RENAME */
7067
7068#ifdef FEAT_MOUSE_GPM
7069/*
7070 * Initializes connection with gpm (if it isn't already opened)
7071 * Return 1 if succeeded (or connection already opened), 0 if failed
7072 */
7073 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007074gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7077
7078 if (!gpm_flag)
7079 {
7080 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7081 gpm_connect.defaultMask = ~GPM_HARD;
7082 /* Default handling for mouse move*/
7083 gpm_connect.minMod = 0; /* Handle any modifier keys */
7084 gpm_connect.maxMod = 0xffff;
7085 if (Gpm_Open(&gpm_connect, 0) > 0)
7086 {
7087 /* gpm library tries to handling TSTP causes
7088 * problems. Anyways, we close connection to Gpm whenever
7089 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007090 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007092# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 return 1; /* succeed */
7096 }
7097 if (gpm_fd == -2)
7098 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7099 return 0;
7100 }
7101 return 1; /* already open */
7102}
7103
7104/*
7105 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 */
7107 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007108gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 if (gpm_flag && gpm_fd >= 0) /* if Open */
7111 Gpm_Close();
7112}
7113
7114/* Reads gpm event and adds special keys to input buf. Returns length of
7115 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007116 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
7118 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007119mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 int button;
7122 static Gpm_Event gpm_event;
7123 char_u string[6];
7124 int_u vim_modifiers;
7125 int row,col;
7126 unsigned char buttons_mask;
7127 unsigned char gpm_modifiers;
7128 static unsigned char old_buttons = 0;
7129
7130 Gpm_GetEvent(&gpm_event);
7131
7132#ifdef FEAT_GUI
7133 /* Don't put events in the input queue now. */
7134 if (hold_gui_events)
7135 return 0;
7136#endif
7137
7138 row = gpm_event.y - 1;
7139 col = gpm_event.x - 1;
7140
7141 string[0] = ESC; /* Our termcode */
7142 string[1] = 'M';
7143 string[2] = 'G';
7144 switch (GPM_BARE_EVENTS(gpm_event.type))
7145 {
7146 case GPM_DRAG:
7147 string[3] = MOUSE_DRAG;
7148 break;
7149 case GPM_DOWN:
7150 buttons_mask = gpm_event.buttons & ~old_buttons;
7151 old_buttons = gpm_event.buttons;
7152 switch (buttons_mask)
7153 {
7154 case GPM_B_LEFT:
7155 button = MOUSE_LEFT;
7156 break;
7157 case GPM_B_MIDDLE:
7158 button = MOUSE_MIDDLE;
7159 break;
7160 case GPM_B_RIGHT:
7161 button = MOUSE_RIGHT;
7162 break;
7163 default:
7164 return 0;
7165 /*Don't know what to do. Can more than one button be
7166 * reported in one event? */
7167 }
7168 string[3] = (char_u)(button | 0x20);
7169 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7170 break;
7171 case GPM_UP:
7172 string[3] = MOUSE_RELEASE;
7173 old_buttons &= ~gpm_event.buttons;
7174 break;
7175 default:
7176 return 0;
7177 }
7178 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7179 gpm_modifiers = gpm_event.modifiers;
7180 vim_modifiers = 0x0;
7181 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7182 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7183 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7184 */
7185 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7186 vim_modifiers |= MOUSE_SHIFT;
7187
7188 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7189 vim_modifiers |= MOUSE_CTRL;
7190 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7191 vim_modifiers |= MOUSE_ALT;
7192 string[3] |= vim_modifiers;
7193 string[4] = (char_u)(col + ' ' + 1);
7194 string[5] = (char_u)(row + ' ' + 1);
7195 add_to_input_buf(string, 6);
7196 return 6;
7197}
7198#endif /* FEAT_MOUSE_GPM */
7199
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007200#ifdef FEAT_SYSMOUSE
7201/*
7202 * Initialize connection with sysmouse.
7203 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7204 * output, any sysmouse output than will be processed via sig_sysmouse().
7205 * Return OK if succeeded, FAIL if failed.
7206 */
7207 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007208sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007209{
7210 struct mouse_info mouse;
7211
7212 mouse.operation = MOUSE_MODE;
7213 mouse.u.mode.mode = 0;
7214 mouse.u.mode.signal = SIGUSR2;
7215 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7216 {
7217 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7218 mouse.operation = MOUSE_SHOW;
7219 ioctl(1, CONS_MOUSECTL, &mouse);
7220 return OK;
7221 }
7222 return FAIL;
7223}
7224
7225/*
7226 * Stop processing SIGUSR2 signals, and also make sure that
7227 * virtual console do not send us any sysmouse related signal.
7228 */
7229 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007230sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007231{
7232 struct mouse_info mouse;
7233
7234 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7235 mouse.operation = MOUSE_MODE;
7236 mouse.u.mode.mode = 0;
7237 mouse.u.mode.signal = 0;
7238 ioctl(1, CONS_MOUSECTL, &mouse);
7239}
7240
7241/*
7242 * Gets info from sysmouse and adds special keys to input buf.
7243 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007244 static RETSIGTYPE
7245sig_sysmouse SIGDEFARG(sigarg)
7246{
7247 struct mouse_info mouse;
7248 struct video_info video;
7249 char_u string[6];
7250 int row, col;
7251 int button;
7252 int buttons;
7253 static int oldbuttons = 0;
7254
7255#ifdef FEAT_GUI
7256 /* Don't put events in the input queue now. */
7257 if (hold_gui_events)
7258 return;
7259#endif
7260
7261 mouse.operation = MOUSE_GETINFO;
7262 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7263 && ioctl(1, FBIO_MODEINFO, &video) != -1
7264 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7265 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7266 {
7267 row = mouse.u.data.y / video.vi_cheight;
7268 col = mouse.u.data.x / video.vi_cwidth;
7269 buttons = mouse.u.data.buttons;
7270 string[0] = ESC; /* Our termcode */
7271 string[1] = 'M';
7272 string[2] = 'S';
7273 if (oldbuttons == buttons && buttons != 0)
7274 {
7275 button = MOUSE_DRAG;
7276 }
7277 else
7278 {
7279 switch (buttons)
7280 {
7281 case 0:
7282 button = MOUSE_RELEASE;
7283 break;
7284 case 1:
7285 button = MOUSE_LEFT;
7286 break;
7287 case 2:
7288 button = MOUSE_MIDDLE;
7289 break;
7290 case 4:
7291 button = MOUSE_RIGHT;
7292 break;
7293 default:
7294 return;
7295 }
7296 oldbuttons = buttons;
7297 }
7298 string[3] = (char_u)(button);
7299 string[4] = (char_u)(col + ' ' + 1);
7300 string[5] = (char_u)(row + ' ' + 1);
7301 add_to_input_buf(string, 6);
7302 }
7303 return;
7304}
7305#endif /* FEAT_SYSMOUSE */
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007308typedef char_u * (*STRPROCSTR)(char_u *);
7309typedef char_u * (*INTPROCSTR)(int);
7310typedef int (*STRPROCINT)(char_u *);
7311typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313/*
7314 * Call a DLL routine which takes either a string or int param
7315 * and returns an allocated string.
7316 */
7317 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007318mch_libcall(
7319 char_u *libname,
7320 char_u *funcname,
7321 char_u *argstring, /* NULL when using a argint */
7322 int argint,
7323 char_u **string_result,/* NULL when using number_result */
7324 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
7326# if defined(USE_DLOPEN)
7327 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007328 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329# else
7330 shl_t hinstLib;
7331# endif
7332 STRPROCSTR ProcAdd;
7333 INTPROCSTR ProcAddI;
7334 char_u *retval_str = NULL;
7335 int retval_int = 0;
7336 int success = FALSE;
7337
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007338 /*
7339 * Get a handle to the DLL module.
7340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007342 /* First clear any error, it's not cleared by the dlopen() call. */
7343 (void)dlerror();
7344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 hinstLib = dlopen((char *)libname, RTLD_LAZY
7346# ifdef RTLD_LOCAL
7347 | RTLD_LOCAL
7348# endif
7349 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007350 if (hinstLib == NULL)
7351 {
7352 /* "dlerr" must be used before dlclose() */
7353 dlerr = (char *)dlerror();
7354 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007355 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357# else
7358 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7359# endif
7360
7361 /* If the handle is valid, try to get the function address. */
7362 if (hinstLib != NULL)
7363 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007364# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 /*
7366 * Catch a crash when calling the library function. For example when
7367 * using a number where a string pointer is expected.
7368 */
7369 mch_startjmp();
7370 if (SETJMP(lc_jump_env) != 0)
7371 {
7372 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007373# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007374 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 mch_didjmp();
7377 }
7378 else
7379# endif
7380 {
7381 retval_str = NULL;
7382 retval_int = 0;
7383
7384 if (argstring != NULL)
7385 {
7386# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007387 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007388 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389# else
7390 if (shl_findsym(&hinstLib, (const char *)funcname,
7391 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7392 ProcAdd = NULL;
7393# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007394 if ((success = (ProcAdd != NULL
7395# if defined(USE_DLOPEN)
7396 && dlerr == NULL
7397# endif
7398 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
7400 if (string_result == NULL)
7401 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7402 else
7403 retval_str = (ProcAdd)(argstring);
7404 }
7405 }
7406 else
7407 {
7408# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007409 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007410 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411# else
7412 if (shl_findsym(&hinstLib, (const char *)funcname,
7413 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7414 ProcAddI = NULL;
7415# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007416 if ((success = (ProcAddI != NULL
7417# if defined(USE_DLOPEN)
7418 && dlerr == NULL
7419# endif
7420 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {
7422 if (string_result == NULL)
7423 retval_int = ((INTPROCINT)ProcAddI)(argint);
7424 else
7425 retval_str = (ProcAddI)(argint);
7426 }
7427 }
7428
7429 /* Save the string before we free the library. */
7430 /* Assume that a "1" or "-1" result is an illegal pointer. */
7431 if (string_result == NULL)
7432 *number_result = retval_int;
7433 else if (retval_str != NULL
7434 && retval_str != (char_u *)1
7435 && retval_str != (char_u *)-1)
7436 *string_result = vim_strsave(retval_str);
7437 }
7438
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007439# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 mch_endjmp();
7441# ifdef SIGHASARG
7442 if (lc_signal != 0)
7443 {
7444 int i;
7445
7446 /* try to find the name of this signal */
7447 for (i = 0; signal_info[i].sig != -1; i++)
7448 if (lc_signal == signal_info[i].sig)
7449 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007450 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452# endif
7453# endif
7454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007456 /* "dlerr" must be used before dlclose() */
7457 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007458 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007459
7460 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 (void)dlclose(hinstLib);
7462# else
7463 (void)shl_unload(hinstLib);
7464# endif
7465 }
7466
7467 if (!success)
7468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007469 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 return FAIL;
7471 }
7472
7473 return OK;
7474}
7475#endif
7476
7477#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7478static int xterm_trace = -1; /* default: disabled */
7479static int xterm_button;
7480
7481/*
7482 * Setup a dummy window for X selections in a terminal.
7483 */
7484 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007485setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487 int z = 0;
7488 char *strp = "";
7489 Widget AppShell;
7490
7491 if (!x_connect_to_server())
7492 return;
7493
7494 open_app_context();
7495 if (app_context != NULL && xterm_Shell == (Widget)0)
7496 {
7497 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007498# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007500# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007501# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007502 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
7504 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007505 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506# endif
7507
7508 /* Ignore X errors while opening the display */
7509 oldhandler = XSetErrorHandler(x_error_check);
7510
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007511# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 /* Ignore X IO errors while opening the display */
7513 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7514 mch_startjmp();
7515 if (SETJMP(lc_jump_env) != 0)
7516 {
7517 mch_didjmp();
7518 xterm_dpy = NULL;
7519 }
7520 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {
7523 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7524 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 if (xterm_dpy != NULL)
7526 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007527# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 }
7531
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007532# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 /* Now handle X IO errors normally. */
7534 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 /* Now handle X errors normally. */
7537 (void)XSetErrorHandler(oldhandler);
7538
7539 if (xterm_dpy == NULL)
7540 {
7541 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007542 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 return;
7544 }
7545
7546 /* Catch terminating error of the X server connection. */
7547 (void)XSetIOErrorHandler(x_IOerror_handler);
7548
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007549# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007551 {
7552 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007553 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007554 verbose_leave();
7555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556# endif
7557
7558 /* Create a Shell to make converters work. */
7559 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7560 applicationShellWidgetClass, xterm_dpy,
7561 NULL);
7562 if (AppShell == (Widget)0)
7563 return;
7564 xterm_Shell = XtVaCreatePopupShell("VIM",
7565 topLevelShellWidgetClass, AppShell,
7566 XtNmappedWhenManaged, 0,
7567 XtNwidth, 1,
7568 XtNheight, 1,
7569 NULL);
7570 if (xterm_Shell == (Widget)0)
7571 return;
7572
7573 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007574 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (x11_display == NULL)
7576 x11_display = xterm_dpy;
7577
7578 XtRealizeWidget(xterm_Shell);
7579 XSync(xterm_dpy, False);
7580 xterm_update();
7581 }
7582 if (xterm_Shell != (Widget)0)
7583 {
7584 clip_init(TRUE);
7585 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7586 x11_window = (Window)atol(strp);
7587 /* Check if $WINDOWID is valid. */
7588 if (test_x11_window(xterm_dpy) == FAIL)
7589 x11_window = 0;
7590 if (x11_window != 0)
7591 xterm_trace = 0;
7592 }
7593}
7594
7595 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007596start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7599 return;
7600 xterm_trace = 1;
7601 xterm_button = button;
7602 do_xterm_trace();
7603}
7604
7605
7606 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007607stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 if (xterm_trace < 0)
7610 return;
7611 xterm_trace = 0;
7612}
7613
7614/*
7615 * Query the xterm pointer and generate mouse termcodes if necessary
7616 * return TRUE if dragging is active, else FALSE
7617 */
7618 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007619do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621 Window root, child;
7622 int root_x, root_y;
7623 int win_x, win_y;
7624 int row, col;
7625 int_u mask_return;
7626 char_u buf[50];
7627 char_u *strp;
7628 long got_hints;
7629 static char_u *mouse_code;
7630 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7631 static int prev_row = 0, prev_col = 0;
7632 static XSizeHints xterm_hints;
7633
7634 if (xterm_trace <= 0)
7635 return FALSE;
7636
7637 if (xterm_trace == 1)
7638 {
7639 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007640 * have changed recently. */
7641 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7642 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 || xterm_hints.width_inc <= 1
7644 || xterm_hints.height_inc <= 1)
7645 {
7646 xterm_trace = -1; /* Not enough data -- disable tracing */
7647 return FALSE;
7648 }
7649
7650 /* Rely on the same mouse code for the duration of this */
7651 mouse_code = find_termcode(mouse_name);
7652 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007653 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 xterm_trace = 2;
7655
7656 /* Find the offset of the chars, there might be a scrollbar on the
7657 * left of the window and/or a menu on the top (eterm etc.) */
7658 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7659 &win_x, &win_y, &mask_return);
7660 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7661 - (xterm_hints.height_inc / 2);
7662 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7663 xterm_hints.y = 2;
7664 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7665 - (xterm_hints.width_inc / 2);
7666 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7667 xterm_hints.x = 2;
7668 return TRUE;
7669 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007670 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {
7672 xterm_trace = 0;
7673 return FALSE;
7674 }
7675
7676 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7677 &win_x, &win_y, &mask_return);
7678
7679 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7680 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7681 if (row == prev_row && col == prev_col)
7682 return TRUE;
7683
7684 STRCPY(buf, mouse_code);
7685 strp = buf + STRLEN(buf);
7686 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7687 *strp++ = (char_u)(col + ' ' + 1);
7688 *strp++ = (char_u)(row + ' ' + 1);
7689 *strp = 0;
7690 add_to_input_buf(buf, STRLEN(buf));
7691
7692 prev_row = row;
7693 prev_col = col;
7694 return TRUE;
7695}
7696
7697# if defined(FEAT_GUI) || defined(PROTO)
7698/*
7699 * Destroy the display, window and app_context. Required for GTK.
7700 */
7701 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007702clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (xterm_Shell != (Widget)0)
7705 {
7706 XtDestroyWidget(xterm_Shell);
7707 xterm_Shell = (Widget)0;
7708 }
7709 if (xterm_dpy != NULL)
7710 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007711# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 /* Lesstif and Solaris crash here, lose some memory */
7713 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (x11_display == xterm_dpy)
7716 x11_display = NULL;
7717 xterm_dpy = NULL;
7718 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007719# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (app_context != (XtAppContext)NULL)
7721 {
7722 /* Lesstif and Solaris crash here, lose some memory */
7723 XtDestroyApplicationContext(app_context);
7724 app_context = (XtAppContext)NULL;
7725 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727}
7728# endif
7729
7730/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007731 * Catch up with GUI or X events.
7732 */
7733 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007734clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007735{
7736# ifdef FEAT_GUI
7737 if (gui.in_use)
7738 gui_mch_update();
7739 else
7740# endif
7741 if (xterm_Shell != (Widget)0)
7742 xterm_update();
7743}
7744
7745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 * Catch up with any queued X events. This may put keyboard input into the
7747 * input buffer, call resize call-backs, trigger timers etc. If there is
7748 * nothing in the X event queue (& no timers pending), then we return
7749 * immediately.
7750 */
7751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007752xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 XEvent event;
7755
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007756 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007758 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007760 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007761 break;
7762
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007763 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007764 {
7765 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007766 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007767#ifdef FEAT_CLIENTSERVER
7768 {
7769 XPropertyEvent *e = (XPropertyEvent *)&event;
7770
7771 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007773 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007776 XtDispatchEvent(&event);
7777 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007778 else
7779 {
7780 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007781 XtAppProcessEvent(app_context, mask);
7782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784}
7785
7786 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007787clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 if (xterm_Shell != (Widget)0)
7790 return clip_x11_own_selection(xterm_Shell, cbd);
7791 return FAIL;
7792}
7793
7794 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007795clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 if (xterm_Shell != (Widget)0)
7798 clip_x11_lose_selection(xterm_Shell, cbd);
7799}
7800
7801 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007802clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 if (xterm_Shell != (Widget)0)
7805 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7806}
7807
7808 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007809clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 clip_x11_set_selection(cbd);
7812}
7813#endif
7814
7815
7816#if defined(USE_XSMP) || defined(PROTO)
7817/*
7818 * Code for X Session Management Protocol.
7819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822/*
7823 * This is our chance to ask the user if they want to save,
7824 * or abort the logout
7825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007827xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 cmdmod_T save_cmdmod;
7830 int cancel_shutdown = False;
7831
7832 save_cmdmod = cmdmod;
7833 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007834 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 /* Mustn't logout */
7836 cancel_shutdown = True;
7837 cmdmod = save_cmdmod;
7838 setcursor(); /* position cursor */
7839 out_flush();
7840
7841 /* Done interaction */
7842 SmcInteractDone(smc_conn, cancel_shutdown);
7843
7844 /* Finish off
7845 * Only end save-yourself here if we're not cancelling shutdown;
7846 * we'll get a cancelled callback later in which we'll end it.
7847 * Hopefully get around glitchy SMs (like GNOME-1)
7848 */
7849 if (!cancel_shutdown)
7850 {
7851 xsmp.save_yourself = False;
7852 SmcSaveYourselfDone(smc_conn, True);
7853 }
7854}
7855# endif
7856
7857/*
7858 * Callback that starts save-yourself.
7859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007861xsmp_handle_save_yourself(
7862 SmcConn smc_conn,
7863 SmPointer client_data UNUSED,
7864 int save_type UNUSED,
7865 Bool shutdown,
7866 int interact_style UNUSED,
7867 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 /* Handle already being in saveyourself */
7870 if (xsmp.save_yourself)
7871 SmcSaveYourselfDone(smc_conn, True);
7872 xsmp.save_yourself = True;
7873 xsmp.shutdown = shutdown;
7874
7875 /* First up, preserve all files */
7876 out_flush();
7877 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7878
7879 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007880 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881
7882# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7883 /* Now see if we can ask about unsaved files */
7884 if (shutdown && !fast && gui.in_use)
7885 /* Need to interact with user, but need SM's permission */
7886 SmcInteractRequest(smc_conn, SmDialogError,
7887 xsmp_handle_interaction, client_data);
7888 else
7889# endif
7890 {
7891 /* Can stop the cycle here */
7892 SmcSaveYourselfDone(smc_conn, True);
7893 xsmp.save_yourself = False;
7894 }
7895}
7896
7897
7898/*
7899 * Callback to warn us of imminent death.
7900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007902xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 xsmp_close();
7905
7906 /* quit quickly leaving swapfiles for modified buffers behind */
7907 getout_preserve_modified(0);
7908}
7909
7910
7911/*
7912 * Callback to tell us that save-yourself has completed.
7913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007915xsmp_save_complete(
7916 SmcConn smc_conn UNUSED,
7917 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 xsmp.save_yourself = False;
7920}
7921
7922
7923/*
7924 * Callback to tell us that an instigated shutdown was cancelled
7925 * (maybe even by us)
7926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007928xsmp_shutdown_cancelled(
7929 SmcConn smc_conn,
7930 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 if (xsmp.save_yourself)
7933 SmcSaveYourselfDone(smc_conn, True);
7934 xsmp.save_yourself = False;
7935 xsmp.shutdown = False;
7936}
7937
7938
7939/*
7940 * Callback to tell us that a new ICE connection has been established.
7941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007943xsmp_ice_connection(
7944 IceConn iceConn,
7945 IcePointer clientData UNUSED,
7946 Bool opening,
7947 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 /* Intercept creation of ICE connection fd */
7950 if (opening)
7951 {
7952 xsmp_icefd = IceConnectionNumber(iceConn);
7953 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7954 }
7955}
7956
7957
7958/* Handle any ICE processing that's required; return FAIL if SM lost */
7959 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007960xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 Bool rep;
7963
7964 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7965 == IceProcessMessagesIOError)
7966 {
7967 /* Lost ICE */
7968 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007969 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 xsmp_close();
7971 return FAIL;
7972 }
7973 else
7974 return OK;
7975}
7976
7977static int dummy;
7978
7979/* Set up X Session Management Protocol */
7980 void
7981xsmp_init(void)
7982{
7983 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 SmcCallbacks smcallbacks;
7985#if 0
7986 SmPropValue smname;
7987 SmProp smnameprop;
7988 SmProp *smprops[1];
7989#endif
7990
7991 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007992 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994 xsmp.save_yourself = xsmp.shutdown = False;
7995
7996 /* Set up SM callbacks - must have all, even if they're not used */
7997 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7998 smcallbacks.save_yourself.client_data = NULL;
7999 smcallbacks.die.callback = xsmp_die;
8000 smcallbacks.die.client_data = NULL;
8001 smcallbacks.save_complete.callback = xsmp_save_complete;
8002 smcallbacks.save_complete.client_data = NULL;
8003 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8004 smcallbacks.shutdown_cancelled.client_data = NULL;
8005
8006 /* Set up a watch on ICE connection creations. The "dummy" argument is
8007 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8008 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8009 {
8010 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008011 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 return;
8013 }
8014
8015 /* Create an SM connection */
8016 xsmp.smcconn = SmcOpenConnection(
8017 NULL,
8018 NULL,
8019 SmProtoMajor,
8020 SmProtoMinor,
8021 SmcSaveYourselfProcMask | SmcDieProcMask
8022 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8023 &smcallbacks,
8024 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008025 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008026 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 errorstring);
8028 if (xsmp.smcconn == NULL)
8029 {
8030 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008033 {
8034 vim_snprintf(errorreport, sizeof(errorreport),
8035 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008036 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 return;
8039 }
8040 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8041
8042#if 0
8043 /* ID ourselves */
8044 smname.value = "vim";
8045 smname.length = 3;
8046 smnameprop.name = "SmProgram";
8047 smnameprop.type = "SmARRAY8";
8048 smnameprop.num_vals = 1;
8049 smnameprop.vals = &smname;
8050
8051 smprops[0] = &smnameprop;
8052 SmcSetProperties(xsmp.smcconn, 1, smprops);
8053#endif
8054}
8055
8056
8057/* Shut down XSMP comms. */
8058 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008059xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 if (xsmp_icefd != -1)
8062 {
8063 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008064 if (xsmp.clientid != NULL)
8065 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008066 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 xsmp_icefd = -1;
8068 }
8069}
8070#endif /* USE_XSMP */
8071
8072
8073#ifdef EBCDIC
8074/* Translate character to its CTRL- value */
8075char CtrlTable[] =
8076{
8077/* 00 - 5E */
8078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8079 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8080 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8081 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8082 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8084/* ^ */ 0x1E,
8085/* - */ 0x1F,
8086/* 61 - 6C */
8087 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8088/* _ */ 0x1F,
8089/* 6E - 80 */
8090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8091/* a */ 0x01,
8092/* b */ 0x02,
8093/* c */ 0x03,
8094/* d */ 0x37,
8095/* e */ 0x2D,
8096/* f */ 0x2E,
8097/* g */ 0x2F,
8098/* h */ 0x16,
8099/* i */ 0x05,
8100/* 8A - 90 */
8101 0, 0, 0, 0, 0, 0, 0,
8102/* j */ 0x15,
8103/* k */ 0x0B,
8104/* l */ 0x0C,
8105/* m */ 0x0D,
8106/* n */ 0x0E,
8107/* o */ 0x0F,
8108/* p */ 0x10,
8109/* q */ 0x11,
8110/* r */ 0x12,
8111/* 9A - A1 */
8112 0, 0, 0, 0, 0, 0, 0, 0,
8113/* s */ 0x13,
8114/* t */ 0x3C,
8115/* u */ 0x3D,
8116/* v */ 0x32,
8117/* w */ 0x26,
8118/* x */ 0x18,
8119/* y */ 0x19,
8120/* z */ 0x3F,
8121/* AA - AC */
8122 0, 0, 0,
8123/* [ */ 0x27,
8124/* AE - BC */
8125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8126/* ] */ 0x1D,
8127/* BE - C0 */ 0, 0, 0,
8128/* A */ 0x01,
8129/* B */ 0x02,
8130/* C */ 0x03,
8131/* D */ 0x37,
8132/* E */ 0x2D,
8133/* F */ 0x2E,
8134/* G */ 0x2F,
8135/* H */ 0x16,
8136/* I */ 0x05,
8137/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8138/* J */ 0x15,
8139/* K */ 0x0B,
8140/* L */ 0x0C,
8141/* M */ 0x0D,
8142/* N */ 0x0E,
8143/* O */ 0x0F,
8144/* P */ 0x10,
8145/* Q */ 0x11,
8146/* R */ 0x12,
8147/* DA - DF */ 0, 0, 0, 0, 0, 0,
8148/* \ */ 0x1C,
8149/* E1 */ 0,
8150/* S */ 0x13,
8151/* T */ 0x3C,
8152/* U */ 0x3D,
8153/* V */ 0x32,
8154/* W */ 0x26,
8155/* X */ 0x18,
8156/* Y */ 0x19,
8157/* Z */ 0x3F,
8158/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8160};
8161
8162char MetaCharTable[]=
8163{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8164 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8165 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8166 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8167 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8168};
8169
8170
8171/* TODO: Use characters NOT numbers!!! */
8172char CtrlCharTable[]=
8173{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8174 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8175 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8176 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8177 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8178};
8179
8180
8181#endif