blob: c56de66db2453115b6bf92241551bc4102106f89 [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 Moolenaarfe86f2d2008-11-28 20:29:07 +0000368/*
369 * Write s[len] to the screen.
370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100372mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000374 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100376 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377}
378
379/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000380 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * Get a characters from the keyboard.
382 * Return the number of characters that are available.
383 * If wtime == 0 do not wait for characters.
384 * If wtime == n wait a short time for characters.
385 * If wtime == -1 wait forever for characters.
386 */
387 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100388mch_inchar(
389 char_u *buf,
390 int maxlen,
391 long wtime, /* don't use "time", MIPS cannot handle it */
392 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393{
394 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200395 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200396 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200397 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200398 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100399#ifdef ELAPSED_FUNC
400 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200401
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100402 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200405 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200406 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200408 /* Check if window changed size while we were busy, perhaps the ":set
409 * columns=99" command was used. */
410 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200412
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200413#ifdef MESSAGE_QUEUE
414 parse_queued_messages();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200415 /* If input was put directly in typeahead buffer bail out here. */
416 if (typebuf_changed(tb_change_cnt))
417 return 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200418#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200419 if (wtime < 0 && did_start_blocking)
420 /* blocking and already waited for p_ut */
421 wait_time = -1;
422 else
423 {
424 if (wtime >= 0)
425 wait_time = wtime;
426 else
427 /* going to block after p_ut */
428 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100429#ifdef ELAPSED_FUNC
430 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200431#endif
432 wait_time -= elapsed_time;
433 if (wait_time < 0)
434 {
435 if (wtime >= 0)
436 /* no character available within "wtime" */
437 return 0;
438
439 if (wtime < 0)
440 {
441 /* no character available within 'updatetime' */
442 did_start_blocking = TRUE;
443#ifdef FEAT_AUTOCMD
444 if (trigger_cursorhold() && maxlen >= 3
445 && !typebuf_changed(tb_change_cnt))
446 {
447 buf[0] = K_SPECIAL;
448 buf[1] = KS_EXTRA;
449 buf[2] = (int)KE_CURSORHOLD;
450 return 3;
451 }
452#endif
453 /*
454 * If there is no character available within 'updatetime'
455 * seconds flush all the swap files to disk.
456 * Also done when interrupted by SIGWINCH.
457 */
458 before_blocking();
459 continue;
460 }
461 }
462 }
463
464#ifdef FEAT_JOB_CHANNEL
465 /* Checking if a job ended requires polling. Do this every 100 msec. */
466 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
467 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100468 /* If there is readahead then parse_queued_messages() timed out and we
469 * should call it again soon. */
470 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
471 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200472#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100473#ifdef FEAT_BEVAL
474 if (p_beval && wait_time > 100L)
475 /* The 'balloonexpr' may indirectly invoke a callback while waiting
476 * for a character, need to check often. */
477 wait_time = 100L;
478#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200479
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200481 * We want to be interrupted by the winch signal
482 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200484 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200485 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200486 /* If input was put directly in typeahead buffer bail out here. */
487 if (typebuf_changed(tb_change_cnt))
488 return 0;
489
490 /*
491 * For some terminals we only get one character at a time.
492 * We want the get all available characters, so we could keep on
493 * trying until none is available
494 * For some other terminals this is quite slow, that's why we don't
495 * do it.
496 */
497 len = read_from_input_buf(buf, (long)maxlen);
498 if (len > 0)
499 return len;
500 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200503 /* no character available */
504#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
505 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100506 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200509 if (do_resize /* interrupted by SIGWINCH signal */
510#ifdef FEAT_CLIENTSERVER
511 || server_waiting()
512#endif
513#ifdef MESSAGE_QUEUE
514 || interrupted
515#endif
516 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100517 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200518 continue;
519
520 /* no character available or interrupted */
521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200523 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524}
525
526 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100527handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529 do_resize = FALSE;
530 shell_resized();
531}
532
533/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200534 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100537mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200539 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200542#if defined(FEAT_TERMINAL) || defined(PROTO)
543/*
544 * Check for any pending input or messages.
545 */
546 int
547mch_check_messages(void)
548{
549 return WaitForChar(0L, NULL, TRUE);
550}
551#endif
552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
554# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000555# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556# endif
557# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
558# include <sys/sysctl.h>
559# endif
560# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
561# include <sys/sysinfo.h>
562# endif
563
564/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000565 * Return total amount of memory available in Kbyte.
566 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100569mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000572 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200574# ifdef HAVE_SYSCTL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 int mib[2], physmem;
576 size_t len;
577
578 /* BSD way of getting the amount of RAM available. */
579 mib[0] = CTL_HW;
580 mib[1] = HW_USERMEM;
581 len = sizeof(physmem);
582 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
583 mem = (long_u)physmem;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200586# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (mem == 0)
588 {
589 struct sysinfo sinfo;
590
591 /* Linux way of getting amount of RAM available */
592 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000593 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200594# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000595 /* avoid overflow as much as possible */
596 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
597 {
598 sinfo.mem_unit = sinfo.mem_unit >> 1;
599 --shiftright;
600 }
601 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200602# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200604# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200609# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 if (mem == 0)
611 {
612 long pagesize, pagecount;
613
614 /* Solaris way of getting amount of RAM available */
615 pagesize = sysconf(_SC_PAGESIZE);
616 pagecount = sysconf(_SC_PHYS_PAGES);
617 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000618 {
619 /* avoid overflow as much as possible */
620 while (shiftright > 0 && (pagesize & 1) == 0)
621 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000622 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000623 --shiftright;
624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
630 /* Return the minimum of the physical memory and the user limit, because
631 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200632# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {
634 struct rlimit rlp;
635
636 if (getrlimit(RLIMIT_DATA, &rlp) == 0
637 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200638# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200640# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000641 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000643 {
644 mem = (long_u)rlp.rlim_cur;
645 shiftright = 10;
646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000651 return mem >> shiftright;
652 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654#endif
655
656 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100657mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000660#ifdef FEAT_MZSCHEME
661 long total = msec; /* remember original value */
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664 if (ignoreinput)
665 {
666 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000667 * here. But we don't want QUIT to kill us (CTRL-\ used in a
668 * shell may produce SIGQUIT). */
669 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 old_tmode = curr_tmode;
671 if (curr_tmode == TMODE_RAW)
672 settmode(TMODE_SLEEP);
673
674 /*
675 * Everybody sleeps in a different way...
676 * Prefer nanosleep(), some versions of usleep() can only sleep up to
677 * one second.
678 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000679#ifdef FEAT_MZSCHEME
680 do
681 {
682 /* if total is large enough, wait by portions in p_mzq */
683 if (total > p_mzq)
684 msec = p_mzq;
685 else
686 msec = total;
687 total -= msec;
688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef HAVE_NANOSLEEP
690 {
691 struct timespec ts;
692
693 ts.tv_sec = msec / 1000;
694 ts.tv_nsec = (msec % 1000) * 1000000;
695 (void)nanosleep(&ts, NULL);
696 }
697#else
698# ifdef HAVE_USLEEP
699 while (msec >= 1000)
700 {
701 usleep((unsigned int)(999 * 1000));
702 msec -= 999;
703 }
704 usleep((unsigned int)(msec * 1000));
705# else
706# ifndef HAVE_SELECT
707 poll(NULL, 0, (int)msec);
708# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
710 struct timeval tv;
711
712 tv.tv_sec = msec / 1000;
713 tv.tv_usec = (msec % 1000) * 1000;
714 /*
715 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
716 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
717 */
718 select(0, NULL, NULL, NULL, &tv);
719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720# endif /* HAVE_SELECT */
721# endif /* HAVE_NANOSLEEP */
722#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000723#ifdef FEAT_MZSCHEME
724 }
725 while (total > 0);
726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000729 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 }
731 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200732 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733}
734
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000735#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
737# define HAVE_CHECK_STACK_GROWTH
738/*
739 * Support for checking for an almost-out-of-stack-space situation.
740 */
741
742/*
743 * Return a pointer to an item on the stack. Used to find out if the stack
744 * grows up or down.
745 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100746static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747static int stack_grows_downwards;
748
749/*
750 * Find out if the stack grows upwards or downwards.
751 * "p" points to a variable on the stack of the caller.
752 */
753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100754check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
756 int i;
757
758 stack_grows_downwards = (p > (char *)&i);
759}
760#endif
761
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000762#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763static char *stack_limit = NULL;
764
765#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
766# include <pthread.h>
767# include <pthread_np.h>
768#endif
769
770/*
771 * Find out until how var the stack can grow without getting into trouble.
772 * Called when starting up and when switching to the signal stack in
773 * deathtrap().
774 */
775 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100776get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 struct rlimit rlp;
779 int i;
780 long lim;
781
782 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
783 * limit doesn't fit in a long (rlim_cur might be "long long"). */
784 if (getrlimit(RLIMIT_STACK, &rlp) == 0
785 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
786# ifdef RLIM_INFINITY
787 && rlp.rlim_cur != RLIM_INFINITY
788# endif
789 )
790 {
791 lim = (long)rlp.rlim_cur;
792#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
793 {
794 pthread_attr_t attr;
795 size_t size;
796
797 /* On FreeBSD the initial thread always has a fixed stack size, no
798 * matter what the limits are set to. Normally it's 1 Mbyte. */
799 pthread_attr_init(&attr);
800 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
801 {
802 pthread_attr_getstacksize(&attr, &size);
803 if (lim > (long)size)
804 lim = (long)size;
805 }
806 pthread_attr_destroy(&attr);
807 }
808#endif
809 if (stack_grows_downwards)
810 {
811 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
812 if (stack_limit >= (char *)&i)
813 /* overflow, set to 1/16 of current stack position */
814 stack_limit = (char *)((long)&i / 16L);
815 }
816 else
817 {
818 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
819 if (stack_limit <= (char *)&i)
820 stack_limit = NULL; /* overflow */
821 }
822 }
823}
824
825/*
826 * Return FAIL when running out of stack space.
827 * "p" must point to any variable local to the caller that's on the stack.
828 */
829 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100830mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
832 if (stack_limit != NULL)
833 {
834 if (stack_grows_downwards)
835 {
836 if (p < stack_limit)
837 return FAIL;
838 }
839 else if (p > stack_limit)
840 return FAIL;
841 }
842 return OK;
843}
844#endif
845
846#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
847/*
848 * Support for using the signal stack.
849 * This helps when we run out of stack space, which causes a SIGSEGV. The
850 * signal handler then must run on another stack, since the normal stack is
851 * completely full.
852 */
853
Bram Moolenaar39766a72013-11-03 00:41:00 +0100854#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100855# include <AvailabilityMacros.h>
856#endif
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#ifndef SIGSTKSZ
859# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
860#endif
861
862# ifdef HAVE_SIGALTSTACK
863static stack_t sigstk; /* for sigaltstack() */
864# else
865static struct sigstack sigstk; /* for sigstack() */
866# endif
867
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100868static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869static char *signal_stack;
870
871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100872init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 if (signal_stack != NULL)
875 {
876# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000877# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
878 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
880 * "struct sigaltstack" needs to be declared. */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100881 extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882# endif
883
884# ifdef HAVE_SS_BASE
885 sigstk.ss_base = signal_stack;
886# else
887 sigstk.ss_sp = signal_stack;
888# endif
889 sigstk.ss_size = SIGSTKSZ;
890 sigstk.ss_flags = 0;
891 (void)sigaltstack(&sigstk, NULL);
892# else
893 sigstk.ss_sp = signal_stack;
894 if (stack_grows_downwards)
895 sigstk.ss_sp += SIGSTKSZ - 1;
896 sigstk.ss_onstack = 0;
897 (void)sigstack(&sigstk, NULL);
898# endif
899 }
900}
901#endif
902
903/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000904 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * will barf when the second argument to signal() is ``wrong''.
906 * Let me try it with a few tricky defines from my own osdef.h (jw).
907 */
908#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 static RETSIGTYPE
910sig_winch SIGDEFARG(sigarg)
911{
912 /* this is not required on all systems, but it doesn't hurt anybody */
913 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
914 do_resize = TRUE;
915 SIGRETURN;
916}
917#endif
918
919#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 static RETSIGTYPE
921catch_sigint SIGDEFARG(sigarg)
922{
923 /* this is not required on all systems, but it doesn't hurt anybody */
924 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
925 got_int = TRUE;
926 SIGRETURN;
927}
928#endif
929
930#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 static RETSIGTYPE
932catch_sigpwr SIGDEFARG(sigarg)
933{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000934 /* this is not required on all systems, but it doesn't hurt anybody */
935 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 /*
937 * I'm not sure we get the SIGPWR signal when the system is really going
938 * down or when the batteries are almost empty. Just preserve the swap
939 * files and don't exit, that can't do any harm.
940 */
941 ml_sync_all(FALSE, FALSE);
942 SIGRETURN;
943}
944#endif
945
946#ifdef SET_SIG_ALARM
947/*
948 * signal function for alarm().
949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 static RETSIGTYPE
951sig_alarm SIGDEFARG(sigarg)
952{
953 /* doesn't do anything, just to break a system call */
954 sig_alarm_called = TRUE;
955 SIGRETURN;
956}
957#endif
958
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000959#if (defined(HAVE_SETJMP_H) \
960 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
961 || defined(FEAT_LIBCALL))) \
962 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963/*
964 * A simplistic version of setjmp() that only allows one level of using.
965 * Don't call twice before calling mch_endjmp()!.
966 * Usage:
967 * mch_startjmp();
968 * if (SETJMP(lc_jump_env) != 0)
969 * {
970 * mch_didjmp();
971 * EMSG("crash!");
972 * }
973 * else
974 * {
975 * do_the_work;
976 * mch_endjmp();
977 * }
978 * Note: Can't move SETJMP() here, because a function calling setjmp() must
979 * not return before the saved environment is used.
980 * Returns OK for normal return, FAIL when the protected code caused a
981 * problem and LONGJMP() was used.
982 */
983 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100984mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986#ifdef SIGHASARG
987 lc_signal = 0;
988#endif
989 lc_active = TRUE;
990}
991
992 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100993mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995 lc_active = FALSE;
996}
997
998 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100999mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1002 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1003 * otherwise catching the signal only works once. */
1004 init_signal_stack();
1005# endif
1006}
1007#endif
1008
1009/*
1010 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001011 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001013 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1014 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 */
1016 static RETSIGTYPE
1017deathtrap SIGDEFARG(sigarg)
1018{
1019 static int entered = 0; /* count the number of times we got here.
1020 Note: when memory has been corrupted
1021 this may get an arbitrary value! */
1022#ifdef SIGHASARG
1023 int i;
1024#endif
1025
1026#if defined(HAVE_SETJMP_H)
1027 /*
1028 * Catch a crash in protected code.
1029 * Restores the environment saved in lc_jump_env, which looks like
1030 * SETJMP() returns 1.
1031 */
1032 if (lc_active)
1033 {
1034# if defined(SIGHASARG)
1035 lc_signal = sigarg;
1036# endif
1037 lc_active = FALSE; /* don't jump again */
1038 LONGJMP(lc_jump_env, 1);
1039 /* NOTREACHED */
1040 }
1041#endif
1042
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001043#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001044# ifdef SIGQUIT
1045 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1046 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1047 * pressing CTRL-\, but we don't want Vim to exit then. */
1048 if (in_mch_delay && sigarg == SIGQUIT)
1049 SIGRETURN;
1050# endif
1051
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001052 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1053 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1054 * free(). Calling free() again may then cause a crash. */
1055 if (entered == 0
1056 && (0
1057# ifdef SIGHUP
1058 || sigarg == SIGHUP
1059# endif
1060# ifdef SIGQUIT
1061 || sigarg == SIGQUIT
1062# endif
1063# ifdef SIGTERM
1064 || sigarg == SIGTERM
1065# endif
1066# ifdef SIGPWR
1067 || sigarg == SIGPWR
1068# endif
1069# ifdef SIGUSR1
1070 || sigarg == SIGUSR1
1071# endif
1072# ifdef SIGUSR2
1073 || sigarg == SIGUSR2
1074# endif
1075 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001076 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001077 SIGRETURN;
1078#endif
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 /* Remember how often we have been called. */
1081 ++entered;
1082
Bram Moolenaare429e702016-06-10 19:49:14 +02001083#ifdef FEAT_AUTOCMD
1084 /* Executing autocommands is likely to use more stack space than we have
1085 * available in the signal stack. */
1086 block_autocmds();
1087#endif
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef FEAT_EVAL
1090 /* Set the v:dying variable. */
1091 set_vim_var_nr(VV_DYING, (long)entered);
1092#endif
1093
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001094#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 /* Since we are now using the signal stack, need to reset the stack
1096 * limit. Otherwise using a regexp will fail. */
1097 get_stack_limit();
1098#endif
1099
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001100#if 0
1101 /* This is for opening gdb the moment Vim crashes.
1102 * You need to manually adjust the file name and Vim executable name.
1103 * Suggested by SungHyun Nam. */
1104 {
1105# define VI_GDB_FILE "/tmp/vimgdb"
1106# define VIM_NAME "/usr/bin/vim"
1107 FILE *fp = fopen(VI_GDB_FILE, "w");
1108 if (fp)
1109 {
1110 fprintf(fp,
1111 "file %s\n"
1112 "attach %d\n"
1113 "set height 1000\n"
1114 "bt full\n"
1115 , VIM_NAME, getpid());
1116 fclose(fp);
1117 system("xterm -e gdb -x "VI_GDB_FILE);
1118 unlink(VI_GDB_FILE);
1119 }
1120 }
1121#endif
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#ifdef SIGHASARG
1124 /* try to find the name of this signal */
1125 for (i = 0; signal_info[i].sig != -1; i++)
1126 if (sigarg == signal_info[i].sig)
1127 break;
1128 deadly_signal = sigarg;
1129#endif
1130
1131 full_screen = FALSE; /* don't write message to the GUI, it might be
1132 * part of the problem... */
1133 /*
1134 * If something goes wrong after entering here, we may get here again.
1135 * When this happens, give a message and try to exit nicely (resetting the
1136 * terminal mode, etc.)
1137 * When this happens twice, just exit, don't even try to give a message,
1138 * stack may be corrupt or something weird.
1139 * When this still happens again (or memory was corrupted in such a way
1140 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1141 */
1142 if (entered >= 3)
1143 {
1144 reset_signals(); /* don't catch any signals anymore */
1145 may_core_dump();
1146 if (entered >= 4)
1147 _exit(8);
1148 exit(7);
1149 }
1150 if (entered == 2)
1151 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001152 /* No translation, it may call malloc(). */
1153 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 out_flush();
1155 getout(1);
1156 }
1157
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001158 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001160 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 signal_info[i].name);
1162#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001163 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001165
1166 /* Preserve files and exit. This sets the really_exiting flag to prevent
1167 * calling free(). */
1168 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
Bram Moolenaare429e702016-06-10 19:49:14 +02001170 /* NOTREACHED */
1171
Bram Moolenaar009b2592004-10-24 19:18:58 +00001172#ifdef NBDEBUG
1173 reset_signals();
1174 may_core_dump();
1175 abort();
1176#endif
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 SIGRETURN;
1179}
1180
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001181#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * On Solaris with multi-threading, suspending might not work immediately.
1184 * Catch the SIGCONT signal, which will be used as an indication whether the
1185 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001186 *
1187 * On Linux, signal is not always handled immediately either.
1188 * See https://bugs.launchpad.net/bugs/291373
1189 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001190 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001192static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001193static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195/*
1196 * signal handler for SIGCONT
1197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 static RETSIGTYPE
1199sigcont_handler SIGDEFARG(sigarg)
1200{
1201 sigcont_received = TRUE;
1202 SIGRETURN;
1203}
1204#endif
1205
Bram Moolenaar62b42182010-09-21 22:09:37 +02001206# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001207static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001208# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001209static void save_clipboard(void);
1210static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001211
1212static void *clip_star_save = NULL;
1213static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001214# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001215
1216/*
1217 * Called when Vim is going to sleep or execute a shell command.
1218 * We can't respond to requests for the X selections. Lose them, otherwise
1219 * other applications will hang. But first copy the text to cut buffer 0.
1220 */
1221 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001222loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001223{
1224 if (clip_star.owned || clip_plus.owned)
1225 {
1226 x11_export_final_selection();
1227 if (clip_star.owned)
1228 clip_lose_selection(&clip_star);
1229 if (clip_plus.owned)
1230 clip_lose_selection(&clip_plus);
1231 if (x11_display != NULL)
1232 XFlush(x11_display);
1233 }
1234}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001235
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001236# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001237/*
1238 * Save clipboard text to restore later.
1239 */
1240 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001241save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001242{
1243 if (clip_star.owned)
1244 clip_star_save = get_register('*', TRUE);
1245 if (clip_plus.owned)
1246 clip_plus_save = get_register('+', TRUE);
1247}
1248
1249/*
1250 * Restore clipboard text if no one own the X selection.
1251 */
1252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001253restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001254{
1255 if (clip_star_save != NULL)
1256 {
1257 if (!clip_gen_owner_exists(&clip_star))
1258 put_register('*', clip_star_save);
1259 else
1260 free_register(clip_star_save);
1261 clip_star_save = NULL;
1262 }
1263 if (clip_plus_save != NULL)
1264 {
1265 if (!clip_gen_owner_exists(&clip_plus))
1266 put_register('+', clip_plus_save);
1267 else
1268 free_register(clip_plus_save);
1269 clip_plus_save = NULL;
1270 }
1271}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001272# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275/*
1276 * If the machine has job control, use it to suspend the program,
1277 * otherwise fake it by starting a new shell.
1278 */
1279 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001280mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 /* BeOS does have SIGTSTP, but it doesn't work. */
1283#if defined(SIGTSTP) && !defined(__BEOS__)
1284 out_flush(); /* needed to make cursor visible on some systems */
1285 settmode(TMODE_COOK);
1286 out_flush(); /* needed to disable mouse on some systems */
1287
1288# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001289 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290# endif
1291
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001292# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 sigcont_received = FALSE;
1294# endif
1295 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001296# if defined(_REENTRANT) && defined(SIGCONT)
1297 /*
1298 * Wait for the SIGCONT signal to be handled. It generally happens
1299 * immediately, but somehow not all the time. Do not call pause()
1300 * because there would be race condition which would hang Vim if
1301 * signal happened in between the test of sigcont_received and the
1302 * call to pause(). If signal is not yet received, call sleep(0)
1303 * to just yield CPU. Signal should then be received. If somehow
1304 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1305 * further if signal is not received after 1+2+3+4 ms (not expected
1306 * to happen).
1307 */
1308 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001309 long wait_time;
1310 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001311 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001312 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314# endif
1315
1316# ifdef FEAT_TITLE
1317 /*
1318 * Set oldtitle to NULL, so the current title is obtained again.
1319 */
1320 vim_free(oldtitle);
1321 oldtitle = NULL;
1322# endif
1323 settmode(TMODE_RAW);
1324 need_check_timestamps = TRUE;
1325 did_check_timestamps = FALSE;
1326#else
1327 suspend_shell();
1328#endif
1329}
1330
1331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001332mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 Columns = 80;
1335 Rows = 24;
1336
1337 out_flush();
1338 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001339
Bram Moolenaar56718732006-03-15 22:53:57 +00001340#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001341 mac_conv_init();
1342#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001343#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1344 win_clip_init();
1345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346}
1347
1348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001349set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351#if defined(SIGWINCH)
1352 /*
1353 * WINDOW CHANGE signal is handled with sig_winch().
1354 */
1355 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1356#endif
1357
1358 /*
1359 * We want the STOP signal to work, to make mch_suspend() work.
1360 * For "rvim" the STOP signal is ignored.
1361 */
1362#ifdef SIGTSTP
1363 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1364#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001365#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 signal(SIGCONT, sigcont_handler);
1367#endif
1368
1369 /*
1370 * We want to ignore breaking of PIPEs.
1371 */
1372#ifdef SIGPIPE
1373 signal(SIGPIPE, SIG_IGN);
1374#endif
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001377 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#endif
1379
1380 /*
1381 * Ignore alarm signals (Perl's alarm() generates it).
1382 */
1383#ifdef SIGALRM
1384 signal(SIGALRM, SIG_IGN);
1385#endif
1386
1387 /*
1388 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1389 * work will be lost.
1390 */
1391#ifdef SIGPWR
1392 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1393#endif
1394
1395 /*
1396 * Arrange for other signals to gracefully shutdown Vim.
1397 */
1398 catch_signals(deathtrap, SIG_ERR);
1399
1400#if defined(FEAT_GUI) && defined(SIGHUP)
1401 /*
1402 * When the GUI is running, ignore the hangup signal.
1403 */
1404 if (gui.in_use)
1405 signal(SIGHUP, SIG_IGN);
1406#endif
1407}
1408
Bram Moolenaardf177f62005-02-22 08:39:57 +00001409#if defined(SIGINT) || defined(PROTO)
1410/*
1411 * Catch CTRL-C (only works while in Cooked mode).
1412 */
1413 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001414catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001415{
1416 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1417}
1418#endif
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001421reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001424#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 /* SIGCONT isn't in the list, because its default action is ignore */
1426 signal(SIGCONT, SIG_DFL);
1427#endif
1428}
1429
1430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001431catch_signals(
1432 RETSIGTYPE (*func_deadly)(),
1433 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 int i;
1436
1437 for (i = 0; signal_info[i].sig != -1; i++)
1438 if (signal_info[i].deadly)
1439 {
1440#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1441 struct sigaction sa;
1442
1443 /* Setup to use the alternate stack for the signal function. */
1444 sa.sa_handler = func_deadly;
1445 sigemptyset(&sa.sa_mask);
1446# if defined(__linux__) && defined(_REENTRANT)
1447 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1448 * thread handling in combination with using the alternate stack:
1449 * pthread library functions try to use the stack pointer to
1450 * identify the current thread, causing a SEGV signal, which
1451 * recursively calls deathtrap() and hangs. */
1452 sa.sa_flags = 0;
1453# else
1454 sa.sa_flags = SA_ONSTACK;
1455# endif
1456 sigaction(signal_info[i].sig, &sa, NULL);
1457#else
1458# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1459 struct sigvec sv;
1460
1461 /* Setup to use the alternate stack for the signal function. */
1462 sv.sv_handler = func_deadly;
1463 sv.sv_mask = 0;
1464 sv.sv_flags = SV_ONSTACK;
1465 sigvec(signal_info[i].sig, &sv, NULL);
1466# else
1467 signal(signal_info[i].sig, func_deadly);
1468# endif
1469#endif
1470 }
1471 else if (func_other != SIG_ERR)
1472 signal(signal_info[i].sig, func_other);
1473}
1474
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001475#ifdef HAVE_SIGPROCMASK
1476 static void
1477block_signals(sigset_t *set)
1478{
1479 sigset_t newset;
1480 int i;
1481
1482 sigemptyset(&newset);
1483
1484 for (i = 0; signal_info[i].sig != -1; i++)
1485 sigaddset(&newset, signal_info[i].sig);
1486
1487# if defined(_REENTRANT) && defined(SIGCONT)
1488 /* SIGCONT isn't in the list, because its default action is ignore */
1489 sigaddset(&newset, SIGCONT);
1490# endif
1491
1492 sigprocmask(SIG_BLOCK, &newset, set);
1493}
1494
1495 static void
1496unblock_signals(sigset_t *set)
1497{
1498 sigprocmask(SIG_SETMASK, set, NULL);
1499}
1500#endif
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001503 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001504 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1505 * return TRUE
1506 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1507 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001508 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001509 * Returns TRUE when Vim should exit.
1510 */
1511 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001512vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001513{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001514 static int got_signal = 0;
1515 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001516
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001517 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001518 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001519 case SIGNAL_BLOCK: blocked = TRUE;
1520 break;
1521
1522 case SIGNAL_UNBLOCK: blocked = FALSE;
1523 if (got_signal != 0)
1524 {
1525 kill(getpid(), got_signal);
1526 got_signal = 0;
1527 }
1528 break;
1529
1530 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001531 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001532 got_signal = sig;
1533#ifdef SIGPWR
1534 if (sig != SIGPWR)
1535#endif
1536 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001537 break;
1538 }
1539 return FALSE;
1540}
1541
1542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * Check_win checks whether we have an interactive stdout.
1544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001546mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (isatty(1))
1549 return OK;
1550 return FAIL;
1551}
1552
1553/*
1554 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1555 */
1556 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001557mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 if (isatty(read_cmd_fd))
1560 return TRUE;
1561 return FALSE;
1562}
1563
1564#ifdef FEAT_X11
1565
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001566# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569/*
1570 * Give a message about the elapsed time for opening the X window.
1571 */
1572 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001573xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001575 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576}
1577# endif
1578#endif
1579
1580#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1581/*
1582 * A few functions shared by X11 title and clipboard code.
1583 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001584static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1585static int x_error_check(Display *dpy, XErrorEvent *error_event);
1586static int x_connect_to_server(void);
1587static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589static int got_x_error = FALSE;
1590
1591/*
1592 * X Error handler, otherwise X just exits! (very rude) -- webb
1593 */
1594 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001595x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001597 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 STRCAT(IObuff, _("\nVim: Got X error\n"));
1599
1600 /* We cannot print a message and continue, because no X calls are allowed
1601 * here (causes my system to hang). Silently continuing might be an
1602 * alternative... */
1603 preserve_exit(); /* preserve files and exit */
1604
1605 return 0; /* NOTREACHED */
1606}
1607
1608/*
1609 * Another X Error handler, just used to check for errors.
1610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001612x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 got_x_error = TRUE;
1615 return 0;
1616}
1617
1618#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1619# if defined(HAVE_SETJMP_H)
1620/*
1621 * An X IO Error handler, used to catch error while opening the display.
1622 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001623static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001626x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
1628 /* This function should not return, it causes exit(). Longjump instead. */
1629 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001630# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001631 return 0; /* avoid the compiler complains about missing return value */
1632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633}
1634# endif
1635
1636/*
1637 * An X IO Error handler, used to catch terminal errors.
1638 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001639static int x_IOerror_handler(Display *dpy);
1640static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001641static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001644x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001647 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 x11_window = 0;
1649 x11_display = NULL;
1650 xterm_Shell = (Widget)0;
1651
1652 /* This function should not return, it causes exit(). Longjump instead. */
1653 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001654# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001655 return 0; /* avoid the compiler complains about missing return value */
1656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001658
1659/*
1660 * If the X11 connection was lost try to restore it.
1661 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001662 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001663 */
1664 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001665may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001666{
1667 if (xterm_dpy_was_reset)
1668 {
1669 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001670
1671# ifndef LESSTIF_VERSION
1672 /* This has been reported to avoid Vim getting stuck. */
1673 if (app_context != (XtAppContext)NULL)
1674 {
1675 XtDestroyApplicationContext(app_context);
1676 app_context = (XtAppContext)NULL;
1677 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1678 }
1679# endif
1680
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001681 setup_term_clip();
1682 get_x11_title(FALSE);
1683 }
1684}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686
1687/*
1688 * Return TRUE when connection to the X server is desired.
1689 */
1690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001691x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#if defined(FEAT_CLIENTSERVER)
1694 if (x_force_connect)
1695 return TRUE;
1696#endif
1697 if (x_no_connect)
1698 return FALSE;
1699
1700 /* Check for a match with "exclude:" from 'clipboard'. */
1701 if (clip_exclude_prog != NULL)
1702 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001703 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 return FALSE;
1705 }
1706 return TRUE;
1707}
1708
1709/*
1710 * Test if "dpy" and x11_window are valid by getting the window title.
1711 * I don't actually want it yet, so there may be a simpler call to use, but
1712 * this will cause the error handler x_error_check() to be called if anything
1713 * is wrong, such as the window pointer being invalid (as can happen when the
1714 * user changes his DISPLAY, but not his WINDOWID) -- webb
1715 */
1716 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001717test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 int (*old_handler)();
1720 XTextProperty text_prop;
1721
1722 old_handler = XSetErrorHandler(x_error_check);
1723 got_x_error = FALSE;
1724 if (XGetWMName(dpy, x11_window, &text_prop))
1725 XFree((void *)text_prop.value);
1726 XSync(dpy, False);
1727 (void)XSetErrorHandler(old_handler);
1728
1729 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001730 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 return (got_x_error ? FAIL : OK);
1733}
1734#endif
1735
1736#ifdef FEAT_TITLE
1737
1738#ifdef FEAT_X11
1739
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001740static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742/*
1743 * try to get x11 window and display
1744 *
1745 * return FAIL for failure, OK otherwise
1746 */
1747 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001748get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 char *winid;
1751 static int result = -1;
1752#define XD_NONE 0 /* x11_display not set here */
1753#define XD_HERE 1 /* x11_display opened here */
1754#define XD_GUI 2 /* x11_display used from gui.dpy */
1755#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1756 static int x11_display_from = XD_NONE;
1757 static int did_set_error_handler = FALSE;
1758
1759 if (!did_set_error_handler)
1760 {
1761 /* X just exits if it finds an error otherwise! */
1762 (void)XSetErrorHandler(x_error_handler);
1763 did_set_error_handler = TRUE;
1764 }
1765
Bram Moolenaar9372a112005-12-06 19:59:18 +00001766#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (gui.in_use)
1768 {
1769 /*
1770 * If the X11 display was opened here before, for the window where Vim
1771 * was started, close that one now to avoid a memory leak.
1772 */
1773 if (x11_display_from == XD_HERE && x11_display != NULL)
1774 {
1775 XCloseDisplay(x11_display);
1776 x11_display_from = XD_NONE;
1777 }
1778 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1779 {
1780 x11_display_from = XD_GUI;
1781 return OK;
1782 }
1783 x11_display = NULL;
1784 return FAIL;
1785 }
1786 else if (x11_display_from == XD_GUI)
1787 {
1788 /* GUI must have stopped somehow, clear x11_display */
1789 x11_window = 0;
1790 x11_display = NULL;
1791 x11_display_from = XD_NONE;
1792 }
1793#endif
1794
1795 /* When started with the "-X" argument, don't try connecting. */
1796 if (!x_connect_to_server())
1797 return FAIL;
1798
1799 /*
1800 * If WINDOWID not set, should try another method to find out
1801 * what the current window number is. The only code I know for
1802 * this is very complicated.
1803 * We assume that zero is invalid for WINDOWID.
1804 */
1805 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1806 x11_window = (Window)atol(winid);
1807
1808#ifdef FEAT_XCLIPBOARD
1809 if (xterm_dpy != NULL && x11_window != 0)
1810 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001811 /* We may have checked it already, but Gnome terminal can move us to
1812 * another window, so we need to check every time. */
1813 if (x11_display_from != XD_XTERM)
1814 {
1815 /*
1816 * If the X11 display was opened here before, for the window where
1817 * Vim was started, close that one now to avoid a memory leak.
1818 */
1819 if (x11_display_from == XD_HERE && x11_display != NULL)
1820 XCloseDisplay(x11_display);
1821 x11_display = xterm_dpy;
1822 x11_display_from = XD_XTERM;
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (test_x11_window(x11_display) == FAIL)
1825 {
1826 /* probably bad $WINDOWID */
1827 x11_window = 0;
1828 x11_display = NULL;
1829 x11_display_from = XD_NONE;
1830 return FAIL;
1831 }
1832 return OK;
1833 }
1834#endif
1835
1836 if (x11_window == 0 || x11_display == NULL)
1837 result = -1;
1838
1839 if (result != -1) /* Have already been here and set this */
1840 return result; /* Don't do all these X calls again */
1841
1842 if (x11_window != 0 && x11_display == NULL)
1843 {
1844#ifdef SET_SIG_ALARM
1845 RETSIGTYPE (*sig_save)();
1846#endif
1847#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1848 struct timeval start_tv;
1849
1850 if (p_verbose > 0)
1851 gettimeofday(&start_tv, NULL);
1852#endif
1853
1854#ifdef SET_SIG_ALARM
1855 /*
1856 * Opening the Display may hang if the DISPLAY setting is wrong, or
1857 * the network connection is bad. Set an alarm timer to get out.
1858 */
1859 sig_alarm_called = FALSE;
1860 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1861 (RETSIGTYPE (*)())sig_alarm);
1862 alarm(2);
1863#endif
1864 x11_display = XOpenDisplay(NULL);
1865
1866#ifdef SET_SIG_ALARM
1867 alarm(0);
1868 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1869 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001870 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#endif
1872 if (x11_display != NULL)
1873 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001874# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001876 {
1877 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001878 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001879 verbose_leave();
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881# endif
1882 if (test_x11_window(x11_display) == FAIL)
1883 {
1884 /* Maybe window id is bad */
1885 x11_window = 0;
1886 XCloseDisplay(x11_display);
1887 x11_display = NULL;
1888 }
1889 else
1890 x11_display_from = XD_HERE;
1891 }
1892 }
1893 if (x11_window == 0 || x11_display == NULL)
1894 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001895
1896# ifdef FEAT_EVAL
1897 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1898# endif
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 return (result = OK);
1901}
1902
1903/*
1904 * Determine original x11 Window Title
1905 */
1906 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001907get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001909 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910}
1911
1912/*
1913 * Determine original x11 Window icon
1914 */
1915 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001916get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 int retval = FALSE;
1919
1920 retval = get_x11_thing(FALSE, test_only);
1921
1922 /* could not get old icon, use terminal name */
1923 if (oldicon == NULL && !test_only)
1924 {
1925 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001926 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001928 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930
1931 return retval;
1932}
1933
1934 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001935get_x11_thing(
1936 int get_title, /* get title string */
1937 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938{
1939 XTextProperty text_prop;
1940 int retval = FALSE;
1941 Status status;
1942
1943 if (get_x11_windis() == OK)
1944 {
1945 /* Get window/icon name if any */
1946 if (get_title)
1947 status = XGetWMName(x11_display, x11_window, &text_prop);
1948 else
1949 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1950
1951 /*
1952 * If terminal is xterm, then x11_window may be a child window of the
1953 * outer xterm window that actually contains the window/icon name, so
1954 * keep traversing up the tree until a window with a title/icon is
1955 * found.
1956 */
1957 /* Previously this was only done for xterm and alikes. I don't see a
1958 * reason why it would fail for other terminal emulators.
1959 * if (term_is_xterm) */
1960 {
1961 Window root;
1962 Window parent;
1963 Window win = x11_window;
1964 Window *children;
1965 unsigned int num_children;
1966
1967 while (!status || text_prop.value == NULL)
1968 {
1969 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1970 &num_children))
1971 break;
1972 if (children)
1973 XFree((void *)children);
1974 if (parent == root || parent == 0)
1975 break;
1976
1977 win = parent;
1978 if (get_title)
1979 status = XGetWMName(x11_display, win, &text_prop);
1980 else
1981 status = XGetWMIconName(x11_display, win, &text_prop);
1982 }
1983 }
1984 if (status && text_prop.value != NULL)
1985 {
1986 retval = TRUE;
1987 if (!test_only)
1988 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001989#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1990 if (text_prop.encoding == XA_STRING
1991# ifdef FEAT_MBYTE
1992 && !has_mbyte
1993# endif
1994 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996#endif
1997 if (get_title)
1998 oldtitle = vim_strsave((char_u *)text_prop.value);
1999 else
2000 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002001#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 else
2004 {
2005 char **cl;
2006 Status transform_status;
2007 int n = 0;
2008
2009 transform_status = XmbTextPropertyToTextList(x11_display,
2010 &text_prop,
2011 &cl, &n);
2012 if (transform_status >= Success && n > 0 && cl[0])
2013 {
2014 if (get_title)
2015 oldtitle = vim_strsave((char_u *) cl[0]);
2016 else
2017 oldicon = vim_strsave((char_u *) cl[0]);
2018 XFreeStringList(cl);
2019 }
2020 else
2021 {
2022 if (get_title)
2023 oldtitle = vim_strsave((char_u *)text_prop.value);
2024 else
2025 oldicon = vim_strsave((char_u *)text_prop.value);
2026 }
2027 }
2028#endif
2029 }
2030 XFree((void *)text_prop.value);
2031 }
2032 }
2033 return retval;
2034}
2035
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002036/* Xutf8 functions are not avaialble on older systems. Note that on some
2037 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2038 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2039 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002041# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042# define USE_UTF8_STRING
2043# endif
2044#endif
2045
2046/*
2047 * Set x11 Window Title
2048 *
2049 * get_x11_windis() must be called before this and have returned OK
2050 */
2051 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002052set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2055 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2056 * supported everywhere and STRING doesn't work for multi-byte titles.
2057 */
2058#ifdef USE_UTF8_STRING
2059 if (enc_utf8)
2060 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2061 NULL, NULL, 0, NULL, NULL, NULL);
2062 else
2063#endif
2064 {
2065#if XtSpecificationRelease >= 4
2066# ifdef FEAT_XFONTSET
2067 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2068 NULL, NULL, 0, NULL, NULL, NULL);
2069# else
2070 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002071 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002074 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 XSetWMProperties(x11_display, x11_window, &text_prop,
2076 NULL, NULL, 0, NULL, NULL, NULL);
2077# endif
2078#else
2079 XStoreName(x11_display, x11_window, (char *)title);
2080#endif
2081 }
2082 XFlush(x11_display);
2083}
2084
2085/*
2086 * Set x11 Window icon
2087 *
2088 * get_x11_windis() must be called before this and have returned OK
2089 */
2090 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002091set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
2093 /* See above for comments about using X*SetWMProperties(). */
2094#ifdef USE_UTF8_STRING
2095 if (enc_utf8)
2096 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2097 NULL, 0, NULL, NULL, NULL);
2098 else
2099#endif
2100 {
2101#if XtSpecificationRelease >= 4
2102# ifdef FEAT_XFONTSET
2103 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2104 NULL, 0, NULL, NULL, NULL);
2105# else
2106 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002107 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002109 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2111 NULL, 0, NULL, NULL, NULL);
2112# endif
2113#else
2114 XSetIconName(x11_display, x11_window, (char *)icon);
2115#endif
2116 }
2117 XFlush(x11_display);
2118}
2119
2120#else /* FEAT_X11 */
2121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002123get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 return FALSE;
2126}
2127
2128 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002129get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
2131 if (!test_only)
2132 {
2133 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002134 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002136 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 return FALSE;
2139}
2140
2141#endif /* FEAT_X11 */
2142
2143 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002144mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 return get_x11_title(TRUE);
2147}
2148
2149 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002150mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152 return get_x11_icon(TRUE);
2153}
2154
2155/*
2156 * Set the window title and icon.
2157 */
2158 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002159mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 int type = 0;
2162 static int recursive = 0;
2163
2164 if (T_NAME == NULL) /* no terminal name (yet) */
2165 return;
2166 if (title == NULL && icon == NULL) /* nothing to do */
2167 return;
2168
2169 /* When one of the X11 functions causes a deadly signal, we get here again
2170 * recursively. Avoid hanging then (something is probably locked). */
2171 if (recursive)
2172 return;
2173 ++recursive;
2174
2175 /*
2176 * if the window ID and the display is known, we may use X11 calls
2177 */
2178#ifdef FEAT_X11
2179 if (get_x11_windis() == OK)
2180 type = 1;
2181#else
2182# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2183 if (gui.in_use)
2184 type = 1;
2185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186#endif
2187
2188 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002189 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * than x11 calls, because the x11 calls don't always work
2191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if ((type || *T_TS != NUL) && title != NULL)
2193 {
2194 if (oldtitle == NULL
2195#ifdef FEAT_GUI
2196 && !gui.in_use
2197#endif
2198 ) /* first call but not in GUI, save title */
2199 (void)get_x11_title(FALSE);
2200
2201 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2202 term_settitle(title);
2203#ifdef FEAT_X11
2204 else
2205# ifdef FEAT_GUI_GTK
2206 if (!gui.in_use) /* don't do this if GTK+ is running */
2207# endif
2208 set_x11_title(title); /* x11 */
2209#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002210#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2212 else
2213 gui_mch_settitle(title, icon);
2214#endif
2215 did_set_title = TRUE;
2216 }
2217
2218 if ((type || *T_CIS != NUL) && icon != NULL)
2219 {
2220 if (oldicon == NULL
2221#ifdef FEAT_GUI
2222 && !gui.in_use
2223#endif
2224 ) /* first call, save icon */
2225 get_x11_icon(FALSE);
2226
2227 if (*T_CIS != NUL)
2228 {
2229 out_str(T_CIS); /* set icon start */
2230 out_str_nf(icon);
2231 out_str(T_CIE); /* set icon end */
2232 out_flush();
2233 }
2234#ifdef FEAT_X11
2235 else
2236# ifdef FEAT_GUI_GTK
2237 if (!gui.in_use) /* don't do this if GTK+ is running */
2238# endif
2239 set_x11_icon(icon); /* x11 */
2240#endif
2241 did_set_icon = TRUE;
2242 }
2243 --recursive;
2244}
2245
2246/*
2247 * Restore the window/icon title.
2248 * "which" is one of:
2249 * 1 only restore title
2250 * 2 only restore icon
2251 * 3 restore title and icon
2252 */
2253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002254mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 /* only restore the title or icon when it has been set */
2257 mch_settitle(((which & 1) && did_set_title) ?
2258 (oldtitle ? oldtitle : p_titleold) : NULL,
2259 ((which & 2) && did_set_icon) ? oldicon : NULL);
2260}
2261
2262#endif /* FEAT_TITLE */
2263
2264/*
2265 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002266 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
2268 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002269vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271 if (name == NULL)
2272 return FALSE;
2273 return (STRNICMP(name, "xterm", 5) == 0
2274 || STRNICMP(name, "nxterm", 6) == 0
2275 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002276 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 || STRNICMP(name, "rxvt", 4) == 0
2278 || STRCMP(name, "builtin_xterm") == 0);
2279}
2280
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002281#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2282/*
2283 * Return TRUE if "name" appears to be that of a terminal
2284 * known to support the xterm-style mouse protocol.
2285 * Relies on term_is_xterm having been set to its correct value.
2286 */
2287 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002288use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002289{
2290 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002291 && (term_is_xterm
2292 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002293 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002294 || STRICMP(name, "st") == 0
2295 || STRNICMP(name, "st-", 3) == 0
2296 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002297}
2298#endif
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2301/*
2302 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2303 * Return 1 for "xterm".
2304 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002305 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002306 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002309use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002311 if (ttym_flags == TTYM_SGR)
2312 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002313 if (ttym_flags == TTYM_URXVT)
2314 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (ttym_flags == TTYM_XTERM2)
2316 return 2;
2317 if (ttym_flags == TTYM_XTERM)
2318 return 1;
2319 return 0;
2320}
2321#endif
2322
2323 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002324vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 if (name == NULL)
2327 return FALSE;
2328 return (STRNICMP(name, "iris-ansi", 9) == 0
2329 || STRCMP(name, "builtin_iris-ansi") == 0);
2330}
2331
2332 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002333vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 if (name == NULL)
2336 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002337 /* catch VT100 - VT5xx */
2338 return ((STRNICMP(name, "vt", 2) == 0
2339 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 || STRCMP(name, "builtin_vt320") == 0);
2341}
2342
2343/*
2344 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2345 * This should include all windowed terminal emulators.
2346 */
2347 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002348vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
2350 if (name == NULL)
2351 return FALSE;
2352 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2353 return TRUE;
2354 return ( STRNICMP(name, "hpterm", 6) == 0
2355 || STRNICMP(name, "sun-cmd", 7) == 0
2356 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002357 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 || STRNICMP(name, "dtterm", 6) == 0);
2359}
2360
2361/*
2362 * Insert user name in s[len].
2363 * Return OK if a name found.
2364 */
2365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002366mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002369 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 return OK;
2371#else
2372 return mch_get_uname(getuid(), s, len);
2373#endif
2374}
2375
2376/*
2377 * Insert user name for "uid" in s[len].
2378 * Return OK if a name found.
2379 */
2380 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002381mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2384 struct passwd *pw;
2385
2386 if ((pw = getpwuid(uid)) != NULL
2387 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2388 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002389 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 return OK;
2391 }
2392#endif
2393 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2394 return FAIL; /* a number is not a name */
2395}
2396
2397/*
2398 * Insert host name is s[len].
2399 */
2400
2401#ifdef HAVE_SYS_UTSNAME_H
2402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002403mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
2405 struct utsname vutsname;
2406
2407 if (uname(&vutsname) < 0)
2408 *s = NUL;
2409 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002410 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411}
2412#else /* HAVE_SYS_UTSNAME_H */
2413
2414# ifdef HAVE_SYS_SYSTEMINFO_H
2415# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2416# endif
2417
2418 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002419mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421# ifdef VAXC
2422 vaxc$gethostname((char *)s, len);
2423# else
2424 gethostname((char *)s, len);
2425# endif
2426 s[len - 1] = NUL; /* make sure it's terminated */
2427}
2428#endif /* HAVE_SYS_UTSNAME_H */
2429
2430/*
2431 * return process ID
2432 */
2433 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002434mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 return (long)getpid();
2437}
2438
2439#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002440static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002443strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 extern int sys_nerr;
2446 extern char *sys_errlist[];
2447 static char er[20];
2448
2449 if (err > 0 && err < sys_nerr)
2450 return (sys_errlist[err]);
2451 sprintf(er, "Error %d", err);
2452 return er;
2453}
2454#endif
2455
2456/*
2457 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2458 * Return OK for success, FAIL for failure.
2459 */
2460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002461mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463#if defined(USE_GETCWD)
2464 if (getcwd((char *)buf, len) == NULL)
2465 {
2466 STRCPY(buf, strerror(errno));
2467 return FAIL;
2468 }
2469 return OK;
2470#else
2471 return (getwd((char *)buf) != NULL ? OK : FAIL);
2472#endif
2473}
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002476 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 *
2478 * return FAIL for failure, OK for success
2479 */
2480 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002481mch_FullName(
2482 char_u *fname,
2483 char_u *buf,
2484 int len,
2485 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002488#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int fd = -1;
2490 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 char_u olddir[MAXPATHL];
2493 char_u *p;
2494 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002495#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002496 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2497 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002498#endif
2499
Bram Moolenaar38323e42007-03-06 19:22:53 +00002500#ifdef VMS
2501 fname = vms_fixfilename(fname);
2502#endif
2503
Bram Moolenaara2442432007-04-26 14:26:37 +00002504#ifdef __CYGWIN__
2505 /*
2506 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2507 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002508# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002509 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2510 * a forward slash. */
2511 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2512 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002513# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002514 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002515# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002516 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002517#endif
2518
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002519 /* Expand it if forced or not an absolute path.
2520 * Do not do it for "/file", the result is always "/". */
2521 if ((force || !mch_isFullName(fname))
2522 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
2524 /*
2525 * If the file name has a path, change to that directory for a moment,
2526 * and then do the getwd() (and get back to where we were).
2527 * This will get the correct path name with "../" things.
2528 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002529 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002531#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /*
2533 * Use fchdir() if possible, it's said to be faster and more
2534 * reliable. But on SunOS 4 it might not work. Check this by
2535 * doing a fchdir() right now.
2536 */
2537 if (!dont_fchdir)
2538 {
2539 fd = open(".", O_RDONLY | O_EXTRA, 0);
2540 if (fd >= 0 && fchdir(fd) < 0)
2541 {
2542 close(fd);
2543 fd = -1;
2544 dont_fchdir = TRUE; /* don't try again */
2545 }
2546 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 /* Only change directory when we are sure we can return to where
2550 * we are now. After doing "su" chdir(".") might not work. */
2551 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002552#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 (mch_dirname(olddir, MAXPATHL) == FAIL
2556 || mch_chdir((char *)olddir) != 0))
2557 {
2558 p = NULL; /* can't get current dir: don't chdir */
2559 retval = FAIL;
2560 }
2561 else
2562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 /* The directory is copied into buf[], to be able to remove
2564 * the file name without changing it (could be a string in
2565 * read-only memory) */
2566 if (p - fname >= len)
2567 retval = FAIL;
2568 else
2569 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002570 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (mch_chdir((char *)buf))
2572 retval = FAIL;
2573 else
2574 fname = p + 1;
2575 *buf = NUL;
2576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578 }
2579 if (mch_dirname(buf, len) == FAIL)
2580 {
2581 retval = FAIL;
2582 *buf = NUL;
2583 }
2584 if (p != NULL)
2585 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002586#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (fd >= 0)
2588 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002589 if (p_verbose >= 5)
2590 {
2591 verbose_enter();
2592 MSG("fchdir() to previous dir");
2593 verbose_leave();
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 l = fchdir(fd);
2596 close(fd);
2597 }
2598 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 l = mch_chdir((char *)olddir);
2601 if (l != 0)
2602 EMSG(_(e_prev_dir));
2603 }
2604
2605 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002606 if (l >= len - 1)
2607 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002608#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002609 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002611 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002616 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return FAIL;
2618
2619 /* Do not append ".", "/dir/." is equal to "/dir". */
2620 if (STRCMP(fname, ".") != 0)
2621 STRCAT(buf, fname);
2622
2623 return OK;
2624}
2625
2626/*
2627 * Return TRUE if "fname" does not depend on the current directory.
2628 */
2629 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002630mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002632#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 return ( fname[0] == '/' || fname[0] == '.' ||
2634 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2635 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2636 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002637#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639#endif
2640}
2641
Bram Moolenaar24552be2005-12-10 20:17:30 +00002642#if defined(USE_FNAME_CASE) || defined(PROTO)
2643/*
2644 * Set the case of the file name, if it already exists. This will cause the
2645 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002646 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002647 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002648 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002649fname_case(
2650 char_u *name,
2651 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002652{
2653 struct stat st;
2654 char_u *slash, *tail;
2655 DIR *dirp;
2656 struct dirent *dp;
2657
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002658 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002659 {
2660 /* Open the directory where the file is located. */
2661 slash = vim_strrchr(name, '/');
2662 if (slash == NULL)
2663 {
2664 dirp = opendir(".");
2665 tail = name;
2666 }
2667 else
2668 {
2669 *slash = NUL;
2670 dirp = opendir((char *)name);
2671 *slash = '/';
2672 tail = slash + 1;
2673 }
2674
2675 if (dirp != NULL)
2676 {
2677 while ((dp = readdir(dirp)) != NULL)
2678 {
2679 /* Only accept names that differ in case and are the same byte
2680 * length. TODO: accept different length name. */
2681 if (STRICMP(tail, dp->d_name) == 0
2682 && STRLEN(tail) == STRLEN(dp->d_name))
2683 {
2684 char_u newname[MAXPATHL + 1];
2685 struct stat st2;
2686
2687 /* Verify the inode is equal. */
2688 vim_strncpy(newname, name, MAXPATHL);
2689 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2690 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002691 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002692 && st.st_ino == st2.st_ino
2693 && st.st_dev == st2.st_dev)
2694 {
2695 STRCPY(tail, dp->d_name);
2696 break;
2697 }
2698 }
2699 }
2700
2701 closedir(dirp);
2702 }
2703 }
2704}
2705#endif
2706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707/*
2708 * Get file permissions for 'name'.
2709 * Returns -1 when it doesn't exist.
2710 */
2711 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002712mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 struct stat statb;
2715
2716 /* Keep the #ifdef outside of stat(), it may be a macro. */
2717#ifdef VMS
2718 if (stat((char *)vms_fixfilename(name), &statb))
2719#else
2720 if (stat((char *)name, &statb))
2721#endif
2722 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002723#ifdef __INTERIX
2724 /* The top bit makes the value negative, which means the file doesn't
2725 * exist. Remove the bit, we don't use it. */
2726 return statb.st_mode & ~S_ADDACE;
2727#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730}
2731
2732/*
2733 * set file permission for 'name' to 'perm'
2734 *
2735 * return FAIL for failure, OK otherwise
2736 */
2737 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002738mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 return (chmod((char *)
2741#ifdef VMS
2742 vms_fixfilename(name),
2743#else
2744 name,
2745#endif
2746 (mode_t)perm) == 0 ? OK : FAIL);
2747}
2748
2749#if defined(HAVE_ACL) || defined(PROTO)
2750# ifdef HAVE_SYS_ACL_H
2751# include <sys/acl.h>
2752# endif
2753# ifdef HAVE_SYS_ACCESS_H
2754# include <sys/access.h>
2755# endif
2756
2757# ifdef HAVE_SOLARIS_ACL
2758typedef struct vim_acl_solaris_T {
2759 int acl_cnt;
2760 aclent_t *acl_entry;
2761} vim_acl_solaris_T;
2762# endif
2763
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002764#if defined(HAVE_SELINUX) || defined(PROTO)
2765/*
2766 * Copy security info from "from_file" to "to_file".
2767 */
2768 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002769mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002770{
2771 if (from_file == NULL)
2772 return;
2773
2774 if (selinux_enabled == -1)
2775 selinux_enabled = is_selinux_enabled();
2776
2777 if (selinux_enabled > 0)
2778 {
2779 security_context_t from_context = NULL;
2780 security_context_t to_context = NULL;
2781
2782 if (getfilecon((char *)from_file, &from_context) < 0)
2783 {
2784 /* If the filesystem doesn't support extended attributes,
2785 the original had no special security context and the
2786 target cannot have one either. */
2787 if (errno == EOPNOTSUPP)
2788 return;
2789
2790 MSG_PUTS(_("\nCould not get security context for "));
2791 msg_outtrans(from_file);
2792 msg_putchar('\n');
2793 return;
2794 }
2795 if (getfilecon((char *)to_file, &to_context) < 0)
2796 {
2797 MSG_PUTS(_("\nCould not get security context for "));
2798 msg_outtrans(to_file);
2799 msg_putchar('\n');
2800 freecon (from_context);
2801 return ;
2802 }
2803 if (strcmp(from_context, to_context) != 0)
2804 {
2805 if (setfilecon((char *)to_file, from_context) < 0)
2806 {
2807 MSG_PUTS(_("\nCould not set security context for "));
2808 msg_outtrans(to_file);
2809 msg_putchar('\n');
2810 }
2811 }
2812 freecon(to_context);
2813 freecon(from_context);
2814 }
2815}
2816#endif /* HAVE_SELINUX */
2817
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002818#if defined(HAVE_SMACK) && !defined(PROTO)
2819/*
2820 * Copy security info from "from_file" to "to_file".
2821 */
2822 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002823mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002824{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002825 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002826 {
2827 XATTR_NAME_SMACK,
2828 XATTR_NAME_SMACKEXEC,
2829 XATTR_NAME_SMACKMMAP
2830 };
2831
2832 char buffer[SMACK_LABEL_LEN];
2833 const char *name;
2834 int index;
2835 int ret;
2836 ssize_t size;
2837
2838 if (from_file == NULL)
2839 return;
2840
2841 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2842 / sizeof(smack_copied_attributes)[0]) ; index++)
2843 {
2844 /* get the name of the attribute to copy */
2845 name = smack_copied_attributes[index];
2846
2847 /* get the value of the attribute in buffer */
2848 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2849 if (size >= 0)
2850 {
2851 /* copy the attribute value of buffer */
2852 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2853 if (ret < 0)
2854 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002855 vim_snprintf((char *)IObuff, IOSIZE,
2856 _("Could not set security context %s for %s"),
2857 name, to_file);
2858 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002859 msg_putchar('\n');
2860 }
2861 }
2862 else
2863 {
2864 /* what reason of not having the attribute value? */
2865 switch (errno)
2866 {
2867 case ENOTSUP:
2868 /* extended attributes aren't supported or enabled */
2869 /* should a message be echoed? not sure... */
2870 return; /* leave because it isn't usefull to continue */
2871
2872 case ERANGE:
2873 default:
2874 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002875 vim_snprintf((char *)IObuff, IOSIZE,
2876 _("Could not get security context %s for %s. Removing it!"),
2877 name, from_file);
2878 msg_puts(IObuff);
2879 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002880 /* FALLTHROUGH to remove the attribute */
2881
2882 case ENODATA:
2883 /* no attribute of this name */
2884 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002885 /* Silently ignore errors, apparently this happens when
2886 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002887 break;
2888 }
2889 }
2890 }
2891}
2892#endif /* HAVE_SMACK */
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894/*
2895 * Return a pointer to the ACL of file "fname" in allocated memory.
2896 * Return NULL if the ACL is not available for whatever reason.
2897 */
2898 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002899mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 vim_acl_T ret = NULL;
2902#ifdef HAVE_POSIX_ACL
2903 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2904#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002905#ifdef HAVE_SOLARIS_ZFS_ACL
2906 acl_t *aclent;
2907
2908 if (acl_get((char *)fname, 0, &aclent) < 0)
2909 return NULL;
2910 ret = (vim_acl_T)aclent;
2911#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef HAVE_SOLARIS_ACL
2913 vim_acl_solaris_T *aclent;
2914
2915 aclent = malloc(sizeof(vim_acl_solaris_T));
2916 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2917 {
2918 free(aclent);
2919 return NULL;
2920 }
2921 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2922 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2923 {
2924 free(aclent->acl_entry);
2925 free(aclent);
2926 return NULL;
2927 }
2928 ret = (vim_acl_T)aclent;
2929#else
2930#if defined(HAVE_AIX_ACL)
2931 int aclsize;
2932 struct acl *aclent;
2933
2934 aclsize = sizeof(struct acl);
2935 aclent = malloc(aclsize);
2936 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2937 {
2938 if (errno == ENOSPC)
2939 {
2940 aclsize = aclent->acl_len;
2941 aclent = realloc(aclent, aclsize);
2942 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2943 {
2944 free(aclent);
2945 return NULL;
2946 }
2947 }
2948 else
2949 {
2950 free(aclent);
2951 return NULL;
2952 }
2953 }
2954 ret = (vim_acl_T)aclent;
2955#endif /* HAVE_AIX_ACL */
2956#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002957#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif /* HAVE_POSIX_ACL */
2959 return ret;
2960}
2961
2962/*
2963 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2964 */
2965 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002966mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
2968 if (aclent == NULL)
2969 return;
2970#ifdef HAVE_POSIX_ACL
2971 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2972#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002973#ifdef HAVE_SOLARIS_ZFS_ACL
2974 acl_set((char *)fname, (acl_t *)aclent);
2975#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#ifdef HAVE_SOLARIS_ACL
2977 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
2978 ((vim_acl_solaris_T *)aclent)->acl_entry);
2979#else
2980#ifdef HAVE_AIX_ACL
2981 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
2982#endif /* HAVE_AIX_ACL */
2983#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002984#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif /* HAVE_POSIX_ACL */
2986}
2987
2988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002989mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 if (aclent == NULL)
2992 return;
2993#ifdef HAVE_POSIX_ACL
2994 acl_free((acl_t)aclent);
2995#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002996#ifdef HAVE_SOLARIS_ZFS_ACL
2997 acl_free((acl_t *)aclent);
2998#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#ifdef HAVE_SOLARIS_ACL
3000 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3001 free(aclent);
3002#else
3003#ifdef HAVE_AIX_ACL
3004 free(aclent);
3005#endif /* HAVE_AIX_ACL */
3006#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003007#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#endif /* HAVE_POSIX_ACL */
3009}
3010#endif
3011
3012/*
3013 * Set hidden flag for "name".
3014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003016mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 /* can't hide a file */
3019}
3020
3021/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003022 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 * return FALSE if "name" is not a directory
3024 * return FALSE for error
3025 */
3026 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003027mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 struct stat statb;
3030
3031 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3032 return FALSE;
3033 if (stat((char *)name, &statb))
3034 return FALSE;
3035#ifdef _POSIX_SOURCE
3036 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3037#else
3038 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3039#endif
3040}
3041
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003042/*
3043 * return TRUE if "name" is a directory, NOT a symlink to a directory
3044 * return FALSE if "name" is not a directory
3045 * return FALSE for error
3046 */
3047 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003048mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003049{
3050 struct stat statb;
3051
3052 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3053 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003054 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003055 return FALSE;
3056#ifdef _POSIX_SOURCE
3057 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3058#else
3059 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3060#endif
3061}
3062
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003063static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065/*
3066 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3067 */
3068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003069executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 struct stat st;
3072
3073 if (stat((char *)name, &st))
3074 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003075#ifdef VMS
3076 /* Like on Unix system file can have executable rights but not necessarily
3077 * be an executable, but on Unix is not a default for an ordianry file to
3078 * have an executable flag - on VMS it is in most cases.
3079 * Therefore, this check does not have any sense - let keep us to the
3080 * conventions instead:
3081 * *.COM and *.EXE files are the executables - the rest are not. This is
3082 * not ideal but better then it was.
3083 */
3084 int vms_executable = 0;
3085 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3086 {
3087 if (strstr(vms_tolower((char*)name),".exe") != NULL
3088 || strstr(vms_tolower((char*)name),".com")!= NULL)
3089 vms_executable = 1;
3090 }
3091 return vms_executable;
3092#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096
3097/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003098 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003099 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * Return -1 if unknown.
3101 */
3102 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003103mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 char_u *buf;
3106 char_u *p, *e;
3107 int retval;
3108
Bram Moolenaarb5971142015-03-21 17:32:19 +01003109 /* When "use_path" is false and if it's an absolute or relative path don't
3110 * need to use $PATH. */
3111 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3112 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003113 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003114 /* There must be a path separator, files in the current directory
3115 * can't be executed. */
3116 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003117 {
3118 if (path != NULL)
3119 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003120 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003121 *path = FullName_save(name, TRUE);
3122 else
3123 *path = vim_strsave(name);
3124 }
3125 return TRUE;
3126 }
3127 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130 p = (char_u *)getenv("PATH");
3131 if (p == NULL || *p == NUL)
3132 return -1;
3133 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3134 if (buf == NULL)
3135 return -1;
3136
3137 /*
3138 * Walk through all entries in $PATH to check if "name" exists there and
3139 * is an executable file.
3140 */
3141 for (;;)
3142 {
3143 e = (char_u *)strchr((char *)p, ':');
3144 if (e == NULL)
3145 e = p + STRLEN(p);
3146 if (e - p <= 1) /* empty entry means current dir */
3147 STRCPY(buf, "./");
3148 else
3149 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003150 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 add_pathsep(buf);
3152 }
3153 STRCAT(buf, name);
3154 retval = executable_file(buf);
3155 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003156 {
3157 if (path != NULL)
3158 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003159 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003160 *path = FullName_save(buf, TRUE);
3161 else
3162 *path = vim_strsave(buf);
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 if (*e != ':')
3168 break;
3169 p = e + 1;
3170 }
3171
3172 vim_free(buf);
3173 return retval;
3174}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176/*
3177 * Check what "name" is:
3178 * NODE_NORMAL: file or directory (or doesn't exist)
3179 * NODE_WRITABLE: writable device, socket, fifo, etc.
3180 * NODE_OTHER: non-writable things
3181 */
3182 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003183mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 struct stat st;
3186
3187 if (stat((char *)name, &st))
3188 return NODE_NORMAL;
3189 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3190 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3192 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 /* Everything else is writable? */
3194 return NODE_WRITABLE;
3195}
3196
3197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003198mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
3200#ifdef HAVE_CHECK_STACK_GROWTH
3201 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 check_stack_growth((char *)&i);
3204
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003205# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 get_stack_limit();
3207# endif
3208
3209#endif
3210
3211 /*
3212 * Setup an alternative stack for signals. Helps to catch signals when
3213 * running out of stack space.
3214 * Use of sigaltstack() is preferred, it's more portable.
3215 * Ignore any errors.
3216 */
3217#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003218 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 init_signal_stack();
3220#endif
3221}
3222
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003223#if defined(EXITFREE) || defined(PROTO)
3224 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003225mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003226{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003227# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3228 if (clip_star.owned)
3229 clip_lose_selection(&clip_star);
3230 if (clip_plus.owned)
3231 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003232# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003233# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003234 if (xterm_Shell != (Widget)0)
3235 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003236# ifndef LESSTIF_VERSION
3237 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003238 if (xterm_dpy != NULL)
3239 XtCloseDisplay(xterm_dpy);
3240 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003241 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003242 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003243# ifdef FEAT_X11
3244 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3245# endif
3246 }
3247# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003248# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003249# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003250 if (x11_display != NULL
3251# ifdef FEAT_XCLIPBOARD
3252 && x11_display != xterm_dpy
3253# endif
3254 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 XCloseDisplay(x11_display);
3256# endif
3257# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3258 vim_free(signal_stack);
3259 signal_stack = NULL;
3260# endif
3261# ifdef FEAT_TITLE
3262 vim_free(oldtitle);
3263 vim_free(oldicon);
3264# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003265}
3266#endif
3267
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003268static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270/*
3271 * Output a newline when exiting.
3272 * Make sure the newline goes to the same stream as the text.
3273 */
3274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003275exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003277 if (silent_mode)
3278 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (newline_on_exit || msg_didout)
3280 {
3281 if (msg_use_printf())
3282 {
3283 if (info_message)
3284 mch_msg("\n");
3285 else
3286 mch_errmsg("\r\n");
3287 }
3288 else
3289 out_char('\n');
3290 }
3291 else
3292 {
3293 restore_cterm_colors(); /* get original colors back */
3294 msg_clr_eos_force(); /* clear the rest of the display */
3295 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3296 }
3297}
3298
3299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003300mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 exiting = TRUE;
3303
3304#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3305 x11_export_final_selection();
3306#endif
3307
3308#ifdef FEAT_GUI
3309 if (!gui.in_use)
3310#endif
3311 {
3312 settmode(TMODE_COOK);
3313#ifdef FEAT_TITLE
3314 mch_restore_title(3); /* restore xterm title and icon name */
3315#endif
3316 /*
3317 * When t_ti is not empty but it doesn't cause swapping terminal
3318 * pages, need to output a newline when msg_didout is set. But when
3319 * t_ti does swap pages it should not go to the shell page. Do this
3320 * before stoptermcap().
3321 */
3322 if (swapping_screen() && !newline_on_exit)
3323 exit_scroll();
3324
3325 /* Stop termcap: May need to check for T_CRV response, which
3326 * requires RAW mode. */
3327 stoptermcap();
3328
3329 /*
3330 * A newline is only required after a message in the alternate screen.
3331 * This is set to TRUE by wait_return().
3332 */
3333 if (!swapping_screen() || newline_on_exit)
3334 exit_scroll();
3335
3336 /* Cursor may have been switched off without calling starttermcap()
3337 * when doing "vim -u vimrc" and vimrc contains ":q". */
3338 if (full_screen)
3339 cursor_on();
3340 }
3341 out_flush();
3342 ml_close_all(TRUE); /* remove all memfiles */
3343 may_core_dump();
3344#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 gui_exit(r);
3347#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003348
Bram Moolenaar56718732006-03-15 22:53:57 +00003349#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003350 mac_conv_cleanup();
3351#endif
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#ifdef __QNX__
3354 /* A core dump won't be created if the signal handler
3355 * doesn't return, so we can't call exit() */
3356 if (deadly_signal != 0)
3357 return;
3358#endif
3359
Bram Moolenaar009b2592004-10-24 19:18:58 +00003360#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003361 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003362#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003363
3364#ifdef EXITFREE
3365 free_all_mem();
3366#endif
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 exit(r);
3369}
3370
3371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003372may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 if (deadly_signal != 0)
3375 {
3376 signal(deadly_signal, SIG_DFL);
3377 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3378 }
3379}
3380
3381#ifndef VMS
3382
3383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003384mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 static int first = TRUE;
3387
3388 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3389#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3390 /*
3391 * for "new" tty systems
3392 */
3393# ifdef HAVE_TERMIOS_H
3394 static struct termios told;
3395 struct termios tnew;
3396# else
3397 static struct termio told;
3398 struct termio tnew;
3399# endif
3400
3401 if (first)
3402 {
3403 first = FALSE;
3404# if defined(HAVE_TERMIOS_H)
3405 tcgetattr(read_cmd_fd, &told);
3406# else
3407 ioctl(read_cmd_fd, TCGETA, &told);
3408# endif
3409 }
3410
3411 tnew = told;
3412 if (tmode == TMODE_RAW)
3413 {
3414 /*
3415 * ~ICRNL enables typing ^V^M
3416 */
3417 tnew.c_iflag &= ~ICRNL;
3418 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3419# if defined(IEXTEN) && !defined(__MINT__)
3420 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3421 /* but it breaks function keys on MINT */
3422# endif
3423 );
3424# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3425 tnew.c_oflag &= ~ONLCR;
3426# endif
3427 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3428 tnew.c_cc[VTIME] = 0; /* don't wait */
3429 }
3430 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003431 {
3432 /* Also reset ICANON here, otherwise on Solaris select() won't see
3433 * typeahead characters. */
3434 tnew.c_lflag &= ~(ICANON | ECHO);
3435 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3436 tnew.c_cc[VTIME] = 0; /* don't wait */
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439# if defined(HAVE_TERMIOS_H)
3440 {
3441 int n = 10;
3442
3443 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3444 * few times. */
3445 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3446 && errno == EINTR && n > 0)
3447 --n;
3448 }
3449# else
3450 ioctl(read_cmd_fd, TCSETA, &tnew);
3451# endif
3452
3453#else
3454
3455 /*
3456 * for "old" tty systems
3457 */
3458# ifndef TIOCSETN
3459# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3460# endif
3461 static struct sgttyb ttybold;
3462 struct sgttyb ttybnew;
3463
3464 if (first)
3465 {
3466 first = FALSE;
3467 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3468 }
3469
3470 ttybnew = ttybold;
3471 if (tmode == TMODE_RAW)
3472 {
3473 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3474 ttybnew.sg_flags |= RAW;
3475 }
3476 else if (tmode == TMODE_SLEEP)
3477 ttybnew.sg_flags &= ~(ECHO);
3478 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3479#endif
3480 curr_tmode = tmode;
3481}
3482
3483/*
3484 * Try to get the code for "t_kb" from the stty setting
3485 *
3486 * Even if termcap claims a backspace key, the user's setting *should*
3487 * prevail. stty knows more about reality than termcap does, and if
3488 * somebody's usual erase key is DEL (which, for most BSD users, it will
3489 * be), they're going to get really annoyed if their erase key starts
3490 * doing forward deletes for no reason. (Eric Fischer)
3491 */
3492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003493get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 char_u buf[2];
3496 char_u *p;
3497
3498 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3499#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3500 /* for "new" tty systems */
3501# ifdef HAVE_TERMIOS_H
3502 struct termios keys;
3503# else
3504 struct termio keys;
3505# endif
3506
3507# if defined(HAVE_TERMIOS_H)
3508 if (tcgetattr(read_cmd_fd, &keys) != -1)
3509# else
3510 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3511# endif
3512 {
3513 buf[0] = keys.c_cc[VERASE];
3514 intr_char = keys.c_cc[VINTR];
3515#else
3516 /* for "old" tty systems */
3517 struct sgttyb keys;
3518
3519 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3520 {
3521 buf[0] = keys.sg_erase;
3522 intr_char = keys.sg_kill;
3523#endif
3524 buf[1] = NUL;
3525 add_termcode((char_u *)"kb", buf, FALSE);
3526
3527 /*
3528 * If <BS> and <DEL> are now the same, redefine <DEL>.
3529 */
3530 p = find_termcode((char_u *)"kD");
3531 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3532 do_fixdel(NULL);
3533 }
3534#if 0
3535 } /* to keep cindent happy */
3536#endif
3537}
3538
3539#endif /* VMS */
3540
3541#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3542/*
3543 * Set mouse clicks on or off.
3544 */
3545 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003546mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
3548 static int ison = FALSE;
3549 int xterm_mouse_vers;
3550
3551 if (on == ison) /* return quickly if nothing to do */
3552 return;
3553
3554 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003555
3556# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003557 if (ttym_flags == TTYM_URXVT)
3558 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003559 out_str_nf((char_u *)
3560 (on
3561 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3562 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3563 ison = on;
3564 }
3565# endif
3566
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003567# ifdef FEAT_MOUSE_SGR
3568 if (ttym_flags == TTYM_SGR)
3569 {
3570 out_str_nf((char_u *)
3571 (on
3572 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3573 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3574 ison = on;
3575 }
3576# endif
3577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (xterm_mouse_vers > 0)
3579 {
3580 if (on) /* enable mouse events, use mouse tracking if available */
3581 out_str_nf((char_u *)
3582 (xterm_mouse_vers > 1
3583 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3584 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3585 else /* disable mouse events, could probably always send the same */
3586 out_str_nf((char_u *)
3587 (xterm_mouse_vers > 1
3588 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3589 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3590 ison = on;
3591 }
3592
3593# ifdef FEAT_MOUSE_DEC
3594 else if (ttym_flags == TTYM_DEC)
3595 {
3596 if (on) /* enable mouse events */
3597 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3598 else /* disable mouse events */
3599 out_str_nf((char_u *)"\033['z");
3600 ison = on;
3601 }
3602# endif
3603
3604# ifdef FEAT_MOUSE_GPM
3605 else
3606 {
3607 if (on)
3608 {
3609 if (gpm_open())
3610 ison = TRUE;
3611 }
3612 else
3613 {
3614 gpm_close();
3615 ison = FALSE;
3616 }
3617 }
3618# endif
3619
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003620# ifdef FEAT_SYSMOUSE
3621 else
3622 {
3623 if (on)
3624 {
3625 if (sysmouse_open() == OK)
3626 ison = TRUE;
3627 }
3628 else
3629 {
3630 sysmouse_close();
3631 ison = FALSE;
3632 }
3633 }
3634# endif
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636# ifdef FEAT_MOUSE_JSB
3637 else
3638 {
3639 if (on)
3640 {
3641 /* D - Enable Mouse up/down messages
3642 * L - Enable Left Button Reporting
3643 * M - Enable Middle Button Reporting
3644 * R - Enable Right Button Reporting
3645 * K - Enable SHIFT and CTRL key Reporting
3646 * + - Enable Advanced messaging of mouse moves and up/down messages
3647 * Q - Quiet No Ack
3648 * # - Numeric value of mouse pointer required
3649 * 0 = Multiview 2000 cursor, used as standard
3650 * 1 = Windows Arrow
3651 * 2 = Windows I Beam
3652 * 3 = Windows Hour Glass
3653 * 4 = Windows Cross Hair
3654 * 5 = Windows UP Arrow
3655 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003656# ifdef JSBTERM_MOUSE_NONADVANCED
3657 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3659 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003660# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3662 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 ison = TRUE;
3665 }
3666 else
3667 {
3668 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3669 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3670 ison = FALSE;
3671 }
3672 }
3673# endif
3674# ifdef FEAT_MOUSE_PTERM
3675 else
3676 {
3677 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3678 if (on)
3679 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3680 else
3681 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3682 ison = on;
3683 }
3684# endif
3685}
3686
3687/*
3688 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3689 */
3690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003691check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
3693# ifdef FEAT_MOUSE_XTERM
3694 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003695# ifdef FEAT_MOUSE_URXVT
3696 && use_xterm_mouse() != 3
3697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698# ifdef FEAT_GUI
3699 && !gui.in_use
3700# endif
3701 )
3702 {
3703 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003704 ? IF_EB("\233M", CSI_STR "M")
3705 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (*p_mouse != NUL)
3707 {
3708 /* force mouse off and maybe on to send possibly new mouse
3709 * activation sequence to the xterm, with(out) drag tracing. */
3710 mch_setmouse(FALSE);
3711 setmouse();
3712 }
3713 }
3714 else
3715 del_mouse_termcode(KS_MOUSE);
3716# endif
3717
3718# ifdef FEAT_MOUSE_GPM
3719 if (!use_xterm_mouse()
3720# ifdef FEAT_GUI
3721 && !gui.in_use
3722# endif
3723 )
3724 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3725# endif
3726
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003727# ifdef FEAT_SYSMOUSE
3728 if (!use_xterm_mouse()
3729# ifdef FEAT_GUI
3730 && !gui.in_use
3731# endif
3732 )
3733 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3734# endif
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003737 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (!use_xterm_mouse()
3739# ifdef FEAT_GUI
3740 && !gui.in_use
3741# endif
3742 )
3743 set_mouse_termcode(KS_JSBTERM_MOUSE,
3744 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3745 else
3746 del_mouse_termcode(KS_JSBTERM_MOUSE);
3747# endif
3748
3749# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003750 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 * define it in the GUI or when using an xterm. */
3752 if (!use_xterm_mouse()
3753# ifdef FEAT_GUI
3754 && !gui.in_use
3755# endif
3756 )
3757 set_mouse_termcode(KS_NETTERM_MOUSE,
3758 (char_u *)IF_EB("\033}", ESC_STR "}"));
3759 else
3760 del_mouse_termcode(KS_NETTERM_MOUSE);
3761# endif
3762
3763# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003764 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003765 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766# ifdef FEAT_GUI
3767 && !gui.in_use
3768# endif
3769 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003770 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3771 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 else
3773 del_mouse_termcode(KS_DEC_MOUSE);
3774# endif
3775# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003776 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003777 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778# ifdef FEAT_GUI
3779 && !gui.in_use
3780# endif
3781 )
3782 set_mouse_termcode(KS_PTERM_MOUSE,
3783 (char_u *) IF_EB("\033[", ESC_STR "["));
3784 else
3785 del_mouse_termcode(KS_PTERM_MOUSE);
3786# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003787# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003788 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003789# ifdef FEAT_GUI
3790 && !gui.in_use
3791# endif
3792 )
3793 {
3794 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003795 ? IF_EB("\233*M", CSI_STR "*M")
3796 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003797
3798 if (*p_mouse != NUL)
3799 {
3800 mch_setmouse(FALSE);
3801 setmouse();
3802 }
3803 }
3804 else
3805 del_mouse_termcode(KS_URXVT_MOUSE);
3806# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003807# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003808 if (use_xterm_mouse() == 4
3809# ifdef FEAT_GUI
3810 && !gui.in_use
3811# endif
3812 )
3813 {
3814 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003815 ? IF_EB("\233<*M", CSI_STR "<*M")
3816 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3817
3818 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3819 ? IF_EB("\233<*m", CSI_STR "<*m")
3820 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003821
3822 if (*p_mouse != NUL)
3823 {
3824 mch_setmouse(FALSE);
3825 setmouse();
3826 }
3827 }
3828 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003829 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003830 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003831 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3832 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834}
3835#endif
3836
3837/*
3838 * set screen mode, always fails.
3839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003841mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
3843 EMSG(_(e_screenmode));
3844 return FAIL;
3845}
3846
3847#ifndef VMS
3848
3849/*
3850 * Try to get the current window size:
3851 * 1. with an ioctl(), most accurate method
3852 * 2. from the environment variables LINES and COLUMNS
3853 * 3. from the termcap
3854 * 4. keep using the old values
3855 * Return OK when size could be determined, FAIL otherwise.
3856 */
3857 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003858mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 long rows = 0;
3861 long columns = 0;
3862 char_u *p;
3863
3864 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 * 1. try using an ioctl. It is the most accurate method.
3866 *
3867 * Try using TIOCGWINSZ first, some systems that have it also define
3868 * TIOCGSIZE but don't have a struct ttysize.
3869 */
3870# ifdef TIOCGWINSZ
3871 {
3872 struct winsize ws;
3873 int fd = 1;
3874
3875 /* When stdout is not a tty, use stdin for the ioctl(). */
3876 if (!isatty(fd) && isatty(read_cmd_fd))
3877 fd = read_cmd_fd;
3878 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3879 {
3880 columns = ws.ws_col;
3881 rows = ws.ws_row;
3882 }
3883 }
3884# else /* TIOCGWINSZ */
3885# ifdef TIOCGSIZE
3886 {
3887 struct ttysize ts;
3888 int fd = 1;
3889
3890 /* When stdout is not a tty, use stdin for the ioctl(). */
3891 if (!isatty(fd) && isatty(read_cmd_fd))
3892 fd = read_cmd_fd;
3893 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3894 {
3895 columns = ts.ts_cols;
3896 rows = ts.ts_lines;
3897 }
3898 }
3899# endif /* TIOCGSIZE */
3900# endif /* TIOCGWINSZ */
3901
3902 /*
3903 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003904 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3905 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003907 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
3909 if ((p = (char_u *)getenv("LINES")))
3910 rows = atoi((char *)p);
3911 if ((p = (char_u *)getenv("COLUMNS")))
3912 columns = atoi((char *)p);
3913 }
3914
3915#ifdef HAVE_TGETENT
3916 /*
3917 * 3. try reading "co" and "li" entries from termcap
3918 */
3919 if (columns == 0 || rows == 0)
3920 getlinecol(&columns, &rows);
3921#endif
3922
3923 /*
3924 * 4. If everything fails, use the old values
3925 */
3926 if (columns <= 0 || rows <= 0)
3927 return FAIL;
3928
3929 Rows = rows;
3930 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003931 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return OK;
3933}
3934
Bram Moolenaarb13501f2017-07-22 22:32:56 +02003935#if defined(FEAT_TERMINAL) || defined(PROTO)
3936/*
3937 * Report the windows size "rows" and "cols" to tty "fd".
3938 */
3939 int
3940mch_report_winsize(int fd, int rows, int cols)
3941{
3942# ifdef TIOCSWINSZ
3943 struct winsize ws;
3944
3945 ws.ws_col = cols;
3946 ws.ws_row = rows;
3947 ws.ws_xpixel = cols * 5;
3948 ws.ws_ypixel = rows * 10;
3949 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
3950 {
3951 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
3952 return OK;
3953 }
3954 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
3955# else
3956# ifdef TIOCSSIZE
3957 struct ttysize ts;
3958
3959 ts.ts_cols = cols;
3960 ts.ts_lines = rows;
3961 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
3962 {
3963 ch_log(NULL, "ioctl(TIOCSSIZE) success");
3964 return OK;
3965 }
3966 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
3967# endif
3968# endif
3969 return FAIL;
3970}
3971#endif
3972
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973/*
3974 * Try to set the window size to Rows and Columns.
3975 */
3976 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003977mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 if (*T_CWS)
3980 {
3981 /*
3982 * NOTE: if you get an error here that term_set_winsize() is
3983 * undefined, check the output of configure. It could probably not
3984 * find a ncurses, termcap or termlib library.
3985 */
3986 term_set_winsize((int)Rows, (int)Columns);
3987 out_flush();
3988 screen_start(); /* don't know where cursor is now */
3989 }
3990}
3991
3992#endif /* VMS */
3993
3994/*
3995 * Rows and/or Columns has changed.
3996 */
3997 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003998mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
4000 /* Nothing to do. */
4001}
4002
Bram Moolenaar205b8862011-09-07 15:04:31 +02004003/*
4004 * Wait for process "child" to end.
4005 * Return "child" if it exited properly, <= 0 on error.
4006 */
4007 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004008wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004009{
4010 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004011 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004012
4013 while (wait_pid != child)
4014 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004015 /* When compiled with Python threads are probably used, in which case
4016 * wait() sometimes hangs for no obvious reason. Use waitpid()
4017 * instead and loop (like the GUI). Also needed for other interfaces,
4018 * they might call system(). */
4019# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004020 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004021# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004022 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004023# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004024 if (wait_pid == 0)
4025 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004026 /* Wait for 1 to 10 msec before trying again. */
4027 mch_delay(delay_msec, TRUE);
4028 if (++delay_msec > 10)
4029 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004030 continue;
4031 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004032 if (wait_pid <= 0
4033# ifdef ECHILD
4034 && errno == ECHILD
4035# endif
4036 )
4037 break;
4038 }
4039 return wait_pid;
4040}
4041
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004042#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01004043/*
4044 * Parse "cmd" and put the white-separated parts in "argv".
4045 * "argv" is an allocated array with "argc" entries.
4046 * Returns FAIL when out of memory.
4047 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004048 int
4049mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
4050{
4051 int i;
4052 char_u *p;
4053 int inquote;
4054
4055 /*
4056 * Do this loop twice:
4057 * 1: find number of arguments
4058 * 2: separate them and build argv[]
4059 */
4060 for (i = 0; i < 2; ++i)
4061 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02004062 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004063 inquote = FALSE;
4064 *argc = 0;
4065 for (;;)
4066 {
4067 if (i == 1)
4068 (*argv)[*argc] = (char *)p;
4069 ++*argc;
4070 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4071 {
4072 if (*p == '"')
4073 inquote = !inquote;
4074 ++p;
4075 }
4076 if (*p == NUL)
4077 break;
4078 if (i == 1)
4079 *p++ = NUL;
4080 p = skipwhite(p);
4081 }
4082 if (*argv == NULL)
4083 {
4084 if (use_shcf)
4085 {
4086 /* Account for possible multiple args in p_shcf. */
4087 p = p_shcf;
4088 for (;;)
4089 {
4090 p = skiptowhite(p);
4091 if (*p == NUL)
4092 break;
4093 ++*argc;
4094 p = skipwhite(p);
4095 }
4096 }
4097
4098 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4099 if (*argv == NULL) /* out of memory */
4100 return FAIL;
4101 }
4102 }
4103 return OK;
4104}
4105#endif
4106
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004107#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004108/*
4109 * Set the environment for a child process.
4110 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004111 static void
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004112set_child_environment(long rows, long columns, char *term)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004113{
4114# ifdef HAVE_SETENV
4115 char envbuf[50];
4116# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004117 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004118 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004119 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004120 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004121 static char envbuf_Colors[20];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004122# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004123 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004124# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004125# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004126 long colors =
4127# ifdef FEAT_GUI
4128 gui.in_use ? 256*256*256 :
4129# endif
4130 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004131
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004132# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004133 setenv("TERM", term, 1);
4134 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004135 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004136 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004137 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004138 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004139 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004140 sprintf((char *)envbuf, "%ld", colors);
4141 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004142# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004143 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004144# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004145# else
4146 /*
4147 * Putenv does not copy the string, it has to remain valid.
4148 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004149 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004150 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004151 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4152 putenv(envbuf_Term);
4153 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004154 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004155 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4156 putenv(envbuf_Lines);
4157 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4158 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004159 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004160 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4161 putenv(envbuf_Colors);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004162# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004163 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4164 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4165 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004166# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004167# endif
4168}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004169
4170 static void
4171set_default_child_environment(void)
4172{
4173 set_child_environment(Rows, Columns, "dumb");
4174}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004175#endif
4176
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004177#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004178/*
4179 * Open a PTY, with FD for the master and slave side.
4180 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4181 * When successful both file descriptors are stored.
4182 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004183 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004184open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004185{
4186 char *tty_name;
4187
4188 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4189 if (*pty_master_fd >= 0)
4190 {
4191 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4192 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4193 * adding O_NOCTTY always works when defined. */
4194#ifdef O_NOCTTY
4195 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4196#else
4197 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4198#endif
4199 if (*pty_slave_fd < 0)
4200 {
4201 close(*pty_master_fd);
4202 *pty_master_fd = -1;
4203 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004204 else if (namep != NULL)
4205 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004206 }
4207}
4208#endif
4209
Bram Moolenaarfae42832017-08-01 22:24:26 +02004210/*
4211 * Send SIGINT to a child process if "c" is an interrupt character.
4212 */
4213 void
4214may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4215{
4216# ifdef SIGINT
4217 if (c == Ctrl_C || c == intr_char)
4218 {
4219# ifdef HAVE_SETSID
4220 kill(-pid, SIGINT);
4221# else
4222 kill(0, SIGINT);
4223# endif
4224 if (wpid > 0)
4225 kill(wpid, SIGINT);
4226 }
4227# endif
4228}
4229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004231mch_call_shell(
4232 char_u *cmd,
4233 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235#ifdef VMS
4236 char *ifn = NULL;
4237 char *ofn = NULL;
4238#endif
4239 int tmode = cur_tmode;
4240#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004241 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004242 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 out_flush();
4245
4246 if (options & SHELL_COOKED)
4247 settmode(TMODE_COOK); /* set to normal mode */
4248
Bram Moolenaar62b42182010-09-21 22:09:37 +02004249# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004250 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004251 loose_clipboard();
4252# endif
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if (cmd == NULL)
4255 x = system((char *)p_sh);
4256 else
4257 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004258# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (ofn = strchr((char *)cmd, '>'))
4260 *ofn++ = '\0';
4261 if (ifn = strchr((char *)cmd, '<'))
4262 {
4263 char *p;
4264
4265 *ifn++ = '\0';
4266 p = strchr(ifn,' '); /* chop off any trailing spaces */
4267 if (p)
4268 *p = '\0';
4269 }
4270 if (ofn)
4271 x = vms_sys((char *)cmd, ofn, ifn);
4272 else
4273 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004274# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 newcmd = lalloc(STRLEN(p_sh)
4276 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4277 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4278 if (newcmd == NULL)
4279 x = 0;
4280 else
4281 {
4282 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4283 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4284 (char *)p_shcf,
4285 (char *)cmd);
4286 x = system((char *)newcmd);
4287 vim_free(newcmd);
4288 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291# ifdef VMS
4292 x = vms_sys_status(x);
4293# endif
4294 if (emsg_silent)
4295 ;
4296 else if (x == 127)
4297 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 else if (x && !(options & SHELL_SILENT))
4299 {
4300 MSG_PUTS(_("\nshell returned "));
4301 msg_outnum((long)x);
4302 msg_putchar('\n');
4303 }
4304
4305 if (tmode == TMODE_RAW)
4306 settmode(TMODE_RAW); /* set to raw mode */
4307# ifdef FEAT_TITLE
4308 resettitle();
4309# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004310# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4311 restore_clipboard();
4312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return x;
4314
4315#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4316
Bram Moolenaardf177f62005-02-22 08:39:57 +00004317# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4318 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar835dc632016-02-07 14:27:38 +01004320 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004322 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 pid_t wait_pid = 0;
4324# ifdef HAVE_UNION_WAIT
4325 union wait status;
4326# else
4327 int status = -1;
4328# endif
4329 int retval = -1;
4330 char **argv = NULL;
4331 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004332 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 int i;
4334 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004336# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004338# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004339 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 int fd_fromshell[2];
4341 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004342 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
Bram Moolenaar62b42182010-09-21 22:09:37 +02004344 newcmd = vim_strsave(p_sh);
4345 if (newcmd == NULL) /* out of memory */
4346 goto error;
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 out_flush();
4349 if (options & SHELL_COOKED)
4350 settmode(TMODE_COOK); /* set to normal mode */
4351
Bram Moolenaar835dc632016-02-07 14:27:38 +01004352 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4353 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004354
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (cmd != NULL)
4356 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004357 char_u *s;
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (extra_shell_arg != NULL)
4360 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004361
4362 /* Break 'shellcmdflag' into white separated parts. This doesn't
4363 * handle quoted strings, they are very unlikely to appear. */
4364 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4365 if (p_shcf_copy == NULL) /* out of memory */
4366 goto error;
4367 s = p_shcf_copy;
4368 p = p_shcf;
4369 while (*p != NUL)
4370 {
4371 argv[argc++] = (char *)s;
4372 while (*p && *p != ' ' && *p != TAB)
4373 *s++ = *p++;
4374 *s++ = NUL;
4375 p = skipwhite(p);
4376 }
4377
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 argv[argc++] = (char *)cmd;
4379 }
4380 argv[argc] = NULL;
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004383 * For the GUI, when writing the output into the buffer and when reading
4384 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4385 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004387 if ((options & (SHELL_READ|SHELL_WRITE))
4388# ifdef FEAT_GUI
4389 || (gui.in_use && show_shell_mess)
4390# endif
4391 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004393# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 /*
4395 * Try to open a master pty.
4396 * If this works, open the slave pty.
4397 * If the slave can't be opened, close the master pty.
4398 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004399 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004400 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 /*
4402 * If not opening a pty or it didn't work, try using pipes.
4403 */
4404 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
4407 pipe_error = (pipe(fd_toshell) < 0);
4408 if (!pipe_error) /* pipe create OK */
4409 {
4410 pipe_error = (pipe(fd_fromshell) < 0);
4411 if (pipe_error) /* pipe create failed */
4412 {
4413 close(fd_toshell[0]);
4414 close(fd_toshell[1]);
4415 }
4416 }
4417 if (pipe_error)
4418 {
4419 MSG_PUTS(_("\nCannot create pipes\n"));
4420 out_flush();
4421 }
4422 }
4423 }
4424
4425 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004427 SIGSET_DECL(curset)
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429# ifdef __BEOS__
4430 beos_cleanup_read_thread();
4431# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004432
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004433 BLOCK_SIGNALS(&curset);
4434 pid = fork(); /* maybe we should use vfork() */
4435 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004437 UNBLOCK_SIGNALS(&curset);
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004440 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004442 || (gui.in_use && show_shell_mess)
4443# endif
4444 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004446# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (pty_master_fd >= 0) /* close the pseudo tty */
4448 {
4449 close(pty_master_fd);
4450 close(pty_slave_fd);
4451 }
4452 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 close(fd_toshell[0]);
4456 close(fd_toshell[1]);
4457 close(fd_fromshell[0]);
4458 close(fd_fromshell[1]);
4459 }
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else if (pid == 0) /* child */
4463 {
4464 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004465 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 if (!show_shell_mess || (options & SHELL_EXPAND))
4468 {
4469 int fd;
4470
4471 /*
4472 * Don't want to show any message from the shell. Can't just
4473 * close stdout and stderr though, because some systems will
4474 * break if you try to write to them after that, so we must
4475 * use dup() to replace them with something else -- webb
4476 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4477 * waiting for input.
4478 */
4479 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4480 fclose(stdin);
4481 fclose(stdout);
4482 fclose(stderr);
4483
4484 /*
4485 * If any of these open()'s and dup()'s fail, we just continue
4486 * anyway. It's not fatal, and on most systems it will make
4487 * no difference at all. On a few it will cause the execvp()
4488 * to exit with a non-zero status even when the completion
4489 * could be done, which is nothing too serious. If the open()
4490 * or dup() failed we'd just do the same thing ourselves
4491 * anyway -- webb
4492 */
4493 if (fd >= 0)
4494 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004495 ignored = dup(fd); /* To replace stdin (fd 0) */
4496 ignored = dup(fd); /* To replace stdout (fd 1) */
4497 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 /* Don't need this now that we've duplicated it */
4500 close(fd);
4501 }
4502 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004503 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004505 || gui.in_use
4506# endif
4507 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
4509
Bram Moolenaardf177f62005-02-22 08:39:57 +00004510# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004511 /* Create our own process group, so that the child and all its
4512 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004513 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004514 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004515 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004516 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004517# if defined(SIGHUP)
4518 /* When doing "!xterm&" and 'shell' is bash: the shell
4519 * will exit and send SIGHUP to all processes in its
4520 * group, killing the just started process. Ignore SIGHUP
4521 * to avoid that. (suggested by Simon Schubert)
4522 */
4523 signal(SIGHUP, SIG_IGN);
4524# endif
4525 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004526# endif
4527# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004528 if (pty_slave_fd >= 0)
4529 {
4530 /* push stream discipline modules */
4531 if (options & SHELL_COOKED)
4532 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004534 /* Try to become controlling tty (probably doesn't work,
4535 * unless run by root) */
4536 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004538 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004539# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004540 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaara5792f52005-11-23 21:25:05 +00004542 /*
4543 * stderr is only redirected when using the GUI, so that a
4544 * program like gpg can still access the terminal to get a
4545 * passphrase using stderr.
4546 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004547# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (pty_master_fd >= 0)
4549 {
4550 close(pty_master_fd); /* close master side of pty */
4551
4552 /* set up stdin/stdout/stderr for the child */
4553 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004554 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004556 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004557 if (gui.in_use)
4558 {
4559 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004560 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 close(pty_slave_fd); /* has been dupped, close it now */
4564 }
4565 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
4568 /* set up stdin for the child */
4569 close(fd_toshell[1]);
4570 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004571 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 close(fd_toshell[0]);
4573
4574 /* set up stdout for the child */
4575 close(fd_fromshell[0]);
4576 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004577 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 close(fd_fromshell[1]);
4579
Bram Moolenaara5792f52005-11-23 21:25:05 +00004580# ifdef FEAT_GUI
4581 if (gui.in_use)
4582 {
4583 /* set up stderr for the child */
4584 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004585 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004586 }
4587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * There is no type cast for the argv, because the type may be
4593 * different on different machines. This may cause a warning
4594 * message with strict compilers, don't worry about it.
4595 * Call _exit() instead of exit() to avoid closing the connection
4596 * to the X server (esp. with GTK, which uses atexit()).
4597 */
4598 execvp(argv[0], argv);
4599 _exit(EXEC_FAILED); /* exec failed, return failure code */
4600 }
4601 else /* parent */
4602 {
4603 /*
4604 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004605 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 */
4607 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004609 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004610# ifdef FEAT_JOB_CHANNEL
4611 ++dont_check_job_ended;
4612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004615 * This is also used to pipe stdin/stdout to/from the external
4616 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004618 if ((options & (SHELL_READ|SHELL_WRITE))
4619# ifdef FEAT_GUI
4620 || (gui.in_use && show_shell_mess)
4621# endif
4622 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004624# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004626# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4630 int ta_len = 0; /* valid bytes in ta_buf[] */
4631 int len;
4632 int p_more_save;
4633 int old_State;
4634 int c;
4635 int toshell_fd;
4636 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004637 garray_T ga;
4638 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004639# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4640 struct timeval start_tv;
4641# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (pty_master_fd >= 0)
4645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 fromshell_fd = pty_master_fd;
4647 toshell_fd = dup(pty_master_fd);
4648 }
4649 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652 close(fd_toshell[0]);
4653 close(fd_fromshell[1]);
4654 toshell_fd = fd_toshell[1];
4655 fromshell_fd = fd_fromshell[0];
4656 }
4657
4658 /*
4659 * Write to the child if there are typed characters.
4660 * Read from the child if there are characters available.
4661 * Repeat the reading a few times if more characters are
4662 * available. Need to check for typed keys now and then, but
4663 * not too often (delays when no chars are available).
4664 * This loop is quit if no characters can be read from the pty
4665 * (WaitForChar detected special condition), or there are no
4666 * characters available and the child has exited.
4667 * Only check if the child has exited when there is no more
4668 * output. The child may exit before all the output has
4669 * been printed.
4670 *
4671 * Currently this busy loops!
4672 * This can probably dead-lock when the write blocks!
4673 */
4674 p_more_save = p_more;
4675 p_more = FALSE;
4676 old_State = State;
4677 State = EXTERNCMD; /* don't redraw at window resize */
4678
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004679 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004680 {
4681 /* Fork a process that will write the lines to the
4682 * external program. */
4683 if ((wpid = fork()) == -1)
4684 {
4685 MSG_PUTS(_("\nCannot fork\n"));
4686 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004687 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688 {
4689 linenr_T lnum = curbuf->b_op_start.lnum;
4690 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004691 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692 size_t l;
4693
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004694 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004695 for (;;)
4696 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004697 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004698 if (l == 0)
4699 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004700 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004701 /* NL -> NUL translation */
4702 len = write(toshell_fd, "", (size_t)1);
4703 else
4704 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004705 char_u *s = vim_strchr(lp + written, NL);
4706
Bram Moolenaar89d40322006-08-29 15:30:07 +00004707 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004708 s == NULL ? l
4709 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004710 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004711 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004712 {
4713 /* Finished a line, add a NL, unless this line
4714 * should not have one. */
4715 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004716 || (!curbuf->b_p_bin
4717 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004718 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004719 && (lnum !=
4720 curbuf->b_ml.ml_line_count
4721 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004722 ignored = write(toshell_fd, "\n",
4723 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724 ++lnum;
4725 if (lnum > curbuf->b_op_end.lnum)
4726 {
4727 /* finished all the lines, close pipe */
4728 close(toshell_fd);
4729 toshell_fd = -1;
4730 break;
4731 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004732 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004733 written = 0;
4734 }
4735 else if (len > 0)
4736 written += len;
4737 }
4738 _exit(0);
4739 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004740 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004741 {
4742 close(toshell_fd);
4743 toshell_fd = -1;
4744 }
4745 }
4746
4747 if (options & SHELL_READ)
4748 ga_init2(&ga, 1, BUFLEN);
4749
4750 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004751# ifdef ELAPSED_FUNC
4752 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 for (;;)
4755 {
4756 /*
4757 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004758 * if there are any.
4759 * Don't do this if we are expanding wild cards (would eat
4760 * typeahead).
4761 * Don't do this when filtering and terminal is in cooked
4762 * mode, the shell command will handle the I/O. Avoids
4763 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004764 * Don't get characters when the child has already
4765 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004767 * while (noread_cnt > 4), avoids that ":r !ls" eats
4768 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 */
4770 len = 0;
4771 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004772 && ((options &
4773 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4774 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004775# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004776 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004777# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004778 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004779 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004780 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004782 if (ta_len == 0)
4783 {
4784 /* Get extra characters when we don't have any.
4785 * Reset the counter and timer. */
4786 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004787# ifdef ELAPSED_FUNC
4788 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004789# endif
4790 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4791 }
4792 if (ta_len > 0 || len > 0)
4793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 /*
4795 * For pipes:
4796 * Check for CTRL-C: send interrupt signal to child.
4797 * Check for CTRL-D: EOF, close pipe to child.
4798 */
4799 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 /*
4802 * Send SIGINT to the child's group or all
4803 * processes in our group.
4804 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004805 may_send_sigint(ta_buf[ta_len], pid, wpid);
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (pty_master_fd < 0 && toshell_fd >= 0
4808 && ta_buf[ta_len] == Ctrl_D)
4809 {
4810 close(toshell_fd);
4811 toshell_fd = -1;
4812 }
4813 }
4814
4815 /* replace K_BS by <BS> and K_DEL by <DEL> */
4816 for (i = ta_len; i < ta_len + len; ++i)
4817 {
4818 if (ta_buf[i] == CSI && len - i > 2)
4819 {
4820 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4821 if (c == K_DEL || c == K_KDEL || c == K_BS)
4822 {
4823 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4824 (size_t)(len - i - 2));
4825 if (c == K_DEL || c == K_KDEL)
4826 ta_buf[i] = DEL;
4827 else
4828 ta_buf[i] = Ctrl_H;
4829 len -= 2;
4830 }
4831 }
4832 else if (ta_buf[i] == '\r')
4833 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004836 i += (*mb_ptr2len_len)(ta_buf + i,
4837 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840
4841 /*
4842 * For pipes: echo the typed characters.
4843 * For a pty this does not seem to work.
4844 */
4845 if (pty_master_fd < 0)
4846 {
4847 for (i = ta_len; i < ta_len + len; ++i)
4848 {
4849 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4850 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004851# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else if (has_mbyte)
4853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004854 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 msg_outtrans_len(ta_buf + i, l);
4857 i += l - 1;
4858 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 else
4861 msg_outtrans_len(ta_buf + i, 1);
4862 }
4863 windgoto(msg_row, msg_col);
4864 out_flush();
4865 }
4866
4867 ta_len += len;
4868
4869 /*
4870 * Write the characters to the child, unless EOF has
4871 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004872 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873 * When writing buffer lines, drop the typed
4874 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004876 if (options & SHELL_WRITE)
4877 ta_len = 0;
4878 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4881 if (len > 0)
4882 {
4883 ta_len -= len;
4884 mch_memmove(ta_buf, ta_buf + len, ta_len);
4885 }
4886 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890 if (got_int)
4891 {
4892 /* CTRL-C sends a signal to the child, we ignore it
4893 * ourselves */
4894# ifdef HAVE_SETSID
4895 kill(-pid, SIGINT);
4896# else
4897 kill(0, SIGINT);
4898# endif
4899 if (wpid > 0)
4900 kill(wpid, SIGINT);
4901 got_int = FALSE;
4902 }
4903
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 /*
4905 * Check if the child has any characters to be printed.
4906 * Read them and write them to our window. Repeat this as
4907 * long as there is something to do, avoid the 10ms wait
4908 * for mch_inchar(), or sending typeahead characters to
4909 * the external process.
4910 * TODO: This should handle escape sequences, compatible
4911 * to some terminal (vt52?).
4912 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01004914 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004916 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 );
4923 if (len <= 0) /* end of file or error */
4924 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004925
4926 noread_cnt = 0;
4927 if (options & SHELL_READ)
4928 {
4929 /* Do NUL -> NL translation, append NL separated
4930 * lines to the current buffer. */
4931 for (i = 0; i < len; ++i)
4932 {
4933 if (buffer[i] == NL)
4934 append_ga_line(&ga);
4935 else if (buffer[i] == NUL)
4936 ga_append(&ga, NL);
4937 else
4938 ga_append(&ga, buffer[i]);
4939 }
4940 }
4941# ifdef FEAT_MBYTE
4942 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 int l;
4945
Bram Moolenaardf177f62005-02-22 08:39:57 +00004946 len += buffer_off;
4947 buffer[len] = NUL;
4948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 /* Check if the last character in buffer[] is
4950 * incomplete, keep these bytes for the next
4951 * round. */
4952 for (p = buffer; p < buffer + len; p += l)
4953 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004954 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (l == 0)
4956 l = 1; /* NUL byte? */
4957 else if (MB_BYTE2LEN(*p) != l)
4958 break;
4959 }
4960 if (p == buffer) /* no complete character */
4961 {
4962 /* avoid getting stuck at an illegal byte */
4963 if (len >= 12)
4964 ++p;
4965 else
4966 {
4967 buffer_off = len;
4968 continue;
4969 }
4970 }
4971 c = *p;
4972 *p = NUL;
4973 msg_puts(buffer);
4974 if (p < buffer + len)
4975 {
4976 *p = c;
4977 buffer_off = (buffer + len) - p;
4978 mch_memmove(buffer, p, buffer_off);
4979 continue;
4980 }
4981 buffer_off = 0;
4982 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 buffer[len] = NUL;
4987 msg_puts(buffer);
4988 }
4989
4990 windgoto(msg_row, msg_col);
4991 cursor_on();
4992 out_flush();
4993 if (got_int)
4994 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004995
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004996# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004997 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004998 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004999 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005000
5001 /* Avoid that we keep looping here without
5002 * checking for a CTRL-C for a long time. Don't
5003 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005004 if (msec > 2000)
5005 {
5006 noread_cnt = 5;
5007 break;
5008 }
5009 }
5010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005013 /* If we already detected the child has finished, continue
5014 * reading output for a short while. Some text may be
5015 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005016 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005017 {
5018 if (noread_cnt < 5)
5019 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005020 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005021 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 /*
5024 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005025 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005027# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005028 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005029# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5033 || (wait_pid == pid && WIFEXITED(status)))
5034 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005035 /* Don't break the loop yet, try reading more
5036 * characters from "fromshell_fd" first. When using
5037 * pipes there might still be something to read and
5038 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005041 else
5042 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005043
Bram Moolenaar95a51352013-03-21 22:53:50 +01005044# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005045 /* Handle any X events, e.g. serving the clipboard. */
5046 clip_update();
5047# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049finished:
5050 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005051 if (options & SHELL_READ)
5052 {
5053 if (ga.ga_len > 0)
5054 {
5055 append_ga_line(&ga);
5056 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005057 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005058 }
5059 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005060 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005061 ga_clear(&ga);
5062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 /*
5065 * Give all typeahead that wasn't used back to ui_inchar().
5066 */
5067 if (ta_len)
5068 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 State = old_State;
5070 if (toshell_fd >= 0)
5071 close(toshell_fd);
5072 close(fromshell_fd);
5073 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005074# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005075 else
5076 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005077 long delay_msec = 1;
5078
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005079 /*
5080 * Similar to the loop above, but only handle X events, no
5081 * I/O.
5082 */
5083 for (;;)
5084 {
5085 if (got_int)
5086 {
5087 /* CTRL-C sends a signal to the child, we ignore it
5088 * ourselves */
5089# ifdef HAVE_SETSID
5090 kill(-pid, SIGINT);
5091# else
5092 kill(0, SIGINT);
5093# endif
5094 got_int = FALSE;
5095 }
5096# ifdef __NeXT__
5097 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5098# else
5099 wait_pid = waitpid(pid, &status, WNOHANG);
5100# endif
5101 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5102 || (wait_pid == pid && WIFEXITED(status)))
5103 {
5104 wait_pid = pid;
5105 break;
5106 }
5107
5108 /* Handle any X events, e.g. serving the clipboard. */
5109 clip_update();
5110
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005111 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5112 * less time. */
5113 mch_delay(delay_msec, TRUE);
5114 if (++delay_msec > 10)
5115 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005116 }
5117 }
5118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
5120 /*
5121 * Wait until our child has exited.
5122 * Ignore wait() returning pids of other children and returning
5123 * because of some signal like SIGWINCH.
5124 * Don't wait if wait_pid was already set above, indicating the
5125 * child already exited.
5126 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005127 if (wait_pid != pid)
5128 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaar624891f2010-10-13 16:22:09 +02005130# ifdef FEAT_GUI
5131 /* Close slave side of pty. Only do this after the child has
5132 * exited, otherwise the child may hang when it tries to write on
5133 * the pty. */
5134 if (pty_master_fd >= 0)
5135 close(pty_slave_fd);
5136# endif
5137
Bram Moolenaardf177f62005-02-22 08:39:57 +00005138 /* Make sure the child that writes to the external program is
5139 * dead. */
5140 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005141 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005142 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005143 wait4pid(wpid, NULL);
5144 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005145
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005146# ifdef FEAT_JOB_CHANNEL
5147 --dont_check_job_ended;
5148# endif
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /*
5151 * Set to raw mode right now, otherwise a CTRL-C after
5152 * catch_signals() will kill Vim.
5153 */
5154 if (tmode == TMODE_RAW)
5155 settmode(TMODE_RAW);
5156 did_settmode = TRUE;
5157 set_signals();
5158
5159 if (WIFEXITED(status))
5160 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005161 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005163 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 if (retval == EXEC_FAILED)
5166 {
5167 MSG_PUTS(_("\nCannot execute shell "));
5168 msg_outtrans(p_sh);
5169 msg_putchar('\n');
5170 }
5171 else if (!(options & SHELL_SILENT))
5172 {
5173 MSG_PUTS(_("\nshell returned "));
5174 msg_outnum((long)retval);
5175 msg_putchar('\n');
5176 }
5177 }
5178 }
5179 else
5180 MSG_PUTS(_("\nCommand terminated\n"));
5181 }
5182 }
5183 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005184 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186error:
5187 if (!did_settmode)
5188 if (tmode == TMODE_RAW)
5189 settmode(TMODE_RAW); /* set to raw mode */
5190# ifdef FEAT_TITLE
5191 resettitle();
5192# endif
5193 vim_free(newcmd);
5194
5195 return retval;
5196
5197#endif /* USE_SYSTEM */
5198}
5199
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005200#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005201 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005202mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005203{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005204 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005205 int fd_in[2] = {-1, -1}; /* for stdin */
5206 int fd_out[2] = {-1, -1}; /* for stdout */
5207 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005208 int pty_master_fd = -1;
5209 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005210 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005211 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5212 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5213 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005214 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005215 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5216 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005217 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005218 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005219
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005220 if (use_out_for_err && use_null_for_out)
5221 use_null_for_err = TRUE;
5222
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005223 /* default is to fail */
5224 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005225
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005226 if (options->jo_pty)
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005227 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005228
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005229 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005230 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005231 if (use_file_for_in)
5232 {
5233 char_u *fname = options->jo_io_name[PART_IN];
5234
5235 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5236 if (fd_in[0] < 0)
5237 {
5238 EMSG2(_(e_notopen), fname);
5239 goto failed;
5240 }
5241 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005242 else if (!use_null_for_in && pty_master_fd < 0 && pipe(fd_in) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005243 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005244
5245 if (use_file_for_out)
5246 {
5247 char_u *fname = options->jo_io_name[PART_OUT];
5248
5249 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5250 if (fd_out[1] < 0)
5251 {
5252 EMSG2(_(e_notopen), fname);
5253 goto failed;
5254 }
5255 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005256 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005257 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005258
5259 if (use_file_for_err)
5260 {
5261 char_u *fname = options->jo_io_name[PART_ERR];
5262
5263 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5264 if (fd_err[1] < 0)
5265 {
5266 EMSG2(_(e_notopen), fname);
5267 goto failed;
5268 }
5269 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005270 else if (!use_out_for_err && !use_null_for_err
5271 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005272 goto failed;
5273
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005274 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5275 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005276 if (options->jo_set & JO_CHANNEL)
5277 {
5278 channel = options->jo_channel;
5279 if (channel != NULL)
5280 ++channel->ch_refcount;
5281 }
5282 else
5283 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005284 if (channel == NULL)
5285 goto failed;
5286 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005287
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005288 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005289 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005290 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005291 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005292 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005293 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005294 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005295 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005296 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005297 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005298 int null_fd = -1;
5299 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005300
Bram Moolenaar835dc632016-02-07 14:27:38 +01005301 /* child */
5302 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005303 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005304
5305# ifdef HAVE_SETSID
5306 /* Create our own process group, so that the child and all its
5307 * children can be kill()ed. Don't do this when using pipes,
5308 * because stdin is not a tty, we would lose /dev/tty. */
5309 (void)setsid();
5310# endif
5311
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005312# ifdef FEAT_TERMINAL
5313 if (options->jo_term_rows > 0)
5314 set_child_environment(
5315 (long)options->jo_term_rows,
5316 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005317 STRNCMP(T_NAME, "xterm", 5) == 0
5318 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005319 else
5320# endif
5321 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005322
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005323 if (options->jo_env != NULL)
5324 {
5325 dict_T *dict = options->jo_env;
5326 hashitem_T *hi;
5327 int todo = (int)dict->dv_hashtab.ht_used;
5328
5329 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5330 if (!HASHITEM_EMPTY(hi))
5331 {
5332 typval_T *item = &dict_lookup(hi)->di_tv;
5333
5334 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5335 --todo;
5336 }
5337 }
5338
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005339 if (use_null_for_in || use_null_for_out || use_null_for_err)
5340 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5341
Bram Moolenaar223896d2017-08-02 22:33:28 +02005342 if (pty_slave_fd >= 0)
5343 {
5344 /* push stream discipline modules */
5345 SetupSlavePTY(pty_slave_fd);
5346# ifdef TIOCSCTTY
5347 /* Try to become controlling tty (probably doesn't work,
5348 * unless run by root) */
5349 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5350# endif
5351 }
5352
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005353 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005354 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005355 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005356 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005357 else if (fd_in[0] < 0)
5358 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005359 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005360 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005361
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005362 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005363 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005364 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005365 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005366 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005367 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005368 }
5369 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005370 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005371 else if (fd_err[1] < 0)
5372 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005373 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005374 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005375
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005376 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005377 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005378 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005379 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005380 else if (fd_out[1] < 0)
5381 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005382 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005383 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005384
5385 if (fd_in[0] >= 0)
5386 close(fd_in[0]);
5387 if (fd_in[1] >= 0)
5388 close(fd_in[1]);
5389 if (fd_out[0] >= 0)
5390 close(fd_out[0]);
5391 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005392 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005393 if (fd_err[0] >= 0)
5394 close(fd_err[0]);
5395 if (fd_err[1] >= 0)
5396 close(fd_err[1]);
5397 if (pty_master_fd >= 0)
5398 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005399 close(pty_master_fd); /* not used in the child */
5400 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005401 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005402
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005403 if (null_fd >= 0)
5404 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005405
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005406 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5407 _exit(EXEC_FAILED);
5408
Bram Moolenaar835dc632016-02-07 14:27:38 +01005409 /* See above for type of argv. */
5410 execvp(argv[0], argv);
5411
Bram Moolenaar4694a172016-04-21 14:05:23 +02005412 if (stderr_works)
5413 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005414# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005415 /* calling free_all_mem() here causes problems. Ignore valgrind
5416 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005417# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005418 _exit(EXEC_FAILED); /* exec failed, return failure code */
5419 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005420
5421 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005422 UNBLOCK_SIGNALS(&curset);
5423
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005424 job->jv_pid = pid;
5425 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005426 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005427
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005428 if (pty_master_fd >= 0)
5429 close(pty_slave_fd); /* duped above */
5430 /* close child stdin, stdout and stderr */
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005431 if (!use_file_for_in && fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005432 close(fd_in[0]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005433 if (!use_file_for_out && fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005434 close(fd_out[1]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005435 if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005436 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005437 if (channel != NULL)
5438 {
5439 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005440 use_file_for_in || use_null_for_in
5441 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5442 use_file_for_out || use_null_for_out
5443 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5444 use_out_for_err || use_file_for_err || use_null_for_err
5445 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005446 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005447 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005448 else
5449 {
5450 if (fd_in[1] >= 0)
5451 close(fd_in[1]);
5452 if (fd_out[0] >= 0)
5453 close(fd_out[0]);
5454 if (fd_err[0] >= 0)
5455 close(fd_err[0]);
5456 if (pty_master_fd >= 0)
5457 close(pty_master_fd);
5458 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005459
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005460 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005461 return;
5462
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005463failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005464 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005465 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005466 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005467 if (fd_in[1] >= 0)
5468 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005469 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005470 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005471 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005472 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005473 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005474 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005475 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005476 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005477 if (pty_master_fd >= 0)
5478 close(pty_master_fd);
5479 if (pty_slave_fd >= 0)
5480 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005481}
5482
5483 char *
5484mch_job_status(job_T *job)
5485{
5486# ifdef HAVE_UNION_WAIT
5487 union wait status;
5488# else
5489 int status = -1;
5490# endif
5491 pid_t wait_pid = 0;
5492
5493# ifdef __NeXT__
5494 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5495# else
5496 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5497# endif
5498 if (wait_pid == -1)
5499 {
5500 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005501 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005502 }
5503 if (wait_pid == 0)
5504 return "run";
5505 if (WIFEXITED(status))
5506 {
5507 /* LINTED avoid "bitwise operation on signed value" */
5508 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005509 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005511 if (WIFSIGNALED(status))
5512 {
5513 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005514 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005515 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005516 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005517
5518return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005519 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005520 {
5521 ch_log(job->jv_channel, "Job ended");
5522 job->jv_status = JOB_ENDED;
5523 }
5524 return "dead";
5525}
5526
5527 job_T *
5528mch_detect_ended_job(job_T *job_list)
5529{
5530# ifdef HAVE_UNION_WAIT
5531 union wait status;
5532# else
5533 int status = -1;
5534# endif
5535 pid_t wait_pid = 0;
5536 job_T *job;
5537
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005538# ifndef USE_SYSTEM
5539 /* Do not do this when waiting for a shell command to finish, we would get
5540 * the exit value here (and discard it), the exit value obtained there
5541 * would then be wrong. */
5542 if (dont_check_job_ended > 0)
5543 return NULL;
5544# endif
5545
Bram Moolenaar97792de2016-10-15 18:36:49 +02005546# ifdef __NeXT__
5547 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5548# else
5549 wait_pid = waitpid(-1, &status, WNOHANG);
5550# endif
5551 if (wait_pid <= 0)
5552 /* no process ended */
5553 return NULL;
5554 for (job = job_list; job != NULL; job = job->jv_next)
5555 {
5556 if (job->jv_pid == wait_pid)
5557 {
5558 if (WIFEXITED(status))
5559 /* LINTED avoid "bitwise operation on signed value" */
5560 job->jv_exitval = WEXITSTATUS(status);
5561 else if (WIFSIGNALED(status))
5562 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005563 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005564 {
5565 ch_log(job->jv_channel, "Job ended");
5566 job->jv_status = JOB_ENDED;
5567 }
5568 return job;
5569 }
5570 }
5571 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005572}
5573
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005574/*
5575 * Send a (deadly) signal to "job".
5576 * Return FAIL if "how" is not a valid name.
5577 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005578 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005579mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005580{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005581 int sig = -1;
5582 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005583
Bram Moolenaar923d9262016-02-25 20:56:01 +01005584 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005585 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005586 else if (STRCMP(how, "hup") == 0)
5587 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005588 else if (STRCMP(how, "quit") == 0)
5589 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005590 else if (STRCMP(how, "int") == 0)
5591 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005592 else if (STRCMP(how, "kill") == 0)
5593 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005594#ifdef SIGWINCH
5595 else if (STRCMP(how, "winch") == 0)
5596 sig = SIGWINCH;
5597#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005598 else if (isdigit(*how))
5599 sig = atoi((char *)how);
5600 else
5601 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005602
Bram Moolenaar72801402016-02-09 11:37:50 +01005603 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005604 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005605#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005606 if (job_pid == getpgid(job_pid))
5607 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005608#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005609
Bram Moolenaar61a66052017-07-22 18:39:00 +02005610 /* Never kill ourselves! */
5611 if (job_pid != 0)
5612 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005613
Bram Moolenaar835dc632016-02-07 14:27:38 +01005614 return OK;
5615}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005616
5617/*
5618 * Clear the data related to "job".
5619 */
5620 void
5621mch_clear_job(job_T *job)
5622{
5623 /* call waitpid because child process may become zombie */
5624# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005625 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005626# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005627 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005628# endif
5629}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005630#endif
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632/*
5633 * Check for CTRL-C typed by reading all available characters.
5634 * In cooked mode we should get SIGINT, no need to check.
5635 */
5636 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005637mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005639 if ((curr_tmode == TMODE_RAW || force)
5640 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 fill_input_buf(FALSE);
5642}
5643
5644/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005645 * Wait "msec" msec until a character is available from the mouse, keyboard,
5646 * from inbuf[].
5647 * "msec" == -1 will block forever.
5648 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005649 * When "ignore_input" is TRUE even check for pending input when input is
5650 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005651 * "interrupted" (if not NULL) is set to TRUE when no character is available
5652 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005653 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005654 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 */
5656 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005657WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005659#ifdef FEAT_TIMERS
5660 long due_time;
5661 long remaining = msec;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005662 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005663
5664 /* When waiting very briefly don't trigger timers. */
5665 if (msec >= 0 && msec < 10L)
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005666 return WaitForCharOrMouse(msec, NULL, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005667
5668 while (msec < 0 || remaining > 0)
5669 {
5670 /* Trigger timers and then get the time in msec until the next one is
5671 * due. Wait up to that time. */
5672 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005673 if (typebuf.tb_change_cnt != tb_change_cnt)
5674 {
5675 /* timer may have used feedkeys() */
5676 return FALSE;
5677 }
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005678 if (due_time <= 0 || (msec > 0 && due_time > remaining))
5679 due_time = remaining;
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005680 if (WaitForCharOrMouse(due_time, interrupted, ignore_input))
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005681 return TRUE;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005682 if (interrupted != NULL && *interrupted)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005683 /* Nothing available, but need to return so that side effects get
5684 * handled, such as handling a message on a channel. */
5685 return FALSE;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005686 if (msec > 0)
5687 remaining -= due_time;
5688 }
5689 return FALSE;
5690#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005691 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005692#endif
5693}
5694
5695/*
5696 * Wait "msec" msec until a character is available from the mouse or keyboard
5697 * or from inbuf[].
5698 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005699 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005700 * "interrupted" (if not NULL) is set to TRUE when no character is available
5701 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005702 * When a GUI is being used, this will never get called -- webb
5703 */
5704 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005705WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005706{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#ifdef FEAT_MOUSE_GPM
5708 int gpm_process_wanted;
5709#endif
5710#ifdef FEAT_XCLIPBOARD
5711 int rest;
5712#endif
5713 int avail;
5714
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005715 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 return 1;
5717
5718#if defined(FEAT_MOUSE_DEC)
5719 /* May need to query the mouse position. */
5720 if (WantQueryMouse)
5721 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005722 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5724 }
5725#endif
5726
5727 /*
5728 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5729 * events. This is a bit complicated, because they might both be defined.
5730 */
5731#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5732# ifdef FEAT_XCLIPBOARD
5733 rest = 0;
5734 if (do_xterm_trace())
5735 rest = msec;
5736# endif
5737 do
5738 {
5739# ifdef FEAT_XCLIPBOARD
5740 if (rest != 0)
5741 {
5742 msec = XT_TRACE_DELAY;
5743 if (rest >= 0 && rest < XT_TRACE_DELAY)
5744 msec = rest;
5745 if (rest >= 0)
5746 rest -= msec;
5747 }
5748# endif
5749# ifdef FEAT_MOUSE_GPM
5750 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005751 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005752 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005754 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755# endif
5756 if (!avail)
5757 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005758 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 return 1;
5760# ifdef FEAT_XCLIPBOARD
5761 if (rest == 0 || !do_xterm_trace())
5762# endif
5763 break;
5764 }
5765 }
5766 while (FALSE
5767# ifdef FEAT_MOUSE_GPM
5768 || (gpm_process_wanted && mch_gpm_process() == 0)
5769# endif
5770# ifdef FEAT_XCLIPBOARD
5771 || (!avail && rest != 0)
5772# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005773 )
5774 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005777 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778#endif
5779 return avail;
5780}
5781
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005782#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783/*
5784 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005785 * "msec" == 0 will check for characters once.
5786 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005789 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005790 * "interrupted" (if not NULL) is set to TRUE when no character is available
5791 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793#if defined(__BEOS__)
5794 int
5795#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005796 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005798RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005801 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005802#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 static int busy = FALSE;
5804
5805 /* May retry getting characters after an event was handled. */
5806# define MAY_LOOP
5807
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005808# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 /* Remember at what time we started, so that we know how much longer we
5810 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005811 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005812 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005814 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005815 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816# endif
5817
5818 /* Handle being called recursively. This may happen for the session
5819 * manager stuff, it may save the file, which does a breakcheck. */
5820 if (busy)
5821 return 0;
5822#endif
5823
5824#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005825 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
5827 {
5828#ifdef MAY_LOOP
5829 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005830# ifdef FEAT_MZSCHEME
5831 int mzquantum_used = FALSE;
5832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
5834#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005835 /* each channel may use in, out and err */
5836 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 int nfd;
5838# ifdef FEAT_XCLIPBOARD
5839 int xterm_idx = -1;
5840# endif
5841# ifdef FEAT_MOUSE_GPM
5842 int gpm_idx = -1;
5843# endif
5844# ifdef USE_XSMP
5845 int xsmp_idx = -1;
5846# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005847 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005849# ifdef FEAT_MZSCHEME
5850 mzvim_check_threads();
5851 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5852 {
5853 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5854 mzquantum_used = TRUE;
5855 }
5856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 fds[0].fd = fd;
5858 fds[0].events = POLLIN;
5859 nfd = 1;
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005862 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (xterm_Shell != (Widget)0)
5864 {
5865 xterm_idx = nfd;
5866 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5867 fds[nfd].events = POLLIN;
5868 nfd++;
5869 }
5870# endif
5871# ifdef FEAT_MOUSE_GPM
5872 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5873 {
5874 gpm_idx = nfd;
5875 fds[nfd].fd = gpm_fd;
5876 fds[nfd].events = POLLIN;
5877 nfd++;
5878 }
5879# endif
5880# ifdef USE_XSMP
5881 if (xsmp_icefd != -1)
5882 {
5883 xsmp_idx = nfd;
5884 fds[nfd].fd = xsmp_icefd;
5885 fds[nfd].events = POLLIN;
5886 nfd++;
5887 }
5888# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005889#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005890 nfd = channel_poll_setup(nfd, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005891#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005892 if (interrupted != NULL)
5893 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005895 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005896
5897 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02005898 if (result == 0 && interrupted != NULL && ret > 0)
5899 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005900
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005901# ifdef FEAT_MZSCHEME
5902 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005903 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005904 finished = FALSE;
5905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907# ifdef FEAT_XCLIPBOARD
5908 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5909 {
5910 xterm_update(); /* Maybe we should hand out clipboard */
5911 if (--ret == 0 && !input_available())
5912 /* Try again */
5913 finished = FALSE;
5914 }
5915# endif
5916# ifdef FEAT_MOUSE_GPM
5917 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5918 {
5919 *check_for_gpm = 1;
5920 }
5921# endif
5922# ifdef USE_XSMP
5923 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5924 {
5925 if (fds[xsmp_idx].revents & POLLIN)
5926 {
5927 busy = TRUE;
5928 xsmp_handle_requests();
5929 busy = FALSE;
5930 }
5931 else if (fds[xsmp_idx].revents & POLLHUP)
5932 {
5933 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005934 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 xsmp_close();
5936 }
5937 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005938 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 }
5940# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005941#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005942 if (ret > 0)
5943 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#else /* HAVE_SELECT */
5947
5948 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005949 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005950 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005952 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005954# ifdef FEAT_MZSCHEME
5955 mzvim_check_threads();
5956 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5957 {
5958 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5959 mzquantum_used = TRUE;
5960 }
5961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005963 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005965 tv.tv_sec = towait / 1000;
5966 tv.tv_usec = (towait % 1000) * (1000000/1000);
5967 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005969 else
5970 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
5972 /*
5973 * Select on ready for reading and exceptional condition (end of file).
5974 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005975select_eintr:
5976 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005977 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 FD_ZERO(&efds);
5979 FD_SET(fd, &rfds);
5980# if !defined(__QNX__) && !defined(__CYGWIN32__)
5981 /* For QNX select() always returns 1 if this is set. Why? */
5982 FD_SET(fd, &efds);
5983# endif
5984 maxfd = fd;
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005987 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 if (xterm_Shell != (Widget)0)
5989 {
5990 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5991 if (maxfd < ConnectionNumber(xterm_dpy))
5992 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005993
5994 /* An event may have already been read but not handled. In
5995 * particulary, XFlush may cause this. */
5996 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 }
5998# endif
5999# ifdef FEAT_MOUSE_GPM
6000 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6001 {
6002 FD_SET(gpm_fd, &rfds);
6003 FD_SET(gpm_fd, &efds);
6004 if (maxfd < gpm_fd)
6005 maxfd = gpm_fd;
6006 }
6007# endif
6008# ifdef USE_XSMP
6009 if (xsmp_icefd != -1)
6010 {
6011 FD_SET(xsmp_icefd, &rfds);
6012 FD_SET(xsmp_icefd, &efds);
6013 if (maxfd < xsmp_icefd)
6014 maxfd = xsmp_icefd;
6015 }
6016# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006017# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006018 maxfd = channel_select_setup(maxfd, &rfds, &wfds);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006019# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006020 if (interrupted != NULL)
6021 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006023 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006024 result = ret > 0 && FD_ISSET(fd, &rfds);
6025 if (result)
6026 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006027 else if (interrupted != NULL && ret > 0)
6028 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006029
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006030# ifdef EINTR
6031 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006032 {
6033 /* Check whether window has been resized, EINTR may be caused by
6034 * SIGWINCH. */
6035 if (do_resize)
6036 handle_resize();
6037
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006038 /* Interrupted by a signal, need to try again. We ignore msec
6039 * here, because we do want to check even after a timeout if
6040 * characters are available. Needed for reading output of an
6041 * external command after the process has finished. */
6042 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006043 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006044# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006045# ifdef __TANDEM
6046 if (ret == -1 && errno == ENOTSUP)
6047 {
6048 FD_ZERO(&rfds);
6049 FD_ZERO(&efds);
6050 ret = 0;
6051 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006052# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006053# ifdef FEAT_MZSCHEME
6054 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006055 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006056 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057# endif
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059# ifdef FEAT_XCLIPBOARD
6060 if (ret > 0 && xterm_Shell != (Widget)0
6061 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6062 {
6063 xterm_update(); /* Maybe we should hand out clipboard */
6064 /* continue looping when we only got the X event and the input
6065 * buffer is empty */
6066 if (--ret == 0 && !input_available())
6067 {
6068 /* Try again */
6069 finished = FALSE;
6070 }
6071 }
6072# endif
6073# ifdef FEAT_MOUSE_GPM
6074 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6075 {
6076 if (FD_ISSET(gpm_fd, &efds))
6077 gpm_close();
6078 else if (FD_ISSET(gpm_fd, &rfds))
6079 *check_for_gpm = 1;
6080 }
6081# endif
6082# ifdef USE_XSMP
6083 if (ret > 0 && xsmp_icefd != -1)
6084 {
6085 if (FD_ISSET(xsmp_icefd, &efds))
6086 {
6087 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006088 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 xsmp_close();
6090 if (--ret == 0)
6091 finished = FALSE; /* keep going if event was only one */
6092 }
6093 else if (FD_ISSET(xsmp_icefd, &rfds))
6094 {
6095 busy = TRUE;
6096 xsmp_handle_requests();
6097 busy = FALSE;
6098 if (--ret == 0)
6099 finished = FALSE; /* keep going if event was only one */
6100 }
6101 }
6102# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006103#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01006104 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006105 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107
6108#endif /* HAVE_SELECT */
6109
6110#ifdef MAY_LOOP
6111 if (finished || msec == 0)
6112 break;
6113
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006114# ifdef FEAT_CLIENTSERVER
6115 if (server_waiting())
6116 break;
6117# endif
6118
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 /* We're going to loop around again, find out for how long */
6120 if (msec > 0)
6121 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006122# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006124 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125# else
6126 /* Guess we got interrupted halfway. */
6127 msec = msec / 2;
6128# endif
6129 if (msec <= 0)
6130 break; /* waited long enough */
6131 }
6132#endif
6133 }
6134
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006135 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136}
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006140 * Expand a path into all matching files and/or directories. Handles "*",
6141 * "?", "[a-z]", "**", etc.
6142 * "path" has backslashes before chars that are not to be expanded.
6143 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 */
6145 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006146mch_expandpath(
6147 garray_T *gap,
6148 char_u *path,
6149 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
Bram Moolenaar02743632005-07-25 20:42:36 +00006151 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152}
6153#endif
6154
6155/*
6156 * mch_expand_wildcards() - this code does wild-card pattern matching using
6157 * the shell
6158 *
6159 * return OK for success, FAIL for error (you may lose some memory) and put
6160 * an error message in *file.
6161 *
6162 * num_pat is number of input patterns
6163 * pat is array of pointers to input patterns
6164 * num_file is pointer to number of matched file names
6165 * file is pointer to array of pointers to matched file names
6166 */
6167
6168#ifndef SEEK_SET
6169# define SEEK_SET 0
6170#endif
6171#ifndef SEEK_END
6172# define SEEK_END 2
6173#endif
6174
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006175#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006176
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006178mch_expand_wildcards(
6179 int num_pat,
6180 char_u **pat,
6181 int *num_file,
6182 char_u ***file,
6183 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 int i;
6186 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006187 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 char_u *p;
6189 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaarc7247912008-01-13 12:54:11 +00006191 /*
6192 * This is the non-OS/2 implementation (really Unix).
6193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 int j;
6195 char_u *tempname;
6196 char_u *command;
6197 FILE *fd;
6198 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006199#define STYLE_ECHO 0 /* use "echo", the default */
6200#define STYLE_GLOB 1 /* use "glob", for csh */
6201#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6202#define STYLE_PRINT 3 /* use "print -N", for zsh */
6203#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6204 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 int shell_style = STYLE_ECHO;
6206 int check_spaces;
6207 static int did_find_nul = FALSE;
6208 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006209 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006210 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 *num_file = 0; /* default: no files found */
6213 *file = NULL;
6214
6215 /*
6216 * If there are no wildcards, just copy the names to allocated memory.
6217 * Saves a lot of time, because we don't have to start a new shell.
6218 */
6219 if (!have_wildcard(num_pat, pat))
6220 return save_patterns(num_pat, pat, num_file, file);
6221
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006222# ifdef HAVE_SANDBOX
6223 /* Don't allow any shell command in the sandbox. */
6224 if (sandbox != 0 && check_secure())
6225 return FAIL;
6226# endif
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 /*
6229 * Don't allow the use of backticks in secure and restricted mode.
6230 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006231 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 for (i = 0; i < num_pat; ++i)
6233 if (vim_strchr(pat[i], '`') != NULL
6234 && (check_restricted() || check_secure()))
6235 return FAIL;
6236
6237 /*
6238 * get a name for the temp file
6239 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006240 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
6242 EMSG(_(e_notmp));
6243 return FAIL;
6244 }
6245
6246 /*
6247 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006248 * file.
6249 * STYLE_BT: NL separated
6250 * If expanding `cmd` execute it directly.
6251 * STYLE_GLOB: NUL separated
6252 * If we use *csh, "glob" will work better than "echo".
6253 * STYLE_PRINT: NL or NUL separated
6254 * If we use *zsh, "print -N" will work better than "glob".
6255 * STYLE_VIMGLOB: NL separated
6256 * If we use *sh*, we define "vimglob()".
6257 * STYLE_ECHO: space separated.
6258 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 */
6260 if (num_pat == 1 && *pat[0] == '`'
6261 && (len = STRLEN(pat[0])) > 2
6262 && *(pat[0] + len - 1) == '`')
6263 shell_style = STYLE_BT;
6264 else if ((len = STRLEN(p_sh)) >= 3)
6265 {
6266 if (STRCMP(p_sh + len - 3, "csh") == 0)
6267 shell_style = STYLE_GLOB;
6268 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6269 shell_style = STYLE_PRINT;
6270 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006271 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6272 "sh") != NULL)
6273 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaarc7247912008-01-13 12:54:11 +00006275 /* Compute the length of the command. We need 2 extra bytes: for the
6276 * optional '&' and for the NUL.
6277 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006279 if (shell_style == STYLE_VIMGLOB)
6280 len += STRLEN(sh_vimglob_func);
6281
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006282 for (i = 0; i < num_pat; ++i)
6283 {
6284 /* Count the length of the patterns in the same way as they are put in
6285 * "command" below. */
6286#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006288#else
6289 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006290 for (j = 0; pat[i][j] != NUL; ++j)
6291 {
6292 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6293 ++len; /* may add a backslash */
6294 ++len;
6295 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006296#endif
6297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 command = alloc(len);
6299 if (command == NULL)
6300 {
6301 /* out of memory */
6302 vim_free(tempname);
6303 return FAIL;
6304 }
6305
6306 /*
6307 * Build the shell command:
6308 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6309 * recognizes this).
6310 * - Add the shell command to print the expanded names.
6311 * - Add the temp file name.
6312 * - Add the file name patterns.
6313 */
6314 if (shell_style == STYLE_BT)
6315 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006316 /* change `command; command& ` to (command; command ) */
6317 STRCPY(command, "(");
6318 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006320 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006321 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 --p;
6323 if (*p == '&') /* remove trailing '&' */
6324 {
6325 ampersent = TRUE;
6326 *p = ' ';
6327 }
6328 STRCAT(command, ">");
6329 }
6330 else
6331 {
6332 if (flags & EW_NOTFOUND)
6333 STRCPY(command, "set nonomatch; ");
6334 else
6335 STRCPY(command, "unset nonomatch; ");
6336 if (shell_style == STYLE_GLOB)
6337 STRCAT(command, "glob >");
6338 else if (shell_style == STYLE_PRINT)
6339 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006340 else if (shell_style == STYLE_VIMGLOB)
6341 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 else
6343 STRCAT(command, "echo >");
6344 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006347
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (shell_style != STYLE_BT)
6349 for (i = 0; i < num_pat; ++i)
6350 {
6351 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006352 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006353 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354#ifdef USE_SYSTEM
6355 STRCAT(command, " \"");
6356 STRCAT(command, pat[i]);
6357 STRCAT(command, "\"");
6358#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006359 int intick = FALSE;
6360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 p = command + STRLEN(command);
6362 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006363 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006364 {
6365 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006366 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006367 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6368 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006369 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006370 * backslash inside backticks, before a special character
6371 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006372 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006373 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6374 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006375 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006376 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006377 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006378 else if (!intick
6379 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6380 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006381 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006382 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6383 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006384 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006385
6386 /* Copy one character. */
6387 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 *p = NUL;
6390#endif
6391 }
6392 if (flags & EW_SILENT)
6393 show_shell_mess = FALSE;
6394 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006395 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397 /*
6398 * Using zsh -G: If a pattern has no matches, it is just deleted from
6399 * the argument list, otherwise zsh gives an error message and doesn't
6400 * expand any other pattern.
6401 */
6402 if (shell_style == STYLE_PRINT)
6403 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6404
6405 /*
6406 * If we use -f then shell variables set in .cshrc won't get expanded.
6407 * vi can do it, so we will too, but it is only necessary if there is a "$"
6408 * in one of the patterns, otherwise we can still use the fast option.
6409 */
6410 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6411 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6412
6413 /*
6414 * execute the shell command
6415 */
6416 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6417
6418 /* When running in the background, give it some time to create the temp
6419 * file, but don't wait for it to finish. */
6420 if (ampersent)
6421 mch_delay(10L, TRUE);
6422
6423 extra_shell_arg = NULL; /* cleanup */
6424 show_shell_mess = TRUE;
6425 vim_free(command);
6426
Bram Moolenaarc7247912008-01-13 12:54:11 +00006427 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
6429 mch_remove(tempname);
6430 vim_free(tempname);
6431 /*
6432 * With interactive completion, the error message is not printed.
6433 * However with USE_SYSTEM, I don't know how to turn off error messages
6434 * from the shell, so screen may still get messed up -- webb.
6435 */
6436#ifndef USE_SYSTEM
6437 if (!(flags & EW_SILENT))
6438#endif
6439 {
6440 redraw_later_clear(); /* probably messed up screen */
6441 msg_putchar('\n'); /* clear bottom line quickly */
6442 cmdline_row = Rows - 1; /* continue on last line */
6443#ifdef USE_SYSTEM
6444 if (!(flags & EW_SILENT))
6445#endif
6446 {
6447 MSG(_(e_wildexpand));
6448 msg_start(); /* don't overwrite this message */
6449 }
6450 }
6451 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6452 * EW_NOTFOUND is given */
6453 if (shell_style == STYLE_BT)
6454 return FAIL;
6455 goto notfound;
6456 }
6457
6458 /*
6459 * read the names from the file into memory
6460 */
6461 fd = fopen((char *)tempname, READBIN);
6462 if (fd == NULL)
6463 {
6464 /* Something went wrong, perhaps a file name with a special char. */
6465 if (!(flags & EW_SILENT))
6466 {
6467 MSG(_(e_wildexpand));
6468 msg_start(); /* don't overwrite this message */
6469 }
6470 vim_free(tempname);
6471 goto notfound;
6472 }
6473 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006474 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006476 if (llen < 0)
6477 /* just in case ftell() would fail */
6478 buffer = NULL;
6479 else
6480 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 if (buffer == NULL)
6482 {
6483 /* out of memory */
6484 mch_remove(tempname);
6485 vim_free(tempname);
6486 fclose(fd);
6487 return FAIL;
6488 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006489 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 i = fread((char *)buffer, 1, len, fd);
6491 fclose(fd);
6492 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006493 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 /* unexpected read error */
6496 EMSG2(_(e_notread), tempname);
6497 vim_free(tempname);
6498 vim_free(buffer);
6499 return FAIL;
6500 }
6501 vim_free(tempname);
6502
Bram Moolenaarc7247912008-01-13 12:54:11 +00006503# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6505 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006506 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6508 *p++ = buffer[i];
6509 len = p - buffer;
6510# endif
6511
6512
6513 /* file names are separated with Space */
6514 if (shell_style == STYLE_ECHO)
6515 {
6516 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6517 p = buffer;
6518 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6519 {
6520 while (*p != ' ' && *p != '\n')
6521 ++p;
6522 p = skipwhite(p); /* skip to next entry */
6523 }
6524 }
6525 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006526 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
6528 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6529 p = buffer;
6530 for (i = 0; *p != NUL; ++i) /* count number of entries */
6531 {
6532 while (*p != '\n' && *p != NUL)
6533 ++p;
6534 if (*p != NUL)
6535 ++p;
6536 p = skipwhite(p); /* skip leading white space */
6537 }
6538 }
6539 /* file names are separated with NUL */
6540 else
6541 {
6542 /*
6543 * Some versions of zsh use spaces instead of NULs to separate
6544 * results. Only do this when there is no NUL before the end of the
6545 * buffer, otherwise we would never be able to use file names with
6546 * embedded spaces when zsh does use NULs.
6547 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6548 * don't check for spaces again.
6549 */
6550 check_spaces = FALSE;
6551 if (shell_style == STYLE_PRINT && !did_find_nul)
6552 {
6553 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006554 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006555 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 did_find_nul = TRUE;
6557 else
6558 check_spaces = TRUE;
6559 }
6560
6561 /*
6562 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6563 * already is one, for STYLE_GLOB it needs to be added.
6564 */
6565 if (len && buffer[len - 1] == NUL)
6566 --len;
6567 else
6568 buffer[len] = NUL;
6569 i = 0;
6570 for (p = buffer; p < buffer + len; ++p)
6571 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6572 {
6573 ++i;
6574 *p = NUL;
6575 }
6576 if (len)
6577 ++i; /* count last entry */
6578 }
6579 if (i == 0)
6580 {
6581 /*
6582 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6583 * /bin/sh will happily expand it to nothing rather than returning an
6584 * error; and hey, it's good to check anyway -- webb.
6585 */
6586 vim_free(buffer);
6587 goto notfound;
6588 }
6589 *num_file = i;
6590 *file = (char_u **)alloc(sizeof(char_u *) * i);
6591 if (*file == NULL)
6592 {
6593 /* out of memory */
6594 vim_free(buffer);
6595 return FAIL;
6596 }
6597
6598 /*
6599 * Isolate the individual file names.
6600 */
6601 p = buffer;
6602 for (i = 0; i < *num_file; ++i)
6603 {
6604 (*file)[i] = p;
6605 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006606 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6607 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006609 while (!(shell_style == STYLE_ECHO && *p == ' ')
6610 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 ++p;
6612 if (p == buffer + len) /* last entry */
6613 *p = NUL;
6614 else
6615 {
6616 *p++ = NUL;
6617 p = skipwhite(p); /* skip to next entry */
6618 }
6619 }
6620 else /* NUL separates */
6621 {
6622 while (*p && p < buffer + len) /* skip entry */
6623 ++p;
6624 ++p; /* skip NUL */
6625 }
6626 }
6627
6628 /*
6629 * Move the file names to allocated memory.
6630 */
6631 for (j = 0, i = 0; i < *num_file; ++i)
6632 {
6633 /* Require the files to exist. Helps when using /bin/sh */
6634 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6635 continue;
6636
6637 /* check if this entry should be included */
6638 dir = (mch_isdir((*file)[i]));
6639 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6640 continue;
6641
Bram Moolenaara2031822006-03-07 22:29:51 +00006642 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006643 if (!dir && (flags & EW_EXEC)
6644 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006645 continue;
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6648 if (p)
6649 {
6650 STRCPY(p, (*file)[i]);
6651 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006652 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 (*file)[j++] = p;
6654 }
6655 }
6656 vim_free(buffer);
6657 *num_file = j;
6658
6659 if (*num_file == 0) /* rejected all entries */
6660 {
6661 vim_free(*file);
6662 *file = NULL;
6663 goto notfound;
6664 }
6665
6666 return OK;
6667
6668notfound:
6669 if (flags & EW_NOTFOUND)
6670 return save_patterns(num_pat, pat, num_file, file);
6671 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672}
6673
6674#endif /* VMS */
6675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006677save_patterns(
6678 int num_pat,
6679 char_u **pat,
6680 int *num_file,
6681 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006684 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6687 if (*file == NULL)
6688 return FAIL;
6689 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006690 {
6691 s = vim_strsave(pat[i]);
6692 if (s != NULL)
6693 /* Be compatible with expand_filename(): halve the number of
6694 * backslashes. */
6695 backslash_halve(s);
6696 (*file)[i] = s;
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 *num_file = num_pat;
6699 return OK;
6700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702/*
6703 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6704 * expand.
6705 */
6706 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006707mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006709 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 if (*p == '\\' && p[1] != NUL)
6712 ++p;
6713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (vim_strchr((char_u *)
6715#ifdef VMS
6716 "*?%"
6717#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719#endif
6720 , *p) != NULL)
6721 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723 return FALSE;
6724}
6725
6726/*
6727 * Return TRUE if the string "p" contains a wildcard.
6728 * Don't recognize '~' at the end as a wildcard.
6729 */
6730 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006731mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006733 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (*p == '\\' && p[1] != NUL)
6736 ++p;
6737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (vim_strchr((char_u *)
6739#ifdef VMS
6740 "*?%$"
6741#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743#endif
6744 , *p) != NULL
6745 || (*p == '~' && p[1] != NUL))
6746 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748 return FALSE;
6749}
6750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006752have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 int i;
6755
6756 for (i = 0; i < num; i++)
6757 if (mch_has_wildcard(file[i]))
6758 return 1;
6759 return 0;
6760}
6761
6762 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006763have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 int i;
6766
6767 for (i = 0; i < num; i++)
6768 if (vim_strchr(file[i], '$') != NULL)
6769 return TRUE;
6770 return FALSE;
6771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006773#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774/*
6775 * Scaled-down version of rename(), which is missing in Xenix.
6776 * This version can only move regular files and will fail if the
6777 * destination exists.
6778 */
6779 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006780mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
6782 struct stat st;
6783
6784 if (stat(dest, &st) >= 0) /* fail if destination exists */
6785 return -1;
6786 if (link(src, dest) != 0) /* link file to new name */
6787 return -1;
6788 if (mch_remove(src) == 0) /* delete link to old name */
6789 return 0;
6790 return -1;
6791}
6792#endif /* !HAVE_RENAME */
6793
6794#ifdef FEAT_MOUSE_GPM
6795/*
6796 * Initializes connection with gpm (if it isn't already opened)
6797 * Return 1 if succeeded (or connection already opened), 0 if failed
6798 */
6799 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006800gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6803
6804 if (!gpm_flag)
6805 {
6806 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6807 gpm_connect.defaultMask = ~GPM_HARD;
6808 /* Default handling for mouse move*/
6809 gpm_connect.minMod = 0; /* Handle any modifier keys */
6810 gpm_connect.maxMod = 0xffff;
6811 if (Gpm_Open(&gpm_connect, 0) > 0)
6812 {
6813 /* gpm library tries to handling TSTP causes
6814 * problems. Anyways, we close connection to Gpm whenever
6815 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006816 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006818# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 return 1; /* succeed */
6822 }
6823 if (gpm_fd == -2)
6824 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6825 return 0;
6826 }
6827 return 1; /* already open */
6828}
6829
6830/*
6831 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 */
6833 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006834gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
6836 if (gpm_flag && gpm_fd >= 0) /* if Open */
6837 Gpm_Close();
6838}
6839
6840/* Reads gpm event and adds special keys to input buf. Returns length of
6841 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006842 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 */
6844 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006845mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 int button;
6848 static Gpm_Event gpm_event;
6849 char_u string[6];
6850 int_u vim_modifiers;
6851 int row,col;
6852 unsigned char buttons_mask;
6853 unsigned char gpm_modifiers;
6854 static unsigned char old_buttons = 0;
6855
6856 Gpm_GetEvent(&gpm_event);
6857
6858#ifdef FEAT_GUI
6859 /* Don't put events in the input queue now. */
6860 if (hold_gui_events)
6861 return 0;
6862#endif
6863
6864 row = gpm_event.y - 1;
6865 col = gpm_event.x - 1;
6866
6867 string[0] = ESC; /* Our termcode */
6868 string[1] = 'M';
6869 string[2] = 'G';
6870 switch (GPM_BARE_EVENTS(gpm_event.type))
6871 {
6872 case GPM_DRAG:
6873 string[3] = MOUSE_DRAG;
6874 break;
6875 case GPM_DOWN:
6876 buttons_mask = gpm_event.buttons & ~old_buttons;
6877 old_buttons = gpm_event.buttons;
6878 switch (buttons_mask)
6879 {
6880 case GPM_B_LEFT:
6881 button = MOUSE_LEFT;
6882 break;
6883 case GPM_B_MIDDLE:
6884 button = MOUSE_MIDDLE;
6885 break;
6886 case GPM_B_RIGHT:
6887 button = MOUSE_RIGHT;
6888 break;
6889 default:
6890 return 0;
6891 /*Don't know what to do. Can more than one button be
6892 * reported in one event? */
6893 }
6894 string[3] = (char_u)(button | 0x20);
6895 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6896 break;
6897 case GPM_UP:
6898 string[3] = MOUSE_RELEASE;
6899 old_buttons &= ~gpm_event.buttons;
6900 break;
6901 default:
6902 return 0;
6903 }
6904 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6905 gpm_modifiers = gpm_event.modifiers;
6906 vim_modifiers = 0x0;
6907 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6908 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6909 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6910 */
6911 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6912 vim_modifiers |= MOUSE_SHIFT;
6913
6914 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6915 vim_modifiers |= MOUSE_CTRL;
6916 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6917 vim_modifiers |= MOUSE_ALT;
6918 string[3] |= vim_modifiers;
6919 string[4] = (char_u)(col + ' ' + 1);
6920 string[5] = (char_u)(row + ' ' + 1);
6921 add_to_input_buf(string, 6);
6922 return 6;
6923}
6924#endif /* FEAT_MOUSE_GPM */
6925
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006926#ifdef FEAT_SYSMOUSE
6927/*
6928 * Initialize connection with sysmouse.
6929 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6930 * output, any sysmouse output than will be processed via sig_sysmouse().
6931 * Return OK if succeeded, FAIL if failed.
6932 */
6933 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006934sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006935{
6936 struct mouse_info mouse;
6937
6938 mouse.operation = MOUSE_MODE;
6939 mouse.u.mode.mode = 0;
6940 mouse.u.mode.signal = SIGUSR2;
6941 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6942 {
6943 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6944 mouse.operation = MOUSE_SHOW;
6945 ioctl(1, CONS_MOUSECTL, &mouse);
6946 return OK;
6947 }
6948 return FAIL;
6949}
6950
6951/*
6952 * Stop processing SIGUSR2 signals, and also make sure that
6953 * virtual console do not send us any sysmouse related signal.
6954 */
6955 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006956sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006957{
6958 struct mouse_info mouse;
6959
6960 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6961 mouse.operation = MOUSE_MODE;
6962 mouse.u.mode.mode = 0;
6963 mouse.u.mode.signal = 0;
6964 ioctl(1, CONS_MOUSECTL, &mouse);
6965}
6966
6967/*
6968 * Gets info from sysmouse and adds special keys to input buf.
6969 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006970 static RETSIGTYPE
6971sig_sysmouse SIGDEFARG(sigarg)
6972{
6973 struct mouse_info mouse;
6974 struct video_info video;
6975 char_u string[6];
6976 int row, col;
6977 int button;
6978 int buttons;
6979 static int oldbuttons = 0;
6980
6981#ifdef FEAT_GUI
6982 /* Don't put events in the input queue now. */
6983 if (hold_gui_events)
6984 return;
6985#endif
6986
6987 mouse.operation = MOUSE_GETINFO;
6988 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6989 && ioctl(1, FBIO_MODEINFO, &video) != -1
6990 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6991 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6992 {
6993 row = mouse.u.data.y / video.vi_cheight;
6994 col = mouse.u.data.x / video.vi_cwidth;
6995 buttons = mouse.u.data.buttons;
6996 string[0] = ESC; /* Our termcode */
6997 string[1] = 'M';
6998 string[2] = 'S';
6999 if (oldbuttons == buttons && buttons != 0)
7000 {
7001 button = MOUSE_DRAG;
7002 }
7003 else
7004 {
7005 switch (buttons)
7006 {
7007 case 0:
7008 button = MOUSE_RELEASE;
7009 break;
7010 case 1:
7011 button = MOUSE_LEFT;
7012 break;
7013 case 2:
7014 button = MOUSE_MIDDLE;
7015 break;
7016 case 4:
7017 button = MOUSE_RIGHT;
7018 break;
7019 default:
7020 return;
7021 }
7022 oldbuttons = buttons;
7023 }
7024 string[3] = (char_u)(button);
7025 string[4] = (char_u)(col + ' ' + 1);
7026 string[5] = (char_u)(row + ' ' + 1);
7027 add_to_input_buf(string, 6);
7028 }
7029 return;
7030}
7031#endif /* FEAT_SYSMOUSE */
7032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007034typedef char_u * (*STRPROCSTR)(char_u *);
7035typedef char_u * (*INTPROCSTR)(int);
7036typedef int (*STRPROCINT)(char_u *);
7037typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
7039/*
7040 * Call a DLL routine which takes either a string or int param
7041 * and returns an allocated string.
7042 */
7043 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007044mch_libcall(
7045 char_u *libname,
7046 char_u *funcname,
7047 char_u *argstring, /* NULL when using a argint */
7048 int argint,
7049 char_u **string_result,/* NULL when using number_result */
7050 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052# if defined(USE_DLOPEN)
7053 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007054 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055# else
7056 shl_t hinstLib;
7057# endif
7058 STRPROCSTR ProcAdd;
7059 INTPROCSTR ProcAddI;
7060 char_u *retval_str = NULL;
7061 int retval_int = 0;
7062 int success = FALSE;
7063
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007064 /*
7065 * Get a handle to the DLL module.
7066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007068 /* First clear any error, it's not cleared by the dlopen() call. */
7069 (void)dlerror();
7070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 hinstLib = dlopen((char *)libname, RTLD_LAZY
7072# ifdef RTLD_LOCAL
7073 | RTLD_LOCAL
7074# endif
7075 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007076 if (hinstLib == NULL)
7077 {
7078 /* "dlerr" must be used before dlclose() */
7079 dlerr = (char *)dlerror();
7080 if (dlerr != NULL)
7081 EMSG2(_("dlerror = \"%s\""), dlerr);
7082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083# else
7084 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7085# endif
7086
7087 /* If the handle is valid, try to get the function address. */
7088 if (hinstLib != NULL)
7089 {
7090# ifdef HAVE_SETJMP_H
7091 /*
7092 * Catch a crash when calling the library function. For example when
7093 * using a number where a string pointer is expected.
7094 */
7095 mch_startjmp();
7096 if (SETJMP(lc_jump_env) != 0)
7097 {
7098 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007099# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007100 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 mch_didjmp();
7103 }
7104 else
7105# endif
7106 {
7107 retval_str = NULL;
7108 retval_int = 0;
7109
7110 if (argstring != NULL)
7111 {
7112# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007113 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007114 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115# else
7116 if (shl_findsym(&hinstLib, (const char *)funcname,
7117 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7118 ProcAdd = NULL;
7119# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007120 if ((success = (ProcAdd != NULL
7121# if defined(USE_DLOPEN)
7122 && dlerr == NULL
7123# endif
7124 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
7126 if (string_result == NULL)
7127 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7128 else
7129 retval_str = (ProcAdd)(argstring);
7130 }
7131 }
7132 else
7133 {
7134# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007135 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007136 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137# else
7138 if (shl_findsym(&hinstLib, (const char *)funcname,
7139 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7140 ProcAddI = NULL;
7141# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007142 if ((success = (ProcAddI != NULL
7143# if defined(USE_DLOPEN)
7144 && dlerr == NULL
7145# endif
7146 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
7148 if (string_result == NULL)
7149 retval_int = ((INTPROCINT)ProcAddI)(argint);
7150 else
7151 retval_str = (ProcAddI)(argint);
7152 }
7153 }
7154
7155 /* Save the string before we free the library. */
7156 /* Assume that a "1" or "-1" result is an illegal pointer. */
7157 if (string_result == NULL)
7158 *number_result = retval_int;
7159 else if (retval_str != NULL
7160 && retval_str != (char_u *)1
7161 && retval_str != (char_u *)-1)
7162 *string_result = vim_strsave(retval_str);
7163 }
7164
7165# ifdef HAVE_SETJMP_H
7166 mch_endjmp();
7167# ifdef SIGHASARG
7168 if (lc_signal != 0)
7169 {
7170 int i;
7171
7172 /* try to find the name of this signal */
7173 for (i = 0; signal_info[i].sig != -1; i++)
7174 if (lc_signal == signal_info[i].sig)
7175 break;
7176 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7177 }
7178# endif
7179# endif
7180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007182 /* "dlerr" must be used before dlclose() */
7183 if (dlerr != NULL)
7184 EMSG2(_("dlerror = \"%s\""), dlerr);
7185
7186 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 (void)dlclose(hinstLib);
7188# else
7189 (void)shl_unload(hinstLib);
7190# endif
7191 }
7192
7193 if (!success)
7194 {
7195 EMSG2(_(e_libcall), funcname);
7196 return FAIL;
7197 }
7198
7199 return OK;
7200}
7201#endif
7202
7203#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7204static int xterm_trace = -1; /* default: disabled */
7205static int xterm_button;
7206
7207/*
7208 * Setup a dummy window for X selections in a terminal.
7209 */
7210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007211setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 int z = 0;
7214 char *strp = "";
7215 Widget AppShell;
7216
7217 if (!x_connect_to_server())
7218 return;
7219
7220 open_app_context();
7221 if (app_context != NULL && xterm_Shell == (Widget)0)
7222 {
7223 int (*oldhandler)();
7224#if defined(HAVE_SETJMP_H)
7225 int (*oldIOhandler)();
7226#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007227# ifdef ELAPSED_FUNC
7228 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007231 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232# endif
7233
7234 /* Ignore X errors while opening the display */
7235 oldhandler = XSetErrorHandler(x_error_check);
7236
7237#if defined(HAVE_SETJMP_H)
7238 /* Ignore X IO errors while opening the display */
7239 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7240 mch_startjmp();
7241 if (SETJMP(lc_jump_env) != 0)
7242 {
7243 mch_didjmp();
7244 xterm_dpy = NULL;
7245 }
7246 else
7247#endif
7248 {
7249 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7250 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7251#if defined(HAVE_SETJMP_H)
7252 mch_endjmp();
7253#endif
7254 }
7255
7256#if defined(HAVE_SETJMP_H)
7257 /* Now handle X IO errors normally. */
7258 (void)XSetIOErrorHandler(oldIOhandler);
7259#endif
7260 /* Now handle X errors normally. */
7261 (void)XSetErrorHandler(oldhandler);
7262
7263 if (xterm_dpy == NULL)
7264 {
7265 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007266 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 return;
7268 }
7269
7270 /* Catch terminating error of the X server connection. */
7271 (void)XSetIOErrorHandler(x_IOerror_handler);
7272
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007273# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007275 {
7276 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007277 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007278 verbose_leave();
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280# endif
7281
7282 /* Create a Shell to make converters work. */
7283 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7284 applicationShellWidgetClass, xterm_dpy,
7285 NULL);
7286 if (AppShell == (Widget)0)
7287 return;
7288 xterm_Shell = XtVaCreatePopupShell("VIM",
7289 topLevelShellWidgetClass, AppShell,
7290 XtNmappedWhenManaged, 0,
7291 XtNwidth, 1,
7292 XtNheight, 1,
7293 NULL);
7294 if (xterm_Shell == (Widget)0)
7295 return;
7296
7297 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007298 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (x11_display == NULL)
7300 x11_display = xterm_dpy;
7301
7302 XtRealizeWidget(xterm_Shell);
7303 XSync(xterm_dpy, False);
7304 xterm_update();
7305 }
7306 if (xterm_Shell != (Widget)0)
7307 {
7308 clip_init(TRUE);
7309 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7310 x11_window = (Window)atol(strp);
7311 /* Check if $WINDOWID is valid. */
7312 if (test_x11_window(xterm_dpy) == FAIL)
7313 x11_window = 0;
7314 if (x11_window != 0)
7315 xterm_trace = 0;
7316 }
7317}
7318
7319 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007320start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7323 return;
7324 xterm_trace = 1;
7325 xterm_button = button;
7326 do_xterm_trace();
7327}
7328
7329
7330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007331stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 if (xterm_trace < 0)
7334 return;
7335 xterm_trace = 0;
7336}
7337
7338/*
7339 * Query the xterm pointer and generate mouse termcodes if necessary
7340 * return TRUE if dragging is active, else FALSE
7341 */
7342 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007343do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345 Window root, child;
7346 int root_x, root_y;
7347 int win_x, win_y;
7348 int row, col;
7349 int_u mask_return;
7350 char_u buf[50];
7351 char_u *strp;
7352 long got_hints;
7353 static char_u *mouse_code;
7354 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7355 static int prev_row = 0, prev_col = 0;
7356 static XSizeHints xterm_hints;
7357
7358 if (xterm_trace <= 0)
7359 return FALSE;
7360
7361 if (xterm_trace == 1)
7362 {
7363 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007364 * have changed recently. */
7365 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7366 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 || xterm_hints.width_inc <= 1
7368 || xterm_hints.height_inc <= 1)
7369 {
7370 xterm_trace = -1; /* Not enough data -- disable tracing */
7371 return FALSE;
7372 }
7373
7374 /* Rely on the same mouse code for the duration of this */
7375 mouse_code = find_termcode(mouse_name);
7376 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007377 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 xterm_trace = 2;
7379
7380 /* Find the offset of the chars, there might be a scrollbar on the
7381 * left of the window and/or a menu on the top (eterm etc.) */
7382 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7383 &win_x, &win_y, &mask_return);
7384 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7385 - (xterm_hints.height_inc / 2);
7386 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7387 xterm_hints.y = 2;
7388 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7389 - (xterm_hints.width_inc / 2);
7390 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7391 xterm_hints.x = 2;
7392 return TRUE;
7393 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007394 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
7396 xterm_trace = 0;
7397 return FALSE;
7398 }
7399
7400 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7401 &win_x, &win_y, &mask_return);
7402
7403 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7404 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7405 if (row == prev_row && col == prev_col)
7406 return TRUE;
7407
7408 STRCPY(buf, mouse_code);
7409 strp = buf + STRLEN(buf);
7410 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7411 *strp++ = (char_u)(col + ' ' + 1);
7412 *strp++ = (char_u)(row + ' ' + 1);
7413 *strp = 0;
7414 add_to_input_buf(buf, STRLEN(buf));
7415
7416 prev_row = row;
7417 prev_col = col;
7418 return TRUE;
7419}
7420
7421# if defined(FEAT_GUI) || defined(PROTO)
7422/*
7423 * Destroy the display, window and app_context. Required for GTK.
7424 */
7425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007426clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 if (xterm_Shell != (Widget)0)
7429 {
7430 XtDestroyWidget(xterm_Shell);
7431 xterm_Shell = (Widget)0;
7432 }
7433 if (xterm_dpy != NULL)
7434 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007435# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 /* Lesstif and Solaris crash here, lose some memory */
7437 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (x11_display == xterm_dpy)
7440 x11_display = NULL;
7441 xterm_dpy = NULL;
7442 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007443# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (app_context != (XtAppContext)NULL)
7445 {
7446 /* Lesstif and Solaris crash here, lose some memory */
7447 XtDestroyApplicationContext(app_context);
7448 app_context = (XtAppContext)NULL;
7449 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451}
7452# endif
7453
7454/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007455 * Catch up with GUI or X events.
7456 */
7457 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007458clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007459{
7460# ifdef FEAT_GUI
7461 if (gui.in_use)
7462 gui_mch_update();
7463 else
7464# endif
7465 if (xterm_Shell != (Widget)0)
7466 xterm_update();
7467}
7468
7469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 * Catch up with any queued X events. This may put keyboard input into the
7471 * input buffer, call resize call-backs, trigger timers etc. If there is
7472 * nothing in the X event queue (& no timers pending), then we return
7473 * immediately.
7474 */
7475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007476xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
7478 XEvent event;
7479
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007480 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007482 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007484 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007485 break;
7486
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007487 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007488 {
7489 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007490 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007491#ifdef FEAT_CLIENTSERVER
7492 {
7493 XPropertyEvent *e = (XPropertyEvent *)&event;
7494
7495 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007497 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007500 XtDispatchEvent(&event);
7501 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007502 else
7503 {
7504 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007505 XtAppProcessEvent(app_context, mask);
7506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
7508}
7509
7510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007511clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
7513 if (xterm_Shell != (Widget)0)
7514 return clip_x11_own_selection(xterm_Shell, cbd);
7515 return FAIL;
7516}
7517
7518 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007519clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 if (xterm_Shell != (Widget)0)
7522 clip_x11_lose_selection(xterm_Shell, cbd);
7523}
7524
7525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007526clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
7528 if (xterm_Shell != (Widget)0)
7529 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7530}
7531
7532 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007533clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
7535 clip_x11_set_selection(cbd);
7536}
7537#endif
7538
7539
7540#if defined(USE_XSMP) || defined(PROTO)
7541/*
7542 * Code for X Session Management Protocol.
7543 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007544static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7545static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7546static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7547static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7548static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550
7551# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007552static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554/*
7555 * This is our chance to ask the user if they want to save,
7556 * or abort the logout
7557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007559xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
7561 cmdmod_T save_cmdmod;
7562 int cancel_shutdown = False;
7563
7564 save_cmdmod = cmdmod;
7565 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007566 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 /* Mustn't logout */
7568 cancel_shutdown = True;
7569 cmdmod = save_cmdmod;
7570 setcursor(); /* position cursor */
7571 out_flush();
7572
7573 /* Done interaction */
7574 SmcInteractDone(smc_conn, cancel_shutdown);
7575
7576 /* Finish off
7577 * Only end save-yourself here if we're not cancelling shutdown;
7578 * we'll get a cancelled callback later in which we'll end it.
7579 * Hopefully get around glitchy SMs (like GNOME-1)
7580 */
7581 if (!cancel_shutdown)
7582 {
7583 xsmp.save_yourself = False;
7584 SmcSaveYourselfDone(smc_conn, True);
7585 }
7586}
7587# endif
7588
7589/*
7590 * Callback that starts save-yourself.
7591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007593xsmp_handle_save_yourself(
7594 SmcConn smc_conn,
7595 SmPointer client_data UNUSED,
7596 int save_type UNUSED,
7597 Bool shutdown,
7598 int interact_style UNUSED,
7599 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 /* Handle already being in saveyourself */
7602 if (xsmp.save_yourself)
7603 SmcSaveYourselfDone(smc_conn, True);
7604 xsmp.save_yourself = True;
7605 xsmp.shutdown = shutdown;
7606
7607 /* First up, preserve all files */
7608 out_flush();
7609 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7610
7611 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007612 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
7614# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7615 /* Now see if we can ask about unsaved files */
7616 if (shutdown && !fast && gui.in_use)
7617 /* Need to interact with user, but need SM's permission */
7618 SmcInteractRequest(smc_conn, SmDialogError,
7619 xsmp_handle_interaction, client_data);
7620 else
7621# endif
7622 {
7623 /* Can stop the cycle here */
7624 SmcSaveYourselfDone(smc_conn, True);
7625 xsmp.save_yourself = False;
7626 }
7627}
7628
7629
7630/*
7631 * Callback to warn us of imminent death.
7632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007634xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 xsmp_close();
7637
7638 /* quit quickly leaving swapfiles for modified buffers behind */
7639 getout_preserve_modified(0);
7640}
7641
7642
7643/*
7644 * Callback to tell us that save-yourself has completed.
7645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007647xsmp_save_complete(
7648 SmcConn smc_conn UNUSED,
7649 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651 xsmp.save_yourself = False;
7652}
7653
7654
7655/*
7656 * Callback to tell us that an instigated shutdown was cancelled
7657 * (maybe even by us)
7658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007660xsmp_shutdown_cancelled(
7661 SmcConn smc_conn,
7662 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 if (xsmp.save_yourself)
7665 SmcSaveYourselfDone(smc_conn, True);
7666 xsmp.save_yourself = False;
7667 xsmp.shutdown = False;
7668}
7669
7670
7671/*
7672 * Callback to tell us that a new ICE connection has been established.
7673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007675xsmp_ice_connection(
7676 IceConn iceConn,
7677 IcePointer clientData UNUSED,
7678 Bool opening,
7679 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
7681 /* Intercept creation of ICE connection fd */
7682 if (opening)
7683 {
7684 xsmp_icefd = IceConnectionNumber(iceConn);
7685 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7686 }
7687}
7688
7689
7690/* Handle any ICE processing that's required; return FAIL if SM lost */
7691 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007692xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 Bool rep;
7695
7696 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7697 == IceProcessMessagesIOError)
7698 {
7699 /* Lost ICE */
7700 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007701 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 xsmp_close();
7703 return FAIL;
7704 }
7705 else
7706 return OK;
7707}
7708
7709static int dummy;
7710
7711/* Set up X Session Management Protocol */
7712 void
7713xsmp_init(void)
7714{
7715 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 SmcCallbacks smcallbacks;
7717#if 0
7718 SmPropValue smname;
7719 SmProp smnameprop;
7720 SmProp *smprops[1];
7721#endif
7722
7723 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007724 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726 xsmp.save_yourself = xsmp.shutdown = False;
7727
7728 /* Set up SM callbacks - must have all, even if they're not used */
7729 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7730 smcallbacks.save_yourself.client_data = NULL;
7731 smcallbacks.die.callback = xsmp_die;
7732 smcallbacks.die.client_data = NULL;
7733 smcallbacks.save_complete.callback = xsmp_save_complete;
7734 smcallbacks.save_complete.client_data = NULL;
7735 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7736 smcallbacks.shutdown_cancelled.client_data = NULL;
7737
7738 /* Set up a watch on ICE connection creations. The "dummy" argument is
7739 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7740 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7741 {
7742 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007743 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 return;
7745 }
7746
7747 /* Create an SM connection */
7748 xsmp.smcconn = SmcOpenConnection(
7749 NULL,
7750 NULL,
7751 SmProtoMajor,
7752 SmProtoMinor,
7753 SmcSaveYourselfProcMask | SmcDieProcMask
7754 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7755 &smcallbacks,
7756 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007757 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 sizeof(errorstring),
7759 errorstring);
7760 if (xsmp.smcconn == NULL)
7761 {
7762 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007763
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007765 {
7766 vim_snprintf(errorreport, sizeof(errorreport),
7767 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7768 verb_msg((char_u *)errorreport);
7769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 return;
7771 }
7772 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7773
7774#if 0
7775 /* ID ourselves */
7776 smname.value = "vim";
7777 smname.length = 3;
7778 smnameprop.name = "SmProgram";
7779 smnameprop.type = "SmARRAY8";
7780 smnameprop.num_vals = 1;
7781 smnameprop.vals = &smname;
7782
7783 smprops[0] = &smnameprop;
7784 SmcSetProperties(xsmp.smcconn, 1, smprops);
7785#endif
7786}
7787
7788
7789/* Shut down XSMP comms. */
7790 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007791xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 if (xsmp_icefd != -1)
7794 {
7795 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007796 if (xsmp.clientid != NULL)
7797 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007798 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 xsmp_icefd = -1;
7800 }
7801}
7802#endif /* USE_XSMP */
7803
7804
7805#ifdef EBCDIC
7806/* Translate character to its CTRL- value */
7807char CtrlTable[] =
7808{
7809/* 00 - 5E */
7810 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7811 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7812 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7813 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7814 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7815 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7816/* ^ */ 0x1E,
7817/* - */ 0x1F,
7818/* 61 - 6C */
7819 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7820/* _ */ 0x1F,
7821/* 6E - 80 */
7822 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7823/* a */ 0x01,
7824/* b */ 0x02,
7825/* c */ 0x03,
7826/* d */ 0x37,
7827/* e */ 0x2D,
7828/* f */ 0x2E,
7829/* g */ 0x2F,
7830/* h */ 0x16,
7831/* i */ 0x05,
7832/* 8A - 90 */
7833 0, 0, 0, 0, 0, 0, 0,
7834/* j */ 0x15,
7835/* k */ 0x0B,
7836/* l */ 0x0C,
7837/* m */ 0x0D,
7838/* n */ 0x0E,
7839/* o */ 0x0F,
7840/* p */ 0x10,
7841/* q */ 0x11,
7842/* r */ 0x12,
7843/* 9A - A1 */
7844 0, 0, 0, 0, 0, 0, 0, 0,
7845/* s */ 0x13,
7846/* t */ 0x3C,
7847/* u */ 0x3D,
7848/* v */ 0x32,
7849/* w */ 0x26,
7850/* x */ 0x18,
7851/* y */ 0x19,
7852/* z */ 0x3F,
7853/* AA - AC */
7854 0, 0, 0,
7855/* [ */ 0x27,
7856/* AE - BC */
7857 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7858/* ] */ 0x1D,
7859/* BE - C0 */ 0, 0, 0,
7860/* A */ 0x01,
7861/* B */ 0x02,
7862/* C */ 0x03,
7863/* D */ 0x37,
7864/* E */ 0x2D,
7865/* F */ 0x2E,
7866/* G */ 0x2F,
7867/* H */ 0x16,
7868/* I */ 0x05,
7869/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7870/* J */ 0x15,
7871/* K */ 0x0B,
7872/* L */ 0x0C,
7873/* M */ 0x0D,
7874/* N */ 0x0E,
7875/* O */ 0x0F,
7876/* P */ 0x10,
7877/* Q */ 0x11,
7878/* R */ 0x12,
7879/* DA - DF */ 0, 0, 0, 0, 0, 0,
7880/* \ */ 0x1C,
7881/* E1 */ 0,
7882/* S */ 0x13,
7883/* T */ 0x3C,
7884/* U */ 0x3D,
7885/* V */ 0x32,
7886/* W */ 0x26,
7887/* X */ 0x18,
7888/* Y */ 0x19,
7889/* Z */ 0x3F,
7890/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7891 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7892};
7893
7894char MetaCharTable[]=
7895{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7896 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7897 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7898 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7899 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7900};
7901
7902
7903/* TODO: Use characters NOT numbers!!! */
7904char CtrlCharTable[]=
7905{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7906 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7907 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7908 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7909 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7910};
7911
7912
7913#endif