blob: eaf07f101aac6fdd5c7948230e7a71d3bceffa73 [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();
330 smsg((char_u *)"chdir(%s)", path);
331 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
377 ELAPSED_TYPE 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 */
483#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
484 /* 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 Moolenaarb7a7e032018-12-28 17:01:59 +0100983#if defined(HAVE_SETJMP_H) || defined(PROTO)
984// argument to SETJMP()
985static JMP_BUF lc_jump_env;
986
987# ifdef SIGHASARG
988// Caught signal number, 0 when no was signal caught; used for mch_libcall().
989// Volatile because it is used in signal handlers.
990static volatile sig_atomic_t lc_signal;
991# endif
992
993// TRUE when lc_jump_env is valid.
994// Volatile because it is used in signal handler deathtrap().
995static volatile sig_atomic_t lc_active INIT(= FALSE);
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/*
998 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100999 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001001 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Usage:
1003 * mch_startjmp();
1004 * if (SETJMP(lc_jump_env) != 0)
1005 * {
1006 * mch_didjmp();
1007 * EMSG("crash!");
1008 * }
1009 * else
1010 * {
1011 * do_the_work;
1012 * mch_endjmp();
1013 * }
1014 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1015 * not return before the saved environment is used.
1016 * Returns OK for normal return, FAIL when the protected code caused a
1017 * problem and LONGJMP() was used.
1018 */
1019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001020mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022#ifdef SIGHASARG
1023 lc_signal = 0;
1024#endif
1025 lc_active = TRUE;
1026}
1027
1028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001029mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 lc_active = FALSE;
1032}
1033
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001038 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1039 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 init_signal_stack();
1041# endif
1042}
1043#endif
1044
1045/*
1046 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001047 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001049 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1050 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
1052 static RETSIGTYPE
1053deathtrap SIGDEFARG(sigarg)
1054{
1055 static int entered = 0; /* count the number of times we got here.
1056 Note: when memory has been corrupted
1057 this may get an arbitrary value! */
1058#ifdef SIGHASARG
1059 int i;
1060#endif
1061
1062#if defined(HAVE_SETJMP_H)
1063 /*
1064 * Catch a crash in protected code.
1065 * Restores the environment saved in lc_jump_env, which looks like
1066 * SETJMP() returns 1.
1067 */
1068 if (lc_active)
1069 {
1070# if defined(SIGHASARG)
1071 lc_signal = sigarg;
1072# endif
1073 lc_active = FALSE; /* don't jump again */
1074 LONGJMP(lc_jump_env, 1);
1075 /* NOTREACHED */
1076 }
1077#endif
1078
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001079#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001080# ifdef SIGQUIT
1081 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1082 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1083 * pressing CTRL-\, but we don't want Vim to exit then. */
1084 if (in_mch_delay && sigarg == SIGQUIT)
1085 SIGRETURN;
1086# endif
1087
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001088 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1089 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1090 * free(). Calling free() again may then cause a crash. */
1091 if (entered == 0
1092 && (0
1093# ifdef SIGHUP
1094 || sigarg == SIGHUP
1095# endif
1096# ifdef SIGQUIT
1097 || sigarg == SIGQUIT
1098# endif
1099# ifdef SIGTERM
1100 || sigarg == SIGTERM
1101# endif
1102# ifdef SIGPWR
1103 || sigarg == SIGPWR
1104# endif
1105# ifdef SIGUSR1
1106 || sigarg == SIGUSR1
1107# endif
1108# ifdef SIGUSR2
1109 || sigarg == SIGUSR2
1110# endif
1111 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001112 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001113 SIGRETURN;
1114#endif
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /* Remember how often we have been called. */
1117 ++entered;
1118
Bram Moolenaare429e702016-06-10 19:49:14 +02001119 /* Executing autocommands is likely to use more stack space than we have
1120 * available in the signal stack. */
1121 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#ifdef FEAT_EVAL
1124 /* Set the v:dying variable. */
1125 set_vim_var_nr(VV_DYING, (long)entered);
1126#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001127 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /* Since we are now using the signal stack, need to reset the stack
1131 * limit. Otherwise using a regexp will fail. */
1132 get_stack_limit();
1133#endif
1134
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001135#if 0
1136 /* This is for opening gdb the moment Vim crashes.
1137 * You need to manually adjust the file name and Vim executable name.
1138 * Suggested by SungHyun Nam. */
1139 {
1140# define VI_GDB_FILE "/tmp/vimgdb"
1141# define VIM_NAME "/usr/bin/vim"
1142 FILE *fp = fopen(VI_GDB_FILE, "w");
1143 if (fp)
1144 {
1145 fprintf(fp,
1146 "file %s\n"
1147 "attach %d\n"
1148 "set height 1000\n"
1149 "bt full\n"
1150 , VIM_NAME, getpid());
1151 fclose(fp);
1152 system("xterm -e gdb -x "VI_GDB_FILE);
1153 unlink(VI_GDB_FILE);
1154 }
1155 }
1156#endif
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef SIGHASARG
1159 /* try to find the name of this signal */
1160 for (i = 0; signal_info[i].sig != -1; i++)
1161 if (sigarg == signal_info[i].sig)
1162 break;
1163 deadly_signal = sigarg;
1164#endif
1165
1166 full_screen = FALSE; /* don't write message to the GUI, it might be
1167 * part of the problem... */
1168 /*
1169 * If something goes wrong after entering here, we may get here again.
1170 * When this happens, give a message and try to exit nicely (resetting the
1171 * terminal mode, etc.)
1172 * When this happens twice, just exit, don't even try to give a message,
1173 * stack may be corrupt or something weird.
1174 * When this still happens again (or memory was corrupted in such a way
1175 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1176 */
1177 if (entered >= 3)
1178 {
1179 reset_signals(); /* don't catch any signals anymore */
1180 may_core_dump();
1181 if (entered >= 4)
1182 _exit(8);
1183 exit(7);
1184 }
1185 if (entered == 2)
1186 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001187 /* No translation, it may call malloc(). */
1188 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 out_flush();
1190 getout(1);
1191 }
1192
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001193 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001195 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 signal_info[i].name);
1197#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001198 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001200
1201 /* Preserve files and exit. This sets the really_exiting flag to prevent
1202 * calling free(). */
1203 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
Bram Moolenaare429e702016-06-10 19:49:14 +02001205 /* NOTREACHED */
1206
Bram Moolenaar009b2592004-10-24 19:18:58 +00001207#ifdef NBDEBUG
1208 reset_signals();
1209 may_core_dump();
1210 abort();
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 SIGRETURN;
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001217 * Invoked after receiving SIGCONT. We don't know what happened while
1218 * sleeping, deal with part of that.
1219 */
1220 static void
1221after_sigcont(void)
1222{
1223# ifdef FEAT_TITLE
1224 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1225 // again later.
1226 oldtitle_outdated = TRUE;
1227# endif
1228 settmode(TMODE_RAW);
1229 need_check_timestamps = TRUE;
1230 did_check_timestamps = FALSE;
1231}
1232
1233#if defined(SIGCONT)
1234static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001235static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001236
1237/*
1238 * With multi-threading, suspending might not work immediately. Catch the
1239 * SIGCONT signal, which will be used as an indication whether the suspending
1240 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001241 *
1242 * On Linux, signal is not always handled immediately either.
1243 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001244 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001245 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001246 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001248static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001249static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251/*
1252 * signal handler for SIGCONT
1253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 static RETSIGTYPE
1255sigcont_handler SIGDEFARG(sigarg)
1256{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001257 if (in_mch_suspend)
1258 {
1259 sigcont_received = TRUE;
1260 }
1261 else
1262 {
1263 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1264 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1265 // back to a sane mode. We should redraw, but we can't really do that
1266 // in a signal handler, do a redraw later.
1267 after_sigcont();
1268 redraw_later(CLEAR);
1269 cursor_on_force();
1270 out_flush();
1271 }
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 SIGRETURN;
1274}
1275#endif
1276
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001277#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001278# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001279static void *clip_star_save = NULL;
1280static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001281# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001282
1283/*
1284 * Called when Vim is going to sleep or execute a shell command.
1285 * We can't respond to requests for the X selections. Lose them, otherwise
1286 * other applications will hang. But first copy the text to cut buffer 0.
1287 */
1288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001290{
1291 if (clip_star.owned || clip_plus.owned)
1292 {
1293 x11_export_final_selection();
1294 if (clip_star.owned)
1295 clip_lose_selection(&clip_star);
1296 if (clip_plus.owned)
1297 clip_lose_selection(&clip_plus);
1298 if (x11_display != NULL)
1299 XFlush(x11_display);
1300 }
1301}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001302
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001303# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001304/*
1305 * Save clipboard text to restore later.
1306 */
1307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309{
1310 if (clip_star.owned)
1311 clip_star_save = get_register('*', TRUE);
1312 if (clip_plus.owned)
1313 clip_plus_save = get_register('+', TRUE);
1314}
1315
1316/*
1317 * Restore clipboard text if no one own the X selection.
1318 */
1319 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001320restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001321{
1322 if (clip_star_save != NULL)
1323 {
1324 if (!clip_gen_owner_exists(&clip_star))
1325 put_register('*', clip_star_save);
1326 else
1327 free_register(clip_star_save);
1328 clip_star_save = NULL;
1329 }
1330 if (clip_plus_save != NULL)
1331 {
1332 if (!clip_gen_owner_exists(&clip_plus))
1333 put_register('+', clip_plus_save);
1334 else
1335 free_register(clip_plus_save);
1336 clip_plus_save = NULL;
1337 }
1338}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001339# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001340#endif
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342/*
1343 * If the machine has job control, use it to suspend the program,
1344 * otherwise fake it by starting a new shell.
1345 */
1346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001347mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 /* BeOS does have SIGTSTP, but it doesn't work. */
1350#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001351 in_mch_suspend = TRUE;
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 out_flush(); /* needed to make cursor visible on some systems */
1354 settmode(TMODE_COOK);
1355 out_flush(); /* needed to disable mouse on some systems */
1356
1357# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001358 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001360# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 sigcont_received = FALSE;
1362# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001365
1366# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001367 /*
1368 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001369 * immediately, but somehow not all the time, probably because it's handled
1370 * in another thread. Do not call pause() because there would be race
1371 * condition which would hang Vim if signal happened in between the test of
1372 * sigcont_received and the call to pause(). If signal is not yet received,
1373 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1374 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001375 */
1376 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001377 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001378
Bram Moolenaar262735e2009-07-14 10:20:22 +00001379 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001380 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
Bram Moolenaar2e310482018-08-21 13:09:10 +02001385 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#else
1387 suspend_shell();
1388#endif
1389}
1390
1391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001392mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 Columns = 80;
1395 Rows = 24;
1396
1397 out_flush();
1398 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001399
Bram Moolenaar56718732006-03-15 22:53:57 +00001400#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001401 mac_conv_init();
1402#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001403#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1404 win_clip_init();
1405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001409set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411#if defined(SIGWINCH)
1412 /*
1413 * WINDOW CHANGE signal is handled with sig_winch().
1414 */
1415 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1416#endif
1417
1418 /*
1419 * We want the STOP signal to work, to make mch_suspend() work.
1420 * For "rvim" the STOP signal is ignored.
1421 */
1422#ifdef SIGTSTP
1423 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1424#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001425#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 signal(SIGCONT, sigcont_handler);
1427#endif
1428
1429 /*
1430 * We want to ignore breaking of PIPEs.
1431 */
1432#ifdef SIGPIPE
1433 signal(SIGPIPE, SIG_IGN);
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001437 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438#endif
1439
1440 /*
1441 * Ignore alarm signals (Perl's alarm() generates it).
1442 */
1443#ifdef SIGALRM
1444 signal(SIGALRM, SIG_IGN);
1445#endif
1446
1447 /*
1448 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1449 * work will be lost.
1450 */
1451#ifdef SIGPWR
1452 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1453#endif
1454
1455 /*
1456 * Arrange for other signals to gracefully shutdown Vim.
1457 */
1458 catch_signals(deathtrap, SIG_ERR);
1459
1460#if defined(FEAT_GUI) && defined(SIGHUP)
1461 /*
1462 * When the GUI is running, ignore the hangup signal.
1463 */
1464 if (gui.in_use)
1465 signal(SIGHUP, SIG_IGN);
1466#endif
1467}
1468
Bram Moolenaardf177f62005-02-22 08:39:57 +00001469#if defined(SIGINT) || defined(PROTO)
1470/*
1471 * Catch CTRL-C (only works while in Cooked mode).
1472 */
1473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001474catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001475{
1476 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1477}
1478#endif
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001481reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001484#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 /* SIGCONT isn't in the list, because its default action is ignore */
1486 signal(SIGCONT, SIG_DFL);
1487#endif
1488}
1489
1490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001491catch_signals(
1492 RETSIGTYPE (*func_deadly)(),
1493 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 int i;
1496
1497 for (i = 0; signal_info[i].sig != -1; i++)
1498 if (signal_info[i].deadly)
1499 {
1500#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1501 struct sigaction sa;
1502
1503 /* Setup to use the alternate stack for the signal function. */
1504 sa.sa_handler = func_deadly;
1505 sigemptyset(&sa.sa_mask);
1506# if defined(__linux__) && defined(_REENTRANT)
1507 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1508 * thread handling in combination with using the alternate stack:
1509 * pthread library functions try to use the stack pointer to
1510 * identify the current thread, causing a SEGV signal, which
1511 * recursively calls deathtrap() and hangs. */
1512 sa.sa_flags = 0;
1513# else
1514 sa.sa_flags = SA_ONSTACK;
1515# endif
1516 sigaction(signal_info[i].sig, &sa, NULL);
1517#else
1518# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1519 struct sigvec sv;
1520
1521 /* Setup to use the alternate stack for the signal function. */
1522 sv.sv_handler = func_deadly;
1523 sv.sv_mask = 0;
1524 sv.sv_flags = SV_ONSTACK;
1525 sigvec(signal_info[i].sig, &sv, NULL);
1526# else
1527 signal(signal_info[i].sig, func_deadly);
1528# endif
1529#endif
1530 }
1531 else if (func_other != SIG_ERR)
1532 signal(signal_info[i].sig, func_other);
1533}
1534
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001535#ifdef HAVE_SIGPROCMASK
1536 static void
1537block_signals(sigset_t *set)
1538{
1539 sigset_t newset;
1540 int i;
1541
1542 sigemptyset(&newset);
1543
1544 for (i = 0; signal_info[i].sig != -1; i++)
1545 sigaddset(&newset, signal_info[i].sig);
1546
Bram Moolenaar2e310482018-08-21 13:09:10 +02001547# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001548 /* SIGCONT isn't in the list, because its default action is ignore */
1549 sigaddset(&newset, SIGCONT);
1550# endif
1551
1552 sigprocmask(SIG_BLOCK, &newset, set);
1553}
1554
1555 static void
1556unblock_signals(sigset_t *set)
1557{
1558 sigprocmask(SIG_SETMASK, set, NULL);
1559}
1560#endif
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001564 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1565 * return TRUE
1566 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1567 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001568 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 * Returns TRUE when Vim should exit.
1570 */
1571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001572vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001574 static int got_signal = 0;
1575 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001576
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001577 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001579 case SIGNAL_BLOCK: blocked = TRUE;
1580 break;
1581
1582 case SIGNAL_UNBLOCK: blocked = FALSE;
1583 if (got_signal != 0)
1584 {
1585 kill(getpid(), got_signal);
1586 got_signal = 0;
1587 }
1588 break;
1589
1590 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001591 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001592 got_signal = sig;
1593#ifdef SIGPWR
1594 if (sig != SIGPWR)
1595#endif
1596 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001597 break;
1598 }
1599 return FALSE;
1600}
1601
1602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * Check_win checks whether we have an interactive stdout.
1604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001606mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (isatty(1))
1609 return OK;
1610 return FAIL;
1611}
1612
1613/*
1614 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1615 */
1616 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001617mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 if (isatty(read_cmd_fd))
1620 return TRUE;
1621 return FALSE;
1622}
1623
1624#ifdef FEAT_X11
1625
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001626# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629/*
1630 * Give a message about the elapsed time for opening the X window.
1631 */
1632 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001633xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001635 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
1637# endif
1638#endif
1639
1640#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1641/*
1642 * A few functions shared by X11 title and clipboard code.
1643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645static int got_x_error = FALSE;
1646
1647/*
1648 * X Error handler, otherwise X just exits! (very rude) -- webb
1649 */
1650 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001651x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001653 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 STRCAT(IObuff, _("\nVim: Got X error\n"));
1655
1656 /* We cannot print a message and continue, because no X calls are allowed
1657 * here (causes my system to hang). Silently continuing might be an
1658 * alternative... */
1659 preserve_exit(); /* preserve files and exit */
1660
1661 return 0; /* NOTREACHED */
1662}
1663
1664/*
1665 * Another X Error handler, just used to check for errors.
1666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001668x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 got_x_error = TRUE;
1671 return 0;
1672}
1673
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001674/*
1675 * Return TRUE when connection to the X server is desired.
1676 */
1677 static int
1678x_connect_to_server(void)
1679{
1680 // No point in connecting if we are exiting or dying.
1681 if (exiting || v_dying)
1682 return FALSE;
1683
1684#if defined(FEAT_CLIENTSERVER)
1685 if (x_force_connect)
1686 return TRUE;
1687#endif
1688 if (x_no_connect)
1689 return FALSE;
1690
1691 /* Check for a match with "exclude:" from 'clipboard'. */
1692 if (clip_exclude_prog != NULL)
1693 {
1694 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1695 return FALSE;
1696 }
1697 return TRUE;
1698}
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1701# if defined(HAVE_SETJMP_H)
1702/*
1703 * An X IO Error handler, used to catch error while opening the display.
1704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001706x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 /* This function should not return, it causes exit(). Longjump instead. */
1709 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001710# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001711 return 0; /* avoid the compiler complains about missing return value */
1712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713}
1714# endif
1715
1716/*
1717 * An X IO Error handler, used to catch terminal errors.
1718 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001722x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001725 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 x11_window = 0;
1727 x11_display = NULL;
1728 xterm_Shell = (Widget)0;
1729
1730 /* This function should not return, it causes exit(). Longjump instead. */
1731 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001732# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001733 return 0; /* avoid the compiler complains about missing return value */
1734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001736
1737/*
1738 * If the X11 connection was lost try to restore it.
1739 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001740 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001741 */
1742 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001743may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001744{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001745 // No point in restoring the connecting if we are exiting or dying.
1746 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001747 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001748 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001749
1750# ifndef LESSTIF_VERSION
1751 /* This has been reported to avoid Vim getting stuck. */
1752 if (app_context != (XtAppContext)NULL)
1753 {
1754 XtDestroyApplicationContext(app_context);
1755 app_context = (XtAppContext)NULL;
1756 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1757 }
1758# endif
1759
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001760 setup_term_clip();
1761 get_x11_title(FALSE);
1762 }
1763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
1765
1766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 * Test if "dpy" and x11_window are valid by getting the window title.
1768 * I don't actually want it yet, so there may be a simpler call to use, but
1769 * this will cause the error handler x_error_check() to be called if anything
1770 * is wrong, such as the window pointer being invalid (as can happen when the
1771 * user changes his DISPLAY, but not his WINDOWID) -- webb
1772 */
1773 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001774test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 int (*old_handler)();
1777 XTextProperty text_prop;
1778
1779 old_handler = XSetErrorHandler(x_error_check);
1780 got_x_error = FALSE;
1781 if (XGetWMName(dpy, x11_window, &text_prop))
1782 XFree((void *)text_prop.value);
1783 XSync(dpy, False);
1784 (void)XSetErrorHandler(old_handler);
1785
1786 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001787 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 return (got_x_error ? FAIL : OK);
1790}
1791#endif
1792
1793#ifdef FEAT_TITLE
1794
1795#ifdef FEAT_X11
1796
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001797static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799/*
1800 * try to get x11 window and display
1801 *
1802 * return FAIL for failure, OK otherwise
1803 */
1804 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001805get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
1807 char *winid;
1808 static int result = -1;
1809#define XD_NONE 0 /* x11_display not set here */
1810#define XD_HERE 1 /* x11_display opened here */
1811#define XD_GUI 2 /* x11_display used from gui.dpy */
1812#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1813 static int x11_display_from = XD_NONE;
1814 static int did_set_error_handler = FALSE;
1815
1816 if (!did_set_error_handler)
1817 {
1818 /* X just exits if it finds an error otherwise! */
1819 (void)XSetErrorHandler(x_error_handler);
1820 did_set_error_handler = TRUE;
1821 }
1822
Bram Moolenaar9372a112005-12-06 19:59:18 +00001823#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (gui.in_use)
1825 {
1826 /*
1827 * If the X11 display was opened here before, for the window where Vim
1828 * was started, close that one now to avoid a memory leak.
1829 */
1830 if (x11_display_from == XD_HERE && x11_display != NULL)
1831 {
1832 XCloseDisplay(x11_display);
1833 x11_display_from = XD_NONE;
1834 }
1835 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1836 {
1837 x11_display_from = XD_GUI;
1838 return OK;
1839 }
1840 x11_display = NULL;
1841 return FAIL;
1842 }
1843 else if (x11_display_from == XD_GUI)
1844 {
1845 /* GUI must have stopped somehow, clear x11_display */
1846 x11_window = 0;
1847 x11_display = NULL;
1848 x11_display_from = XD_NONE;
1849 }
1850#endif
1851
1852 /* When started with the "-X" argument, don't try connecting. */
1853 if (!x_connect_to_server())
1854 return FAIL;
1855
1856 /*
1857 * If WINDOWID not set, should try another method to find out
1858 * what the current window number is. The only code I know for
1859 * this is very complicated.
1860 * We assume that zero is invalid for WINDOWID.
1861 */
1862 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1863 x11_window = (Window)atol(winid);
1864
1865#ifdef FEAT_XCLIPBOARD
1866 if (xterm_dpy != NULL && x11_window != 0)
1867 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001868 /* We may have checked it already, but Gnome terminal can move us to
1869 * another window, so we need to check every time. */
1870 if (x11_display_from != XD_XTERM)
1871 {
1872 /*
1873 * If the X11 display was opened here before, for the window where
1874 * Vim was started, close that one now to avoid a memory leak.
1875 */
1876 if (x11_display_from == XD_HERE && x11_display != NULL)
1877 XCloseDisplay(x11_display);
1878 x11_display = xterm_dpy;
1879 x11_display_from = XD_XTERM;
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (test_x11_window(x11_display) == FAIL)
1882 {
1883 /* probably bad $WINDOWID */
1884 x11_window = 0;
1885 x11_display = NULL;
1886 x11_display_from = XD_NONE;
1887 return FAIL;
1888 }
1889 return OK;
1890 }
1891#endif
1892
1893 if (x11_window == 0 || x11_display == NULL)
1894 result = -1;
1895
1896 if (result != -1) /* Have already been here and set this */
1897 return result; /* Don't do all these X calls again */
1898
1899 if (x11_window != 0 && x11_display == NULL)
1900 {
1901#ifdef SET_SIG_ALARM
1902 RETSIGTYPE (*sig_save)();
1903#endif
1904#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1905 struct timeval start_tv;
1906
1907 if (p_verbose > 0)
1908 gettimeofday(&start_tv, NULL);
1909#endif
1910
1911#ifdef SET_SIG_ALARM
1912 /*
1913 * Opening the Display may hang if the DISPLAY setting is wrong, or
1914 * the network connection is bad. Set an alarm timer to get out.
1915 */
1916 sig_alarm_called = FALSE;
1917 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1918 (RETSIGTYPE (*)())sig_alarm);
1919 alarm(2);
1920#endif
1921 x11_display = XOpenDisplay(NULL);
1922
1923#ifdef SET_SIG_ALARM
1924 alarm(0);
1925 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1926 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001927 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#endif
1929 if (x11_display != NULL)
1930 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001931# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001933 {
1934 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001935 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001936 verbose_leave();
1937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938# endif
1939 if (test_x11_window(x11_display) == FAIL)
1940 {
1941 /* Maybe window id is bad */
1942 x11_window = 0;
1943 XCloseDisplay(x11_display);
1944 x11_display = NULL;
1945 }
1946 else
1947 x11_display_from = XD_HERE;
1948 }
1949 }
1950 if (x11_window == 0 || x11_display == NULL)
1951 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001952
1953# ifdef FEAT_EVAL
1954 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1955# endif
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 return (result = OK);
1958}
1959
1960/*
1961 * Determine original x11 Window Title
1962 */
1963 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001964get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001966 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967}
1968
1969/*
1970 * Determine original x11 Window icon
1971 */
1972 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001973get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 int retval = FALSE;
1976
1977 retval = get_x11_thing(FALSE, test_only);
1978
1979 /* could not get old icon, use terminal name */
1980 if (oldicon == NULL && !test_only)
1981 {
1982 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001983 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001985 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987
1988 return retval;
1989}
1990
1991 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001992get_x11_thing(
1993 int get_title, /* get title string */
1994 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 XTextProperty text_prop;
1997 int retval = FALSE;
1998 Status status;
1999
2000 if (get_x11_windis() == OK)
2001 {
2002 /* Get window/icon name if any */
2003 if (get_title)
2004 status = XGetWMName(x11_display, x11_window, &text_prop);
2005 else
2006 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2007
2008 /*
2009 * If terminal is xterm, then x11_window may be a child window of the
2010 * outer xterm window that actually contains the window/icon name, so
2011 * keep traversing up the tree until a window with a title/icon is
2012 * found.
2013 */
2014 /* Previously this was only done for xterm and alikes. I don't see a
2015 * reason why it would fail for other terminal emulators.
2016 * if (term_is_xterm) */
2017 {
2018 Window root;
2019 Window parent;
2020 Window win = x11_window;
2021 Window *children;
2022 unsigned int num_children;
2023
2024 while (!status || text_prop.value == NULL)
2025 {
2026 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2027 &num_children))
2028 break;
2029 if (children)
2030 XFree((void *)children);
2031 if (parent == root || parent == 0)
2032 break;
2033
2034 win = parent;
2035 if (get_title)
2036 status = XGetWMName(x11_display, win, &text_prop);
2037 else
2038 status = XGetWMIconName(x11_display, win, &text_prop);
2039 }
2040 }
2041 if (status && text_prop.value != NULL)
2042 {
2043 retval = TRUE;
2044 if (!test_only)
2045 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002046#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2047 if (text_prop.encoding == XA_STRING
2048# ifdef FEAT_MBYTE
2049 && !has_mbyte
2050# endif
2051 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053#endif
2054 if (get_title)
2055 oldtitle = vim_strsave((char_u *)text_prop.value);
2056 else
2057 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002058#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060 else
2061 {
2062 char **cl;
2063 Status transform_status;
2064 int n = 0;
2065
2066 transform_status = XmbTextPropertyToTextList(x11_display,
2067 &text_prop,
2068 &cl, &n);
2069 if (transform_status >= Success && n > 0 && cl[0])
2070 {
2071 if (get_title)
2072 oldtitle = vim_strsave((char_u *) cl[0]);
2073 else
2074 oldicon = vim_strsave((char_u *) cl[0]);
2075 XFreeStringList(cl);
2076 }
2077 else
2078 {
2079 if (get_title)
2080 oldtitle = vim_strsave((char_u *)text_prop.value);
2081 else
2082 oldicon = vim_strsave((char_u *)text_prop.value);
2083 }
2084 }
2085#endif
2086 }
2087 XFree((void *)text_prop.value);
2088 }
2089 }
2090 return retval;
2091}
2092
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002093/* Xutf8 functions are not avaialble on older systems. Note that on some
2094 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2095 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2096 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002098# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099# define USE_UTF8_STRING
2100# endif
2101#endif
2102
2103/*
2104 * Set x11 Window Title
2105 *
2106 * get_x11_windis() must be called before this and have returned OK
2107 */
2108 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002109set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2112 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2113 * supported everywhere and STRING doesn't work for multi-byte titles.
2114 */
2115#ifdef USE_UTF8_STRING
2116 if (enc_utf8)
2117 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2118 NULL, NULL, 0, NULL, NULL, NULL);
2119 else
2120#endif
2121 {
2122#if XtSpecificationRelease >= 4
2123# ifdef FEAT_XFONTSET
2124 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2125 NULL, NULL, 0, NULL, NULL, NULL);
2126# else
2127 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002128 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002131 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 XSetWMProperties(x11_display, x11_window, &text_prop,
2133 NULL, NULL, 0, NULL, NULL, NULL);
2134# endif
2135#else
2136 XStoreName(x11_display, x11_window, (char *)title);
2137#endif
2138 }
2139 XFlush(x11_display);
2140}
2141
2142/*
2143 * Set x11 Window icon
2144 *
2145 * get_x11_windis() must be called before this and have returned OK
2146 */
2147 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002148set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 /* See above for comments about using X*SetWMProperties(). */
2151#ifdef USE_UTF8_STRING
2152 if (enc_utf8)
2153 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2154 NULL, 0, NULL, NULL, NULL);
2155 else
2156#endif
2157 {
2158#if XtSpecificationRelease >= 4
2159# ifdef FEAT_XFONTSET
2160 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2161 NULL, 0, NULL, NULL, NULL);
2162# else
2163 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002164 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002166 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2168 NULL, 0, NULL, NULL, NULL);
2169# endif
2170#else
2171 XSetIconName(x11_display, x11_window, (char *)icon);
2172#endif
2173 }
2174 XFlush(x11_display);
2175}
2176
2177#else /* FEAT_X11 */
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002180get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 return FALSE;
2183}
2184
2185 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002186get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188 if (!test_only)
2189 {
2190 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002191 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002193 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
2195 return FALSE;
2196}
2197
2198#endif /* FEAT_X11 */
2199
2200 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002201mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 return get_x11_title(TRUE);
2204}
2205
2206 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002207mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 return get_x11_icon(TRUE);
2210}
2211
2212/*
2213 * Set the window title and icon.
2214 */
2215 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002216mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 int type = 0;
2219 static int recursive = 0;
2220
2221 if (T_NAME == NULL) /* no terminal name (yet) */
2222 return;
2223 if (title == NULL && icon == NULL) /* nothing to do */
2224 return;
2225
2226 /* When one of the X11 functions causes a deadly signal, we get here again
2227 * recursively. Avoid hanging then (something is probably locked). */
2228 if (recursive)
2229 return;
2230 ++recursive;
2231
2232 /*
2233 * if the window ID and the display is known, we may use X11 calls
2234 */
2235#ifdef FEAT_X11
2236 if (get_x11_windis() == OK)
2237 type = 1;
2238#else
2239# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2240 if (gui.in_use)
2241 type = 1;
2242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#endif
2244
2245 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002246 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * than x11 calls, because the x11 calls don't always work
2248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if ((type || *T_TS != NUL) && title != NULL)
2250 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002251 if (oldtitle_outdated)
2252 {
2253 oldtitle_outdated = FALSE;
2254 VIM_CLEAR(oldtitle);
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (oldtitle == NULL
2257#ifdef FEAT_GUI
2258 && !gui.in_use
2259#endif
2260 ) /* first call but not in GUI, save title */
2261 (void)get_x11_title(FALSE);
2262
2263 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2264 term_settitle(title);
2265#ifdef FEAT_X11
2266 else
2267# ifdef FEAT_GUI_GTK
2268 if (!gui.in_use) /* don't do this if GTK+ is running */
2269# endif
2270 set_x11_title(title); /* x11 */
2271#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002272#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2274 else
2275 gui_mch_settitle(title, icon);
2276#endif
2277 did_set_title = TRUE;
2278 }
2279
2280 if ((type || *T_CIS != NUL) && icon != NULL)
2281 {
2282 if (oldicon == NULL
2283#ifdef FEAT_GUI
2284 && !gui.in_use
2285#endif
2286 ) /* first call, save icon */
2287 get_x11_icon(FALSE);
2288
2289 if (*T_CIS != NUL)
2290 {
2291 out_str(T_CIS); /* set icon start */
2292 out_str_nf(icon);
2293 out_str(T_CIE); /* set icon end */
2294 out_flush();
2295 }
2296#ifdef FEAT_X11
2297 else
2298# ifdef FEAT_GUI_GTK
2299 if (!gui.in_use) /* don't do this if GTK+ is running */
2300# endif
2301 set_x11_icon(icon); /* x11 */
2302#endif
2303 did_set_icon = TRUE;
2304 }
2305 --recursive;
2306}
2307
2308/*
2309 * Restore the window/icon title.
2310 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002311 * SAVE_RESTORE_TITLE only restore title
2312 * SAVE_RESTORE_ICON only restore icon
2313 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
2315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002316mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002319 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002321 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2322
2323 // pop and push from/to the stack
2324 term_pop_title(which);
2325 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326}
2327
2328#endif /* FEAT_TITLE */
2329
2330/*
2331 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002332 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 */
2334 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002335vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 if (name == NULL)
2338 return FALSE;
2339 return (STRNICMP(name, "xterm", 5) == 0
2340 || STRNICMP(name, "nxterm", 6) == 0
2341 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002342 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002344 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 || STRCMP(name, "builtin_xterm") == 0);
2346}
2347
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002348#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2349/*
2350 * Return TRUE if "name" appears to be that of a terminal
2351 * known to support the xterm-style mouse protocol.
2352 * Relies on term_is_xterm having been set to its correct value.
2353 */
2354 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002355use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002356{
2357 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002358 && (term_is_xterm
2359 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002360 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002361 || STRICMP(name, "st") == 0
2362 || STRNICMP(name, "st-", 3) == 0
2363 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002364}
2365#endif
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2368/*
2369 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2370 * Return 1 for "xterm".
2371 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002372 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002373 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 */
2375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002376use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002378 if (ttym_flags == TTYM_SGR)
2379 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002380 if (ttym_flags == TTYM_URXVT)
2381 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (ttym_flags == TTYM_XTERM2)
2383 return 2;
2384 if (ttym_flags == TTYM_XTERM)
2385 return 1;
2386 return 0;
2387}
2388#endif
2389
2390 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002391vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 if (name == NULL)
2394 return FALSE;
2395 return (STRNICMP(name, "iris-ansi", 9) == 0
2396 || STRCMP(name, "builtin_iris-ansi") == 0);
2397}
2398
2399 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002400vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 if (name == NULL)
2403 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002404 /* catch VT100 - VT5xx */
2405 return ((STRNICMP(name, "vt", 2) == 0
2406 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 || STRCMP(name, "builtin_vt320") == 0);
2408}
2409
2410/*
2411 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2412 * This should include all windowed terminal emulators.
2413 */
2414 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002415vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417 if (name == NULL)
2418 return FALSE;
2419 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2420 return TRUE;
2421 return ( STRNICMP(name, "hpterm", 6) == 0
2422 || STRNICMP(name, "sun-cmd", 7) == 0
2423 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002424 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 || STRNICMP(name, "dtterm", 6) == 0);
2426}
2427
2428/*
2429 * Insert user name in s[len].
2430 * Return OK if a name found.
2431 */
2432 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002433mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434{
2435#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002436 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 return OK;
2438#else
2439 return mch_get_uname(getuid(), s, len);
2440#endif
2441}
2442
2443/*
2444 * Insert user name for "uid" in s[len].
2445 * Return OK if a name found.
2446 */
2447 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002448mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2451 struct passwd *pw;
2452
2453 if ((pw = getpwuid(uid)) != NULL
2454 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2455 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002456 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 return OK;
2458 }
2459#endif
2460 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2461 return FAIL; /* a number is not a name */
2462}
2463
2464/*
2465 * Insert host name is s[len].
2466 */
2467
2468#ifdef HAVE_SYS_UTSNAME_H
2469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002470mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 struct utsname vutsname;
2473
2474 if (uname(&vutsname) < 0)
2475 *s = NUL;
2476 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002477 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478}
2479#else /* HAVE_SYS_UTSNAME_H */
2480
2481# ifdef HAVE_SYS_SYSTEMINFO_H
2482# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2483# endif
2484
2485 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002486mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488# ifdef VAXC
2489 vaxc$gethostname((char *)s, len);
2490# else
2491 gethostname((char *)s, len);
2492# endif
2493 s[len - 1] = NUL; /* make sure it's terminated */
2494}
2495#endif /* HAVE_SYS_UTSNAME_H */
2496
2497/*
2498 * return process ID
2499 */
2500 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002501mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 return (long)getpid();
2504}
2505
2506#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002508strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 extern int sys_nerr;
2511 extern char *sys_errlist[];
2512 static char er[20];
2513
2514 if (err > 0 && err < sys_nerr)
2515 return (sys_errlist[err]);
2516 sprintf(er, "Error %d", err);
2517 return er;
2518}
2519#endif
2520
2521/*
2522 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2523 * Return OK for success, FAIL for failure.
2524 */
2525 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002526mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528#if defined(USE_GETCWD)
2529 if (getcwd((char *)buf, len) == NULL)
2530 {
2531 STRCPY(buf, strerror(errno));
2532 return FAIL;
2533 }
2534 return OK;
2535#else
2536 return (getwd((char *)buf) != NULL ? OK : FAIL);
2537#endif
2538}
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002541 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 *
2543 * return FAIL for failure, OK for success
2544 */
2545 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002546mch_FullName(
2547 char_u *fname,
2548 char_u *buf,
2549 int len,
2550 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002553#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 int fd = -1;
2555 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 char_u olddir[MAXPATHL];
2558 char_u *p;
2559 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002560#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002561 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2562 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002563#endif
2564
Bram Moolenaar38323e42007-03-06 19:22:53 +00002565#ifdef VMS
2566 fname = vms_fixfilename(fname);
2567#endif
2568
Bram Moolenaara2442432007-04-26 14:26:37 +00002569#ifdef __CYGWIN__
2570 /*
2571 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2572 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002573# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002574 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2575 * a forward slash. */
2576 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2577 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002578# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002579 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002580# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002581 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002582#endif
2583
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002584 /* Expand it if forced or not an absolute path.
2585 * Do not do it for "/file", the result is always "/". */
2586 if ((force || !mch_isFullName(fname))
2587 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 /*
2590 * If the file name has a path, change to that directory for a moment,
2591 * and then do the getwd() (and get back to where we were).
2592 * This will get the correct path name with "../" things.
2593 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002594 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002596#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 /*
2598 * Use fchdir() if possible, it's said to be faster and more
2599 * reliable. But on SunOS 4 it might not work. Check this by
2600 * doing a fchdir() right now.
2601 */
2602 if (!dont_fchdir)
2603 {
2604 fd = open(".", O_RDONLY | O_EXTRA, 0);
2605 if (fd >= 0 && fchdir(fd) < 0)
2606 {
2607 close(fd);
2608 fd = -1;
2609 dont_fchdir = TRUE; /* don't try again */
2610 }
2611 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 /* Only change directory when we are sure we can return to where
2615 * we are now. After doing "su" chdir(".") might not work. */
2616 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002617#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 (mch_dirname(olddir, MAXPATHL) == FAIL
2621 || mch_chdir((char *)olddir) != 0))
2622 {
2623 p = NULL; /* can't get current dir: don't chdir */
2624 retval = FAIL;
2625 }
2626 else
2627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /* The directory is copied into buf[], to be able to remove
2629 * the file name without changing it (could be a string in
2630 * read-only memory) */
2631 if (p - fname >= len)
2632 retval = FAIL;
2633 else
2634 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002635 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (mch_chdir((char *)buf))
2637 retval = FAIL;
2638 else
2639 fname = p + 1;
2640 *buf = NUL;
2641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 }
2644 if (mch_dirname(buf, len) == FAIL)
2645 {
2646 retval = FAIL;
2647 *buf = NUL;
2648 }
2649 if (p != NULL)
2650 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002651#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (fd >= 0)
2653 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002654 if (p_verbose >= 5)
2655 {
2656 verbose_enter();
2657 MSG("fchdir() to previous dir");
2658 verbose_leave();
2659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 l = fchdir(fd);
2661 close(fd);
2662 }
2663 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 l = mch_chdir((char *)olddir);
2666 if (l != 0)
2667 EMSG(_(e_prev_dir));
2668 }
2669
2670 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002671 if (l >= len - 1)
2672 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002673#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002674 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002676 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002681 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return FAIL;
2683
2684 /* Do not append ".", "/dir/." is equal to "/dir". */
2685 if (STRCMP(fname, ".") != 0)
2686 STRCAT(buf, fname);
2687
2688 return OK;
2689}
2690
2691/*
2692 * Return TRUE if "fname" does not depend on the current directory.
2693 */
2694 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002695mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002697#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 return ( fname[0] == '/' || fname[0] == '.' ||
2699 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2700 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2701 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002702#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#endif
2705}
2706
Bram Moolenaar24552be2005-12-10 20:17:30 +00002707#if defined(USE_FNAME_CASE) || defined(PROTO)
2708/*
2709 * Set the case of the file name, if it already exists. This will cause the
2710 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002711 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002712 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002713 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002714fname_case(
2715 char_u *name,
2716 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002717{
2718 struct stat st;
2719 char_u *slash, *tail;
2720 DIR *dirp;
2721 struct dirent *dp;
2722
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002723 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002724 {
2725 /* Open the directory where the file is located. */
2726 slash = vim_strrchr(name, '/');
2727 if (slash == NULL)
2728 {
2729 dirp = opendir(".");
2730 tail = name;
2731 }
2732 else
2733 {
2734 *slash = NUL;
2735 dirp = opendir((char *)name);
2736 *slash = '/';
2737 tail = slash + 1;
2738 }
2739
2740 if (dirp != NULL)
2741 {
2742 while ((dp = readdir(dirp)) != NULL)
2743 {
2744 /* Only accept names that differ in case and are the same byte
2745 * length. TODO: accept different length name. */
2746 if (STRICMP(tail, dp->d_name) == 0
2747 && STRLEN(tail) == STRLEN(dp->d_name))
2748 {
2749 char_u newname[MAXPATHL + 1];
2750 struct stat st2;
2751
2752 /* Verify the inode is equal. */
2753 vim_strncpy(newname, name, MAXPATHL);
2754 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2755 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002756 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002757 && st.st_ino == st2.st_ino
2758 && st.st_dev == st2.st_dev)
2759 {
2760 STRCPY(tail, dp->d_name);
2761 break;
2762 }
2763 }
2764 }
2765
2766 closedir(dirp);
2767 }
2768 }
2769}
2770#endif
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772/*
2773 * Get file permissions for 'name'.
2774 * Returns -1 when it doesn't exist.
2775 */
2776 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002777mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 struct stat statb;
2780
2781 /* Keep the #ifdef outside of stat(), it may be a macro. */
2782#ifdef VMS
2783 if (stat((char *)vms_fixfilename(name), &statb))
2784#else
2785 if (stat((char *)name, &statb))
2786#endif
2787 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002788#ifdef __INTERIX
2789 /* The top bit makes the value negative, which means the file doesn't
2790 * exist. Remove the bit, we don't use it. */
2791 return statb.st_mode & ~S_ADDACE;
2792#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795}
2796
2797/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002798 * Set file permission for "name" to "perm".
2799 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
2801 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002802mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
2804 return (chmod((char *)
2805#ifdef VMS
2806 vms_fixfilename(name),
2807#else
2808 name,
2809#endif
2810 (mode_t)perm) == 0 ? OK : FAIL);
2811}
2812
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002813#if defined(HAVE_FCHMOD) || defined(PROTO)
2814/*
2815 * Set file permission for open file "fd" to "perm".
2816 * Return FAIL for failure, OK otherwise.
2817 */
2818 int
2819mch_fsetperm(int fd, long perm)
2820{
2821 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2822}
2823#endif
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825#if defined(HAVE_ACL) || defined(PROTO)
2826# ifdef HAVE_SYS_ACL_H
2827# include <sys/acl.h>
2828# endif
2829# ifdef HAVE_SYS_ACCESS_H
2830# include <sys/access.h>
2831# endif
2832
2833# ifdef HAVE_SOLARIS_ACL
2834typedef struct vim_acl_solaris_T {
2835 int acl_cnt;
2836 aclent_t *acl_entry;
2837} vim_acl_solaris_T;
2838# endif
2839
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002840#if defined(HAVE_SELINUX) || defined(PROTO)
2841/*
2842 * Copy security info from "from_file" to "to_file".
2843 */
2844 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002845mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002846{
2847 if (from_file == NULL)
2848 return;
2849
2850 if (selinux_enabled == -1)
2851 selinux_enabled = is_selinux_enabled();
2852
2853 if (selinux_enabled > 0)
2854 {
2855 security_context_t from_context = NULL;
2856 security_context_t to_context = NULL;
2857
2858 if (getfilecon((char *)from_file, &from_context) < 0)
2859 {
2860 /* If the filesystem doesn't support extended attributes,
2861 the original had no special security context and the
2862 target cannot have one either. */
2863 if (errno == EOPNOTSUPP)
2864 return;
2865
2866 MSG_PUTS(_("\nCould not get security context for "));
2867 msg_outtrans(from_file);
2868 msg_putchar('\n');
2869 return;
2870 }
2871 if (getfilecon((char *)to_file, &to_context) < 0)
2872 {
2873 MSG_PUTS(_("\nCould not get security context for "));
2874 msg_outtrans(to_file);
2875 msg_putchar('\n');
2876 freecon (from_context);
2877 return ;
2878 }
2879 if (strcmp(from_context, to_context) != 0)
2880 {
2881 if (setfilecon((char *)to_file, from_context) < 0)
2882 {
2883 MSG_PUTS(_("\nCould not set security context for "));
2884 msg_outtrans(to_file);
2885 msg_putchar('\n');
2886 }
2887 }
2888 freecon(to_context);
2889 freecon(from_context);
2890 }
2891}
2892#endif /* HAVE_SELINUX */
2893
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002894#if defined(HAVE_SMACK) && !defined(PROTO)
2895/*
2896 * Copy security info from "from_file" to "to_file".
2897 */
2898 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002899mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002900{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002901 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002902 {
2903 XATTR_NAME_SMACK,
2904 XATTR_NAME_SMACKEXEC,
2905 XATTR_NAME_SMACKMMAP
2906 };
2907
2908 char buffer[SMACK_LABEL_LEN];
2909 const char *name;
2910 int index;
2911 int ret;
2912 ssize_t size;
2913
2914 if (from_file == NULL)
2915 return;
2916
2917 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2918 / sizeof(smack_copied_attributes)[0]) ; index++)
2919 {
2920 /* get the name of the attribute to copy */
2921 name = smack_copied_attributes[index];
2922
2923 /* get the value of the attribute in buffer */
2924 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2925 if (size >= 0)
2926 {
2927 /* copy the attribute value of buffer */
2928 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2929 if (ret < 0)
2930 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002931 vim_snprintf((char *)IObuff, IOSIZE,
2932 _("Could not set security context %s for %s"),
2933 name, to_file);
2934 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002935 msg_putchar('\n');
2936 }
2937 }
2938 else
2939 {
2940 /* what reason of not having the attribute value? */
2941 switch (errno)
2942 {
2943 case ENOTSUP:
2944 /* extended attributes aren't supported or enabled */
2945 /* should a message be echoed? not sure... */
2946 return; /* leave because it isn't usefull to continue */
2947
2948 case ERANGE:
2949 default:
2950 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002951 vim_snprintf((char *)IObuff, IOSIZE,
2952 _("Could not get security context %s for %s. Removing it!"),
2953 name, from_file);
2954 msg_puts(IObuff);
2955 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002956 /* FALLTHROUGH to remove the attribute */
2957
2958 case ENODATA:
2959 /* no attribute of this name */
2960 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002961 /* Silently ignore errors, apparently this happens when
2962 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002963 break;
2964 }
2965 }
2966 }
2967}
2968#endif /* HAVE_SMACK */
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
2971 * Return a pointer to the ACL of file "fname" in allocated memory.
2972 * Return NULL if the ACL is not available for whatever reason.
2973 */
2974 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002975mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 vim_acl_T ret = NULL;
2978#ifdef HAVE_POSIX_ACL
2979 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2980#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002981#ifdef HAVE_SOLARIS_ZFS_ACL
2982 acl_t *aclent;
2983
2984 if (acl_get((char *)fname, 0, &aclent) < 0)
2985 return NULL;
2986 ret = (vim_acl_T)aclent;
2987#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#ifdef HAVE_SOLARIS_ACL
2989 vim_acl_solaris_T *aclent;
2990
2991 aclent = malloc(sizeof(vim_acl_solaris_T));
2992 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2993 {
2994 free(aclent);
2995 return NULL;
2996 }
2997 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2998 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2999 {
3000 free(aclent->acl_entry);
3001 free(aclent);
3002 return NULL;
3003 }
3004 ret = (vim_acl_T)aclent;
3005#else
3006#if defined(HAVE_AIX_ACL)
3007 int aclsize;
3008 struct acl *aclent;
3009
3010 aclsize = sizeof(struct acl);
3011 aclent = malloc(aclsize);
3012 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3013 {
3014 if (errno == ENOSPC)
3015 {
3016 aclsize = aclent->acl_len;
3017 aclent = realloc(aclent, aclsize);
3018 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3019 {
3020 free(aclent);
3021 return NULL;
3022 }
3023 }
3024 else
3025 {
3026 free(aclent);
3027 return NULL;
3028 }
3029 }
3030 ret = (vim_acl_T)aclent;
3031#endif /* HAVE_AIX_ACL */
3032#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003033#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#endif /* HAVE_POSIX_ACL */
3035 return ret;
3036}
3037
3038/*
3039 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3040 */
3041 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003042mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 if (aclent == NULL)
3045 return;
3046#ifdef HAVE_POSIX_ACL
3047 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3048#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003049#ifdef HAVE_SOLARIS_ZFS_ACL
3050 acl_set((char *)fname, (acl_t *)aclent);
3051#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052#ifdef HAVE_SOLARIS_ACL
3053 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3054 ((vim_acl_solaris_T *)aclent)->acl_entry);
3055#else
3056#ifdef HAVE_AIX_ACL
3057 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3058#endif /* HAVE_AIX_ACL */
3059#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003060#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#endif /* HAVE_POSIX_ACL */
3062}
3063
3064 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003065mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 if (aclent == NULL)
3068 return;
3069#ifdef HAVE_POSIX_ACL
3070 acl_free((acl_t)aclent);
3071#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003072#ifdef HAVE_SOLARIS_ZFS_ACL
3073 acl_free((acl_t *)aclent);
3074#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075#ifdef HAVE_SOLARIS_ACL
3076 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3077 free(aclent);
3078#else
3079#ifdef HAVE_AIX_ACL
3080 free(aclent);
3081#endif /* HAVE_AIX_ACL */
3082#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003083#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084#endif /* HAVE_POSIX_ACL */
3085}
3086#endif
3087
3088/*
3089 * Set hidden flag for "name".
3090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003092mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 /* can't hide a file */
3095}
3096
3097/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003098 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * return FALSE if "name" is not a directory
3100 * return FALSE for error
3101 */
3102 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003103mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 struct stat statb;
3106
3107 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3108 return FALSE;
3109 if (stat((char *)name, &statb))
3110 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003114/*
3115 * return TRUE if "name" is a directory, NOT a symlink to a directory
3116 * return FALSE if "name" is not a directory
3117 * return FALSE for error
3118 */
3119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003120mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003121{
3122 struct stat statb;
3123
3124 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3125 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003126 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003127 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003128 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003129}
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131/*
3132 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3133 */
3134 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003135executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 struct stat st;
3138
3139 if (stat((char *)name, &st))
3140 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003141#ifdef VMS
3142 /* Like on Unix system file can have executable rights but not necessarily
3143 * be an executable, but on Unix is not a default for an ordianry file to
3144 * have an executable flag - on VMS it is in most cases.
3145 * Therefore, this check does not have any sense - let keep us to the
3146 * conventions instead:
3147 * *.COM and *.EXE files are the executables - the rest are not. This is
3148 * not ideal but better then it was.
3149 */
3150 int vms_executable = 0;
3151 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3152 {
3153 if (strstr(vms_tolower((char*)name),".exe") != NULL
3154 || strstr(vms_tolower((char*)name),".com")!= NULL)
3155 vms_executable = 1;
3156 }
3157 return vms_executable;
3158#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161}
3162
3163/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003164 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003165 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 * Return -1 if unknown.
3167 */
3168 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003169mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 char_u *buf;
3172 char_u *p, *e;
3173 int retval;
3174
Bram Moolenaarb5971142015-03-21 17:32:19 +01003175 /* When "use_path" is false and if it's an absolute or relative path don't
3176 * need to use $PATH. */
3177 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3178 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003179 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003180 /* There must be a path separator, files in the current directory
3181 * can't be executed. */
3182 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003183 {
3184 if (path != NULL)
3185 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003186 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003187 *path = FullName_save(name, TRUE);
3188 else
3189 *path = vim_strsave(name);
3190 }
3191 return TRUE;
3192 }
3193 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 p = (char_u *)getenv("PATH");
3197 if (p == NULL || *p == NUL)
3198 return -1;
3199 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3200 if (buf == NULL)
3201 return -1;
3202
3203 /*
3204 * Walk through all entries in $PATH to check if "name" exists there and
3205 * is an executable file.
3206 */
3207 for (;;)
3208 {
3209 e = (char_u *)strchr((char *)p, ':');
3210 if (e == NULL)
3211 e = p + STRLEN(p);
3212 if (e - p <= 1) /* empty entry means current dir */
3213 STRCPY(buf, "./");
3214 else
3215 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003216 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 add_pathsep(buf);
3218 }
3219 STRCAT(buf, name);
3220 retval = executable_file(buf);
3221 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003222 {
3223 if (path != NULL)
3224 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003225 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003226 *path = FullName_save(buf, TRUE);
3227 else
3228 *path = vim_strsave(buf);
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
3233 if (*e != ':')
3234 break;
3235 p = e + 1;
3236 }
3237
3238 vim_free(buf);
3239 return retval;
3240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242/*
3243 * Check what "name" is:
3244 * NODE_NORMAL: file or directory (or doesn't exist)
3245 * NODE_WRITABLE: writable device, socket, fifo, etc.
3246 * NODE_OTHER: non-writable things
3247 */
3248 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003249mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 struct stat st;
3252
3253 if (stat((char *)name, &st))
3254 return NODE_NORMAL;
3255 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3256 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3258 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 /* Everything else is writable? */
3260 return NODE_WRITABLE;
3261}
3262
3263 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003264mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266#ifdef HAVE_CHECK_STACK_GROWTH
3267 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 check_stack_growth((char *)&i);
3270
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003271# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 get_stack_limit();
3273# endif
3274
3275#endif
3276
3277 /*
3278 * Setup an alternative stack for signals. Helps to catch signals when
3279 * running out of stack space.
3280 * Use of sigaltstack() is preferred, it's more portable.
3281 * Ignore any errors.
3282 */
3283#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003284 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 init_signal_stack();
3286#endif
3287}
3288
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003289#if defined(EXITFREE) || defined(PROTO)
3290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003291mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003292{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003293# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3294 if (clip_star.owned)
3295 clip_lose_selection(&clip_star);
3296 if (clip_plus.owned)
3297 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003298# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003299# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003300 if (xterm_Shell != (Widget)0)
3301 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003302# ifndef LESSTIF_VERSION
3303 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003304 if (xterm_dpy != NULL)
3305 XtCloseDisplay(xterm_dpy);
3306 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003307 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003308 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003309# ifdef FEAT_X11
3310 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3311# endif
3312 }
3313# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003314# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003315# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003316 if (x11_display != NULL
3317# ifdef FEAT_XCLIPBOARD
3318 && x11_display != xterm_dpy
3319# endif
3320 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 XCloseDisplay(x11_display);
3322# endif
3323# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003324 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325# endif
3326# ifdef FEAT_TITLE
3327 vim_free(oldtitle);
3328 vim_free(oldicon);
3329# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003330}
3331#endif
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
3334 * Output a newline when exiting.
3335 * Make sure the newline goes to the same stream as the text.
3336 */
3337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003338exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003340 if (silent_mode)
3341 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (newline_on_exit || msg_didout)
3343 {
3344 if (msg_use_printf())
3345 {
3346 if (info_message)
3347 mch_msg("\n");
3348 else
3349 mch_errmsg("\r\n");
3350 }
3351 else
3352 out_char('\n');
3353 }
3354 else
3355 {
3356 restore_cterm_colors(); /* get original colors back */
3357 msg_clr_eos_force(); /* clear the rest of the display */
3358 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3359 }
3360}
3361
3362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003363mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 exiting = TRUE;
3366
3367#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3368 x11_export_final_selection();
3369#endif
3370
3371#ifdef FEAT_GUI
3372 if (!gui.in_use)
3373#endif
3374 {
3375 settmode(TMODE_COOK);
3376#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003377 // restore xterm title and icon name
3378 mch_restore_title(SAVE_RESTORE_BOTH);
3379 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380#endif
3381 /*
3382 * When t_ti is not empty but it doesn't cause swapping terminal
3383 * pages, need to output a newline when msg_didout is set. But when
3384 * t_ti does swap pages it should not go to the shell page. Do this
3385 * before stoptermcap().
3386 */
3387 if (swapping_screen() && !newline_on_exit)
3388 exit_scroll();
3389
3390 /* Stop termcap: May need to check for T_CRV response, which
3391 * requires RAW mode. */
3392 stoptermcap();
3393
3394 /*
3395 * A newline is only required after a message in the alternate screen.
3396 * This is set to TRUE by wait_return().
3397 */
3398 if (!swapping_screen() || newline_on_exit)
3399 exit_scroll();
3400
3401 /* Cursor may have been switched off without calling starttermcap()
3402 * when doing "vim -u vimrc" and vimrc contains ":q". */
3403 if (full_screen)
3404 cursor_on();
3405 }
3406 out_flush();
3407 ml_close_all(TRUE); /* remove all memfiles */
3408 may_core_dump();
3409#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 gui_exit(r);
3412#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003413
Bram Moolenaar56718732006-03-15 22:53:57 +00003414#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003415 mac_conv_cleanup();
3416#endif
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#ifdef __QNX__
3419 /* A core dump won't be created if the signal handler
3420 * doesn't return, so we can't call exit() */
3421 if (deadly_signal != 0)
3422 return;
3423#endif
3424
Bram Moolenaar009b2592004-10-24 19:18:58 +00003425#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003426 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003427#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003428
3429#ifdef EXITFREE
3430 free_all_mem();
3431#endif
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 exit(r);
3434}
3435
3436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003437may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 if (deadly_signal != 0)
3440 {
3441 signal(deadly_signal, SIG_DFL);
3442 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3443 }
3444}
3445
3446#ifndef VMS
3447
3448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003449mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 static int first = TRUE;
3452
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003453#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454# ifdef HAVE_TERMIOS_H
3455 static struct termios told;
3456 struct termios tnew;
3457# else
3458 static struct termio told;
3459 struct termio tnew;
3460# endif
3461
3462 if (first)
3463 {
3464 first = FALSE;
3465# if defined(HAVE_TERMIOS_H)
3466 tcgetattr(read_cmd_fd, &told);
3467# else
3468 ioctl(read_cmd_fd, TCGETA, &told);
3469# endif
3470 }
3471
3472 tnew = told;
3473 if (tmode == TMODE_RAW)
3474 {
3475 /*
3476 * ~ICRNL enables typing ^V^M
3477 */
3478 tnew.c_iflag &= ~ICRNL;
3479 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3480# if defined(IEXTEN) && !defined(__MINT__)
3481 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3482 /* but it breaks function keys on MINT */
3483# endif
3484 );
3485# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3486 tnew.c_oflag &= ~ONLCR;
3487# endif
3488 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3489 tnew.c_cc[VTIME] = 0; /* don't wait */
3490 }
3491 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003492 {
3493 /* Also reset ICANON here, otherwise on Solaris select() won't see
3494 * typeahead characters. */
3495 tnew.c_lflag &= ~(ICANON | ECHO);
3496 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3497 tnew.c_cc[VTIME] = 0; /* don't wait */
3498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500# if defined(HAVE_TERMIOS_H)
3501 {
3502 int n = 10;
3503
3504 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3505 * few times. */
3506 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3507 && errno == EINTR && n > 0)
3508 --n;
3509 }
3510# else
3511 ioctl(read_cmd_fd, TCSETA, &tnew);
3512# endif
3513
3514#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 /*
3516 * for "old" tty systems
3517 */
3518# ifndef TIOCSETN
3519# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3520# endif
3521 static struct sgttyb ttybold;
3522 struct sgttyb ttybnew;
3523
3524 if (first)
3525 {
3526 first = FALSE;
3527 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3528 }
3529
3530 ttybnew = ttybold;
3531 if (tmode == TMODE_RAW)
3532 {
3533 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3534 ttybnew.sg_flags |= RAW;
3535 }
3536 else if (tmode == TMODE_SLEEP)
3537 ttybnew.sg_flags &= ~(ECHO);
3538 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3539#endif
3540 curr_tmode = tmode;
3541}
3542
3543/*
3544 * Try to get the code for "t_kb" from the stty setting
3545 *
3546 * Even if termcap claims a backspace key, the user's setting *should*
3547 * prevail. stty knows more about reality than termcap does, and if
3548 * somebody's usual erase key is DEL (which, for most BSD users, it will
3549 * be), they're going to get really annoyed if their erase key starts
3550 * doing forward deletes for no reason. (Eric Fischer)
3551 */
3552 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003553get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003555 ttyinfo_T info;
3556 char_u buf[2];
3557 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003559 if (get_tty_info(read_cmd_fd, &info) == OK)
3560 {
3561 intr_char = info.interrupt;
3562 buf[0] = info.backspace;
3563 buf[1] = NUL;
3564 add_termcode((char_u *)"kb", buf, FALSE);
3565
3566 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3567 p = find_termcode((char_u *)"kD");
3568 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3569 do_fixdel(NULL);
3570 }
3571}
3572
3573/*
3574 * Obtain the characters that Backspace and Enter produce on "fd".
3575 * Returns OK or FAIL.
3576 */
3577 int
3578get_tty_info(int fd, ttyinfo_T *info)
3579{
3580#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581# ifdef HAVE_TERMIOS_H
3582 struct termios keys;
3583# else
3584 struct termio keys;
3585# endif
3586
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003589 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003591 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003593 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003595 info->backspace = keys.c_cc[VERASE];
3596 info->interrupt = keys.c_cc[VINTR];
3597 if (keys.c_iflag & ICRNL)
3598 info->enter = NL;
3599 else
3600 info->enter = CAR;
3601 if (keys.c_oflag & ONLCR)
3602 info->nl_does_cr = TRUE;
3603 else
3604 info->nl_does_cr = FALSE;
3605 return OK;
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607#else
3608 /* for "old" tty systems */
3609 struct sgttyb keys;
3610
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003611 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003613 info->backspace = keys.sg_erase;
3614 info->interrupt = keys.sg_kill;
3615 info->enter = CAR;
3616 info->nl_does_cr = TRUE;
3617 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003620 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621}
3622
3623#endif /* VMS */
3624
3625#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003626static int mouse_ison = FALSE;
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628/*
3629 * Set mouse clicks on or off.
3630 */
3631 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003632mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003634# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003635 static int bevalterm_ison = FALSE;
3636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 int xterm_mouse_vers;
3638
Bram Moolenaara06afc72018-08-27 23:24:16 +02003639# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3640 if (!on)
3641 // Make sure not tracing mouse movements. Important when a button-down
3642 // was received but no release yet.
3643 stop_xterm_trace();
3644# endif
3645
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003646 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003647# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003648 && p_bevalterm == bevalterm_ison
3649# endif
3650 )
3651 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return;
3653
3654 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003655
3656# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003657 if (ttym_flags == TTYM_URXVT)
3658 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003659 out_str_nf((char_u *)
3660 (on
3661 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3662 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003663 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003664 }
3665# endif
3666
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003667# ifdef FEAT_MOUSE_SGR
3668 if (ttym_flags == TTYM_SGR)
3669 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003670 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003671 out_str_nf((char_u *)
3672 (on
3673 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3674 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003675 mouse_ison = on;
3676 }
3677# endif
3678
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003679# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003680 if (bevalterm_ison != (p_bevalterm && on))
3681 {
3682 bevalterm_ison = (p_bevalterm && on);
3683 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3684 /* disable mouse movement events, enabling is below */
3685 out_str_nf((char_u *)
3686 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003687 }
3688# endif
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (xterm_mouse_vers > 0)
3691 {
3692 if (on) /* enable mouse events, use mouse tracking if available */
3693 out_str_nf((char_u *)
3694 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003695 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003696# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003697 bevalterm_ison
3698 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3699# endif
3700 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3702 else /* disable mouse events, could probably always send the same */
3703 out_str_nf((char_u *)
3704 (xterm_mouse_vers > 1
3705 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3706 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709
3710# ifdef FEAT_MOUSE_DEC
3711 else if (ttym_flags == TTYM_DEC)
3712 {
3713 if (on) /* enable mouse events */
3714 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3715 else /* disable mouse events */
3716 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003717 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719# endif
3720
3721# ifdef FEAT_MOUSE_GPM
3722 else
3723 {
3724 if (on)
3725 {
3726 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 else
3730 {
3731 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003732 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734 }
3735# endif
3736
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003737# ifdef FEAT_SYSMOUSE
3738 else
3739 {
3740 if (on)
3741 {
3742 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003743 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003744 }
3745 else
3746 {
3747 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003748 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003749 }
3750 }
3751# endif
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753# ifdef FEAT_MOUSE_JSB
3754 else
3755 {
3756 if (on)
3757 {
3758 /* D - Enable Mouse up/down messages
3759 * L - Enable Left Button Reporting
3760 * M - Enable Middle Button Reporting
3761 * R - Enable Right Button Reporting
3762 * K - Enable SHIFT and CTRL key Reporting
3763 * + - Enable Advanced messaging of mouse moves and up/down messages
3764 * Q - Quiet No Ack
3765 * # - Numeric value of mouse pointer required
3766 * 0 = Multiview 2000 cursor, used as standard
3767 * 1 = Windows Arrow
3768 * 2 = Windows I Beam
3769 * 3 = Windows Hour Glass
3770 * 4 = Windows Cross Hair
3771 * 5 = Windows UP Arrow
3772 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003773# ifdef JSBTERM_MOUSE_NONADVANCED
3774 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3776 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003777# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3779 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003780# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003781 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 else
3784 {
3785 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3786 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003787 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 }
3790# endif
3791# ifdef FEAT_MOUSE_PTERM
3792 else
3793 {
3794 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3795 if (on)
3796 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3797 else
3798 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003799 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801# endif
3802}
3803
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003804#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003805/*
3806 * Called when 'balloonevalterm' changed.
3807 */
3808 void
3809mch_bevalterm_changed(void)
3810{
3811 mch_setmouse(mouse_ison);
3812}
3813#endif
3814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815/*
3816 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3817 */
3818 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003819check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821# ifdef FEAT_MOUSE_XTERM
3822 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003823# ifdef FEAT_MOUSE_URXVT
3824 && use_xterm_mouse() != 3
3825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826# ifdef FEAT_GUI
3827 && !gui.in_use
3828# endif
3829 )
3830 {
3831 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003832 ? IF_EB("\233M", CSI_STR "M")
3833 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (*p_mouse != NUL)
3835 {
3836 /* force mouse off and maybe on to send possibly new mouse
3837 * activation sequence to the xterm, with(out) drag tracing. */
3838 mch_setmouse(FALSE);
3839 setmouse();
3840 }
3841 }
3842 else
3843 del_mouse_termcode(KS_MOUSE);
3844# endif
3845
3846# ifdef FEAT_MOUSE_GPM
3847 if (!use_xterm_mouse()
3848# ifdef FEAT_GUI
3849 && !gui.in_use
3850# endif
3851 )
3852 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3853# endif
3854
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003855# ifdef FEAT_SYSMOUSE
3856 if (!use_xterm_mouse()
3857# ifdef FEAT_GUI
3858 && !gui.in_use
3859# endif
3860 )
3861 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3862# endif
3863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003865 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (!use_xterm_mouse()
3867# ifdef FEAT_GUI
3868 && !gui.in_use
3869# endif
3870 )
3871 set_mouse_termcode(KS_JSBTERM_MOUSE,
3872 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3873 else
3874 del_mouse_termcode(KS_JSBTERM_MOUSE);
3875# endif
3876
3877# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003878 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 * define it in the GUI or when using an xterm. */
3880 if (!use_xterm_mouse()
3881# ifdef FEAT_GUI
3882 && !gui.in_use
3883# endif
3884 )
3885 set_mouse_termcode(KS_NETTERM_MOUSE,
3886 (char_u *)IF_EB("\033}", ESC_STR "}"));
3887 else
3888 del_mouse_termcode(KS_NETTERM_MOUSE);
3889# endif
3890
3891# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003892 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003893 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894# ifdef FEAT_GUI
3895 && !gui.in_use
3896# endif
3897 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003898 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3899 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
3901 del_mouse_termcode(KS_DEC_MOUSE);
3902# endif
3903# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003904 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003905 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906# ifdef FEAT_GUI
3907 && !gui.in_use
3908# endif
3909 )
3910 set_mouse_termcode(KS_PTERM_MOUSE,
3911 (char_u *) IF_EB("\033[", ESC_STR "["));
3912 else
3913 del_mouse_termcode(KS_PTERM_MOUSE);
3914# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003915# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003916 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003917# ifdef FEAT_GUI
3918 && !gui.in_use
3919# endif
3920 )
3921 {
3922 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003923 ? IF_EB("\233*M", CSI_STR "*M")
3924 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003925
3926 if (*p_mouse != NUL)
3927 {
3928 mch_setmouse(FALSE);
3929 setmouse();
3930 }
3931 }
3932 else
3933 del_mouse_termcode(KS_URXVT_MOUSE);
3934# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003935# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003936 if (use_xterm_mouse() == 4
3937# ifdef FEAT_GUI
3938 && !gui.in_use
3939# endif
3940 )
3941 {
3942 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003943 ? IF_EB("\233<*M", CSI_STR "<*M")
3944 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3945
3946 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3947 ? IF_EB("\233<*m", CSI_STR "<*m")
3948 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949
3950 if (*p_mouse != NUL)
3951 {
3952 mch_setmouse(FALSE);
3953 setmouse();
3954 }
3955 }
3956 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003957 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003958 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003959 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3960 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962}
3963#endif
3964
3965/*
3966 * set screen mode, always fails.
3967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003969mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 EMSG(_(e_screenmode));
3972 return FAIL;
3973}
3974
3975#ifndef VMS
3976
3977/*
3978 * Try to get the current window size:
3979 * 1. with an ioctl(), most accurate method
3980 * 2. from the environment variables LINES and COLUMNS
3981 * 3. from the termcap
3982 * 4. keep using the old values
3983 * Return OK when size could be determined, FAIL otherwise.
3984 */
3985 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003986mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 long rows = 0;
3989 long columns = 0;
3990 char_u *p;
3991
3992 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * 1. try using an ioctl. It is the most accurate method.
3994 *
3995 * Try using TIOCGWINSZ first, some systems that have it also define
3996 * TIOCGSIZE but don't have a struct ttysize.
3997 */
3998# ifdef TIOCGWINSZ
3999 {
4000 struct winsize ws;
4001 int fd = 1;
4002
4003 /* When stdout is not a tty, use stdin for the ioctl(). */
4004 if (!isatty(fd) && isatty(read_cmd_fd))
4005 fd = read_cmd_fd;
4006 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4007 {
4008 columns = ws.ws_col;
4009 rows = ws.ws_row;
4010 }
4011 }
4012# else /* TIOCGWINSZ */
4013# ifdef TIOCGSIZE
4014 {
4015 struct ttysize ts;
4016 int fd = 1;
4017
4018 /* When stdout is not a tty, use stdin for the ioctl(). */
4019 if (!isatty(fd) && isatty(read_cmd_fd))
4020 fd = read_cmd_fd;
4021 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4022 {
4023 columns = ts.ts_cols;
4024 rows = ts.ts_lines;
4025 }
4026 }
4027# endif /* TIOCGSIZE */
4028# endif /* TIOCGWINSZ */
4029
4030 /*
4031 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004032 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4033 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004035 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
4037 if ((p = (char_u *)getenv("LINES")))
4038 rows = atoi((char *)p);
4039 if ((p = (char_u *)getenv("COLUMNS")))
4040 columns = atoi((char *)p);
4041 }
4042
4043#ifdef HAVE_TGETENT
4044 /*
4045 * 3. try reading "co" and "li" entries from termcap
4046 */
4047 if (columns == 0 || rows == 0)
4048 getlinecol(&columns, &rows);
4049#endif
4050
4051 /*
4052 * 4. If everything fails, use the old values
4053 */
4054 if (columns <= 0 || rows <= 0)
4055 return FAIL;
4056
4057 Rows = rows;
4058 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004059 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return OK;
4061}
4062
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004063#if defined(FEAT_TERMINAL) || defined(PROTO)
4064/*
4065 * Report the windows size "rows" and "cols" to tty "fd".
4066 */
4067 int
4068mch_report_winsize(int fd, int rows, int cols)
4069{
4070# ifdef TIOCSWINSZ
4071 struct winsize ws;
4072
4073 ws.ws_col = cols;
4074 ws.ws_row = rows;
4075 ws.ws_xpixel = cols * 5;
4076 ws.ws_ypixel = rows * 10;
4077 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4078 {
4079 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4080 return OK;
4081 }
4082 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4083# else
4084# ifdef TIOCSSIZE
4085 struct ttysize ts;
4086
4087 ts.ts_cols = cols;
4088 ts.ts_lines = rows;
4089 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4090 {
4091 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4092 return OK;
4093 }
4094 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4095# endif
4096# endif
4097 return FAIL;
4098}
4099#endif
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101/*
4102 * Try to set the window size to Rows and Columns.
4103 */
4104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004105mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 if (*T_CWS)
4108 {
4109 /*
4110 * NOTE: if you get an error here that term_set_winsize() is
4111 * undefined, check the output of configure. It could probably not
4112 * find a ncurses, termcap or termlib library.
4113 */
4114 term_set_winsize((int)Rows, (int)Columns);
4115 out_flush();
4116 screen_start(); /* don't know where cursor is now */
4117 }
4118}
4119
4120#endif /* VMS */
4121
4122/*
4123 * Rows and/or Columns has changed.
4124 */
4125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004126mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
4128 /* Nothing to do. */
4129}
4130
Bram Moolenaar205b8862011-09-07 15:04:31 +02004131/*
4132 * Wait for process "child" to end.
4133 * Return "child" if it exited properly, <= 0 on error.
4134 */
4135 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004136wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004137{
4138 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004139 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004140
4141 while (wait_pid != child)
4142 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004143 /* When compiled with Python threads are probably used, in which case
4144 * wait() sometimes hangs for no obvious reason. Use waitpid()
4145 * instead and loop (like the GUI). Also needed for other interfaces,
4146 * they might call system(). */
4147# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004148 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004149# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004151# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004152 if (wait_pid == 0)
4153 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004154 /* Wait for 1 to 10 msec before trying again. */
4155 mch_delay(delay_msec, TRUE);
4156 if (++delay_msec > 10)
4157 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158 continue;
4159 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004160 if (wait_pid <= 0
4161# ifdef ECHILD
4162 && errno == ECHILD
4163# endif
4164 )
4165 break;
4166 }
4167 return wait_pid;
4168}
4169
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004170#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004171/*
4172 * Set the environment for a child process.
4173 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004174 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004175set_child_environment(
4176 long rows,
4177 long columns,
4178 char *term,
4179 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004180{
4181# ifdef HAVE_SETENV
4182 char envbuf[50];
4183# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004184 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004185 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004186 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004187 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004188 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004189# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004190 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004191# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004192# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004193 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004194# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004195# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004196 long colors =
4197# ifdef FEAT_GUI
4198 gui.in_use ? 256*256*256 :
4199# endif
4200 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004201
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004203 setenv("TERM", term, 1);
4204 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004205 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004206 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004207 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004208 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004209 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004210 sprintf((char *)envbuf, "%ld", colors);
4211 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004212# ifdef FEAT_TERMINAL
4213 if (is_terminal)
4214 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004215 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004216 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4217 }
4218# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004219# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004220 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004221# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222# else
4223 /*
4224 * Putenv does not copy the string, it has to remain valid.
4225 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004226 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004227 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004228 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4229 putenv(envbuf_Term);
4230 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004231 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004232 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4233 putenv(envbuf_Lines);
4234 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4235 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004236 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004237 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4238 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004239# ifdef FEAT_TERMINAL
4240 if (is_terminal)
4241 {
4242 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004243 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004244 putenv(envbuf_Version);
4245 }
4246# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004247# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004248 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4249 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4250 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004251# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004252# endif
4253}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254
4255 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004256set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004257{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004258 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004259}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004260#endif
4261
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004262#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004263/*
4264 * Open a PTY, with FD for the master and slave side.
4265 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4266 * When successful both file descriptors are stored.
4267 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004268 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004269open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004270{
4271 char *tty_name;
4272
4273 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4274 if (*pty_master_fd >= 0)
4275 {
4276 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4277 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4278 * adding O_NOCTTY always works when defined. */
4279#ifdef O_NOCTTY
4280 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4281#else
4282 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4283#endif
4284 if (*pty_slave_fd < 0)
4285 {
4286 close(*pty_master_fd);
4287 *pty_master_fd = -1;
4288 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004289 else if (namep != NULL)
4290 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004291 }
4292}
4293#endif
4294
Bram Moolenaarfae42832017-08-01 22:24:26 +02004295/*
4296 * Send SIGINT to a child process if "c" is an interrupt character.
4297 */
4298 void
4299may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4300{
4301# ifdef SIGINT
4302 if (c == Ctrl_C || c == intr_char)
4303 {
4304# ifdef HAVE_SETSID
4305 kill(-pid, SIGINT);
4306# else
4307 kill(0, SIGINT);
4308# endif
4309 if (wpid > 0)
4310 kill(wpid, SIGINT);
4311 }
4312# endif
4313}
4314
Bram Moolenaar13568252018-03-16 20:46:58 +01004315#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4316
4317 static int
4318build_argv(
4319 char_u *cmd,
4320 char ***argvp,
4321 char_u **sh_tofree,
4322 char_u **shcf_tofree)
4323{
4324 char **argv = NULL;
4325 int argc;
4326
4327 *sh_tofree = vim_strsave(p_sh);
4328 if (*sh_tofree == NULL) /* out of memory */
4329 return FAIL;
4330
4331 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4332 return FAIL;
4333 *argvp = argv;
4334
4335 if (cmd != NULL)
4336 {
4337 char_u *s;
4338 char_u *p;
4339
4340 if (extra_shell_arg != NULL)
4341 argv[argc++] = (char *)extra_shell_arg;
4342
4343 /* Break 'shellcmdflag' into white separated parts. This doesn't
4344 * handle quoted strings, they are very unlikely to appear. */
4345 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4346 if (*shcf_tofree == NULL) /* out of memory */
4347 return FAIL;
4348 s = *shcf_tofree;
4349 p = p_shcf;
4350 while (*p != NUL)
4351 {
4352 argv[argc++] = (char *)s;
4353 while (*p && *p != ' ' && *p != TAB)
4354 *s++ = *p++;
4355 *s++ = NUL;
4356 p = skipwhite(p);
4357 }
4358
4359 argv[argc++] = (char *)cmd;
4360 }
4361 argv[argc] = NULL;
4362 return OK;
4363}
4364#endif
4365
4366#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4367/*
4368 * Use a terminal window to run a shell command in.
4369 */
4370 static int
4371mch_call_shell_terminal(
4372 char_u *cmd,
4373 int options UNUSED) /* SHELL_*, see vim.h */
4374{
4375 jobopt_T opt;
4376 char **argv = NULL;
4377 char_u *tofree1 = NULL;
4378 char_u *tofree2 = NULL;
4379 int retval = -1;
4380 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004381 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004382 aco_save_T aco;
4383 oparg_T oa; /* operator arguments */
4384
4385 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4386 goto theend;
4387
4388 init_job_options(&opt);
4389 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4390 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004391 if (buf == NULL)
4392 goto theend;
4393
4394 job = term_getjob(buf->b_term);
4395 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004396
4397 /* Find a window to make "buf" curbuf. */
4398 aucmd_prepbuf(&aco, buf);
4399
4400 clear_oparg(&oa);
4401 while (term_use_loop())
4402 {
4403 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4404 {
4405 /* If terminal_loop() returns OK we got a key that is handled
4406 * in Normal model. We don't do redrawing anyway. */
4407 if (terminal_loop(TRUE) == OK)
4408 normal_cmd(&oa, TRUE);
4409 }
4410 else
4411 normal_cmd(&oa, TRUE);
4412 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004413 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004414 ch_log(NULL, "system command finished");
4415
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004416 job_unref(job);
4417
Bram Moolenaar13568252018-03-16 20:46:58 +01004418 /* restore curwin/curbuf and a few other things */
4419 aucmd_restbuf(&aco);
4420
4421 wait_return(TRUE);
4422 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4423
4424theend:
4425 vim_free(argv);
4426 vim_free(tofree1);
4427 vim_free(tofree2);
4428 return retval;
4429}
4430#endif
4431
4432#ifdef USE_SYSTEM
4433/*
4434 * Use system() to start the shell: simple but slow.
4435 */
4436 static int
4437mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004438 char_u *cmd,
4439 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441#ifdef VMS
4442 char *ifn = NULL;
4443 char *ofn = NULL;
4444#endif
4445 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004446 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004447 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 out_flush();
4450
4451 if (options & SHELL_COOKED)
4452 settmode(TMODE_COOK); /* set to normal mode */
4453
Bram Moolenaar62b42182010-09-21 22:09:37 +02004454# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004455 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004456 loose_clipboard();
4457# endif
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (cmd == NULL)
4460 x = system((char *)p_sh);
4461 else
4462 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004463# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 if (ofn = strchr((char *)cmd, '>'))
4465 *ofn++ = '\0';
4466 if (ifn = strchr((char *)cmd, '<'))
4467 {
4468 char *p;
4469
4470 *ifn++ = '\0';
4471 p = strchr(ifn,' '); /* chop off any trailing spaces */
4472 if (p)
4473 *p = '\0';
4474 }
4475 if (ofn)
4476 x = vms_sys((char *)cmd, ofn, ifn);
4477 else
4478 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004479# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 newcmd = lalloc(STRLEN(p_sh)
4481 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4482 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4483 if (newcmd == NULL)
4484 x = 0;
4485 else
4486 {
4487 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4488 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4489 (char *)p_shcf,
4490 (char *)cmd);
4491 x = system((char *)newcmd);
4492 vim_free(newcmd);
4493 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496# ifdef VMS
4497 x = vms_sys_status(x);
4498# endif
4499 if (emsg_silent)
4500 ;
4501 else if (x == 127)
4502 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 else if (x && !(options & SHELL_SILENT))
4504 {
4505 MSG_PUTS(_("\nshell returned "));
4506 msg_outnum((long)x);
4507 msg_putchar('\n');
4508 }
4509
4510 if (tmode == TMODE_RAW)
4511 settmode(TMODE_RAW); /* set to raw mode */
4512# ifdef FEAT_TITLE
4513 resettitle();
4514# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004515# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4516 restore_clipboard();
4517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004519}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar13568252018-03-16 20:46:58 +01004521#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaardf177f62005-02-22 08:39:57 +00004523# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4524 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004525# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaar13568252018-03-16 20:46:58 +01004527/*
4528 * Don't use system(), use fork()/exec().
4529 */
4530 static int
4531mch_call_shell_fork(
4532 char_u *cmd,
4533 int options) /* SHELL_*, see vim.h */
4534{
4535 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004537 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 pid_t wait_pid = 0;
4539# ifdef HAVE_UNION_WAIT
4540 union wait status;
4541# else
4542 int status = -1;
4543# endif
4544 int retval = -1;
4545 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004546 char_u *tofree1 = NULL;
4547 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004550# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004552# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004553 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 int fd_fromshell[2];
4555 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004556 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 out_flush();
4559 if (options & SHELL_COOKED)
4560 settmode(TMODE_COOK); /* set to normal mode */
4561
Bram Moolenaar13568252018-03-16 20:46:58 +01004562 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004563 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004566 * For the GUI, when writing the output into the buffer and when reading
4567 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4568 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004570 if ((options & (SHELL_READ|SHELL_WRITE))
4571# ifdef FEAT_GUI
4572 || (gui.in_use && show_shell_mess)
4573# endif
4574 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004576# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 /*
4578 * Try to open a master pty.
4579 * If this works, open the slave pty.
4580 * If the slave can't be opened, close the master pty.
4581 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004582 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004583 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 /*
4585 * If not opening a pty or it didn't work, try using pipes.
4586 */
4587 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
4590 pipe_error = (pipe(fd_toshell) < 0);
4591 if (!pipe_error) /* pipe create OK */
4592 {
4593 pipe_error = (pipe(fd_fromshell) < 0);
4594 if (pipe_error) /* pipe create failed */
4595 {
4596 close(fd_toshell[0]);
4597 close(fd_toshell[1]);
4598 }
4599 }
4600 if (pipe_error)
4601 {
4602 MSG_PUTS(_("\nCannot create pipes\n"));
4603 out_flush();
4604 }
4605 }
4606 }
4607
4608 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004610 SIGSET_DECL(curset)
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612# ifdef __BEOS__
4613 beos_cleanup_read_thread();
4614# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004615
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004616 BLOCK_SIGNALS(&curset);
4617 pid = fork(); /* maybe we should use vfork() */
4618 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004620 UNBLOCK_SIGNALS(&curset);
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004623 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004625 || (gui.in_use && show_shell_mess)
4626# endif
4627 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004629# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (pty_master_fd >= 0) /* close the pseudo tty */
4631 {
4632 close(pty_master_fd);
4633 close(pty_slave_fd);
4634 }
4635 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 close(fd_toshell[0]);
4639 close(fd_toshell[1]);
4640 close(fd_fromshell[0]);
4641 close(fd_fromshell[1]);
4642 }
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 else if (pid == 0) /* child */
4646 {
4647 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004648 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaar819524702018-02-27 19:10:00 +01004650# ifdef FEAT_JOB_CHANNEL
4651 if (ch_log_active())
4652 /* close the log file in the child */
4653 ch_logfile((char_u *)"", (char_u *)"");
4654# endif
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (!show_shell_mess || (options & SHELL_EXPAND))
4657 {
4658 int fd;
4659
4660 /*
4661 * Don't want to show any message from the shell. Can't just
4662 * close stdout and stderr though, because some systems will
4663 * break if you try to write to them after that, so we must
4664 * use dup() to replace them with something else -- webb
4665 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4666 * waiting for input.
4667 */
4668 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4669 fclose(stdin);
4670 fclose(stdout);
4671 fclose(stderr);
4672
4673 /*
4674 * If any of these open()'s and dup()'s fail, we just continue
4675 * anyway. It's not fatal, and on most systems it will make
4676 * no difference at all. On a few it will cause the execvp()
4677 * to exit with a non-zero status even when the completion
4678 * could be done, which is nothing too serious. If the open()
4679 * or dup() failed we'd just do the same thing ourselves
4680 * anyway -- webb
4681 */
4682 if (fd >= 0)
4683 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004684 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4685 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4686 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 /* Don't need this now that we've duplicated it */
4689 close(fd);
4690 }
4691 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004694 || gui.in_use
4695# endif
4696 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698
Bram Moolenaardf177f62005-02-22 08:39:57 +00004699# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004700 /* Create our own process group, so that the child and all its
4701 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004702 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004703 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004704 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004705 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004706# if defined(SIGHUP)
4707 /* When doing "!xterm&" and 'shell' is bash: the shell
4708 * will exit and send SIGHUP to all processes in its
4709 * group, killing the just started process. Ignore SIGHUP
4710 * to avoid that. (suggested by Simon Schubert)
4711 */
4712 signal(SIGHUP, SIG_IGN);
4713# endif
4714 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004715# endif
4716# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004717 if (pty_slave_fd >= 0)
4718 {
4719 /* push stream discipline modules */
4720 if (options & SHELL_COOKED)
4721 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004723 /* Try to become controlling tty (probably doesn't work,
4724 * unless run by root) */
4725 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004727 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004728# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004729 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaara5792f52005-11-23 21:25:05 +00004731 /*
4732 * stderr is only redirected when using the GUI, so that a
4733 * program like gpg can still access the terminal to get a
4734 * passphrase using stderr.
4735 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004736# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if (pty_master_fd >= 0)
4738 {
4739 close(pty_master_fd); /* close master side of pty */
4740
4741 /* set up stdin/stdout/stderr for the child */
4742 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004743 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004745 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004746 if (gui.in_use)
4747 {
4748 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004749 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 close(pty_slave_fd); /* has been dupped, close it now */
4753 }
4754 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 /* set up stdin for the child */
4758 close(fd_toshell[1]);
4759 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004760 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 close(fd_toshell[0]);
4762
4763 /* set up stdout for the child */
4764 close(fd_fromshell[0]);
4765 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004766 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 close(fd_fromshell[1]);
4768
Bram Moolenaara5792f52005-11-23 21:25:05 +00004769# ifdef FEAT_GUI
4770 if (gui.in_use)
4771 {
4772 /* set up stderr for the child */
4773 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004774 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004775 }
4776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /*
4781 * There is no type cast for the argv, because the type may be
4782 * different on different machines. This may cause a warning
4783 * message with strict compilers, don't worry about it.
4784 * Call _exit() instead of exit() to avoid closing the connection
4785 * to the X server (esp. with GTK, which uses atexit()).
4786 */
4787 execvp(argv[0], argv);
4788 _exit(EXEC_FAILED); /* exec failed, return failure code */
4789 }
4790 else /* parent */
4791 {
4792 /*
4793 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004794 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 */
4796 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004797 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004798 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004799# ifdef FEAT_JOB_CHANNEL
4800 ++dont_check_job_ended;
4801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 /*
4803 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004804 * This is also used to pipe stdin/stdout to/from the external
4805 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807 if ((options & (SHELL_READ|SHELL_WRITE))
4808# ifdef FEAT_GUI
4809 || (gui.in_use && show_shell_mess)
4810# endif
4811 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4819 int ta_len = 0; /* valid bytes in ta_buf[] */
4820 int len;
4821 int p_more_save;
4822 int old_State;
4823 int c;
4824 int toshell_fd;
4825 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826 garray_T ga;
4827 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004828# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4829 struct timeval start_tv;
4830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (pty_master_fd >= 0)
4834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 fromshell_fd = pty_master_fd;
4836 toshell_fd = dup(pty_master_fd);
4837 }
4838 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 close(fd_toshell[0]);
4842 close(fd_fromshell[1]);
4843 toshell_fd = fd_toshell[1];
4844 fromshell_fd = fd_fromshell[0];
4845 }
4846
4847 /*
4848 * Write to the child if there are typed characters.
4849 * Read from the child if there are characters available.
4850 * Repeat the reading a few times if more characters are
4851 * available. Need to check for typed keys now and then, but
4852 * not too often (delays when no chars are available).
4853 * This loop is quit if no characters can be read from the pty
4854 * (WaitForChar detected special condition), or there are no
4855 * characters available and the child has exited.
4856 * Only check if the child has exited when there is no more
4857 * output. The child may exit before all the output has
4858 * been printed.
4859 *
4860 * Currently this busy loops!
4861 * This can probably dead-lock when the write blocks!
4862 */
4863 p_more_save = p_more;
4864 p_more = FALSE;
4865 old_State = State;
4866 State = EXTERNCMD; /* don't redraw at window resize */
4867
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004868 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004869 {
4870 /* Fork a process that will write the lines to the
4871 * external program. */
4872 if ((wpid = fork()) == -1)
4873 {
4874 MSG_PUTS(_("\nCannot fork\n"));
4875 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004876 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877 {
4878 linenr_T lnum = curbuf->b_op_start.lnum;
4879 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004880 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 size_t l;
4882
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004883 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884 for (;;)
4885 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004886 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 if (l == 0)
4888 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004889 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890 /* NL -> NUL translation */
4891 len = write(toshell_fd, "", (size_t)1);
4892 else
4893 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004894 char_u *s = vim_strchr(lp + written, NL);
4895
Bram Moolenaar89d40322006-08-29 15:30:07 +00004896 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004897 s == NULL ? l
4898 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004899 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004900 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 {
4902 /* Finished a line, add a NL, unless this line
4903 * should not have one. */
4904 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004905 || (!curbuf->b_p_bin
4906 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004907 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 && (lnum !=
4909 curbuf->b_ml.ml_line_count
4910 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004911 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004912 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 ++lnum;
4914 if (lnum > curbuf->b_op_end.lnum)
4915 {
4916 /* finished all the lines, close pipe */
4917 close(toshell_fd);
4918 toshell_fd = -1;
4919 break;
4920 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004921 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922 written = 0;
4923 }
4924 else if (len > 0)
4925 written += len;
4926 }
4927 _exit(0);
4928 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004929 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 {
4931 close(toshell_fd);
4932 toshell_fd = -1;
4933 }
4934 }
4935
4936 if (options & SHELL_READ)
4937 ga_init2(&ga, 1, BUFLEN);
4938
4939 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004940# ifdef ELAPSED_FUNC
4941 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 for (;;)
4944 {
4945 /*
4946 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004947 * if there are any.
4948 * Don't do this if we are expanding wild cards (would eat
4949 * typeahead).
4950 * Don't do this when filtering and terminal is in cooked
4951 * mode, the shell command will handle the I/O. Avoids
4952 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004953 * Don't get characters when the child has already
4954 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004955 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004956 * while (noread_cnt > 4), avoids that ":r !ls" eats
4957 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
4959 len = 0;
4960 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004961 && ((options &
4962 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4963 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004964# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004965 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004966# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004967 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004968 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004969 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004971 if (ta_len == 0)
4972 {
4973 /* Get extra characters when we don't have any.
4974 * Reset the counter and timer. */
4975 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004976# ifdef ELAPSED_FUNC
4977 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004978# endif
4979 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4980 }
4981 if (ta_len > 0 || len > 0)
4982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 /*
4984 * For pipes:
4985 * Check for CTRL-C: send interrupt signal to child.
4986 * Check for CTRL-D: EOF, close pipe to child.
4987 */
4988 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 /*
4991 * Send SIGINT to the child's group or all
4992 * processes in our group.
4993 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004994 may_send_sigint(ta_buf[ta_len], pid, wpid);
4995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 if (pty_master_fd < 0 && toshell_fd >= 0
4997 && ta_buf[ta_len] == Ctrl_D)
4998 {
4999 close(toshell_fd);
5000 toshell_fd = -1;
5001 }
5002 }
5003
5004 /* replace K_BS by <BS> and K_DEL by <DEL> */
5005 for (i = ta_len; i < ta_len + len; ++i)
5006 {
5007 if (ta_buf[i] == CSI && len - i > 2)
5008 {
5009 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5010 if (c == K_DEL || c == K_KDEL || c == K_BS)
5011 {
5012 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5013 (size_t)(len - i - 2));
5014 if (c == K_DEL || c == K_KDEL)
5015 ta_buf[i] = DEL;
5016 else
5017 ta_buf[i] = Ctrl_H;
5018 len -= 2;
5019 }
5020 }
5021 else if (ta_buf[i] == '\r')
5022 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005023# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005025 i += (*mb_ptr2len_len)(ta_buf + i,
5026 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029
5030 /*
5031 * For pipes: echo the typed characters.
5032 * For a pty this does not seem to work.
5033 */
5034 if (pty_master_fd < 0)
5035 {
5036 for (i = ta_len; i < ta_len + len; ++i)
5037 {
5038 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5039 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005040# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 else if (has_mbyte)
5042 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005043 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 msg_outtrans_len(ta_buf + i, l);
5046 i += l - 1;
5047 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 else
5050 msg_outtrans_len(ta_buf + i, 1);
5051 }
5052 windgoto(msg_row, msg_col);
5053 out_flush();
5054 }
5055
5056 ta_len += len;
5057
5058 /*
5059 * Write the characters to the child, unless EOF has
5060 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005061 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005062 * When writing buffer lines, drop the typed
5063 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005065 if (options & SHELL_WRITE)
5066 ta_len = 0;
5067 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5070 if (len > 0)
5071 {
5072 ta_len -= len;
5073 mch_memmove(ta_buf, ta_buf + len, ta_len);
5074 }
5075 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078
Bram Moolenaardf177f62005-02-22 08:39:57 +00005079 if (got_int)
5080 {
5081 /* CTRL-C sends a signal to the child, we ignore it
5082 * ourselves */
5083# ifdef HAVE_SETSID
5084 kill(-pid, SIGINT);
5085# else
5086 kill(0, SIGINT);
5087# endif
5088 if (wpid > 0)
5089 kill(wpid, SIGINT);
5090 got_int = FALSE;
5091 }
5092
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /*
5094 * Check if the child has any characters to be printed.
5095 * Read them and write them to our window. Repeat this as
5096 * long as there is something to do, avoid the 10ms wait
5097 * for mch_inchar(), or sending typeahead characters to
5098 * the external process.
5099 * TODO: This should handle escape sequences, compatible
5100 * to some terminal (vt52?).
5101 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005102 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005103 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005105 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005106# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005108# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 );
5112 if (len <= 0) /* end of file or error */
5113 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005114
5115 noread_cnt = 0;
5116 if (options & SHELL_READ)
5117 {
5118 /* Do NUL -> NL translation, append NL separated
5119 * lines to the current buffer. */
5120 for (i = 0; i < len; ++i)
5121 {
5122 if (buffer[i] == NL)
5123 append_ga_line(&ga);
5124 else if (buffer[i] == NUL)
5125 ga_append(&ga, NL);
5126 else
5127 ga_append(&ga, buffer[i]);
5128 }
5129 }
5130# ifdef FEAT_MBYTE
5131 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005134 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
Bram Moolenaardf177f62005-02-22 08:39:57 +00005136 len += buffer_off;
5137 buffer[len] = NUL;
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 /* Check if the last character in buffer[] is
5140 * incomplete, keep these bytes for the next
5141 * round. */
5142 for (p = buffer; p < buffer + len; p += l)
5143 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005144 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (l == 0)
5146 l = 1; /* NUL byte? */
5147 else if (MB_BYTE2LEN(*p) != l)
5148 break;
5149 }
5150 if (p == buffer) /* no complete character */
5151 {
5152 /* avoid getting stuck at an illegal byte */
5153 if (len >= 12)
5154 ++p;
5155 else
5156 {
5157 buffer_off = len;
5158 continue;
5159 }
5160 }
5161 c = *p;
5162 *p = NUL;
5163 msg_puts(buffer);
5164 if (p < buffer + len)
5165 {
5166 *p = c;
5167 buffer_off = (buffer + len) - p;
5168 mch_memmove(buffer, p, buffer_off);
5169 continue;
5170 }
5171 buffer_off = 0;
5172 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005173# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 buffer[len] = NUL;
5177 msg_puts(buffer);
5178 }
5179
5180 windgoto(msg_row, msg_col);
5181 cursor_on();
5182 out_flush();
5183 if (got_int)
5184 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005185
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005186# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005187 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005188 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005189 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005190
5191 /* Avoid that we keep looping here without
5192 * checking for a CTRL-C for a long time. Don't
5193 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005194 if (msec > 2000)
5195 {
5196 noread_cnt = 5;
5197 break;
5198 }
5199 }
5200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005203 /* If we already detected the child has finished, continue
5204 * reading output for a short while. Some text may be
5205 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005206 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005207 {
5208 if (noread_cnt < 5)
5209 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005210 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005211 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 /*
5214 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005215 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005217# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005218 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005219# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5223 || (wait_pid == pid && WIFEXITED(status)))
5224 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005225 /* Don't break the loop yet, try reading more
5226 * characters from "fromshell_fd" first. When using
5227 * pipes there might still be something to read and
5228 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005231 else
5232 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005233
Bram Moolenaar95a51352013-03-21 22:53:50 +01005234# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005235 /* Handle any X events, e.g. serving the clipboard. */
5236 clip_update();
5237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239finished:
5240 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005241 if (options & SHELL_READ)
5242 {
5243 if (ga.ga_len > 0)
5244 {
5245 append_ga_line(&ga);
5246 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005247 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005248 }
5249 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005250 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005251 ga_clear(&ga);
5252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 /*
5255 * Give all typeahead that wasn't used back to ui_inchar().
5256 */
5257 if (ta_len)
5258 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 State = old_State;
5260 if (toshell_fd >= 0)
5261 close(toshell_fd);
5262 close(fromshell_fd);
5263 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005264# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005265 else
5266 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005267 long delay_msec = 1;
5268
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005269 /*
5270 * Similar to the loop above, but only handle X events, no
5271 * I/O.
5272 */
5273 for (;;)
5274 {
5275 if (got_int)
5276 {
5277 /* CTRL-C sends a signal to the child, we ignore it
5278 * ourselves */
5279# ifdef HAVE_SETSID
5280 kill(-pid, SIGINT);
5281# else
5282 kill(0, SIGINT);
5283# endif
5284 got_int = FALSE;
5285 }
5286# ifdef __NeXT__
5287 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5288# else
5289 wait_pid = waitpid(pid, &status, WNOHANG);
5290# endif
5291 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5292 || (wait_pid == pid && WIFEXITED(status)))
5293 {
5294 wait_pid = pid;
5295 break;
5296 }
5297
5298 /* Handle any X events, e.g. serving the clipboard. */
5299 clip_update();
5300
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005301 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5302 * less time. */
5303 mch_delay(delay_msec, TRUE);
5304 if (++delay_msec > 10)
5305 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005306 }
5307 }
5308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
5310 /*
5311 * Wait until our child has exited.
5312 * Ignore wait() returning pids of other children and returning
5313 * because of some signal like SIGWINCH.
5314 * Don't wait if wait_pid was already set above, indicating the
5315 * child already exited.
5316 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005317 if (wait_pid != pid)
5318 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar624891f2010-10-13 16:22:09 +02005320# ifdef FEAT_GUI
5321 /* Close slave side of pty. Only do this after the child has
5322 * exited, otherwise the child may hang when it tries to write on
5323 * the pty. */
5324 if (pty_master_fd >= 0)
5325 close(pty_slave_fd);
5326# endif
5327
Bram Moolenaardf177f62005-02-22 08:39:57 +00005328 /* Make sure the child that writes to the external program is
5329 * dead. */
5330 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005331 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005332 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005333 wait4pid(wpid, NULL);
5334 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005335
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005336# ifdef FEAT_JOB_CHANNEL
5337 --dont_check_job_ended;
5338# endif
5339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 /*
5341 * Set to raw mode right now, otherwise a CTRL-C after
5342 * catch_signals() will kill Vim.
5343 */
5344 if (tmode == TMODE_RAW)
5345 settmode(TMODE_RAW);
5346 did_settmode = TRUE;
5347 set_signals();
5348
5349 if (WIFEXITED(status))
5350 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005351 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005353 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 if (retval == EXEC_FAILED)
5356 {
5357 MSG_PUTS(_("\nCannot execute shell "));
5358 msg_outtrans(p_sh);
5359 msg_putchar('\n');
5360 }
5361 else if (!(options & SHELL_SILENT))
5362 {
5363 MSG_PUTS(_("\nshell returned "));
5364 msg_outnum((long)retval);
5365 msg_putchar('\n');
5366 }
5367 }
5368 }
5369 else
5370 MSG_PUTS(_("\nCommand terminated\n"));
5371 }
5372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
5374error:
5375 if (!did_settmode)
5376 if (tmode == TMODE_RAW)
5377 settmode(TMODE_RAW); /* set to raw mode */
5378# ifdef FEAT_TITLE
5379 resettitle();
5380# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005381 vim_free(argv);
5382 vim_free(tofree1);
5383 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005388
5389 int
5390mch_call_shell(
5391 char_u *cmd,
5392 int options) /* SHELL_*, see vim.h */
5393{
5394#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5395 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5396 return mch_call_shell_terminal(cmd, options);
5397#endif
5398#ifdef USE_SYSTEM
5399 return mch_call_shell_system(cmd, options);
5400#else
5401 return mch_call_shell_fork(cmd, options);
5402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403}
5404
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005405#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005406 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005407mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005408{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005409 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005410 int fd_in[2] = {-1, -1}; /* for stdin */
5411 int fd_out[2] = {-1, -1}; /* for stdout */
5412 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005413 int pty_master_fd = -1;
5414 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005415 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005416 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5417 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5418 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005419 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005420 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5421 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005422 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005423 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005424 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005425
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005426 if (use_out_for_err && use_null_for_out)
5427 use_null_for_err = TRUE;
5428
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005429 /* default is to fail */
5430 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005431
Bram Moolenaarb2412082017-08-20 18:09:14 +02005432 if (options->jo_pty
5433 && (!(use_file_for_in || use_null_for_in)
5434 || !(use_file_for_in || use_null_for_out)
5435 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005436 {
5437 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5438 if (job->jv_tty_out != NULL)
5439 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5440 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005441
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005442 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005443 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005444 if (use_file_for_in)
5445 {
5446 char_u *fname = options->jo_io_name[PART_IN];
5447
5448 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5449 if (fd_in[0] < 0)
5450 {
5451 EMSG2(_(e_notopen), fname);
5452 goto failed;
5453 }
5454 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005455 else
5456 /* When writing buffer lines to the input don't use the pty, so that
5457 * the pipe can be closed when all lines were written. */
5458 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5459 && pipe(fd_in) < 0)
5460 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005461
5462 if (use_file_for_out)
5463 {
5464 char_u *fname = options->jo_io_name[PART_OUT];
5465
5466 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5467 if (fd_out[1] < 0)
5468 {
5469 EMSG2(_(e_notopen), fname);
5470 goto failed;
5471 }
5472 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005473 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005474 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005475
5476 if (use_file_for_err)
5477 {
5478 char_u *fname = options->jo_io_name[PART_ERR];
5479
5480 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5481 if (fd_err[1] < 0)
5482 {
5483 EMSG2(_(e_notopen), fname);
5484 goto failed;
5485 }
5486 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005487 else if (!use_out_for_err && !use_null_for_err
5488 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005489 goto failed;
5490
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005491 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5492 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005493 if (options->jo_set & JO_CHANNEL)
5494 {
5495 channel = options->jo_channel;
5496 if (channel != NULL)
5497 ++channel->ch_refcount;
5498 }
5499 else
5500 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005501 if (channel == NULL)
5502 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005503 if (job->jv_tty_out != NULL)
5504 ch_log(channel, "using pty %s on fd %d",
5505 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005506 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005507
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005508 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005509 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005510 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005511 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005512 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005513 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005514 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005515 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005516 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005518 int null_fd = -1;
5519 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005520
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 /* child */
5522 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005523 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005524
Bram Moolenaar819524702018-02-27 19:10:00 +01005525# ifdef FEAT_JOB_CHANNEL
5526 if (ch_log_active())
5527 /* close the log file in the child */
5528 ch_logfile((char_u *)"", (char_u *)"");
5529# endif
5530
Bram Moolenaar835dc632016-02-07 14:27:38 +01005531# ifdef HAVE_SETSID
5532 /* Create our own process group, so that the child and all its
5533 * children can be kill()ed. Don't do this when using pipes,
5534 * because stdin is not a tty, we would lose /dev/tty. */
5535 (void)setsid();
5536# endif
5537
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005538# ifdef FEAT_TERMINAL
5539 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005540 {
5541 char *term = (char *)T_NAME;
5542
5543#ifdef FEAT_GUI
5544 if (term_is_gui(T_NAME))
5545 /* In the GUI 'term' is not what we want, use $TERM. */
5546 term = getenv("TERM");
5547#endif
5548 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5549 * back to "xterm". */
5550 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5551 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005552 set_child_environment(
5553 (long)options->jo_term_rows,
5554 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005555 term,
5556 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005557 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005558 else
5559# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005560 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005561
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005562 if (options->jo_env != NULL)
5563 {
5564 dict_T *dict = options->jo_env;
5565 hashitem_T *hi;
5566 int todo = (int)dict->dv_hashtab.ht_used;
5567
5568 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5569 if (!HASHITEM_EMPTY(hi))
5570 {
5571 typval_T *item = &dict_lookup(hi)->di_tv;
5572
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005573 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005574 --todo;
5575 }
5576 }
5577
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005578 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005579 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005580 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005581 if (null_fd < 0)
5582 {
5583 perror("opening /dev/null failed");
5584 _exit(OPEN_NULL_FAILED);
5585 }
5586 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005587
Bram Moolenaar223896d2017-08-02 22:33:28 +02005588 if (pty_slave_fd >= 0)
5589 {
5590 /* push stream discipline modules */
5591 SetupSlavePTY(pty_slave_fd);
5592# ifdef TIOCSCTTY
5593 /* Try to become controlling tty (probably doesn't work,
5594 * unless run by root) */
5595 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5596# endif
5597 }
5598
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005599 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005600 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005601 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005602 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005603 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005604 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005606 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005607
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005608 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005609 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005610 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005611 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005612 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005613 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005614 }
5615 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005616 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005617 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005618 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005619 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005620 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005621
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005622 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005623 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005624 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005625 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005626 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005627 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005628 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005629 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005630
5631 if (fd_in[0] >= 0)
5632 close(fd_in[0]);
5633 if (fd_in[1] >= 0)
5634 close(fd_in[1]);
5635 if (fd_out[0] >= 0)
5636 close(fd_out[0]);
5637 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005638 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005639 if (fd_err[0] >= 0)
5640 close(fd_err[0]);
5641 if (fd_err[1] >= 0)
5642 close(fd_err[1]);
5643 if (pty_master_fd >= 0)
5644 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005645 close(pty_master_fd); /* not used in the child */
5646 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005647 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005648
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005649 if (null_fd >= 0)
5650 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005651
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005652 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5653 _exit(EXEC_FAILED);
5654
Bram Moolenaar835dc632016-02-07 14:27:38 +01005655 /* See above for type of argv. */
5656 execvp(argv[0], argv);
5657
Bram Moolenaar4694a172016-04-21 14:05:23 +02005658 if (stderr_works)
5659 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005660# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005661 /* calling free_all_mem() here causes problems. Ignore valgrind
5662 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005663# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005664 _exit(EXEC_FAILED); /* exec failed, return failure code */
5665 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005666
5667 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005668 UNBLOCK_SIGNALS(&curset);
5669
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005670 job->jv_pid = pid;
5671 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005672 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005673
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005674 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005675 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005676 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005677 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005678 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005679 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005680 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005681 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005682 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005683 if (channel != NULL)
5684 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005685 int in_fd = use_file_for_in || use_null_for_in
5686 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5687 int out_fd = use_file_for_out || use_null_for_out
5688 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005689 /* When using pty_master_fd only set it for stdout, do not duplicate it
5690 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005691 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005692 ? INVALID_FD
5693 : fd_err[0] >= 0
5694 ? fd_err[0]
5695 : (out_fd == pty_master_fd
5696 ? INVALID_FD
5697 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005698
5699 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005700 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005701 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005702 else
5703 {
5704 if (fd_in[1] >= 0)
5705 close(fd_in[1]);
5706 if (fd_out[0] >= 0)
5707 close(fd_out[0]);
5708 if (fd_err[0] >= 0)
5709 close(fd_err[0]);
5710 if (pty_master_fd >= 0)
5711 close(pty_master_fd);
5712 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005713
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005714 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 return;
5716
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005717failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005718 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005720 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005721 if (fd_in[1] >= 0)
5722 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005723 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005724 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005725 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005727 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005728 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005729 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005730 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005731 if (pty_master_fd >= 0)
5732 close(pty_master_fd);
5733 if (pty_slave_fd >= 0)
5734 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005735}
5736
5737 char *
5738mch_job_status(job_T *job)
5739{
5740# ifdef HAVE_UNION_WAIT
5741 union wait status;
5742# else
5743 int status = -1;
5744# endif
5745 pid_t wait_pid = 0;
5746
5747# ifdef __NeXT__
5748 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5749# else
5750 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5751# endif
5752 if (wait_pid == -1)
5753 {
5754 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005755 if (job->jv_status < JOB_ENDED)
5756 ch_log(job->jv_channel, "Job no longer exists: %s",
5757 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005758 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005759 }
5760 if (wait_pid == 0)
5761 return "run";
5762 if (WIFEXITED(status))
5763 {
5764 /* LINTED avoid "bitwise operation on signed value" */
5765 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005766 if (job->jv_status < JOB_ENDED)
5767 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005768 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005770 if (WIFSIGNALED(status))
5771 {
5772 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005773 if (job->jv_status < JOB_ENDED)
5774 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005775 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005776 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005777 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005778
5779return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005780 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005781 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005782 return "dead";
5783}
5784
5785 job_T *
5786mch_detect_ended_job(job_T *job_list)
5787{
5788# ifdef HAVE_UNION_WAIT
5789 union wait status;
5790# else
5791 int status = -1;
5792# endif
5793 pid_t wait_pid = 0;
5794 job_T *job;
5795
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005796# ifndef USE_SYSTEM
5797 /* Do not do this when waiting for a shell command to finish, we would get
5798 * the exit value here (and discard it), the exit value obtained there
5799 * would then be wrong. */
5800 if (dont_check_job_ended > 0)
5801 return NULL;
5802# endif
5803
Bram Moolenaar97792de2016-10-15 18:36:49 +02005804# ifdef __NeXT__
5805 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5806# else
5807 wait_pid = waitpid(-1, &status, WNOHANG);
5808# endif
5809 if (wait_pid <= 0)
5810 /* no process ended */
5811 return NULL;
5812 for (job = job_list; job != NULL; job = job->jv_next)
5813 {
5814 if (job->jv_pid == wait_pid)
5815 {
5816 if (WIFEXITED(status))
5817 /* LINTED avoid "bitwise operation on signed value" */
5818 job->jv_exitval = WEXITSTATUS(status);
5819 else if (WIFSIGNALED(status))
5820 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005821 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005822 {
5823 ch_log(job->jv_channel, "Job ended");
5824 job->jv_status = JOB_ENDED;
5825 }
5826 return job;
5827 }
5828 }
5829 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005830}
5831
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005832/*
5833 * Send a (deadly) signal to "job".
5834 * Return FAIL if "how" is not a valid name.
5835 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005836 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005837mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005838{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005839 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840
Bram Moolenaar923d9262016-02-25 20:56:01 +01005841 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005842 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005843 else if (STRCMP(how, "hup") == 0)
5844 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005845 else if (STRCMP(how, "quit") == 0)
5846 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005847 else if (STRCMP(how, "int") == 0)
5848 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849 else if (STRCMP(how, "kill") == 0)
5850 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005851#ifdef SIGWINCH
5852 else if (STRCMP(how, "winch") == 0)
5853 sig = SIGWINCH;
5854#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 else if (isdigit(*how))
5856 sig = atoi((char *)how);
5857 else
5858 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005859
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005860 // Never kill ourselves!
5861 if (job->jv_pid != 0)
5862 {
5863 // TODO: have an option to only kill the process, not the group?
5864 kill(-job->jv_pid, sig);
5865 kill(job->jv_pid, sig);
5866 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005867
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868 return OK;
5869}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005870
5871/*
5872 * Clear the data related to "job".
5873 */
5874 void
5875mch_clear_job(job_T *job)
5876{
5877 /* call waitpid because child process may become zombie */
5878# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005879 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005880# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005881 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005882# endif
5883}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005884#endif
5885
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005886#if defined(FEAT_TERMINAL) || defined(PROTO)
5887 int
5888mch_create_pty_channel(job_T *job, jobopt_T *options)
5889{
5890 int pty_master_fd = -1;
5891 int pty_slave_fd = -1;
5892 channel_T *channel;
5893
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005894 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5895 if (job->jv_tty_out != NULL)
5896 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005897 close(pty_slave_fd);
5898
5899 channel = add_channel();
5900 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005901 {
5902 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005903 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005904 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005905 if (job->jv_tty_out != NULL)
5906 ch_log(channel, "using pty %s on fd %d",
5907 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005908 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5909 channel->ch_keep_open = TRUE;
5910
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005911 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5912 * it only needs to be read once. */
5913 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005914 channel_set_job(channel, job, options);
5915 return OK;
5916}
5917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919/*
5920 * Check for CTRL-C typed by reading all available characters.
5921 * In cooked mode we should get SIGINT, no need to check.
5922 */
5923 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005924mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005926 if ((curr_tmode == TMODE_RAW || force)
5927 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 fill_input_buf(FALSE);
5929}
5930
5931/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005932 * Wait "msec" msec until a character is available from the mouse, keyboard,
5933 * from inbuf[].
5934 * "msec" == -1 will block forever.
5935 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005936 * When "ignore_input" is TRUE even check for pending input when input is
5937 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005938 * "interrupted" (if not NULL) is set to TRUE when no character is available
5939 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005940 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005941 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 */
5943 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005944WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005946#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005947 return ui_wait_for_chars_or_timer(
5948 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005949#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005950 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005951#endif
5952}
5953
5954/*
5955 * Wait "msec" msec until a character is available from the mouse or keyboard
5956 * or from inbuf[].
5957 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005958 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005959 * "interrupted" (if not NULL) is set to TRUE when no character is available
5960 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005961 * When a GUI is being used, this will never get called -- webb
5962 */
5963 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005964WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005965{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966#ifdef FEAT_MOUSE_GPM
5967 int gpm_process_wanted;
5968#endif
5969#ifdef FEAT_XCLIPBOARD
5970 int rest;
5971#endif
5972 int avail;
5973
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005974 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 return 1;
5976
5977#if defined(FEAT_MOUSE_DEC)
5978 /* May need to query the mouse position. */
5979 if (WantQueryMouse)
5980 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005981 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5983 }
5984#endif
5985
5986 /*
5987 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5988 * events. This is a bit complicated, because they might both be defined.
5989 */
5990#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5991# ifdef FEAT_XCLIPBOARD
5992 rest = 0;
5993 if (do_xterm_trace())
5994 rest = msec;
5995# endif
5996 do
5997 {
5998# ifdef FEAT_XCLIPBOARD
5999 if (rest != 0)
6000 {
6001 msec = XT_TRACE_DELAY;
6002 if (rest >= 0 && rest < XT_TRACE_DELAY)
6003 msec = rest;
6004 if (rest >= 0)
6005 rest -= msec;
6006 }
6007# endif
6008# ifdef FEAT_MOUSE_GPM
6009 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006010 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006011 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006013 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014# endif
6015 if (!avail)
6016 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006017 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 return 1;
6019# ifdef FEAT_XCLIPBOARD
6020 if (rest == 0 || !do_xterm_trace())
6021# endif
6022 break;
6023 }
6024 }
6025 while (FALSE
6026# ifdef FEAT_MOUSE_GPM
6027 || (gpm_process_wanted && mch_gpm_process() == 0)
6028# endif
6029# ifdef FEAT_XCLIPBOARD
6030 || (!avail && rest != 0)
6031# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006032 )
6033 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006036 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037#endif
6038 return avail;
6039}
6040
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006041#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042/*
6043 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006044 * "msec" == 0 will check for characters once.
6045 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006048 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006049 * "interrupted" (if not NULL) is set to TRUE when no character is available
6050 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052#if defined(__BEOS__)
6053 int
6054#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006055 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006057RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006060 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006061#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 static int busy = FALSE;
6063
6064 /* May retry getting characters after an event was handled. */
6065# define MAY_LOOP
6066
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006067# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 /* Remember at what time we started, so that we know how much longer we
6069 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006070 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006071 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006073 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006074 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075# endif
6076
6077 /* Handle being called recursively. This may happen for the session
6078 * manager stuff, it may save the file, which does a breakcheck. */
6079 if (busy)
6080 return 0;
6081#endif
6082
6083#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006084 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085#endif
6086 {
6087#ifdef MAY_LOOP
6088 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006089# ifdef FEAT_MZSCHEME
6090 int mzquantum_used = FALSE;
6091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092#endif
6093#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006094 /* each channel may use in, out and err */
6095 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 int nfd;
6097# ifdef FEAT_XCLIPBOARD
6098 int xterm_idx = -1;
6099# endif
6100# ifdef FEAT_MOUSE_GPM
6101 int gpm_idx = -1;
6102# endif
6103# ifdef USE_XSMP
6104 int xsmp_idx = -1;
6105# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006106 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006108# ifdef FEAT_MZSCHEME
6109 mzvim_check_threads();
6110 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6111 {
6112 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6113 mzquantum_used = TRUE;
6114 }
6115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 fds[0].fd = fd;
6117 fds[0].events = POLLIN;
6118 nfd = 1;
6119
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006121 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (xterm_Shell != (Widget)0)
6123 {
6124 xterm_idx = nfd;
6125 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6126 fds[nfd].events = POLLIN;
6127 nfd++;
6128 }
6129# endif
6130# ifdef FEAT_MOUSE_GPM
6131 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6132 {
6133 gpm_idx = nfd;
6134 fds[nfd].fd = gpm_fd;
6135 fds[nfd].events = POLLIN;
6136 nfd++;
6137 }
6138# endif
6139# ifdef USE_XSMP
6140 if (xsmp_icefd != -1)
6141 {
6142 xsmp_idx = nfd;
6143 fds[nfd].fd = xsmp_icefd;
6144 fds[nfd].events = POLLIN;
6145 nfd++;
6146 }
6147# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006148#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006149 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006150#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006151 if (interrupted != NULL)
6152 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006154 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006155
6156 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 if (result == 0 && interrupted != NULL && ret > 0)
6158 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006159
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006160# ifdef FEAT_MZSCHEME
6161 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006162 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006163 finished = FALSE;
6164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166# ifdef FEAT_XCLIPBOARD
6167 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6168 {
6169 xterm_update(); /* Maybe we should hand out clipboard */
6170 if (--ret == 0 && !input_available())
6171 /* Try again */
6172 finished = FALSE;
6173 }
6174# endif
6175# ifdef FEAT_MOUSE_GPM
6176 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6177 {
6178 *check_for_gpm = 1;
6179 }
6180# endif
6181# ifdef USE_XSMP
6182 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6183 {
6184 if (fds[xsmp_idx].revents & POLLIN)
6185 {
6186 busy = TRUE;
6187 xsmp_handle_requests();
6188 busy = FALSE;
6189 }
6190 else if (fds[xsmp_idx].revents & POLLHUP)
6191 {
6192 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006193 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 xsmp_close();
6195 }
6196 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006197 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006200#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006201 /* also call when ret == 0, we may be polling a keep-open channel */
6202 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006203 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206#else /* HAVE_SELECT */
6207
6208 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006209 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006210 // These are static because they can take 8 Kbyte each and cause the
6211 // signal stack to run out with -O3.
6212 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006214 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006216# ifdef FEAT_MZSCHEME
6217 mzvim_check_threads();
6218 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6219 {
6220 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6221 mzquantum_used = TRUE;
6222 }
6223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006225 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006227 tv.tv_sec = towait / 1000;
6228 tv.tv_usec = (towait % 1000) * (1000000/1000);
6229 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 else
6232 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 /*
6235 * Select on ready for reading and exceptional condition (end of file).
6236 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006237select_eintr:
6238 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006239 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 FD_ZERO(&efds);
6241 FD_SET(fd, &rfds);
6242# if !defined(__QNX__) && !defined(__CYGWIN32__)
6243 /* For QNX select() always returns 1 if this is set. Why? */
6244 FD_SET(fd, &efds);
6245# endif
6246 maxfd = fd;
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006249 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (xterm_Shell != (Widget)0)
6251 {
6252 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6253 if (maxfd < ConnectionNumber(xterm_dpy))
6254 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006255
6256 /* An event may have already been read but not handled. In
6257 * particulary, XFlush may cause this. */
6258 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260# endif
6261# ifdef FEAT_MOUSE_GPM
6262 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6263 {
6264 FD_SET(gpm_fd, &rfds);
6265 FD_SET(gpm_fd, &efds);
6266 if (maxfd < gpm_fd)
6267 maxfd = gpm_fd;
6268 }
6269# endif
6270# ifdef USE_XSMP
6271 if (xsmp_icefd != -1)
6272 {
6273 FD_SET(xsmp_icefd, &rfds);
6274 FD_SET(xsmp_icefd, &efds);
6275 if (maxfd < xsmp_icefd)
6276 maxfd = xsmp_icefd;
6277 }
6278# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006279# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006280 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006281# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006282 if (interrupted != NULL)
6283 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
Bram Moolenaar643b6142018-09-12 20:29:09 +02006285 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6286 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006287 result = ret > 0 && FD_ISSET(fd, &rfds);
6288 if (result)
6289 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006290 else if (interrupted != NULL && ret > 0)
6291 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006292
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006293# ifdef EINTR
6294 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006295 {
6296 /* Check whether window has been resized, EINTR may be caused by
6297 * SIGWINCH. */
6298 if (do_resize)
6299 handle_resize();
6300
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006301 /* Interrupted by a signal, need to try again. We ignore msec
6302 * here, because we do want to check even after a timeout if
6303 * characters are available. Needed for reading output of an
6304 * external command after the process has finished. */
6305 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006306 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006307# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006308# ifdef __TANDEM
6309 if (ret == -1 && errno == ENOTSUP)
6310 {
6311 FD_ZERO(&rfds);
6312 FD_ZERO(&efds);
6313 ret = 0;
6314 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006315# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006316# ifdef FEAT_MZSCHEME
6317 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006318 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006319 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320# endif
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322# ifdef FEAT_XCLIPBOARD
6323 if (ret > 0 && xterm_Shell != (Widget)0
6324 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6325 {
6326 xterm_update(); /* Maybe we should hand out clipboard */
6327 /* continue looping when we only got the X event and the input
6328 * buffer is empty */
6329 if (--ret == 0 && !input_available())
6330 {
6331 /* Try again */
6332 finished = FALSE;
6333 }
6334 }
6335# endif
6336# ifdef FEAT_MOUSE_GPM
6337 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6338 {
6339 if (FD_ISSET(gpm_fd, &efds))
6340 gpm_close();
6341 else if (FD_ISSET(gpm_fd, &rfds))
6342 *check_for_gpm = 1;
6343 }
6344# endif
6345# ifdef USE_XSMP
6346 if (ret > 0 && xsmp_icefd != -1)
6347 {
6348 if (FD_ISSET(xsmp_icefd, &efds))
6349 {
6350 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006351 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 xsmp_close();
6353 if (--ret == 0)
6354 finished = FALSE; /* keep going if event was only one */
6355 }
6356 else if (FD_ISSET(xsmp_icefd, &rfds))
6357 {
6358 busy = TRUE;
6359 xsmp_handle_requests();
6360 busy = FALSE;
6361 if (--ret == 0)
6362 finished = FALSE; /* keep going if event was only one */
6363 }
6364 }
6365# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006366#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006367 /* also call when ret == 0, we may be polling a keep-open channel */
6368 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006369 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372#endif /* HAVE_SELECT */
6373
6374#ifdef MAY_LOOP
6375 if (finished || msec == 0)
6376 break;
6377
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006378# ifdef FEAT_CLIENTSERVER
6379 if (server_waiting())
6380 break;
6381# endif
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 /* We're going to loop around again, find out for how long */
6384 if (msec > 0)
6385 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006386# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006388 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389# else
6390 /* Guess we got interrupted halfway. */
6391 msec = msec / 2;
6392# endif
6393 if (msec <= 0)
6394 break; /* waited long enough */
6395 }
6396#endif
6397 }
6398
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006399 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400}
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006404 * Expand a path into all matching files and/or directories. Handles "*",
6405 * "?", "[a-z]", "**", etc.
6406 * "path" has backslashes before chars that are not to be expanded.
6407 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
6409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006410mch_expandpath(
6411 garray_T *gap,
6412 char_u *path,
6413 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
Bram Moolenaar02743632005-07-25 20:42:36 +00006415 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416}
6417#endif
6418
6419/*
6420 * mch_expand_wildcards() - this code does wild-card pattern matching using
6421 * the shell
6422 *
6423 * return OK for success, FAIL for error (you may lose some memory) and put
6424 * an error message in *file.
6425 *
6426 * num_pat is number of input patterns
6427 * pat is array of pointers to input patterns
6428 * num_file is pointer to number of matched file names
6429 * file is pointer to array of pointers to matched file names
6430 */
6431
6432#ifndef SEEK_SET
6433# define SEEK_SET 0
6434#endif
6435#ifndef SEEK_END
6436# define SEEK_END 2
6437#endif
6438
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006439#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006442mch_expand_wildcards(
6443 int num_pat,
6444 char_u **pat,
6445 int *num_file,
6446 char_u ***file,
6447 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 int i;
6450 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006451 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 char_u *p;
6453 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
Bram Moolenaarc7247912008-01-13 12:54:11 +00006455 /*
6456 * This is the non-OS/2 implementation (really Unix).
6457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 int j;
6459 char_u *tempname;
6460 char_u *command;
6461 FILE *fd;
6462 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006463#define STYLE_ECHO 0 /* use "echo", the default */
6464#define STYLE_GLOB 1 /* use "glob", for csh */
6465#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6466#define STYLE_PRINT 3 /* use "print -N", for zsh */
6467#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6468 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 int shell_style = STYLE_ECHO;
6470 int check_spaces;
6471 static int did_find_nul = FALSE;
6472 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006473 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006474 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
6476 *num_file = 0; /* default: no files found */
6477 *file = NULL;
6478
6479 /*
6480 * If there are no wildcards, just copy the names to allocated memory.
6481 * Saves a lot of time, because we don't have to start a new shell.
6482 */
6483 if (!have_wildcard(num_pat, pat))
6484 return save_patterns(num_pat, pat, num_file, file);
6485
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006486# ifdef HAVE_SANDBOX
6487 /* Don't allow any shell command in the sandbox. */
6488 if (sandbox != 0 && check_secure())
6489 return FAIL;
6490# endif
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 /*
6493 * Don't allow the use of backticks in secure and restricted mode.
6494 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006495 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 for (i = 0; i < num_pat; ++i)
6497 if (vim_strchr(pat[i], '`') != NULL
6498 && (check_restricted() || check_secure()))
6499 return FAIL;
6500
6501 /*
6502 * get a name for the temp file
6503 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006504 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 EMSG(_(e_notmp));
6507 return FAIL;
6508 }
6509
6510 /*
6511 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006512 * file.
6513 * STYLE_BT: NL separated
6514 * If expanding `cmd` execute it directly.
6515 * STYLE_GLOB: NUL separated
6516 * If we use *csh, "glob" will work better than "echo".
6517 * STYLE_PRINT: NL or NUL separated
6518 * If we use *zsh, "print -N" will work better than "glob".
6519 * STYLE_VIMGLOB: NL separated
6520 * If we use *sh*, we define "vimglob()".
6521 * STYLE_ECHO: space separated.
6522 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 */
6524 if (num_pat == 1 && *pat[0] == '`'
6525 && (len = STRLEN(pat[0])) > 2
6526 && *(pat[0] + len - 1) == '`')
6527 shell_style = STYLE_BT;
6528 else if ((len = STRLEN(p_sh)) >= 3)
6529 {
6530 if (STRCMP(p_sh + len - 3, "csh") == 0)
6531 shell_style = STYLE_GLOB;
6532 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6533 shell_style = STYLE_PRINT;
6534 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006535 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6536 "sh") != NULL)
6537 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538
Bram Moolenaarc7247912008-01-13 12:54:11 +00006539 /* Compute the length of the command. We need 2 extra bytes: for the
6540 * optional '&' and for the NUL.
6541 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006543 if (shell_style == STYLE_VIMGLOB)
6544 len += STRLEN(sh_vimglob_func);
6545
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006546 for (i = 0; i < num_pat; ++i)
6547 {
6548 /* Count the length of the patterns in the same way as they are put in
6549 * "command" below. */
6550#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006552#else
6553 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006554 for (j = 0; pat[i][j] != NUL; ++j)
6555 {
6556 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6557 ++len; /* may add a backslash */
6558 ++len;
6559 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006560#endif
6561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 command = alloc(len);
6563 if (command == NULL)
6564 {
6565 /* out of memory */
6566 vim_free(tempname);
6567 return FAIL;
6568 }
6569
6570 /*
6571 * Build the shell command:
6572 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6573 * recognizes this).
6574 * - Add the shell command to print the expanded names.
6575 * - Add the temp file name.
6576 * - Add the file name patterns.
6577 */
6578 if (shell_style == STYLE_BT)
6579 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006580 /* change `command; command& ` to (command; command ) */
6581 STRCPY(command, "(");
6582 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006584 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006585 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 --p;
6587 if (*p == '&') /* remove trailing '&' */
6588 {
6589 ampersent = TRUE;
6590 *p = ' ';
6591 }
6592 STRCAT(command, ">");
6593 }
6594 else
6595 {
6596 if (flags & EW_NOTFOUND)
6597 STRCPY(command, "set nonomatch; ");
6598 else
6599 STRCPY(command, "unset nonomatch; ");
6600 if (shell_style == STYLE_GLOB)
6601 STRCAT(command, "glob >");
6602 else if (shell_style == STYLE_PRINT)
6603 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006604 else if (shell_style == STYLE_VIMGLOB)
6605 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 else
6607 STRCAT(command, "echo >");
6608 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006609
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (shell_style != STYLE_BT)
6613 for (i = 0; i < num_pat; ++i)
6614 {
6615 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006616 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006617 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618#ifdef USE_SYSTEM
6619 STRCAT(command, " \"");
6620 STRCAT(command, pat[i]);
6621 STRCAT(command, "\"");
6622#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006623 int intick = FALSE;
6624
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 p = command + STRLEN(command);
6626 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006627 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006628 {
6629 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006630 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006631 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6632 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006633 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006634 * backslash inside backticks, before a special character
6635 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006636 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006637 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6638 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006639 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006640 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006641 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006642 else if (!intick
6643 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6644 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006645 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006646 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6647 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006648 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006649
6650 /* Copy one character. */
6651 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 *p = NUL;
6654#endif
6655 }
6656 if (flags & EW_SILENT)
6657 show_shell_mess = FALSE;
6658 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006659 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 /*
6662 * Using zsh -G: If a pattern has no matches, it is just deleted from
6663 * the argument list, otherwise zsh gives an error message and doesn't
6664 * expand any other pattern.
6665 */
6666 if (shell_style == STYLE_PRINT)
6667 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6668
6669 /*
6670 * If we use -f then shell variables set in .cshrc won't get expanded.
6671 * vi can do it, so we will too, but it is only necessary if there is a "$"
6672 * in one of the patterns, otherwise we can still use the fast option.
6673 */
6674 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6675 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6676
6677 /*
6678 * execute the shell command
6679 */
6680 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6681
6682 /* When running in the background, give it some time to create the temp
6683 * file, but don't wait for it to finish. */
6684 if (ampersent)
6685 mch_delay(10L, TRUE);
6686
6687 extra_shell_arg = NULL; /* cleanup */
6688 show_shell_mess = TRUE;
6689 vim_free(command);
6690
Bram Moolenaarc7247912008-01-13 12:54:11 +00006691 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
6693 mch_remove(tempname);
6694 vim_free(tempname);
6695 /*
6696 * With interactive completion, the error message is not printed.
6697 * However with USE_SYSTEM, I don't know how to turn off error messages
6698 * from the shell, so screen may still get messed up -- webb.
6699 */
6700#ifndef USE_SYSTEM
6701 if (!(flags & EW_SILENT))
6702#endif
6703 {
6704 redraw_later_clear(); /* probably messed up screen */
6705 msg_putchar('\n'); /* clear bottom line quickly */
6706 cmdline_row = Rows - 1; /* continue on last line */
6707#ifdef USE_SYSTEM
6708 if (!(flags & EW_SILENT))
6709#endif
6710 {
6711 MSG(_(e_wildexpand));
6712 msg_start(); /* don't overwrite this message */
6713 }
6714 }
6715 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6716 * EW_NOTFOUND is given */
6717 if (shell_style == STYLE_BT)
6718 return FAIL;
6719 goto notfound;
6720 }
6721
6722 /*
6723 * read the names from the file into memory
6724 */
6725 fd = fopen((char *)tempname, READBIN);
6726 if (fd == NULL)
6727 {
6728 /* Something went wrong, perhaps a file name with a special char. */
6729 if (!(flags & EW_SILENT))
6730 {
6731 MSG(_(e_wildexpand));
6732 msg_start(); /* don't overwrite this message */
6733 }
6734 vim_free(tempname);
6735 goto notfound;
6736 }
6737 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006738 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006740 if (llen < 0)
6741 /* just in case ftell() would fail */
6742 buffer = NULL;
6743 else
6744 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (buffer == NULL)
6746 {
6747 /* out of memory */
6748 mch_remove(tempname);
6749 vim_free(tempname);
6750 fclose(fd);
6751 return FAIL;
6752 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006753 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 i = fread((char *)buffer, 1, len, fd);
6755 fclose(fd);
6756 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006757 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759 /* unexpected read error */
6760 EMSG2(_(e_notread), tempname);
6761 vim_free(tempname);
6762 vim_free(buffer);
6763 return FAIL;
6764 }
6765 vim_free(tempname);
6766
Bram Moolenaarc7247912008-01-13 12:54:11 +00006767# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6769 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006770 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6772 *p++ = buffer[i];
6773 len = p - buffer;
6774# endif
6775
6776
6777 /* file names are separated with Space */
6778 if (shell_style == STYLE_ECHO)
6779 {
6780 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6781 p = buffer;
6782 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6783 {
6784 while (*p != ' ' && *p != '\n')
6785 ++p;
6786 p = skipwhite(p); /* skip to next entry */
6787 }
6788 }
6789 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006790 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6793 p = buffer;
6794 for (i = 0; *p != NUL; ++i) /* count number of entries */
6795 {
6796 while (*p != '\n' && *p != NUL)
6797 ++p;
6798 if (*p != NUL)
6799 ++p;
6800 p = skipwhite(p); /* skip leading white space */
6801 }
6802 }
6803 /* file names are separated with NUL */
6804 else
6805 {
6806 /*
6807 * Some versions of zsh use spaces instead of NULs to separate
6808 * results. Only do this when there is no NUL before the end of the
6809 * buffer, otherwise we would never be able to use file names with
6810 * embedded spaces when zsh does use NULs.
6811 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6812 * don't check for spaces again.
6813 */
6814 check_spaces = FALSE;
6815 if (shell_style == STYLE_PRINT && !did_find_nul)
6816 {
6817 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006818 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006819 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 did_find_nul = TRUE;
6821 else
6822 check_spaces = TRUE;
6823 }
6824
6825 /*
6826 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6827 * already is one, for STYLE_GLOB it needs to be added.
6828 */
6829 if (len && buffer[len - 1] == NUL)
6830 --len;
6831 else
6832 buffer[len] = NUL;
6833 i = 0;
6834 for (p = buffer; p < buffer + len; ++p)
6835 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6836 {
6837 ++i;
6838 *p = NUL;
6839 }
6840 if (len)
6841 ++i; /* count last entry */
6842 }
6843 if (i == 0)
6844 {
6845 /*
6846 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6847 * /bin/sh will happily expand it to nothing rather than returning an
6848 * error; and hey, it's good to check anyway -- webb.
6849 */
6850 vim_free(buffer);
6851 goto notfound;
6852 }
6853 *num_file = i;
6854 *file = (char_u **)alloc(sizeof(char_u *) * i);
6855 if (*file == NULL)
6856 {
6857 /* out of memory */
6858 vim_free(buffer);
6859 return FAIL;
6860 }
6861
6862 /*
6863 * Isolate the individual file names.
6864 */
6865 p = buffer;
6866 for (i = 0; i < *num_file; ++i)
6867 {
6868 (*file)[i] = p;
6869 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006870 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6871 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006873 while (!(shell_style == STYLE_ECHO && *p == ' ')
6874 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 ++p;
6876 if (p == buffer + len) /* last entry */
6877 *p = NUL;
6878 else
6879 {
6880 *p++ = NUL;
6881 p = skipwhite(p); /* skip to next entry */
6882 }
6883 }
6884 else /* NUL separates */
6885 {
6886 while (*p && p < buffer + len) /* skip entry */
6887 ++p;
6888 ++p; /* skip NUL */
6889 }
6890 }
6891
6892 /*
6893 * Move the file names to allocated memory.
6894 */
6895 for (j = 0, i = 0; i < *num_file; ++i)
6896 {
6897 /* Require the files to exist. Helps when using /bin/sh */
6898 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6899 continue;
6900
6901 /* check if this entry should be included */
6902 dir = (mch_isdir((*file)[i]));
6903 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6904 continue;
6905
Bram Moolenaara2031822006-03-07 22:29:51 +00006906 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006907 if (!dir && (flags & EW_EXEC)
6908 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006909 continue;
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6912 if (p)
6913 {
6914 STRCPY(p, (*file)[i]);
6915 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006916 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 (*file)[j++] = p;
6918 }
6919 }
6920 vim_free(buffer);
6921 *num_file = j;
6922
6923 if (*num_file == 0) /* rejected all entries */
6924 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006925 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 goto notfound;
6927 }
6928
6929 return OK;
6930
6931notfound:
6932 if (flags & EW_NOTFOUND)
6933 return save_patterns(num_pat, pat, num_file, file);
6934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935}
6936
6937#endif /* VMS */
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006940save_patterns(
6941 int num_pat,
6942 char_u **pat,
6943 int *num_file,
6944 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945{
6946 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006947 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6950 if (*file == NULL)
6951 return FAIL;
6952 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006953 {
6954 s = vim_strsave(pat[i]);
6955 if (s != NULL)
6956 /* Be compatible with expand_filename(): halve the number of
6957 * backslashes. */
6958 backslash_halve(s);
6959 (*file)[i] = s;
6960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 *num_file = num_pat;
6962 return OK;
6963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965/*
6966 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6967 * expand.
6968 */
6969 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006970mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006972 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 if (*p == '\\' && p[1] != NUL)
6975 ++p;
6976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (vim_strchr((char_u *)
6978#ifdef VMS
6979 "*?%"
6980#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982#endif
6983 , *p) != NULL)
6984 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986 return FALSE;
6987}
6988
6989/*
6990 * Return TRUE if the string "p" contains a wildcard.
6991 * Don't recognize '~' at the end as a wildcard.
6992 */
6993 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006994mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006996 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (*p == '\\' && p[1] != NUL)
6999 ++p;
7000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (vim_strchr((char_u *)
7002#ifdef VMS
7003 "*?%$"
7004#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006#endif
7007 , *p) != NULL
7008 || (*p == '~' && p[1] != NUL))
7009 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011 return FALSE;
7012}
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007015have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 int i;
7018
7019 for (i = 0; i < num; i++)
7020 if (mch_has_wildcard(file[i]))
7021 return 1;
7022 return 0;
7023}
7024
7025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007026have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 int i;
7029
7030 for (i = 0; i < num; i++)
7031 if (vim_strchr(file[i], '$') != NULL)
7032 return TRUE;
7033 return FALSE;
7034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007036#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037/*
7038 * Scaled-down version of rename(), which is missing in Xenix.
7039 * This version can only move regular files and will fail if the
7040 * destination exists.
7041 */
7042 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007043mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 struct stat st;
7046
7047 if (stat(dest, &st) >= 0) /* fail if destination exists */
7048 return -1;
7049 if (link(src, dest) != 0) /* link file to new name */
7050 return -1;
7051 if (mch_remove(src) == 0) /* delete link to old name */
7052 return 0;
7053 return -1;
7054}
7055#endif /* !HAVE_RENAME */
7056
7057#ifdef FEAT_MOUSE_GPM
7058/*
7059 * Initializes connection with gpm (if it isn't already opened)
7060 * Return 1 if succeeded (or connection already opened), 0 if failed
7061 */
7062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007063gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7066
7067 if (!gpm_flag)
7068 {
7069 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7070 gpm_connect.defaultMask = ~GPM_HARD;
7071 /* Default handling for mouse move*/
7072 gpm_connect.minMod = 0; /* Handle any modifier keys */
7073 gpm_connect.maxMod = 0xffff;
7074 if (Gpm_Open(&gpm_connect, 0) > 0)
7075 {
7076 /* gpm library tries to handling TSTP causes
7077 * problems. Anyways, we close connection to Gpm whenever
7078 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007079 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007081# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 return 1; /* succeed */
7085 }
7086 if (gpm_fd == -2)
7087 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7088 return 0;
7089 }
7090 return 1; /* already open */
7091}
7092
7093/*
7094 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 */
7096 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007097gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 if (gpm_flag && gpm_fd >= 0) /* if Open */
7100 Gpm_Close();
7101}
7102
7103/* Reads gpm event and adds special keys to input buf. Returns length of
7104 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007105 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 */
7107 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007108mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 int button;
7111 static Gpm_Event gpm_event;
7112 char_u string[6];
7113 int_u vim_modifiers;
7114 int row,col;
7115 unsigned char buttons_mask;
7116 unsigned char gpm_modifiers;
7117 static unsigned char old_buttons = 0;
7118
7119 Gpm_GetEvent(&gpm_event);
7120
7121#ifdef FEAT_GUI
7122 /* Don't put events in the input queue now. */
7123 if (hold_gui_events)
7124 return 0;
7125#endif
7126
7127 row = gpm_event.y - 1;
7128 col = gpm_event.x - 1;
7129
7130 string[0] = ESC; /* Our termcode */
7131 string[1] = 'M';
7132 string[2] = 'G';
7133 switch (GPM_BARE_EVENTS(gpm_event.type))
7134 {
7135 case GPM_DRAG:
7136 string[3] = MOUSE_DRAG;
7137 break;
7138 case GPM_DOWN:
7139 buttons_mask = gpm_event.buttons & ~old_buttons;
7140 old_buttons = gpm_event.buttons;
7141 switch (buttons_mask)
7142 {
7143 case GPM_B_LEFT:
7144 button = MOUSE_LEFT;
7145 break;
7146 case GPM_B_MIDDLE:
7147 button = MOUSE_MIDDLE;
7148 break;
7149 case GPM_B_RIGHT:
7150 button = MOUSE_RIGHT;
7151 break;
7152 default:
7153 return 0;
7154 /*Don't know what to do. Can more than one button be
7155 * reported in one event? */
7156 }
7157 string[3] = (char_u)(button | 0x20);
7158 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7159 break;
7160 case GPM_UP:
7161 string[3] = MOUSE_RELEASE;
7162 old_buttons &= ~gpm_event.buttons;
7163 break;
7164 default:
7165 return 0;
7166 }
7167 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7168 gpm_modifiers = gpm_event.modifiers;
7169 vim_modifiers = 0x0;
7170 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7171 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7172 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7173 */
7174 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7175 vim_modifiers |= MOUSE_SHIFT;
7176
7177 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7178 vim_modifiers |= MOUSE_CTRL;
7179 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7180 vim_modifiers |= MOUSE_ALT;
7181 string[3] |= vim_modifiers;
7182 string[4] = (char_u)(col + ' ' + 1);
7183 string[5] = (char_u)(row + ' ' + 1);
7184 add_to_input_buf(string, 6);
7185 return 6;
7186}
7187#endif /* FEAT_MOUSE_GPM */
7188
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007189#ifdef FEAT_SYSMOUSE
7190/*
7191 * Initialize connection with sysmouse.
7192 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7193 * output, any sysmouse output than will be processed via sig_sysmouse().
7194 * Return OK if succeeded, FAIL if failed.
7195 */
7196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007197sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007198{
7199 struct mouse_info mouse;
7200
7201 mouse.operation = MOUSE_MODE;
7202 mouse.u.mode.mode = 0;
7203 mouse.u.mode.signal = SIGUSR2;
7204 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7205 {
7206 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7207 mouse.operation = MOUSE_SHOW;
7208 ioctl(1, CONS_MOUSECTL, &mouse);
7209 return OK;
7210 }
7211 return FAIL;
7212}
7213
7214/*
7215 * Stop processing SIGUSR2 signals, and also make sure that
7216 * virtual console do not send us any sysmouse related signal.
7217 */
7218 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007219sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007220{
7221 struct mouse_info mouse;
7222
7223 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7224 mouse.operation = MOUSE_MODE;
7225 mouse.u.mode.mode = 0;
7226 mouse.u.mode.signal = 0;
7227 ioctl(1, CONS_MOUSECTL, &mouse);
7228}
7229
7230/*
7231 * Gets info from sysmouse and adds special keys to input buf.
7232 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007233 static RETSIGTYPE
7234sig_sysmouse SIGDEFARG(sigarg)
7235{
7236 struct mouse_info mouse;
7237 struct video_info video;
7238 char_u string[6];
7239 int row, col;
7240 int button;
7241 int buttons;
7242 static int oldbuttons = 0;
7243
7244#ifdef FEAT_GUI
7245 /* Don't put events in the input queue now. */
7246 if (hold_gui_events)
7247 return;
7248#endif
7249
7250 mouse.operation = MOUSE_GETINFO;
7251 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7252 && ioctl(1, FBIO_MODEINFO, &video) != -1
7253 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7254 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7255 {
7256 row = mouse.u.data.y / video.vi_cheight;
7257 col = mouse.u.data.x / video.vi_cwidth;
7258 buttons = mouse.u.data.buttons;
7259 string[0] = ESC; /* Our termcode */
7260 string[1] = 'M';
7261 string[2] = 'S';
7262 if (oldbuttons == buttons && buttons != 0)
7263 {
7264 button = MOUSE_DRAG;
7265 }
7266 else
7267 {
7268 switch (buttons)
7269 {
7270 case 0:
7271 button = MOUSE_RELEASE;
7272 break;
7273 case 1:
7274 button = MOUSE_LEFT;
7275 break;
7276 case 2:
7277 button = MOUSE_MIDDLE;
7278 break;
7279 case 4:
7280 button = MOUSE_RIGHT;
7281 break;
7282 default:
7283 return;
7284 }
7285 oldbuttons = buttons;
7286 }
7287 string[3] = (char_u)(button);
7288 string[4] = (char_u)(col + ' ' + 1);
7289 string[5] = (char_u)(row + ' ' + 1);
7290 add_to_input_buf(string, 6);
7291 }
7292 return;
7293}
7294#endif /* FEAT_SYSMOUSE */
7295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007297typedef char_u * (*STRPROCSTR)(char_u *);
7298typedef char_u * (*INTPROCSTR)(int);
7299typedef int (*STRPROCINT)(char_u *);
7300typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
7302/*
7303 * Call a DLL routine which takes either a string or int param
7304 * and returns an allocated string.
7305 */
7306 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007307mch_libcall(
7308 char_u *libname,
7309 char_u *funcname,
7310 char_u *argstring, /* NULL when using a argint */
7311 int argint,
7312 char_u **string_result,/* NULL when using number_result */
7313 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315# if defined(USE_DLOPEN)
7316 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007317 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318# else
7319 shl_t hinstLib;
7320# endif
7321 STRPROCSTR ProcAdd;
7322 INTPROCSTR ProcAddI;
7323 char_u *retval_str = NULL;
7324 int retval_int = 0;
7325 int success = FALSE;
7326
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007327 /*
7328 * Get a handle to the DLL module.
7329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007331 /* First clear any error, it's not cleared by the dlopen() call. */
7332 (void)dlerror();
7333
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 hinstLib = dlopen((char *)libname, RTLD_LAZY
7335# ifdef RTLD_LOCAL
7336 | RTLD_LOCAL
7337# endif
7338 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007339 if (hinstLib == NULL)
7340 {
7341 /* "dlerr" must be used before dlclose() */
7342 dlerr = (char *)dlerror();
7343 if (dlerr != NULL)
7344 EMSG2(_("dlerror = \"%s\""), dlerr);
7345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# else
7347 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7348# endif
7349
7350 /* If the handle is valid, try to get the function address. */
7351 if (hinstLib != NULL)
7352 {
7353# ifdef HAVE_SETJMP_H
7354 /*
7355 * Catch a crash when calling the library function. For example when
7356 * using a number where a string pointer is expected.
7357 */
7358 mch_startjmp();
7359 if (SETJMP(lc_jump_env) != 0)
7360 {
7361 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007362# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007363 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 mch_didjmp();
7366 }
7367 else
7368# endif
7369 {
7370 retval_str = NULL;
7371 retval_int = 0;
7372
7373 if (argstring != NULL)
7374 {
7375# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007376 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007377 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378# else
7379 if (shl_findsym(&hinstLib, (const char *)funcname,
7380 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7381 ProcAdd = NULL;
7382# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007383 if ((success = (ProcAdd != NULL
7384# if defined(USE_DLOPEN)
7385 && dlerr == NULL
7386# endif
7387 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
7389 if (string_result == NULL)
7390 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7391 else
7392 retval_str = (ProcAdd)(argstring);
7393 }
7394 }
7395 else
7396 {
7397# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007398 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400# else
7401 if (shl_findsym(&hinstLib, (const char *)funcname,
7402 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7403 ProcAddI = NULL;
7404# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007405 if ((success = (ProcAddI != NULL
7406# if defined(USE_DLOPEN)
7407 && dlerr == NULL
7408# endif
7409 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
7411 if (string_result == NULL)
7412 retval_int = ((INTPROCINT)ProcAddI)(argint);
7413 else
7414 retval_str = (ProcAddI)(argint);
7415 }
7416 }
7417
7418 /* Save the string before we free the library. */
7419 /* Assume that a "1" or "-1" result is an illegal pointer. */
7420 if (string_result == NULL)
7421 *number_result = retval_int;
7422 else if (retval_str != NULL
7423 && retval_str != (char_u *)1
7424 && retval_str != (char_u *)-1)
7425 *string_result = vim_strsave(retval_str);
7426 }
7427
7428# ifdef HAVE_SETJMP_H
7429 mch_endjmp();
7430# ifdef SIGHASARG
7431 if (lc_signal != 0)
7432 {
7433 int i;
7434
7435 /* try to find the name of this signal */
7436 for (i = 0; signal_info[i].sig != -1; i++)
7437 if (lc_signal == signal_info[i].sig)
7438 break;
7439 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7440 }
7441# endif
7442# endif
7443
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007445 /* "dlerr" must be used before dlclose() */
7446 if (dlerr != NULL)
7447 EMSG2(_("dlerror = \"%s\""), dlerr);
7448
7449 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 (void)dlclose(hinstLib);
7451# else
7452 (void)shl_unload(hinstLib);
7453# endif
7454 }
7455
7456 if (!success)
7457 {
7458 EMSG2(_(e_libcall), funcname);
7459 return FAIL;
7460 }
7461
7462 return OK;
7463}
7464#endif
7465
7466#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7467static int xterm_trace = -1; /* default: disabled */
7468static int xterm_button;
7469
7470/*
7471 * Setup a dummy window for X selections in a terminal.
7472 */
7473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007474setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
7476 int z = 0;
7477 char *strp = "";
7478 Widget AppShell;
7479
7480 if (!x_connect_to_server())
7481 return;
7482
7483 open_app_context();
7484 if (app_context != NULL && xterm_Shell == (Widget)0)
7485 {
7486 int (*oldhandler)();
7487#if defined(HAVE_SETJMP_H)
7488 int (*oldIOhandler)();
7489#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007490# ifdef ELAPSED_FUNC
7491 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
7493 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007494 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495# endif
7496
7497 /* Ignore X errors while opening the display */
7498 oldhandler = XSetErrorHandler(x_error_check);
7499
7500#if defined(HAVE_SETJMP_H)
7501 /* Ignore X IO errors while opening the display */
7502 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7503 mch_startjmp();
7504 if (SETJMP(lc_jump_env) != 0)
7505 {
7506 mch_didjmp();
7507 xterm_dpy = NULL;
7508 }
7509 else
7510#endif
7511 {
7512 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7513 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007514 if (xterm_dpy != NULL)
7515 xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516#if defined(HAVE_SETJMP_H)
7517 mch_endjmp();
7518#endif
7519 }
7520
7521#if defined(HAVE_SETJMP_H)
7522 /* Now handle X IO errors normally. */
7523 (void)XSetIOErrorHandler(oldIOhandler);
7524#endif
7525 /* Now handle X errors normally. */
7526 (void)XSetErrorHandler(oldhandler);
7527
7528 if (xterm_dpy == NULL)
7529 {
7530 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007531 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 return;
7533 }
7534
7535 /* Catch terminating error of the X server connection. */
7536 (void)XSetIOErrorHandler(x_IOerror_handler);
7537
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007538# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007540 {
7541 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007542 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007543 verbose_leave();
7544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545# endif
7546
7547 /* Create a Shell to make converters work. */
7548 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7549 applicationShellWidgetClass, xterm_dpy,
7550 NULL);
7551 if (AppShell == (Widget)0)
7552 return;
7553 xterm_Shell = XtVaCreatePopupShell("VIM",
7554 topLevelShellWidgetClass, AppShell,
7555 XtNmappedWhenManaged, 0,
7556 XtNwidth, 1,
7557 XtNheight, 1,
7558 NULL);
7559 if (xterm_Shell == (Widget)0)
7560 return;
7561
7562 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007563 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (x11_display == NULL)
7565 x11_display = xterm_dpy;
7566
7567 XtRealizeWidget(xterm_Shell);
7568 XSync(xterm_dpy, False);
7569 xterm_update();
7570 }
7571 if (xterm_Shell != (Widget)0)
7572 {
7573 clip_init(TRUE);
7574 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7575 x11_window = (Window)atol(strp);
7576 /* Check if $WINDOWID is valid. */
7577 if (test_x11_window(xterm_dpy) == FAIL)
7578 x11_window = 0;
7579 if (x11_window != 0)
7580 xterm_trace = 0;
7581 }
7582}
7583
7584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007585start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
7587 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7588 return;
7589 xterm_trace = 1;
7590 xterm_button = button;
7591 do_xterm_trace();
7592}
7593
7594
7595 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007596stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 if (xterm_trace < 0)
7599 return;
7600 xterm_trace = 0;
7601}
7602
7603/*
7604 * Query the xterm pointer and generate mouse termcodes if necessary
7605 * return TRUE if dragging is active, else FALSE
7606 */
7607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007608do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 Window root, child;
7611 int root_x, root_y;
7612 int win_x, win_y;
7613 int row, col;
7614 int_u mask_return;
7615 char_u buf[50];
7616 char_u *strp;
7617 long got_hints;
7618 static char_u *mouse_code;
7619 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7620 static int prev_row = 0, prev_col = 0;
7621 static XSizeHints xterm_hints;
7622
7623 if (xterm_trace <= 0)
7624 return FALSE;
7625
7626 if (xterm_trace == 1)
7627 {
7628 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007629 * have changed recently. */
7630 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7631 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 || xterm_hints.width_inc <= 1
7633 || xterm_hints.height_inc <= 1)
7634 {
7635 xterm_trace = -1; /* Not enough data -- disable tracing */
7636 return FALSE;
7637 }
7638
7639 /* Rely on the same mouse code for the duration of this */
7640 mouse_code = find_termcode(mouse_name);
7641 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007642 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 xterm_trace = 2;
7644
7645 /* Find the offset of the chars, there might be a scrollbar on the
7646 * left of the window and/or a menu on the top (eterm etc.) */
7647 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7648 &win_x, &win_y, &mask_return);
7649 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7650 - (xterm_hints.height_inc / 2);
7651 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7652 xterm_hints.y = 2;
7653 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7654 - (xterm_hints.width_inc / 2);
7655 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7656 xterm_hints.x = 2;
7657 return TRUE;
7658 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007659 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {
7661 xterm_trace = 0;
7662 return FALSE;
7663 }
7664
7665 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7666 &win_x, &win_y, &mask_return);
7667
7668 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7669 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7670 if (row == prev_row && col == prev_col)
7671 return TRUE;
7672
7673 STRCPY(buf, mouse_code);
7674 strp = buf + STRLEN(buf);
7675 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7676 *strp++ = (char_u)(col + ' ' + 1);
7677 *strp++ = (char_u)(row + ' ' + 1);
7678 *strp = 0;
7679 add_to_input_buf(buf, STRLEN(buf));
7680
7681 prev_row = row;
7682 prev_col = col;
7683 return TRUE;
7684}
7685
7686# if defined(FEAT_GUI) || defined(PROTO)
7687/*
7688 * Destroy the display, window and app_context. Required for GTK.
7689 */
7690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007691clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 if (xterm_Shell != (Widget)0)
7694 {
7695 XtDestroyWidget(xterm_Shell);
7696 xterm_Shell = (Widget)0;
7697 }
7698 if (xterm_dpy != NULL)
7699 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007700# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 /* Lesstif and Solaris crash here, lose some memory */
7702 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (x11_display == xterm_dpy)
7705 x11_display = NULL;
7706 xterm_dpy = NULL;
7707 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007708# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 if (app_context != (XtAppContext)NULL)
7710 {
7711 /* Lesstif and Solaris crash here, lose some memory */
7712 XtDestroyApplicationContext(app_context);
7713 app_context = (XtAppContext)NULL;
7714 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717# endif
7718
7719/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007720 * Catch up with GUI or X events.
7721 */
7722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007723clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007724{
7725# ifdef FEAT_GUI
7726 if (gui.in_use)
7727 gui_mch_update();
7728 else
7729# endif
7730 if (xterm_Shell != (Widget)0)
7731 xterm_update();
7732}
7733
7734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 * Catch up with any queued X events. This may put keyboard input into the
7736 * input buffer, call resize call-backs, trigger timers etc. If there is
7737 * nothing in the X event queue (& no timers pending), then we return
7738 * immediately.
7739 */
7740 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007741xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 XEvent event;
7744
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007745 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007747 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007749 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007750 break;
7751
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007752 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007753 {
7754 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007755 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007756#ifdef FEAT_CLIENTSERVER
7757 {
7758 XPropertyEvent *e = (XPropertyEvent *)&event;
7759
7760 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007762 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007765 XtDispatchEvent(&event);
7766 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007767 else
7768 {
7769 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007770 XtAppProcessEvent(app_context, mask);
7771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773}
7774
7775 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007776clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 if (xterm_Shell != (Widget)0)
7779 return clip_x11_own_selection(xterm_Shell, cbd);
7780 return FAIL;
7781}
7782
7783 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007784clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 if (xterm_Shell != (Widget)0)
7787 clip_x11_lose_selection(xterm_Shell, cbd);
7788}
7789
7790 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007791clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 if (xterm_Shell != (Widget)0)
7794 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7795}
7796
7797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007798clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 clip_x11_set_selection(cbd);
7801}
7802#endif
7803
7804
7805#if defined(USE_XSMP) || defined(PROTO)
7806/*
7807 * Code for X Session Management Protocol.
7808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811/*
7812 * This is our chance to ask the user if they want to save,
7813 * or abort the logout
7814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007816xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 cmdmod_T save_cmdmod;
7819 int cancel_shutdown = False;
7820
7821 save_cmdmod = cmdmod;
7822 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007823 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 /* Mustn't logout */
7825 cancel_shutdown = True;
7826 cmdmod = save_cmdmod;
7827 setcursor(); /* position cursor */
7828 out_flush();
7829
7830 /* Done interaction */
7831 SmcInteractDone(smc_conn, cancel_shutdown);
7832
7833 /* Finish off
7834 * Only end save-yourself here if we're not cancelling shutdown;
7835 * we'll get a cancelled callback later in which we'll end it.
7836 * Hopefully get around glitchy SMs (like GNOME-1)
7837 */
7838 if (!cancel_shutdown)
7839 {
7840 xsmp.save_yourself = False;
7841 SmcSaveYourselfDone(smc_conn, True);
7842 }
7843}
7844# endif
7845
7846/*
7847 * Callback that starts save-yourself.
7848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007850xsmp_handle_save_yourself(
7851 SmcConn smc_conn,
7852 SmPointer client_data UNUSED,
7853 int save_type UNUSED,
7854 Bool shutdown,
7855 int interact_style UNUSED,
7856 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 /* Handle already being in saveyourself */
7859 if (xsmp.save_yourself)
7860 SmcSaveYourselfDone(smc_conn, True);
7861 xsmp.save_yourself = True;
7862 xsmp.shutdown = shutdown;
7863
7864 /* First up, preserve all files */
7865 out_flush();
7866 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7867
7868 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007869 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
7871# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7872 /* Now see if we can ask about unsaved files */
7873 if (shutdown && !fast && gui.in_use)
7874 /* Need to interact with user, but need SM's permission */
7875 SmcInteractRequest(smc_conn, SmDialogError,
7876 xsmp_handle_interaction, client_data);
7877 else
7878# endif
7879 {
7880 /* Can stop the cycle here */
7881 SmcSaveYourselfDone(smc_conn, True);
7882 xsmp.save_yourself = False;
7883 }
7884}
7885
7886
7887/*
7888 * Callback to warn us of imminent death.
7889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007891xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 xsmp_close();
7894
7895 /* quit quickly leaving swapfiles for modified buffers behind */
7896 getout_preserve_modified(0);
7897}
7898
7899
7900/*
7901 * Callback to tell us that save-yourself has completed.
7902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007904xsmp_save_complete(
7905 SmcConn smc_conn UNUSED,
7906 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 xsmp.save_yourself = False;
7909}
7910
7911
7912/*
7913 * Callback to tell us that an instigated shutdown was cancelled
7914 * (maybe even by us)
7915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007917xsmp_shutdown_cancelled(
7918 SmcConn smc_conn,
7919 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 if (xsmp.save_yourself)
7922 SmcSaveYourselfDone(smc_conn, True);
7923 xsmp.save_yourself = False;
7924 xsmp.shutdown = False;
7925}
7926
7927
7928/*
7929 * Callback to tell us that a new ICE connection has been established.
7930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007932xsmp_ice_connection(
7933 IceConn iceConn,
7934 IcePointer clientData UNUSED,
7935 Bool opening,
7936 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 /* Intercept creation of ICE connection fd */
7939 if (opening)
7940 {
7941 xsmp_icefd = IceConnectionNumber(iceConn);
7942 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7943 }
7944}
7945
7946
7947/* Handle any ICE processing that's required; return FAIL if SM lost */
7948 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007949xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 Bool rep;
7952
7953 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7954 == IceProcessMessagesIOError)
7955 {
7956 /* Lost ICE */
7957 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007958 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 xsmp_close();
7960 return FAIL;
7961 }
7962 else
7963 return OK;
7964}
7965
7966static int dummy;
7967
7968/* Set up X Session Management Protocol */
7969 void
7970xsmp_init(void)
7971{
7972 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 SmcCallbacks smcallbacks;
7974#if 0
7975 SmPropValue smname;
7976 SmProp smnameprop;
7977 SmProp *smprops[1];
7978#endif
7979
7980 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007981 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
7983 xsmp.save_yourself = xsmp.shutdown = False;
7984
7985 /* Set up SM callbacks - must have all, even if they're not used */
7986 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7987 smcallbacks.save_yourself.client_data = NULL;
7988 smcallbacks.die.callback = xsmp_die;
7989 smcallbacks.die.client_data = NULL;
7990 smcallbacks.save_complete.callback = xsmp_save_complete;
7991 smcallbacks.save_complete.client_data = NULL;
7992 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7993 smcallbacks.shutdown_cancelled.client_data = NULL;
7994
7995 /* Set up a watch on ICE connection creations. The "dummy" argument is
7996 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7997 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7998 {
7999 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008000 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 return;
8002 }
8003
8004 /* Create an SM connection */
8005 xsmp.smcconn = SmcOpenConnection(
8006 NULL,
8007 NULL,
8008 SmProtoMajor,
8009 SmProtoMinor,
8010 SmcSaveYourselfProcMask | SmcDieProcMask
8011 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8012 &smcallbacks,
8013 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008014 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008015 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 errorstring);
8017 if (xsmp.smcconn == NULL)
8018 {
8019 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008020
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008022 {
8023 vim_snprintf(errorreport, sizeof(errorreport),
8024 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8025 verb_msg((char_u *)errorreport);
8026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 return;
8028 }
8029 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8030
8031#if 0
8032 /* ID ourselves */
8033 smname.value = "vim";
8034 smname.length = 3;
8035 smnameprop.name = "SmProgram";
8036 smnameprop.type = "SmARRAY8";
8037 smnameprop.num_vals = 1;
8038 smnameprop.vals = &smname;
8039
8040 smprops[0] = &smnameprop;
8041 SmcSetProperties(xsmp.smcconn, 1, smprops);
8042#endif
8043}
8044
8045
8046/* Shut down XSMP comms. */
8047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008048xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 if (xsmp_icefd != -1)
8051 {
8052 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008053 if (xsmp.clientid != NULL)
8054 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008055 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 xsmp_icefd = -1;
8057 }
8058}
8059#endif /* USE_XSMP */
8060
8061
8062#ifdef EBCDIC
8063/* Translate character to its CTRL- value */
8064char CtrlTable[] =
8065{
8066/* 00 - 5E */
8067 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8069 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8070 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8071 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8072 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8073/* ^ */ 0x1E,
8074/* - */ 0x1F,
8075/* 61 - 6C */
8076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8077/* _ */ 0x1F,
8078/* 6E - 80 */
8079 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8080/* a */ 0x01,
8081/* b */ 0x02,
8082/* c */ 0x03,
8083/* d */ 0x37,
8084/* e */ 0x2D,
8085/* f */ 0x2E,
8086/* g */ 0x2F,
8087/* h */ 0x16,
8088/* i */ 0x05,
8089/* 8A - 90 */
8090 0, 0, 0, 0, 0, 0, 0,
8091/* j */ 0x15,
8092/* k */ 0x0B,
8093/* l */ 0x0C,
8094/* m */ 0x0D,
8095/* n */ 0x0E,
8096/* o */ 0x0F,
8097/* p */ 0x10,
8098/* q */ 0x11,
8099/* r */ 0x12,
8100/* 9A - A1 */
8101 0, 0, 0, 0, 0, 0, 0, 0,
8102/* s */ 0x13,
8103/* t */ 0x3C,
8104/* u */ 0x3D,
8105/* v */ 0x32,
8106/* w */ 0x26,
8107/* x */ 0x18,
8108/* y */ 0x19,
8109/* z */ 0x3F,
8110/* AA - AC */
8111 0, 0, 0,
8112/* [ */ 0x27,
8113/* AE - BC */
8114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8115/* ] */ 0x1D,
8116/* BE - C0 */ 0, 0, 0,
8117/* A */ 0x01,
8118/* B */ 0x02,
8119/* C */ 0x03,
8120/* D */ 0x37,
8121/* E */ 0x2D,
8122/* F */ 0x2E,
8123/* G */ 0x2F,
8124/* H */ 0x16,
8125/* I */ 0x05,
8126/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8127/* J */ 0x15,
8128/* K */ 0x0B,
8129/* L */ 0x0C,
8130/* M */ 0x0D,
8131/* N */ 0x0E,
8132/* O */ 0x0F,
8133/* P */ 0x10,
8134/* Q */ 0x11,
8135/* R */ 0x12,
8136/* DA - DF */ 0, 0, 0, 0, 0, 0,
8137/* \ */ 0x1C,
8138/* E1 */ 0,
8139/* S */ 0x13,
8140/* T */ 0x3C,
8141/* U */ 0x3D,
8142/* V */ 0x32,
8143/* W */ 0x26,
8144/* X */ 0x18,
8145/* Y */ 0x19,
8146/* Z */ 0x3F,
8147/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8149};
8150
8151char MetaCharTable[]=
8152{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8153 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8154 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8155 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8156 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8157};
8158
8159
8160/* TODO: Use characters NOT numbers!!! */
8161char CtrlCharTable[]=
8162{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8163 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8164 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8165 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8166 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8167};
8168
8169
8170#endif