blob: b811ac3f4b812adb33a2bbe5fa56b6444b50ffa0 [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
21/*
22 * Some systems have a prototype for select() that has (int *) instead of
23 * (fd_set *), which is wrong. This define removes that prototype. We define
24 * our own prototype below.
25 * Don't use it for the Mac, it causes a warning for precompiled headers.
26 * TODO: use a configure check for precompiled headers?
27 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000028#if !defined(__APPLE__) && !defined(__TANDEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +000029# define select select_declared_wrong
30#endif
31
32#include "vim.h"
33
Bram Moolenaar325b7a22004-07-05 15:58:32 +000034#ifdef FEAT_MZSCHEME
35# include "if_mzsch.h"
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "os_unixx.h" /* unix includes for os_unix.c only */
39
40#ifdef USE_XSMP
41# include <X11/SM/SMlib.h>
42#endif
43
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000044#ifdef HAVE_SELINUX
45# include <selinux/selinux.h>
46static int selinux_enabled = -1;
47#endif
48
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020049#ifdef HAVE_SMACK
50# include <attr/xattr.h>
51# include <linux/xattr.h>
52# ifndef SMACK_LABEL_LEN
53# define SMACK_LABEL_LEN 1024
54# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * Use this prototype for select, some include files have a wrong prototype
59 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000060#ifndef __TANDEM
61# undef select
62# ifdef __BEOS__
63# define select beos_select
64# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
66
Bram Moolenaara2442432007-04-26 14:26:37 +000067#ifdef __CYGWIN__
68# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000069# include <cygwin/version.h>
70# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
71 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010072# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
73# define WIN32_LEAN_AND_MEAN
74# include <windows.h>
75# include "winclip.pro"
76# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000077# endif
78#endif
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#if defined(HAVE_SELECT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
83
84#ifdef FEAT_MOUSE_GPM
85# include <gpm.h>
86/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
87 * I just copied relevant defines here. A cleaner solution would be to put gpm
88 * code into separate file and include there linux/keyboard.h
89 */
90/* #include <linux/keyboard.h> */
91# define KG_SHIFT 0
92# define KG_CTRL 2
93# define KG_ALT 3
94# define KG_ALTGR 1
95# define KG_SHIFTL 4
96# define KG_SHIFTR 5
97# define KG_CTRLL 6
98# define KG_CTRLR 7
99# define KG_CAPSSHIFT 8
100
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void gpm_close(void);
102static int gpm_open(void);
103static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000106#ifdef FEAT_SYSMOUSE
107# include <sys/consio.h>
108# include <sys/fbio.h>
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static int sysmouse_open(void);
111static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +0100112static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000113#endif
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115/*
116 * end of autoconf section. To be extended...
117 */
118
119/* Are the following #ifdefs still required? And why? Is that for X11? */
120
121#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
122# ifdef SIGWINCH
123# undef SIGWINCH
124# endif
125# ifdef TIOCGWINSZ
126# undef TIOCGWINSZ
127# endif
128#endif
129
130#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
131# define SIGWINCH SIGWINDOW
132#endif
133
134#ifdef FEAT_X11
135# include <X11/Xlib.h>
136# include <X11/Xutil.h>
137# include <X11/Xatom.h>
138# ifdef FEAT_XCLIPBOARD
139# include <X11/Intrinsic.h>
140# include <X11/Shell.h>
141# include <X11/StringDefs.h>
142static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void clip_update(void);
144static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
146
147# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
148Window x11_window = 0;
149# endif
150Display *x11_display = NULL;
151
152# ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static int get_x11_windis(void);
154static void set_x11_title(char_u *);
155static void set_x11_icon(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156# endif
157#endif
158
159#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int get_x11_title(int);
161static int get_x11_icon(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163static char_u *oldtitle = NULL;
164static int did_set_title = FALSE;
165static char_u *oldicon = NULL;
166static int did_set_icon = FALSE;
167#endif
168
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
Bram Moolenaar205b8862011-09-07 15:04:31 +0200171#ifdef HAVE_UNION_WAIT
172typedef union wait waitstatus;
173#else
174typedef int waitstatus;
175#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static pid_t wait4pid(pid_t, waitstatus *);
Bram Moolenaar205b8862011-09-07 15:04:31 +0200177
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200178static int WaitForChar(long msec, int *interrupted, int ignore_input);
179static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100180#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200181int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200183static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
185
186#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100187static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000188# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100191static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100194static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100197static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#endif
199#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100200static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202#if defined(SIGALRM) && defined(FEAT_X11) \
203 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
204# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100205static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000206/* volatile because it is used in signal handler sig_alarm(). */
207static volatile int sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100209static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100211static void catch_int_signal(void);
212static void set_signals(void);
213static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200214#ifdef HAVE_SIGPROCMASK
215# define SIGSET_DECL(set) sigset_t set;
216# define BLOCK_SIGNALS(set) block_signals(set)
217# define UNBLOCK_SIGNALS(set) unblock_signals(set)
218#else
219# define SIGSET_DECL(set)
220# define BLOCK_SIGNALS(set) do { /**/ } while (0)
221# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
222#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100223static int have_wildcard(int, char_u **);
224static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100226static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000229# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#endif
231
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000232/* volatile because it is used in signal handler sig_winch(). */
233static volatile int do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234static char_u *extra_shell_arg = NULL;
235static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000236/* volatile because it is used in signal handler deathtrap(). */
237static volatile int deadly_signal = 0; /* The signal we caught */
238/* volatile because it is used in signal handler deathtrap(). */
239static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100241#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
242static int dont_check_job_ended = 0;
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
246
247#ifdef USE_XSMP
248typedef struct
249{
250 SmcConn smcconn; /* The SM connection ID */
251 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200252 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 Bool save_yourself; /* If we're in the middle of a save_yourself */
254 Bool shutdown; /* If we're in shutdown mode */
255} xsmp_config_T;
256
257static xsmp_config_T xsmp;
258#endif
259
260#ifdef SYS_SIGLIST_DECLARED
261/*
262 * I have seen
263 * extern char *_sys_siglist[NSIG];
264 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
265 * that describe the signals. That is nearly what we want here. But
266 * autoconf does only check for sys_siglist (without the underscore), I
267 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100268 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 */
270#endif
271
272static struct signalinfo
273{
274 int sig; /* Signal number, eg. SIGSEGV etc */
275 char *name; /* Signal name (not char_u!). */
276 char deadly; /* Catch as a deadly signal? */
277} signal_info[] =
278{
279#ifdef SIGHUP
280 {SIGHUP, "HUP", TRUE},
281#endif
282#ifdef SIGQUIT
283 {SIGQUIT, "QUIT", TRUE},
284#endif
285#ifdef SIGILL
286 {SIGILL, "ILL", TRUE},
287#endif
288#ifdef SIGTRAP
289 {SIGTRAP, "TRAP", TRUE},
290#endif
291#ifdef SIGABRT
292 {SIGABRT, "ABRT", TRUE},
293#endif
294#ifdef SIGEMT
295 {SIGEMT, "EMT", TRUE},
296#endif
297#ifdef SIGFPE
298 {SIGFPE, "FPE", TRUE},
299#endif
300#ifdef SIGBUS
301 {SIGBUS, "BUS", TRUE},
302#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100303#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
304 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {SIGSEGV, "SEGV", TRUE},
306#endif
307#ifdef SIGSYS
308 {SIGSYS, "SYS", TRUE},
309#endif
310#ifdef SIGALRM
311 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
312#endif
313#ifdef SIGTERM
314 {SIGTERM, "TERM", TRUE},
315#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100316#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {SIGVTALRM, "VTALRM", TRUE},
318#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000319#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
320 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
321 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 {SIGPROF, "PROF", TRUE},
323#endif
324#ifdef SIGXCPU
325 {SIGXCPU, "XCPU", TRUE},
326#endif
327#ifdef SIGXFSZ
328 {SIGXFSZ, "XFSZ", TRUE},
329#endif
330#ifdef SIGUSR1
331 {SIGUSR1, "USR1", TRUE},
332#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000333#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
334 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {SIGUSR2, "USR2", TRUE},
336#endif
337#ifdef SIGINT
338 {SIGINT, "INT", FALSE},
339#endif
340#ifdef SIGWINCH
341 {SIGWINCH, "WINCH", FALSE},
342#endif
343#ifdef SIGTSTP
344 {SIGTSTP, "TSTP", FALSE},
345#endif
346#ifdef SIGPIPE
347 {SIGPIPE, "PIPE", FALSE},
348#endif
349 {-1, "Unknown!", FALSE}
350};
351
Bram Moolenaar25724922009-07-14 15:38:41 +0000352 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100353mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000354{
355 if (p_verbose >= 5)
356 {
357 verbose_enter();
358 smsg((char_u *)"chdir(%s)", path);
359 verbose_leave();
360 }
361# ifdef VMS
362 return chdir(vms_fixfilename(path));
363# else
364 return chdir(path);
365# endif
366}
367
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200368/* Why is NeXT excluded here (and not in os_unixx.h)? */
369#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
370# define NEW_TTY_SYSTEM
371#endif
372
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000373/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100374 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100377mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000379 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100381 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382}
383
384/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000385 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 * Get a characters from the keyboard.
387 * Return the number of characters that are available.
388 * If wtime == 0 do not wait for characters.
389 * If wtime == n wait a short time for characters.
390 * If wtime == -1 wait forever for characters.
391 */
392 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100393mch_inchar(
394 char_u *buf,
395 int maxlen,
396 long wtime, /* don't use "time", MIPS cannot handle it */
397 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
399 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200400 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200401 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200402 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200403 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100404#ifdef ELAPSED_FUNC
405 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200406
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100407 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200410 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200411 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200413 /* Check if window changed size while we were busy, perhaps the ":set
414 * columns=99" command was used. */
415 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200417
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200418#ifdef MESSAGE_QUEUE
419 parse_queued_messages();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200420 /* If input was put directly in typeahead buffer bail out here. */
421 if (typebuf_changed(tb_change_cnt))
422 return 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200423#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200424 if (wtime < 0 && did_start_blocking)
425 /* blocking and already waited for p_ut */
426 wait_time = -1;
427 else
428 {
429 if (wtime >= 0)
430 wait_time = wtime;
431 else
432 /* going to block after p_ut */
433 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100434#ifdef ELAPSED_FUNC
435 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200436#endif
437 wait_time -= elapsed_time;
438 if (wait_time < 0)
439 {
440 if (wtime >= 0)
441 /* no character available within "wtime" */
442 return 0;
443
444 if (wtime < 0)
445 {
446 /* no character available within 'updatetime' */
447 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200448 if (trigger_cursorhold() && maxlen >= 3
449 && !typebuf_changed(tb_change_cnt))
450 {
451 buf[0] = K_SPECIAL;
452 buf[1] = KS_EXTRA;
453 buf[2] = (int)KE_CURSORHOLD;
454 return 3;
455 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200456 /*
457 * If there is no character available within 'updatetime'
458 * seconds flush all the swap files to disk.
459 * Also done when interrupted by SIGWINCH.
460 */
461 before_blocking();
462 continue;
463 }
464 }
465 }
466
467#ifdef FEAT_JOB_CHANNEL
468 /* Checking if a job ended requires polling. Do this every 100 msec. */
469 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
470 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100471 /* If there is readahead then parse_queued_messages() timed out and we
472 * should call it again soon. */
473 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
474 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200475#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100476#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100477 if (p_beval && wait_time > 100L)
478 /* The 'balloonexpr' may indirectly invoke a callback while waiting
479 * for a character, need to check often. */
480 wait_time = 100L;
481#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200482
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200484 * We want to be interrupted by the winch signal
485 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200487 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200488 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200489 /* If input was put directly in typeahead buffer bail out here. */
490 if (typebuf_changed(tb_change_cnt))
491 return 0;
492
493 /*
494 * For some terminals we only get one character at a time.
495 * We want the get all available characters, so we could keep on
496 * trying until none is available
497 * For some other terminals this is quite slow, that's why we don't
498 * do it.
499 */
500 len = read_from_input_buf(buf, (long)maxlen);
501 if (len > 0)
502 return len;
503 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200506 /* no character available */
507#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
508 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100509 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200512 if (do_resize /* interrupted by SIGWINCH signal */
513#ifdef FEAT_CLIENTSERVER
514 || server_waiting()
515#endif
516#ifdef MESSAGE_QUEUE
517 || interrupted
518#endif
519 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100520 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200521 continue;
522
523 /* no character available or interrupted */
524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200526 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100530handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 do_resize = FALSE;
533 shell_resized();
534}
535
536/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200537 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100540mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200542 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543}
544
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200545#if defined(FEAT_TERMINAL) || defined(PROTO)
546/*
547 * Check for any pending input or messages.
548 */
549 int
550mch_check_messages(void)
551{
552 return WaitForChar(0L, NULL, TRUE);
553}
554#endif
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
557# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000558# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559# endif
560# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
561# include <sys/sysctl.h>
562# endif
563# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
564# include <sys/sysinfo.h>
565# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100566# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100567# include <mach/mach_host.h>
568# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000572 * Return total amount of memory available in Kbyte.
573 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100576mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000579 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaar362dc332018-03-05 21:59:37 +0100581# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100582 {
583 /* Mac (Darwin) way of getting the amount of RAM available */
584 mach_port_t host = mach_host_self();
585 kern_return_t kret;
586# ifdef HOST_VM_INFO64
587 struct vm_statistics64 vm_stat;
588 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
Bram Moolenaar988615f2018-02-27 17:58:20 +0100590 kret = host_statistics64(host, HOST_VM_INFO64,
591 (host_info64_t)&vm_stat, &count);
592# else
593 struct vm_statistics vm_stat;
594 natural_t count = HOST_VM_INFO_COUNT;
595
596 kret = host_statistics(host, HOST_VM_INFO,
597 (host_info_t)&vm_stat, &count);
598# endif
599 if (kret == KERN_SUCCESS)
600 /* get the amount of user memory by summing each usage */
601 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
602 + vm_stat.inactive_count
603# ifdef MAC_OS_X_VERSION_10_9
604 + vm_stat.compressor_page_count
605# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100606 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100607 mach_port_deallocate(mach_task_self(), host);
608 }
609# endif
610
611# ifdef HAVE_SYSCTL
612 if (mem == 0)
613 {
614 /* BSD way of getting the amount of RAM available. */
615 int mib[2];
616 size_t len = sizeof(long_u);
617# ifdef HW_USERMEM64
618 long_u physmem;
619# else
620 /* sysctl() may return 32 bit or 64 bit, accept both */
621 union {
622 int_u u32;
623 long_u u64;
624 } physmem;
625# endif
626
627 mib[0] = CTL_HW;
628# ifdef HW_USERMEM64
629 mib[1] = HW_USERMEM64;
630# else
631 mib[1] = HW_USERMEM;
632# endif
633 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
634 {
635# ifdef HW_USERMEM64
636 mem = (long_u)physmem;
637# else
638 if (len == sizeof(physmem.u64))
639 mem = (long_u)physmem.u64;
640 else
641 mem = (long_u)physmem.u32;
642# endif
643 }
644 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200647# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (mem == 0)
649 {
650 struct sysinfo sinfo;
651
652 /* Linux way of getting amount of RAM available */
653 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000654 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200655# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000656 /* avoid overflow as much as possible */
657 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
658 {
659 sinfo.mem_unit = sinfo.mem_unit >> 1;
660 --shiftright;
661 }
662 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200663# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200665# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200670# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (mem == 0)
672 {
673 long pagesize, pagecount;
674
675 /* Solaris way of getting amount of RAM available */
676 pagesize = sysconf(_SC_PAGESIZE);
677 pagecount = sysconf(_SC_PHYS_PAGES);
678 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000679 {
680 /* avoid overflow as much as possible */
681 while (shiftright > 0 && (pagesize & 1) == 0)
682 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000683 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000684 --shiftright;
685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 /* Return the minimum of the physical memory and the user limit, because
692 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200693# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 struct rlimit rlp;
696
697 if (getrlimit(RLIMIT_DATA, &rlp) == 0
698 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200699# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200701# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000702 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000704 {
705 mem = (long_u)rlp.rlim_cur;
706 shiftright = 10;
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
711 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000712 return mem >> shiftright;
713 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714}
715#endif
716
717 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100718mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000721#ifdef FEAT_MZSCHEME
722 long total = msec; /* remember original value */
723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725 if (ignoreinput)
726 {
727 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000728 * here. But we don't want QUIT to kill us (CTRL-\ used in a
729 * shell may produce SIGQUIT). */
730 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 old_tmode = curr_tmode;
732 if (curr_tmode == TMODE_RAW)
733 settmode(TMODE_SLEEP);
734
735 /*
736 * Everybody sleeps in a different way...
737 * Prefer nanosleep(), some versions of usleep() can only sleep up to
738 * one second.
739 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000740#ifdef FEAT_MZSCHEME
741 do
742 {
743 /* if total is large enough, wait by portions in p_mzq */
744 if (total > p_mzq)
745 msec = p_mzq;
746 else
747 msec = total;
748 total -= msec;
749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef HAVE_NANOSLEEP
751 {
752 struct timespec ts;
753
754 ts.tv_sec = msec / 1000;
755 ts.tv_nsec = (msec % 1000) * 1000000;
756 (void)nanosleep(&ts, NULL);
757 }
758#else
759# ifdef HAVE_USLEEP
760 while (msec >= 1000)
761 {
762 usleep((unsigned int)(999 * 1000));
763 msec -= 999;
764 }
765 usleep((unsigned int)(msec * 1000));
766# else
767# ifndef HAVE_SELECT
768 poll(NULL, 0, (int)msec);
769# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
771 struct timeval tv;
772
773 tv.tv_sec = msec / 1000;
774 tv.tv_usec = (msec % 1000) * 1000;
775 /*
776 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
777 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
778 */
779 select(0, NULL, NULL, NULL, &tv);
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781# endif /* HAVE_SELECT */
782# endif /* HAVE_NANOSLEEP */
783#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000784#ifdef FEAT_MZSCHEME
785 }
786 while (total > 0);
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000790 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200793 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
795
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000796#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
798# define HAVE_CHECK_STACK_GROWTH
799/*
800 * Support for checking for an almost-out-of-stack-space situation.
801 */
802
803/*
804 * Return a pointer to an item on the stack. Used to find out if the stack
805 * grows up or down.
806 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100807static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808static int stack_grows_downwards;
809
810/*
811 * Find out if the stack grows upwards or downwards.
812 * "p" points to a variable on the stack of the caller.
813 */
814 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100815check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 int i;
818
819 stack_grows_downwards = (p > (char *)&i);
820}
821#endif
822
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000823#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824static char *stack_limit = NULL;
825
826#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
827# include <pthread.h>
828# include <pthread_np.h>
829#endif
830
831/*
832 * Find out until how var the stack can grow without getting into trouble.
833 * Called when starting up and when switching to the signal stack in
834 * deathtrap().
835 */
836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100837get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 struct rlimit rlp;
840 int i;
841 long lim;
842
843 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
844 * limit doesn't fit in a long (rlim_cur might be "long long"). */
845 if (getrlimit(RLIMIT_STACK, &rlp) == 0
846 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
847# ifdef RLIM_INFINITY
848 && rlp.rlim_cur != RLIM_INFINITY
849# endif
850 )
851 {
852 lim = (long)rlp.rlim_cur;
853#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
854 {
855 pthread_attr_t attr;
856 size_t size;
857
858 /* On FreeBSD the initial thread always has a fixed stack size, no
859 * matter what the limits are set to. Normally it's 1 Mbyte. */
860 pthread_attr_init(&attr);
861 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
862 {
863 pthread_attr_getstacksize(&attr, &size);
864 if (lim > (long)size)
865 lim = (long)size;
866 }
867 pthread_attr_destroy(&attr);
868 }
869#endif
870 if (stack_grows_downwards)
871 {
872 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
873 if (stack_limit >= (char *)&i)
874 /* overflow, set to 1/16 of current stack position */
875 stack_limit = (char *)((long)&i / 16L);
876 }
877 else
878 {
879 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
880 if (stack_limit <= (char *)&i)
881 stack_limit = NULL; /* overflow */
882 }
883 }
884}
885
886/*
887 * Return FAIL when running out of stack space.
888 * "p" must point to any variable local to the caller that's on the stack.
889 */
890 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100891mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 if (stack_limit != NULL)
894 {
895 if (stack_grows_downwards)
896 {
897 if (p < stack_limit)
898 return FAIL;
899 }
900 else if (p > stack_limit)
901 return FAIL;
902 }
903 return OK;
904}
905#endif
906
907#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
908/*
909 * Support for using the signal stack.
910 * This helps when we run out of stack space, which causes a SIGSEGV. The
911 * signal handler then must run on another stack, since the normal stack is
912 * completely full.
913 */
914
915#ifndef SIGSTKSZ
916# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
917#endif
918
919# ifdef HAVE_SIGALTSTACK
920static stack_t sigstk; /* for sigaltstack() */
921# else
922static struct sigstack sigstk; /* for sigstack() */
923# endif
924
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100925static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926static char *signal_stack;
927
928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 if (signal_stack != NULL)
932 {
933# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934# ifdef HAVE_SS_BASE
935 sigstk.ss_base = signal_stack;
936# else
937 sigstk.ss_sp = signal_stack;
938# endif
939 sigstk.ss_size = SIGSTKSZ;
940 sigstk.ss_flags = 0;
941 (void)sigaltstack(&sigstk, NULL);
942# else
943 sigstk.ss_sp = signal_stack;
944 if (stack_grows_downwards)
945 sigstk.ss_sp += SIGSTKSZ - 1;
946 sigstk.ss_onstack = 0;
947 (void)sigstack(&sigstk, NULL);
948# endif
949 }
950}
951#endif
952
953/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000954 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * will barf when the second argument to signal() is ``wrong''.
956 * Let me try it with a few tricky defines from my own osdef.h (jw).
957 */
958#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 static RETSIGTYPE
960sig_winch SIGDEFARG(sigarg)
961{
962 /* this is not required on all systems, but it doesn't hurt anybody */
963 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
964 do_resize = TRUE;
965 SIGRETURN;
966}
967#endif
968
969#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 static RETSIGTYPE
971catch_sigint SIGDEFARG(sigarg)
972{
973 /* this is not required on all systems, but it doesn't hurt anybody */
974 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
975 got_int = TRUE;
976 SIGRETURN;
977}
978#endif
979
980#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 static RETSIGTYPE
982catch_sigpwr SIGDEFARG(sigarg)
983{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000984 /* this is not required on all systems, but it doesn't hurt anybody */
985 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 /*
987 * I'm not sure we get the SIGPWR signal when the system is really going
988 * down or when the batteries are almost empty. Just preserve the swap
989 * files and don't exit, that can't do any harm.
990 */
991 ml_sync_all(FALSE, FALSE);
992 SIGRETURN;
993}
994#endif
995
996#ifdef SET_SIG_ALARM
997/*
998 * signal function for alarm().
999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 static RETSIGTYPE
1001sig_alarm SIGDEFARG(sigarg)
1002{
1003 /* doesn't do anything, just to break a system call */
1004 sig_alarm_called = TRUE;
1005 SIGRETURN;
1006}
1007#endif
1008
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001009#if (defined(HAVE_SETJMP_H) \
1010 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1011 || defined(FEAT_LIBCALL))) \
1012 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013/*
1014 * A simplistic version of setjmp() that only allows one level of using.
1015 * Don't call twice before calling mch_endjmp()!.
1016 * Usage:
1017 * mch_startjmp();
1018 * if (SETJMP(lc_jump_env) != 0)
1019 * {
1020 * mch_didjmp();
1021 * EMSG("crash!");
1022 * }
1023 * else
1024 * {
1025 * do_the_work;
1026 * mch_endjmp();
1027 * }
1028 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1029 * not return before the saved environment is used.
1030 * Returns OK for normal return, FAIL when the protected code caused a
1031 * problem and LONGJMP() was used.
1032 */
1033 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001034mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036#ifdef SIGHASARG
1037 lc_signal = 0;
1038#endif
1039 lc_active = TRUE;
1040}
1041
1042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001043mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
1045 lc_active = FALSE;
1046}
1047
1048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1052 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1053 * otherwise catching the signal only works once. */
1054 init_signal_stack();
1055# endif
1056}
1057#endif
1058
1059/*
1060 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001061 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001063 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1064 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
1066 static RETSIGTYPE
1067deathtrap SIGDEFARG(sigarg)
1068{
1069 static int entered = 0; /* count the number of times we got here.
1070 Note: when memory has been corrupted
1071 this may get an arbitrary value! */
1072#ifdef SIGHASARG
1073 int i;
1074#endif
1075
1076#if defined(HAVE_SETJMP_H)
1077 /*
1078 * Catch a crash in protected code.
1079 * Restores the environment saved in lc_jump_env, which looks like
1080 * SETJMP() returns 1.
1081 */
1082 if (lc_active)
1083 {
1084# if defined(SIGHASARG)
1085 lc_signal = sigarg;
1086# endif
1087 lc_active = FALSE; /* don't jump again */
1088 LONGJMP(lc_jump_env, 1);
1089 /* NOTREACHED */
1090 }
1091#endif
1092
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001093#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001094# ifdef SIGQUIT
1095 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1096 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1097 * pressing CTRL-\, but we don't want Vim to exit then. */
1098 if (in_mch_delay && sigarg == SIGQUIT)
1099 SIGRETURN;
1100# endif
1101
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001102 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1103 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1104 * free(). Calling free() again may then cause a crash. */
1105 if (entered == 0
1106 && (0
1107# ifdef SIGHUP
1108 || sigarg == SIGHUP
1109# endif
1110# ifdef SIGQUIT
1111 || sigarg == SIGQUIT
1112# endif
1113# ifdef SIGTERM
1114 || sigarg == SIGTERM
1115# endif
1116# ifdef SIGPWR
1117 || sigarg == SIGPWR
1118# endif
1119# ifdef SIGUSR1
1120 || sigarg == SIGUSR1
1121# endif
1122# ifdef SIGUSR2
1123 || sigarg == SIGUSR2
1124# endif
1125 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001126 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001127 SIGRETURN;
1128#endif
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /* Remember how often we have been called. */
1131 ++entered;
1132
Bram Moolenaare429e702016-06-10 19:49:14 +02001133 /* Executing autocommands is likely to use more stack space than we have
1134 * available in the signal stack. */
1135 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_EVAL
1138 /* Set the v:dying variable. */
1139 set_vim_var_nr(VV_DYING, (long)entered);
1140#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001141 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001143#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /* Since we are now using the signal stack, need to reset the stack
1145 * limit. Otherwise using a regexp will fail. */
1146 get_stack_limit();
1147#endif
1148
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001149#if 0
1150 /* This is for opening gdb the moment Vim crashes.
1151 * You need to manually adjust the file name and Vim executable name.
1152 * Suggested by SungHyun Nam. */
1153 {
1154# define VI_GDB_FILE "/tmp/vimgdb"
1155# define VIM_NAME "/usr/bin/vim"
1156 FILE *fp = fopen(VI_GDB_FILE, "w");
1157 if (fp)
1158 {
1159 fprintf(fp,
1160 "file %s\n"
1161 "attach %d\n"
1162 "set height 1000\n"
1163 "bt full\n"
1164 , VIM_NAME, getpid());
1165 fclose(fp);
1166 system("xterm -e gdb -x "VI_GDB_FILE);
1167 unlink(VI_GDB_FILE);
1168 }
1169 }
1170#endif
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#ifdef SIGHASARG
1173 /* try to find the name of this signal */
1174 for (i = 0; signal_info[i].sig != -1; i++)
1175 if (sigarg == signal_info[i].sig)
1176 break;
1177 deadly_signal = sigarg;
1178#endif
1179
1180 full_screen = FALSE; /* don't write message to the GUI, it might be
1181 * part of the problem... */
1182 /*
1183 * If something goes wrong after entering here, we may get here again.
1184 * When this happens, give a message and try to exit nicely (resetting the
1185 * terminal mode, etc.)
1186 * When this happens twice, just exit, don't even try to give a message,
1187 * stack may be corrupt or something weird.
1188 * When this still happens again (or memory was corrupted in such a way
1189 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1190 */
1191 if (entered >= 3)
1192 {
1193 reset_signals(); /* don't catch any signals anymore */
1194 may_core_dump();
1195 if (entered >= 4)
1196 _exit(8);
1197 exit(7);
1198 }
1199 if (entered == 2)
1200 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001201 /* No translation, it may call malloc(). */
1202 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 out_flush();
1204 getout(1);
1205 }
1206
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001207 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001209 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 signal_info[i].name);
1211#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001212 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001214
1215 /* Preserve files and exit. This sets the really_exiting flag to prevent
1216 * calling free(). */
1217 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaare429e702016-06-10 19:49:14 +02001219 /* NOTREACHED */
1220
Bram Moolenaar009b2592004-10-24 19:18:58 +00001221#ifdef NBDEBUG
1222 reset_signals();
1223 may_core_dump();
1224 abort();
1225#endif
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 SIGRETURN;
1228}
1229
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001230#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231/*
1232 * On Solaris with multi-threading, suspending might not work immediately.
1233 * Catch the SIGCONT signal, which will be used as an indication whether the
1234 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001235 *
1236 * On Linux, signal is not always handled immediately either.
1237 * See https://bugs.launchpad.net/bugs/291373
1238 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001239 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001241static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001242static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244/*
1245 * signal handler for SIGCONT
1246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 static RETSIGTYPE
1248sigcont_handler SIGDEFARG(sigarg)
1249{
1250 sigcont_received = TRUE;
1251 SIGRETURN;
1252}
1253#endif
1254
Bram Moolenaar62b42182010-09-21 22:09:37 +02001255# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001256static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001257# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001258static void save_clipboard(void);
1259static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001260
1261static void *clip_star_save = NULL;
1262static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001263# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001264
1265/*
1266 * Called when Vim is going to sleep or execute a shell command.
1267 * We can't respond to requests for the X selections. Lose them, otherwise
1268 * other applications will hang. But first copy the text to cut buffer 0.
1269 */
1270 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001271loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001272{
1273 if (clip_star.owned || clip_plus.owned)
1274 {
1275 x11_export_final_selection();
1276 if (clip_star.owned)
1277 clip_lose_selection(&clip_star);
1278 if (clip_plus.owned)
1279 clip_lose_selection(&clip_plus);
1280 if (x11_display != NULL)
1281 XFlush(x11_display);
1282 }
1283}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001284
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001285# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001286/*
1287 * Save clipboard text to restore later.
1288 */
1289 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001290save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001291{
1292 if (clip_star.owned)
1293 clip_star_save = get_register('*', TRUE);
1294 if (clip_plus.owned)
1295 clip_plus_save = get_register('+', TRUE);
1296}
1297
1298/*
1299 * Restore clipboard text if no one own the X selection.
1300 */
1301 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001302restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001303{
1304 if (clip_star_save != NULL)
1305 {
1306 if (!clip_gen_owner_exists(&clip_star))
1307 put_register('*', clip_star_save);
1308 else
1309 free_register(clip_star_save);
1310 clip_star_save = NULL;
1311 }
1312 if (clip_plus_save != NULL)
1313 {
1314 if (!clip_gen_owner_exists(&clip_plus))
1315 put_register('+', clip_plus_save);
1316 else
1317 free_register(clip_plus_save);
1318 clip_plus_save = NULL;
1319 }
1320}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001321# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001322#endif
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/*
1325 * If the machine has job control, use it to suspend the program,
1326 * otherwise fake it by starting a new shell.
1327 */
1328 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001329mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
1331 /* BeOS does have SIGTSTP, but it doesn't work. */
1332#if defined(SIGTSTP) && !defined(__BEOS__)
1333 out_flush(); /* needed to make cursor visible on some systems */
1334 settmode(TMODE_COOK);
1335 out_flush(); /* needed to disable mouse on some systems */
1336
1337# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001338 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339# endif
1340
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001341# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 sigcont_received = FALSE;
1343# endif
1344 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001345# if defined(_REENTRANT) && defined(SIGCONT)
1346 /*
1347 * Wait for the SIGCONT signal to be handled. It generally happens
1348 * immediately, but somehow not all the time. Do not call pause()
1349 * because there would be race condition which would hang Vim if
1350 * signal happened in between the test of sigcont_received and the
1351 * call to pause(). If signal is not yet received, call sleep(0)
1352 * to just yield CPU. Signal should then be received. If somehow
1353 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1354 * further if signal is not received after 1+2+3+4 ms (not expected
1355 * to happen).
1356 */
1357 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001358 long wait_time;
1359 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001360 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001361 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363# endif
1364
1365# ifdef FEAT_TITLE
1366 /*
1367 * Set oldtitle to NULL, so the current title is obtained again.
1368 */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001369 VIM_CLEAR(oldtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370# endif
1371 settmode(TMODE_RAW);
1372 need_check_timestamps = TRUE;
1373 did_check_timestamps = FALSE;
1374#else
1375 suspend_shell();
1376#endif
1377}
1378
1379 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001380mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 Columns = 80;
1383 Rows = 24;
1384
1385 out_flush();
1386 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001387
Bram Moolenaar56718732006-03-15 22:53:57 +00001388#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001389 mac_conv_init();
1390#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001391#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1392 win_clip_init();
1393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394}
1395
1396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399#if defined(SIGWINCH)
1400 /*
1401 * WINDOW CHANGE signal is handled with sig_winch().
1402 */
1403 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1404#endif
1405
1406 /*
1407 * We want the STOP signal to work, to make mch_suspend() work.
1408 * For "rvim" the STOP signal is ignored.
1409 */
1410#ifdef SIGTSTP
1411 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1412#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001413#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 signal(SIGCONT, sigcont_handler);
1415#endif
1416
1417 /*
1418 * We want to ignore breaking of PIPEs.
1419 */
1420#ifdef SIGPIPE
1421 signal(SIGPIPE, SIG_IGN);
1422#endif
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001425 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#endif
1427
1428 /*
1429 * Ignore alarm signals (Perl's alarm() generates it).
1430 */
1431#ifdef SIGALRM
1432 signal(SIGALRM, SIG_IGN);
1433#endif
1434
1435 /*
1436 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1437 * work will be lost.
1438 */
1439#ifdef SIGPWR
1440 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1441#endif
1442
1443 /*
1444 * Arrange for other signals to gracefully shutdown Vim.
1445 */
1446 catch_signals(deathtrap, SIG_ERR);
1447
1448#if defined(FEAT_GUI) && defined(SIGHUP)
1449 /*
1450 * When the GUI is running, ignore the hangup signal.
1451 */
1452 if (gui.in_use)
1453 signal(SIGHUP, SIG_IGN);
1454#endif
1455}
1456
Bram Moolenaardf177f62005-02-22 08:39:57 +00001457#if defined(SIGINT) || defined(PROTO)
1458/*
1459 * Catch CTRL-C (only works while in Cooked mode).
1460 */
1461 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001462catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001463{
1464 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1465}
1466#endif
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001469reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001472#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 /* SIGCONT isn't in the list, because its default action is ignore */
1474 signal(SIGCONT, SIG_DFL);
1475#endif
1476}
1477
1478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001479catch_signals(
1480 RETSIGTYPE (*func_deadly)(),
1481 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 int i;
1484
1485 for (i = 0; signal_info[i].sig != -1; i++)
1486 if (signal_info[i].deadly)
1487 {
1488#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1489 struct sigaction sa;
1490
1491 /* Setup to use the alternate stack for the signal function. */
1492 sa.sa_handler = func_deadly;
1493 sigemptyset(&sa.sa_mask);
1494# if defined(__linux__) && defined(_REENTRANT)
1495 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1496 * thread handling in combination with using the alternate stack:
1497 * pthread library functions try to use the stack pointer to
1498 * identify the current thread, causing a SEGV signal, which
1499 * recursively calls deathtrap() and hangs. */
1500 sa.sa_flags = 0;
1501# else
1502 sa.sa_flags = SA_ONSTACK;
1503# endif
1504 sigaction(signal_info[i].sig, &sa, NULL);
1505#else
1506# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1507 struct sigvec sv;
1508
1509 /* Setup to use the alternate stack for the signal function. */
1510 sv.sv_handler = func_deadly;
1511 sv.sv_mask = 0;
1512 sv.sv_flags = SV_ONSTACK;
1513 sigvec(signal_info[i].sig, &sv, NULL);
1514# else
1515 signal(signal_info[i].sig, func_deadly);
1516# endif
1517#endif
1518 }
1519 else if (func_other != SIG_ERR)
1520 signal(signal_info[i].sig, func_other);
1521}
1522
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001523#ifdef HAVE_SIGPROCMASK
1524 static void
1525block_signals(sigset_t *set)
1526{
1527 sigset_t newset;
1528 int i;
1529
1530 sigemptyset(&newset);
1531
1532 for (i = 0; signal_info[i].sig != -1; i++)
1533 sigaddset(&newset, signal_info[i].sig);
1534
1535# if defined(_REENTRANT) && defined(SIGCONT)
1536 /* SIGCONT isn't in the list, because its default action is ignore */
1537 sigaddset(&newset, SIGCONT);
1538# endif
1539
1540 sigprocmask(SIG_BLOCK, &newset, set);
1541}
1542
1543 static void
1544unblock_signals(sigset_t *set)
1545{
1546 sigprocmask(SIG_SETMASK, set, NULL);
1547}
1548#endif
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001551 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001552 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1553 * return TRUE
1554 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1555 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001556 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001557 * Returns TRUE when Vim should exit.
1558 */
1559 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001560vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001561{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001562 static int got_signal = 0;
1563 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001564
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001565 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001566 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001567 case SIGNAL_BLOCK: blocked = TRUE;
1568 break;
1569
1570 case SIGNAL_UNBLOCK: blocked = FALSE;
1571 if (got_signal != 0)
1572 {
1573 kill(getpid(), got_signal);
1574 got_signal = 0;
1575 }
1576 break;
1577
1578 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001579 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001580 got_signal = sig;
1581#ifdef SIGPWR
1582 if (sig != SIGPWR)
1583#endif
1584 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001585 break;
1586 }
1587 return FALSE;
1588}
1589
1590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 * Check_win checks whether we have an interactive stdout.
1592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001594mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (isatty(1))
1597 return OK;
1598 return FAIL;
1599}
1600
1601/*
1602 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1603 */
1604 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001605mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 if (isatty(read_cmd_fd))
1608 return TRUE;
1609 return FALSE;
1610}
1611
1612#ifdef FEAT_X11
1613
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001614# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
1618 * Give a message about the elapsed time for opening the X window.
1619 */
1620 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001621xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001623 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624}
1625# endif
1626#endif
1627
1628#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1629/*
1630 * A few functions shared by X11 title and clipboard code.
1631 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001632static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1633static int x_error_check(Display *dpy, XErrorEvent *error_event);
1634static int x_connect_to_server(void);
1635static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637static int got_x_error = FALSE;
1638
1639/*
1640 * X Error handler, otherwise X just exits! (very rude) -- webb
1641 */
1642 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001643x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001645 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 STRCAT(IObuff, _("\nVim: Got X error\n"));
1647
1648 /* We cannot print a message and continue, because no X calls are allowed
1649 * here (causes my system to hang). Silently continuing might be an
1650 * alternative... */
1651 preserve_exit(); /* preserve files and exit */
1652
1653 return 0; /* NOTREACHED */
1654}
1655
1656/*
1657 * Another X Error handler, just used to check for errors.
1658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001660x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 got_x_error = TRUE;
1663 return 0;
1664}
1665
1666#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1667# if defined(HAVE_SETJMP_H)
1668/*
1669 * An X IO Error handler, used to catch error while opening the display.
1670 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001671static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001674x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 /* This function should not return, it causes exit(). Longjump instead. */
1677 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001678# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001679 return 0; /* avoid the compiler complains about missing return value */
1680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681}
1682# endif
1683
1684/*
1685 * An X IO Error handler, used to catch terminal errors.
1686 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001687static int x_IOerror_handler(Display *dpy);
1688static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001689static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001692x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001695 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 x11_window = 0;
1697 x11_display = NULL;
1698 xterm_Shell = (Widget)0;
1699
1700 /* This function should not return, it causes exit(). Longjump instead. */
1701 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001702# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001703 return 0; /* avoid the compiler complains about missing return value */
1704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001706
1707/*
1708 * If the X11 connection was lost try to restore it.
1709 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001710 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001711 */
1712 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001713may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001714{
1715 if (xterm_dpy_was_reset)
1716 {
1717 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001718
1719# ifndef LESSTIF_VERSION
1720 /* This has been reported to avoid Vim getting stuck. */
1721 if (app_context != (XtAppContext)NULL)
1722 {
1723 XtDestroyApplicationContext(app_context);
1724 app_context = (XtAppContext)NULL;
1725 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1726 }
1727# endif
1728
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001729 setup_term_clip();
1730 get_x11_title(FALSE);
1731 }
1732}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
1734
1735/*
1736 * Return TRUE when connection to the X server is desired.
1737 */
1738 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001739x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#if defined(FEAT_CLIENTSERVER)
1742 if (x_force_connect)
1743 return TRUE;
1744#endif
1745 if (x_no_connect)
1746 return FALSE;
1747
1748 /* Check for a match with "exclude:" from 'clipboard'. */
1749 if (clip_exclude_prog != NULL)
1750 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001751 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 return FALSE;
1753 }
1754 return TRUE;
1755}
1756
1757/*
1758 * Test if "dpy" and x11_window are valid by getting the window title.
1759 * I don't actually want it yet, so there may be a simpler call to use, but
1760 * this will cause the error handler x_error_check() to be called if anything
1761 * is wrong, such as the window pointer being invalid (as can happen when the
1762 * user changes his DISPLAY, but not his WINDOWID) -- webb
1763 */
1764 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001765test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 int (*old_handler)();
1768 XTextProperty text_prop;
1769
1770 old_handler = XSetErrorHandler(x_error_check);
1771 got_x_error = FALSE;
1772 if (XGetWMName(dpy, x11_window, &text_prop))
1773 XFree((void *)text_prop.value);
1774 XSync(dpy, False);
1775 (void)XSetErrorHandler(old_handler);
1776
1777 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001778 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 return (got_x_error ? FAIL : OK);
1781}
1782#endif
1783
1784#ifdef FEAT_TITLE
1785
1786#ifdef FEAT_X11
1787
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001788static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790/*
1791 * try to get x11 window and display
1792 *
1793 * return FAIL for failure, OK otherwise
1794 */
1795 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001796get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 char *winid;
1799 static int result = -1;
1800#define XD_NONE 0 /* x11_display not set here */
1801#define XD_HERE 1 /* x11_display opened here */
1802#define XD_GUI 2 /* x11_display used from gui.dpy */
1803#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1804 static int x11_display_from = XD_NONE;
1805 static int did_set_error_handler = FALSE;
1806
1807 if (!did_set_error_handler)
1808 {
1809 /* X just exits if it finds an error otherwise! */
1810 (void)XSetErrorHandler(x_error_handler);
1811 did_set_error_handler = TRUE;
1812 }
1813
Bram Moolenaar9372a112005-12-06 19:59:18 +00001814#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (gui.in_use)
1816 {
1817 /*
1818 * If the X11 display was opened here before, for the window where Vim
1819 * was started, close that one now to avoid a memory leak.
1820 */
1821 if (x11_display_from == XD_HERE && x11_display != NULL)
1822 {
1823 XCloseDisplay(x11_display);
1824 x11_display_from = XD_NONE;
1825 }
1826 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1827 {
1828 x11_display_from = XD_GUI;
1829 return OK;
1830 }
1831 x11_display = NULL;
1832 return FAIL;
1833 }
1834 else if (x11_display_from == XD_GUI)
1835 {
1836 /* GUI must have stopped somehow, clear x11_display */
1837 x11_window = 0;
1838 x11_display = NULL;
1839 x11_display_from = XD_NONE;
1840 }
1841#endif
1842
1843 /* When started with the "-X" argument, don't try connecting. */
1844 if (!x_connect_to_server())
1845 return FAIL;
1846
1847 /*
1848 * If WINDOWID not set, should try another method to find out
1849 * what the current window number is. The only code I know for
1850 * this is very complicated.
1851 * We assume that zero is invalid for WINDOWID.
1852 */
1853 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1854 x11_window = (Window)atol(winid);
1855
1856#ifdef FEAT_XCLIPBOARD
1857 if (xterm_dpy != NULL && x11_window != 0)
1858 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001859 /* We may have checked it already, but Gnome terminal can move us to
1860 * another window, so we need to check every time. */
1861 if (x11_display_from != XD_XTERM)
1862 {
1863 /*
1864 * If the X11 display was opened here before, for the window where
1865 * Vim was started, close that one now to avoid a memory leak.
1866 */
1867 if (x11_display_from == XD_HERE && x11_display != NULL)
1868 XCloseDisplay(x11_display);
1869 x11_display = xterm_dpy;
1870 x11_display_from = XD_XTERM;
1871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (test_x11_window(x11_display) == FAIL)
1873 {
1874 /* probably bad $WINDOWID */
1875 x11_window = 0;
1876 x11_display = NULL;
1877 x11_display_from = XD_NONE;
1878 return FAIL;
1879 }
1880 return OK;
1881 }
1882#endif
1883
1884 if (x11_window == 0 || x11_display == NULL)
1885 result = -1;
1886
1887 if (result != -1) /* Have already been here and set this */
1888 return result; /* Don't do all these X calls again */
1889
1890 if (x11_window != 0 && x11_display == NULL)
1891 {
1892#ifdef SET_SIG_ALARM
1893 RETSIGTYPE (*sig_save)();
1894#endif
1895#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1896 struct timeval start_tv;
1897
1898 if (p_verbose > 0)
1899 gettimeofday(&start_tv, NULL);
1900#endif
1901
1902#ifdef SET_SIG_ALARM
1903 /*
1904 * Opening the Display may hang if the DISPLAY setting is wrong, or
1905 * the network connection is bad. Set an alarm timer to get out.
1906 */
1907 sig_alarm_called = FALSE;
1908 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1909 (RETSIGTYPE (*)())sig_alarm);
1910 alarm(2);
1911#endif
1912 x11_display = XOpenDisplay(NULL);
1913
1914#ifdef SET_SIG_ALARM
1915 alarm(0);
1916 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1917 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001918 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919#endif
1920 if (x11_display != NULL)
1921 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001922# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001924 {
1925 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001926 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001927 verbose_leave();
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929# endif
1930 if (test_x11_window(x11_display) == FAIL)
1931 {
1932 /* Maybe window id is bad */
1933 x11_window = 0;
1934 XCloseDisplay(x11_display);
1935 x11_display = NULL;
1936 }
1937 else
1938 x11_display_from = XD_HERE;
1939 }
1940 }
1941 if (x11_window == 0 || x11_display == NULL)
1942 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001943
1944# ifdef FEAT_EVAL
1945 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1946# endif
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 return (result = OK);
1949}
1950
1951/*
1952 * Determine original x11 Window Title
1953 */
1954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001955get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001957 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958}
1959
1960/*
1961 * Determine original x11 Window icon
1962 */
1963 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001964get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
1966 int retval = FALSE;
1967
1968 retval = get_x11_thing(FALSE, test_only);
1969
1970 /* could not get old icon, use terminal name */
1971 if (oldicon == NULL && !test_only)
1972 {
1973 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001974 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001976 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
1979 return retval;
1980}
1981
1982 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001983get_x11_thing(
1984 int get_title, /* get title string */
1985 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 XTextProperty text_prop;
1988 int retval = FALSE;
1989 Status status;
1990
1991 if (get_x11_windis() == OK)
1992 {
1993 /* Get window/icon name if any */
1994 if (get_title)
1995 status = XGetWMName(x11_display, x11_window, &text_prop);
1996 else
1997 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1998
1999 /*
2000 * If terminal is xterm, then x11_window may be a child window of the
2001 * outer xterm window that actually contains the window/icon name, so
2002 * keep traversing up the tree until a window with a title/icon is
2003 * found.
2004 */
2005 /* Previously this was only done for xterm and alikes. I don't see a
2006 * reason why it would fail for other terminal emulators.
2007 * if (term_is_xterm) */
2008 {
2009 Window root;
2010 Window parent;
2011 Window win = x11_window;
2012 Window *children;
2013 unsigned int num_children;
2014
2015 while (!status || text_prop.value == NULL)
2016 {
2017 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2018 &num_children))
2019 break;
2020 if (children)
2021 XFree((void *)children);
2022 if (parent == root || parent == 0)
2023 break;
2024
2025 win = parent;
2026 if (get_title)
2027 status = XGetWMName(x11_display, win, &text_prop);
2028 else
2029 status = XGetWMIconName(x11_display, win, &text_prop);
2030 }
2031 }
2032 if (status && text_prop.value != NULL)
2033 {
2034 retval = TRUE;
2035 if (!test_only)
2036 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002037#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2038 if (text_prop.encoding == XA_STRING
2039# ifdef FEAT_MBYTE
2040 && !has_mbyte
2041# endif
2042 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
2044#endif
2045 if (get_title)
2046 oldtitle = vim_strsave((char_u *)text_prop.value);
2047 else
2048 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002049#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 else
2052 {
2053 char **cl;
2054 Status transform_status;
2055 int n = 0;
2056
2057 transform_status = XmbTextPropertyToTextList(x11_display,
2058 &text_prop,
2059 &cl, &n);
2060 if (transform_status >= Success && n > 0 && cl[0])
2061 {
2062 if (get_title)
2063 oldtitle = vim_strsave((char_u *) cl[0]);
2064 else
2065 oldicon = vim_strsave((char_u *) cl[0]);
2066 XFreeStringList(cl);
2067 }
2068 else
2069 {
2070 if (get_title)
2071 oldtitle = vim_strsave((char_u *)text_prop.value);
2072 else
2073 oldicon = vim_strsave((char_u *)text_prop.value);
2074 }
2075 }
2076#endif
2077 }
2078 XFree((void *)text_prop.value);
2079 }
2080 }
2081 return retval;
2082}
2083
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002084/* Xutf8 functions are not avaialble on older systems. Note that on some
2085 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2086 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2087 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002089# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090# define USE_UTF8_STRING
2091# endif
2092#endif
2093
2094/*
2095 * Set x11 Window Title
2096 *
2097 * get_x11_windis() must be called before this and have returned OK
2098 */
2099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002100set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101{
2102 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2103 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2104 * supported everywhere and STRING doesn't work for multi-byte titles.
2105 */
2106#ifdef USE_UTF8_STRING
2107 if (enc_utf8)
2108 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2109 NULL, NULL, 0, NULL, NULL, NULL);
2110 else
2111#endif
2112 {
2113#if XtSpecificationRelease >= 4
2114# ifdef FEAT_XFONTSET
2115 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2116 NULL, NULL, 0, NULL, NULL, NULL);
2117# else
2118 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002119 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002122 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 XSetWMProperties(x11_display, x11_window, &text_prop,
2124 NULL, NULL, 0, NULL, NULL, NULL);
2125# endif
2126#else
2127 XStoreName(x11_display, x11_window, (char *)title);
2128#endif
2129 }
2130 XFlush(x11_display);
2131}
2132
2133/*
2134 * Set x11 Window icon
2135 *
2136 * get_x11_windis() must be called before this and have returned OK
2137 */
2138 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002139set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 /* See above for comments about using X*SetWMProperties(). */
2142#ifdef USE_UTF8_STRING
2143 if (enc_utf8)
2144 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2145 NULL, 0, NULL, NULL, NULL);
2146 else
2147#endif
2148 {
2149#if XtSpecificationRelease >= 4
2150# ifdef FEAT_XFONTSET
2151 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2152 NULL, 0, NULL, NULL, NULL);
2153# else
2154 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002155 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002157 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2159 NULL, 0, NULL, NULL, NULL);
2160# endif
2161#else
2162 XSetIconName(x11_display, x11_window, (char *)icon);
2163#endif
2164 }
2165 XFlush(x11_display);
2166}
2167
2168#else /* FEAT_X11 */
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002171get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 return FALSE;
2174}
2175
2176 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002177get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 if (!test_only)
2180 {
2181 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002182 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002184 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186 return FALSE;
2187}
2188
2189#endif /* FEAT_X11 */
2190
2191 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002192mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
2194 return get_x11_title(TRUE);
2195}
2196
2197 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002198mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 return get_x11_icon(TRUE);
2201}
2202
2203/*
2204 * Set the window title and icon.
2205 */
2206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002207mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 int type = 0;
2210 static int recursive = 0;
2211
2212 if (T_NAME == NULL) /* no terminal name (yet) */
2213 return;
2214 if (title == NULL && icon == NULL) /* nothing to do */
2215 return;
2216
2217 /* When one of the X11 functions causes a deadly signal, we get here again
2218 * recursively. Avoid hanging then (something is probably locked). */
2219 if (recursive)
2220 return;
2221 ++recursive;
2222
2223 /*
2224 * if the window ID and the display is known, we may use X11 calls
2225 */
2226#ifdef FEAT_X11
2227 if (get_x11_windis() == OK)
2228 type = 1;
2229#else
2230# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2231 if (gui.in_use)
2232 type = 1;
2233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235
2236 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002237 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 * than x11 calls, because the x11 calls don't always work
2239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if ((type || *T_TS != NUL) && title != NULL)
2241 {
2242 if (oldtitle == NULL
2243#ifdef FEAT_GUI
2244 && !gui.in_use
2245#endif
2246 ) /* first call but not in GUI, save title */
2247 (void)get_x11_title(FALSE);
2248
2249 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2250 term_settitle(title);
2251#ifdef FEAT_X11
2252 else
2253# ifdef FEAT_GUI_GTK
2254 if (!gui.in_use) /* don't do this if GTK+ is running */
2255# endif
2256 set_x11_title(title); /* x11 */
2257#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002258#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2260 else
2261 gui_mch_settitle(title, icon);
2262#endif
2263 did_set_title = TRUE;
2264 }
2265
2266 if ((type || *T_CIS != NUL) && icon != NULL)
2267 {
2268 if (oldicon == NULL
2269#ifdef FEAT_GUI
2270 && !gui.in_use
2271#endif
2272 ) /* first call, save icon */
2273 get_x11_icon(FALSE);
2274
2275 if (*T_CIS != NUL)
2276 {
2277 out_str(T_CIS); /* set icon start */
2278 out_str_nf(icon);
2279 out_str(T_CIE); /* set icon end */
2280 out_flush();
2281 }
2282#ifdef FEAT_X11
2283 else
2284# ifdef FEAT_GUI_GTK
2285 if (!gui.in_use) /* don't do this if GTK+ is running */
2286# endif
2287 set_x11_icon(icon); /* x11 */
2288#endif
2289 did_set_icon = TRUE;
2290 }
2291 --recursive;
2292}
2293
2294/*
2295 * Restore the window/icon title.
2296 * "which" is one of:
2297 * 1 only restore title
2298 * 2 only restore icon
2299 * 3 restore title and icon
2300 */
2301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002302mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304 /* only restore the title or icon when it has been set */
2305 mch_settitle(((which & 1) && did_set_title) ?
2306 (oldtitle ? oldtitle : p_titleold) : NULL,
2307 ((which & 2) && did_set_icon) ? oldicon : NULL);
2308}
2309
2310#endif /* FEAT_TITLE */
2311
2312/*
2313 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002314 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 */
2316 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002317vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 if (name == NULL)
2320 return FALSE;
2321 return (STRNICMP(name, "xterm", 5) == 0
2322 || STRNICMP(name, "nxterm", 6) == 0
2323 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002324 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002326 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 || STRCMP(name, "builtin_xterm") == 0);
2328}
2329
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002330#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2331/*
2332 * Return TRUE if "name" appears to be that of a terminal
2333 * known to support the xterm-style mouse protocol.
2334 * Relies on term_is_xterm having been set to its correct value.
2335 */
2336 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002337use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002338{
2339 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002340 && (term_is_xterm
2341 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002342 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002343 || STRICMP(name, "st") == 0
2344 || STRNICMP(name, "st-", 3) == 0
2345 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002346}
2347#endif
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2350/*
2351 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2352 * Return 1 for "xterm".
2353 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002354 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002355 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
2357 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002358use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002360 if (ttym_flags == TTYM_SGR)
2361 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002362 if (ttym_flags == TTYM_URXVT)
2363 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (ttym_flags == TTYM_XTERM2)
2365 return 2;
2366 if (ttym_flags == TTYM_XTERM)
2367 return 1;
2368 return 0;
2369}
2370#endif
2371
2372 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002373vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 if (name == NULL)
2376 return FALSE;
2377 return (STRNICMP(name, "iris-ansi", 9) == 0
2378 || STRCMP(name, "builtin_iris-ansi") == 0);
2379}
2380
2381 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002382vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384 if (name == NULL)
2385 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002386 /* catch VT100 - VT5xx */
2387 return ((STRNICMP(name, "vt", 2) == 0
2388 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 || STRCMP(name, "builtin_vt320") == 0);
2390}
2391
2392/*
2393 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2394 * This should include all windowed terminal emulators.
2395 */
2396 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002397vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399 if (name == NULL)
2400 return FALSE;
2401 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2402 return TRUE;
2403 return ( STRNICMP(name, "hpterm", 6) == 0
2404 || STRNICMP(name, "sun-cmd", 7) == 0
2405 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002406 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 || STRNICMP(name, "dtterm", 6) == 0);
2408}
2409
2410/*
2411 * Insert user name in s[len].
2412 * Return OK if a name found.
2413 */
2414 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002415mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002418 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return OK;
2420#else
2421 return mch_get_uname(getuid(), s, len);
2422#endif
2423}
2424
2425/*
2426 * Insert user name for "uid" in s[len].
2427 * Return OK if a name found.
2428 */
2429 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002430mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2433 struct passwd *pw;
2434
2435 if ((pw = getpwuid(uid)) != NULL
2436 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2437 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002438 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 return OK;
2440 }
2441#endif
2442 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2443 return FAIL; /* a number is not a name */
2444}
2445
2446/*
2447 * Insert host name is s[len].
2448 */
2449
2450#ifdef HAVE_SYS_UTSNAME_H
2451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002452mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 struct utsname vutsname;
2455
2456 if (uname(&vutsname) < 0)
2457 *s = NUL;
2458 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002459 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460}
2461#else /* HAVE_SYS_UTSNAME_H */
2462
2463# ifdef HAVE_SYS_SYSTEMINFO_H
2464# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2465# endif
2466
2467 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002468mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470# ifdef VAXC
2471 vaxc$gethostname((char *)s, len);
2472# else
2473 gethostname((char *)s, len);
2474# endif
2475 s[len - 1] = NUL; /* make sure it's terminated */
2476}
2477#endif /* HAVE_SYS_UTSNAME_H */
2478
2479/*
2480 * return process ID
2481 */
2482 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002483mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 return (long)getpid();
2486}
2487
2488#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002489static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002492strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 extern int sys_nerr;
2495 extern char *sys_errlist[];
2496 static char er[20];
2497
2498 if (err > 0 && err < sys_nerr)
2499 return (sys_errlist[err]);
2500 sprintf(er, "Error %d", err);
2501 return er;
2502}
2503#endif
2504
2505/*
2506 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2507 * Return OK for success, FAIL for failure.
2508 */
2509 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002510mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512#if defined(USE_GETCWD)
2513 if (getcwd((char *)buf, len) == NULL)
2514 {
2515 STRCPY(buf, strerror(errno));
2516 return FAIL;
2517 }
2518 return OK;
2519#else
2520 return (getwd((char *)buf) != NULL ? OK : FAIL);
2521#endif
2522}
2523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002525 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 *
2527 * return FAIL for failure, OK for success
2528 */
2529 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002530mch_FullName(
2531 char_u *fname,
2532 char_u *buf,
2533 int len,
2534 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002537#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 int fd = -1;
2539 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 char_u olddir[MAXPATHL];
2542 char_u *p;
2543 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002544#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002545 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2546 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002547#endif
2548
Bram Moolenaar38323e42007-03-06 19:22:53 +00002549#ifdef VMS
2550 fname = vms_fixfilename(fname);
2551#endif
2552
Bram Moolenaara2442432007-04-26 14:26:37 +00002553#ifdef __CYGWIN__
2554 /*
2555 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2556 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002557# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002558 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2559 * a forward slash. */
2560 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2561 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002562# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002563 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002564# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002565 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002566#endif
2567
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002568 /* Expand it if forced or not an absolute path.
2569 * Do not do it for "/file", the result is always "/". */
2570 if ((force || !mch_isFullName(fname))
2571 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 /*
2574 * If the file name has a path, change to that directory for a moment,
2575 * and then do the getwd() (and get back to where we were).
2576 * This will get the correct path name with "../" things.
2577 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002578 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002580#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 /*
2582 * Use fchdir() if possible, it's said to be faster and more
2583 * reliable. But on SunOS 4 it might not work. Check this by
2584 * doing a fchdir() right now.
2585 */
2586 if (!dont_fchdir)
2587 {
2588 fd = open(".", O_RDONLY | O_EXTRA, 0);
2589 if (fd >= 0 && fchdir(fd) < 0)
2590 {
2591 close(fd);
2592 fd = -1;
2593 dont_fchdir = TRUE; /* don't try again */
2594 }
2595 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 /* Only change directory when we are sure we can return to where
2599 * we are now. After doing "su" chdir(".") might not work. */
2600 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002601#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 (mch_dirname(olddir, MAXPATHL) == FAIL
2605 || mch_chdir((char *)olddir) != 0))
2606 {
2607 p = NULL; /* can't get current dir: don't chdir */
2608 retval = FAIL;
2609 }
2610 else
2611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 /* The directory is copied into buf[], to be able to remove
2613 * the file name without changing it (could be a string in
2614 * read-only memory) */
2615 if (p - fname >= len)
2616 retval = FAIL;
2617 else
2618 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002619 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (mch_chdir((char *)buf))
2621 retval = FAIL;
2622 else
2623 fname = p + 1;
2624 *buf = NUL;
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627 }
2628 if (mch_dirname(buf, len) == FAIL)
2629 {
2630 retval = FAIL;
2631 *buf = NUL;
2632 }
2633 if (p != NULL)
2634 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002635#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (fd >= 0)
2637 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002638 if (p_verbose >= 5)
2639 {
2640 verbose_enter();
2641 MSG("fchdir() to previous dir");
2642 verbose_leave();
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 l = fchdir(fd);
2645 close(fd);
2646 }
2647 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 l = mch_chdir((char *)olddir);
2650 if (l != 0)
2651 EMSG(_(e_prev_dir));
2652 }
2653
2654 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002655 if (l >= len - 1)
2656 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002657#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002658 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002660 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002665 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 return FAIL;
2667
2668 /* Do not append ".", "/dir/." is equal to "/dir". */
2669 if (STRCMP(fname, ".") != 0)
2670 STRCAT(buf, fname);
2671
2672 return OK;
2673}
2674
2675/*
2676 * Return TRUE if "fname" does not depend on the current directory.
2677 */
2678 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002679mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002681#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return ( fname[0] == '/' || fname[0] == '.' ||
2683 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2684 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2685 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002686#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#endif
2689}
2690
Bram Moolenaar24552be2005-12-10 20:17:30 +00002691#if defined(USE_FNAME_CASE) || defined(PROTO)
2692/*
2693 * Set the case of the file name, if it already exists. This will cause the
2694 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002695 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002696 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002698fname_case(
2699 char_u *name,
2700 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002701{
2702 struct stat st;
2703 char_u *slash, *tail;
2704 DIR *dirp;
2705 struct dirent *dp;
2706
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002707 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002708 {
2709 /* Open the directory where the file is located. */
2710 slash = vim_strrchr(name, '/');
2711 if (slash == NULL)
2712 {
2713 dirp = opendir(".");
2714 tail = name;
2715 }
2716 else
2717 {
2718 *slash = NUL;
2719 dirp = opendir((char *)name);
2720 *slash = '/';
2721 tail = slash + 1;
2722 }
2723
2724 if (dirp != NULL)
2725 {
2726 while ((dp = readdir(dirp)) != NULL)
2727 {
2728 /* Only accept names that differ in case and are the same byte
2729 * length. TODO: accept different length name. */
2730 if (STRICMP(tail, dp->d_name) == 0
2731 && STRLEN(tail) == STRLEN(dp->d_name))
2732 {
2733 char_u newname[MAXPATHL + 1];
2734 struct stat st2;
2735
2736 /* Verify the inode is equal. */
2737 vim_strncpy(newname, name, MAXPATHL);
2738 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2739 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002740 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002741 && st.st_ino == st2.st_ino
2742 && st.st_dev == st2.st_dev)
2743 {
2744 STRCPY(tail, dp->d_name);
2745 break;
2746 }
2747 }
2748 }
2749
2750 closedir(dirp);
2751 }
2752 }
2753}
2754#endif
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756/*
2757 * Get file permissions for 'name'.
2758 * Returns -1 when it doesn't exist.
2759 */
2760 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002761mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
2763 struct stat statb;
2764
2765 /* Keep the #ifdef outside of stat(), it may be a macro. */
2766#ifdef VMS
2767 if (stat((char *)vms_fixfilename(name), &statb))
2768#else
2769 if (stat((char *)name, &statb))
2770#endif
2771 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002772#ifdef __INTERIX
2773 /* The top bit makes the value negative, which means the file doesn't
2774 * exist. Remove the bit, we don't use it. */
2775 return statb.st_mode & ~S_ADDACE;
2776#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779}
2780
2781/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002782 * Set file permission for "name" to "perm".
2783 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 */
2785 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002786mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 return (chmod((char *)
2789#ifdef VMS
2790 vms_fixfilename(name),
2791#else
2792 name,
2793#endif
2794 (mode_t)perm) == 0 ? OK : FAIL);
2795}
2796
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002797#if defined(HAVE_FCHMOD) || defined(PROTO)
2798/*
2799 * Set file permission for open file "fd" to "perm".
2800 * Return FAIL for failure, OK otherwise.
2801 */
2802 int
2803mch_fsetperm(int fd, long perm)
2804{
2805 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2806}
2807#endif
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#if defined(HAVE_ACL) || defined(PROTO)
2810# ifdef HAVE_SYS_ACL_H
2811# include <sys/acl.h>
2812# endif
2813# ifdef HAVE_SYS_ACCESS_H
2814# include <sys/access.h>
2815# endif
2816
2817# ifdef HAVE_SOLARIS_ACL
2818typedef struct vim_acl_solaris_T {
2819 int acl_cnt;
2820 aclent_t *acl_entry;
2821} vim_acl_solaris_T;
2822# endif
2823
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002824#if defined(HAVE_SELINUX) || defined(PROTO)
2825/*
2826 * Copy security info from "from_file" to "to_file".
2827 */
2828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002829mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002830{
2831 if (from_file == NULL)
2832 return;
2833
2834 if (selinux_enabled == -1)
2835 selinux_enabled = is_selinux_enabled();
2836
2837 if (selinux_enabled > 0)
2838 {
2839 security_context_t from_context = NULL;
2840 security_context_t to_context = NULL;
2841
2842 if (getfilecon((char *)from_file, &from_context) < 0)
2843 {
2844 /* If the filesystem doesn't support extended attributes,
2845 the original had no special security context and the
2846 target cannot have one either. */
2847 if (errno == EOPNOTSUPP)
2848 return;
2849
2850 MSG_PUTS(_("\nCould not get security context for "));
2851 msg_outtrans(from_file);
2852 msg_putchar('\n');
2853 return;
2854 }
2855 if (getfilecon((char *)to_file, &to_context) < 0)
2856 {
2857 MSG_PUTS(_("\nCould not get security context for "));
2858 msg_outtrans(to_file);
2859 msg_putchar('\n');
2860 freecon (from_context);
2861 return ;
2862 }
2863 if (strcmp(from_context, to_context) != 0)
2864 {
2865 if (setfilecon((char *)to_file, from_context) < 0)
2866 {
2867 MSG_PUTS(_("\nCould not set security context for "));
2868 msg_outtrans(to_file);
2869 msg_putchar('\n');
2870 }
2871 }
2872 freecon(to_context);
2873 freecon(from_context);
2874 }
2875}
2876#endif /* HAVE_SELINUX */
2877
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002878#if defined(HAVE_SMACK) && !defined(PROTO)
2879/*
2880 * Copy security info from "from_file" to "to_file".
2881 */
2882 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002883mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002884{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002885 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002886 {
2887 XATTR_NAME_SMACK,
2888 XATTR_NAME_SMACKEXEC,
2889 XATTR_NAME_SMACKMMAP
2890 };
2891
2892 char buffer[SMACK_LABEL_LEN];
2893 const char *name;
2894 int index;
2895 int ret;
2896 ssize_t size;
2897
2898 if (from_file == NULL)
2899 return;
2900
2901 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2902 / sizeof(smack_copied_attributes)[0]) ; index++)
2903 {
2904 /* get the name of the attribute to copy */
2905 name = smack_copied_attributes[index];
2906
2907 /* get the value of the attribute in buffer */
2908 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2909 if (size >= 0)
2910 {
2911 /* copy the attribute value of buffer */
2912 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2913 if (ret < 0)
2914 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002915 vim_snprintf((char *)IObuff, IOSIZE,
2916 _("Could not set security context %s for %s"),
2917 name, to_file);
2918 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002919 msg_putchar('\n');
2920 }
2921 }
2922 else
2923 {
2924 /* what reason of not having the attribute value? */
2925 switch (errno)
2926 {
2927 case ENOTSUP:
2928 /* extended attributes aren't supported or enabled */
2929 /* should a message be echoed? not sure... */
2930 return; /* leave because it isn't usefull to continue */
2931
2932 case ERANGE:
2933 default:
2934 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002935 vim_snprintf((char *)IObuff, IOSIZE,
2936 _("Could not get security context %s for %s. Removing it!"),
2937 name, from_file);
2938 msg_puts(IObuff);
2939 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002940 /* FALLTHROUGH to remove the attribute */
2941
2942 case ENODATA:
2943 /* no attribute of this name */
2944 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002945 /* Silently ignore errors, apparently this happens when
2946 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002947 break;
2948 }
2949 }
2950 }
2951}
2952#endif /* HAVE_SMACK */
2953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954/*
2955 * Return a pointer to the ACL of file "fname" in allocated memory.
2956 * Return NULL if the ACL is not available for whatever reason.
2957 */
2958 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002959mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
2961 vim_acl_T ret = NULL;
2962#ifdef HAVE_POSIX_ACL
2963 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2964#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002965#ifdef HAVE_SOLARIS_ZFS_ACL
2966 acl_t *aclent;
2967
2968 if (acl_get((char *)fname, 0, &aclent) < 0)
2969 return NULL;
2970 ret = (vim_acl_T)aclent;
2971#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#ifdef HAVE_SOLARIS_ACL
2973 vim_acl_solaris_T *aclent;
2974
2975 aclent = malloc(sizeof(vim_acl_solaris_T));
2976 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2977 {
2978 free(aclent);
2979 return NULL;
2980 }
2981 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2982 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2983 {
2984 free(aclent->acl_entry);
2985 free(aclent);
2986 return NULL;
2987 }
2988 ret = (vim_acl_T)aclent;
2989#else
2990#if defined(HAVE_AIX_ACL)
2991 int aclsize;
2992 struct acl *aclent;
2993
2994 aclsize = sizeof(struct acl);
2995 aclent = malloc(aclsize);
2996 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2997 {
2998 if (errno == ENOSPC)
2999 {
3000 aclsize = aclent->acl_len;
3001 aclent = realloc(aclent, aclsize);
3002 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3003 {
3004 free(aclent);
3005 return NULL;
3006 }
3007 }
3008 else
3009 {
3010 free(aclent);
3011 return NULL;
3012 }
3013 }
3014 ret = (vim_acl_T)aclent;
3015#endif /* HAVE_AIX_ACL */
3016#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003017#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018#endif /* HAVE_POSIX_ACL */
3019 return ret;
3020}
3021
3022/*
3023 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3024 */
3025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003026mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 if (aclent == NULL)
3029 return;
3030#ifdef HAVE_POSIX_ACL
3031 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3032#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003033#ifdef HAVE_SOLARIS_ZFS_ACL
3034 acl_set((char *)fname, (acl_t *)aclent);
3035#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#ifdef HAVE_SOLARIS_ACL
3037 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3038 ((vim_acl_solaris_T *)aclent)->acl_entry);
3039#else
3040#ifdef HAVE_AIX_ACL
3041 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3042#endif /* HAVE_AIX_ACL */
3043#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003044#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045#endif /* HAVE_POSIX_ACL */
3046}
3047
3048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003049mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 if (aclent == NULL)
3052 return;
3053#ifdef HAVE_POSIX_ACL
3054 acl_free((acl_t)aclent);
3055#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003056#ifdef HAVE_SOLARIS_ZFS_ACL
3057 acl_free((acl_t *)aclent);
3058#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#ifdef HAVE_SOLARIS_ACL
3060 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3061 free(aclent);
3062#else
3063#ifdef HAVE_AIX_ACL
3064 free(aclent);
3065#endif /* HAVE_AIX_ACL */
3066#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003067#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068#endif /* HAVE_POSIX_ACL */
3069}
3070#endif
3071
3072/*
3073 * Set hidden flag for "name".
3074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003076mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 /* can't hide a file */
3079}
3080
3081/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003082 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * return FALSE if "name" is not a directory
3084 * return FALSE for error
3085 */
3086 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003087mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 struct stat statb;
3090
3091 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3092 return FALSE;
3093 if (stat((char *)name, &statb))
3094 return FALSE;
3095#ifdef _POSIX_SOURCE
3096 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3097#else
3098 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3099#endif
3100}
3101
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003102/*
3103 * return TRUE if "name" is a directory, NOT a symlink to a directory
3104 * return FALSE if "name" is not a directory
3105 * return FALSE for error
3106 */
3107 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003108mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003109{
3110 struct stat statb;
3111
3112 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3113 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003114 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003115 return FALSE;
3116#ifdef _POSIX_SOURCE
3117 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3118#else
3119 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3120#endif
3121}
3122
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003123static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125/*
3126 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3127 */
3128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003129executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 struct stat st;
3132
3133 if (stat((char *)name, &st))
3134 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003135#ifdef VMS
3136 /* Like on Unix system file can have executable rights but not necessarily
3137 * be an executable, but on Unix is not a default for an ordianry file to
3138 * have an executable flag - on VMS it is in most cases.
3139 * Therefore, this check does not have any sense - let keep us to the
3140 * conventions instead:
3141 * *.COM and *.EXE files are the executables - the rest are not. This is
3142 * not ideal but better then it was.
3143 */
3144 int vms_executable = 0;
3145 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3146 {
3147 if (strstr(vms_tolower((char*)name),".exe") != NULL
3148 || strstr(vms_tolower((char*)name),".com")!= NULL)
3149 vms_executable = 1;
3150 }
3151 return vms_executable;
3152#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155}
3156
3157/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003158 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003159 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 * Return -1 if unknown.
3161 */
3162 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003163mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 char_u *buf;
3166 char_u *p, *e;
3167 int retval;
3168
Bram Moolenaarb5971142015-03-21 17:32:19 +01003169 /* When "use_path" is false and if it's an absolute or relative path don't
3170 * need to use $PATH. */
3171 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3172 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003173 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003174 /* There must be a path separator, files in the current directory
3175 * can't be executed. */
3176 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003177 {
3178 if (path != NULL)
3179 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003180 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003181 *path = FullName_save(name, TRUE);
3182 else
3183 *path = vim_strsave(name);
3184 }
3185 return TRUE;
3186 }
3187 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 p = (char_u *)getenv("PATH");
3191 if (p == NULL || *p == NUL)
3192 return -1;
3193 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3194 if (buf == NULL)
3195 return -1;
3196
3197 /*
3198 * Walk through all entries in $PATH to check if "name" exists there and
3199 * is an executable file.
3200 */
3201 for (;;)
3202 {
3203 e = (char_u *)strchr((char *)p, ':');
3204 if (e == NULL)
3205 e = p + STRLEN(p);
3206 if (e - p <= 1) /* empty entry means current dir */
3207 STRCPY(buf, "./");
3208 else
3209 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003210 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 add_pathsep(buf);
3212 }
3213 STRCAT(buf, name);
3214 retval = executable_file(buf);
3215 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003216 {
3217 if (path != NULL)
3218 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003219 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003220 *path = FullName_save(buf, TRUE);
3221 else
3222 *path = vim_strsave(buf);
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
3227 if (*e != ':')
3228 break;
3229 p = e + 1;
3230 }
3231
3232 vim_free(buf);
3233 return retval;
3234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236/*
3237 * Check what "name" is:
3238 * NODE_NORMAL: file or directory (or doesn't exist)
3239 * NODE_WRITABLE: writable device, socket, fifo, etc.
3240 * NODE_OTHER: non-writable things
3241 */
3242 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003243mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 struct stat st;
3246
3247 if (stat((char *)name, &st))
3248 return NODE_NORMAL;
3249 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3250 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3252 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 /* Everything else is writable? */
3254 return NODE_WRITABLE;
3255}
3256
3257 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003258mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260#ifdef HAVE_CHECK_STACK_GROWTH
3261 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 check_stack_growth((char *)&i);
3264
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003265# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 get_stack_limit();
3267# endif
3268
3269#endif
3270
3271 /*
3272 * Setup an alternative stack for signals. Helps to catch signals when
3273 * running out of stack space.
3274 * Use of sigaltstack() is preferred, it's more portable.
3275 * Ignore any errors.
3276 */
3277#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003278 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 init_signal_stack();
3280#endif
3281}
3282
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003283#if defined(EXITFREE) || defined(PROTO)
3284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003285mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003286{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3288 if (clip_star.owned)
3289 clip_lose_selection(&clip_star);
3290 if (clip_plus.owned)
3291 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003292# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003293# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003294 if (xterm_Shell != (Widget)0)
3295 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003296# ifndef LESSTIF_VERSION
3297 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003298 if (xterm_dpy != NULL)
3299 XtCloseDisplay(xterm_dpy);
3300 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003301 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003302 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003303# ifdef FEAT_X11
3304 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3305# endif
3306 }
3307# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003308# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003309# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003310 if (x11_display != NULL
3311# ifdef FEAT_XCLIPBOARD
3312 && x11_display != xterm_dpy
3313# endif
3314 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 XCloseDisplay(x11_display);
3316# endif
3317# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003318 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319# endif
3320# ifdef FEAT_TITLE
3321 vim_free(oldtitle);
3322 vim_free(oldicon);
3323# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003324}
3325#endif
3326
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003327static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329/*
3330 * Output a newline when exiting.
3331 * Make sure the newline goes to the same stream as the text.
3332 */
3333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003334exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003336 if (silent_mode)
3337 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (newline_on_exit || msg_didout)
3339 {
3340 if (msg_use_printf())
3341 {
3342 if (info_message)
3343 mch_msg("\n");
3344 else
3345 mch_errmsg("\r\n");
3346 }
3347 else
3348 out_char('\n');
3349 }
3350 else
3351 {
3352 restore_cterm_colors(); /* get original colors back */
3353 msg_clr_eos_force(); /* clear the rest of the display */
3354 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3355 }
3356}
3357
3358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003359mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 exiting = TRUE;
3362
3363#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3364 x11_export_final_selection();
3365#endif
3366
3367#ifdef FEAT_GUI
3368 if (!gui.in_use)
3369#endif
3370 {
3371 settmode(TMODE_COOK);
3372#ifdef FEAT_TITLE
3373 mch_restore_title(3); /* restore xterm title and icon name */
3374#endif
3375 /*
3376 * When t_ti is not empty but it doesn't cause swapping terminal
3377 * pages, need to output a newline when msg_didout is set. But when
3378 * t_ti does swap pages it should not go to the shell page. Do this
3379 * before stoptermcap().
3380 */
3381 if (swapping_screen() && !newline_on_exit)
3382 exit_scroll();
3383
3384 /* Stop termcap: May need to check for T_CRV response, which
3385 * requires RAW mode. */
3386 stoptermcap();
3387
3388 /*
3389 * A newline is only required after a message in the alternate screen.
3390 * This is set to TRUE by wait_return().
3391 */
3392 if (!swapping_screen() || newline_on_exit)
3393 exit_scroll();
3394
3395 /* Cursor may have been switched off without calling starttermcap()
3396 * when doing "vim -u vimrc" and vimrc contains ":q". */
3397 if (full_screen)
3398 cursor_on();
3399 }
3400 out_flush();
3401 ml_close_all(TRUE); /* remove all memfiles */
3402 may_core_dump();
3403#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 gui_exit(r);
3406#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003407
Bram Moolenaar56718732006-03-15 22:53:57 +00003408#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003409 mac_conv_cleanup();
3410#endif
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#ifdef __QNX__
3413 /* A core dump won't be created if the signal handler
3414 * doesn't return, so we can't call exit() */
3415 if (deadly_signal != 0)
3416 return;
3417#endif
3418
Bram Moolenaar009b2592004-10-24 19:18:58 +00003419#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003420 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003421#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003422
3423#ifdef EXITFREE
3424 free_all_mem();
3425#endif
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 exit(r);
3428}
3429
3430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003431may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
3433 if (deadly_signal != 0)
3434 {
3435 signal(deadly_signal, SIG_DFL);
3436 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3437 }
3438}
3439
3440#ifndef VMS
3441
3442 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003443mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 static int first = TRUE;
3446
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003447#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448# ifdef HAVE_TERMIOS_H
3449 static struct termios told;
3450 struct termios tnew;
3451# else
3452 static struct termio told;
3453 struct termio tnew;
3454# endif
3455
3456 if (first)
3457 {
3458 first = FALSE;
3459# if defined(HAVE_TERMIOS_H)
3460 tcgetattr(read_cmd_fd, &told);
3461# else
3462 ioctl(read_cmd_fd, TCGETA, &told);
3463# endif
3464 }
3465
3466 tnew = told;
3467 if (tmode == TMODE_RAW)
3468 {
3469 /*
3470 * ~ICRNL enables typing ^V^M
3471 */
3472 tnew.c_iflag &= ~ICRNL;
3473 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3474# if defined(IEXTEN) && !defined(__MINT__)
3475 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3476 /* but it breaks function keys on MINT */
3477# endif
3478 );
3479# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3480 tnew.c_oflag &= ~ONLCR;
3481# endif
3482 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3483 tnew.c_cc[VTIME] = 0; /* don't wait */
3484 }
3485 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003486 {
3487 /* Also reset ICANON here, otherwise on Solaris select() won't see
3488 * typeahead characters. */
3489 tnew.c_lflag &= ~(ICANON | ECHO);
3490 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3491 tnew.c_cc[VTIME] = 0; /* don't wait */
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494# if defined(HAVE_TERMIOS_H)
3495 {
3496 int n = 10;
3497
3498 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3499 * few times. */
3500 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3501 && errno == EINTR && n > 0)
3502 --n;
3503 }
3504# else
3505 ioctl(read_cmd_fd, TCSETA, &tnew);
3506# endif
3507
3508#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 /*
3510 * for "old" tty systems
3511 */
3512# ifndef TIOCSETN
3513# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3514# endif
3515 static struct sgttyb ttybold;
3516 struct sgttyb ttybnew;
3517
3518 if (first)
3519 {
3520 first = FALSE;
3521 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3522 }
3523
3524 ttybnew = ttybold;
3525 if (tmode == TMODE_RAW)
3526 {
3527 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3528 ttybnew.sg_flags |= RAW;
3529 }
3530 else if (tmode == TMODE_SLEEP)
3531 ttybnew.sg_flags &= ~(ECHO);
3532 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3533#endif
3534 curr_tmode = tmode;
3535}
3536
3537/*
3538 * Try to get the code for "t_kb" from the stty setting
3539 *
3540 * Even if termcap claims a backspace key, the user's setting *should*
3541 * prevail. stty knows more about reality than termcap does, and if
3542 * somebody's usual erase key is DEL (which, for most BSD users, it will
3543 * be), they're going to get really annoyed if their erase key starts
3544 * doing forward deletes for no reason. (Eric Fischer)
3545 */
3546 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003547get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003549 ttyinfo_T info;
3550 char_u buf[2];
3551 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003553 if (get_tty_info(read_cmd_fd, &info) == OK)
3554 {
3555 intr_char = info.interrupt;
3556 buf[0] = info.backspace;
3557 buf[1] = NUL;
3558 add_termcode((char_u *)"kb", buf, FALSE);
3559
3560 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3561 p = find_termcode((char_u *)"kD");
3562 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3563 do_fixdel(NULL);
3564 }
3565}
3566
3567/*
3568 * Obtain the characters that Backspace and Enter produce on "fd".
3569 * Returns OK or FAIL.
3570 */
3571 int
3572get_tty_info(int fd, ttyinfo_T *info)
3573{
3574#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575# ifdef HAVE_TERMIOS_H
3576 struct termios keys;
3577# else
3578 struct termio keys;
3579# endif
3580
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003581 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003583 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003585 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003589 info->backspace = keys.c_cc[VERASE];
3590 info->interrupt = keys.c_cc[VINTR];
3591 if (keys.c_iflag & ICRNL)
3592 info->enter = NL;
3593 else
3594 info->enter = CAR;
3595 if (keys.c_oflag & ONLCR)
3596 info->nl_does_cr = TRUE;
3597 else
3598 info->nl_does_cr = FALSE;
3599 return OK;
3600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601#else
3602 /* for "old" tty systems */
3603 struct sgttyb keys;
3604
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003605 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003607 info->backspace = keys.sg_erase;
3608 info->interrupt = keys.sg_kill;
3609 info->enter = CAR;
3610 info->nl_does_cr = TRUE;
3611 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615}
3616
3617#endif /* VMS */
3618
3619#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003620static int mouse_ison = FALSE;
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622/*
3623 * Set mouse clicks on or off.
3624 */
3625 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003626mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003628# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003629 static int bevalterm_ison = FALSE;
3630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 int xterm_mouse_vers;
3632
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003633 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003634# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003635 && p_bevalterm == bevalterm_ison
3636# endif
3637 )
3638 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return;
3640
3641 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003642
3643# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003644 if (ttym_flags == TTYM_URXVT)
3645 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003646 out_str_nf((char_u *)
3647 (on
3648 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3649 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003650 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003651 }
3652# endif
3653
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003654# ifdef FEAT_MOUSE_SGR
3655 if (ttym_flags == TTYM_SGR)
3656 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003657 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003658 out_str_nf((char_u *)
3659 (on
3660 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3661 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003662 mouse_ison = on;
3663 }
3664# endif
3665
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003666# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003667 if (bevalterm_ison != (p_bevalterm && on))
3668 {
3669 bevalterm_ison = (p_bevalterm && on);
3670 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3671 /* disable mouse movement events, enabling is below */
3672 out_str_nf((char_u *)
3673 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003674 }
3675# endif
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (xterm_mouse_vers > 0)
3678 {
3679 if (on) /* enable mouse events, use mouse tracking if available */
3680 out_str_nf((char_u *)
3681 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003682 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003683# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003684 bevalterm_ison
3685 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3686# endif
3687 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3689 else /* disable mouse events, could probably always send the same */
3690 out_str_nf((char_u *)
3691 (xterm_mouse_vers > 1
3692 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3693 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003694 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697# ifdef FEAT_MOUSE_DEC
3698 else if (ttym_flags == TTYM_DEC)
3699 {
3700 if (on) /* enable mouse events */
3701 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3702 else /* disable mouse events */
3703 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003704 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706# endif
3707
3708# ifdef FEAT_MOUSE_GPM
3709 else
3710 {
3711 if (on)
3712 {
3713 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003714 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 else
3717 {
3718 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003719 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721 }
3722# endif
3723
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003724# ifdef FEAT_SYSMOUSE
3725 else
3726 {
3727 if (on)
3728 {
3729 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003730 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003731 }
3732 else
3733 {
3734 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003736 }
3737 }
3738# endif
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740# ifdef FEAT_MOUSE_JSB
3741 else
3742 {
3743 if (on)
3744 {
3745 /* D - Enable Mouse up/down messages
3746 * L - Enable Left Button Reporting
3747 * M - Enable Middle Button Reporting
3748 * R - Enable Right Button Reporting
3749 * K - Enable SHIFT and CTRL key Reporting
3750 * + - Enable Advanced messaging of mouse moves and up/down messages
3751 * Q - Quiet No Ack
3752 * # - Numeric value of mouse pointer required
3753 * 0 = Multiview 2000 cursor, used as standard
3754 * 1 = Windows Arrow
3755 * 2 = Windows I Beam
3756 * 3 = Windows Hour Glass
3757 * 4 = Windows Cross Hair
3758 * 5 = Windows UP Arrow
3759 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003760# ifdef JSBTERM_MOUSE_NONADVANCED
3761 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3763 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003764# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3766 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003767# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003768 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 else
3771 {
3772 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3773 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003774 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 }
3777# endif
3778# ifdef FEAT_MOUSE_PTERM
3779 else
3780 {
3781 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3782 if (on)
3783 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3784 else
3785 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003786 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788# endif
3789}
3790
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003791#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003792/*
3793 * Called when 'balloonevalterm' changed.
3794 */
3795 void
3796mch_bevalterm_changed(void)
3797{
3798 mch_setmouse(mouse_ison);
3799}
3800#endif
3801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802/*
3803 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3804 */
3805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003806check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808# ifdef FEAT_MOUSE_XTERM
3809 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003810# ifdef FEAT_MOUSE_URXVT
3811 && use_xterm_mouse() != 3
3812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813# ifdef FEAT_GUI
3814 && !gui.in_use
3815# endif
3816 )
3817 {
3818 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003819 ? IF_EB("\233M", CSI_STR "M")
3820 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (*p_mouse != NUL)
3822 {
3823 /* force mouse off and maybe on to send possibly new mouse
3824 * activation sequence to the xterm, with(out) drag tracing. */
3825 mch_setmouse(FALSE);
3826 setmouse();
3827 }
3828 }
3829 else
3830 del_mouse_termcode(KS_MOUSE);
3831# endif
3832
3833# ifdef FEAT_MOUSE_GPM
3834 if (!use_xterm_mouse()
3835# ifdef FEAT_GUI
3836 && !gui.in_use
3837# endif
3838 )
3839 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3840# endif
3841
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003842# ifdef FEAT_SYSMOUSE
3843 if (!use_xterm_mouse()
3844# ifdef FEAT_GUI
3845 && !gui.in_use
3846# endif
3847 )
3848 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3849# endif
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003852 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (!use_xterm_mouse()
3854# ifdef FEAT_GUI
3855 && !gui.in_use
3856# endif
3857 )
3858 set_mouse_termcode(KS_JSBTERM_MOUSE,
3859 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3860 else
3861 del_mouse_termcode(KS_JSBTERM_MOUSE);
3862# endif
3863
3864# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003865 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * define it in the GUI or when using an xterm. */
3867 if (!use_xterm_mouse()
3868# ifdef FEAT_GUI
3869 && !gui.in_use
3870# endif
3871 )
3872 set_mouse_termcode(KS_NETTERM_MOUSE,
3873 (char_u *)IF_EB("\033}", ESC_STR "}"));
3874 else
3875 del_mouse_termcode(KS_NETTERM_MOUSE);
3876# endif
3877
3878# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003879 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003880 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881# ifdef FEAT_GUI
3882 && !gui.in_use
3883# endif
3884 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003885 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3886 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 else
3888 del_mouse_termcode(KS_DEC_MOUSE);
3889# endif
3890# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003891 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003892 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893# ifdef FEAT_GUI
3894 && !gui.in_use
3895# endif
3896 )
3897 set_mouse_termcode(KS_PTERM_MOUSE,
3898 (char_u *) IF_EB("\033[", ESC_STR "["));
3899 else
3900 del_mouse_termcode(KS_PTERM_MOUSE);
3901# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003902# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003903 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003904# ifdef FEAT_GUI
3905 && !gui.in_use
3906# endif
3907 )
3908 {
3909 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003910 ? IF_EB("\233*M", CSI_STR "*M")
3911 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003912
3913 if (*p_mouse != NUL)
3914 {
3915 mch_setmouse(FALSE);
3916 setmouse();
3917 }
3918 }
3919 else
3920 del_mouse_termcode(KS_URXVT_MOUSE);
3921# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003922# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003923 if (use_xterm_mouse() == 4
3924# ifdef FEAT_GUI
3925 && !gui.in_use
3926# endif
3927 )
3928 {
3929 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003930 ? IF_EB("\233<*M", CSI_STR "<*M")
3931 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3932
3933 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3934 ? IF_EB("\233<*m", CSI_STR "<*m")
3935 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003936
3937 if (*p_mouse != NUL)
3938 {
3939 mch_setmouse(FALSE);
3940 setmouse();
3941 }
3942 }
3943 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003944 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003945 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003946 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3947 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950#endif
3951
3952/*
3953 * set screen mode, always fails.
3954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003956mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 EMSG(_(e_screenmode));
3959 return FAIL;
3960}
3961
3962#ifndef VMS
3963
3964/*
3965 * Try to get the current window size:
3966 * 1. with an ioctl(), most accurate method
3967 * 2. from the environment variables LINES and COLUMNS
3968 * 3. from the termcap
3969 * 4. keep using the old values
3970 * Return OK when size could be determined, FAIL otherwise.
3971 */
3972 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003973mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 long rows = 0;
3976 long columns = 0;
3977 char_u *p;
3978
3979 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 * 1. try using an ioctl. It is the most accurate method.
3981 *
3982 * Try using TIOCGWINSZ first, some systems that have it also define
3983 * TIOCGSIZE but don't have a struct ttysize.
3984 */
3985# ifdef TIOCGWINSZ
3986 {
3987 struct winsize ws;
3988 int fd = 1;
3989
3990 /* When stdout is not a tty, use stdin for the ioctl(). */
3991 if (!isatty(fd) && isatty(read_cmd_fd))
3992 fd = read_cmd_fd;
3993 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3994 {
3995 columns = ws.ws_col;
3996 rows = ws.ws_row;
3997 }
3998 }
3999# else /* TIOCGWINSZ */
4000# ifdef TIOCGSIZE
4001 {
4002 struct ttysize ts;
4003 int fd = 1;
4004
4005 /* When stdout is not a tty, use stdin for the ioctl(). */
4006 if (!isatty(fd) && isatty(read_cmd_fd))
4007 fd = read_cmd_fd;
4008 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4009 {
4010 columns = ts.ts_cols;
4011 rows = ts.ts_lines;
4012 }
4013 }
4014# endif /* TIOCGSIZE */
4015# endif /* TIOCGWINSZ */
4016
4017 /*
4018 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004019 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4020 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004022 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
4024 if ((p = (char_u *)getenv("LINES")))
4025 rows = atoi((char *)p);
4026 if ((p = (char_u *)getenv("COLUMNS")))
4027 columns = atoi((char *)p);
4028 }
4029
4030#ifdef HAVE_TGETENT
4031 /*
4032 * 3. try reading "co" and "li" entries from termcap
4033 */
4034 if (columns == 0 || rows == 0)
4035 getlinecol(&columns, &rows);
4036#endif
4037
4038 /*
4039 * 4. If everything fails, use the old values
4040 */
4041 if (columns <= 0 || rows <= 0)
4042 return FAIL;
4043
4044 Rows = rows;
4045 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004046 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return OK;
4048}
4049
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004050#if defined(FEAT_TERMINAL) || defined(PROTO)
4051/*
4052 * Report the windows size "rows" and "cols" to tty "fd".
4053 */
4054 int
4055mch_report_winsize(int fd, int rows, int cols)
4056{
4057# ifdef TIOCSWINSZ
4058 struct winsize ws;
4059
4060 ws.ws_col = cols;
4061 ws.ws_row = rows;
4062 ws.ws_xpixel = cols * 5;
4063 ws.ws_ypixel = rows * 10;
4064 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4065 {
4066 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4067 return OK;
4068 }
4069 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4070# else
4071# ifdef TIOCSSIZE
4072 struct ttysize ts;
4073
4074 ts.ts_cols = cols;
4075 ts.ts_lines = rows;
4076 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4077 {
4078 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4079 return OK;
4080 }
4081 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4082# endif
4083# endif
4084 return FAIL;
4085}
4086#endif
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088/*
4089 * Try to set the window size to Rows and Columns.
4090 */
4091 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004092mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
4094 if (*T_CWS)
4095 {
4096 /*
4097 * NOTE: if you get an error here that term_set_winsize() is
4098 * undefined, check the output of configure. It could probably not
4099 * find a ncurses, termcap or termlib library.
4100 */
4101 term_set_winsize((int)Rows, (int)Columns);
4102 out_flush();
4103 screen_start(); /* don't know where cursor is now */
4104 }
4105}
4106
4107#endif /* VMS */
4108
4109/*
4110 * Rows and/or Columns has changed.
4111 */
4112 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004113mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
4115 /* Nothing to do. */
4116}
4117
Bram Moolenaar205b8862011-09-07 15:04:31 +02004118/*
4119 * Wait for process "child" to end.
4120 * Return "child" if it exited properly, <= 0 on error.
4121 */
4122 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004123wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004124{
4125 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004126 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004127
4128 while (wait_pid != child)
4129 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004130 /* When compiled with Python threads are probably used, in which case
4131 * wait() sometimes hangs for no obvious reason. Use waitpid()
4132 * instead and loop (like the GUI). Also needed for other interfaces,
4133 * they might call system(). */
4134# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004135 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004136# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004137 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004138# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004139 if (wait_pid == 0)
4140 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004141 /* Wait for 1 to 10 msec before trying again. */
4142 mch_delay(delay_msec, TRUE);
4143 if (++delay_msec > 10)
4144 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004145 continue;
4146 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004147 if (wait_pid <= 0
4148# ifdef ECHILD
4149 && errno == ECHILD
4150# endif
4151 )
4152 break;
4153 }
4154 return wait_pid;
4155}
4156
Bram Moolenaar13568252018-03-16 20:46:58 +01004157#if defined(FEAT_JOB_CHANNEL) \
4158 || !defined(USE_SYSTEM) \
4159 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)) \
4160 || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01004161/*
4162 * Parse "cmd" and put the white-separated parts in "argv".
Bram Moolenaar13568252018-03-16 20:46:58 +01004163 * "argv" is an allocated array with "argc" entries and room for 4 more.
Bram Moolenaar72801402016-02-09 11:37:50 +01004164 * Returns FAIL when out of memory.
4165 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004166 int
4167mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
4168{
4169 int i;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004170 char_u *p, *d;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004171 int inquote;
4172
4173 /*
4174 * Do this loop twice:
4175 * 1: find number of arguments
4176 * 2: separate them and build argv[]
4177 */
4178 for (i = 0; i < 2; ++i)
4179 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02004180 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004181 inquote = FALSE;
4182 *argc = 0;
4183 for (;;)
4184 {
4185 if (i == 1)
4186 (*argv)[*argc] = (char *)p;
4187 ++*argc;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004188 d = p;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004189 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4190 {
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004191 if (p[0] == '"')
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004192 /* quotes surrounding an argument and are dropped */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004193 inquote = !inquote;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004194 else
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004195 {
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004196 if (p[0] == '\\' && p[1] != NUL)
4197 {
4198 /* First pass: skip over "\ " and "\"".
4199 * Second pass: Remove the backslash. */
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004200 ++p;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004201 }
4202 if (i == 1)
4203 *d++ = *p;
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004204 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01004205 ++p;
4206 }
4207 if (*p == NUL)
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004208 {
4209 if (i == 1)
4210 *d++ = NUL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004211 break;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004212 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01004213 if (i == 1)
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004214 *d++ = NUL;
4215 p = skipwhite(p + 1);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004216 }
4217 if (*argv == NULL)
4218 {
4219 if (use_shcf)
4220 {
4221 /* Account for possible multiple args in p_shcf. */
4222 p = p_shcf;
4223 for (;;)
4224 {
4225 p = skiptowhite(p);
4226 if (*p == NUL)
4227 break;
4228 ++*argc;
4229 p = skipwhite(p);
4230 }
4231 }
4232
4233 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4234 if (*argv == NULL) /* out of memory */
4235 return FAIL;
4236 }
4237 }
4238 return OK;
4239}
4240#endif
4241
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004242#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004243/*
4244 * Set the environment for a child process.
4245 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004246 static void
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004247set_child_environment(long rows, long columns, char *term)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004248{
4249# ifdef HAVE_SETENV
4250 char envbuf[50];
4251# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004252 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004255 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004256 static char envbuf_Colors[20];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004257# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004258 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004259# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004260# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004261 long colors =
4262# ifdef FEAT_GUI
4263 gui.in_use ? 256*256*256 :
4264# endif
4265 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004266
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004267# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004268 setenv("TERM", term, 1);
4269 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004271 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004272 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004273 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004274 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004275 sprintf((char *)envbuf, "%ld", colors);
4276 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004277# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004278 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004279# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004280# else
4281 /*
4282 * Putenv does not copy the string, it has to remain valid.
4283 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004284 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004285 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004286 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4287 putenv(envbuf_Term);
4288 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004289 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004290 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4291 putenv(envbuf_Lines);
4292 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4293 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004294 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004295 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4296 putenv(envbuf_Colors);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004297# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004298 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4299 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4300 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004301# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004302# endif
4303}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004304
4305 static void
4306set_default_child_environment(void)
4307{
4308 set_child_environment(Rows, Columns, "dumb");
4309}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004310#endif
4311
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004312#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004313/*
4314 * Open a PTY, with FD for the master and slave side.
4315 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4316 * When successful both file descriptors are stored.
4317 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004318 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004319open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004320{
4321 char *tty_name;
4322
4323 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4324 if (*pty_master_fd >= 0)
4325 {
4326 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4327 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4328 * adding O_NOCTTY always works when defined. */
4329#ifdef O_NOCTTY
4330 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4331#else
4332 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4333#endif
4334 if (*pty_slave_fd < 0)
4335 {
4336 close(*pty_master_fd);
4337 *pty_master_fd = -1;
4338 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004339 else if (namep != NULL)
4340 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004341 }
4342}
4343#endif
4344
Bram Moolenaarfae42832017-08-01 22:24:26 +02004345/*
4346 * Send SIGINT to a child process if "c" is an interrupt character.
4347 */
4348 void
4349may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4350{
4351# ifdef SIGINT
4352 if (c == Ctrl_C || c == intr_char)
4353 {
4354# ifdef HAVE_SETSID
4355 kill(-pid, SIGINT);
4356# else
4357 kill(0, SIGINT);
4358# endif
4359 if (wpid > 0)
4360 kill(wpid, SIGINT);
4361 }
4362# endif
4363}
4364
Bram Moolenaar13568252018-03-16 20:46:58 +01004365#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4366
4367 static int
4368build_argv(
4369 char_u *cmd,
4370 char ***argvp,
4371 char_u **sh_tofree,
4372 char_u **shcf_tofree)
4373{
4374 char **argv = NULL;
4375 int argc;
4376
4377 *sh_tofree = vim_strsave(p_sh);
4378 if (*sh_tofree == NULL) /* out of memory */
4379 return FAIL;
4380
4381 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4382 return FAIL;
4383 *argvp = argv;
4384
4385 if (cmd != NULL)
4386 {
4387 char_u *s;
4388 char_u *p;
4389
4390 if (extra_shell_arg != NULL)
4391 argv[argc++] = (char *)extra_shell_arg;
4392
4393 /* Break 'shellcmdflag' into white separated parts. This doesn't
4394 * handle quoted strings, they are very unlikely to appear. */
4395 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4396 if (*shcf_tofree == NULL) /* out of memory */
4397 return FAIL;
4398 s = *shcf_tofree;
4399 p = p_shcf;
4400 while (*p != NUL)
4401 {
4402 argv[argc++] = (char *)s;
4403 while (*p && *p != ' ' && *p != TAB)
4404 *s++ = *p++;
4405 *s++ = NUL;
4406 p = skipwhite(p);
4407 }
4408
4409 argv[argc++] = (char *)cmd;
4410 }
4411 argv[argc] = NULL;
4412 return OK;
4413}
4414#endif
4415
4416#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4417/*
4418 * Use a terminal window to run a shell command in.
4419 */
4420 static int
4421mch_call_shell_terminal(
4422 char_u *cmd,
4423 int options UNUSED) /* SHELL_*, see vim.h */
4424{
4425 jobopt_T opt;
4426 char **argv = NULL;
4427 char_u *tofree1 = NULL;
4428 char_u *tofree2 = NULL;
4429 int retval = -1;
4430 buf_T *buf;
4431 aco_save_T aco;
4432 oparg_T oa; /* operator arguments */
4433
4434 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4435 goto theend;
4436
4437 init_job_options(&opt);
4438 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4439 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
4440
4441 /* Find a window to make "buf" curbuf. */
4442 aucmd_prepbuf(&aco, buf);
4443
4444 clear_oparg(&oa);
4445 while (term_use_loop())
4446 {
4447 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4448 {
4449 /* If terminal_loop() returns OK we got a key that is handled
4450 * in Normal model. We don't do redrawing anyway. */
4451 if (terminal_loop(TRUE) == OK)
4452 normal_cmd(&oa, TRUE);
4453 }
4454 else
4455 normal_cmd(&oa, TRUE);
4456 }
4457 retval = 0;
4458 ch_log(NULL, "system command finished");
4459
4460 /* restore curwin/curbuf and a few other things */
4461 aucmd_restbuf(&aco);
4462
4463 wait_return(TRUE);
4464 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4465
4466theend:
4467 vim_free(argv);
4468 vim_free(tofree1);
4469 vim_free(tofree2);
4470 return retval;
4471}
4472#endif
4473
4474#ifdef USE_SYSTEM
4475/*
4476 * Use system() to start the shell: simple but slow.
4477 */
4478 static int
4479mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004480 char_u *cmd,
4481 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483#ifdef VMS
4484 char *ifn = NULL;
4485 char *ofn = NULL;
4486#endif
4487 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004488 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004489 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 out_flush();
4492
4493 if (options & SHELL_COOKED)
4494 settmode(TMODE_COOK); /* set to normal mode */
4495
Bram Moolenaar62b42182010-09-21 22:09:37 +02004496# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004497 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004498 loose_clipboard();
4499# endif
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (cmd == NULL)
4502 x = system((char *)p_sh);
4503 else
4504 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004505# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (ofn = strchr((char *)cmd, '>'))
4507 *ofn++ = '\0';
4508 if (ifn = strchr((char *)cmd, '<'))
4509 {
4510 char *p;
4511
4512 *ifn++ = '\0';
4513 p = strchr(ifn,' '); /* chop off any trailing spaces */
4514 if (p)
4515 *p = '\0';
4516 }
4517 if (ofn)
4518 x = vms_sys((char *)cmd, ofn, ifn);
4519 else
4520 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004521# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 newcmd = lalloc(STRLEN(p_sh)
4523 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4524 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4525 if (newcmd == NULL)
4526 x = 0;
4527 else
4528 {
4529 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4530 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4531 (char *)p_shcf,
4532 (char *)cmd);
4533 x = system((char *)newcmd);
4534 vim_free(newcmd);
4535 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538# ifdef VMS
4539 x = vms_sys_status(x);
4540# endif
4541 if (emsg_silent)
4542 ;
4543 else if (x == 127)
4544 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else if (x && !(options & SHELL_SILENT))
4546 {
4547 MSG_PUTS(_("\nshell returned "));
4548 msg_outnum((long)x);
4549 msg_putchar('\n');
4550 }
4551
4552 if (tmode == TMODE_RAW)
4553 settmode(TMODE_RAW); /* set to raw mode */
4554# ifdef FEAT_TITLE
4555 resettitle();
4556# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004557# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4558 restore_clipboard();
4559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar13568252018-03-16 20:46:58 +01004563#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4566 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004567# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar13568252018-03-16 20:46:58 +01004569/*
4570 * Don't use system(), use fork()/exec().
4571 */
4572 static int
4573mch_call_shell_fork(
4574 char_u *cmd,
4575 int options) /* SHELL_*, see vim.h */
4576{
4577 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 pid_t wait_pid = 0;
4581# ifdef HAVE_UNION_WAIT
4582 union wait status;
4583# else
4584 int status = -1;
4585# endif
4586 int retval = -1;
4587 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004588 char_u *tofree1 = NULL;
4589 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004595 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 int fd_fromshell[2];
4597 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004598 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 out_flush();
4601 if (options & SHELL_COOKED)
4602 settmode(TMODE_COOK); /* set to normal mode */
4603
Bram Moolenaar13568252018-03-16 20:46:58 +01004604 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004605 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608 * For the GUI, when writing the output into the buffer and when reading
4609 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4610 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004612 if ((options & (SHELL_READ|SHELL_WRITE))
4613# ifdef FEAT_GUI
4614 || (gui.in_use && show_shell_mess)
4615# endif
4616 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004618# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 /*
4620 * Try to open a master pty.
4621 * If this works, open the slave pty.
4622 * If the slave can't be opened, close the master pty.
4623 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004624 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004625 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 /*
4627 * If not opening a pty or it didn't work, try using pipes.
4628 */
4629 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 pipe_error = (pipe(fd_toshell) < 0);
4633 if (!pipe_error) /* pipe create OK */
4634 {
4635 pipe_error = (pipe(fd_fromshell) < 0);
4636 if (pipe_error) /* pipe create failed */
4637 {
4638 close(fd_toshell[0]);
4639 close(fd_toshell[1]);
4640 }
4641 }
4642 if (pipe_error)
4643 {
4644 MSG_PUTS(_("\nCannot create pipes\n"));
4645 out_flush();
4646 }
4647 }
4648 }
4649
4650 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004652 SIGSET_DECL(curset)
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654# ifdef __BEOS__
4655 beos_cleanup_read_thread();
4656# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004657
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004658 BLOCK_SIGNALS(&curset);
4659 pid = fork(); /* maybe we should use vfork() */
4660 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004662 UNBLOCK_SIGNALS(&curset);
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004667 || (gui.in_use && show_shell_mess)
4668# endif
4669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004671# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (pty_master_fd >= 0) /* close the pseudo tty */
4673 {
4674 close(pty_master_fd);
4675 close(pty_slave_fd);
4676 }
4677 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
4680 close(fd_toshell[0]);
4681 close(fd_toshell[1]);
4682 close(fd_fromshell[0]);
4683 close(fd_fromshell[1]);
4684 }
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 else if (pid == 0) /* child */
4688 {
4689 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004690 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
Bram Moolenaar819524702018-02-27 19:10:00 +01004692# ifdef FEAT_JOB_CHANNEL
4693 if (ch_log_active())
4694 /* close the log file in the child */
4695 ch_logfile((char_u *)"", (char_u *)"");
4696# endif
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (!show_shell_mess || (options & SHELL_EXPAND))
4699 {
4700 int fd;
4701
4702 /*
4703 * Don't want to show any message from the shell. Can't just
4704 * close stdout and stderr though, because some systems will
4705 * break if you try to write to them after that, so we must
4706 * use dup() to replace them with something else -- webb
4707 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4708 * waiting for input.
4709 */
4710 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4711 fclose(stdin);
4712 fclose(stdout);
4713 fclose(stderr);
4714
4715 /*
4716 * If any of these open()'s and dup()'s fail, we just continue
4717 * anyway. It's not fatal, and on most systems it will make
4718 * no difference at all. On a few it will cause the execvp()
4719 * to exit with a non-zero status even when the completion
4720 * could be done, which is nothing too serious. If the open()
4721 * or dup() failed we'd just do the same thing ourselves
4722 * anyway -- webb
4723 */
4724 if (fd >= 0)
4725 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004726 ignored = dup(fd); /* To replace stdin (fd 0) */
4727 ignored = dup(fd); /* To replace stdout (fd 1) */
4728 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 /* Don't need this now that we've duplicated it */
4731 close(fd);
4732 }
4733 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004734 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004736 || gui.in_use
4737# endif
4738 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004742 /* Create our own process group, so that the child and all its
4743 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004744 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004745 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004746 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004747 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004748# if defined(SIGHUP)
4749 /* When doing "!xterm&" and 'shell' is bash: the shell
4750 * will exit and send SIGHUP to all processes in its
4751 * group, killing the just started process. Ignore SIGHUP
4752 * to avoid that. (suggested by Simon Schubert)
4753 */
4754 signal(SIGHUP, SIG_IGN);
4755# endif
4756 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004757# endif
4758# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004759 if (pty_slave_fd >= 0)
4760 {
4761 /* push stream discipline modules */
4762 if (options & SHELL_COOKED)
4763 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004765 /* Try to become controlling tty (probably doesn't work,
4766 * unless run by root) */
4767 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004769 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004771 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaara5792f52005-11-23 21:25:05 +00004773 /*
4774 * stderr is only redirected when using the GUI, so that a
4775 * program like gpg can still access the terminal to get a
4776 * passphrase using stderr.
4777 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004778# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (pty_master_fd >= 0)
4780 {
4781 close(pty_master_fd); /* close master side of pty */
4782
4783 /* set up stdin/stdout/stderr for the child */
4784 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004785 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004787 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004788 if (gui.in_use)
4789 {
4790 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004791 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 close(pty_slave_fd); /* has been dupped, close it now */
4795 }
4796 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 /* set up stdin for the child */
4800 close(fd_toshell[1]);
4801 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004802 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 close(fd_toshell[0]);
4804
4805 /* set up stdout for the child */
4806 close(fd_fromshell[0]);
4807 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004808 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 close(fd_fromshell[1]);
4810
Bram Moolenaara5792f52005-11-23 21:25:05 +00004811# ifdef FEAT_GUI
4812 if (gui.in_use)
4813 {
4814 /* set up stderr for the child */
4815 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004816 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004817 }
4818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * There is no type cast for the argv, because the type may be
4824 * different on different machines. This may cause a warning
4825 * message with strict compilers, don't worry about it.
4826 * Call _exit() instead of exit() to avoid closing the connection
4827 * to the X server (esp. with GTK, which uses atexit()).
4828 */
4829 execvp(argv[0], argv);
4830 _exit(EXEC_FAILED); /* exec failed, return failure code */
4831 }
4832 else /* parent */
4833 {
4834 /*
4835 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 */
4838 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004839 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004840 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004841# ifdef FEAT_JOB_CHANNEL
4842 ++dont_check_job_ended;
4843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 /*
4845 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004846 * This is also used to pipe stdin/stdout to/from the external
4847 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849 if ((options & (SHELL_READ|SHELL_WRITE))
4850# ifdef FEAT_GUI
4851 || (gui.in_use && show_shell_mess)
4852# endif
4853 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004857# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4861 int ta_len = 0; /* valid bytes in ta_buf[] */
4862 int len;
4863 int p_more_save;
4864 int old_State;
4865 int c;
4866 int toshell_fd;
4867 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004868 garray_T ga;
4869 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004870# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4871 struct timeval start_tv;
4872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
Bram Moolenaardf177f62005-02-22 08:39:57 +00004874# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if (pty_master_fd >= 0)
4876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 fromshell_fd = pty_master_fd;
4878 toshell_fd = dup(pty_master_fd);
4879 }
4880 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 close(fd_toshell[0]);
4884 close(fd_fromshell[1]);
4885 toshell_fd = fd_toshell[1];
4886 fromshell_fd = fd_fromshell[0];
4887 }
4888
4889 /*
4890 * Write to the child if there are typed characters.
4891 * Read from the child if there are characters available.
4892 * Repeat the reading a few times if more characters are
4893 * available. Need to check for typed keys now and then, but
4894 * not too often (delays when no chars are available).
4895 * This loop is quit if no characters can be read from the pty
4896 * (WaitForChar detected special condition), or there are no
4897 * characters available and the child has exited.
4898 * Only check if the child has exited when there is no more
4899 * output. The child may exit before all the output has
4900 * been printed.
4901 *
4902 * Currently this busy loops!
4903 * This can probably dead-lock when the write blocks!
4904 */
4905 p_more_save = p_more;
4906 p_more = FALSE;
4907 old_State = State;
4908 State = EXTERNCMD; /* don't redraw at window resize */
4909
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004910 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 {
4912 /* Fork a process that will write the lines to the
4913 * external program. */
4914 if ((wpid = fork()) == -1)
4915 {
4916 MSG_PUTS(_("\nCannot fork\n"));
4917 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004918 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 {
4920 linenr_T lnum = curbuf->b_op_start.lnum;
4921 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004922 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 size_t l;
4924
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004925 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 for (;;)
4927 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004928 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004929 if (l == 0)
4930 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004931 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932 /* NL -> NUL translation */
4933 len = write(toshell_fd, "", (size_t)1);
4934 else
4935 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004936 char_u *s = vim_strchr(lp + written, NL);
4937
Bram Moolenaar89d40322006-08-29 15:30:07 +00004938 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004939 s == NULL ? l
4940 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004941 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004942 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004943 {
4944 /* Finished a line, add a NL, unless this line
4945 * should not have one. */
4946 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004947 || (!curbuf->b_p_bin
4948 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004949 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 && (lnum !=
4951 curbuf->b_ml.ml_line_count
4952 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004953 ignored = write(toshell_fd, "\n",
4954 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004955 ++lnum;
4956 if (lnum > curbuf->b_op_end.lnum)
4957 {
4958 /* finished all the lines, close pipe */
4959 close(toshell_fd);
4960 toshell_fd = -1;
4961 break;
4962 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004963 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004964 written = 0;
4965 }
4966 else if (len > 0)
4967 written += len;
4968 }
4969 _exit(0);
4970 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004971 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004972 {
4973 close(toshell_fd);
4974 toshell_fd = -1;
4975 }
4976 }
4977
4978 if (options & SHELL_READ)
4979 ga_init2(&ga, 1, BUFLEN);
4980
4981 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004982# ifdef ELAPSED_FUNC
4983 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 for (;;)
4986 {
4987 /*
4988 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004989 * if there are any.
4990 * Don't do this if we are expanding wild cards (would eat
4991 * typeahead).
4992 * Don't do this when filtering and terminal is in cooked
4993 * mode, the shell command will handle the I/O. Avoids
4994 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004995 * Don't get characters when the child has already
4996 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004997 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004998 * while (noread_cnt > 4), avoids that ":r !ls" eats
4999 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 */
5001 len = 0;
5002 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005003 && ((options &
5004 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
5005 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005006# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005007 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005008# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005009 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005010 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005011 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005013 if (ta_len == 0)
5014 {
5015 /* Get extra characters when we don't have any.
5016 * Reset the counter and timer. */
5017 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005018# ifdef ELAPSED_FUNC
5019 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005020# endif
5021 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5022 }
5023 if (ta_len > 0 || len > 0)
5024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 /*
5026 * For pipes:
5027 * Check for CTRL-C: send interrupt signal to child.
5028 * Check for CTRL-D: EOF, close pipe to child.
5029 */
5030 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 /*
5033 * Send SIGINT to the child's group or all
5034 * processes in our group.
5035 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005036 may_send_sigint(ta_buf[ta_len], pid, wpid);
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (pty_master_fd < 0 && toshell_fd >= 0
5039 && ta_buf[ta_len] == Ctrl_D)
5040 {
5041 close(toshell_fd);
5042 toshell_fd = -1;
5043 }
5044 }
5045
5046 /* replace K_BS by <BS> and K_DEL by <DEL> */
5047 for (i = ta_len; i < ta_len + len; ++i)
5048 {
5049 if (ta_buf[i] == CSI && len - i > 2)
5050 {
5051 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5052 if (c == K_DEL || c == K_KDEL || c == K_BS)
5053 {
5054 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5055 (size_t)(len - i - 2));
5056 if (c == K_DEL || c == K_KDEL)
5057 ta_buf[i] = DEL;
5058 else
5059 ta_buf[i] = Ctrl_H;
5060 len -= 2;
5061 }
5062 }
5063 else if (ta_buf[i] == '\r')
5064 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005065# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005067 i += (*mb_ptr2len_len)(ta_buf + i,
5068 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071
5072 /*
5073 * For pipes: echo the typed characters.
5074 * For a pty this does not seem to work.
5075 */
5076 if (pty_master_fd < 0)
5077 {
5078 for (i = ta_len; i < ta_len + len; ++i)
5079 {
5080 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5081 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005082# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else if (has_mbyte)
5084 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005085 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 msg_outtrans_len(ta_buf + i, l);
5088 i += l - 1;
5089 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 else
5092 msg_outtrans_len(ta_buf + i, 1);
5093 }
5094 windgoto(msg_row, msg_col);
5095 out_flush();
5096 }
5097
5098 ta_len += len;
5099
5100 /*
5101 * Write the characters to the child, unless EOF has
5102 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005103 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005104 * When writing buffer lines, drop the typed
5105 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005107 if (options & SHELL_WRITE)
5108 ta_len = 0;
5109 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5112 if (len > 0)
5113 {
5114 ta_len -= len;
5115 mch_memmove(ta_buf, ta_buf + len, ta_len);
5116 }
5117 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
Bram Moolenaardf177f62005-02-22 08:39:57 +00005121 if (got_int)
5122 {
5123 /* CTRL-C sends a signal to the child, we ignore it
5124 * ourselves */
5125# ifdef HAVE_SETSID
5126 kill(-pid, SIGINT);
5127# else
5128 kill(0, SIGINT);
5129# endif
5130 if (wpid > 0)
5131 kill(wpid, SIGINT);
5132 got_int = FALSE;
5133 }
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /*
5136 * Check if the child has any characters to be printed.
5137 * Read them and write them to our window. Repeat this as
5138 * long as there is something to do, avoid the 10ms wait
5139 * for mch_inchar(), or sending typeahead characters to
5140 * the external process.
5141 * TODO: This should handle escape sequences, compatible
5142 * to some terminal (vt52?).
5143 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005144 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005145 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005147 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005148# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005150# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 );
5154 if (len <= 0) /* end of file or error */
5155 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005156
5157 noread_cnt = 0;
5158 if (options & SHELL_READ)
5159 {
5160 /* Do NUL -> NL translation, append NL separated
5161 * lines to the current buffer. */
5162 for (i = 0; i < len; ++i)
5163 {
5164 if (buffer[i] == NL)
5165 append_ga_line(&ga);
5166 else if (buffer[i] == NUL)
5167 ga_append(&ga, NL);
5168 else
5169 ga_append(&ga, buffer[i]);
5170 }
5171 }
5172# ifdef FEAT_MBYTE
5173 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005176 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
Bram Moolenaardf177f62005-02-22 08:39:57 +00005178 len += buffer_off;
5179 buffer[len] = NUL;
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 /* Check if the last character in buffer[] is
5182 * incomplete, keep these bytes for the next
5183 * round. */
5184 for (p = buffer; p < buffer + len; p += l)
5185 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005186 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (l == 0)
5188 l = 1; /* NUL byte? */
5189 else if (MB_BYTE2LEN(*p) != l)
5190 break;
5191 }
5192 if (p == buffer) /* no complete character */
5193 {
5194 /* avoid getting stuck at an illegal byte */
5195 if (len >= 12)
5196 ++p;
5197 else
5198 {
5199 buffer_off = len;
5200 continue;
5201 }
5202 }
5203 c = *p;
5204 *p = NUL;
5205 msg_puts(buffer);
5206 if (p < buffer + len)
5207 {
5208 *p = c;
5209 buffer_off = (buffer + len) - p;
5210 mch_memmove(buffer, p, buffer_off);
5211 continue;
5212 }
5213 buffer_off = 0;
5214 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005215# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 buffer[len] = NUL;
5219 msg_puts(buffer);
5220 }
5221
5222 windgoto(msg_row, msg_col);
5223 cursor_on();
5224 out_flush();
5225 if (got_int)
5226 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005227
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005228# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005229 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005230 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005231 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005232
5233 /* Avoid that we keep looping here without
5234 * checking for a CTRL-C for a long time. Don't
5235 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005236 if (msec > 2000)
5237 {
5238 noread_cnt = 5;
5239 break;
5240 }
5241 }
5242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005245 /* If we already detected the child has finished, continue
5246 * reading output for a short while. Some text may be
5247 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005248 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005249 {
5250 if (noread_cnt < 5)
5251 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005252 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005253 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 /*
5256 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005257 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005259# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005260 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005261# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5265 || (wait_pid == pid && WIFEXITED(status)))
5266 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005267 /* Don't break the loop yet, try reading more
5268 * characters from "fromshell_fd" first. When using
5269 * pipes there might still be something to read and
5270 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005273 else
5274 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005275
Bram Moolenaar95a51352013-03-21 22:53:50 +01005276# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005277 /* Handle any X events, e.g. serving the clipboard. */
5278 clip_update();
5279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281finished:
5282 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005283 if (options & SHELL_READ)
5284 {
5285 if (ga.ga_len > 0)
5286 {
5287 append_ga_line(&ga);
5288 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005289 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005290 }
5291 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005292 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005293 ga_clear(&ga);
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 /*
5297 * Give all typeahead that wasn't used back to ui_inchar().
5298 */
5299 if (ta_len)
5300 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 State = old_State;
5302 if (toshell_fd >= 0)
5303 close(toshell_fd);
5304 close(fromshell_fd);
5305 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005306# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005307 else
5308 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005309 long delay_msec = 1;
5310
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005311 /*
5312 * Similar to the loop above, but only handle X events, no
5313 * I/O.
5314 */
5315 for (;;)
5316 {
5317 if (got_int)
5318 {
5319 /* CTRL-C sends a signal to the child, we ignore it
5320 * ourselves */
5321# ifdef HAVE_SETSID
5322 kill(-pid, SIGINT);
5323# else
5324 kill(0, SIGINT);
5325# endif
5326 got_int = FALSE;
5327 }
5328# ifdef __NeXT__
5329 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5330# else
5331 wait_pid = waitpid(pid, &status, WNOHANG);
5332# endif
5333 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5334 || (wait_pid == pid && WIFEXITED(status)))
5335 {
5336 wait_pid = pid;
5337 break;
5338 }
5339
5340 /* Handle any X events, e.g. serving the clipboard. */
5341 clip_update();
5342
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005343 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5344 * less time. */
5345 mch_delay(delay_msec, TRUE);
5346 if (++delay_msec > 10)
5347 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005348 }
5349 }
5350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 /*
5353 * Wait until our child has exited.
5354 * Ignore wait() returning pids of other children and returning
5355 * because of some signal like SIGWINCH.
5356 * Don't wait if wait_pid was already set above, indicating the
5357 * child already exited.
5358 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005359 if (wait_pid != pid)
5360 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
Bram Moolenaar624891f2010-10-13 16:22:09 +02005362# ifdef FEAT_GUI
5363 /* Close slave side of pty. Only do this after the child has
5364 * exited, otherwise the child may hang when it tries to write on
5365 * the pty. */
5366 if (pty_master_fd >= 0)
5367 close(pty_slave_fd);
5368# endif
5369
Bram Moolenaardf177f62005-02-22 08:39:57 +00005370 /* Make sure the child that writes to the external program is
5371 * dead. */
5372 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005373 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005374 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005375 wait4pid(wpid, NULL);
5376 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005377
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005378# ifdef FEAT_JOB_CHANNEL
5379 --dont_check_job_ended;
5380# endif
5381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 /*
5383 * Set to raw mode right now, otherwise a CTRL-C after
5384 * catch_signals() will kill Vim.
5385 */
5386 if (tmode == TMODE_RAW)
5387 settmode(TMODE_RAW);
5388 did_settmode = TRUE;
5389 set_signals();
5390
5391 if (WIFEXITED(status))
5392 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005393 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005395 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
5397 if (retval == EXEC_FAILED)
5398 {
5399 MSG_PUTS(_("\nCannot execute shell "));
5400 msg_outtrans(p_sh);
5401 msg_putchar('\n');
5402 }
5403 else if (!(options & SHELL_SILENT))
5404 {
5405 MSG_PUTS(_("\nshell returned "));
5406 msg_outnum((long)retval);
5407 msg_putchar('\n');
5408 }
5409 }
5410 }
5411 else
5412 MSG_PUTS(_("\nCommand terminated\n"));
5413 }
5414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416error:
5417 if (!did_settmode)
5418 if (tmode == TMODE_RAW)
5419 settmode(TMODE_RAW); /* set to raw mode */
5420# ifdef FEAT_TITLE
5421 resettitle();
5422# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005423 vim_free(argv);
5424 vim_free(tofree1);
5425 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005428}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005430
5431 int
5432mch_call_shell(
5433 char_u *cmd,
5434 int options) /* SHELL_*, see vim.h */
5435{
5436#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5437 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5438 return mch_call_shell_terminal(cmd, options);
5439#endif
5440#ifdef USE_SYSTEM
5441 return mch_call_shell_system(cmd, options);
5442#else
5443 return mch_call_shell_fork(cmd, options);
5444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445}
5446
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005447#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005448 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005449mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005450{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005451 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005452 int fd_in[2] = {-1, -1}; /* for stdin */
5453 int fd_out[2] = {-1, -1}; /* for stdout */
5454 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005455 int pty_master_fd = -1;
5456 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005457 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005458 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5459 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5460 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005461 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005462 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5463 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005464 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005465 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005466 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005467
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005468 if (use_out_for_err && use_null_for_out)
5469 use_null_for_err = TRUE;
5470
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005471 /* default is to fail */
5472 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005473
Bram Moolenaarb2412082017-08-20 18:09:14 +02005474 if (options->jo_pty
5475 && (!(use_file_for_in || use_null_for_in)
5476 || !(use_file_for_in || use_null_for_out)
5477 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005478 {
5479 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5480 if (job->jv_tty_out != NULL)
5481 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5482 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005483
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005484 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005485 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005486 if (use_file_for_in)
5487 {
5488 char_u *fname = options->jo_io_name[PART_IN];
5489
5490 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5491 if (fd_in[0] < 0)
5492 {
5493 EMSG2(_(e_notopen), fname);
5494 goto failed;
5495 }
5496 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005497 else
5498 /* When writing buffer lines to the input don't use the pty, so that
5499 * the pipe can be closed when all lines were written. */
5500 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5501 && pipe(fd_in) < 0)
5502 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005503
5504 if (use_file_for_out)
5505 {
5506 char_u *fname = options->jo_io_name[PART_OUT];
5507
5508 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5509 if (fd_out[1] < 0)
5510 {
5511 EMSG2(_(e_notopen), fname);
5512 goto failed;
5513 }
5514 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005515 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005516 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005517
5518 if (use_file_for_err)
5519 {
5520 char_u *fname = options->jo_io_name[PART_ERR];
5521
5522 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5523 if (fd_err[1] < 0)
5524 {
5525 EMSG2(_(e_notopen), fname);
5526 goto failed;
5527 }
5528 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005529 else if (!use_out_for_err && !use_null_for_err
5530 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005531 goto failed;
5532
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005533 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5534 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005535 if (options->jo_set & JO_CHANNEL)
5536 {
5537 channel = options->jo_channel;
5538 if (channel != NULL)
5539 ++channel->ch_refcount;
5540 }
5541 else
5542 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005543 if (channel == NULL)
5544 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005545 if (job->jv_tty_out != NULL)
5546 ch_log(channel, "using pty %s on fd %d",
5547 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005548 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005549
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005550 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005551 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005552 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005553 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005554 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005555 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005556 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005557 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005558 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005559 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005560 int null_fd = -1;
5561 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005562
Bram Moolenaar835dc632016-02-07 14:27:38 +01005563 /* child */
5564 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005565 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005566
Bram Moolenaar819524702018-02-27 19:10:00 +01005567# ifdef FEAT_JOB_CHANNEL
5568 if (ch_log_active())
5569 /* close the log file in the child */
5570 ch_logfile((char_u *)"", (char_u *)"");
5571# endif
5572
Bram Moolenaar835dc632016-02-07 14:27:38 +01005573# ifdef HAVE_SETSID
5574 /* Create our own process group, so that the child and all its
5575 * children can be kill()ed. Don't do this when using pipes,
5576 * because stdin is not a tty, we would lose /dev/tty. */
5577 (void)setsid();
5578# endif
5579
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005580# ifdef FEAT_TERMINAL
5581 if (options->jo_term_rows > 0)
5582 set_child_environment(
5583 (long)options->jo_term_rows,
5584 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005585 STRNCMP(T_NAME, "xterm", 5) == 0
5586 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005587 else
5588# endif
5589 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005590
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005591 if (options->jo_env != NULL)
5592 {
5593 dict_T *dict = options->jo_env;
5594 hashitem_T *hi;
5595 int todo = (int)dict->dv_hashtab.ht_used;
5596
5597 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5598 if (!HASHITEM_EMPTY(hi))
5599 {
5600 typval_T *item = &dict_lookup(hi)->di_tv;
5601
5602 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5603 --todo;
5604 }
5605 }
5606
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005607 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005608 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005609 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005610 if (null_fd < 0)
5611 {
5612 perror("opening /dev/null failed");
5613 _exit(OPEN_NULL_FAILED);
5614 }
5615 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005616
Bram Moolenaar223896d2017-08-02 22:33:28 +02005617 if (pty_slave_fd >= 0)
5618 {
5619 /* push stream discipline modules */
5620 SetupSlavePTY(pty_slave_fd);
5621# ifdef TIOCSCTTY
5622 /* Try to become controlling tty (probably doesn't work,
5623 * unless run by root) */
5624 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5625# endif
5626 }
5627
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005628 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005629 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005630 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005631 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005632 else if (fd_in[0] < 0)
5633 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005634 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005635 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005636
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005637 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005638 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005639 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005640 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005641 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005642 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005643 }
5644 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005645 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005646 else if (fd_err[1] < 0)
5647 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005648 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005649 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005650
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005651 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005652 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005653 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005654 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005655 else if (fd_out[1] < 0)
5656 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005657 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005658 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005659
5660 if (fd_in[0] >= 0)
5661 close(fd_in[0]);
5662 if (fd_in[1] >= 0)
5663 close(fd_in[1]);
5664 if (fd_out[0] >= 0)
5665 close(fd_out[0]);
5666 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005667 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005668 if (fd_err[0] >= 0)
5669 close(fd_err[0]);
5670 if (fd_err[1] >= 0)
5671 close(fd_err[1]);
5672 if (pty_master_fd >= 0)
5673 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005674 close(pty_master_fd); /* not used in the child */
5675 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005676 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005677
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005678 if (null_fd >= 0)
5679 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005680
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005681 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5682 _exit(EXEC_FAILED);
5683
Bram Moolenaar835dc632016-02-07 14:27:38 +01005684 /* See above for type of argv. */
5685 execvp(argv[0], argv);
5686
Bram Moolenaar4694a172016-04-21 14:05:23 +02005687 if (stderr_works)
5688 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005689# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005690 /* calling free_all_mem() here causes problems. Ignore valgrind
5691 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005692# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005693 _exit(EXEC_FAILED); /* exec failed, return failure code */
5694 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005695
5696 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005697 UNBLOCK_SIGNALS(&curset);
5698
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005699 job->jv_pid = pid;
5700 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005701 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005703 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005704 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005705 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005706 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005707 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005708 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005709 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005710 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005711 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005712 if (channel != NULL)
5713 {
5714 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005715 use_file_for_in || use_null_for_in
5716 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5717 use_file_for_out || use_null_for_out
5718 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5719 use_out_for_err || use_file_for_err || use_null_for_err
5720 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005721 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005722 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005723 else
5724 {
5725 if (fd_in[1] >= 0)
5726 close(fd_in[1]);
5727 if (fd_out[0] >= 0)
5728 close(fd_out[0]);
5729 if (fd_err[0] >= 0)
5730 close(fd_err[0]);
5731 if (pty_master_fd >= 0)
5732 close(pty_master_fd);
5733 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005735 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005736 return;
5737
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005738failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005739 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005740 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005742 if (fd_in[1] >= 0)
5743 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005744 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005745 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005746 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005747 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005748 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005749 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005750 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005752 if (pty_master_fd >= 0)
5753 close(pty_master_fd);
5754 if (pty_slave_fd >= 0)
5755 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005756}
5757
5758 char *
5759mch_job_status(job_T *job)
5760{
5761# ifdef HAVE_UNION_WAIT
5762 union wait status;
5763# else
5764 int status = -1;
5765# endif
5766 pid_t wait_pid = 0;
5767
5768# ifdef __NeXT__
5769 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5770# else
5771 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5772# endif
5773 if (wait_pid == -1)
5774 {
5775 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005776 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005777 }
5778 if (wait_pid == 0)
5779 return "run";
5780 if (WIFEXITED(status))
5781 {
5782 /* LINTED avoid "bitwise operation on signed value" */
5783 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005784 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005785 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005786 if (WIFSIGNALED(status))
5787 {
5788 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005789 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005790 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005791 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005792
5793return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005794 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005795 {
5796 ch_log(job->jv_channel, "Job ended");
5797 job->jv_status = JOB_ENDED;
5798 }
5799 return "dead";
5800}
5801
5802 job_T *
5803mch_detect_ended_job(job_T *job_list)
5804{
5805# ifdef HAVE_UNION_WAIT
5806 union wait status;
5807# else
5808 int status = -1;
5809# endif
5810 pid_t wait_pid = 0;
5811 job_T *job;
5812
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005813# ifndef USE_SYSTEM
5814 /* Do not do this when waiting for a shell command to finish, we would get
5815 * the exit value here (and discard it), the exit value obtained there
5816 * would then be wrong. */
5817 if (dont_check_job_ended > 0)
5818 return NULL;
5819# endif
5820
Bram Moolenaar97792de2016-10-15 18:36:49 +02005821# ifdef __NeXT__
5822 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5823# else
5824 wait_pid = waitpid(-1, &status, WNOHANG);
5825# endif
5826 if (wait_pid <= 0)
5827 /* no process ended */
5828 return NULL;
5829 for (job = job_list; job != NULL; job = job->jv_next)
5830 {
5831 if (job->jv_pid == wait_pid)
5832 {
5833 if (WIFEXITED(status))
5834 /* LINTED avoid "bitwise operation on signed value" */
5835 job->jv_exitval = WEXITSTATUS(status);
5836 else if (WIFSIGNALED(status))
5837 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005838 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005839 {
5840 ch_log(job->jv_channel, "Job ended");
5841 job->jv_status = JOB_ENDED;
5842 }
5843 return job;
5844 }
5845 }
5846 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847}
5848
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005849/*
5850 * Send a (deadly) signal to "job".
5851 * Return FAIL if "how" is not a valid name.
5852 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005854mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005856 int sig = -1;
5857 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005858
Bram Moolenaar923d9262016-02-25 20:56:01 +01005859 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005860 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005861 else if (STRCMP(how, "hup") == 0)
5862 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005863 else if (STRCMP(how, "quit") == 0)
5864 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005865 else if (STRCMP(how, "int") == 0)
5866 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867 else if (STRCMP(how, "kill") == 0)
5868 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005869#ifdef SIGWINCH
5870 else if (STRCMP(how, "winch") == 0)
5871 sig = SIGWINCH;
5872#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005873 else if (isdigit(*how))
5874 sig = atoi((char *)how);
5875 else
5876 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005877
Bram Moolenaar72801402016-02-09 11:37:50 +01005878 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005879 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005880#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881 if (job_pid == getpgid(job_pid))
5882 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005883#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005884
Bram Moolenaar61a66052017-07-22 18:39:00 +02005885 /* Never kill ourselves! */
5886 if (job_pid != 0)
5887 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888
Bram Moolenaar835dc632016-02-07 14:27:38 +01005889 return OK;
5890}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891
5892/*
5893 * Clear the data related to "job".
5894 */
5895 void
5896mch_clear_job(job_T *job)
5897{
5898 /* call waitpid because child process may become zombie */
5899# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005900 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005901# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005902 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005903# endif
5904}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005905#endif
5906
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005907#if defined(FEAT_TERMINAL) || defined(PROTO)
5908 int
5909mch_create_pty_channel(job_T *job, jobopt_T *options)
5910{
5911 int pty_master_fd = -1;
5912 int pty_slave_fd = -1;
5913 channel_T *channel;
5914
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005915 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5916 if (job->jv_tty_out != NULL)
5917 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005918 close(pty_slave_fd);
5919
5920 channel = add_channel();
5921 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005922 {
5923 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005924 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005925 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005926 if (job->jv_tty_out != NULL)
5927 ch_log(channel, "using pty %s on fd %d",
5928 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005929 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5930 channel->ch_keep_open = TRUE;
5931
5932 channel_set_pipes(channel, pty_master_fd, pty_master_fd, pty_master_fd);
5933 channel_set_job(channel, job, options);
5934 return OK;
5935}
5936#endif
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938/*
5939 * Check for CTRL-C typed by reading all available characters.
5940 * In cooked mode we should get SIGINT, no need to check.
5941 */
5942 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005943mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005945 if ((curr_tmode == TMODE_RAW || force)
5946 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 fill_input_buf(FALSE);
5948}
5949
5950/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005951 * Wait "msec" msec until a character is available from the mouse, keyboard,
5952 * from inbuf[].
5953 * "msec" == -1 will block forever.
5954 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005955 * When "ignore_input" is TRUE even check for pending input when input is
5956 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005957 * "interrupted" (if not NULL) is set to TRUE when no character is available
5958 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005959 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005960 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 */
5962 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005963WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005965#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005966 return ui_wait_for_chars_or_timer(
5967 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005968#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005969 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005970#endif
5971}
5972
5973/*
5974 * Wait "msec" msec until a character is available from the mouse or keyboard
5975 * or from inbuf[].
5976 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005977 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005978 * "interrupted" (if not NULL) is set to TRUE when no character is available
5979 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005980 * When a GUI is being used, this will never get called -- webb
5981 */
5982 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005983WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985#ifdef FEAT_MOUSE_GPM
5986 int gpm_process_wanted;
5987#endif
5988#ifdef FEAT_XCLIPBOARD
5989 int rest;
5990#endif
5991 int avail;
5992
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005993 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 return 1;
5995
5996#if defined(FEAT_MOUSE_DEC)
5997 /* May need to query the mouse position. */
5998 if (WantQueryMouse)
5999 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006000 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
6002 }
6003#endif
6004
6005 /*
6006 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6007 * events. This is a bit complicated, because they might both be defined.
6008 */
6009#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6010# ifdef FEAT_XCLIPBOARD
6011 rest = 0;
6012 if (do_xterm_trace())
6013 rest = msec;
6014# endif
6015 do
6016 {
6017# ifdef FEAT_XCLIPBOARD
6018 if (rest != 0)
6019 {
6020 msec = XT_TRACE_DELAY;
6021 if (rest >= 0 && rest < XT_TRACE_DELAY)
6022 msec = rest;
6023 if (rest >= 0)
6024 rest -= msec;
6025 }
6026# endif
6027# ifdef FEAT_MOUSE_GPM
6028 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006029 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006030 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006032 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033# endif
6034 if (!avail)
6035 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006036 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 return 1;
6038# ifdef FEAT_XCLIPBOARD
6039 if (rest == 0 || !do_xterm_trace())
6040# endif
6041 break;
6042 }
6043 }
6044 while (FALSE
6045# ifdef FEAT_MOUSE_GPM
6046 || (gpm_process_wanted && mch_gpm_process() == 0)
6047# endif
6048# ifdef FEAT_XCLIPBOARD
6049 || (!avail && rest != 0)
6050# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006051 )
6052 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006055 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#endif
6057 return avail;
6058}
6059
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006060#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061/*
6062 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006063 * "msec" == 0 will check for characters once.
6064 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006067 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006068 * "interrupted" (if not NULL) is set to TRUE when no character is available
6069 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071#if defined(__BEOS__)
6072 int
6073#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006074 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006076RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006079 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006080#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 static int busy = FALSE;
6082
6083 /* May retry getting characters after an event was handled. */
6084# define MAY_LOOP
6085
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006086# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /* Remember at what time we started, so that we know how much longer we
6088 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006089 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006090 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006092 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006093 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094# endif
6095
6096 /* Handle being called recursively. This may happen for the session
6097 * manager stuff, it may save the file, which does a breakcheck. */
6098 if (busy)
6099 return 0;
6100#endif
6101
6102#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006103 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#endif
6105 {
6106#ifdef MAY_LOOP
6107 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006108# ifdef FEAT_MZSCHEME
6109 int mzquantum_used = FALSE;
6110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111#endif
6112#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006113 /* each channel may use in, out and err */
6114 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int nfd;
6116# ifdef FEAT_XCLIPBOARD
6117 int xterm_idx = -1;
6118# endif
6119# ifdef FEAT_MOUSE_GPM
6120 int gpm_idx = -1;
6121# endif
6122# ifdef USE_XSMP
6123 int xsmp_idx = -1;
6124# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006125 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006127# ifdef FEAT_MZSCHEME
6128 mzvim_check_threads();
6129 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6130 {
6131 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6132 mzquantum_used = TRUE;
6133 }
6134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 fds[0].fd = fd;
6136 fds[0].events = POLLIN;
6137 nfd = 1;
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006140 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (xterm_Shell != (Widget)0)
6142 {
6143 xterm_idx = nfd;
6144 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6145 fds[nfd].events = POLLIN;
6146 nfd++;
6147 }
6148# endif
6149# ifdef FEAT_MOUSE_GPM
6150 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6151 {
6152 gpm_idx = nfd;
6153 fds[nfd].fd = gpm_fd;
6154 fds[nfd].events = POLLIN;
6155 nfd++;
6156 }
6157# endif
6158# ifdef USE_XSMP
6159 if (xsmp_icefd != -1)
6160 {
6161 xsmp_idx = nfd;
6162 fds[nfd].fd = xsmp_icefd;
6163 fds[nfd].events = POLLIN;
6164 nfd++;
6165 }
6166# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006167#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006168 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006169#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006170 if (interrupted != NULL)
6171 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006173 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006174
6175 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006176 if (result == 0 && interrupted != NULL && ret > 0)
6177 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006178
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006179# ifdef FEAT_MZSCHEME
6180 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006181 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006182 finished = FALSE;
6183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185# ifdef FEAT_XCLIPBOARD
6186 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6187 {
6188 xterm_update(); /* Maybe we should hand out clipboard */
6189 if (--ret == 0 && !input_available())
6190 /* Try again */
6191 finished = FALSE;
6192 }
6193# endif
6194# ifdef FEAT_MOUSE_GPM
6195 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6196 {
6197 *check_for_gpm = 1;
6198 }
6199# endif
6200# ifdef USE_XSMP
6201 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6202 {
6203 if (fds[xsmp_idx].revents & POLLIN)
6204 {
6205 busy = TRUE;
6206 xsmp_handle_requests();
6207 busy = FALSE;
6208 }
6209 else if (fds[xsmp_idx].revents & POLLHUP)
6210 {
6211 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006212 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 xsmp_close();
6214 }
6215 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006216 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006219#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006220 /* also call when ret == 0, we may be polling a keep-open channel */
6221 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006222 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225#else /* HAVE_SELECT */
6226
6227 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006229 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006233# ifdef FEAT_MZSCHEME
6234 mzvim_check_threads();
6235 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6236 {
6237 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6238 mzquantum_used = TRUE;
6239 }
6240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006242 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006244 tv.tv_sec = towait / 1000;
6245 tv.tv_usec = (towait % 1000) * (1000000/1000);
6246 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006248 else
6249 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251 /*
6252 * Select on ready for reading and exceptional condition (end of file).
6253 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006254select_eintr:
6255 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006256 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 FD_ZERO(&efds);
6258 FD_SET(fd, &rfds);
6259# if !defined(__QNX__) && !defined(__CYGWIN32__)
6260 /* For QNX select() always returns 1 if this is set. Why? */
6261 FD_SET(fd, &efds);
6262# endif
6263 maxfd = fd;
6264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006266 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (xterm_Shell != (Widget)0)
6268 {
6269 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6270 if (maxfd < ConnectionNumber(xterm_dpy))
6271 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006272
6273 /* An event may have already been read but not handled. In
6274 * particulary, XFlush may cause this. */
6275 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277# endif
6278# ifdef FEAT_MOUSE_GPM
6279 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6280 {
6281 FD_SET(gpm_fd, &rfds);
6282 FD_SET(gpm_fd, &efds);
6283 if (maxfd < gpm_fd)
6284 maxfd = gpm_fd;
6285 }
6286# endif
6287# ifdef USE_XSMP
6288 if (xsmp_icefd != -1)
6289 {
6290 FD_SET(xsmp_icefd, &rfds);
6291 FD_SET(xsmp_icefd, &efds);
6292 if (maxfd < xsmp_icefd)
6293 maxfd = xsmp_icefd;
6294 }
6295# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006296# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006297 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006298# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006299 if (interrupted != NULL)
6300 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006302 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006303 result = ret > 0 && FD_ISSET(fd, &rfds);
6304 if (result)
6305 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006306 else if (interrupted != NULL && ret > 0)
6307 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006308
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006309# ifdef EINTR
6310 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006311 {
6312 /* Check whether window has been resized, EINTR may be caused by
6313 * SIGWINCH. */
6314 if (do_resize)
6315 handle_resize();
6316
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006317 /* Interrupted by a signal, need to try again. We ignore msec
6318 * here, because we do want to check even after a timeout if
6319 * characters are available. Needed for reading output of an
6320 * external command after the process has finished. */
6321 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006322 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006323# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006324# ifdef __TANDEM
6325 if (ret == -1 && errno == ENOTSUP)
6326 {
6327 FD_ZERO(&rfds);
6328 FD_ZERO(&efds);
6329 ret = 0;
6330 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006331# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006332# ifdef FEAT_MZSCHEME
6333 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006334 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006335 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336# endif
6337
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338# ifdef FEAT_XCLIPBOARD
6339 if (ret > 0 && xterm_Shell != (Widget)0
6340 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6341 {
6342 xterm_update(); /* Maybe we should hand out clipboard */
6343 /* continue looping when we only got the X event and the input
6344 * buffer is empty */
6345 if (--ret == 0 && !input_available())
6346 {
6347 /* Try again */
6348 finished = FALSE;
6349 }
6350 }
6351# endif
6352# ifdef FEAT_MOUSE_GPM
6353 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6354 {
6355 if (FD_ISSET(gpm_fd, &efds))
6356 gpm_close();
6357 else if (FD_ISSET(gpm_fd, &rfds))
6358 *check_for_gpm = 1;
6359 }
6360# endif
6361# ifdef USE_XSMP
6362 if (ret > 0 && xsmp_icefd != -1)
6363 {
6364 if (FD_ISSET(xsmp_icefd, &efds))
6365 {
6366 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006367 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 xsmp_close();
6369 if (--ret == 0)
6370 finished = FALSE; /* keep going if event was only one */
6371 }
6372 else if (FD_ISSET(xsmp_icefd, &rfds))
6373 {
6374 busy = TRUE;
6375 xsmp_handle_requests();
6376 busy = FALSE;
6377 if (--ret == 0)
6378 finished = FALSE; /* keep going if event was only one */
6379 }
6380 }
6381# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006382#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006383 /* also call when ret == 0, we may be polling a keep-open channel */
6384 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006385 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388#endif /* HAVE_SELECT */
6389
6390#ifdef MAY_LOOP
6391 if (finished || msec == 0)
6392 break;
6393
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006394# ifdef FEAT_CLIENTSERVER
6395 if (server_waiting())
6396 break;
6397# endif
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 /* We're going to loop around again, find out for how long */
6400 if (msec > 0)
6401 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006402# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006404 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405# else
6406 /* Guess we got interrupted halfway. */
6407 msec = msec / 2;
6408# endif
6409 if (msec <= 0)
6410 break; /* waited long enough */
6411 }
6412#endif
6413 }
6414
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006415 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416}
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006420 * Expand a path into all matching files and/or directories. Handles "*",
6421 * "?", "[a-z]", "**", etc.
6422 * "path" has backslashes before chars that are not to be expanded.
6423 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
6425 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006426mch_expandpath(
6427 garray_T *gap,
6428 char_u *path,
6429 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
Bram Moolenaar02743632005-07-25 20:42:36 +00006431 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433#endif
6434
6435/*
6436 * mch_expand_wildcards() - this code does wild-card pattern matching using
6437 * the shell
6438 *
6439 * return OK for success, FAIL for error (you may lose some memory) and put
6440 * an error message in *file.
6441 *
6442 * num_pat is number of input patterns
6443 * pat is array of pointers to input patterns
6444 * num_file is pointer to number of matched file names
6445 * file is pointer to array of pointers to matched file names
6446 */
6447
6448#ifndef SEEK_SET
6449# define SEEK_SET 0
6450#endif
6451#ifndef SEEK_END
6452# define SEEK_END 2
6453#endif
6454
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006455#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006458mch_expand_wildcards(
6459 int num_pat,
6460 char_u **pat,
6461 int *num_file,
6462 char_u ***file,
6463 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 int i;
6466 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006467 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 char_u *p;
6469 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
Bram Moolenaarc7247912008-01-13 12:54:11 +00006471 /*
6472 * This is the non-OS/2 implementation (really Unix).
6473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 int j;
6475 char_u *tempname;
6476 char_u *command;
6477 FILE *fd;
6478 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006479#define STYLE_ECHO 0 /* use "echo", the default */
6480#define STYLE_GLOB 1 /* use "glob", for csh */
6481#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6482#define STYLE_PRINT 3 /* use "print -N", for zsh */
6483#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6484 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 int shell_style = STYLE_ECHO;
6486 int check_spaces;
6487 static int did_find_nul = FALSE;
6488 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006489 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006490 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 *num_file = 0; /* default: no files found */
6493 *file = NULL;
6494
6495 /*
6496 * If there are no wildcards, just copy the names to allocated memory.
6497 * Saves a lot of time, because we don't have to start a new shell.
6498 */
6499 if (!have_wildcard(num_pat, pat))
6500 return save_patterns(num_pat, pat, num_file, file);
6501
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006502# ifdef HAVE_SANDBOX
6503 /* Don't allow any shell command in the sandbox. */
6504 if (sandbox != 0 && check_secure())
6505 return FAIL;
6506# endif
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 /*
6509 * Don't allow the use of backticks in secure and restricted mode.
6510 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006511 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 for (i = 0; i < num_pat; ++i)
6513 if (vim_strchr(pat[i], '`') != NULL
6514 && (check_restricted() || check_secure()))
6515 return FAIL;
6516
6517 /*
6518 * get a name for the temp file
6519 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006520 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
6522 EMSG(_(e_notmp));
6523 return FAIL;
6524 }
6525
6526 /*
6527 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006528 * file.
6529 * STYLE_BT: NL separated
6530 * If expanding `cmd` execute it directly.
6531 * STYLE_GLOB: NUL separated
6532 * If we use *csh, "glob" will work better than "echo".
6533 * STYLE_PRINT: NL or NUL separated
6534 * If we use *zsh, "print -N" will work better than "glob".
6535 * STYLE_VIMGLOB: NL separated
6536 * If we use *sh*, we define "vimglob()".
6537 * STYLE_ECHO: space separated.
6538 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 */
6540 if (num_pat == 1 && *pat[0] == '`'
6541 && (len = STRLEN(pat[0])) > 2
6542 && *(pat[0] + len - 1) == '`')
6543 shell_style = STYLE_BT;
6544 else if ((len = STRLEN(p_sh)) >= 3)
6545 {
6546 if (STRCMP(p_sh + len - 3, "csh") == 0)
6547 shell_style = STYLE_GLOB;
6548 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6549 shell_style = STYLE_PRINT;
6550 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006551 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6552 "sh") != NULL)
6553 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
Bram Moolenaarc7247912008-01-13 12:54:11 +00006555 /* Compute the length of the command. We need 2 extra bytes: for the
6556 * optional '&' and for the NUL.
6557 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006559 if (shell_style == STYLE_VIMGLOB)
6560 len += STRLEN(sh_vimglob_func);
6561
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006562 for (i = 0; i < num_pat; ++i)
6563 {
6564 /* Count the length of the patterns in the same way as they are put in
6565 * "command" below. */
6566#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006568#else
6569 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006570 for (j = 0; pat[i][j] != NUL; ++j)
6571 {
6572 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6573 ++len; /* may add a backslash */
6574 ++len;
6575 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006576#endif
6577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 command = alloc(len);
6579 if (command == NULL)
6580 {
6581 /* out of memory */
6582 vim_free(tempname);
6583 return FAIL;
6584 }
6585
6586 /*
6587 * Build the shell command:
6588 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6589 * recognizes this).
6590 * - Add the shell command to print the expanded names.
6591 * - Add the temp file name.
6592 * - Add the file name patterns.
6593 */
6594 if (shell_style == STYLE_BT)
6595 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006596 /* change `command; command& ` to (command; command ) */
6597 STRCPY(command, "(");
6598 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006600 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006601 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 --p;
6603 if (*p == '&') /* remove trailing '&' */
6604 {
6605 ampersent = TRUE;
6606 *p = ' ';
6607 }
6608 STRCAT(command, ">");
6609 }
6610 else
6611 {
6612 if (flags & EW_NOTFOUND)
6613 STRCPY(command, "set nonomatch; ");
6614 else
6615 STRCPY(command, "unset nonomatch; ");
6616 if (shell_style == STYLE_GLOB)
6617 STRCAT(command, "glob >");
6618 else if (shell_style == STYLE_PRINT)
6619 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006620 else if (shell_style == STYLE_VIMGLOB)
6621 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 else
6623 STRCAT(command, "echo >");
6624 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006627
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 if (shell_style != STYLE_BT)
6629 for (i = 0; i < num_pat; ++i)
6630 {
6631 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006632 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006633 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634#ifdef USE_SYSTEM
6635 STRCAT(command, " \"");
6636 STRCAT(command, pat[i]);
6637 STRCAT(command, "\"");
6638#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006639 int intick = FALSE;
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 p = command + STRLEN(command);
6642 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006643 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006644 {
6645 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006646 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006647 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6648 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006649 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006650 * backslash inside backticks, before a special character
6651 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006652 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006653 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6654 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006655 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006656 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006657 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006658 else if (!intick
6659 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6660 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006661 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006662 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6663 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006664 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006665
6666 /* Copy one character. */
6667 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 *p = NUL;
6670#endif
6671 }
6672 if (flags & EW_SILENT)
6673 show_shell_mess = FALSE;
6674 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006675 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676
6677 /*
6678 * Using zsh -G: If a pattern has no matches, it is just deleted from
6679 * the argument list, otherwise zsh gives an error message and doesn't
6680 * expand any other pattern.
6681 */
6682 if (shell_style == STYLE_PRINT)
6683 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6684
6685 /*
6686 * If we use -f then shell variables set in .cshrc won't get expanded.
6687 * vi can do it, so we will too, but it is only necessary if there is a "$"
6688 * in one of the patterns, otherwise we can still use the fast option.
6689 */
6690 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6691 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6692
6693 /*
6694 * execute the shell command
6695 */
6696 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6697
6698 /* When running in the background, give it some time to create the temp
6699 * file, but don't wait for it to finish. */
6700 if (ampersent)
6701 mch_delay(10L, TRUE);
6702
6703 extra_shell_arg = NULL; /* cleanup */
6704 show_shell_mess = TRUE;
6705 vim_free(command);
6706
Bram Moolenaarc7247912008-01-13 12:54:11 +00006707 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
6709 mch_remove(tempname);
6710 vim_free(tempname);
6711 /*
6712 * With interactive completion, the error message is not printed.
6713 * However with USE_SYSTEM, I don't know how to turn off error messages
6714 * from the shell, so screen may still get messed up -- webb.
6715 */
6716#ifndef USE_SYSTEM
6717 if (!(flags & EW_SILENT))
6718#endif
6719 {
6720 redraw_later_clear(); /* probably messed up screen */
6721 msg_putchar('\n'); /* clear bottom line quickly */
6722 cmdline_row = Rows - 1; /* continue on last line */
6723#ifdef USE_SYSTEM
6724 if (!(flags & EW_SILENT))
6725#endif
6726 {
6727 MSG(_(e_wildexpand));
6728 msg_start(); /* don't overwrite this message */
6729 }
6730 }
6731 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6732 * EW_NOTFOUND is given */
6733 if (shell_style == STYLE_BT)
6734 return FAIL;
6735 goto notfound;
6736 }
6737
6738 /*
6739 * read the names from the file into memory
6740 */
6741 fd = fopen((char *)tempname, READBIN);
6742 if (fd == NULL)
6743 {
6744 /* Something went wrong, perhaps a file name with a special char. */
6745 if (!(flags & EW_SILENT))
6746 {
6747 MSG(_(e_wildexpand));
6748 msg_start(); /* don't overwrite this message */
6749 }
6750 vim_free(tempname);
6751 goto notfound;
6752 }
6753 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006754 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006756 if (llen < 0)
6757 /* just in case ftell() would fail */
6758 buffer = NULL;
6759 else
6760 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (buffer == NULL)
6762 {
6763 /* out of memory */
6764 mch_remove(tempname);
6765 vim_free(tempname);
6766 fclose(fd);
6767 return FAIL;
6768 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006769 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 i = fread((char *)buffer, 1, len, fd);
6771 fclose(fd);
6772 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006773 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 /* unexpected read error */
6776 EMSG2(_(e_notread), tempname);
6777 vim_free(tempname);
6778 vim_free(buffer);
6779 return FAIL;
6780 }
6781 vim_free(tempname);
6782
Bram Moolenaarc7247912008-01-13 12:54:11 +00006783# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6785 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006786 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6788 *p++ = buffer[i];
6789 len = p - buffer;
6790# endif
6791
6792
6793 /* file names are separated with Space */
6794 if (shell_style == STYLE_ECHO)
6795 {
6796 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6797 p = buffer;
6798 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6799 {
6800 while (*p != ' ' && *p != '\n')
6801 ++p;
6802 p = skipwhite(p); /* skip to next entry */
6803 }
6804 }
6805 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006806 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6809 p = buffer;
6810 for (i = 0; *p != NUL; ++i) /* count number of entries */
6811 {
6812 while (*p != '\n' && *p != NUL)
6813 ++p;
6814 if (*p != NUL)
6815 ++p;
6816 p = skipwhite(p); /* skip leading white space */
6817 }
6818 }
6819 /* file names are separated with NUL */
6820 else
6821 {
6822 /*
6823 * Some versions of zsh use spaces instead of NULs to separate
6824 * results. Only do this when there is no NUL before the end of the
6825 * buffer, otherwise we would never be able to use file names with
6826 * embedded spaces when zsh does use NULs.
6827 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6828 * don't check for spaces again.
6829 */
6830 check_spaces = FALSE;
6831 if (shell_style == STYLE_PRINT && !did_find_nul)
6832 {
6833 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006834 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006835 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 did_find_nul = TRUE;
6837 else
6838 check_spaces = TRUE;
6839 }
6840
6841 /*
6842 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6843 * already is one, for STYLE_GLOB it needs to be added.
6844 */
6845 if (len && buffer[len - 1] == NUL)
6846 --len;
6847 else
6848 buffer[len] = NUL;
6849 i = 0;
6850 for (p = buffer; p < buffer + len; ++p)
6851 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6852 {
6853 ++i;
6854 *p = NUL;
6855 }
6856 if (len)
6857 ++i; /* count last entry */
6858 }
6859 if (i == 0)
6860 {
6861 /*
6862 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6863 * /bin/sh will happily expand it to nothing rather than returning an
6864 * error; and hey, it's good to check anyway -- webb.
6865 */
6866 vim_free(buffer);
6867 goto notfound;
6868 }
6869 *num_file = i;
6870 *file = (char_u **)alloc(sizeof(char_u *) * i);
6871 if (*file == NULL)
6872 {
6873 /* out of memory */
6874 vim_free(buffer);
6875 return FAIL;
6876 }
6877
6878 /*
6879 * Isolate the individual file names.
6880 */
6881 p = buffer;
6882 for (i = 0; i < *num_file; ++i)
6883 {
6884 (*file)[i] = p;
6885 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006886 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6887 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006889 while (!(shell_style == STYLE_ECHO && *p == ' ')
6890 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 ++p;
6892 if (p == buffer + len) /* last entry */
6893 *p = NUL;
6894 else
6895 {
6896 *p++ = NUL;
6897 p = skipwhite(p); /* skip to next entry */
6898 }
6899 }
6900 else /* NUL separates */
6901 {
6902 while (*p && p < buffer + len) /* skip entry */
6903 ++p;
6904 ++p; /* skip NUL */
6905 }
6906 }
6907
6908 /*
6909 * Move the file names to allocated memory.
6910 */
6911 for (j = 0, i = 0; i < *num_file; ++i)
6912 {
6913 /* Require the files to exist. Helps when using /bin/sh */
6914 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6915 continue;
6916
6917 /* check if this entry should be included */
6918 dir = (mch_isdir((*file)[i]));
6919 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6920 continue;
6921
Bram Moolenaara2031822006-03-07 22:29:51 +00006922 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006923 if (!dir && (flags & EW_EXEC)
6924 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006925 continue;
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6928 if (p)
6929 {
6930 STRCPY(p, (*file)[i]);
6931 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006932 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 (*file)[j++] = p;
6934 }
6935 }
6936 vim_free(buffer);
6937 *num_file = j;
6938
6939 if (*num_file == 0) /* rejected all entries */
6940 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006941 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 goto notfound;
6943 }
6944
6945 return OK;
6946
6947notfound:
6948 if (flags & EW_NOTFOUND)
6949 return save_patterns(num_pat, pat, num_file, file);
6950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951}
6952
6953#endif /* VMS */
6954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006956save_patterns(
6957 int num_pat,
6958 char_u **pat,
6959 int *num_file,
6960 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
6962 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006963 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
6965 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6966 if (*file == NULL)
6967 return FAIL;
6968 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006969 {
6970 s = vim_strsave(pat[i]);
6971 if (s != NULL)
6972 /* Be compatible with expand_filename(): halve the number of
6973 * backslashes. */
6974 backslash_halve(s);
6975 (*file)[i] = s;
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 *num_file = num_pat;
6978 return OK;
6979}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981/*
6982 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6983 * expand.
6984 */
6985 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006986mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006988 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (*p == '\\' && p[1] != NUL)
6991 ++p;
6992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (vim_strchr((char_u *)
6994#ifdef VMS
6995 "*?%"
6996#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
6999 , *p) != NULL)
7000 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002 return FALSE;
7003}
7004
7005/*
7006 * Return TRUE if the string "p" contains a wildcard.
7007 * Don't recognize '~' at the end as a wildcard.
7008 */
7009 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007010mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007012 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (*p == '\\' && p[1] != NUL)
7015 ++p;
7016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (vim_strchr((char_u *)
7018#ifdef VMS
7019 "*?%$"
7020#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022#endif
7023 , *p) != NULL
7024 || (*p == '~' && p[1] != NUL))
7025 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027 return FALSE;
7028}
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007031have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 int i;
7034
7035 for (i = 0; i < num; i++)
7036 if (mch_has_wildcard(file[i]))
7037 return 1;
7038 return 0;
7039}
7040
7041 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007042have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 int i;
7045
7046 for (i = 0; i < num; i++)
7047 if (vim_strchr(file[i], '$') != NULL)
7048 return TRUE;
7049 return FALSE;
7050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007052#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053/*
7054 * Scaled-down version of rename(), which is missing in Xenix.
7055 * This version can only move regular files and will fail if the
7056 * destination exists.
7057 */
7058 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007059mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 struct stat st;
7062
7063 if (stat(dest, &st) >= 0) /* fail if destination exists */
7064 return -1;
7065 if (link(src, dest) != 0) /* link file to new name */
7066 return -1;
7067 if (mch_remove(src) == 0) /* delete link to old name */
7068 return 0;
7069 return -1;
7070}
7071#endif /* !HAVE_RENAME */
7072
7073#ifdef FEAT_MOUSE_GPM
7074/*
7075 * Initializes connection with gpm (if it isn't already opened)
7076 * Return 1 if succeeded (or connection already opened), 0 if failed
7077 */
7078 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007079gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
7081 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7082
7083 if (!gpm_flag)
7084 {
7085 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7086 gpm_connect.defaultMask = ~GPM_HARD;
7087 /* Default handling for mouse move*/
7088 gpm_connect.minMod = 0; /* Handle any modifier keys */
7089 gpm_connect.maxMod = 0xffff;
7090 if (Gpm_Open(&gpm_connect, 0) > 0)
7091 {
7092 /* gpm library tries to handling TSTP causes
7093 * problems. Anyways, we close connection to Gpm whenever
7094 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007095 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007097# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 return 1; /* succeed */
7101 }
7102 if (gpm_fd == -2)
7103 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7104 return 0;
7105 }
7106 return 1; /* already open */
7107}
7108
7109/*
7110 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 */
7112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007113gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115 if (gpm_flag && gpm_fd >= 0) /* if Open */
7116 Gpm_Close();
7117}
7118
7119/* Reads gpm event and adds special keys to input buf. Returns length of
7120 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007121 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 */
7123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007124mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 int button;
7127 static Gpm_Event gpm_event;
7128 char_u string[6];
7129 int_u vim_modifiers;
7130 int row,col;
7131 unsigned char buttons_mask;
7132 unsigned char gpm_modifiers;
7133 static unsigned char old_buttons = 0;
7134
7135 Gpm_GetEvent(&gpm_event);
7136
7137#ifdef FEAT_GUI
7138 /* Don't put events in the input queue now. */
7139 if (hold_gui_events)
7140 return 0;
7141#endif
7142
7143 row = gpm_event.y - 1;
7144 col = gpm_event.x - 1;
7145
7146 string[0] = ESC; /* Our termcode */
7147 string[1] = 'M';
7148 string[2] = 'G';
7149 switch (GPM_BARE_EVENTS(gpm_event.type))
7150 {
7151 case GPM_DRAG:
7152 string[3] = MOUSE_DRAG;
7153 break;
7154 case GPM_DOWN:
7155 buttons_mask = gpm_event.buttons & ~old_buttons;
7156 old_buttons = gpm_event.buttons;
7157 switch (buttons_mask)
7158 {
7159 case GPM_B_LEFT:
7160 button = MOUSE_LEFT;
7161 break;
7162 case GPM_B_MIDDLE:
7163 button = MOUSE_MIDDLE;
7164 break;
7165 case GPM_B_RIGHT:
7166 button = MOUSE_RIGHT;
7167 break;
7168 default:
7169 return 0;
7170 /*Don't know what to do. Can more than one button be
7171 * reported in one event? */
7172 }
7173 string[3] = (char_u)(button | 0x20);
7174 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7175 break;
7176 case GPM_UP:
7177 string[3] = MOUSE_RELEASE;
7178 old_buttons &= ~gpm_event.buttons;
7179 break;
7180 default:
7181 return 0;
7182 }
7183 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7184 gpm_modifiers = gpm_event.modifiers;
7185 vim_modifiers = 0x0;
7186 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7187 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7188 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7189 */
7190 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7191 vim_modifiers |= MOUSE_SHIFT;
7192
7193 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7194 vim_modifiers |= MOUSE_CTRL;
7195 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7196 vim_modifiers |= MOUSE_ALT;
7197 string[3] |= vim_modifiers;
7198 string[4] = (char_u)(col + ' ' + 1);
7199 string[5] = (char_u)(row + ' ' + 1);
7200 add_to_input_buf(string, 6);
7201 return 6;
7202}
7203#endif /* FEAT_MOUSE_GPM */
7204
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007205#ifdef FEAT_SYSMOUSE
7206/*
7207 * Initialize connection with sysmouse.
7208 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7209 * output, any sysmouse output than will be processed via sig_sysmouse().
7210 * Return OK if succeeded, FAIL if failed.
7211 */
7212 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007213sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007214{
7215 struct mouse_info mouse;
7216
7217 mouse.operation = MOUSE_MODE;
7218 mouse.u.mode.mode = 0;
7219 mouse.u.mode.signal = SIGUSR2;
7220 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7221 {
7222 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7223 mouse.operation = MOUSE_SHOW;
7224 ioctl(1, CONS_MOUSECTL, &mouse);
7225 return OK;
7226 }
7227 return FAIL;
7228}
7229
7230/*
7231 * Stop processing SIGUSR2 signals, and also make sure that
7232 * virtual console do not send us any sysmouse related signal.
7233 */
7234 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007235sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007236{
7237 struct mouse_info mouse;
7238
7239 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7240 mouse.operation = MOUSE_MODE;
7241 mouse.u.mode.mode = 0;
7242 mouse.u.mode.signal = 0;
7243 ioctl(1, CONS_MOUSECTL, &mouse);
7244}
7245
7246/*
7247 * Gets info from sysmouse and adds special keys to input buf.
7248 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007249 static RETSIGTYPE
7250sig_sysmouse SIGDEFARG(sigarg)
7251{
7252 struct mouse_info mouse;
7253 struct video_info video;
7254 char_u string[6];
7255 int row, col;
7256 int button;
7257 int buttons;
7258 static int oldbuttons = 0;
7259
7260#ifdef FEAT_GUI
7261 /* Don't put events in the input queue now. */
7262 if (hold_gui_events)
7263 return;
7264#endif
7265
7266 mouse.operation = MOUSE_GETINFO;
7267 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7268 && ioctl(1, FBIO_MODEINFO, &video) != -1
7269 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7270 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7271 {
7272 row = mouse.u.data.y / video.vi_cheight;
7273 col = mouse.u.data.x / video.vi_cwidth;
7274 buttons = mouse.u.data.buttons;
7275 string[0] = ESC; /* Our termcode */
7276 string[1] = 'M';
7277 string[2] = 'S';
7278 if (oldbuttons == buttons && buttons != 0)
7279 {
7280 button = MOUSE_DRAG;
7281 }
7282 else
7283 {
7284 switch (buttons)
7285 {
7286 case 0:
7287 button = MOUSE_RELEASE;
7288 break;
7289 case 1:
7290 button = MOUSE_LEFT;
7291 break;
7292 case 2:
7293 button = MOUSE_MIDDLE;
7294 break;
7295 case 4:
7296 button = MOUSE_RIGHT;
7297 break;
7298 default:
7299 return;
7300 }
7301 oldbuttons = buttons;
7302 }
7303 string[3] = (char_u)(button);
7304 string[4] = (char_u)(col + ' ' + 1);
7305 string[5] = (char_u)(row + ' ' + 1);
7306 add_to_input_buf(string, 6);
7307 }
7308 return;
7309}
7310#endif /* FEAT_SYSMOUSE */
7311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007313typedef char_u * (*STRPROCSTR)(char_u *);
7314typedef char_u * (*INTPROCSTR)(int);
7315typedef int (*STRPROCINT)(char_u *);
7316typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318/*
7319 * Call a DLL routine which takes either a string or int param
7320 * and returns an allocated string.
7321 */
7322 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007323mch_libcall(
7324 char_u *libname,
7325 char_u *funcname,
7326 char_u *argstring, /* NULL when using a argint */
7327 int argint,
7328 char_u **string_result,/* NULL when using number_result */
7329 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331# if defined(USE_DLOPEN)
7332 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007333 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334# else
7335 shl_t hinstLib;
7336# endif
7337 STRPROCSTR ProcAdd;
7338 INTPROCSTR ProcAddI;
7339 char_u *retval_str = NULL;
7340 int retval_int = 0;
7341 int success = FALSE;
7342
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007343 /*
7344 * Get a handle to the DLL module.
7345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007347 /* First clear any error, it's not cleared by the dlopen() call. */
7348 (void)dlerror();
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 hinstLib = dlopen((char *)libname, RTLD_LAZY
7351# ifdef RTLD_LOCAL
7352 | RTLD_LOCAL
7353# endif
7354 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007355 if (hinstLib == NULL)
7356 {
7357 /* "dlerr" must be used before dlclose() */
7358 dlerr = (char *)dlerror();
7359 if (dlerr != NULL)
7360 EMSG2(_("dlerror = \"%s\""), dlerr);
7361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362# else
7363 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7364# endif
7365
7366 /* If the handle is valid, try to get the function address. */
7367 if (hinstLib != NULL)
7368 {
7369# ifdef HAVE_SETJMP_H
7370 /*
7371 * Catch a crash when calling the library function. For example when
7372 * using a number where a string pointer is expected.
7373 */
7374 mch_startjmp();
7375 if (SETJMP(lc_jump_env) != 0)
7376 {
7377 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007378# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007379 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 mch_didjmp();
7382 }
7383 else
7384# endif
7385 {
7386 retval_str = NULL;
7387 retval_int = 0;
7388
7389 if (argstring != NULL)
7390 {
7391# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007392 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007393 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394# else
7395 if (shl_findsym(&hinstLib, (const char *)funcname,
7396 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7397 ProcAdd = NULL;
7398# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 if ((success = (ProcAdd != NULL
7400# if defined(USE_DLOPEN)
7401 && dlerr == NULL
7402# endif
7403 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 {
7405 if (string_result == NULL)
7406 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7407 else
7408 retval_str = (ProcAdd)(argstring);
7409 }
7410 }
7411 else
7412 {
7413# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007414 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007415 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416# else
7417 if (shl_findsym(&hinstLib, (const char *)funcname,
7418 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7419 ProcAddI = NULL;
7420# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007421 if ((success = (ProcAddI != NULL
7422# if defined(USE_DLOPEN)
7423 && dlerr == NULL
7424# endif
7425 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
7427 if (string_result == NULL)
7428 retval_int = ((INTPROCINT)ProcAddI)(argint);
7429 else
7430 retval_str = (ProcAddI)(argint);
7431 }
7432 }
7433
7434 /* Save the string before we free the library. */
7435 /* Assume that a "1" or "-1" result is an illegal pointer. */
7436 if (string_result == NULL)
7437 *number_result = retval_int;
7438 else if (retval_str != NULL
7439 && retval_str != (char_u *)1
7440 && retval_str != (char_u *)-1)
7441 *string_result = vim_strsave(retval_str);
7442 }
7443
7444# ifdef HAVE_SETJMP_H
7445 mch_endjmp();
7446# ifdef SIGHASARG
7447 if (lc_signal != 0)
7448 {
7449 int i;
7450
7451 /* try to find the name of this signal */
7452 for (i = 0; signal_info[i].sig != -1; i++)
7453 if (lc_signal == signal_info[i].sig)
7454 break;
7455 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7456 }
7457# endif
7458# endif
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007461 /* "dlerr" must be used before dlclose() */
7462 if (dlerr != NULL)
7463 EMSG2(_("dlerror = \"%s\""), dlerr);
7464
7465 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 (void)dlclose(hinstLib);
7467# else
7468 (void)shl_unload(hinstLib);
7469# endif
7470 }
7471
7472 if (!success)
7473 {
7474 EMSG2(_(e_libcall), funcname);
7475 return FAIL;
7476 }
7477
7478 return OK;
7479}
7480#endif
7481
7482#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7483static int xterm_trace = -1; /* default: disabled */
7484static int xterm_button;
7485
7486/*
7487 * Setup a dummy window for X selections in a terminal.
7488 */
7489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007490setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 int z = 0;
7493 char *strp = "";
7494 Widget AppShell;
7495
7496 if (!x_connect_to_server())
7497 return;
7498
7499 open_app_context();
7500 if (app_context != NULL && xterm_Shell == (Widget)0)
7501 {
7502 int (*oldhandler)();
7503#if defined(HAVE_SETJMP_H)
7504 int (*oldIOhandler)();
7505#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007506# ifdef ELAPSED_FUNC
7507 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
7509 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007510 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511# endif
7512
7513 /* Ignore X errors while opening the display */
7514 oldhandler = XSetErrorHandler(x_error_check);
7515
7516#if defined(HAVE_SETJMP_H)
7517 /* Ignore X IO errors while opening the display */
7518 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7519 mch_startjmp();
7520 if (SETJMP(lc_jump_env) != 0)
7521 {
7522 mch_didjmp();
7523 xterm_dpy = NULL;
7524 }
7525 else
7526#endif
7527 {
7528 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7529 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7530#if defined(HAVE_SETJMP_H)
7531 mch_endjmp();
7532#endif
7533 }
7534
7535#if defined(HAVE_SETJMP_H)
7536 /* Now handle X IO errors normally. */
7537 (void)XSetIOErrorHandler(oldIOhandler);
7538#endif
7539 /* Now handle X errors normally. */
7540 (void)XSetErrorHandler(oldhandler);
7541
7542 if (xterm_dpy == NULL)
7543 {
7544 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007545 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 return;
7547 }
7548
7549 /* Catch terminating error of the X server connection. */
7550 (void)XSetIOErrorHandler(x_IOerror_handler);
7551
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007552# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007554 {
7555 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007556 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007557 verbose_leave();
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559# endif
7560
7561 /* Create a Shell to make converters work. */
7562 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7563 applicationShellWidgetClass, xterm_dpy,
7564 NULL);
7565 if (AppShell == (Widget)0)
7566 return;
7567 xterm_Shell = XtVaCreatePopupShell("VIM",
7568 topLevelShellWidgetClass, AppShell,
7569 XtNmappedWhenManaged, 0,
7570 XtNwidth, 1,
7571 XtNheight, 1,
7572 NULL);
7573 if (xterm_Shell == (Widget)0)
7574 return;
7575
7576 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007577 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (x11_display == NULL)
7579 x11_display = xterm_dpy;
7580
7581 XtRealizeWidget(xterm_Shell);
7582 XSync(xterm_dpy, False);
7583 xterm_update();
7584 }
7585 if (xterm_Shell != (Widget)0)
7586 {
7587 clip_init(TRUE);
7588 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7589 x11_window = (Window)atol(strp);
7590 /* Check if $WINDOWID is valid. */
7591 if (test_x11_window(xterm_dpy) == FAIL)
7592 x11_window = 0;
7593 if (x11_window != 0)
7594 xterm_trace = 0;
7595 }
7596}
7597
7598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007599start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7602 return;
7603 xterm_trace = 1;
7604 xterm_button = button;
7605 do_xterm_trace();
7606}
7607
7608
7609 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007610stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 if (xterm_trace < 0)
7613 return;
7614 xterm_trace = 0;
7615}
7616
7617/*
7618 * Query the xterm pointer and generate mouse termcodes if necessary
7619 * return TRUE if dragging is active, else FALSE
7620 */
7621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007622do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 Window root, child;
7625 int root_x, root_y;
7626 int win_x, win_y;
7627 int row, col;
7628 int_u mask_return;
7629 char_u buf[50];
7630 char_u *strp;
7631 long got_hints;
7632 static char_u *mouse_code;
7633 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7634 static int prev_row = 0, prev_col = 0;
7635 static XSizeHints xterm_hints;
7636
7637 if (xterm_trace <= 0)
7638 return FALSE;
7639
7640 if (xterm_trace == 1)
7641 {
7642 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007643 * have changed recently. */
7644 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7645 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 || xterm_hints.width_inc <= 1
7647 || xterm_hints.height_inc <= 1)
7648 {
7649 xterm_trace = -1; /* Not enough data -- disable tracing */
7650 return FALSE;
7651 }
7652
7653 /* Rely on the same mouse code for the duration of this */
7654 mouse_code = find_termcode(mouse_name);
7655 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007656 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 xterm_trace = 2;
7658
7659 /* Find the offset of the chars, there might be a scrollbar on the
7660 * left of the window and/or a menu on the top (eterm etc.) */
7661 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7662 &win_x, &win_y, &mask_return);
7663 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7664 - (xterm_hints.height_inc / 2);
7665 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7666 xterm_hints.y = 2;
7667 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7668 - (xterm_hints.width_inc / 2);
7669 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7670 xterm_hints.x = 2;
7671 return TRUE;
7672 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007673 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
7675 xterm_trace = 0;
7676 return FALSE;
7677 }
7678
7679 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7680 &win_x, &win_y, &mask_return);
7681
7682 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7683 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7684 if (row == prev_row && col == prev_col)
7685 return TRUE;
7686
7687 STRCPY(buf, mouse_code);
7688 strp = buf + STRLEN(buf);
7689 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7690 *strp++ = (char_u)(col + ' ' + 1);
7691 *strp++ = (char_u)(row + ' ' + 1);
7692 *strp = 0;
7693 add_to_input_buf(buf, STRLEN(buf));
7694
7695 prev_row = row;
7696 prev_col = col;
7697 return TRUE;
7698}
7699
7700# if defined(FEAT_GUI) || defined(PROTO)
7701/*
7702 * Destroy the display, window and app_context. Required for GTK.
7703 */
7704 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007705clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 if (xterm_Shell != (Widget)0)
7708 {
7709 XtDestroyWidget(xterm_Shell);
7710 xterm_Shell = (Widget)0;
7711 }
7712 if (xterm_dpy != NULL)
7713 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007714# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 /* Lesstif and Solaris crash here, lose some memory */
7716 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (x11_display == xterm_dpy)
7719 x11_display = NULL;
7720 xterm_dpy = NULL;
7721 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007722# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 if (app_context != (XtAppContext)NULL)
7724 {
7725 /* Lesstif and Solaris crash here, lose some memory */
7726 XtDestroyApplicationContext(app_context);
7727 app_context = (XtAppContext)NULL;
7728 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730}
7731# endif
7732
7733/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007734 * Catch up with GUI or X events.
7735 */
7736 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007737clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007738{
7739# ifdef FEAT_GUI
7740 if (gui.in_use)
7741 gui_mch_update();
7742 else
7743# endif
7744 if (xterm_Shell != (Widget)0)
7745 xterm_update();
7746}
7747
7748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 * Catch up with any queued X events. This may put keyboard input into the
7750 * input buffer, call resize call-backs, trigger timers etc. If there is
7751 * nothing in the X event queue (& no timers pending), then we return
7752 * immediately.
7753 */
7754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007755xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 XEvent event;
7758
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007759 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007761 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007763 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007764 break;
7765
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007766 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007767 {
7768 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007769 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007770#ifdef FEAT_CLIENTSERVER
7771 {
7772 XPropertyEvent *e = (XPropertyEvent *)&event;
7773
7774 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007776 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007779 XtDispatchEvent(&event);
7780 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007781 else
7782 {
7783 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007784 XtAppProcessEvent(app_context, mask);
7785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 }
7787}
7788
7789 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007790clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 if (xterm_Shell != (Widget)0)
7793 return clip_x11_own_selection(xterm_Shell, cbd);
7794 return FAIL;
7795}
7796
7797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007798clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 if (xterm_Shell != (Widget)0)
7801 clip_x11_lose_selection(xterm_Shell, cbd);
7802}
7803
7804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007805clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 if (xterm_Shell != (Widget)0)
7808 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7809}
7810
7811 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007812clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 clip_x11_set_selection(cbd);
7815}
7816#endif
7817
7818
7819#if defined(USE_XSMP) || defined(PROTO)
7820/*
7821 * Code for X Session Management Protocol.
7822 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007823static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7824static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7825static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7826static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7827static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828
7829
7830# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007831static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
7833/*
7834 * This is our chance to ask the user if they want to save,
7835 * or abort the logout
7836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007838xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 cmdmod_T save_cmdmod;
7841 int cancel_shutdown = False;
7842
7843 save_cmdmod = cmdmod;
7844 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007845 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 /* Mustn't logout */
7847 cancel_shutdown = True;
7848 cmdmod = save_cmdmod;
7849 setcursor(); /* position cursor */
7850 out_flush();
7851
7852 /* Done interaction */
7853 SmcInteractDone(smc_conn, cancel_shutdown);
7854
7855 /* Finish off
7856 * Only end save-yourself here if we're not cancelling shutdown;
7857 * we'll get a cancelled callback later in which we'll end it.
7858 * Hopefully get around glitchy SMs (like GNOME-1)
7859 */
7860 if (!cancel_shutdown)
7861 {
7862 xsmp.save_yourself = False;
7863 SmcSaveYourselfDone(smc_conn, True);
7864 }
7865}
7866# endif
7867
7868/*
7869 * Callback that starts save-yourself.
7870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007872xsmp_handle_save_yourself(
7873 SmcConn smc_conn,
7874 SmPointer client_data UNUSED,
7875 int save_type UNUSED,
7876 Bool shutdown,
7877 int interact_style UNUSED,
7878 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 /* Handle already being in saveyourself */
7881 if (xsmp.save_yourself)
7882 SmcSaveYourselfDone(smc_conn, True);
7883 xsmp.save_yourself = True;
7884 xsmp.shutdown = shutdown;
7885
7886 /* First up, preserve all files */
7887 out_flush();
7888 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7889
7890 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007891 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892
7893# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7894 /* Now see if we can ask about unsaved files */
7895 if (shutdown && !fast && gui.in_use)
7896 /* Need to interact with user, but need SM's permission */
7897 SmcInteractRequest(smc_conn, SmDialogError,
7898 xsmp_handle_interaction, client_data);
7899 else
7900# endif
7901 {
7902 /* Can stop the cycle here */
7903 SmcSaveYourselfDone(smc_conn, True);
7904 xsmp.save_yourself = False;
7905 }
7906}
7907
7908
7909/*
7910 * Callback to warn us of imminent death.
7911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007913xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 xsmp_close();
7916
7917 /* quit quickly leaving swapfiles for modified buffers behind */
7918 getout_preserve_modified(0);
7919}
7920
7921
7922/*
7923 * Callback to tell us that save-yourself has completed.
7924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007926xsmp_save_complete(
7927 SmcConn smc_conn UNUSED,
7928 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 xsmp.save_yourself = False;
7931}
7932
7933
7934/*
7935 * Callback to tell us that an instigated shutdown was cancelled
7936 * (maybe even by us)
7937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007939xsmp_shutdown_cancelled(
7940 SmcConn smc_conn,
7941 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 if (xsmp.save_yourself)
7944 SmcSaveYourselfDone(smc_conn, True);
7945 xsmp.save_yourself = False;
7946 xsmp.shutdown = False;
7947}
7948
7949
7950/*
7951 * Callback to tell us that a new ICE connection has been established.
7952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007954xsmp_ice_connection(
7955 IceConn iceConn,
7956 IcePointer clientData UNUSED,
7957 Bool opening,
7958 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 /* Intercept creation of ICE connection fd */
7961 if (opening)
7962 {
7963 xsmp_icefd = IceConnectionNumber(iceConn);
7964 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7965 }
7966}
7967
7968
7969/* Handle any ICE processing that's required; return FAIL if SM lost */
7970 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007971xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 Bool rep;
7974
7975 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7976 == IceProcessMessagesIOError)
7977 {
7978 /* Lost ICE */
7979 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007980 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 xsmp_close();
7982 return FAIL;
7983 }
7984 else
7985 return OK;
7986}
7987
7988static int dummy;
7989
7990/* Set up X Session Management Protocol */
7991 void
7992xsmp_init(void)
7993{
7994 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 SmcCallbacks smcallbacks;
7996#if 0
7997 SmPropValue smname;
7998 SmProp smnameprop;
7999 SmProp *smprops[1];
8000#endif
8001
8002 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008003 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005 xsmp.save_yourself = xsmp.shutdown = False;
8006
8007 /* Set up SM callbacks - must have all, even if they're not used */
8008 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8009 smcallbacks.save_yourself.client_data = NULL;
8010 smcallbacks.die.callback = xsmp_die;
8011 smcallbacks.die.client_data = NULL;
8012 smcallbacks.save_complete.callback = xsmp_save_complete;
8013 smcallbacks.save_complete.client_data = NULL;
8014 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8015 smcallbacks.shutdown_cancelled.client_data = NULL;
8016
8017 /* Set up a watch on ICE connection creations. The "dummy" argument is
8018 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8019 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8020 {
8021 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008022 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 return;
8024 }
8025
8026 /* Create an SM connection */
8027 xsmp.smcconn = SmcOpenConnection(
8028 NULL,
8029 NULL,
8030 SmProtoMajor,
8031 SmProtoMinor,
8032 SmcSaveYourselfProcMask | SmcDieProcMask
8033 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8034 &smcallbacks,
8035 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008036 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 sizeof(errorstring),
8038 errorstring);
8039 if (xsmp.smcconn == NULL)
8040 {
8041 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008042
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008044 {
8045 vim_snprintf(errorreport, sizeof(errorreport),
8046 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8047 verb_msg((char_u *)errorreport);
8048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 return;
8050 }
8051 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8052
8053#if 0
8054 /* ID ourselves */
8055 smname.value = "vim";
8056 smname.length = 3;
8057 smnameprop.name = "SmProgram";
8058 smnameprop.type = "SmARRAY8";
8059 smnameprop.num_vals = 1;
8060 smnameprop.vals = &smname;
8061
8062 smprops[0] = &smnameprop;
8063 SmcSetProperties(xsmp.smcconn, 1, smprops);
8064#endif
8065}
8066
8067
8068/* Shut down XSMP comms. */
8069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008070xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 if (xsmp_icefd != -1)
8073 {
8074 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008075 if (xsmp.clientid != NULL)
8076 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008077 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 xsmp_icefd = -1;
8079 }
8080}
8081#endif /* USE_XSMP */
8082
8083
8084#ifdef EBCDIC
8085/* Translate character to its CTRL- value */
8086char CtrlTable[] =
8087{
8088/* 00 - 5E */
8089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8094 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8095/* ^ */ 0x1E,
8096/* - */ 0x1F,
8097/* 61 - 6C */
8098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8099/* _ */ 0x1F,
8100/* 6E - 80 */
8101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8102/* a */ 0x01,
8103/* b */ 0x02,
8104/* c */ 0x03,
8105/* d */ 0x37,
8106/* e */ 0x2D,
8107/* f */ 0x2E,
8108/* g */ 0x2F,
8109/* h */ 0x16,
8110/* i */ 0x05,
8111/* 8A - 90 */
8112 0, 0, 0, 0, 0, 0, 0,
8113/* j */ 0x15,
8114/* k */ 0x0B,
8115/* l */ 0x0C,
8116/* m */ 0x0D,
8117/* n */ 0x0E,
8118/* o */ 0x0F,
8119/* p */ 0x10,
8120/* q */ 0x11,
8121/* r */ 0x12,
8122/* 9A - A1 */
8123 0, 0, 0, 0, 0, 0, 0, 0,
8124/* s */ 0x13,
8125/* t */ 0x3C,
8126/* u */ 0x3D,
8127/* v */ 0x32,
8128/* w */ 0x26,
8129/* x */ 0x18,
8130/* y */ 0x19,
8131/* z */ 0x3F,
8132/* AA - AC */
8133 0, 0, 0,
8134/* [ */ 0x27,
8135/* AE - BC */
8136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8137/* ] */ 0x1D,
8138/* BE - C0 */ 0, 0, 0,
8139/* A */ 0x01,
8140/* B */ 0x02,
8141/* C */ 0x03,
8142/* D */ 0x37,
8143/* E */ 0x2D,
8144/* F */ 0x2E,
8145/* G */ 0x2F,
8146/* H */ 0x16,
8147/* I */ 0x05,
8148/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8149/* J */ 0x15,
8150/* K */ 0x0B,
8151/* L */ 0x0C,
8152/* M */ 0x0D,
8153/* N */ 0x0E,
8154/* O */ 0x0F,
8155/* P */ 0x10,
8156/* Q */ 0x11,
8157/* R */ 0x12,
8158/* DA - DF */ 0, 0, 0, 0, 0, 0,
8159/* \ */ 0x1C,
8160/* E1 */ 0,
8161/* S */ 0x13,
8162/* T */ 0x3C,
8163/* U */ 0x3D,
8164/* V */ 0x32,
8165/* W */ 0x26,
8166/* X */ 0x18,
8167/* Y */ 0x19,
8168/* Z */ 0x3F,
8169/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8171};
8172
8173char MetaCharTable[]=
8174{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8175 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8176 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8177 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8178 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8179};
8180
8181
8182/* TODO: Use characters NOT numbers!!! */
8183char CtrlCharTable[]=
8184{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8185 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8186 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8187 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8188 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8189};
8190
8191
8192#endif