blob: 3919c1cf24686628b30100a299010b14d4554d5c [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)
987
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100988// argument to SETJMP()
989static JMP_BUF lc_jump_env;
990
991# ifdef SIGHASARG
992// Caught signal number, 0 when no was signal caught; used for mch_libcall().
993// Volatile because it is used in signal handlers.
994static volatile sig_atomic_t lc_signal;
995# endif
996
997// TRUE when lc_jump_env is valid.
998// Volatile because it is used in signal handler deathtrap().
999static volatile sig_atomic_t lc_active INIT(= FALSE);
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001003 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001005 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Usage:
1007 * mch_startjmp();
1008 * if (SETJMP(lc_jump_env) != 0)
1009 * {
1010 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001011 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * }
1013 * else
1014 * {
1015 * do_the_work;
1016 * mch_endjmp();
1017 * }
1018 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1019 * not return before the saved environment is used.
1020 * Returns OK for normal return, FAIL when the protected code caused a
1021 * problem and LONGJMP() was used.
1022 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001023 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001024mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026#ifdef SIGHASARG
1027 lc_signal = 0;
1028#endif
1029 lc_active = TRUE;
1030}
1031
Bram Moolenaar113e1072019-01-20 15:30:40 +01001032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001033mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 lc_active = FALSE;
1036}
1037
Bram Moolenaar113e1072019-01-20 15:30:40 +01001038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001039mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001042 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1043 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 init_signal_stack();
1045# endif
1046}
1047#endif
1048
1049/*
1050 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001051 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001053 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1054 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 */
1056 static RETSIGTYPE
1057deathtrap SIGDEFARG(sigarg)
1058{
1059 static int entered = 0; /* count the number of times we got here.
1060 Note: when memory has been corrupted
1061 this may get an arbitrary value! */
1062#ifdef SIGHASARG
1063 int i;
1064#endif
1065
1066#if defined(HAVE_SETJMP_H)
1067 /*
1068 * Catch a crash in protected code.
1069 * Restores the environment saved in lc_jump_env, which looks like
1070 * SETJMP() returns 1.
1071 */
1072 if (lc_active)
1073 {
1074# if defined(SIGHASARG)
1075 lc_signal = sigarg;
1076# endif
1077 lc_active = FALSE; /* don't jump again */
1078 LONGJMP(lc_jump_env, 1);
1079 /* NOTREACHED */
1080 }
1081#endif
1082
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001083#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001084# ifdef SIGQUIT
1085 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1086 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1087 * pressing CTRL-\, but we don't want Vim to exit then. */
1088 if (in_mch_delay && sigarg == SIGQUIT)
1089 SIGRETURN;
1090# endif
1091
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001092 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1093 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1094 * free(). Calling free() again may then cause a crash. */
1095 if (entered == 0
1096 && (0
1097# ifdef SIGHUP
1098 || sigarg == SIGHUP
1099# endif
1100# ifdef SIGQUIT
1101 || sigarg == SIGQUIT
1102# endif
1103# ifdef SIGTERM
1104 || sigarg == SIGTERM
1105# endif
1106# ifdef SIGPWR
1107 || sigarg == SIGPWR
1108# endif
1109# ifdef SIGUSR1
1110 || sigarg == SIGUSR1
1111# endif
1112# ifdef SIGUSR2
1113 || sigarg == SIGUSR2
1114# endif
1115 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001116 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001117 SIGRETURN;
1118#endif
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 /* Remember how often we have been called. */
1121 ++entered;
1122
Bram Moolenaare429e702016-06-10 19:49:14 +02001123 /* Executing autocommands is likely to use more stack space than we have
1124 * available in the signal stack. */
1125 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#ifdef FEAT_EVAL
1128 /* Set the v:dying variable. */
1129 set_vim_var_nr(VV_DYING, (long)entered);
1130#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001131 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001133#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 /* Since we are now using the signal stack, need to reset the stack
1135 * limit. Otherwise using a regexp will fail. */
1136 get_stack_limit();
1137#endif
1138
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001139#if 0
1140 /* This is for opening gdb the moment Vim crashes.
1141 * You need to manually adjust the file name and Vim executable name.
1142 * Suggested by SungHyun Nam. */
1143 {
1144# define VI_GDB_FILE "/tmp/vimgdb"
1145# define VIM_NAME "/usr/bin/vim"
1146 FILE *fp = fopen(VI_GDB_FILE, "w");
1147 if (fp)
1148 {
1149 fprintf(fp,
1150 "file %s\n"
1151 "attach %d\n"
1152 "set height 1000\n"
1153 "bt full\n"
1154 , VIM_NAME, getpid());
1155 fclose(fp);
1156 system("xterm -e gdb -x "VI_GDB_FILE);
1157 unlink(VI_GDB_FILE);
1158 }
1159 }
1160#endif
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#ifdef SIGHASARG
1163 /* try to find the name of this signal */
1164 for (i = 0; signal_info[i].sig != -1; i++)
1165 if (sigarg == signal_info[i].sig)
1166 break;
1167 deadly_signal = sigarg;
1168#endif
1169
1170 full_screen = FALSE; /* don't write message to the GUI, it might be
1171 * part of the problem... */
1172 /*
1173 * If something goes wrong after entering here, we may get here again.
1174 * When this happens, give a message and try to exit nicely (resetting the
1175 * terminal mode, etc.)
1176 * When this happens twice, just exit, don't even try to give a message,
1177 * stack may be corrupt or something weird.
1178 * When this still happens again (or memory was corrupted in such a way
1179 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1180 */
1181 if (entered >= 3)
1182 {
1183 reset_signals(); /* don't catch any signals anymore */
1184 may_core_dump();
1185 if (entered >= 4)
1186 _exit(8);
1187 exit(7);
1188 }
1189 if (entered == 2)
1190 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001191 /* No translation, it may call malloc(). */
1192 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 out_flush();
1194 getout(1);
1195 }
1196
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001197 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001199 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 signal_info[i].name);
1201#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001202 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001204
1205 /* Preserve files and exit. This sets the really_exiting flag to prevent
1206 * calling free(). */
1207 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaare429e702016-06-10 19:49:14 +02001209 /* NOTREACHED */
1210
Bram Moolenaar009b2592004-10-24 19:18:58 +00001211#ifdef NBDEBUG
1212 reset_signals();
1213 may_core_dump();
1214 abort();
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 SIGRETURN;
1218}
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001221 * Invoked after receiving SIGCONT. We don't know what happened while
1222 * sleeping, deal with part of that.
1223 */
1224 static void
1225after_sigcont(void)
1226{
1227# ifdef FEAT_TITLE
1228 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1229 // again later.
1230 oldtitle_outdated = TRUE;
1231# endif
1232 settmode(TMODE_RAW);
1233 need_check_timestamps = TRUE;
1234 did_check_timestamps = FALSE;
1235}
1236
1237#if defined(SIGCONT)
1238static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001239static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001240
1241/*
1242 * With multi-threading, suspending might not work immediately. Catch the
1243 * SIGCONT signal, which will be used as an indication whether the suspending
1244 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001245 *
1246 * On Linux, signal is not always handled immediately either.
1247 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001248 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001249 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001250 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001252static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001253static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
1255/*
1256 * signal handler for SIGCONT
1257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 static RETSIGTYPE
1259sigcont_handler SIGDEFARG(sigarg)
1260{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001261 if (in_mch_suspend)
1262 {
1263 sigcont_received = TRUE;
1264 }
1265 else
1266 {
1267 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1268 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1269 // back to a sane mode. We should redraw, but we can't really do that
1270 // in a signal handler, do a redraw later.
1271 after_sigcont();
1272 redraw_later(CLEAR);
1273 cursor_on_force();
1274 out_flush();
1275 }
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 SIGRETURN;
1278}
1279#endif
1280
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001281#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001282# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001283static void *clip_star_save = NULL;
1284static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001285# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001286
1287/*
1288 * Called when Vim is going to sleep or execute a shell command.
1289 * We can't respond to requests for the X selections. Lose them, otherwise
1290 * other applications will hang. But first copy the text to cut buffer 0.
1291 */
1292 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001293loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001294{
1295 if (clip_star.owned || clip_plus.owned)
1296 {
1297 x11_export_final_selection();
1298 if (clip_star.owned)
1299 clip_lose_selection(&clip_star);
1300 if (clip_plus.owned)
1301 clip_lose_selection(&clip_plus);
1302 if (x11_display != NULL)
1303 XFlush(x11_display);
1304 }
1305}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001306
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001307# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001308/*
1309 * Save clipboard text to restore later.
1310 */
1311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001312save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001313{
1314 if (clip_star.owned)
1315 clip_star_save = get_register('*', TRUE);
1316 if (clip_plus.owned)
1317 clip_plus_save = get_register('+', TRUE);
1318}
1319
1320/*
1321 * Restore clipboard text if no one own the X selection.
1322 */
1323 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001324restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001325{
1326 if (clip_star_save != NULL)
1327 {
1328 if (!clip_gen_owner_exists(&clip_star))
1329 put_register('*', clip_star_save);
1330 else
1331 free_register(clip_star_save);
1332 clip_star_save = NULL;
1333 }
1334 if (clip_plus_save != NULL)
1335 {
1336 if (!clip_gen_owner_exists(&clip_plus))
1337 put_register('+', clip_plus_save);
1338 else
1339 free_register(clip_plus_save);
1340 clip_plus_save = NULL;
1341 }
1342}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001343# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001344#endif
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346/*
1347 * If the machine has job control, use it to suspend the program,
1348 * otherwise fake it by starting a new shell.
1349 */
1350 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001351mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 /* BeOS does have SIGTSTP, but it doesn't work. */
1354#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001355 in_mch_suspend = TRUE;
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 out_flush(); /* needed to make cursor visible on some systems */
1358 settmode(TMODE_COOK);
1359 out_flush(); /* needed to disable mouse on some systems */
1360
1361# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001362 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001364# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 sigcont_received = FALSE;
1366# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001369
1370# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001371 /*
1372 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001373 * immediately, but somehow not all the time, probably because it's handled
1374 * in another thread. Do not call pause() because there would be race
1375 * condition which would hang Vim if signal happened in between the test of
1376 * sigcont_received and the call to pause(). If signal is not yet received,
1377 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1378 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001379 */
1380 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001381 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001382
Bram Moolenaar262735e2009-07-14 10:20:22 +00001383 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001384 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001387 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
Bram Moolenaar2e310482018-08-21 13:09:10 +02001389 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#else
1391 suspend_shell();
1392#endif
1393}
1394
1395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001396mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 Columns = 80;
1399 Rows = 24;
1400
1401 out_flush();
1402 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001403
Bram Moolenaar56718732006-03-15 22:53:57 +00001404#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001405 mac_conv_init();
1406#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001407#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1408 win_clip_init();
1409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410}
1411
1412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001413set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415#if defined(SIGWINCH)
1416 /*
1417 * WINDOW CHANGE signal is handled with sig_winch().
1418 */
1419 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1420#endif
1421
1422 /*
1423 * We want the STOP signal to work, to make mch_suspend() work.
1424 * For "rvim" the STOP signal is ignored.
1425 */
1426#ifdef SIGTSTP
1427 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1428#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001429#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 signal(SIGCONT, sigcont_handler);
1431#endif
1432
1433 /*
1434 * We want to ignore breaking of PIPEs.
1435 */
1436#ifdef SIGPIPE
1437 signal(SIGPIPE, SIG_IGN);
1438#endif
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001441 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442#endif
1443
1444 /*
1445 * Ignore alarm signals (Perl's alarm() generates it).
1446 */
1447#ifdef SIGALRM
1448 signal(SIGALRM, SIG_IGN);
1449#endif
1450
1451 /*
1452 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1453 * work will be lost.
1454 */
1455#ifdef SIGPWR
1456 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1457#endif
1458
1459 /*
1460 * Arrange for other signals to gracefully shutdown Vim.
1461 */
1462 catch_signals(deathtrap, SIG_ERR);
1463
1464#if defined(FEAT_GUI) && defined(SIGHUP)
1465 /*
1466 * When the GUI is running, ignore the hangup signal.
1467 */
1468 if (gui.in_use)
1469 signal(SIGHUP, SIG_IGN);
1470#endif
1471}
1472
Bram Moolenaardf177f62005-02-22 08:39:57 +00001473#if defined(SIGINT) || defined(PROTO)
1474/*
1475 * Catch CTRL-C (only works while in Cooked mode).
1476 */
1477 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001478catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001479{
1480 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1481}
1482#endif
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001485reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001488#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 /* SIGCONT isn't in the list, because its default action is ignore */
1490 signal(SIGCONT, SIG_DFL);
1491#endif
1492}
1493
1494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001495catch_signals(
1496 RETSIGTYPE (*func_deadly)(),
1497 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 int i;
1500
1501 for (i = 0; signal_info[i].sig != -1; i++)
1502 if (signal_info[i].deadly)
1503 {
1504#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1505 struct sigaction sa;
1506
1507 /* Setup to use the alternate stack for the signal function. */
1508 sa.sa_handler = func_deadly;
1509 sigemptyset(&sa.sa_mask);
1510# if defined(__linux__) && defined(_REENTRANT)
1511 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1512 * thread handling in combination with using the alternate stack:
1513 * pthread library functions try to use the stack pointer to
1514 * identify the current thread, causing a SEGV signal, which
1515 * recursively calls deathtrap() and hangs. */
1516 sa.sa_flags = 0;
1517# else
1518 sa.sa_flags = SA_ONSTACK;
1519# endif
1520 sigaction(signal_info[i].sig, &sa, NULL);
1521#else
1522# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1523 struct sigvec sv;
1524
1525 /* Setup to use the alternate stack for the signal function. */
1526 sv.sv_handler = func_deadly;
1527 sv.sv_mask = 0;
1528 sv.sv_flags = SV_ONSTACK;
1529 sigvec(signal_info[i].sig, &sv, NULL);
1530# else
1531 signal(signal_info[i].sig, func_deadly);
1532# endif
1533#endif
1534 }
1535 else if (func_other != SIG_ERR)
1536 signal(signal_info[i].sig, func_other);
1537}
1538
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001539#ifdef HAVE_SIGPROCMASK
1540 static void
1541block_signals(sigset_t *set)
1542{
1543 sigset_t newset;
1544 int i;
1545
1546 sigemptyset(&newset);
1547
1548 for (i = 0; signal_info[i].sig != -1; i++)
1549 sigaddset(&newset, signal_info[i].sig);
1550
Bram Moolenaar2e310482018-08-21 13:09:10 +02001551# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001552 /* SIGCONT isn't in the list, because its default action is ignore */
1553 sigaddset(&newset, SIGCONT);
1554# endif
1555
1556 sigprocmask(SIG_BLOCK, &newset, set);
1557}
1558
1559 static void
1560unblock_signals(sigset_t *set)
1561{
1562 sigprocmask(SIG_SETMASK, set, NULL);
1563}
1564#endif
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001567 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001568 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1569 * return TRUE
1570 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1571 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001572 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573 * Returns TRUE when Vim should exit.
1574 */
1575 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001576vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001577{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001578 static int got_signal = 0;
1579 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001580
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001581 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001582 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001583 case SIGNAL_BLOCK: blocked = TRUE;
1584 break;
1585
1586 case SIGNAL_UNBLOCK: blocked = FALSE;
1587 if (got_signal != 0)
1588 {
1589 kill(getpid(), got_signal);
1590 got_signal = 0;
1591 }
1592 break;
1593
1594 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001595 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001596 got_signal = sig;
1597#ifdef SIGPWR
1598 if (sig != SIGPWR)
1599#endif
1600 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001601 break;
1602 }
1603 return FALSE;
1604}
1605
1606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 * Check_win checks whether we have an interactive stdout.
1608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001610mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (isatty(1))
1613 return OK;
1614 return FAIL;
1615}
1616
1617/*
1618 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1619 */
1620 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001621mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622{
1623 if (isatty(read_cmd_fd))
1624 return TRUE;
1625 return FALSE;
1626}
1627
1628#ifdef FEAT_X11
1629
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001630# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633/*
1634 * Give a message about the elapsed time for opening the X window.
1635 */
1636 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001637xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640}
1641# endif
1642#endif
1643
1644#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1645/*
1646 * A few functions shared by X11 title and clipboard code.
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649static int got_x_error = FALSE;
1650
1651/*
1652 * X Error handler, otherwise X just exits! (very rude) -- webb
1653 */
1654 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001655x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001657 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 STRCAT(IObuff, _("\nVim: Got X error\n"));
1659
1660 /* We cannot print a message and continue, because no X calls are allowed
1661 * here (causes my system to hang). Silently continuing might be an
1662 * alternative... */
1663 preserve_exit(); /* preserve files and exit */
1664
1665 return 0; /* NOTREACHED */
1666}
1667
1668/*
1669 * Another X Error handler, just used to check for errors.
1670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001672x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 got_x_error = TRUE;
1675 return 0;
1676}
1677
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001678/*
1679 * Return TRUE when connection to the X server is desired.
1680 */
1681 static int
1682x_connect_to_server(void)
1683{
1684 // No point in connecting if we are exiting or dying.
1685 if (exiting || v_dying)
1686 return FALSE;
1687
1688#if defined(FEAT_CLIENTSERVER)
1689 if (x_force_connect)
1690 return TRUE;
1691#endif
1692 if (x_no_connect)
1693 return FALSE;
1694
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001695 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001696 if (clip_exclude_prog != NULL)
1697 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001698 // Just in case we get called recursively, return FALSE. This could
1699 // happen if vpeekc() is used while executing the prog and it causes a
1700 // related callback to be invoked.
1701 if (regprog_in_use(clip_exclude_prog))
1702 return FALSE;
1703
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001704 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1705 return FALSE;
1706 }
1707 return TRUE;
1708}
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1711# if defined(HAVE_SETJMP_H)
1712/*
1713 * An X IO Error handler, used to catch error while opening the display.
1714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001716x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 /* This function should not return, it causes exit(). Longjump instead. */
1719 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001720# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001721 return 0; /* avoid the compiler complains about missing return value */
1722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724# endif
1725
1726/*
1727 * An X IO Error handler, used to catch terminal errors.
1728 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001729static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001732x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001735 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 x11_window = 0;
1737 x11_display = NULL;
1738 xterm_Shell = (Widget)0;
1739
1740 /* This function should not return, it causes exit(). Longjump instead. */
1741 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001742# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001743 return 0; /* avoid the compiler complains about missing return value */
1744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001746
1747/*
1748 * If the X11 connection was lost try to restore it.
1749 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001750 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001751 */
1752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001753may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001754{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001755 // No point in restoring the connecting if we are exiting or dying.
1756 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001757 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001758 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001759
1760# ifndef LESSTIF_VERSION
1761 /* This has been reported to avoid Vim getting stuck. */
1762 if (app_context != (XtAppContext)NULL)
1763 {
1764 XtDestroyApplicationContext(app_context);
1765 app_context = (XtAppContext)NULL;
1766 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1767 }
1768# endif
1769
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001770 setup_term_clip();
1771 get_x11_title(FALSE);
1772 }
1773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
1775
1776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 * Test if "dpy" and x11_window are valid by getting the window title.
1778 * I don't actually want it yet, so there may be a simpler call to use, but
1779 * this will cause the error handler x_error_check() to be called if anything
1780 * is wrong, such as the window pointer being invalid (as can happen when the
1781 * user changes his DISPLAY, but not his WINDOWID) -- webb
1782 */
1783 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001784test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 int (*old_handler)();
1787 XTextProperty text_prop;
1788
1789 old_handler = XSetErrorHandler(x_error_check);
1790 got_x_error = FALSE;
1791 if (XGetWMName(dpy, x11_window, &text_prop))
1792 XFree((void *)text_prop.value);
1793 XSync(dpy, False);
1794 (void)XSetErrorHandler(old_handler);
1795
1796 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001797 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 return (got_x_error ? FAIL : OK);
1800}
1801#endif
1802
1803#ifdef FEAT_TITLE
1804
1805#ifdef FEAT_X11
1806
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001807static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809/*
1810 * try to get x11 window and display
1811 *
1812 * return FAIL for failure, OK otherwise
1813 */
1814 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001815get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 char *winid;
1818 static int result = -1;
1819#define XD_NONE 0 /* x11_display not set here */
1820#define XD_HERE 1 /* x11_display opened here */
1821#define XD_GUI 2 /* x11_display used from gui.dpy */
1822#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1823 static int x11_display_from = XD_NONE;
1824 static int did_set_error_handler = FALSE;
1825
1826 if (!did_set_error_handler)
1827 {
1828 /* X just exits if it finds an error otherwise! */
1829 (void)XSetErrorHandler(x_error_handler);
1830 did_set_error_handler = TRUE;
1831 }
1832
Bram Moolenaar9372a112005-12-06 19:59:18 +00001833#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (gui.in_use)
1835 {
1836 /*
1837 * If the X11 display was opened here before, for the window where Vim
1838 * was started, close that one now to avoid a memory leak.
1839 */
1840 if (x11_display_from == XD_HERE && x11_display != NULL)
1841 {
1842 XCloseDisplay(x11_display);
1843 x11_display_from = XD_NONE;
1844 }
1845 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1846 {
1847 x11_display_from = XD_GUI;
1848 return OK;
1849 }
1850 x11_display = NULL;
1851 return FAIL;
1852 }
1853 else if (x11_display_from == XD_GUI)
1854 {
1855 /* GUI must have stopped somehow, clear x11_display */
1856 x11_window = 0;
1857 x11_display = NULL;
1858 x11_display_from = XD_NONE;
1859 }
1860#endif
1861
1862 /* When started with the "-X" argument, don't try connecting. */
1863 if (!x_connect_to_server())
1864 return FAIL;
1865
1866 /*
1867 * If WINDOWID not set, should try another method to find out
1868 * what the current window number is. The only code I know for
1869 * this is very complicated.
1870 * We assume that zero is invalid for WINDOWID.
1871 */
1872 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1873 x11_window = (Window)atol(winid);
1874
1875#ifdef FEAT_XCLIPBOARD
1876 if (xterm_dpy != NULL && x11_window != 0)
1877 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001878 /* We may have checked it already, but Gnome terminal can move us to
1879 * another window, so we need to check every time. */
1880 if (x11_display_from != XD_XTERM)
1881 {
1882 /*
1883 * If the X11 display was opened here before, for the window where
1884 * Vim was started, close that one now to avoid a memory leak.
1885 */
1886 if (x11_display_from == XD_HERE && x11_display != NULL)
1887 XCloseDisplay(x11_display);
1888 x11_display = xterm_dpy;
1889 x11_display_from = XD_XTERM;
1890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (test_x11_window(x11_display) == FAIL)
1892 {
1893 /* probably bad $WINDOWID */
1894 x11_window = 0;
1895 x11_display = NULL;
1896 x11_display_from = XD_NONE;
1897 return FAIL;
1898 }
1899 return OK;
1900 }
1901#endif
1902
1903 if (x11_window == 0 || x11_display == NULL)
1904 result = -1;
1905
1906 if (result != -1) /* Have already been here and set this */
1907 return result; /* Don't do all these X calls again */
1908
1909 if (x11_window != 0 && x11_display == NULL)
1910 {
1911#ifdef SET_SIG_ALARM
1912 RETSIGTYPE (*sig_save)();
1913#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001914#ifdef ELAPSED_FUNC
1915 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
1917 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001918 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919#endif
1920
1921#ifdef SET_SIG_ALARM
1922 /*
1923 * Opening the Display may hang if the DISPLAY setting is wrong, or
1924 * the network connection is bad. Set an alarm timer to get out.
1925 */
1926 sig_alarm_called = FALSE;
1927 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1928 (RETSIGTYPE (*)())sig_alarm);
1929 alarm(2);
1930#endif
1931 x11_display = XOpenDisplay(NULL);
1932
1933#ifdef SET_SIG_ALARM
1934 alarm(0);
1935 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1936 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001937 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#endif
1939 if (x11_display != NULL)
1940 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001941# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001943 {
1944 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001945 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001946 verbose_leave();
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948# endif
1949 if (test_x11_window(x11_display) == FAIL)
1950 {
1951 /* Maybe window id is bad */
1952 x11_window = 0;
1953 XCloseDisplay(x11_display);
1954 x11_display = NULL;
1955 }
1956 else
1957 x11_display_from = XD_HERE;
1958 }
1959 }
1960 if (x11_window == 0 || x11_display == NULL)
1961 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001962
1963# ifdef FEAT_EVAL
1964 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1965# endif
1966
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 return (result = OK);
1968}
1969
1970/*
1971 * Determine original x11 Window Title
1972 */
1973 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001974get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001976 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977}
1978
1979/*
1980 * Determine original x11 Window icon
1981 */
1982 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001983get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
1985 int retval = FALSE;
1986
1987 retval = get_x11_thing(FALSE, test_only);
1988
1989 /* could not get old icon, use terminal name */
1990 if (oldicon == NULL && !test_only)
1991 {
1992 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001993 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001995 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997
1998 return retval;
1999}
2000
2001 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002002get_x11_thing(
2003 int get_title, /* get title string */
2004 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 XTextProperty text_prop;
2007 int retval = FALSE;
2008 Status status;
2009
2010 if (get_x11_windis() == OK)
2011 {
2012 /* Get window/icon name if any */
2013 if (get_title)
2014 status = XGetWMName(x11_display, x11_window, &text_prop);
2015 else
2016 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2017
2018 /*
2019 * If terminal is xterm, then x11_window may be a child window of the
2020 * outer xterm window that actually contains the window/icon name, so
2021 * keep traversing up the tree until a window with a title/icon is
2022 * found.
2023 */
2024 /* Previously this was only done for xterm and alikes. I don't see a
2025 * reason why it would fail for other terminal emulators.
2026 * if (term_is_xterm) */
2027 {
2028 Window root;
2029 Window parent;
2030 Window win = x11_window;
2031 Window *children;
2032 unsigned int num_children;
2033
2034 while (!status || text_prop.value == NULL)
2035 {
2036 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2037 &num_children))
2038 break;
2039 if (children)
2040 XFree((void *)children);
2041 if (parent == root || parent == 0)
2042 break;
2043
2044 win = parent;
2045 if (get_title)
2046 status = XGetWMName(x11_display, win, &text_prop);
2047 else
2048 status = XGetWMIconName(x11_display, win, &text_prop);
2049 }
2050 }
2051 if (status && text_prop.value != NULL)
2052 {
2053 retval = TRUE;
2054 if (!test_only)
2055 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002056#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2057 if (text_prop.encoding == XA_STRING
2058# ifdef FEAT_MBYTE
2059 && !has_mbyte
2060# endif
2061 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
2063#endif
2064 if (get_title)
2065 oldtitle = vim_strsave((char_u *)text_prop.value);
2066 else
2067 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002068#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 else
2071 {
2072 char **cl;
2073 Status transform_status;
2074 int n = 0;
2075
2076 transform_status = XmbTextPropertyToTextList(x11_display,
2077 &text_prop,
2078 &cl, &n);
2079 if (transform_status >= Success && n > 0 && cl[0])
2080 {
2081 if (get_title)
2082 oldtitle = vim_strsave((char_u *) cl[0]);
2083 else
2084 oldicon = vim_strsave((char_u *) cl[0]);
2085 XFreeStringList(cl);
2086 }
2087 else
2088 {
2089 if (get_title)
2090 oldtitle = vim_strsave((char_u *)text_prop.value);
2091 else
2092 oldicon = vim_strsave((char_u *)text_prop.value);
2093 }
2094 }
2095#endif
2096 }
2097 XFree((void *)text_prop.value);
2098 }
2099 }
2100 return retval;
2101}
2102
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002103/* Xutf8 functions are not avaialble on older systems. Note that on some
2104 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2105 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2106 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002108# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109# define USE_UTF8_STRING
2110# endif
2111#endif
2112
2113/*
2114 * Set x11 Window Title
2115 *
2116 * get_x11_windis() must be called before this and have returned OK
2117 */
2118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002119set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2122 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2123 * supported everywhere and STRING doesn't work for multi-byte titles.
2124 */
2125#ifdef USE_UTF8_STRING
2126 if (enc_utf8)
2127 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2128 NULL, NULL, 0, NULL, NULL, NULL);
2129 else
2130#endif
2131 {
2132#if XtSpecificationRelease >= 4
2133# ifdef FEAT_XFONTSET
2134 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2135 NULL, NULL, 0, NULL, NULL, NULL);
2136# else
2137 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002138 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002141 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 XSetWMProperties(x11_display, x11_window, &text_prop,
2143 NULL, NULL, 0, NULL, NULL, NULL);
2144# endif
2145#else
2146 XStoreName(x11_display, x11_window, (char *)title);
2147#endif
2148 }
2149 XFlush(x11_display);
2150}
2151
2152/*
2153 * Set x11 Window icon
2154 *
2155 * get_x11_windis() must be called before this and have returned OK
2156 */
2157 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002158set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
2160 /* See above for comments about using X*SetWMProperties(). */
2161#ifdef USE_UTF8_STRING
2162 if (enc_utf8)
2163 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2164 NULL, 0, NULL, NULL, NULL);
2165 else
2166#endif
2167 {
2168#if XtSpecificationRelease >= 4
2169# ifdef FEAT_XFONTSET
2170 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2171 NULL, 0, NULL, NULL, NULL);
2172# else
2173 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002174 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002176 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2178 NULL, 0, NULL, NULL, NULL);
2179# endif
2180#else
2181 XSetIconName(x11_display, x11_window, (char *)icon);
2182#endif
2183 }
2184 XFlush(x11_display);
2185}
2186
2187#else /* FEAT_X11 */
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002190get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
2192 return FALSE;
2193}
2194
2195 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002196get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 if (!test_only)
2199 {
2200 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002201 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002203 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 return FALSE;
2206}
2207
2208#endif /* FEAT_X11 */
2209
2210 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002211mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 return get_x11_title(TRUE);
2214}
2215
2216 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002217mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 return get_x11_icon(TRUE);
2220}
2221
2222/*
2223 * Set the window title and icon.
2224 */
2225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002226mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 int type = 0;
2229 static int recursive = 0;
2230
2231 if (T_NAME == NULL) /* no terminal name (yet) */
2232 return;
2233 if (title == NULL && icon == NULL) /* nothing to do */
2234 return;
2235
2236 /* When one of the X11 functions causes a deadly signal, we get here again
2237 * recursively. Avoid hanging then (something is probably locked). */
2238 if (recursive)
2239 return;
2240 ++recursive;
2241
2242 /*
2243 * if the window ID and the display is known, we may use X11 calls
2244 */
2245#ifdef FEAT_X11
2246 if (get_x11_windis() == OK)
2247 type = 1;
2248#else
2249# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2250 if (gui.in_use)
2251 type = 1;
2252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#endif
2254
2255 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002256 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * than x11 calls, because the x11 calls don't always work
2258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if ((type || *T_TS != NUL) && title != NULL)
2260 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002261 if (oldtitle_outdated)
2262 {
2263 oldtitle_outdated = FALSE;
2264 VIM_CLEAR(oldtitle);
2265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (oldtitle == NULL
2267#ifdef FEAT_GUI
2268 && !gui.in_use
2269#endif
2270 ) /* first call but not in GUI, save title */
2271 (void)get_x11_title(FALSE);
2272
2273 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2274 term_settitle(title);
2275#ifdef FEAT_X11
2276 else
2277# ifdef FEAT_GUI_GTK
2278 if (!gui.in_use) /* don't do this if GTK+ is running */
2279# endif
2280 set_x11_title(title); /* x11 */
2281#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002282#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2284 else
2285 gui_mch_settitle(title, icon);
2286#endif
2287 did_set_title = TRUE;
2288 }
2289
2290 if ((type || *T_CIS != NUL) && icon != NULL)
2291 {
2292 if (oldicon == NULL
2293#ifdef FEAT_GUI
2294 && !gui.in_use
2295#endif
2296 ) /* first call, save icon */
2297 get_x11_icon(FALSE);
2298
2299 if (*T_CIS != NUL)
2300 {
2301 out_str(T_CIS); /* set icon start */
2302 out_str_nf(icon);
2303 out_str(T_CIE); /* set icon end */
2304 out_flush();
2305 }
2306#ifdef FEAT_X11
2307 else
2308# ifdef FEAT_GUI_GTK
2309 if (!gui.in_use) /* don't do this if GTK+ is running */
2310# endif
2311 set_x11_icon(icon); /* x11 */
2312#endif
2313 did_set_icon = TRUE;
2314 }
2315 --recursive;
2316}
2317
2318/*
2319 * Restore the window/icon title.
2320 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002321 * SAVE_RESTORE_TITLE only restore title
2322 * SAVE_RESTORE_ICON only restore icon
2323 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 */
2325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002326mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002329 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002331 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2332
2333 // pop and push from/to the stack
2334 term_pop_title(which);
2335 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336}
2337
2338#endif /* FEAT_TITLE */
2339
2340/*
2341 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002342 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 */
2344 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002345vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
2347 if (name == NULL)
2348 return FALSE;
2349 return (STRNICMP(name, "xterm", 5) == 0
2350 || STRNICMP(name, "nxterm", 6) == 0
2351 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002352 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002354 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 || STRCMP(name, "builtin_xterm") == 0);
2356}
2357
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002358#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2359/*
2360 * Return TRUE if "name" appears to be that of a terminal
2361 * known to support the xterm-style mouse protocol.
2362 * Relies on term_is_xterm having been set to its correct value.
2363 */
2364 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002365use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002366{
2367 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002368 && (term_is_xterm
2369 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002370 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002371 || STRICMP(name, "st") == 0
2372 || STRNICMP(name, "st-", 3) == 0
2373 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002374}
2375#endif
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2378/*
2379 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2380 * Return 1 for "xterm".
2381 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002382 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002383 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 */
2385 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002386use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002388 if (ttym_flags == TTYM_SGR)
2389 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002390 if (ttym_flags == TTYM_URXVT)
2391 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (ttym_flags == TTYM_XTERM2)
2393 return 2;
2394 if (ttym_flags == TTYM_XTERM)
2395 return 1;
2396 return 0;
2397}
2398#endif
2399
2400 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002401vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 if (name == NULL)
2404 return FALSE;
2405 return (STRNICMP(name, "iris-ansi", 9) == 0
2406 || STRCMP(name, "builtin_iris-ansi") == 0);
2407}
2408
2409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002410vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 if (name == NULL)
2413 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002414 /* catch VT100 - VT5xx */
2415 return ((STRNICMP(name, "vt", 2) == 0
2416 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 || STRCMP(name, "builtin_vt320") == 0);
2418}
2419
2420/*
2421 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2422 * This should include all windowed terminal emulators.
2423 */
2424 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002425vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 if (name == NULL)
2428 return FALSE;
2429 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2430 return TRUE;
2431 return ( STRNICMP(name, "hpterm", 6) == 0
2432 || STRNICMP(name, "sun-cmd", 7) == 0
2433 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002434 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 || STRNICMP(name, "dtterm", 6) == 0);
2436}
2437
2438/*
2439 * Insert user name in s[len].
2440 * Return OK if a name found.
2441 */
2442 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002443mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002446 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 return OK;
2448#else
2449 return mch_get_uname(getuid(), s, len);
2450#endif
2451}
2452
2453/*
2454 * Insert user name for "uid" in s[len].
2455 * Return OK if a name found.
2456 */
2457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002458mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2461 struct passwd *pw;
2462
2463 if ((pw = getpwuid(uid)) != NULL
2464 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2465 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002466 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 return OK;
2468 }
2469#endif
2470 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2471 return FAIL; /* a number is not a name */
2472}
2473
2474/*
2475 * Insert host name is s[len].
2476 */
2477
2478#ifdef HAVE_SYS_UTSNAME_H
2479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002480mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 struct utsname vutsname;
2483
2484 if (uname(&vutsname) < 0)
2485 *s = NUL;
2486 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002487 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488}
2489#else /* HAVE_SYS_UTSNAME_H */
2490
2491# ifdef HAVE_SYS_SYSTEMINFO_H
2492# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2493# endif
2494
2495 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002496mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498# ifdef VAXC
2499 vaxc$gethostname((char *)s, len);
2500# else
2501 gethostname((char *)s, len);
2502# endif
2503 s[len - 1] = NUL; /* make sure it's terminated */
2504}
2505#endif /* HAVE_SYS_UTSNAME_H */
2506
2507/*
2508 * return process ID
2509 */
2510 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002511mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 return (long)getpid();
2514}
2515
2516#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002518strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520 extern int sys_nerr;
2521 extern char *sys_errlist[];
2522 static char er[20];
2523
2524 if (err > 0 && err < sys_nerr)
2525 return (sys_errlist[err]);
2526 sprintf(er, "Error %d", err);
2527 return er;
2528}
2529#endif
2530
2531/*
2532 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2533 * Return OK for success, FAIL for failure.
2534 */
2535 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002536mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538#if defined(USE_GETCWD)
2539 if (getcwd((char *)buf, len) == NULL)
2540 {
2541 STRCPY(buf, strerror(errno));
2542 return FAIL;
2543 }
2544 return OK;
2545#else
2546 return (getwd((char *)buf) != NULL ? OK : FAIL);
2547#endif
2548}
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002551 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 *
2553 * return FAIL for failure, OK for success
2554 */
2555 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002556mch_FullName(
2557 char_u *fname,
2558 char_u *buf,
2559 int len,
2560 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002563#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 int fd = -1;
2565 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 char_u olddir[MAXPATHL];
2568 char_u *p;
2569 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002570#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002571 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2572 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002573#endif
2574
Bram Moolenaar38323e42007-03-06 19:22:53 +00002575#ifdef VMS
2576 fname = vms_fixfilename(fname);
2577#endif
2578
Bram Moolenaara2442432007-04-26 14:26:37 +00002579#ifdef __CYGWIN__
2580 /*
2581 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2582 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002583# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002584 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2585 * a forward slash. */
2586 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2587 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002588# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002589 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002590# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002591 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002592#endif
2593
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002594 /* Expand it if forced or not an absolute path.
2595 * Do not do it for "/file", the result is always "/". */
2596 if ((force || !mch_isFullName(fname))
2597 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
2599 /*
2600 * If the file name has a path, change to that directory for a moment,
2601 * and then do the getwd() (and get back to where we were).
2602 * This will get the correct path name with "../" things.
2603 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002604 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002606#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 /*
2608 * Use fchdir() if possible, it's said to be faster and more
2609 * reliable. But on SunOS 4 it might not work. Check this by
2610 * doing a fchdir() right now.
2611 */
2612 if (!dont_fchdir)
2613 {
2614 fd = open(".", O_RDONLY | O_EXTRA, 0);
2615 if (fd >= 0 && fchdir(fd) < 0)
2616 {
2617 close(fd);
2618 fd = -1;
2619 dont_fchdir = TRUE; /* don't try again */
2620 }
2621 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 /* Only change directory when we are sure we can return to where
2625 * we are now. After doing "su" chdir(".") might not work. */
2626 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002627#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 (mch_dirname(olddir, MAXPATHL) == FAIL
2631 || mch_chdir((char *)olddir) != 0))
2632 {
2633 p = NULL; /* can't get current dir: don't chdir */
2634 retval = FAIL;
2635 }
2636 else
2637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 /* The directory is copied into buf[], to be able to remove
2639 * the file name without changing it (could be a string in
2640 * read-only memory) */
2641 if (p - fname >= len)
2642 retval = FAIL;
2643 else
2644 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002645 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (mch_chdir((char *)buf))
2647 retval = FAIL;
2648 else
2649 fname = p + 1;
2650 *buf = NUL;
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653 }
2654 if (mch_dirname(buf, len) == FAIL)
2655 {
2656 retval = FAIL;
2657 *buf = NUL;
2658 }
2659 if (p != NULL)
2660 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002661#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (fd >= 0)
2663 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002664 if (p_verbose >= 5)
2665 {
2666 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002667 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002668 verbose_leave();
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 l = fchdir(fd);
2671 close(fd);
2672 }
2673 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 l = mch_chdir((char *)olddir);
2676 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002677 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 }
2679
2680 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002681 if (l >= len - 1)
2682 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002683#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002684 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002686 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002691 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
2693
2694 /* Do not append ".", "/dir/." is equal to "/dir". */
2695 if (STRCMP(fname, ".") != 0)
2696 STRCAT(buf, fname);
2697
2698 return OK;
2699}
2700
2701/*
2702 * Return TRUE if "fname" does not depend on the current directory.
2703 */
2704 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002705mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002707#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return ( fname[0] == '/' || fname[0] == '.' ||
2709 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2710 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2711 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002712#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#endif
2715}
2716
Bram Moolenaar24552be2005-12-10 20:17:30 +00002717#if defined(USE_FNAME_CASE) || defined(PROTO)
2718/*
2719 * Set the case of the file name, if it already exists. This will cause the
2720 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002721 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002722 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002724fname_case(
2725 char_u *name,
2726 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002727{
2728 struct stat st;
2729 char_u *slash, *tail;
2730 DIR *dirp;
2731 struct dirent *dp;
2732
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002733 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002734 {
2735 /* Open the directory where the file is located. */
2736 slash = vim_strrchr(name, '/');
2737 if (slash == NULL)
2738 {
2739 dirp = opendir(".");
2740 tail = name;
2741 }
2742 else
2743 {
2744 *slash = NUL;
2745 dirp = opendir((char *)name);
2746 *slash = '/';
2747 tail = slash + 1;
2748 }
2749
2750 if (dirp != NULL)
2751 {
2752 while ((dp = readdir(dirp)) != NULL)
2753 {
2754 /* Only accept names that differ in case and are the same byte
2755 * length. TODO: accept different length name. */
2756 if (STRICMP(tail, dp->d_name) == 0
2757 && STRLEN(tail) == STRLEN(dp->d_name))
2758 {
2759 char_u newname[MAXPATHL + 1];
2760 struct stat st2;
2761
2762 /* Verify the inode is equal. */
2763 vim_strncpy(newname, name, MAXPATHL);
2764 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2765 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002766 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002767 && st.st_ino == st2.st_ino
2768 && st.st_dev == st2.st_dev)
2769 {
2770 STRCPY(tail, dp->d_name);
2771 break;
2772 }
2773 }
2774 }
2775
2776 closedir(dirp);
2777 }
2778 }
2779}
2780#endif
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782/*
2783 * Get file permissions for 'name'.
2784 * Returns -1 when it doesn't exist.
2785 */
2786 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002787mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 struct stat statb;
2790
2791 /* Keep the #ifdef outside of stat(), it may be a macro. */
2792#ifdef VMS
2793 if (stat((char *)vms_fixfilename(name), &statb))
2794#else
2795 if (stat((char *)name, &statb))
2796#endif
2797 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002798#ifdef __INTERIX
2799 /* The top bit makes the value negative, which means the file doesn't
2800 * exist. Remove the bit, we don't use it. */
2801 return statb.st_mode & ~S_ADDACE;
2802#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805}
2806
2807/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002808 * Set file permission for "name" to "perm".
2809 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 */
2811 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002812mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
2814 return (chmod((char *)
2815#ifdef VMS
2816 vms_fixfilename(name),
2817#else
2818 name,
2819#endif
2820 (mode_t)perm) == 0 ? OK : FAIL);
2821}
2822
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002823#if defined(HAVE_FCHMOD) || defined(PROTO)
2824/*
2825 * Set file permission for open file "fd" to "perm".
2826 * Return FAIL for failure, OK otherwise.
2827 */
2828 int
2829mch_fsetperm(int fd, long perm)
2830{
2831 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2832}
2833#endif
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835#if defined(HAVE_ACL) || defined(PROTO)
2836# ifdef HAVE_SYS_ACL_H
2837# include <sys/acl.h>
2838# endif
2839# ifdef HAVE_SYS_ACCESS_H
2840# include <sys/access.h>
2841# endif
2842
2843# ifdef HAVE_SOLARIS_ACL
2844typedef struct vim_acl_solaris_T {
2845 int acl_cnt;
2846 aclent_t *acl_entry;
2847} vim_acl_solaris_T;
2848# endif
2849
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002850#if defined(HAVE_SELINUX) || defined(PROTO)
2851/*
2852 * Copy security info from "from_file" to "to_file".
2853 */
2854 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002855mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002856{
2857 if (from_file == NULL)
2858 return;
2859
2860 if (selinux_enabled == -1)
2861 selinux_enabled = is_selinux_enabled();
2862
2863 if (selinux_enabled > 0)
2864 {
2865 security_context_t from_context = NULL;
2866 security_context_t to_context = NULL;
2867
2868 if (getfilecon((char *)from_file, &from_context) < 0)
2869 {
2870 /* If the filesystem doesn't support extended attributes,
2871 the original had no special security context and the
2872 target cannot have one either. */
2873 if (errno == EOPNOTSUPP)
2874 return;
2875
Bram Moolenaar32526b32019-01-19 17:43:09 +01002876 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002877 msg_outtrans(from_file);
2878 msg_putchar('\n');
2879 return;
2880 }
2881 if (getfilecon((char *)to_file, &to_context) < 0)
2882 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002883 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002884 msg_outtrans(to_file);
2885 msg_putchar('\n');
2886 freecon (from_context);
2887 return ;
2888 }
2889 if (strcmp(from_context, to_context) != 0)
2890 {
2891 if (setfilecon((char *)to_file, from_context) < 0)
2892 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002893 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002894 msg_outtrans(to_file);
2895 msg_putchar('\n');
2896 }
2897 }
2898 freecon(to_context);
2899 freecon(from_context);
2900 }
2901}
2902#endif /* HAVE_SELINUX */
2903
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002904#if defined(HAVE_SMACK) && !defined(PROTO)
2905/*
2906 * Copy security info from "from_file" to "to_file".
2907 */
2908 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002909mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002910{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002911 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002912 {
2913 XATTR_NAME_SMACK,
2914 XATTR_NAME_SMACKEXEC,
2915 XATTR_NAME_SMACKMMAP
2916 };
2917
2918 char buffer[SMACK_LABEL_LEN];
2919 const char *name;
2920 int index;
2921 int ret;
2922 ssize_t size;
2923
2924 if (from_file == NULL)
2925 return;
2926
2927 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2928 / sizeof(smack_copied_attributes)[0]) ; index++)
2929 {
2930 /* get the name of the attribute to copy */
2931 name = smack_copied_attributes[index];
2932
2933 /* get the value of the attribute in buffer */
2934 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2935 if (size >= 0)
2936 {
2937 /* copy the attribute value of buffer */
2938 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2939 if (ret < 0)
2940 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002941 vim_snprintf((char *)IObuff, IOSIZE,
2942 _("Could not set security context %s for %s"),
2943 name, to_file);
2944 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002945 msg_putchar('\n');
2946 }
2947 }
2948 else
2949 {
2950 /* what reason of not having the attribute value? */
2951 switch (errno)
2952 {
2953 case ENOTSUP:
2954 /* extended attributes aren't supported or enabled */
2955 /* should a message be echoed? not sure... */
2956 return; /* leave because it isn't usefull to continue */
2957
2958 case ERANGE:
2959 default:
2960 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002961 vim_snprintf((char *)IObuff, IOSIZE,
2962 _("Could not get security context %s for %s. Removing it!"),
2963 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002964 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002965 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002966 /* FALLTHROUGH to remove the attribute */
2967
2968 case ENODATA:
2969 /* no attribute of this name */
2970 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002971 /* Silently ignore errors, apparently this happens when
2972 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002973 break;
2974 }
2975 }
2976 }
2977}
2978#endif /* HAVE_SMACK */
2979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980/*
2981 * Return a pointer to the ACL of file "fname" in allocated memory.
2982 * Return NULL if the ACL is not available for whatever reason.
2983 */
2984 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002985mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 vim_acl_T ret = NULL;
2988#ifdef HAVE_POSIX_ACL
2989 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2990#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002991#ifdef HAVE_SOLARIS_ZFS_ACL
2992 acl_t *aclent;
2993
2994 if (acl_get((char *)fname, 0, &aclent) < 0)
2995 return NULL;
2996 ret = (vim_acl_T)aclent;
2997#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#ifdef HAVE_SOLARIS_ACL
2999 vim_acl_solaris_T *aclent;
3000
3001 aclent = malloc(sizeof(vim_acl_solaris_T));
3002 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3003 {
3004 free(aclent);
3005 return NULL;
3006 }
3007 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3008 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3009 {
3010 free(aclent->acl_entry);
3011 free(aclent);
3012 return NULL;
3013 }
3014 ret = (vim_acl_T)aclent;
3015#else
3016#if defined(HAVE_AIX_ACL)
3017 int aclsize;
3018 struct acl *aclent;
3019
3020 aclsize = sizeof(struct acl);
3021 aclent = malloc(aclsize);
3022 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3023 {
3024 if (errno == ENOSPC)
3025 {
3026 aclsize = aclent->acl_len;
3027 aclent = realloc(aclent, aclsize);
3028 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3029 {
3030 free(aclent);
3031 return NULL;
3032 }
3033 }
3034 else
3035 {
3036 free(aclent);
3037 return NULL;
3038 }
3039 }
3040 ret = (vim_acl_T)aclent;
3041#endif /* HAVE_AIX_ACL */
3042#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003043#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#endif /* HAVE_POSIX_ACL */
3045 return ret;
3046}
3047
3048/*
3049 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3050 */
3051 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003052mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 if (aclent == NULL)
3055 return;
3056#ifdef HAVE_POSIX_ACL
3057 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3058#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003059#ifdef HAVE_SOLARIS_ZFS_ACL
3060 acl_set((char *)fname, (acl_t *)aclent);
3061#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062#ifdef HAVE_SOLARIS_ACL
3063 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3064 ((vim_acl_solaris_T *)aclent)->acl_entry);
3065#else
3066#ifdef HAVE_AIX_ACL
3067 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3068#endif /* HAVE_AIX_ACL */
3069#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003070#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#endif /* HAVE_POSIX_ACL */
3072}
3073
3074 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003075mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 if (aclent == NULL)
3078 return;
3079#ifdef HAVE_POSIX_ACL
3080 acl_free((acl_t)aclent);
3081#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003082#ifdef HAVE_SOLARIS_ZFS_ACL
3083 acl_free((acl_t *)aclent);
3084#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#ifdef HAVE_SOLARIS_ACL
3086 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3087 free(aclent);
3088#else
3089#ifdef HAVE_AIX_ACL
3090 free(aclent);
3091#endif /* HAVE_AIX_ACL */
3092#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003093#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#endif /* HAVE_POSIX_ACL */
3095}
3096#endif
3097
3098/*
3099 * Set hidden flag for "name".
3100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003102mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 /* can't hide a file */
3105}
3106
3107/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003108 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * return FALSE if "name" is not a directory
3110 * return FALSE for error
3111 */
3112 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003113mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 struct stat statb;
3116
3117 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3118 return FALSE;
3119 if (stat((char *)name, &statb))
3120 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
3123
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003124/*
3125 * return TRUE if "name" is a directory, NOT a symlink to a directory
3126 * return FALSE if "name" is not a directory
3127 * return FALSE for error
3128 */
3129 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003130mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003131{
3132 struct stat statb;
3133
3134 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3135 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003136 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003137 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003138 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003139}
3140
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141/*
3142 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3143 */
3144 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003145executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 struct stat st;
3148
3149 if (stat((char *)name, &st))
3150 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003151#ifdef VMS
3152 /* Like on Unix system file can have executable rights but not necessarily
3153 * be an executable, but on Unix is not a default for an ordianry file to
3154 * have an executable flag - on VMS it is in most cases.
3155 * Therefore, this check does not have any sense - let keep us to the
3156 * conventions instead:
3157 * *.COM and *.EXE files are the executables - the rest are not. This is
3158 * not ideal but better then it was.
3159 */
3160 int vms_executable = 0;
3161 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3162 {
3163 if (strstr(vms_tolower((char*)name),".exe") != NULL
3164 || strstr(vms_tolower((char*)name),".com")!= NULL)
3165 vms_executable = 1;
3166 }
3167 return vms_executable;
3168#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171}
3172
3173/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003174 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003175 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * Return -1 if unknown.
3177 */
3178 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003179mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 char_u *buf;
3182 char_u *p, *e;
3183 int retval;
3184
Bram Moolenaarb5971142015-03-21 17:32:19 +01003185 /* When "use_path" is false and if it's an absolute or relative path don't
3186 * need to use $PATH. */
3187 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3188 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003189 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003190 /* There must be a path separator, files in the current directory
3191 * can't be executed. */
3192 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003193 {
3194 if (path != NULL)
3195 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003196 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003197 *path = FullName_save(name, TRUE);
3198 else
3199 *path = vim_strsave(name);
3200 }
3201 return TRUE;
3202 }
3203 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 p = (char_u *)getenv("PATH");
3207 if (p == NULL || *p == NUL)
3208 return -1;
3209 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3210 if (buf == NULL)
3211 return -1;
3212
3213 /*
3214 * Walk through all entries in $PATH to check if "name" exists there and
3215 * is an executable file.
3216 */
3217 for (;;)
3218 {
3219 e = (char_u *)strchr((char *)p, ':');
3220 if (e == NULL)
3221 e = p + STRLEN(p);
3222 if (e - p <= 1) /* empty entry means current dir */
3223 STRCPY(buf, "./");
3224 else
3225 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003226 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 add_pathsep(buf);
3228 }
3229 STRCAT(buf, name);
3230 retval = executable_file(buf);
3231 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003232 {
3233 if (path != NULL)
3234 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003235 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003236 *path = FullName_save(buf, TRUE);
3237 else
3238 *path = vim_strsave(buf);
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 if (*e != ':')
3244 break;
3245 p = e + 1;
3246 }
3247
3248 vim_free(buf);
3249 return retval;
3250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252/*
3253 * Check what "name" is:
3254 * NODE_NORMAL: file or directory (or doesn't exist)
3255 * NODE_WRITABLE: writable device, socket, fifo, etc.
3256 * NODE_OTHER: non-writable things
3257 */
3258 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003259mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 struct stat st;
3262
3263 if (stat((char *)name, &st))
3264 return NODE_NORMAL;
3265 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3266 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3268 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 /* Everything else is writable? */
3270 return NODE_WRITABLE;
3271}
3272
3273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003274mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276#ifdef HAVE_CHECK_STACK_GROWTH
3277 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 check_stack_growth((char *)&i);
3280
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003281# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 get_stack_limit();
3283# endif
3284
3285#endif
3286
3287 /*
3288 * Setup an alternative stack for signals. Helps to catch signals when
3289 * running out of stack space.
3290 * Use of sigaltstack() is preferred, it's more portable.
3291 * Ignore any errors.
3292 */
3293#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003294 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 init_signal_stack();
3296#endif
3297}
3298
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003299#if defined(EXITFREE) || defined(PROTO)
3300 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003301mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003302{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003303# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3304 if (clip_star.owned)
3305 clip_lose_selection(&clip_star);
3306 if (clip_plus.owned)
3307 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003308# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003309# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003310 if (xterm_Shell != (Widget)0)
3311 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003312# ifndef LESSTIF_VERSION
3313 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003314 if (xterm_dpy != NULL)
3315 XtCloseDisplay(xterm_dpy);
3316 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003317 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003318 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003319# ifdef FEAT_X11
3320 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3321# endif
3322 }
3323# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003324# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003325# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003326 if (x11_display != NULL
3327# ifdef FEAT_XCLIPBOARD
3328 && x11_display != xterm_dpy
3329# endif
3330 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 XCloseDisplay(x11_display);
3332# endif
3333# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003334 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003335# endif
3336# ifdef FEAT_TITLE
3337 vim_free(oldtitle);
3338 vim_free(oldicon);
3339# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003340}
3341#endif
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343/*
3344 * Output a newline when exiting.
3345 * Make sure the newline goes to the same stream as the text.
3346 */
3347 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003348exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003350 if (silent_mode)
3351 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (newline_on_exit || msg_didout)
3353 {
3354 if (msg_use_printf())
3355 {
3356 if (info_message)
3357 mch_msg("\n");
3358 else
3359 mch_errmsg("\r\n");
3360 }
3361 else
3362 out_char('\n');
3363 }
3364 else
3365 {
3366 restore_cterm_colors(); /* get original colors back */
3367 msg_clr_eos_force(); /* clear the rest of the display */
3368 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3369 }
3370}
3371
3372 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003373mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 exiting = TRUE;
3376
3377#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3378 x11_export_final_selection();
3379#endif
3380
3381#ifdef FEAT_GUI
3382 if (!gui.in_use)
3383#endif
3384 {
3385 settmode(TMODE_COOK);
3386#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003387 // restore xterm title and icon name
3388 mch_restore_title(SAVE_RESTORE_BOTH);
3389 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#endif
3391 /*
3392 * When t_ti is not empty but it doesn't cause swapping terminal
3393 * pages, need to output a newline when msg_didout is set. But when
3394 * t_ti does swap pages it should not go to the shell page. Do this
3395 * before stoptermcap().
3396 */
3397 if (swapping_screen() && !newline_on_exit)
3398 exit_scroll();
3399
3400 /* Stop termcap: May need to check for T_CRV response, which
3401 * requires RAW mode. */
3402 stoptermcap();
3403
3404 /*
3405 * A newline is only required after a message in the alternate screen.
3406 * This is set to TRUE by wait_return().
3407 */
3408 if (!swapping_screen() || newline_on_exit)
3409 exit_scroll();
3410
3411 /* Cursor may have been switched off without calling starttermcap()
3412 * when doing "vim -u vimrc" and vimrc contains ":q". */
3413 if (full_screen)
3414 cursor_on();
3415 }
3416 out_flush();
3417 ml_close_all(TRUE); /* remove all memfiles */
3418 may_core_dump();
3419#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 gui_exit(r);
3422#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003423
Bram Moolenaar56718732006-03-15 22:53:57 +00003424#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003425 mac_conv_cleanup();
3426#endif
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#ifdef __QNX__
3429 /* A core dump won't be created if the signal handler
3430 * doesn't return, so we can't call exit() */
3431 if (deadly_signal != 0)
3432 return;
3433#endif
3434
Bram Moolenaar009b2592004-10-24 19:18:58 +00003435#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003436 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003437#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003438
3439#ifdef EXITFREE
3440 free_all_mem();
3441#endif
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 exit(r);
3444}
3445
3446 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003447may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 if (deadly_signal != 0)
3450 {
3451 signal(deadly_signal, SIG_DFL);
3452 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3453 }
3454}
3455
3456#ifndef VMS
3457
3458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003459mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 static int first = TRUE;
3462
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003463#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464# ifdef HAVE_TERMIOS_H
3465 static struct termios told;
3466 struct termios tnew;
3467# else
3468 static struct termio told;
3469 struct termio tnew;
3470# endif
3471
3472 if (first)
3473 {
3474 first = FALSE;
3475# if defined(HAVE_TERMIOS_H)
3476 tcgetattr(read_cmd_fd, &told);
3477# else
3478 ioctl(read_cmd_fd, TCGETA, &told);
3479# endif
3480 }
3481
3482 tnew = told;
3483 if (tmode == TMODE_RAW)
3484 {
3485 /*
3486 * ~ICRNL enables typing ^V^M
3487 */
3488 tnew.c_iflag &= ~ICRNL;
3489 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3490# if defined(IEXTEN) && !defined(__MINT__)
3491 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3492 /* but it breaks function keys on MINT */
3493# endif
3494 );
3495# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3496 tnew.c_oflag &= ~ONLCR;
3497# endif
3498 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3499 tnew.c_cc[VTIME] = 0; /* don't wait */
3500 }
3501 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003502 {
3503 /* Also reset ICANON here, otherwise on Solaris select() won't see
3504 * typeahead characters. */
3505 tnew.c_lflag &= ~(ICANON | ECHO);
3506 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3507 tnew.c_cc[VTIME] = 0; /* don't wait */
3508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510# if defined(HAVE_TERMIOS_H)
3511 {
3512 int n = 10;
3513
3514 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3515 * few times. */
3516 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3517 && errno == EINTR && n > 0)
3518 --n;
3519 }
3520# else
3521 ioctl(read_cmd_fd, TCSETA, &tnew);
3522# endif
3523
3524#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 /*
3526 * for "old" tty systems
3527 */
3528# ifndef TIOCSETN
3529# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3530# endif
3531 static struct sgttyb ttybold;
3532 struct sgttyb ttybnew;
3533
3534 if (first)
3535 {
3536 first = FALSE;
3537 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3538 }
3539
3540 ttybnew = ttybold;
3541 if (tmode == TMODE_RAW)
3542 {
3543 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3544 ttybnew.sg_flags |= RAW;
3545 }
3546 else if (tmode == TMODE_SLEEP)
3547 ttybnew.sg_flags &= ~(ECHO);
3548 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3549#endif
3550 curr_tmode = tmode;
3551}
3552
3553/*
3554 * Try to get the code for "t_kb" from the stty setting
3555 *
3556 * Even if termcap claims a backspace key, the user's setting *should*
3557 * prevail. stty knows more about reality than termcap does, and if
3558 * somebody's usual erase key is DEL (which, for most BSD users, it will
3559 * be), they're going to get really annoyed if their erase key starts
3560 * doing forward deletes for no reason. (Eric Fischer)
3561 */
3562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003563get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003565 ttyinfo_T info;
3566 char_u buf[2];
3567 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003569 if (get_tty_info(read_cmd_fd, &info) == OK)
3570 {
3571 intr_char = info.interrupt;
3572 buf[0] = info.backspace;
3573 buf[1] = NUL;
3574 add_termcode((char_u *)"kb", buf, FALSE);
3575
3576 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3577 p = find_termcode((char_u *)"kD");
3578 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3579 do_fixdel(NULL);
3580 }
3581}
3582
3583/*
3584 * Obtain the characters that Backspace and Enter produce on "fd".
3585 * Returns OK or FAIL.
3586 */
3587 int
3588get_tty_info(int fd, ttyinfo_T *info)
3589{
3590#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591# ifdef HAVE_TERMIOS_H
3592 struct termios keys;
3593# else
3594 struct termio keys;
3595# endif
3596
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003597 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003599 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003601 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003603 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003605 info->backspace = keys.c_cc[VERASE];
3606 info->interrupt = keys.c_cc[VINTR];
3607 if (keys.c_iflag & ICRNL)
3608 info->enter = NL;
3609 else
3610 info->enter = CAR;
3611 if (keys.c_oflag & ONLCR)
3612 info->nl_does_cr = TRUE;
3613 else
3614 info->nl_does_cr = FALSE;
3615 return OK;
3616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#else
3618 /* for "old" tty systems */
3619 struct sgttyb keys;
3620
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003621 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003623 info->backspace = keys.sg_erase;
3624 info->interrupt = keys.sg_kill;
3625 info->enter = CAR;
3626 info->nl_does_cr = TRUE;
3627 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003630 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631}
3632
3633#endif /* VMS */
3634
3635#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003636static int mouse_ison = FALSE;
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638/*
3639 * Set mouse clicks on or off.
3640 */
3641 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003642mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003644# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003645 static int bevalterm_ison = FALSE;
3646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 int xterm_mouse_vers;
3648
Bram Moolenaara06afc72018-08-27 23:24:16 +02003649# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3650 if (!on)
3651 // Make sure not tracing mouse movements. Important when a button-down
3652 // was received but no release yet.
3653 stop_xterm_trace();
3654# endif
3655
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003656 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003657# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003658 && p_bevalterm == bevalterm_ison
3659# endif
3660 )
3661 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return;
3663
3664 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003665
3666# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003667 if (ttym_flags == TTYM_URXVT)
3668 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003669 out_str_nf((char_u *)
3670 (on
3671 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3672 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003673 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003674 }
3675# endif
3676
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003677# ifdef FEAT_MOUSE_SGR
3678 if (ttym_flags == TTYM_SGR)
3679 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003680 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003681 out_str_nf((char_u *)
3682 (on
3683 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3684 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003685 mouse_ison = on;
3686 }
3687# endif
3688
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003689# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690 if (bevalterm_ison != (p_bevalterm && on))
3691 {
3692 bevalterm_ison = (p_bevalterm && on);
3693 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3694 /* disable mouse movement events, enabling is below */
3695 out_str_nf((char_u *)
3696 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003697 }
3698# endif
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (xterm_mouse_vers > 0)
3701 {
3702 if (on) /* enable mouse events, use mouse tracking if available */
3703 out_str_nf((char_u *)
3704 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003705 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003706# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 bevalterm_ison
3708 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3709# endif
3710 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3712 else /* disable mouse events, could probably always send the same */
3713 out_str_nf((char_u *)
3714 (xterm_mouse_vers > 1
3715 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3716 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003717 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
3720# ifdef FEAT_MOUSE_DEC
3721 else if (ttym_flags == TTYM_DEC)
3722 {
3723 if (on) /* enable mouse events */
3724 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3725 else /* disable mouse events */
3726 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729# endif
3730
3731# ifdef FEAT_MOUSE_GPM
3732 else
3733 {
3734 if (on)
3735 {
3736 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003737 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739 else
3740 {
3741 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003742 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744 }
3745# endif
3746
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003747# ifdef FEAT_SYSMOUSE
3748 else
3749 {
3750 if (on)
3751 {
3752 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003753 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003754 }
3755 else
3756 {
3757 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003758 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003759 }
3760 }
3761# endif
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763# ifdef FEAT_MOUSE_JSB
3764 else
3765 {
3766 if (on)
3767 {
3768 /* D - Enable Mouse up/down messages
3769 * L - Enable Left Button Reporting
3770 * M - Enable Middle Button Reporting
3771 * R - Enable Right Button Reporting
3772 * K - Enable SHIFT and CTRL key Reporting
3773 * + - Enable Advanced messaging of mouse moves and up/down messages
3774 * Q - Quiet No Ack
3775 * # - Numeric value of mouse pointer required
3776 * 0 = Multiview 2000 cursor, used as standard
3777 * 1 = Windows Arrow
3778 * 2 = Windows I Beam
3779 * 3 = Windows Hour Glass
3780 * 4 = Windows Cross Hair
3781 * 5 = Windows UP Arrow
3782 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003783# ifdef JSBTERM_MOUSE_NONADVANCED
3784 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3786 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003787# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3789 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003790# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003791 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 else
3794 {
3795 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3796 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003797 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 }
3800# endif
3801# ifdef FEAT_MOUSE_PTERM
3802 else
3803 {
3804 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3805 if (on)
3806 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3807 else
3808 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003809 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811# endif
3812}
3813
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003814#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003815/*
3816 * Called when 'balloonevalterm' changed.
3817 */
3818 void
3819mch_bevalterm_changed(void)
3820{
3821 mch_setmouse(mouse_ison);
3822}
3823#endif
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825/*
3826 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3827 */
3828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003829check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831# ifdef FEAT_MOUSE_XTERM
3832 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003833# ifdef FEAT_MOUSE_URXVT
3834 && use_xterm_mouse() != 3
3835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836# ifdef FEAT_GUI
3837 && !gui.in_use
3838# endif
3839 )
3840 {
3841 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003842 ? IF_EB("\233M", CSI_STR "M")
3843 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (*p_mouse != NUL)
3845 {
3846 /* force mouse off and maybe on to send possibly new mouse
3847 * activation sequence to the xterm, with(out) drag tracing. */
3848 mch_setmouse(FALSE);
3849 setmouse();
3850 }
3851 }
3852 else
3853 del_mouse_termcode(KS_MOUSE);
3854# endif
3855
3856# ifdef FEAT_MOUSE_GPM
3857 if (!use_xterm_mouse()
3858# ifdef FEAT_GUI
3859 && !gui.in_use
3860# endif
3861 )
3862 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3863# endif
3864
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003865# ifdef FEAT_SYSMOUSE
3866 if (!use_xterm_mouse()
3867# ifdef FEAT_GUI
3868 && !gui.in_use
3869# endif
3870 )
3871 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3872# endif
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003875 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (!use_xterm_mouse()
3877# ifdef FEAT_GUI
3878 && !gui.in_use
3879# endif
3880 )
3881 set_mouse_termcode(KS_JSBTERM_MOUSE,
3882 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3883 else
3884 del_mouse_termcode(KS_JSBTERM_MOUSE);
3885# endif
3886
3887# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003888 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * define it in the GUI or when using an xterm. */
3890 if (!use_xterm_mouse()
3891# ifdef FEAT_GUI
3892 && !gui.in_use
3893# endif
3894 )
3895 set_mouse_termcode(KS_NETTERM_MOUSE,
3896 (char_u *)IF_EB("\033}", ESC_STR "}"));
3897 else
3898 del_mouse_termcode(KS_NETTERM_MOUSE);
3899# endif
3900
3901# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003902 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003903 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904# ifdef FEAT_GUI
3905 && !gui.in_use
3906# endif
3907 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003908 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3909 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 else
3911 del_mouse_termcode(KS_DEC_MOUSE);
3912# endif
3913# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003914 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003915 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916# ifdef FEAT_GUI
3917 && !gui.in_use
3918# endif
3919 )
3920 set_mouse_termcode(KS_PTERM_MOUSE,
3921 (char_u *) IF_EB("\033[", ESC_STR "["));
3922 else
3923 del_mouse_termcode(KS_PTERM_MOUSE);
3924# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003925# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003926 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003927# ifdef FEAT_GUI
3928 && !gui.in_use
3929# endif
3930 )
3931 {
3932 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003933 ? IF_EB("\233*M", CSI_STR "*M")
3934 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003935
3936 if (*p_mouse != NUL)
3937 {
3938 mch_setmouse(FALSE);
3939 setmouse();
3940 }
3941 }
3942 else
3943 del_mouse_termcode(KS_URXVT_MOUSE);
3944# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003945# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003946 if (use_xterm_mouse() == 4
3947# ifdef FEAT_GUI
3948 && !gui.in_use
3949# endif
3950 )
3951 {
3952 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003953 ? IF_EB("\233<*M", CSI_STR "<*M")
3954 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3955
3956 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3957 ? IF_EB("\233<*m", CSI_STR "<*m")
3958 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003959
3960 if (*p_mouse != NUL)
3961 {
3962 mch_setmouse(FALSE);
3963 setmouse();
3964 }
3965 }
3966 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003967 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003968 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003969 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3970 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972}
3973#endif
3974
3975/*
3976 * set screen mode, always fails.
3977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003979mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003981 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
3983}
3984
3985#ifndef VMS
3986
3987/*
3988 * Try to get the current window size:
3989 * 1. with an ioctl(), most accurate method
3990 * 2. from the environment variables LINES and COLUMNS
3991 * 3. from the termcap
3992 * 4. keep using the old values
3993 * Return OK when size could be determined, FAIL otherwise.
3994 */
3995 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003996mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
3998 long rows = 0;
3999 long columns = 0;
4000 char_u *p;
4001
4002 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 * 1. try using an ioctl. It is the most accurate method.
4004 *
4005 * Try using TIOCGWINSZ first, some systems that have it also define
4006 * TIOCGSIZE but don't have a struct ttysize.
4007 */
4008# ifdef TIOCGWINSZ
4009 {
4010 struct winsize ws;
4011 int fd = 1;
4012
4013 /* When stdout is not a tty, use stdin for the ioctl(). */
4014 if (!isatty(fd) && isatty(read_cmd_fd))
4015 fd = read_cmd_fd;
4016 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4017 {
4018 columns = ws.ws_col;
4019 rows = ws.ws_row;
4020 }
4021 }
4022# else /* TIOCGWINSZ */
4023# ifdef TIOCGSIZE
4024 {
4025 struct ttysize ts;
4026 int fd = 1;
4027
4028 /* When stdout is not a tty, use stdin for the ioctl(). */
4029 if (!isatty(fd) && isatty(read_cmd_fd))
4030 fd = read_cmd_fd;
4031 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4032 {
4033 columns = ts.ts_cols;
4034 rows = ts.ts_lines;
4035 }
4036 }
4037# endif /* TIOCGSIZE */
4038# endif /* TIOCGWINSZ */
4039
4040 /*
4041 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004042 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4043 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004045 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 if ((p = (char_u *)getenv("LINES")))
4048 rows = atoi((char *)p);
4049 if ((p = (char_u *)getenv("COLUMNS")))
4050 columns = atoi((char *)p);
4051 }
4052
4053#ifdef HAVE_TGETENT
4054 /*
4055 * 3. try reading "co" and "li" entries from termcap
4056 */
4057 if (columns == 0 || rows == 0)
4058 getlinecol(&columns, &rows);
4059#endif
4060
4061 /*
4062 * 4. If everything fails, use the old values
4063 */
4064 if (columns <= 0 || rows <= 0)
4065 return FAIL;
4066
4067 Rows = rows;
4068 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004069 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return OK;
4071}
4072
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004073#if defined(FEAT_TERMINAL) || defined(PROTO)
4074/*
4075 * Report the windows size "rows" and "cols" to tty "fd".
4076 */
4077 int
4078mch_report_winsize(int fd, int rows, int cols)
4079{
4080# ifdef TIOCSWINSZ
4081 struct winsize ws;
4082
4083 ws.ws_col = cols;
4084 ws.ws_row = rows;
4085 ws.ws_xpixel = cols * 5;
4086 ws.ws_ypixel = rows * 10;
4087 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4088 {
4089 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4090 return OK;
4091 }
4092 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4093# else
4094# ifdef TIOCSSIZE
4095 struct ttysize ts;
4096
4097 ts.ts_cols = cols;
4098 ts.ts_lines = rows;
4099 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4100 {
4101 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4102 return OK;
4103 }
4104 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4105# endif
4106# endif
4107 return FAIL;
4108}
4109#endif
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111/*
4112 * Try to set the window size to Rows and Columns.
4113 */
4114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004115mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
4117 if (*T_CWS)
4118 {
4119 /*
4120 * NOTE: if you get an error here that term_set_winsize() is
4121 * undefined, check the output of configure. It could probably not
4122 * find a ncurses, termcap or termlib library.
4123 */
4124 term_set_winsize((int)Rows, (int)Columns);
4125 out_flush();
4126 screen_start(); /* don't know where cursor is now */
4127 }
4128}
4129
4130#endif /* VMS */
4131
4132/*
4133 * Rows and/or Columns has changed.
4134 */
4135 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004136mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
4138 /* Nothing to do. */
4139}
4140
Bram Moolenaar205b8862011-09-07 15:04:31 +02004141/*
4142 * Wait for process "child" to end.
4143 * Return "child" if it exited properly, <= 0 on error.
4144 */
4145 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004146wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004147{
4148 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004149 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150
4151 while (wait_pid != child)
4152 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004153 /* When compiled with Python threads are probably used, in which case
4154 * wait() sometimes hangs for no obvious reason. Use waitpid()
4155 * instead and loop (like the GUI). Also needed for other interfaces,
4156 * they might call system(). */
4157# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004159# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004160 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004161# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004162 if (wait_pid == 0)
4163 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004164 /* Wait for 1 to 10 msec before trying again. */
4165 mch_delay(delay_msec, TRUE);
4166 if (++delay_msec > 10)
4167 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004168 continue;
4169 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004170 if (wait_pid <= 0
4171# ifdef ECHILD
4172 && errno == ECHILD
4173# endif
4174 )
4175 break;
4176 }
4177 return wait_pid;
4178}
4179
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004180#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004181/*
4182 * Set the environment for a child process.
4183 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004184 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004185set_child_environment(
4186 long rows,
4187 long columns,
4188 char *term,
4189 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004190{
4191# ifdef HAVE_SETENV
4192 char envbuf[50];
4193# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004194 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004195 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004196 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004197 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004198 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004199# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004200 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004201# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004202# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004203 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004204# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004205# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004206 long colors =
4207# ifdef FEAT_GUI
4208 gui.in_use ? 256*256*256 :
4209# endif
4210 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004211
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004212# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004213 setenv("TERM", term, 1);
4214 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004215 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004216 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004217 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004218 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004219 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004220 sprintf((char *)envbuf, "%ld", colors);
4221 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004222# ifdef FEAT_TERMINAL
4223 if (is_terminal)
4224 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004225 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004226 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4227 }
4228# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004229# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004230 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004231# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004232# else
4233 /*
4234 * Putenv does not copy the string, it has to remain valid.
4235 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004236 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004237 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004238 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4239 putenv(envbuf_Term);
4240 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004241 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004242 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4243 putenv(envbuf_Lines);
4244 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4245 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004246 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004247 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4248 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004249# ifdef FEAT_TERMINAL
4250 if (is_terminal)
4251 {
4252 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004253 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004254 putenv(envbuf_Version);
4255 }
4256# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004257# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004258 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4259 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4260 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004261# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004262# endif
4263}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004264
4265 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004266set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004267{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004268 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004269}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270#endif
4271
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004272#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004273/*
4274 * Open a PTY, with FD for the master and slave side.
4275 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4276 * When successful both file descriptors are stored.
4277 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004278 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004279open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004280{
4281 char *tty_name;
4282
4283 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4284 if (*pty_master_fd >= 0)
4285 {
4286 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4287 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4288 * adding O_NOCTTY always works when defined. */
4289#ifdef O_NOCTTY
4290 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4291#else
4292 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4293#endif
4294 if (*pty_slave_fd < 0)
4295 {
4296 close(*pty_master_fd);
4297 *pty_master_fd = -1;
4298 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004299 else if (namep != NULL)
4300 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004301 }
4302}
4303#endif
4304
Bram Moolenaarfae42832017-08-01 22:24:26 +02004305/*
4306 * Send SIGINT to a child process if "c" is an interrupt character.
4307 */
4308 void
4309may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4310{
4311# ifdef SIGINT
4312 if (c == Ctrl_C || c == intr_char)
4313 {
4314# ifdef HAVE_SETSID
4315 kill(-pid, SIGINT);
4316# else
4317 kill(0, SIGINT);
4318# endif
4319 if (wpid > 0)
4320 kill(wpid, SIGINT);
4321 }
4322# endif
4323}
4324
Bram Moolenaar13568252018-03-16 20:46:58 +01004325#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4326
4327 static int
4328build_argv(
4329 char_u *cmd,
4330 char ***argvp,
4331 char_u **sh_tofree,
4332 char_u **shcf_tofree)
4333{
4334 char **argv = NULL;
4335 int argc;
4336
4337 *sh_tofree = vim_strsave(p_sh);
4338 if (*sh_tofree == NULL) /* out of memory */
4339 return FAIL;
4340
4341 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4342 return FAIL;
4343 *argvp = argv;
4344
4345 if (cmd != NULL)
4346 {
4347 char_u *s;
4348 char_u *p;
4349
4350 if (extra_shell_arg != NULL)
4351 argv[argc++] = (char *)extra_shell_arg;
4352
4353 /* Break 'shellcmdflag' into white separated parts. This doesn't
4354 * handle quoted strings, they are very unlikely to appear. */
4355 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4356 if (*shcf_tofree == NULL) /* out of memory */
4357 return FAIL;
4358 s = *shcf_tofree;
4359 p = p_shcf;
4360 while (*p != NUL)
4361 {
4362 argv[argc++] = (char *)s;
4363 while (*p && *p != ' ' && *p != TAB)
4364 *s++ = *p++;
4365 *s++ = NUL;
4366 p = skipwhite(p);
4367 }
4368
4369 argv[argc++] = (char *)cmd;
4370 }
4371 argv[argc] = NULL;
4372 return OK;
4373}
4374#endif
4375
4376#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4377/*
4378 * Use a terminal window to run a shell command in.
4379 */
4380 static int
4381mch_call_shell_terminal(
4382 char_u *cmd,
4383 int options UNUSED) /* SHELL_*, see vim.h */
4384{
4385 jobopt_T opt;
4386 char **argv = NULL;
4387 char_u *tofree1 = NULL;
4388 char_u *tofree2 = NULL;
4389 int retval = -1;
4390 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004391 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004392 aco_save_T aco;
4393 oparg_T oa; /* operator arguments */
4394
4395 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4396 goto theend;
4397
4398 init_job_options(&opt);
4399 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4400 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004401 if (buf == NULL)
4402 goto theend;
4403
4404 job = term_getjob(buf->b_term);
4405 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004406
4407 /* Find a window to make "buf" curbuf. */
4408 aucmd_prepbuf(&aco, buf);
4409
4410 clear_oparg(&oa);
4411 while (term_use_loop())
4412 {
4413 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4414 {
4415 /* If terminal_loop() returns OK we got a key that is handled
4416 * in Normal model. We don't do redrawing anyway. */
4417 if (terminal_loop(TRUE) == OK)
4418 normal_cmd(&oa, TRUE);
4419 }
4420 else
4421 normal_cmd(&oa, TRUE);
4422 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004423 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004424 ch_log(NULL, "system command finished");
4425
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004426 job_unref(job);
4427
Bram Moolenaar13568252018-03-16 20:46:58 +01004428 /* restore curwin/curbuf and a few other things */
4429 aucmd_restbuf(&aco);
4430
4431 wait_return(TRUE);
4432 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4433
4434theend:
4435 vim_free(argv);
4436 vim_free(tofree1);
4437 vim_free(tofree2);
4438 return retval;
4439}
4440#endif
4441
4442#ifdef USE_SYSTEM
4443/*
4444 * Use system() to start the shell: simple but slow.
4445 */
4446 static int
4447mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004448 char_u *cmd,
4449 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
4451#ifdef VMS
4452 char *ifn = NULL;
4453 char *ofn = NULL;
4454#endif
4455 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004456 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004457 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459 out_flush();
4460
4461 if (options & SHELL_COOKED)
4462 settmode(TMODE_COOK); /* set to normal mode */
4463
Bram Moolenaar62b42182010-09-21 22:09:37 +02004464# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004465 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004466 loose_clipboard();
4467# endif
4468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (cmd == NULL)
4470 x = system((char *)p_sh);
4471 else
4472 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004473# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (ofn = strchr((char *)cmd, '>'))
4475 *ofn++ = '\0';
4476 if (ifn = strchr((char *)cmd, '<'))
4477 {
4478 char *p;
4479
4480 *ifn++ = '\0';
4481 p = strchr(ifn,' '); /* chop off any trailing spaces */
4482 if (p)
4483 *p = '\0';
4484 }
4485 if (ofn)
4486 x = vms_sys((char *)cmd, ofn, ifn);
4487 else
4488 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004489# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 newcmd = lalloc(STRLEN(p_sh)
4491 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4492 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4493 if (newcmd == NULL)
4494 x = 0;
4495 else
4496 {
4497 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4498 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4499 (char *)p_shcf,
4500 (char *)cmd);
4501 x = system((char *)newcmd);
4502 vim_free(newcmd);
4503 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506# ifdef VMS
4507 x = vms_sys_status(x);
4508# endif
4509 if (emsg_silent)
4510 ;
4511 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004512 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else if (x && !(options & SHELL_SILENT))
4514 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004515 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 msg_outnum((long)x);
4517 msg_putchar('\n');
4518 }
4519
4520 if (tmode == TMODE_RAW)
4521 settmode(TMODE_RAW); /* set to raw mode */
4522# ifdef FEAT_TITLE
4523 resettitle();
4524# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004525# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4526 restore_clipboard();
4527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
Bram Moolenaar13568252018-03-16 20:46:58 +01004531#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaardf177f62005-02-22 08:39:57 +00004533# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4534 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004535# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
Bram Moolenaar13568252018-03-16 20:46:58 +01004537/*
4538 * Don't use system(), use fork()/exec().
4539 */
4540 static int
4541mch_call_shell_fork(
4542 char_u *cmd,
4543 int options) /* SHELL_*, see vim.h */
4544{
4545 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004547 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 pid_t wait_pid = 0;
4549# ifdef HAVE_UNION_WAIT
4550 union wait status;
4551# else
4552 int status = -1;
4553# endif
4554 int retval = -1;
4555 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004556 char_u *tofree1 = NULL;
4557 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004560# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004562# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004563 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 int fd_fromshell[2];
4565 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004566 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 out_flush();
4569 if (options & SHELL_COOKED)
4570 settmode(TMODE_COOK); /* set to normal mode */
4571
Bram Moolenaar13568252018-03-16 20:46:58 +01004572 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004573 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004576 * For the GUI, when writing the output into the buffer and when reading
4577 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4578 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004580 if ((options & (SHELL_READ|SHELL_WRITE))
4581# ifdef FEAT_GUI
4582 || (gui.in_use && show_shell_mess)
4583# endif
4584 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004586# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 /*
4588 * Try to open a master pty.
4589 * If this works, open the slave pty.
4590 * If the slave can't be opened, close the master pty.
4591 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004593 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 /*
4595 * If not opening a pty or it didn't work, try using pipes.
4596 */
4597 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
4600 pipe_error = (pipe(fd_toshell) < 0);
4601 if (!pipe_error) /* pipe create OK */
4602 {
4603 pipe_error = (pipe(fd_fromshell) < 0);
4604 if (pipe_error) /* pipe create failed */
4605 {
4606 close(fd_toshell[0]);
4607 close(fd_toshell[1]);
4608 }
4609 }
4610 if (pipe_error)
4611 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004612 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 out_flush();
4614 }
4615 }
4616 }
4617
4618 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004620 SIGSET_DECL(curset)
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622# ifdef __BEOS__
4623 beos_cleanup_read_thread();
4624# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004625
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004626 BLOCK_SIGNALS(&curset);
4627 pid = fork(); /* maybe we should use vfork() */
4628 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004630 UNBLOCK_SIGNALS(&curset);
4631
Bram Moolenaar32526b32019-01-19 17:43:09 +01004632 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004633 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004635 || (gui.in_use && show_shell_mess)
4636# endif
4637 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004639# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (pty_master_fd >= 0) /* close the pseudo tty */
4641 {
4642 close(pty_master_fd);
4643 close(pty_slave_fd);
4644 }
4645 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 close(fd_toshell[0]);
4649 close(fd_toshell[1]);
4650 close(fd_fromshell[0]);
4651 close(fd_fromshell[1]);
4652 }
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 else if (pid == 0) /* child */
4656 {
4657 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004658 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
Bram Moolenaar819524702018-02-27 19:10:00 +01004660# ifdef FEAT_JOB_CHANNEL
4661 if (ch_log_active())
4662 /* close the log file in the child */
4663 ch_logfile((char_u *)"", (char_u *)"");
4664# endif
4665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (!show_shell_mess || (options & SHELL_EXPAND))
4667 {
4668 int fd;
4669
4670 /*
4671 * Don't want to show any message from the shell. Can't just
4672 * close stdout and stderr though, because some systems will
4673 * break if you try to write to them after that, so we must
4674 * use dup() to replace them with something else -- webb
4675 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4676 * waiting for input.
4677 */
4678 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4679 fclose(stdin);
4680 fclose(stdout);
4681 fclose(stderr);
4682
4683 /*
4684 * If any of these open()'s and dup()'s fail, we just continue
4685 * anyway. It's not fatal, and on most systems it will make
4686 * no difference at all. On a few it will cause the execvp()
4687 * to exit with a non-zero status even when the completion
4688 * could be done, which is nothing too serious. If the open()
4689 * or dup() failed we'd just do the same thing ourselves
4690 * anyway -- webb
4691 */
4692 if (fd >= 0)
4693 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004694 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4695 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4696 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698 /* Don't need this now that we've duplicated it */
4699 close(fd);
4700 }
4701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004702 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004704 || gui.in_use
4705# endif
4706 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708
Bram Moolenaardf177f62005-02-22 08:39:57 +00004709# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004710 /* Create our own process group, so that the child and all its
4711 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004712 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004713 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004714 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004715 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004716# if defined(SIGHUP)
4717 /* When doing "!xterm&" and 'shell' is bash: the shell
4718 * will exit and send SIGHUP to all processes in its
4719 * group, killing the just started process. Ignore SIGHUP
4720 * to avoid that. (suggested by Simon Schubert)
4721 */
4722 signal(SIGHUP, SIG_IGN);
4723# endif
4724 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004725# endif
4726# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004727 if (pty_slave_fd >= 0)
4728 {
4729 /* push stream discipline modules */
4730 if (options & SHELL_COOKED)
4731 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004733 /* Try to become controlling tty (probably doesn't work,
4734 * unless run by root) */
4735 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004737 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004738# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004739 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
Bram Moolenaara5792f52005-11-23 21:25:05 +00004741 /*
4742 * stderr is only redirected when using the GUI, so that a
4743 * program like gpg can still access the terminal to get a
4744 * passphrase using stderr.
4745 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004746# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (pty_master_fd >= 0)
4748 {
4749 close(pty_master_fd); /* close master side of pty */
4750
4751 /* set up stdin/stdout/stderr for the child */
4752 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004753 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004755 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004756 if (gui.in_use)
4757 {
4758 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004759 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
4762 close(pty_slave_fd); /* has been dupped, close it now */
4763 }
4764 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 /* set up stdin for the child */
4768 close(fd_toshell[1]);
4769 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004770 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 close(fd_toshell[0]);
4772
4773 /* set up stdout for the child */
4774 close(fd_fromshell[0]);
4775 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004776 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 close(fd_fromshell[1]);
4778
Bram Moolenaara5792f52005-11-23 21:25:05 +00004779# ifdef FEAT_GUI
4780 if (gui.in_use)
4781 {
4782 /* set up stderr for the child */
4783 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004784 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004785 }
4786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 /*
4791 * There is no type cast for the argv, because the type may be
4792 * different on different machines. This may cause a warning
4793 * message with strict compilers, don't worry about it.
4794 * Call _exit() instead of exit() to avoid closing the connection
4795 * to the X server (esp. with GTK, which uses atexit()).
4796 */
4797 execvp(argv[0], argv);
4798 _exit(EXEC_FAILED); /* exec failed, return failure code */
4799 }
4800 else /* parent */
4801 {
4802 /*
4803 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004804 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 */
4806 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004808 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004809# ifdef FEAT_JOB_CHANNEL
4810 ++dont_check_job_ended;
4811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
4813 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004814 * This is also used to pipe stdin/stdout to/from the external
4815 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817 if ((options & (SHELL_READ|SHELL_WRITE))
4818# ifdef FEAT_GUI
4819 || (gui.in_use && show_shell_mess)
4820# endif
4821 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004825# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4829 int ta_len = 0; /* valid bytes in ta_buf[] */
4830 int len;
4831 int p_more_save;
4832 int old_State;
4833 int c;
4834 int toshell_fd;
4835 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836 garray_T ga;
4837 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004838# ifdef ELAPSED_FUNC
4839 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (pty_master_fd >= 0)
4844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 fromshell_fd = pty_master_fd;
4846 toshell_fd = dup(pty_master_fd);
4847 }
4848 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 close(fd_toshell[0]);
4852 close(fd_fromshell[1]);
4853 toshell_fd = fd_toshell[1];
4854 fromshell_fd = fd_fromshell[0];
4855 }
4856
4857 /*
4858 * Write to the child if there are typed characters.
4859 * Read from the child if there are characters available.
4860 * Repeat the reading a few times if more characters are
4861 * available. Need to check for typed keys now and then, but
4862 * not too often (delays when no chars are available).
4863 * This loop is quit if no characters can be read from the pty
4864 * (WaitForChar detected special condition), or there are no
4865 * characters available and the child has exited.
4866 * Only check if the child has exited when there is no more
4867 * output. The child may exit before all the output has
4868 * been printed.
4869 *
4870 * Currently this busy loops!
4871 * This can probably dead-lock when the write blocks!
4872 */
4873 p_more_save = p_more;
4874 p_more = FALSE;
4875 old_State = State;
4876 State = EXTERNCMD; /* don't redraw at window resize */
4877
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004878 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004879 {
4880 /* Fork a process that will write the lines to the
4881 * external program. */
4882 if ((wpid = fork()) == -1)
4883 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004884 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004886 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 {
4888 linenr_T lnum = curbuf->b_op_start.lnum;
4889 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004890 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 size_t l;
4892
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004893 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894 for (;;)
4895 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004896 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897 if (l == 0)
4898 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004899 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004900 /* NL -> NUL translation */
4901 len = write(toshell_fd, "", (size_t)1);
4902 else
4903 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004904 char_u *s = vim_strchr(lp + written, NL);
4905
Bram Moolenaar89d40322006-08-29 15:30:07 +00004906 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004907 s == NULL ? l
4908 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004910 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 {
4912 /* Finished a line, add a NL, unless this line
4913 * should not have one. */
4914 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004915 || (!curbuf->b_p_bin
4916 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004917 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 && (lnum !=
4919 curbuf->b_ml.ml_line_count
4920 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004921 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004922 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 ++lnum;
4924 if (lnum > curbuf->b_op_end.lnum)
4925 {
4926 /* finished all the lines, close pipe */
4927 close(toshell_fd);
4928 toshell_fd = -1;
4929 break;
4930 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004931 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932 written = 0;
4933 }
4934 else if (len > 0)
4935 written += len;
4936 }
4937 _exit(0);
4938 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004939 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 {
4941 close(toshell_fd);
4942 toshell_fd = -1;
4943 }
4944 }
4945
4946 if (options & SHELL_READ)
4947 ga_init2(&ga, 1, BUFLEN);
4948
4949 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004950# ifdef ELAPSED_FUNC
4951 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 for (;;)
4954 {
4955 /*
4956 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004957 * if there are any.
4958 * Don't do this if we are expanding wild cards (would eat
4959 * typeahead).
4960 * Don't do this when filtering and terminal is in cooked
4961 * mode, the shell command will handle the I/O. Avoids
4962 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004963 * Don't get characters when the child has already
4964 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004965 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004966 * while (noread_cnt > 4), avoids that ":r !ls" eats
4967 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 */
4969 len = 0;
4970 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004971 && ((options &
4972 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4973 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004974# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004975 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004976# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004977 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004978 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004979 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004981 if (ta_len == 0)
4982 {
4983 /* Get extra characters when we don't have any.
4984 * Reset the counter and timer. */
4985 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004986# ifdef ELAPSED_FUNC
4987 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004988# endif
4989 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4990 }
4991 if (ta_len > 0 || len > 0)
4992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /*
4994 * For pipes:
4995 * Check for CTRL-C: send interrupt signal to child.
4996 * Check for CTRL-D: EOF, close pipe to child.
4997 */
4998 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * Send SIGINT to the child's group or all
5002 * processes in our group.
5003 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005004 may_send_sigint(ta_buf[ta_len], pid, wpid);
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (pty_master_fd < 0 && toshell_fd >= 0
5007 && ta_buf[ta_len] == Ctrl_D)
5008 {
5009 close(toshell_fd);
5010 toshell_fd = -1;
5011 }
5012 }
5013
5014 /* replace K_BS by <BS> and K_DEL by <DEL> */
5015 for (i = ta_len; i < ta_len + len; ++i)
5016 {
5017 if (ta_buf[i] == CSI && len - i > 2)
5018 {
5019 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5020 if (c == K_DEL || c == K_KDEL || c == K_BS)
5021 {
5022 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5023 (size_t)(len - i - 2));
5024 if (c == K_DEL || c == K_KDEL)
5025 ta_buf[i] = DEL;
5026 else
5027 ta_buf[i] = Ctrl_H;
5028 len -= 2;
5029 }
5030 }
5031 else if (ta_buf[i] == '\r')
5032 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005033# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005035 i += (*mb_ptr2len_len)(ta_buf + i,
5036 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039
5040 /*
5041 * For pipes: echo the typed characters.
5042 * For a pty this does not seem to work.
5043 */
5044 if (pty_master_fd < 0)
5045 {
5046 for (i = ta_len; i < ta_len + len; ++i)
5047 {
5048 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5049 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005050# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 else if (has_mbyte)
5052 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005053 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 msg_outtrans_len(ta_buf + i, l);
5056 i += l - 1;
5057 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 else
5060 msg_outtrans_len(ta_buf + i, 1);
5061 }
5062 windgoto(msg_row, msg_col);
5063 out_flush();
5064 }
5065
5066 ta_len += len;
5067
5068 /*
5069 * Write the characters to the child, unless EOF has
5070 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005071 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005072 * When writing buffer lines, drop the typed
5073 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005075 if (options & SHELL_WRITE)
5076 ta_len = 0;
5077 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
5079 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5080 if (len > 0)
5081 {
5082 ta_len -= len;
5083 mch_memmove(ta_buf, ta_buf + len, ta_len);
5084 }
5085 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088
Bram Moolenaardf177f62005-02-22 08:39:57 +00005089 if (got_int)
5090 {
5091 /* CTRL-C sends a signal to the child, we ignore it
5092 * ourselves */
5093# ifdef HAVE_SETSID
5094 kill(-pid, SIGINT);
5095# else
5096 kill(0, SIGINT);
5097# endif
5098 if (wpid > 0)
5099 kill(wpid, SIGINT);
5100 got_int = FALSE;
5101 }
5102
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 /*
5104 * Check if the child has any characters to be printed.
5105 * Read them and write them to our window. Repeat this as
5106 * long as there is something to do, avoid the 10ms wait
5107 * for mch_inchar(), or sending typeahead characters to
5108 * the external process.
5109 * TODO: This should handle escape sequences, compatible
5110 * to some terminal (vt52?).
5111 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005112 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005113 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005115 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005116# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005118# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 );
5122 if (len <= 0) /* end of file or error */
5123 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005124
5125 noread_cnt = 0;
5126 if (options & SHELL_READ)
5127 {
5128 /* Do NUL -> NL translation, append NL separated
5129 * lines to the current buffer. */
5130 for (i = 0; i < len; ++i)
5131 {
5132 if (buffer[i] == NL)
5133 append_ga_line(&ga);
5134 else if (buffer[i] == NUL)
5135 ga_append(&ga, NL);
5136 else
5137 ga_append(&ga, buffer[i]);
5138 }
5139 }
5140# ifdef FEAT_MBYTE
5141 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
5143 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005144 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaardf177f62005-02-22 08:39:57 +00005146 len += buffer_off;
5147 buffer[len] = NUL;
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 /* Check if the last character in buffer[] is
5150 * incomplete, keep these bytes for the next
5151 * round. */
5152 for (p = buffer; p < buffer + len; p += l)
5153 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005154 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (l == 0)
5156 l = 1; /* NUL byte? */
5157 else if (MB_BYTE2LEN(*p) != l)
5158 break;
5159 }
5160 if (p == buffer) /* no complete character */
5161 {
5162 /* avoid getting stuck at an illegal byte */
5163 if (len >= 12)
5164 ++p;
5165 else
5166 {
5167 buffer_off = len;
5168 continue;
5169 }
5170 }
5171 c = *p;
5172 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005173 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (p < buffer + len)
5175 {
5176 *p = c;
5177 buffer_off = (buffer + len) - p;
5178 mch_memmove(buffer, p, buffer_off);
5179 continue;
5180 }
5181 buffer_off = 0;
5182 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005183# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005187 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189
5190 windgoto(msg_row, msg_col);
5191 cursor_on();
5192 out_flush();
5193 if (got_int)
5194 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005195
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005196# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005197 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005198 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005199 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005200
5201 /* Avoid that we keep looping here without
5202 * checking for a CTRL-C for a long time. Don't
5203 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005204 if (msec > 2000)
5205 {
5206 noread_cnt = 5;
5207 break;
5208 }
5209 }
5210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005213 /* If we already detected the child has finished, continue
5214 * reading output for a short while. Some text may be
5215 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005216 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005217 {
5218 if (noread_cnt < 5)
5219 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005220 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005221 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 /*
5224 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005225 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005227# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005228 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005229# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5233 || (wait_pid == pid && WIFEXITED(status)))
5234 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005235 /* Don't break the loop yet, try reading more
5236 * characters from "fromshell_fd" first. When using
5237 * pipes there might still be something to read and
5238 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005241 else
5242 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005243
Bram Moolenaar95a51352013-03-21 22:53:50 +01005244# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005245 /* Handle any X events, e.g. serving the clipboard. */
5246 clip_update();
5247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249finished:
5250 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005251 if (options & SHELL_READ)
5252 {
5253 if (ga.ga_len > 0)
5254 {
5255 append_ga_line(&ga);
5256 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005257 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005258 }
5259 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005260 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005261 ga_clear(&ga);
5262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 /*
5265 * Give all typeahead that wasn't used back to ui_inchar().
5266 */
5267 if (ta_len)
5268 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 State = old_State;
5270 if (toshell_fd >= 0)
5271 close(toshell_fd);
5272 close(fromshell_fd);
5273 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005274# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005275 else
5276 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005277 long delay_msec = 1;
5278
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005279 /*
5280 * Similar to the loop above, but only handle X events, no
5281 * I/O.
5282 */
5283 for (;;)
5284 {
5285 if (got_int)
5286 {
5287 /* CTRL-C sends a signal to the child, we ignore it
5288 * ourselves */
5289# ifdef HAVE_SETSID
5290 kill(-pid, SIGINT);
5291# else
5292 kill(0, SIGINT);
5293# endif
5294 got_int = FALSE;
5295 }
5296# ifdef __NeXT__
5297 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5298# else
5299 wait_pid = waitpid(pid, &status, WNOHANG);
5300# endif
5301 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5302 || (wait_pid == pid && WIFEXITED(status)))
5303 {
5304 wait_pid = pid;
5305 break;
5306 }
5307
5308 /* Handle any X events, e.g. serving the clipboard. */
5309 clip_update();
5310
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005311 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5312 * less time. */
5313 mch_delay(delay_msec, TRUE);
5314 if (++delay_msec > 10)
5315 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005316 }
5317 }
5318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /*
5321 * Wait until our child has exited.
5322 * Ignore wait() returning pids of other children and returning
5323 * because of some signal like SIGWINCH.
5324 * Don't wait if wait_pid was already set above, indicating the
5325 * child already exited.
5326 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005327 if (wait_pid != pid)
5328 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
Bram Moolenaar624891f2010-10-13 16:22:09 +02005330# ifdef FEAT_GUI
5331 /* Close slave side of pty. Only do this after the child has
5332 * exited, otherwise the child may hang when it tries to write on
5333 * the pty. */
5334 if (pty_master_fd >= 0)
5335 close(pty_slave_fd);
5336# endif
5337
Bram Moolenaardf177f62005-02-22 08:39:57 +00005338 /* Make sure the child that writes to the external program is
5339 * dead. */
5340 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005341 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005342 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005343 wait4pid(wpid, NULL);
5344 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005345
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005346# ifdef FEAT_JOB_CHANNEL
5347 --dont_check_job_ended;
5348# endif
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 /*
5351 * Set to raw mode right now, otherwise a CTRL-C after
5352 * catch_signals() will kill Vim.
5353 */
5354 if (tmode == TMODE_RAW)
5355 settmode(TMODE_RAW);
5356 did_settmode = TRUE;
5357 set_signals();
5358
5359 if (WIFEXITED(status))
5360 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005361 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005363 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
5365 if (retval == EXEC_FAILED)
5366 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005367 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 msg_outtrans(p_sh);
5369 msg_putchar('\n');
5370 }
5371 else if (!(options & SHELL_SILENT))
5372 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005373 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 msg_outnum((long)retval);
5375 msg_putchar('\n');
5376 }
5377 }
5378 }
5379 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005380 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384error:
5385 if (!did_settmode)
5386 if (tmode == TMODE_RAW)
5387 settmode(TMODE_RAW); /* set to raw mode */
5388# ifdef FEAT_TITLE
5389 resettitle();
5390# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005391 vim_free(argv);
5392 vim_free(tofree1);
5393 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005398
5399 int
5400mch_call_shell(
5401 char_u *cmd,
5402 int options) /* SHELL_*, see vim.h */
5403{
5404#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5405 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5406 return mch_call_shell_terminal(cmd, options);
5407#endif
5408#ifdef USE_SYSTEM
5409 return mch_call_shell_system(cmd, options);
5410#else
5411 return mch_call_shell_fork(cmd, options);
5412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413}
5414
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005415#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005416 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005417mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005418{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005419 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005420 int fd_in[2] = {-1, -1}; /* for stdin */
5421 int fd_out[2] = {-1, -1}; /* for stdout */
5422 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005423 int pty_master_fd = -1;
5424 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005425 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005426 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5427 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5428 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005429 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005430 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5431 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005432 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005433 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005434 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005435
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005436 if (use_out_for_err && use_null_for_out)
5437 use_null_for_err = TRUE;
5438
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005439 /* default is to fail */
5440 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005441
Bram Moolenaarb2412082017-08-20 18:09:14 +02005442 if (options->jo_pty
5443 && (!(use_file_for_in || use_null_for_in)
5444 || !(use_file_for_in || use_null_for_out)
5445 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005446 {
5447 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5448 if (job->jv_tty_out != NULL)
5449 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5450 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005451
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005452 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005453 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005454 if (use_file_for_in)
5455 {
5456 char_u *fname = options->jo_io_name[PART_IN];
5457
5458 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5459 if (fd_in[0] < 0)
5460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005461 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005462 goto failed;
5463 }
5464 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005465 else
5466 /* When writing buffer lines to the input don't use the pty, so that
5467 * the pipe can be closed when all lines were written. */
5468 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5469 && pipe(fd_in) < 0)
5470 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005471
5472 if (use_file_for_out)
5473 {
5474 char_u *fname = options->jo_io_name[PART_OUT];
5475
5476 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5477 if (fd_out[1] < 0)
5478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005479 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005480 goto failed;
5481 }
5482 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005483 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005484 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005485
5486 if (use_file_for_err)
5487 {
5488 char_u *fname = options->jo_io_name[PART_ERR];
5489
5490 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5491 if (fd_err[1] < 0)
5492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005493 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005494 goto failed;
5495 }
5496 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005497 else if (!use_out_for_err && !use_null_for_err
5498 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005499 goto failed;
5500
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005501 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5502 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005503 if (options->jo_set & JO_CHANNEL)
5504 {
5505 channel = options->jo_channel;
5506 if (channel != NULL)
5507 ++channel->ch_refcount;
5508 }
5509 else
5510 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005511 if (channel == NULL)
5512 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005513 if (job->jv_tty_out != NULL)
5514 ch_log(channel, "using pty %s on fd %d",
5515 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005516 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005517
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005518 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005519 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005520 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005522 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005523 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005524 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005525 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005526 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005527 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005528 int null_fd = -1;
5529 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005530
Bram Moolenaar835dc632016-02-07 14:27:38 +01005531 /* child */
5532 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005533 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005534
Bram Moolenaar819524702018-02-27 19:10:00 +01005535# ifdef FEAT_JOB_CHANNEL
5536 if (ch_log_active())
5537 /* close the log file in the child */
5538 ch_logfile((char_u *)"", (char_u *)"");
5539# endif
5540
Bram Moolenaar835dc632016-02-07 14:27:38 +01005541# ifdef HAVE_SETSID
5542 /* Create our own process group, so that the child and all its
5543 * children can be kill()ed. Don't do this when using pipes,
5544 * because stdin is not a tty, we would lose /dev/tty. */
5545 (void)setsid();
5546# endif
5547
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005548# ifdef FEAT_TERMINAL
5549 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005550 {
5551 char *term = (char *)T_NAME;
5552
5553#ifdef FEAT_GUI
5554 if (term_is_gui(T_NAME))
5555 /* In the GUI 'term' is not what we want, use $TERM. */
5556 term = getenv("TERM");
5557#endif
5558 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5559 * back to "xterm". */
5560 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5561 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005562 set_child_environment(
5563 (long)options->jo_term_rows,
5564 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005565 term,
5566 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005567 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005568 else
5569# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005570 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005571
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005572 if (options->jo_env != NULL)
5573 {
5574 dict_T *dict = options->jo_env;
5575 hashitem_T *hi;
5576 int todo = (int)dict->dv_hashtab.ht_used;
5577
5578 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5579 if (!HASHITEM_EMPTY(hi))
5580 {
5581 typval_T *item = &dict_lookup(hi)->di_tv;
5582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005583 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005584 --todo;
5585 }
5586 }
5587
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005588 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005589 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005590 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005591 if (null_fd < 0)
5592 {
5593 perror("opening /dev/null failed");
5594 _exit(OPEN_NULL_FAILED);
5595 }
5596 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005597
Bram Moolenaar223896d2017-08-02 22:33:28 +02005598 if (pty_slave_fd >= 0)
5599 {
5600 /* push stream discipline modules */
5601 SetupSlavePTY(pty_slave_fd);
5602# ifdef TIOCSCTTY
5603 /* Try to become controlling tty (probably doesn't work,
5604 * unless run by root) */
5605 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5606# endif
5607 }
5608
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005609 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005610 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005611 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005612 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005613 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005614 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005615 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005616 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005617
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005618 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005619 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005620 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005621 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005622 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005623 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005624 }
5625 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005626 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005627 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005628 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005629 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005630 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005631
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005632 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005633 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005634 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005635 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005636 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005637 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005638 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005639 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005640
5641 if (fd_in[0] >= 0)
5642 close(fd_in[0]);
5643 if (fd_in[1] >= 0)
5644 close(fd_in[1]);
5645 if (fd_out[0] >= 0)
5646 close(fd_out[0]);
5647 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005648 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005649 if (fd_err[0] >= 0)
5650 close(fd_err[0]);
5651 if (fd_err[1] >= 0)
5652 close(fd_err[1]);
5653 if (pty_master_fd >= 0)
5654 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005655 close(pty_master_fd); /* not used in the child */
5656 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005657 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005658
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005659 if (null_fd >= 0)
5660 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005661
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005662 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5663 _exit(EXEC_FAILED);
5664
Bram Moolenaar835dc632016-02-07 14:27:38 +01005665 /* See above for type of argv. */
5666 execvp(argv[0], argv);
5667
Bram Moolenaar4694a172016-04-21 14:05:23 +02005668 if (stderr_works)
5669 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005670# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005671 /* calling free_all_mem() here causes problems. Ignore valgrind
5672 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005673# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005674 _exit(EXEC_FAILED); /* exec failed, return failure code */
5675 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005676
5677 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005678 UNBLOCK_SIGNALS(&curset);
5679
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005680 job->jv_pid = pid;
5681 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005682 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005683
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005684 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005685 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005686 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005687 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005688 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005689 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005690 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005691 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005692 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005693 if (channel != NULL)
5694 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005695 int in_fd = use_file_for_in || use_null_for_in
5696 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5697 int out_fd = use_file_for_out || use_null_for_out
5698 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005699 /* When using pty_master_fd only set it for stdout, do not duplicate it
5700 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005701 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005702 ? INVALID_FD
5703 : fd_err[0] >= 0
5704 ? fd_err[0]
5705 : (out_fd == pty_master_fd
5706 ? INVALID_FD
5707 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005708
5709 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005710 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005711 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005712 else
5713 {
5714 if (fd_in[1] >= 0)
5715 close(fd_in[1]);
5716 if (fd_out[0] >= 0)
5717 close(fd_out[0]);
5718 if (fd_err[0] >= 0)
5719 close(fd_err[0]);
5720 if (pty_master_fd >= 0)
5721 close(pty_master_fd);
5722 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005723
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005724 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005725 return;
5726
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005727failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005728 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005729 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005730 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005731 if (fd_in[1] >= 0)
5732 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005733 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005735 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005736 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005737 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005738 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005739 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005740 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005741 if (pty_master_fd >= 0)
5742 close(pty_master_fd);
5743 if (pty_slave_fd >= 0)
5744 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005745}
5746
5747 char *
5748mch_job_status(job_T *job)
5749{
5750# ifdef HAVE_UNION_WAIT
5751 union wait status;
5752# else
5753 int status = -1;
5754# endif
5755 pid_t wait_pid = 0;
5756
5757# ifdef __NeXT__
5758 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5759# else
5760 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5761# endif
5762 if (wait_pid == -1)
5763 {
5764 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005765 if (job->jv_status < JOB_ENDED)
5766 ch_log(job->jv_channel, "Job no longer exists: %s",
5767 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005768 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769 }
5770 if (wait_pid == 0)
5771 return "run";
5772 if (WIFEXITED(status))
5773 {
5774 /* LINTED avoid "bitwise operation on signed value" */
5775 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005776 if (job->jv_status < JOB_ENDED)
5777 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005778 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005779 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005780 if (WIFSIGNALED(status))
5781 {
5782 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005783 if (job->jv_status < JOB_ENDED)
5784 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005785 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005786 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005787 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005788
5789return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005790 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005791 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005792 return "dead";
5793}
5794
5795 job_T *
5796mch_detect_ended_job(job_T *job_list)
5797{
5798# ifdef HAVE_UNION_WAIT
5799 union wait status;
5800# else
5801 int status = -1;
5802# endif
5803 pid_t wait_pid = 0;
5804 job_T *job;
5805
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005806# ifndef USE_SYSTEM
5807 /* Do not do this when waiting for a shell command to finish, we would get
5808 * the exit value here (and discard it), the exit value obtained there
5809 * would then be wrong. */
5810 if (dont_check_job_ended > 0)
5811 return NULL;
5812# endif
5813
Bram Moolenaar97792de2016-10-15 18:36:49 +02005814# ifdef __NeXT__
5815 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5816# else
5817 wait_pid = waitpid(-1, &status, WNOHANG);
5818# endif
5819 if (wait_pid <= 0)
5820 /* no process ended */
5821 return NULL;
5822 for (job = job_list; job != NULL; job = job->jv_next)
5823 {
5824 if (job->jv_pid == wait_pid)
5825 {
5826 if (WIFEXITED(status))
5827 /* LINTED avoid "bitwise operation on signed value" */
5828 job->jv_exitval = WEXITSTATUS(status);
5829 else if (WIFSIGNALED(status))
5830 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005831 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005832 {
5833 ch_log(job->jv_channel, "Job ended");
5834 job->jv_status = JOB_ENDED;
5835 }
5836 return job;
5837 }
5838 }
5839 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840}
5841
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005842/*
5843 * Send a (deadly) signal to "job".
5844 * Return FAIL if "how" is not a valid name.
5845 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005847mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005848{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005849 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850
Bram Moolenaar923d9262016-02-25 20:56:01 +01005851 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005852 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005853 else if (STRCMP(how, "hup") == 0)
5854 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 else if (STRCMP(how, "quit") == 0)
5856 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005857 else if (STRCMP(how, "int") == 0)
5858 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005859 else if (STRCMP(how, "kill") == 0)
5860 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005861#ifdef SIGWINCH
5862 else if (STRCMP(how, "winch") == 0)
5863 sig = SIGWINCH;
5864#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005865 else if (isdigit(*how))
5866 sig = atoi((char *)how);
5867 else
5868 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005869
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005870 // Never kill ourselves!
5871 if (job->jv_pid != 0)
5872 {
5873 // TODO: have an option to only kill the process, not the group?
5874 kill(-job->jv_pid, sig);
5875 kill(job->jv_pid, sig);
5876 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005877
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 return OK;
5879}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005880
5881/*
5882 * Clear the data related to "job".
5883 */
5884 void
5885mch_clear_job(job_T *job)
5886{
5887 /* call waitpid because child process may become zombie */
5888# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005889 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005890# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005891 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005892# endif
5893}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005894#endif
5895
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005896#if defined(FEAT_TERMINAL) || defined(PROTO)
5897 int
5898mch_create_pty_channel(job_T *job, jobopt_T *options)
5899{
5900 int pty_master_fd = -1;
5901 int pty_slave_fd = -1;
5902 channel_T *channel;
5903
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005904 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5905 if (job->jv_tty_out != NULL)
5906 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005907 close(pty_slave_fd);
5908
5909 channel = add_channel();
5910 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005911 {
5912 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005913 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005914 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005915 if (job->jv_tty_out != NULL)
5916 ch_log(channel, "using pty %s on fd %d",
5917 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005918 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5919 channel->ch_keep_open = TRUE;
5920
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005921 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5922 * it only needs to be read once. */
5923 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005924 channel_set_job(channel, job, options);
5925 return OK;
5926}
5927#endif
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929/*
5930 * Check for CTRL-C typed by reading all available characters.
5931 * In cooked mode we should get SIGINT, no need to check.
5932 */
5933 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005934mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005936 if ((curr_tmode == TMODE_RAW || force)
5937 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 fill_input_buf(FALSE);
5939}
5940
5941/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005942 * Wait "msec" msec until a character is available from the mouse, keyboard,
5943 * from inbuf[].
5944 * "msec" == -1 will block forever.
5945 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005946 * When "ignore_input" is TRUE even check for pending input when input is
5947 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005948 * "interrupted" (if not NULL) is set to TRUE when no character is available
5949 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005950 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005951 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 */
5953 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005954WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005956#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005957 return ui_wait_for_chars_or_timer(
5958 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005959#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005960 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005961#endif
5962}
5963
5964/*
5965 * Wait "msec" msec until a character is available from the mouse or keyboard
5966 * or from inbuf[].
5967 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005968 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005969 * "interrupted" (if not NULL) is set to TRUE when no character is available
5970 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005971 * When a GUI is being used, this will never get called -- webb
5972 */
5973 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005974WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976#ifdef FEAT_MOUSE_GPM
5977 int gpm_process_wanted;
5978#endif
5979#ifdef FEAT_XCLIPBOARD
5980 int rest;
5981#endif
5982 int avail;
5983
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005984 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 return 1;
5986
5987#if defined(FEAT_MOUSE_DEC)
5988 /* May need to query the mouse position. */
5989 if (WantQueryMouse)
5990 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005991 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5993 }
5994#endif
5995
5996 /*
5997 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5998 * events. This is a bit complicated, because they might both be defined.
5999 */
6000#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6001# ifdef FEAT_XCLIPBOARD
6002 rest = 0;
6003 if (do_xterm_trace())
6004 rest = msec;
6005# endif
6006 do
6007 {
6008# ifdef FEAT_XCLIPBOARD
6009 if (rest != 0)
6010 {
6011 msec = XT_TRACE_DELAY;
6012 if (rest >= 0 && rest < XT_TRACE_DELAY)
6013 msec = rest;
6014 if (rest >= 0)
6015 rest -= msec;
6016 }
6017# endif
6018# ifdef FEAT_MOUSE_GPM
6019 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006020 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006021 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006023 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024# endif
6025 if (!avail)
6026 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006027 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 return 1;
6029# ifdef FEAT_XCLIPBOARD
6030 if (rest == 0 || !do_xterm_trace())
6031# endif
6032 break;
6033 }
6034 }
6035 while (FALSE
6036# ifdef FEAT_MOUSE_GPM
6037 || (gpm_process_wanted && mch_gpm_process() == 0)
6038# endif
6039# ifdef FEAT_XCLIPBOARD
6040 || (!avail && rest != 0)
6041# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006042 )
6043 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006046 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047#endif
6048 return avail;
6049}
6050
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006051#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052/*
6053 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006054 * "msec" == 0 will check for characters once.
6055 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006058 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006059 * "interrupted" (if not NULL) is set to TRUE when no character is available
6060 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#if defined(__BEOS__)
6063 int
6064#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006065 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006067RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006070 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006071#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 static int busy = FALSE;
6073
6074 /* May retry getting characters after an event was handled. */
6075# define MAY_LOOP
6076
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006077# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 /* Remember at what time we started, so that we know how much longer we
6079 * should wait after being interrupted. */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006080 long start_msec = msec;
6081 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006083 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006084 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085# endif
6086
6087 /* Handle being called recursively. This may happen for the session
6088 * manager stuff, it may save the file, which does a breakcheck. */
6089 if (busy)
6090 return 0;
6091#endif
6092
6093#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006094 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095#endif
6096 {
6097#ifdef MAY_LOOP
6098 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006099# ifdef FEAT_MZSCHEME
6100 int mzquantum_used = FALSE;
6101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#endif
6103#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006104 /* each channel may use in, out and err */
6105 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 int nfd;
6107# ifdef FEAT_XCLIPBOARD
6108 int xterm_idx = -1;
6109# endif
6110# ifdef FEAT_MOUSE_GPM
6111 int gpm_idx = -1;
6112# endif
6113# ifdef USE_XSMP
6114 int xsmp_idx = -1;
6115# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006116 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006118# ifdef FEAT_MZSCHEME
6119 mzvim_check_threads();
6120 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6121 {
6122 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6123 mzquantum_used = TRUE;
6124 }
6125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 fds[0].fd = fd;
6127 fds[0].events = POLLIN;
6128 nfd = 1;
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006131 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (xterm_Shell != (Widget)0)
6133 {
6134 xterm_idx = nfd;
6135 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6136 fds[nfd].events = POLLIN;
6137 nfd++;
6138 }
6139# endif
6140# ifdef FEAT_MOUSE_GPM
6141 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6142 {
6143 gpm_idx = nfd;
6144 fds[nfd].fd = gpm_fd;
6145 fds[nfd].events = POLLIN;
6146 nfd++;
6147 }
6148# endif
6149# ifdef USE_XSMP
6150 if (xsmp_icefd != -1)
6151 {
6152 xsmp_idx = nfd;
6153 fds[nfd].fd = xsmp_icefd;
6154 fds[nfd].events = POLLIN;
6155 nfd++;
6156 }
6157# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006158#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006159 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006160#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006161 if (interrupted != NULL)
6162 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006164 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006165
6166 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006167 if (result == 0 && interrupted != NULL && ret > 0)
6168 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006169
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006170# ifdef FEAT_MZSCHEME
6171 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006172 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006173 finished = FALSE;
6174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176# ifdef FEAT_XCLIPBOARD
6177 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6178 {
6179 xterm_update(); /* Maybe we should hand out clipboard */
6180 if (--ret == 0 && !input_available())
6181 /* Try again */
6182 finished = FALSE;
6183 }
6184# endif
6185# ifdef FEAT_MOUSE_GPM
6186 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6187 {
6188 *check_for_gpm = 1;
6189 }
6190# endif
6191# ifdef USE_XSMP
6192 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6193 {
6194 if (fds[xsmp_idx].revents & POLLIN)
6195 {
6196 busy = TRUE;
6197 xsmp_handle_requests();
6198 busy = FALSE;
6199 }
6200 else if (fds[xsmp_idx].revents & POLLHUP)
6201 {
6202 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006203 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 xsmp_close();
6205 }
6206 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006207 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
6209# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006210#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006211 /* also call when ret == 0, we may be polling a keep-open channel */
6212 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006213 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#else /* HAVE_SELECT */
6217
6218 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006219 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006220 // These are static because they can take 8 Kbyte each and cause the
6221 // signal stack to run out with -O3.
6222 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006224 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006226# ifdef FEAT_MZSCHEME
6227 mzvim_check_threads();
6228 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6229 {
6230 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6231 mzquantum_used = TRUE;
6232 }
6233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006235 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237 tv.tv_sec = towait / 1000;
6238 tv.tv_usec = (towait % 1000) * (1000000/1000);
6239 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006241 else
6242 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244 /*
6245 * Select on ready for reading and exceptional condition (end of file).
6246 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006247select_eintr:
6248 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006249 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 FD_ZERO(&efds);
6251 FD_SET(fd, &rfds);
6252# if !defined(__QNX__) && !defined(__CYGWIN32__)
6253 /* For QNX select() always returns 1 if this is set. Why? */
6254 FD_SET(fd, &efds);
6255# endif
6256 maxfd = fd;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006259 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (xterm_Shell != (Widget)0)
6261 {
6262 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6263 if (maxfd < ConnectionNumber(xterm_dpy))
6264 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006265
6266 /* An event may have already been read but not handled. In
6267 * particulary, XFlush may cause this. */
6268 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
6270# endif
6271# ifdef FEAT_MOUSE_GPM
6272 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6273 {
6274 FD_SET(gpm_fd, &rfds);
6275 FD_SET(gpm_fd, &efds);
6276 if (maxfd < gpm_fd)
6277 maxfd = gpm_fd;
6278 }
6279# endif
6280# ifdef USE_XSMP
6281 if (xsmp_icefd != -1)
6282 {
6283 FD_SET(xsmp_icefd, &rfds);
6284 FD_SET(xsmp_icefd, &efds);
6285 if (maxfd < xsmp_icefd)
6286 maxfd = xsmp_icefd;
6287 }
6288# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006289# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006290 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006291# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006292 if (interrupted != NULL)
6293 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaar643b6142018-09-12 20:29:09 +02006295 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6296 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006297 result = ret > 0 && FD_ISSET(fd, &rfds);
6298 if (result)
6299 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006300 else if (interrupted != NULL && ret > 0)
6301 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006302
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006303# ifdef EINTR
6304 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006305 {
6306 /* Check whether window has been resized, EINTR may be caused by
6307 * SIGWINCH. */
6308 if (do_resize)
6309 handle_resize();
6310
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006311 /* Interrupted by a signal, need to try again. We ignore msec
6312 * here, because we do want to check even after a timeout if
6313 * characters are available. Needed for reading output of an
6314 * external command after the process has finished. */
6315 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006316 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006317# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006318# ifdef __TANDEM
6319 if (ret == -1 && errno == ENOTSUP)
6320 {
6321 FD_ZERO(&rfds);
6322 FD_ZERO(&efds);
6323 ret = 0;
6324 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006325# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006326# ifdef FEAT_MZSCHEME
6327 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006328 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006329 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330# endif
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# ifdef FEAT_XCLIPBOARD
6333 if (ret > 0 && xterm_Shell != (Widget)0
6334 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6335 {
6336 xterm_update(); /* Maybe we should hand out clipboard */
6337 /* continue looping when we only got the X event and the input
6338 * buffer is empty */
6339 if (--ret == 0 && !input_available())
6340 {
6341 /* Try again */
6342 finished = FALSE;
6343 }
6344 }
6345# endif
6346# ifdef FEAT_MOUSE_GPM
6347 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6348 {
6349 if (FD_ISSET(gpm_fd, &efds))
6350 gpm_close();
6351 else if (FD_ISSET(gpm_fd, &rfds))
6352 *check_for_gpm = 1;
6353 }
6354# endif
6355# ifdef USE_XSMP
6356 if (ret > 0 && xsmp_icefd != -1)
6357 {
6358 if (FD_ISSET(xsmp_icefd, &efds))
6359 {
6360 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006361 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 xsmp_close();
6363 if (--ret == 0)
6364 finished = FALSE; /* keep going if event was only one */
6365 }
6366 else if (FD_ISSET(xsmp_icefd, &rfds))
6367 {
6368 busy = TRUE;
6369 xsmp_handle_requests();
6370 busy = FALSE;
6371 if (--ret == 0)
6372 finished = FALSE; /* keep going if event was only one */
6373 }
6374 }
6375# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006376#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006377 /* also call when ret == 0, we may be polling a keep-open channel */
6378 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006379 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
6382#endif /* HAVE_SELECT */
6383
6384#ifdef MAY_LOOP
6385 if (finished || msec == 0)
6386 break;
6387
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006388# ifdef FEAT_CLIENTSERVER
6389 if (server_waiting())
6390 break;
6391# endif
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 /* We're going to loop around again, find out for how long */
6394 if (msec > 0)
6395 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006396# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006398 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399# else
6400 /* Guess we got interrupted halfway. */
6401 msec = msec / 2;
6402# endif
6403 if (msec <= 0)
6404 break; /* waited long enough */
6405 }
6406#endif
6407 }
6408
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006409 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006414 * Expand a path into all matching files and/or directories. Handles "*",
6415 * "?", "[a-z]", "**", etc.
6416 * "path" has backslashes before chars that are not to be expanded.
6417 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 */
6419 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006420mch_expandpath(
6421 garray_T *gap,
6422 char_u *path,
6423 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaar02743632005-07-25 20:42:36 +00006425 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426}
6427#endif
6428
6429/*
6430 * mch_expand_wildcards() - this code does wild-card pattern matching using
6431 * the shell
6432 *
6433 * return OK for success, FAIL for error (you may lose some memory) and put
6434 * an error message in *file.
6435 *
6436 * num_pat is number of input patterns
6437 * pat is array of pointers to input patterns
6438 * num_file is pointer to number of matched file names
6439 * file is pointer to array of pointers to matched file names
6440 */
6441
6442#ifndef SEEK_SET
6443# define SEEK_SET 0
6444#endif
6445#ifndef SEEK_END
6446# define SEEK_END 2
6447#endif
6448
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006449#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006452mch_expand_wildcards(
6453 int num_pat,
6454 char_u **pat,
6455 int *num_file,
6456 char_u ***file,
6457 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 int i;
6460 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006461 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 char_u *p;
6463 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
Bram Moolenaarc7247912008-01-13 12:54:11 +00006465 /*
6466 * This is the non-OS/2 implementation (really Unix).
6467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 int j;
6469 char_u *tempname;
6470 char_u *command;
6471 FILE *fd;
6472 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006473#define STYLE_ECHO 0 /* use "echo", the default */
6474#define STYLE_GLOB 1 /* use "glob", for csh */
6475#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6476#define STYLE_PRINT 3 /* use "print -N", for zsh */
6477#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6478 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 int shell_style = STYLE_ECHO;
6480 int check_spaces;
6481 static int did_find_nul = FALSE;
6482 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006483 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006484 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486 *num_file = 0; /* default: no files found */
6487 *file = NULL;
6488
6489 /*
6490 * If there are no wildcards, just copy the names to allocated memory.
6491 * Saves a lot of time, because we don't have to start a new shell.
6492 */
6493 if (!have_wildcard(num_pat, pat))
6494 return save_patterns(num_pat, pat, num_file, file);
6495
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006496# ifdef HAVE_SANDBOX
6497 /* Don't allow any shell command in the sandbox. */
6498 if (sandbox != 0 && check_secure())
6499 return FAIL;
6500# endif
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 /*
6503 * Don't allow the use of backticks in secure and restricted mode.
6504 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006505 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 for (i = 0; i < num_pat; ++i)
6507 if (vim_strchr(pat[i], '`') != NULL
6508 && (check_restricted() || check_secure()))
6509 return FAIL;
6510
6511 /*
6512 * get a name for the temp file
6513 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006514 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006516 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 return FAIL;
6518 }
6519
6520 /*
6521 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006522 * file.
6523 * STYLE_BT: NL separated
6524 * If expanding `cmd` execute it directly.
6525 * STYLE_GLOB: NUL separated
6526 * If we use *csh, "glob" will work better than "echo".
6527 * STYLE_PRINT: NL or NUL separated
6528 * If we use *zsh, "print -N" will work better than "glob".
6529 * STYLE_VIMGLOB: NL separated
6530 * If we use *sh*, we define "vimglob()".
6531 * STYLE_ECHO: space separated.
6532 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 */
6534 if (num_pat == 1 && *pat[0] == '`'
6535 && (len = STRLEN(pat[0])) > 2
6536 && *(pat[0] + len - 1) == '`')
6537 shell_style = STYLE_BT;
6538 else if ((len = STRLEN(p_sh)) >= 3)
6539 {
6540 if (STRCMP(p_sh + len - 3, "csh") == 0)
6541 shell_style = STYLE_GLOB;
6542 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6543 shell_style = STYLE_PRINT;
6544 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006545 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6546 "sh") != NULL)
6547 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaarc7247912008-01-13 12:54:11 +00006549 /* Compute the length of the command. We need 2 extra bytes: for the
6550 * optional '&' and for the NUL.
6551 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006553 if (shell_style == STYLE_VIMGLOB)
6554 len += STRLEN(sh_vimglob_func);
6555
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006556 for (i = 0; i < num_pat; ++i)
6557 {
6558 /* Count the length of the patterns in the same way as they are put in
6559 * "command" below. */
6560#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006562#else
6563 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006564 for (j = 0; pat[i][j] != NUL; ++j)
6565 {
6566 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6567 ++len; /* may add a backslash */
6568 ++len;
6569 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006570#endif
6571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 command = alloc(len);
6573 if (command == NULL)
6574 {
6575 /* out of memory */
6576 vim_free(tempname);
6577 return FAIL;
6578 }
6579
6580 /*
6581 * Build the shell command:
6582 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6583 * recognizes this).
6584 * - Add the shell command to print the expanded names.
6585 * - Add the temp file name.
6586 * - Add the file name patterns.
6587 */
6588 if (shell_style == STYLE_BT)
6589 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006590 /* change `command; command& ` to (command; command ) */
6591 STRCPY(command, "(");
6592 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006594 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006595 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 --p;
6597 if (*p == '&') /* remove trailing '&' */
6598 {
6599 ampersent = TRUE;
6600 *p = ' ';
6601 }
6602 STRCAT(command, ">");
6603 }
6604 else
6605 {
6606 if (flags & EW_NOTFOUND)
6607 STRCPY(command, "set nonomatch; ");
6608 else
6609 STRCPY(command, "unset nonomatch; ");
6610 if (shell_style == STYLE_GLOB)
6611 STRCAT(command, "glob >");
6612 else if (shell_style == STYLE_PRINT)
6613 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006614 else if (shell_style == STYLE_VIMGLOB)
6615 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 else
6617 STRCAT(command, "echo >");
6618 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (shell_style != STYLE_BT)
6623 for (i = 0; i < num_pat; ++i)
6624 {
6625 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006626 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006627 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628#ifdef USE_SYSTEM
6629 STRCAT(command, " \"");
6630 STRCAT(command, pat[i]);
6631 STRCAT(command, "\"");
6632#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006633 int intick = FALSE;
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 p = command + STRLEN(command);
6636 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006637 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006638 {
6639 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006640 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006641 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6642 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006643 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006644 * backslash inside backticks, before a special character
6645 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006646 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006647 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6648 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006649 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006650 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006651 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006652 else if (!intick
6653 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6654 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006655 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006656 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6657 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006658 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006659
6660 /* Copy one character. */
6661 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 *p = NUL;
6664#endif
6665 }
6666 if (flags & EW_SILENT)
6667 show_shell_mess = FALSE;
6668 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006669 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671 /*
6672 * Using zsh -G: If a pattern has no matches, it is just deleted from
6673 * the argument list, otherwise zsh gives an error message and doesn't
6674 * expand any other pattern.
6675 */
6676 if (shell_style == STYLE_PRINT)
6677 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6678
6679 /*
6680 * If we use -f then shell variables set in .cshrc won't get expanded.
6681 * vi can do it, so we will too, but it is only necessary if there is a "$"
6682 * in one of the patterns, otherwise we can still use the fast option.
6683 */
6684 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6685 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6686
6687 /*
6688 * execute the shell command
6689 */
6690 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6691
6692 /* When running in the background, give it some time to create the temp
6693 * file, but don't wait for it to finish. */
6694 if (ampersent)
6695 mch_delay(10L, TRUE);
6696
6697 extra_shell_arg = NULL; /* cleanup */
6698 show_shell_mess = TRUE;
6699 vim_free(command);
6700
Bram Moolenaarc7247912008-01-13 12:54:11 +00006701 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 mch_remove(tempname);
6704 vim_free(tempname);
6705 /*
6706 * With interactive completion, the error message is not printed.
6707 * However with USE_SYSTEM, I don't know how to turn off error messages
6708 * from the shell, so screen may still get messed up -- webb.
6709 */
6710#ifndef USE_SYSTEM
6711 if (!(flags & EW_SILENT))
6712#endif
6713 {
6714 redraw_later_clear(); /* probably messed up screen */
6715 msg_putchar('\n'); /* clear bottom line quickly */
6716 cmdline_row = Rows - 1; /* continue on last line */
6717#ifdef USE_SYSTEM
6718 if (!(flags & EW_SILENT))
6719#endif
6720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006721 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 msg_start(); /* don't overwrite this message */
6723 }
6724 }
6725 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6726 * EW_NOTFOUND is given */
6727 if (shell_style == STYLE_BT)
6728 return FAIL;
6729 goto notfound;
6730 }
6731
6732 /*
6733 * read the names from the file into memory
6734 */
6735 fd = fopen((char *)tempname, READBIN);
6736 if (fd == NULL)
6737 {
6738 /* Something went wrong, perhaps a file name with a special char. */
6739 if (!(flags & EW_SILENT))
6740 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006741 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 msg_start(); /* don't overwrite this message */
6743 }
6744 vim_free(tempname);
6745 goto notfound;
6746 }
6747 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006748 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006750 if (llen < 0)
6751 /* just in case ftell() would fail */
6752 buffer = NULL;
6753 else
6754 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (buffer == NULL)
6756 {
6757 /* out of memory */
6758 mch_remove(tempname);
6759 vim_free(tempname);
6760 fclose(fd);
6761 return FAIL;
6762 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006763 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 i = fread((char *)buffer, 1, len, fd);
6765 fclose(fd);
6766 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006767 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
6769 /* unexpected read error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006770 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 vim_free(tempname);
6772 vim_free(buffer);
6773 return FAIL;
6774 }
6775 vim_free(tempname);
6776
Bram Moolenaarc7247912008-01-13 12:54:11 +00006777# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6779 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006780 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6782 *p++ = buffer[i];
6783 len = p - buffer;
6784# endif
6785
6786
6787 /* file names are separated with Space */
6788 if (shell_style == STYLE_ECHO)
6789 {
6790 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6791 p = buffer;
6792 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6793 {
6794 while (*p != ' ' && *p != '\n')
6795 ++p;
6796 p = skipwhite(p); /* skip to next entry */
6797 }
6798 }
6799 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006800 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6803 p = buffer;
6804 for (i = 0; *p != NUL; ++i) /* count number of entries */
6805 {
6806 while (*p != '\n' && *p != NUL)
6807 ++p;
6808 if (*p != NUL)
6809 ++p;
6810 p = skipwhite(p); /* skip leading white space */
6811 }
6812 }
6813 /* file names are separated with NUL */
6814 else
6815 {
6816 /*
6817 * Some versions of zsh use spaces instead of NULs to separate
6818 * results. Only do this when there is no NUL before the end of the
6819 * buffer, otherwise we would never be able to use file names with
6820 * embedded spaces when zsh does use NULs.
6821 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6822 * don't check for spaces again.
6823 */
6824 check_spaces = FALSE;
6825 if (shell_style == STYLE_PRINT && !did_find_nul)
6826 {
6827 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006828 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006829 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 did_find_nul = TRUE;
6831 else
6832 check_spaces = TRUE;
6833 }
6834
6835 /*
6836 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6837 * already is one, for STYLE_GLOB it needs to be added.
6838 */
6839 if (len && buffer[len - 1] == NUL)
6840 --len;
6841 else
6842 buffer[len] = NUL;
6843 i = 0;
6844 for (p = buffer; p < buffer + len; ++p)
6845 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6846 {
6847 ++i;
6848 *p = NUL;
6849 }
6850 if (len)
6851 ++i; /* count last entry */
6852 }
6853 if (i == 0)
6854 {
6855 /*
6856 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6857 * /bin/sh will happily expand it to nothing rather than returning an
6858 * error; and hey, it's good to check anyway -- webb.
6859 */
6860 vim_free(buffer);
6861 goto notfound;
6862 }
6863 *num_file = i;
6864 *file = (char_u **)alloc(sizeof(char_u *) * i);
6865 if (*file == NULL)
6866 {
6867 /* out of memory */
6868 vim_free(buffer);
6869 return FAIL;
6870 }
6871
6872 /*
6873 * Isolate the individual file names.
6874 */
6875 p = buffer;
6876 for (i = 0; i < *num_file; ++i)
6877 {
6878 (*file)[i] = p;
6879 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006880 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6881 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006883 while (!(shell_style == STYLE_ECHO && *p == ' ')
6884 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 ++p;
6886 if (p == buffer + len) /* last entry */
6887 *p = NUL;
6888 else
6889 {
6890 *p++ = NUL;
6891 p = skipwhite(p); /* skip to next entry */
6892 }
6893 }
6894 else /* NUL separates */
6895 {
6896 while (*p && p < buffer + len) /* skip entry */
6897 ++p;
6898 ++p; /* skip NUL */
6899 }
6900 }
6901
6902 /*
6903 * Move the file names to allocated memory.
6904 */
6905 for (j = 0, i = 0; i < *num_file; ++i)
6906 {
6907 /* Require the files to exist. Helps when using /bin/sh */
6908 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6909 continue;
6910
6911 /* check if this entry should be included */
6912 dir = (mch_isdir((*file)[i]));
6913 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6914 continue;
6915
Bram Moolenaara2031822006-03-07 22:29:51 +00006916 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006917 if (!dir && (flags & EW_EXEC)
6918 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006919 continue;
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6922 if (p)
6923 {
6924 STRCPY(p, (*file)[i]);
6925 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006926 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 (*file)[j++] = p;
6928 }
6929 }
6930 vim_free(buffer);
6931 *num_file = j;
6932
6933 if (*num_file == 0) /* rejected all entries */
6934 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006935 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 goto notfound;
6937 }
6938
6939 return OK;
6940
6941notfound:
6942 if (flags & EW_NOTFOUND)
6943 return save_patterns(num_pat, pat, num_file, file);
6944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945}
6946
6947#endif /* VMS */
6948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006950save_patterns(
6951 int num_pat,
6952 char_u **pat,
6953 int *num_file,
6954 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955{
6956 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006957 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6960 if (*file == NULL)
6961 return FAIL;
6962 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006963 {
6964 s = vim_strsave(pat[i]);
6965 if (s != NULL)
6966 /* Be compatible with expand_filename(): halve the number of
6967 * backslashes. */
6968 backslash_halve(s);
6969 (*file)[i] = s;
6970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 *num_file = num_pat;
6972 return OK;
6973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975/*
6976 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6977 * expand.
6978 */
6979 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006980mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006982 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (*p == '\\' && p[1] != NUL)
6985 ++p;
6986 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (vim_strchr((char_u *)
6988#ifdef VMS
6989 "*?%"
6990#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992#endif
6993 , *p) != NULL)
6994 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 return FALSE;
6997}
6998
6999/*
7000 * Return TRUE if the string "p" contains a wildcard.
7001 * Don't recognize '~' at the end as a wildcard.
7002 */
7003 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007004mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007006 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (*p == '\\' && p[1] != NUL)
7009 ++p;
7010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (vim_strchr((char_u *)
7012#ifdef VMS
7013 "*?%$"
7014#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016#endif
7017 , *p) != NULL
7018 || (*p == '~' && p[1] != NUL))
7019 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021 return FALSE;
7022}
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007025have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 int i;
7028
7029 for (i = 0; i < num; i++)
7030 if (mch_has_wildcard(file[i]))
7031 return 1;
7032 return 0;
7033}
7034
7035 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007036have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 int i;
7039
7040 for (i = 0; i < num; i++)
7041 if (vim_strchr(file[i], '$') != NULL)
7042 return TRUE;
7043 return FALSE;
7044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007046#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047/*
7048 * Scaled-down version of rename(), which is missing in Xenix.
7049 * This version can only move regular files and will fail if the
7050 * destination exists.
7051 */
7052 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007053mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055 struct stat st;
7056
7057 if (stat(dest, &st) >= 0) /* fail if destination exists */
7058 return -1;
7059 if (link(src, dest) != 0) /* link file to new name */
7060 return -1;
7061 if (mch_remove(src) == 0) /* delete link to old name */
7062 return 0;
7063 return -1;
7064}
7065#endif /* !HAVE_RENAME */
7066
7067#ifdef FEAT_MOUSE_GPM
7068/*
7069 * Initializes connection with gpm (if it isn't already opened)
7070 * Return 1 if succeeded (or connection already opened), 0 if failed
7071 */
7072 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007073gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7076
7077 if (!gpm_flag)
7078 {
7079 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7080 gpm_connect.defaultMask = ~GPM_HARD;
7081 /* Default handling for mouse move*/
7082 gpm_connect.minMod = 0; /* Handle any modifier keys */
7083 gpm_connect.maxMod = 0xffff;
7084 if (Gpm_Open(&gpm_connect, 0) > 0)
7085 {
7086 /* gpm library tries to handling TSTP causes
7087 * problems. Anyways, we close connection to Gpm whenever
7088 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007089 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007091# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 return 1; /* succeed */
7095 }
7096 if (gpm_fd == -2)
7097 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7098 return 0;
7099 }
7100 return 1; /* already open */
7101}
7102
7103/*
7104 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 */
7106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007107gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 if (gpm_flag && gpm_fd >= 0) /* if Open */
7110 Gpm_Close();
7111}
7112
7113/* Reads gpm event and adds special keys to input buf. Returns length of
7114 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007115 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 */
7117 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007118mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 int button;
7121 static Gpm_Event gpm_event;
7122 char_u string[6];
7123 int_u vim_modifiers;
7124 int row,col;
7125 unsigned char buttons_mask;
7126 unsigned char gpm_modifiers;
7127 static unsigned char old_buttons = 0;
7128
7129 Gpm_GetEvent(&gpm_event);
7130
7131#ifdef FEAT_GUI
7132 /* Don't put events in the input queue now. */
7133 if (hold_gui_events)
7134 return 0;
7135#endif
7136
7137 row = gpm_event.y - 1;
7138 col = gpm_event.x - 1;
7139
7140 string[0] = ESC; /* Our termcode */
7141 string[1] = 'M';
7142 string[2] = 'G';
7143 switch (GPM_BARE_EVENTS(gpm_event.type))
7144 {
7145 case GPM_DRAG:
7146 string[3] = MOUSE_DRAG;
7147 break;
7148 case GPM_DOWN:
7149 buttons_mask = gpm_event.buttons & ~old_buttons;
7150 old_buttons = gpm_event.buttons;
7151 switch (buttons_mask)
7152 {
7153 case GPM_B_LEFT:
7154 button = MOUSE_LEFT;
7155 break;
7156 case GPM_B_MIDDLE:
7157 button = MOUSE_MIDDLE;
7158 break;
7159 case GPM_B_RIGHT:
7160 button = MOUSE_RIGHT;
7161 break;
7162 default:
7163 return 0;
7164 /*Don't know what to do. Can more than one button be
7165 * reported in one event? */
7166 }
7167 string[3] = (char_u)(button | 0x20);
7168 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7169 break;
7170 case GPM_UP:
7171 string[3] = MOUSE_RELEASE;
7172 old_buttons &= ~gpm_event.buttons;
7173 break;
7174 default:
7175 return 0;
7176 }
7177 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7178 gpm_modifiers = gpm_event.modifiers;
7179 vim_modifiers = 0x0;
7180 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7181 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7182 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7183 */
7184 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7185 vim_modifiers |= MOUSE_SHIFT;
7186
7187 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7188 vim_modifiers |= MOUSE_CTRL;
7189 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7190 vim_modifiers |= MOUSE_ALT;
7191 string[3] |= vim_modifiers;
7192 string[4] = (char_u)(col + ' ' + 1);
7193 string[5] = (char_u)(row + ' ' + 1);
7194 add_to_input_buf(string, 6);
7195 return 6;
7196}
7197#endif /* FEAT_MOUSE_GPM */
7198
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007199#ifdef FEAT_SYSMOUSE
7200/*
7201 * Initialize connection with sysmouse.
7202 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7203 * output, any sysmouse output than will be processed via sig_sysmouse().
7204 * Return OK if succeeded, FAIL if failed.
7205 */
7206 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007207sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007208{
7209 struct mouse_info mouse;
7210
7211 mouse.operation = MOUSE_MODE;
7212 mouse.u.mode.mode = 0;
7213 mouse.u.mode.signal = SIGUSR2;
7214 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7215 {
7216 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7217 mouse.operation = MOUSE_SHOW;
7218 ioctl(1, CONS_MOUSECTL, &mouse);
7219 return OK;
7220 }
7221 return FAIL;
7222}
7223
7224/*
7225 * Stop processing SIGUSR2 signals, and also make sure that
7226 * virtual console do not send us any sysmouse related signal.
7227 */
7228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007229sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007230{
7231 struct mouse_info mouse;
7232
7233 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7234 mouse.operation = MOUSE_MODE;
7235 mouse.u.mode.mode = 0;
7236 mouse.u.mode.signal = 0;
7237 ioctl(1, CONS_MOUSECTL, &mouse);
7238}
7239
7240/*
7241 * Gets info from sysmouse and adds special keys to input buf.
7242 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007243 static RETSIGTYPE
7244sig_sysmouse SIGDEFARG(sigarg)
7245{
7246 struct mouse_info mouse;
7247 struct video_info video;
7248 char_u string[6];
7249 int row, col;
7250 int button;
7251 int buttons;
7252 static int oldbuttons = 0;
7253
7254#ifdef FEAT_GUI
7255 /* Don't put events in the input queue now. */
7256 if (hold_gui_events)
7257 return;
7258#endif
7259
7260 mouse.operation = MOUSE_GETINFO;
7261 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7262 && ioctl(1, FBIO_MODEINFO, &video) != -1
7263 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7264 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7265 {
7266 row = mouse.u.data.y / video.vi_cheight;
7267 col = mouse.u.data.x / video.vi_cwidth;
7268 buttons = mouse.u.data.buttons;
7269 string[0] = ESC; /* Our termcode */
7270 string[1] = 'M';
7271 string[2] = 'S';
7272 if (oldbuttons == buttons && buttons != 0)
7273 {
7274 button = MOUSE_DRAG;
7275 }
7276 else
7277 {
7278 switch (buttons)
7279 {
7280 case 0:
7281 button = MOUSE_RELEASE;
7282 break;
7283 case 1:
7284 button = MOUSE_LEFT;
7285 break;
7286 case 2:
7287 button = MOUSE_MIDDLE;
7288 break;
7289 case 4:
7290 button = MOUSE_RIGHT;
7291 break;
7292 default:
7293 return;
7294 }
7295 oldbuttons = buttons;
7296 }
7297 string[3] = (char_u)(button);
7298 string[4] = (char_u)(col + ' ' + 1);
7299 string[5] = (char_u)(row + ' ' + 1);
7300 add_to_input_buf(string, 6);
7301 }
7302 return;
7303}
7304#endif /* FEAT_SYSMOUSE */
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007307typedef char_u * (*STRPROCSTR)(char_u *);
7308typedef char_u * (*INTPROCSTR)(int);
7309typedef int (*STRPROCINT)(char_u *);
7310typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312/*
7313 * Call a DLL routine which takes either a string or int param
7314 * and returns an allocated string.
7315 */
7316 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007317mch_libcall(
7318 char_u *libname,
7319 char_u *funcname,
7320 char_u *argstring, /* NULL when using a argint */
7321 int argint,
7322 char_u **string_result,/* NULL when using number_result */
7323 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325# if defined(USE_DLOPEN)
7326 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007327 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328# else
7329 shl_t hinstLib;
7330# endif
7331 STRPROCSTR ProcAdd;
7332 INTPROCSTR ProcAddI;
7333 char_u *retval_str = NULL;
7334 int retval_int = 0;
7335 int success = FALSE;
7336
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007337 /*
7338 * Get a handle to the DLL module.
7339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007341 /* First clear any error, it's not cleared by the dlopen() call. */
7342 (void)dlerror();
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 hinstLib = dlopen((char *)libname, RTLD_LAZY
7345# ifdef RTLD_LOCAL
7346 | RTLD_LOCAL
7347# endif
7348 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007349 if (hinstLib == NULL)
7350 {
7351 /* "dlerr" must be used before dlclose() */
7352 dlerr = (char *)dlerror();
7353 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007354 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356# else
7357 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7358# endif
7359
7360 /* If the handle is valid, try to get the function address. */
7361 if (hinstLib != NULL)
7362 {
7363# ifdef HAVE_SETJMP_H
7364 /*
7365 * Catch a crash when calling the library function. For example when
7366 * using a number where a string pointer is expected.
7367 */
7368 mch_startjmp();
7369 if (SETJMP(lc_jump_env) != 0)
7370 {
7371 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007372# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007373 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 mch_didjmp();
7376 }
7377 else
7378# endif
7379 {
7380 retval_str = NULL;
7381 retval_int = 0;
7382
7383 if (argstring != NULL)
7384 {
7385# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007386 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007387 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388# else
7389 if (shl_findsym(&hinstLib, (const char *)funcname,
7390 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7391 ProcAdd = NULL;
7392# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007393 if ((success = (ProcAdd != NULL
7394# if defined(USE_DLOPEN)
7395 && dlerr == NULL
7396# endif
7397 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 if (string_result == NULL)
7400 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7401 else
7402 retval_str = (ProcAdd)(argstring);
7403 }
7404 }
7405 else
7406 {
7407# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007408 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007409 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410# else
7411 if (shl_findsym(&hinstLib, (const char *)funcname,
7412 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7413 ProcAddI = NULL;
7414# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007415 if ((success = (ProcAddI != NULL
7416# if defined(USE_DLOPEN)
7417 && dlerr == NULL
7418# endif
7419 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
7421 if (string_result == NULL)
7422 retval_int = ((INTPROCINT)ProcAddI)(argint);
7423 else
7424 retval_str = (ProcAddI)(argint);
7425 }
7426 }
7427
7428 /* Save the string before we free the library. */
7429 /* Assume that a "1" or "-1" result is an illegal pointer. */
7430 if (string_result == NULL)
7431 *number_result = retval_int;
7432 else if (retval_str != NULL
7433 && retval_str != (char_u *)1
7434 && retval_str != (char_u *)-1)
7435 *string_result = vim_strsave(retval_str);
7436 }
7437
7438# ifdef HAVE_SETJMP_H
7439 mch_endjmp();
7440# ifdef SIGHASARG
7441 if (lc_signal != 0)
7442 {
7443 int i;
7444
7445 /* try to find the name of this signal */
7446 for (i = 0; signal_info[i].sig != -1; i++)
7447 if (lc_signal == signal_info[i].sig)
7448 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007449 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451# endif
7452# endif
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007455 /* "dlerr" must be used before dlclose() */
7456 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007457 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007458
7459 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 (void)dlclose(hinstLib);
7461# else
7462 (void)shl_unload(hinstLib);
7463# endif
7464 }
7465
7466 if (!success)
7467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007468 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 return FAIL;
7470 }
7471
7472 return OK;
7473}
7474#endif
7475
7476#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7477static int xterm_trace = -1; /* default: disabled */
7478static int xterm_button;
7479
7480/*
7481 * Setup a dummy window for X selections in a terminal.
7482 */
7483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007484setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 int z = 0;
7487 char *strp = "";
7488 Widget AppShell;
7489
7490 if (!x_connect_to_server())
7491 return;
7492
7493 open_app_context();
7494 if (app_context != NULL && xterm_Shell == (Widget)0)
7495 {
7496 int (*oldhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007497# if defined(HAVE_SETJMP_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007499# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007500# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007501 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
7503 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007504 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505# endif
7506
7507 /* Ignore X errors while opening the display */
7508 oldhandler = XSetErrorHandler(x_error_check);
7509
Bram Moolenaaredce7422019-01-20 18:39:30 +01007510# if defined(HAVE_SETJMP_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 /* Ignore X IO errors while opening the display */
7512 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7513 mch_startjmp();
7514 if (SETJMP(lc_jump_env) != 0)
7515 {
7516 mch_didjmp();
7517 xterm_dpy = NULL;
7518 }
7519 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {
7522 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7523 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007524 if (xterm_dpy != NULL)
7525 xterm_dpy_retry_count = 0;
Bram Moolenaaredce7422019-01-20 18:39:30 +01007526# if defined(HAVE_SETJMP_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530
Bram Moolenaaredce7422019-01-20 18:39:30 +01007531# if defined(HAVE_SETJMP_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 /* Now handle X IO errors normally. */
7533 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 /* Now handle X errors normally. */
7536 (void)XSetErrorHandler(oldhandler);
7537
7538 if (xterm_dpy == NULL)
7539 {
7540 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007541 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 return;
7543 }
7544
7545 /* Catch terminating error of the X server connection. */
7546 (void)XSetIOErrorHandler(x_IOerror_handler);
7547
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007548# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007550 {
7551 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007552 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007553 verbose_leave();
7554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555# endif
7556
7557 /* Create a Shell to make converters work. */
7558 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7559 applicationShellWidgetClass, xterm_dpy,
7560 NULL);
7561 if (AppShell == (Widget)0)
7562 return;
7563 xterm_Shell = XtVaCreatePopupShell("VIM",
7564 topLevelShellWidgetClass, AppShell,
7565 XtNmappedWhenManaged, 0,
7566 XtNwidth, 1,
7567 XtNheight, 1,
7568 NULL);
7569 if (xterm_Shell == (Widget)0)
7570 return;
7571
7572 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007573 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (x11_display == NULL)
7575 x11_display = xterm_dpy;
7576
7577 XtRealizeWidget(xterm_Shell);
7578 XSync(xterm_dpy, False);
7579 xterm_update();
7580 }
7581 if (xterm_Shell != (Widget)0)
7582 {
7583 clip_init(TRUE);
7584 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7585 x11_window = (Window)atol(strp);
7586 /* Check if $WINDOWID is valid. */
7587 if (test_x11_window(xterm_dpy) == FAIL)
7588 x11_window = 0;
7589 if (x11_window != 0)
7590 xterm_trace = 0;
7591 }
7592}
7593
7594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007595start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7598 return;
7599 xterm_trace = 1;
7600 xterm_button = button;
7601 do_xterm_trace();
7602}
7603
7604
7605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007606stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 if (xterm_trace < 0)
7609 return;
7610 xterm_trace = 0;
7611}
7612
7613/*
7614 * Query the xterm pointer and generate mouse termcodes if necessary
7615 * return TRUE if dragging is active, else FALSE
7616 */
7617 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007618do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 Window root, child;
7621 int root_x, root_y;
7622 int win_x, win_y;
7623 int row, col;
7624 int_u mask_return;
7625 char_u buf[50];
7626 char_u *strp;
7627 long got_hints;
7628 static char_u *mouse_code;
7629 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7630 static int prev_row = 0, prev_col = 0;
7631 static XSizeHints xterm_hints;
7632
7633 if (xterm_trace <= 0)
7634 return FALSE;
7635
7636 if (xterm_trace == 1)
7637 {
7638 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007639 * have changed recently. */
7640 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7641 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 || xterm_hints.width_inc <= 1
7643 || xterm_hints.height_inc <= 1)
7644 {
7645 xterm_trace = -1; /* Not enough data -- disable tracing */
7646 return FALSE;
7647 }
7648
7649 /* Rely on the same mouse code for the duration of this */
7650 mouse_code = find_termcode(mouse_name);
7651 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007652 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 xterm_trace = 2;
7654
7655 /* Find the offset of the chars, there might be a scrollbar on the
7656 * left of the window and/or a menu on the top (eterm etc.) */
7657 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7658 &win_x, &win_y, &mask_return);
7659 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7660 - (xterm_hints.height_inc / 2);
7661 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7662 xterm_hints.y = 2;
7663 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7664 - (xterm_hints.width_inc / 2);
7665 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7666 xterm_hints.x = 2;
7667 return TRUE;
7668 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007669 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
7671 xterm_trace = 0;
7672 return FALSE;
7673 }
7674
7675 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7676 &win_x, &win_y, &mask_return);
7677
7678 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7679 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7680 if (row == prev_row && col == prev_col)
7681 return TRUE;
7682
7683 STRCPY(buf, mouse_code);
7684 strp = buf + STRLEN(buf);
7685 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7686 *strp++ = (char_u)(col + ' ' + 1);
7687 *strp++ = (char_u)(row + ' ' + 1);
7688 *strp = 0;
7689 add_to_input_buf(buf, STRLEN(buf));
7690
7691 prev_row = row;
7692 prev_col = col;
7693 return TRUE;
7694}
7695
7696# if defined(FEAT_GUI) || defined(PROTO)
7697/*
7698 * Destroy the display, window and app_context. Required for GTK.
7699 */
7700 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007701clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703 if (xterm_Shell != (Widget)0)
7704 {
7705 XtDestroyWidget(xterm_Shell);
7706 xterm_Shell = (Widget)0;
7707 }
7708 if (xterm_dpy != NULL)
7709 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007710# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 /* Lesstif and Solaris crash here, lose some memory */
7712 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 if (x11_display == xterm_dpy)
7715 x11_display = NULL;
7716 xterm_dpy = NULL;
7717 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007718# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (app_context != (XtAppContext)NULL)
7720 {
7721 /* Lesstif and Solaris crash here, lose some memory */
7722 XtDestroyApplicationContext(app_context);
7723 app_context = (XtAppContext)NULL;
7724 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726}
7727# endif
7728
7729/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007730 * Catch up with GUI or X events.
7731 */
7732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007733clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007734{
7735# ifdef FEAT_GUI
7736 if (gui.in_use)
7737 gui_mch_update();
7738 else
7739# endif
7740 if (xterm_Shell != (Widget)0)
7741 xterm_update();
7742}
7743
7744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 * Catch up with any queued X events. This may put keyboard input into the
7746 * input buffer, call resize call-backs, trigger timers etc. If there is
7747 * nothing in the X event queue (& no timers pending), then we return
7748 * immediately.
7749 */
7750 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007751xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 XEvent event;
7754
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007755 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007757 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007759 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007760 break;
7761
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007762 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007763 {
7764 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007765 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007766#ifdef FEAT_CLIENTSERVER
7767 {
7768 XPropertyEvent *e = (XPropertyEvent *)&event;
7769
7770 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007772 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007775 XtDispatchEvent(&event);
7776 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007777 else
7778 {
7779 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007780 XtAppProcessEvent(app_context, mask);
7781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783}
7784
7785 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007786clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 if (xterm_Shell != (Widget)0)
7789 return clip_x11_own_selection(xterm_Shell, cbd);
7790 return FAIL;
7791}
7792
7793 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007794clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 if (xterm_Shell != (Widget)0)
7797 clip_x11_lose_selection(xterm_Shell, cbd);
7798}
7799
7800 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007801clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
7803 if (xterm_Shell != (Widget)0)
7804 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7805}
7806
7807 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007808clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 clip_x11_set_selection(cbd);
7811}
7812#endif
7813
7814
7815#if defined(USE_XSMP) || defined(PROTO)
7816/*
7817 * Code for X Session Management Protocol.
7818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
7820# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821/*
7822 * This is our chance to ask the user if they want to save,
7823 * or abort the logout
7824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007826xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 cmdmod_T save_cmdmod;
7829 int cancel_shutdown = False;
7830
7831 save_cmdmod = cmdmod;
7832 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007833 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 /* Mustn't logout */
7835 cancel_shutdown = True;
7836 cmdmod = save_cmdmod;
7837 setcursor(); /* position cursor */
7838 out_flush();
7839
7840 /* Done interaction */
7841 SmcInteractDone(smc_conn, cancel_shutdown);
7842
7843 /* Finish off
7844 * Only end save-yourself here if we're not cancelling shutdown;
7845 * we'll get a cancelled callback later in which we'll end it.
7846 * Hopefully get around glitchy SMs (like GNOME-1)
7847 */
7848 if (!cancel_shutdown)
7849 {
7850 xsmp.save_yourself = False;
7851 SmcSaveYourselfDone(smc_conn, True);
7852 }
7853}
7854# endif
7855
7856/*
7857 * Callback that starts save-yourself.
7858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007860xsmp_handle_save_yourself(
7861 SmcConn smc_conn,
7862 SmPointer client_data UNUSED,
7863 int save_type UNUSED,
7864 Bool shutdown,
7865 int interact_style UNUSED,
7866 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 /* Handle already being in saveyourself */
7869 if (xsmp.save_yourself)
7870 SmcSaveYourselfDone(smc_conn, True);
7871 xsmp.save_yourself = True;
7872 xsmp.shutdown = shutdown;
7873
7874 /* First up, preserve all files */
7875 out_flush();
7876 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7877
7878 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007879 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7882 /* Now see if we can ask about unsaved files */
7883 if (shutdown && !fast && gui.in_use)
7884 /* Need to interact with user, but need SM's permission */
7885 SmcInteractRequest(smc_conn, SmDialogError,
7886 xsmp_handle_interaction, client_data);
7887 else
7888# endif
7889 {
7890 /* Can stop the cycle here */
7891 SmcSaveYourselfDone(smc_conn, True);
7892 xsmp.save_yourself = False;
7893 }
7894}
7895
7896
7897/*
7898 * Callback to warn us of imminent death.
7899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007901xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 xsmp_close();
7904
7905 /* quit quickly leaving swapfiles for modified buffers behind */
7906 getout_preserve_modified(0);
7907}
7908
7909
7910/*
7911 * Callback to tell us that save-yourself has completed.
7912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007914xsmp_save_complete(
7915 SmcConn smc_conn UNUSED,
7916 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
7918 xsmp.save_yourself = False;
7919}
7920
7921
7922/*
7923 * Callback to tell us that an instigated shutdown was cancelled
7924 * (maybe even by us)
7925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007927xsmp_shutdown_cancelled(
7928 SmcConn smc_conn,
7929 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 if (xsmp.save_yourself)
7932 SmcSaveYourselfDone(smc_conn, True);
7933 xsmp.save_yourself = False;
7934 xsmp.shutdown = False;
7935}
7936
7937
7938/*
7939 * Callback to tell us that a new ICE connection has been established.
7940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007942xsmp_ice_connection(
7943 IceConn iceConn,
7944 IcePointer clientData UNUSED,
7945 Bool opening,
7946 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 /* Intercept creation of ICE connection fd */
7949 if (opening)
7950 {
7951 xsmp_icefd = IceConnectionNumber(iceConn);
7952 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7953 }
7954}
7955
7956
7957/* Handle any ICE processing that's required; return FAIL if SM lost */
7958 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007959xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 Bool rep;
7962
7963 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7964 == IceProcessMessagesIOError)
7965 {
7966 /* Lost ICE */
7967 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007968 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 xsmp_close();
7970 return FAIL;
7971 }
7972 else
7973 return OK;
7974}
7975
7976static int dummy;
7977
7978/* Set up X Session Management Protocol */
7979 void
7980xsmp_init(void)
7981{
7982 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 SmcCallbacks smcallbacks;
7984#if 0
7985 SmPropValue smname;
7986 SmProp smnameprop;
7987 SmProp *smprops[1];
7988#endif
7989
7990 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007991 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
7993 xsmp.save_yourself = xsmp.shutdown = False;
7994
7995 /* Set up SM callbacks - must have all, even if they're not used */
7996 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7997 smcallbacks.save_yourself.client_data = NULL;
7998 smcallbacks.die.callback = xsmp_die;
7999 smcallbacks.die.client_data = NULL;
8000 smcallbacks.save_complete.callback = xsmp_save_complete;
8001 smcallbacks.save_complete.client_data = NULL;
8002 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8003 smcallbacks.shutdown_cancelled.client_data = NULL;
8004
8005 /* Set up a watch on ICE connection creations. The "dummy" argument is
8006 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8007 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8008 {
8009 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008010 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 return;
8012 }
8013
8014 /* Create an SM connection */
8015 xsmp.smcconn = SmcOpenConnection(
8016 NULL,
8017 NULL,
8018 SmProtoMajor,
8019 SmProtoMinor,
8020 SmcSaveYourselfProcMask | SmcDieProcMask
8021 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8022 &smcallbacks,
8023 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008024 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008025 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 errorstring);
8027 if (xsmp.smcconn == NULL)
8028 {
8029 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008032 {
8033 vim_snprintf(errorreport, sizeof(errorreport),
8034 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008035 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 return;
8038 }
8039 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8040
8041#if 0
8042 /* ID ourselves */
8043 smname.value = "vim";
8044 smname.length = 3;
8045 smnameprop.name = "SmProgram";
8046 smnameprop.type = "SmARRAY8";
8047 smnameprop.num_vals = 1;
8048 smnameprop.vals = &smname;
8049
8050 smprops[0] = &smnameprop;
8051 SmcSetProperties(xsmp.smcconn, 1, smprops);
8052#endif
8053}
8054
8055
8056/* Shut down XSMP comms. */
8057 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008058xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 if (xsmp_icefd != -1)
8061 {
8062 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008063 if (xsmp.clientid != NULL)
8064 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008065 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 xsmp_icefd = -1;
8067 }
8068}
8069#endif /* USE_XSMP */
8070
8071
8072#ifdef EBCDIC
8073/* Translate character to its CTRL- value */
8074char CtrlTable[] =
8075{
8076/* 00 - 5E */
8077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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,
8083/* ^ */ 0x1E,
8084/* - */ 0x1F,
8085/* 61 - 6C */
8086 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8087/* _ */ 0x1F,
8088/* 6E - 80 */
8089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8090/* a */ 0x01,
8091/* b */ 0x02,
8092/* c */ 0x03,
8093/* d */ 0x37,
8094/* e */ 0x2D,
8095/* f */ 0x2E,
8096/* g */ 0x2F,
8097/* h */ 0x16,
8098/* i */ 0x05,
8099/* 8A - 90 */
8100 0, 0, 0, 0, 0, 0, 0,
8101/* j */ 0x15,
8102/* k */ 0x0B,
8103/* l */ 0x0C,
8104/* m */ 0x0D,
8105/* n */ 0x0E,
8106/* o */ 0x0F,
8107/* p */ 0x10,
8108/* q */ 0x11,
8109/* r */ 0x12,
8110/* 9A - A1 */
8111 0, 0, 0, 0, 0, 0, 0, 0,
8112/* s */ 0x13,
8113/* t */ 0x3C,
8114/* u */ 0x3D,
8115/* v */ 0x32,
8116/* w */ 0x26,
8117/* x */ 0x18,
8118/* y */ 0x19,
8119/* z */ 0x3F,
8120/* AA - AC */
8121 0, 0, 0,
8122/* [ */ 0x27,
8123/* AE - BC */
8124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8125/* ] */ 0x1D,
8126/* BE - C0 */ 0, 0, 0,
8127/* A */ 0x01,
8128/* B */ 0x02,
8129/* C */ 0x03,
8130/* D */ 0x37,
8131/* E */ 0x2D,
8132/* F */ 0x2E,
8133/* G */ 0x2F,
8134/* H */ 0x16,
8135/* I */ 0x05,
8136/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8137/* J */ 0x15,
8138/* K */ 0x0B,
8139/* L */ 0x0C,
8140/* M */ 0x0D,
8141/* N */ 0x0E,
8142/* O */ 0x0F,
8143/* P */ 0x10,
8144/* Q */ 0x11,
8145/* R */ 0x12,
8146/* DA - DF */ 0, 0, 0, 0, 0, 0,
8147/* \ */ 0x1C,
8148/* E1 */ 0,
8149/* S */ 0x13,
8150/* T */ 0x3C,
8151/* U */ 0x3D,
8152/* V */ 0x32,
8153/* W */ 0x26,
8154/* X */ 0x18,
8155/* Y */ 0x19,
8156/* Z */ 0x3F,
8157/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8159};
8160
8161char MetaCharTable[]=
8162{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8163 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8164 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8165 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8166 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8167};
8168
8169
8170/* TODO: Use characters NOT numbers!!! */
8171char CtrlCharTable[]=
8172{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8173 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8174 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8175 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8176 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8177};
8178
8179
8180#endif