blob: ff7661833f73108735f8b1b2be66d7cbab8a521a [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 Moolenaarcda77642016-06-04 13:32:35 +0200178static int WaitForChar(long msec, int *interrupted);
179static int WaitForCharOrMouse(long msec, int *interrupted);
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 Moolenaar01688ad2016-10-27 20:00:07 +0200484 if (WaitForChar(wait_time, &interrupted))
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 Moolenaarcda77642016-06-04 13:32:35 +0200539 return WaitForChar(0L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541
542#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
543# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000544# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545# endif
546# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
547# include <sys/sysctl.h>
548# endif
549# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
550# include <sys/sysinfo.h>
551# endif
552
553/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000554 * Return total amount of memory available in Kbyte.
555 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100558mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000561 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200563# ifdef HAVE_SYSCTL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 int mib[2], physmem;
565 size_t len;
566
567 /* BSD way of getting the amount of RAM available. */
568 mib[0] = CTL_HW;
569 mib[1] = HW_USERMEM;
570 len = sizeof(physmem);
571 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
572 mem = (long_u)physmem;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200575# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (mem == 0)
577 {
578 struct sysinfo sinfo;
579
580 /* Linux way of getting amount of RAM available */
581 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000582 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200583# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000584 /* avoid overflow as much as possible */
585 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
586 {
587 sinfo.mem_unit = sinfo.mem_unit >> 1;
588 --shiftright;
589 }
590 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200591# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200593# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200598# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (mem == 0)
600 {
601 long pagesize, pagecount;
602
603 /* Solaris way of getting amount of RAM available */
604 pagesize = sysconf(_SC_PAGESIZE);
605 pagecount = sysconf(_SC_PHYS_PAGES);
606 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000607 {
608 /* avoid overflow as much as possible */
609 while (shiftright > 0 && (pagesize & 1) == 0)
610 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000611 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000612 --shiftright;
613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 /* Return the minimum of the physical memory and the user limit, because
620 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200621# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {
623 struct rlimit rlp;
624
625 if (getrlimit(RLIMIT_DATA, &rlp) == 0
626 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200627# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200629# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000630 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000632 {
633 mem = (long_u)rlp.rlim_cur;
634 shiftright = 10;
635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000640 return mem >> shiftright;
641 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642}
643#endif
644
645 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100646mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000649#ifdef FEAT_MZSCHEME
650 long total = msec; /* remember original value */
651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 if (ignoreinput)
654 {
655 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000656 * here. But we don't want QUIT to kill us (CTRL-\ used in a
657 * shell may produce SIGQUIT). */
658 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 old_tmode = curr_tmode;
660 if (curr_tmode == TMODE_RAW)
661 settmode(TMODE_SLEEP);
662
663 /*
664 * Everybody sleeps in a different way...
665 * Prefer nanosleep(), some versions of usleep() can only sleep up to
666 * one second.
667 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000668#ifdef FEAT_MZSCHEME
669 do
670 {
671 /* if total is large enough, wait by portions in p_mzq */
672 if (total > p_mzq)
673 msec = p_mzq;
674 else
675 msec = total;
676 total -= msec;
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef HAVE_NANOSLEEP
679 {
680 struct timespec ts;
681
682 ts.tv_sec = msec / 1000;
683 ts.tv_nsec = (msec % 1000) * 1000000;
684 (void)nanosleep(&ts, NULL);
685 }
686#else
687# ifdef HAVE_USLEEP
688 while (msec >= 1000)
689 {
690 usleep((unsigned int)(999 * 1000));
691 msec -= 999;
692 }
693 usleep((unsigned int)(msec * 1000));
694# else
695# ifndef HAVE_SELECT
696 poll(NULL, 0, (int)msec);
697# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 struct timeval tv;
700
701 tv.tv_sec = msec / 1000;
702 tv.tv_usec = (msec % 1000) * 1000;
703 /*
704 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
705 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
706 */
707 select(0, NULL, NULL, NULL, &tv);
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709# endif /* HAVE_SELECT */
710# endif /* HAVE_NANOSLEEP */
711#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000712#ifdef FEAT_MZSCHEME
713 }
714 while (total > 0);
715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
717 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000718 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720 else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200721 WaitForChar(msec, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000724#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
726# define HAVE_CHECK_STACK_GROWTH
727/*
728 * Support for checking for an almost-out-of-stack-space situation.
729 */
730
731/*
732 * Return a pointer to an item on the stack. Used to find out if the stack
733 * grows up or down.
734 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100735static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736static int stack_grows_downwards;
737
738/*
739 * Find out if the stack grows upwards or downwards.
740 * "p" points to a variable on the stack of the caller.
741 */
742 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100743check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 int i;
746
747 stack_grows_downwards = (p > (char *)&i);
748}
749#endif
750
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000751#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752static char *stack_limit = NULL;
753
754#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
755# include <pthread.h>
756# include <pthread_np.h>
757#endif
758
759/*
760 * Find out until how var the stack can grow without getting into trouble.
761 * Called when starting up and when switching to the signal stack in
762 * deathtrap().
763 */
764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100765get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
767 struct rlimit rlp;
768 int i;
769 long lim;
770
771 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
772 * limit doesn't fit in a long (rlim_cur might be "long long"). */
773 if (getrlimit(RLIMIT_STACK, &rlp) == 0
774 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
775# ifdef RLIM_INFINITY
776 && rlp.rlim_cur != RLIM_INFINITY
777# endif
778 )
779 {
780 lim = (long)rlp.rlim_cur;
781#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
782 {
783 pthread_attr_t attr;
784 size_t size;
785
786 /* On FreeBSD the initial thread always has a fixed stack size, no
787 * matter what the limits are set to. Normally it's 1 Mbyte. */
788 pthread_attr_init(&attr);
789 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
790 {
791 pthread_attr_getstacksize(&attr, &size);
792 if (lim > (long)size)
793 lim = (long)size;
794 }
795 pthread_attr_destroy(&attr);
796 }
797#endif
798 if (stack_grows_downwards)
799 {
800 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
801 if (stack_limit >= (char *)&i)
802 /* overflow, set to 1/16 of current stack position */
803 stack_limit = (char *)((long)&i / 16L);
804 }
805 else
806 {
807 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
808 if (stack_limit <= (char *)&i)
809 stack_limit = NULL; /* overflow */
810 }
811 }
812}
813
814/*
815 * Return FAIL when running out of stack space.
816 * "p" must point to any variable local to the caller that's on the stack.
817 */
818 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100819mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 if (stack_limit != NULL)
822 {
823 if (stack_grows_downwards)
824 {
825 if (p < stack_limit)
826 return FAIL;
827 }
828 else if (p > stack_limit)
829 return FAIL;
830 }
831 return OK;
832}
833#endif
834
835#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
836/*
837 * Support for using the signal stack.
838 * This helps when we run out of stack space, which causes a SIGSEGV. The
839 * signal handler then must run on another stack, since the normal stack is
840 * completely full.
841 */
842
Bram Moolenaar39766a72013-11-03 00:41:00 +0100843#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100844# include <AvailabilityMacros.h>
845#endif
846
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifndef SIGSTKSZ
848# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
849#endif
850
851# ifdef HAVE_SIGALTSTACK
852static stack_t sigstk; /* for sigaltstack() */
853# else
854static struct sigstack sigstk; /* for sigstack() */
855# endif
856
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100857static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858static char *signal_stack;
859
860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100861init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 if (signal_stack != NULL)
864 {
865# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000866# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
867 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
869 * "struct sigaltstack" needs to be declared. */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100870 extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# endif
872
873# ifdef HAVE_SS_BASE
874 sigstk.ss_base = signal_stack;
875# else
876 sigstk.ss_sp = signal_stack;
877# endif
878 sigstk.ss_size = SIGSTKSZ;
879 sigstk.ss_flags = 0;
880 (void)sigaltstack(&sigstk, NULL);
881# else
882 sigstk.ss_sp = signal_stack;
883 if (stack_grows_downwards)
884 sigstk.ss_sp += SIGSTKSZ - 1;
885 sigstk.ss_onstack = 0;
886 (void)sigstack(&sigstk, NULL);
887# endif
888 }
889}
890#endif
891
892/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000893 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * will barf when the second argument to signal() is ``wrong''.
895 * Let me try it with a few tricky defines from my own osdef.h (jw).
896 */
897#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 static RETSIGTYPE
899sig_winch SIGDEFARG(sigarg)
900{
901 /* this is not required on all systems, but it doesn't hurt anybody */
902 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
903 do_resize = TRUE;
904 SIGRETURN;
905}
906#endif
907
908#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 static RETSIGTYPE
910catch_sigint SIGDEFARG(sigarg)
911{
912 /* this is not required on all systems, but it doesn't hurt anybody */
913 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
914 got_int = TRUE;
915 SIGRETURN;
916}
917#endif
918
919#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 static RETSIGTYPE
921catch_sigpwr SIGDEFARG(sigarg)
922{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000923 /* this is not required on all systems, but it doesn't hurt anybody */
924 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 /*
926 * I'm not sure we get the SIGPWR signal when the system is really going
927 * down or when the batteries are almost empty. Just preserve the swap
928 * files and don't exit, that can't do any harm.
929 */
930 ml_sync_all(FALSE, FALSE);
931 SIGRETURN;
932}
933#endif
934
935#ifdef SET_SIG_ALARM
936/*
937 * signal function for alarm().
938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 static RETSIGTYPE
940sig_alarm SIGDEFARG(sigarg)
941{
942 /* doesn't do anything, just to break a system call */
943 sig_alarm_called = TRUE;
944 SIGRETURN;
945}
946#endif
947
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000948#if (defined(HAVE_SETJMP_H) \
949 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
950 || defined(FEAT_LIBCALL))) \
951 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952/*
953 * A simplistic version of setjmp() that only allows one level of using.
954 * Don't call twice before calling mch_endjmp()!.
955 * Usage:
956 * mch_startjmp();
957 * if (SETJMP(lc_jump_env) != 0)
958 * {
959 * mch_didjmp();
960 * EMSG("crash!");
961 * }
962 * else
963 * {
964 * do_the_work;
965 * mch_endjmp();
966 * }
967 * Note: Can't move SETJMP() here, because a function calling setjmp() must
968 * not return before the saved environment is used.
969 * Returns OK for normal return, FAIL when the protected code caused a
970 * problem and LONGJMP() was used.
971 */
972 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100973mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
975#ifdef SIGHASARG
976 lc_signal = 0;
977#endif
978 lc_active = TRUE;
979}
980
981 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100982mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 lc_active = FALSE;
985}
986
987 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100988mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
991 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
992 * otherwise catching the signal only works once. */
993 init_signal_stack();
994# endif
995}
996#endif
997
998/*
999 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001000 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001002 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1003 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
1005 static RETSIGTYPE
1006deathtrap SIGDEFARG(sigarg)
1007{
1008 static int entered = 0; /* count the number of times we got here.
1009 Note: when memory has been corrupted
1010 this may get an arbitrary value! */
1011#ifdef SIGHASARG
1012 int i;
1013#endif
1014
1015#if defined(HAVE_SETJMP_H)
1016 /*
1017 * Catch a crash in protected code.
1018 * Restores the environment saved in lc_jump_env, which looks like
1019 * SETJMP() returns 1.
1020 */
1021 if (lc_active)
1022 {
1023# if defined(SIGHASARG)
1024 lc_signal = sigarg;
1025# endif
1026 lc_active = FALSE; /* don't jump again */
1027 LONGJMP(lc_jump_env, 1);
1028 /* NOTREACHED */
1029 }
1030#endif
1031
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001032#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001033# ifdef SIGQUIT
1034 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1035 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1036 * pressing CTRL-\, but we don't want Vim to exit then. */
1037 if (in_mch_delay && sigarg == SIGQUIT)
1038 SIGRETURN;
1039# endif
1040
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001041 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1042 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1043 * free(). Calling free() again may then cause a crash. */
1044 if (entered == 0
1045 && (0
1046# ifdef SIGHUP
1047 || sigarg == SIGHUP
1048# endif
1049# ifdef SIGQUIT
1050 || sigarg == SIGQUIT
1051# endif
1052# ifdef SIGTERM
1053 || sigarg == SIGTERM
1054# endif
1055# ifdef SIGPWR
1056 || sigarg == SIGPWR
1057# endif
1058# ifdef SIGUSR1
1059 || sigarg == SIGUSR1
1060# endif
1061# ifdef SIGUSR2
1062 || sigarg == SIGUSR2
1063# endif
1064 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001065 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001066 SIGRETURN;
1067#endif
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 /* Remember how often we have been called. */
1070 ++entered;
1071
Bram Moolenaare429e702016-06-10 19:49:14 +02001072#ifdef FEAT_AUTOCMD
1073 /* Executing autocommands is likely to use more stack space than we have
1074 * available in the signal stack. */
1075 block_autocmds();
1076#endif
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#ifdef FEAT_EVAL
1079 /* Set the v:dying variable. */
1080 set_vim_var_nr(VV_DYING, (long)entered);
1081#endif
1082
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001083#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 /* Since we are now using the signal stack, need to reset the stack
1085 * limit. Otherwise using a regexp will fail. */
1086 get_stack_limit();
1087#endif
1088
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001089#if 0
1090 /* This is for opening gdb the moment Vim crashes.
1091 * You need to manually adjust the file name and Vim executable name.
1092 * Suggested by SungHyun Nam. */
1093 {
1094# define VI_GDB_FILE "/tmp/vimgdb"
1095# define VIM_NAME "/usr/bin/vim"
1096 FILE *fp = fopen(VI_GDB_FILE, "w");
1097 if (fp)
1098 {
1099 fprintf(fp,
1100 "file %s\n"
1101 "attach %d\n"
1102 "set height 1000\n"
1103 "bt full\n"
1104 , VIM_NAME, getpid());
1105 fclose(fp);
1106 system("xterm -e gdb -x "VI_GDB_FILE);
1107 unlink(VI_GDB_FILE);
1108 }
1109 }
1110#endif
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#ifdef SIGHASARG
1113 /* try to find the name of this signal */
1114 for (i = 0; signal_info[i].sig != -1; i++)
1115 if (sigarg == signal_info[i].sig)
1116 break;
1117 deadly_signal = sigarg;
1118#endif
1119
1120 full_screen = FALSE; /* don't write message to the GUI, it might be
1121 * part of the problem... */
1122 /*
1123 * If something goes wrong after entering here, we may get here again.
1124 * When this happens, give a message and try to exit nicely (resetting the
1125 * terminal mode, etc.)
1126 * When this happens twice, just exit, don't even try to give a message,
1127 * stack may be corrupt or something weird.
1128 * When this still happens again (or memory was corrupted in such a way
1129 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1130 */
1131 if (entered >= 3)
1132 {
1133 reset_signals(); /* don't catch any signals anymore */
1134 may_core_dump();
1135 if (entered >= 4)
1136 _exit(8);
1137 exit(7);
1138 }
1139 if (entered == 2)
1140 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001141 /* No translation, it may call malloc(). */
1142 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 out_flush();
1144 getout(1);
1145 }
1146
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001147 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001149 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 signal_info[i].name);
1151#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001152 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001154
1155 /* Preserve files and exit. This sets the really_exiting flag to prevent
1156 * calling free(). */
1157 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaare429e702016-06-10 19:49:14 +02001159 /* NOTREACHED */
1160
Bram Moolenaar009b2592004-10-24 19:18:58 +00001161#ifdef NBDEBUG
1162 reset_signals();
1163 may_core_dump();
1164 abort();
1165#endif
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 SIGRETURN;
1168}
1169
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001170#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171/*
1172 * On Solaris with multi-threading, suspending might not work immediately.
1173 * Catch the SIGCONT signal, which will be used as an indication whether the
1174 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001175 *
1176 * On Linux, signal is not always handled immediately either.
1177 * See https://bugs.launchpad.net/bugs/291373
1178 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001179 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001181static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001182static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
1184/*
1185 * signal handler for SIGCONT
1186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 static RETSIGTYPE
1188sigcont_handler SIGDEFARG(sigarg)
1189{
1190 sigcont_received = TRUE;
1191 SIGRETURN;
1192}
1193#endif
1194
Bram Moolenaar62b42182010-09-21 22:09:37 +02001195# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001196static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001197# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001198static void save_clipboard(void);
1199static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001200
1201static void *clip_star_save = NULL;
1202static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001203# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001204
1205/*
1206 * Called when Vim is going to sleep or execute a shell command.
1207 * We can't respond to requests for the X selections. Lose them, otherwise
1208 * other applications will hang. But first copy the text to cut buffer 0.
1209 */
1210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001211loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001212{
1213 if (clip_star.owned || clip_plus.owned)
1214 {
1215 x11_export_final_selection();
1216 if (clip_star.owned)
1217 clip_lose_selection(&clip_star);
1218 if (clip_plus.owned)
1219 clip_lose_selection(&clip_plus);
1220 if (x11_display != NULL)
1221 XFlush(x11_display);
1222 }
1223}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001224
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001225# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001226/*
1227 * Save clipboard text to restore later.
1228 */
1229 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001230save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001231{
1232 if (clip_star.owned)
1233 clip_star_save = get_register('*', TRUE);
1234 if (clip_plus.owned)
1235 clip_plus_save = get_register('+', TRUE);
1236}
1237
1238/*
1239 * Restore clipboard text if no one own the X selection.
1240 */
1241 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001242restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001243{
1244 if (clip_star_save != NULL)
1245 {
1246 if (!clip_gen_owner_exists(&clip_star))
1247 put_register('*', clip_star_save);
1248 else
1249 free_register(clip_star_save);
1250 clip_star_save = NULL;
1251 }
1252 if (clip_plus_save != NULL)
1253 {
1254 if (!clip_gen_owner_exists(&clip_plus))
1255 put_register('+', clip_plus_save);
1256 else
1257 free_register(clip_plus_save);
1258 clip_plus_save = NULL;
1259 }
1260}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001261# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001262#endif
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264/*
1265 * If the machine has job control, use it to suspend the program,
1266 * otherwise fake it by starting a new shell.
1267 */
1268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001269mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 /* BeOS does have SIGTSTP, but it doesn't work. */
1272#if defined(SIGTSTP) && !defined(__BEOS__)
1273 out_flush(); /* needed to make cursor visible on some systems */
1274 settmode(TMODE_COOK);
1275 out_flush(); /* needed to disable mouse on some systems */
1276
1277# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001278 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279# endif
1280
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001281# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 sigcont_received = FALSE;
1283# endif
1284 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001285# if defined(_REENTRANT) && defined(SIGCONT)
1286 /*
1287 * Wait for the SIGCONT signal to be handled. It generally happens
1288 * immediately, but somehow not all the time. Do not call pause()
1289 * because there would be race condition which would hang Vim if
1290 * signal happened in between the test of sigcont_received and the
1291 * call to pause(). If signal is not yet received, call sleep(0)
1292 * to just yield CPU. Signal should then be received. If somehow
1293 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1294 * further if signal is not received after 1+2+3+4 ms (not expected
1295 * to happen).
1296 */
1297 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001298 long wait_time;
1299 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001300 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001301 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303# endif
1304
1305# ifdef FEAT_TITLE
1306 /*
1307 * Set oldtitle to NULL, so the current title is obtained again.
1308 */
1309 vim_free(oldtitle);
1310 oldtitle = NULL;
1311# endif
1312 settmode(TMODE_RAW);
1313 need_check_timestamps = TRUE;
1314 did_check_timestamps = FALSE;
1315#else
1316 suspend_shell();
1317#endif
1318}
1319
1320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001321mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 Columns = 80;
1324 Rows = 24;
1325
1326 out_flush();
1327 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001328
Bram Moolenaar56718732006-03-15 22:53:57 +00001329#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001330 mac_conv_init();
1331#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001332#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1333 win_clip_init();
1334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335}
1336
1337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001338set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340#if defined(SIGWINCH)
1341 /*
1342 * WINDOW CHANGE signal is handled with sig_winch().
1343 */
1344 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1345#endif
1346
1347 /*
1348 * We want the STOP signal to work, to make mch_suspend() work.
1349 * For "rvim" the STOP signal is ignored.
1350 */
1351#ifdef SIGTSTP
1352 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1353#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001354#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 signal(SIGCONT, sigcont_handler);
1356#endif
1357
1358 /*
1359 * We want to ignore breaking of PIPEs.
1360 */
1361#ifdef SIGPIPE
1362 signal(SIGPIPE, SIG_IGN);
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001366 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#endif
1368
1369 /*
1370 * Ignore alarm signals (Perl's alarm() generates it).
1371 */
1372#ifdef SIGALRM
1373 signal(SIGALRM, SIG_IGN);
1374#endif
1375
1376 /*
1377 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1378 * work will be lost.
1379 */
1380#ifdef SIGPWR
1381 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1382#endif
1383
1384 /*
1385 * Arrange for other signals to gracefully shutdown Vim.
1386 */
1387 catch_signals(deathtrap, SIG_ERR);
1388
1389#if defined(FEAT_GUI) && defined(SIGHUP)
1390 /*
1391 * When the GUI is running, ignore the hangup signal.
1392 */
1393 if (gui.in_use)
1394 signal(SIGHUP, SIG_IGN);
1395#endif
1396}
1397
Bram Moolenaardf177f62005-02-22 08:39:57 +00001398#if defined(SIGINT) || defined(PROTO)
1399/*
1400 * Catch CTRL-C (only works while in Cooked mode).
1401 */
1402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001403catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001404{
1405 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1406}
1407#endif
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001410reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001413#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /* SIGCONT isn't in the list, because its default action is ignore */
1415 signal(SIGCONT, SIG_DFL);
1416#endif
1417}
1418
1419 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001420catch_signals(
1421 RETSIGTYPE (*func_deadly)(),
1422 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 int i;
1425
1426 for (i = 0; signal_info[i].sig != -1; i++)
1427 if (signal_info[i].deadly)
1428 {
1429#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1430 struct sigaction sa;
1431
1432 /* Setup to use the alternate stack for the signal function. */
1433 sa.sa_handler = func_deadly;
1434 sigemptyset(&sa.sa_mask);
1435# if defined(__linux__) && defined(_REENTRANT)
1436 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1437 * thread handling in combination with using the alternate stack:
1438 * pthread library functions try to use the stack pointer to
1439 * identify the current thread, causing a SEGV signal, which
1440 * recursively calls deathtrap() and hangs. */
1441 sa.sa_flags = 0;
1442# else
1443 sa.sa_flags = SA_ONSTACK;
1444# endif
1445 sigaction(signal_info[i].sig, &sa, NULL);
1446#else
1447# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1448 struct sigvec sv;
1449
1450 /* Setup to use the alternate stack for the signal function. */
1451 sv.sv_handler = func_deadly;
1452 sv.sv_mask = 0;
1453 sv.sv_flags = SV_ONSTACK;
1454 sigvec(signal_info[i].sig, &sv, NULL);
1455# else
1456 signal(signal_info[i].sig, func_deadly);
1457# endif
1458#endif
1459 }
1460 else if (func_other != SIG_ERR)
1461 signal(signal_info[i].sig, func_other);
1462}
1463
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001464#ifdef HAVE_SIGPROCMASK
1465 static void
1466block_signals(sigset_t *set)
1467{
1468 sigset_t newset;
1469 int i;
1470
1471 sigemptyset(&newset);
1472
1473 for (i = 0; signal_info[i].sig != -1; i++)
1474 sigaddset(&newset, signal_info[i].sig);
1475
1476# if defined(_REENTRANT) && defined(SIGCONT)
1477 /* SIGCONT isn't in the list, because its default action is ignore */
1478 sigaddset(&newset, SIGCONT);
1479# endif
1480
1481 sigprocmask(SIG_BLOCK, &newset, set);
1482}
1483
1484 static void
1485unblock_signals(sigset_t *set)
1486{
1487 sigprocmask(SIG_SETMASK, set, NULL);
1488}
1489#endif
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001492 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001493 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1494 * return TRUE
1495 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1496 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001497 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001498 * Returns TRUE when Vim should exit.
1499 */
1500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001501vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001502{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001503 static int got_signal = 0;
1504 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001505
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001506 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001507 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001508 case SIGNAL_BLOCK: blocked = TRUE;
1509 break;
1510
1511 case SIGNAL_UNBLOCK: blocked = FALSE;
1512 if (got_signal != 0)
1513 {
1514 kill(getpid(), got_signal);
1515 got_signal = 0;
1516 }
1517 break;
1518
1519 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001520 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001521 got_signal = sig;
1522#ifdef SIGPWR
1523 if (sig != SIGPWR)
1524#endif
1525 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001526 break;
1527 }
1528 return FALSE;
1529}
1530
1531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 * Check_win checks whether we have an interactive stdout.
1533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001535mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (isatty(1))
1538 return OK;
1539 return FAIL;
1540}
1541
1542/*
1543 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1544 */
1545 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001546mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 if (isatty(read_cmd_fd))
1549 return TRUE;
1550 return FALSE;
1551}
1552
1553#ifdef FEAT_X11
1554
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001555# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558/*
1559 * Give a message about the elapsed time for opening the X window.
1560 */
1561 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001562xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001564 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565}
1566# endif
1567#endif
1568
1569#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1570/*
1571 * A few functions shared by X11 title and clipboard code.
1572 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001573static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1574static int x_error_check(Display *dpy, XErrorEvent *error_event);
1575static int x_connect_to_server(void);
1576static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578static int got_x_error = FALSE;
1579
1580/*
1581 * X Error handler, otherwise X just exits! (very rude) -- webb
1582 */
1583 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001584x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001586 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 STRCAT(IObuff, _("\nVim: Got X error\n"));
1588
1589 /* We cannot print a message and continue, because no X calls are allowed
1590 * here (causes my system to hang). Silently continuing might be an
1591 * alternative... */
1592 preserve_exit(); /* preserve files and exit */
1593
1594 return 0; /* NOTREACHED */
1595}
1596
1597/*
1598 * Another X Error handler, just used to check for errors.
1599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001601x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 got_x_error = TRUE;
1604 return 0;
1605}
1606
1607#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1608# if defined(HAVE_SETJMP_H)
1609/*
1610 * An X IO Error handler, used to catch error while opening the display.
1611 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001612static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001615x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 /* This function should not return, it causes exit(). Longjump instead. */
1618 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001619# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001620 return 0; /* avoid the compiler complains about missing return value */
1621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622}
1623# endif
1624
1625/*
1626 * An X IO Error handler, used to catch terminal errors.
1627 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001628static int x_IOerror_handler(Display *dpy);
1629static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001630static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001633x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001636 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 x11_window = 0;
1638 x11_display = NULL;
1639 xterm_Shell = (Widget)0;
1640
1641 /* This function should not return, it causes exit(). Longjump instead. */
1642 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001643# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001644 return 0; /* avoid the compiler complains about missing return value */
1645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001647
1648/*
1649 * If the X11 connection was lost try to restore it.
1650 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001651 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001652 */
1653 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001654may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001655{
1656 if (xterm_dpy_was_reset)
1657 {
1658 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001659
1660# ifndef LESSTIF_VERSION
1661 /* This has been reported to avoid Vim getting stuck. */
1662 if (app_context != (XtAppContext)NULL)
1663 {
1664 XtDestroyApplicationContext(app_context);
1665 app_context = (XtAppContext)NULL;
1666 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1667 }
1668# endif
1669
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001670 setup_term_clip();
1671 get_x11_title(FALSE);
1672 }
1673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#endif
1675
1676/*
1677 * Return TRUE when connection to the X server is desired.
1678 */
1679 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001680x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#if defined(FEAT_CLIENTSERVER)
1683 if (x_force_connect)
1684 return TRUE;
1685#endif
1686 if (x_no_connect)
1687 return FALSE;
1688
1689 /* Check for a match with "exclude:" from 'clipboard'. */
1690 if (clip_exclude_prog != NULL)
1691 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001692 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return FALSE;
1694 }
1695 return TRUE;
1696}
1697
1698/*
1699 * Test if "dpy" and x11_window are valid by getting the window title.
1700 * I don't actually want it yet, so there may be a simpler call to use, but
1701 * this will cause the error handler x_error_check() to be called if anything
1702 * is wrong, such as the window pointer being invalid (as can happen when the
1703 * user changes his DISPLAY, but not his WINDOWID) -- webb
1704 */
1705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001706test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 int (*old_handler)();
1709 XTextProperty text_prop;
1710
1711 old_handler = XSetErrorHandler(x_error_check);
1712 got_x_error = FALSE;
1713 if (XGetWMName(dpy, x11_window, &text_prop))
1714 XFree((void *)text_prop.value);
1715 XSync(dpy, False);
1716 (void)XSetErrorHandler(old_handler);
1717
1718 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001719 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 return (got_x_error ? FAIL : OK);
1722}
1723#endif
1724
1725#ifdef FEAT_TITLE
1726
1727#ifdef FEAT_X11
1728
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001729static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731/*
1732 * try to get x11 window and display
1733 *
1734 * return FAIL for failure, OK otherwise
1735 */
1736 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001737get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 char *winid;
1740 static int result = -1;
1741#define XD_NONE 0 /* x11_display not set here */
1742#define XD_HERE 1 /* x11_display opened here */
1743#define XD_GUI 2 /* x11_display used from gui.dpy */
1744#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1745 static int x11_display_from = XD_NONE;
1746 static int did_set_error_handler = FALSE;
1747
1748 if (!did_set_error_handler)
1749 {
1750 /* X just exits if it finds an error otherwise! */
1751 (void)XSetErrorHandler(x_error_handler);
1752 did_set_error_handler = TRUE;
1753 }
1754
Bram Moolenaar9372a112005-12-06 19:59:18 +00001755#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (gui.in_use)
1757 {
1758 /*
1759 * If the X11 display was opened here before, for the window where Vim
1760 * was started, close that one now to avoid a memory leak.
1761 */
1762 if (x11_display_from == XD_HERE && x11_display != NULL)
1763 {
1764 XCloseDisplay(x11_display);
1765 x11_display_from = XD_NONE;
1766 }
1767 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1768 {
1769 x11_display_from = XD_GUI;
1770 return OK;
1771 }
1772 x11_display = NULL;
1773 return FAIL;
1774 }
1775 else if (x11_display_from == XD_GUI)
1776 {
1777 /* GUI must have stopped somehow, clear x11_display */
1778 x11_window = 0;
1779 x11_display = NULL;
1780 x11_display_from = XD_NONE;
1781 }
1782#endif
1783
1784 /* When started with the "-X" argument, don't try connecting. */
1785 if (!x_connect_to_server())
1786 return FAIL;
1787
1788 /*
1789 * If WINDOWID not set, should try another method to find out
1790 * what the current window number is. The only code I know for
1791 * this is very complicated.
1792 * We assume that zero is invalid for WINDOWID.
1793 */
1794 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1795 x11_window = (Window)atol(winid);
1796
1797#ifdef FEAT_XCLIPBOARD
1798 if (xterm_dpy != NULL && x11_window != 0)
1799 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001800 /* We may have checked it already, but Gnome terminal can move us to
1801 * another window, so we need to check every time. */
1802 if (x11_display_from != XD_XTERM)
1803 {
1804 /*
1805 * If the X11 display was opened here before, for the window where
1806 * Vim was started, close that one now to avoid a memory leak.
1807 */
1808 if (x11_display_from == XD_HERE && x11_display != NULL)
1809 XCloseDisplay(x11_display);
1810 x11_display = xterm_dpy;
1811 x11_display_from = XD_XTERM;
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (test_x11_window(x11_display) == FAIL)
1814 {
1815 /* probably bad $WINDOWID */
1816 x11_window = 0;
1817 x11_display = NULL;
1818 x11_display_from = XD_NONE;
1819 return FAIL;
1820 }
1821 return OK;
1822 }
1823#endif
1824
1825 if (x11_window == 0 || x11_display == NULL)
1826 result = -1;
1827
1828 if (result != -1) /* Have already been here and set this */
1829 return result; /* Don't do all these X calls again */
1830
1831 if (x11_window != 0 && x11_display == NULL)
1832 {
1833#ifdef SET_SIG_ALARM
1834 RETSIGTYPE (*sig_save)();
1835#endif
1836#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1837 struct timeval start_tv;
1838
1839 if (p_verbose > 0)
1840 gettimeofday(&start_tv, NULL);
1841#endif
1842
1843#ifdef SET_SIG_ALARM
1844 /*
1845 * Opening the Display may hang if the DISPLAY setting is wrong, or
1846 * the network connection is bad. Set an alarm timer to get out.
1847 */
1848 sig_alarm_called = FALSE;
1849 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1850 (RETSIGTYPE (*)())sig_alarm);
1851 alarm(2);
1852#endif
1853 x11_display = XOpenDisplay(NULL);
1854
1855#ifdef SET_SIG_ALARM
1856 alarm(0);
1857 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1858 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001859 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#endif
1861 if (x11_display != NULL)
1862 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001863# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001865 {
1866 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001867 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001868 verbose_leave();
1869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870# endif
1871 if (test_x11_window(x11_display) == FAIL)
1872 {
1873 /* Maybe window id is bad */
1874 x11_window = 0;
1875 XCloseDisplay(x11_display);
1876 x11_display = NULL;
1877 }
1878 else
1879 x11_display_from = XD_HERE;
1880 }
1881 }
1882 if (x11_window == 0 || x11_display == NULL)
1883 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001884
1885# ifdef FEAT_EVAL
1886 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1887# endif
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 return (result = OK);
1890}
1891
1892/*
1893 * Determine original x11 Window Title
1894 */
1895 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001896get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001898 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899}
1900
1901/*
1902 * Determine original x11 Window icon
1903 */
1904 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001905get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 int retval = FALSE;
1908
1909 retval = get_x11_thing(FALSE, test_only);
1910
1911 /* could not get old icon, use terminal name */
1912 if (oldicon == NULL && !test_only)
1913 {
1914 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001915 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001917 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919
1920 return retval;
1921}
1922
1923 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001924get_x11_thing(
1925 int get_title, /* get title string */
1926 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
1928 XTextProperty text_prop;
1929 int retval = FALSE;
1930 Status status;
1931
1932 if (get_x11_windis() == OK)
1933 {
1934 /* Get window/icon name if any */
1935 if (get_title)
1936 status = XGetWMName(x11_display, x11_window, &text_prop);
1937 else
1938 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1939
1940 /*
1941 * If terminal is xterm, then x11_window may be a child window of the
1942 * outer xterm window that actually contains the window/icon name, so
1943 * keep traversing up the tree until a window with a title/icon is
1944 * found.
1945 */
1946 /* Previously this was only done for xterm and alikes. I don't see a
1947 * reason why it would fail for other terminal emulators.
1948 * if (term_is_xterm) */
1949 {
1950 Window root;
1951 Window parent;
1952 Window win = x11_window;
1953 Window *children;
1954 unsigned int num_children;
1955
1956 while (!status || text_prop.value == NULL)
1957 {
1958 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1959 &num_children))
1960 break;
1961 if (children)
1962 XFree((void *)children);
1963 if (parent == root || parent == 0)
1964 break;
1965
1966 win = parent;
1967 if (get_title)
1968 status = XGetWMName(x11_display, win, &text_prop);
1969 else
1970 status = XGetWMIconName(x11_display, win, &text_prop);
1971 }
1972 }
1973 if (status && text_prop.value != NULL)
1974 {
1975 retval = TRUE;
1976 if (!test_only)
1977 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001978#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1979 if (text_prop.encoding == XA_STRING
1980# ifdef FEAT_MBYTE
1981 && !has_mbyte
1982# endif
1983 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985#endif
1986 if (get_title)
1987 oldtitle = vim_strsave((char_u *)text_prop.value);
1988 else
1989 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001990#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 else
1993 {
1994 char **cl;
1995 Status transform_status;
1996 int n = 0;
1997
1998 transform_status = XmbTextPropertyToTextList(x11_display,
1999 &text_prop,
2000 &cl, &n);
2001 if (transform_status >= Success && n > 0 && cl[0])
2002 {
2003 if (get_title)
2004 oldtitle = vim_strsave((char_u *) cl[0]);
2005 else
2006 oldicon = vim_strsave((char_u *) cl[0]);
2007 XFreeStringList(cl);
2008 }
2009 else
2010 {
2011 if (get_title)
2012 oldtitle = vim_strsave((char_u *)text_prop.value);
2013 else
2014 oldicon = vim_strsave((char_u *)text_prop.value);
2015 }
2016 }
2017#endif
2018 }
2019 XFree((void *)text_prop.value);
2020 }
2021 }
2022 return retval;
2023}
2024
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002025/* Xutf8 functions are not avaialble on older systems. Note that on some
2026 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2027 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2028 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002030# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031# define USE_UTF8_STRING
2032# endif
2033#endif
2034
2035/*
2036 * Set x11 Window Title
2037 *
2038 * get_x11_windis() must be called before this and have returned OK
2039 */
2040 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002041set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
2043 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2044 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2045 * supported everywhere and STRING doesn't work for multi-byte titles.
2046 */
2047#ifdef USE_UTF8_STRING
2048 if (enc_utf8)
2049 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2050 NULL, NULL, 0, NULL, NULL, NULL);
2051 else
2052#endif
2053 {
2054#if XtSpecificationRelease >= 4
2055# ifdef FEAT_XFONTSET
2056 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2057 NULL, NULL, 0, NULL, NULL, NULL);
2058# else
2059 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002060 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002063 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 XSetWMProperties(x11_display, x11_window, &text_prop,
2065 NULL, NULL, 0, NULL, NULL, NULL);
2066# endif
2067#else
2068 XStoreName(x11_display, x11_window, (char *)title);
2069#endif
2070 }
2071 XFlush(x11_display);
2072}
2073
2074/*
2075 * Set x11 Window icon
2076 *
2077 * get_x11_windis() must be called before this and have returned OK
2078 */
2079 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002080set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
2082 /* See above for comments about using X*SetWMProperties(). */
2083#ifdef USE_UTF8_STRING
2084 if (enc_utf8)
2085 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2086 NULL, 0, NULL, NULL, NULL);
2087 else
2088#endif
2089 {
2090#if XtSpecificationRelease >= 4
2091# ifdef FEAT_XFONTSET
2092 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2093 NULL, 0, NULL, NULL, NULL);
2094# else
2095 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002096 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002098 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2100 NULL, 0, NULL, NULL, NULL);
2101# endif
2102#else
2103 XSetIconName(x11_display, x11_window, (char *)icon);
2104#endif
2105 }
2106 XFlush(x11_display);
2107}
2108
2109#else /* FEAT_X11 */
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002112get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 return FALSE;
2115}
2116
2117 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002118get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 if (!test_only)
2121 {
2122 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002123 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002125 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 }
2127 return FALSE;
2128}
2129
2130#endif /* FEAT_X11 */
2131
2132 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002133mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 return get_x11_title(TRUE);
2136}
2137
2138 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002139mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 return get_x11_icon(TRUE);
2142}
2143
2144/*
2145 * Set the window title and icon.
2146 */
2147 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002148mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int type = 0;
2151 static int recursive = 0;
2152
2153 if (T_NAME == NULL) /* no terminal name (yet) */
2154 return;
2155 if (title == NULL && icon == NULL) /* nothing to do */
2156 return;
2157
2158 /* When one of the X11 functions causes a deadly signal, we get here again
2159 * recursively. Avoid hanging then (something is probably locked). */
2160 if (recursive)
2161 return;
2162 ++recursive;
2163
2164 /*
2165 * if the window ID and the display is known, we may use X11 calls
2166 */
2167#ifdef FEAT_X11
2168 if (get_x11_windis() == OK)
2169 type = 1;
2170#else
2171# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2172 if (gui.in_use)
2173 type = 1;
2174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#endif
2176
2177 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002178 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 * than x11 calls, because the x11 calls don't always work
2180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if ((type || *T_TS != NUL) && title != NULL)
2182 {
2183 if (oldtitle == NULL
2184#ifdef FEAT_GUI
2185 && !gui.in_use
2186#endif
2187 ) /* first call but not in GUI, save title */
2188 (void)get_x11_title(FALSE);
2189
2190 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2191 term_settitle(title);
2192#ifdef FEAT_X11
2193 else
2194# ifdef FEAT_GUI_GTK
2195 if (!gui.in_use) /* don't do this if GTK+ is running */
2196# endif
2197 set_x11_title(title); /* x11 */
2198#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002199#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2201 else
2202 gui_mch_settitle(title, icon);
2203#endif
2204 did_set_title = TRUE;
2205 }
2206
2207 if ((type || *T_CIS != NUL) && icon != NULL)
2208 {
2209 if (oldicon == NULL
2210#ifdef FEAT_GUI
2211 && !gui.in_use
2212#endif
2213 ) /* first call, save icon */
2214 get_x11_icon(FALSE);
2215
2216 if (*T_CIS != NUL)
2217 {
2218 out_str(T_CIS); /* set icon start */
2219 out_str_nf(icon);
2220 out_str(T_CIE); /* set icon end */
2221 out_flush();
2222 }
2223#ifdef FEAT_X11
2224 else
2225# ifdef FEAT_GUI_GTK
2226 if (!gui.in_use) /* don't do this if GTK+ is running */
2227# endif
2228 set_x11_icon(icon); /* x11 */
2229#endif
2230 did_set_icon = TRUE;
2231 }
2232 --recursive;
2233}
2234
2235/*
2236 * Restore the window/icon title.
2237 * "which" is one of:
2238 * 1 only restore title
2239 * 2 only restore icon
2240 * 3 restore title and icon
2241 */
2242 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002243mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
2245 /* only restore the title or icon when it has been set */
2246 mch_settitle(((which & 1) && did_set_title) ?
2247 (oldtitle ? oldtitle : p_titleold) : NULL,
2248 ((which & 2) && did_set_icon) ? oldicon : NULL);
2249}
2250
2251#endif /* FEAT_TITLE */
2252
2253/*
2254 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002255 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 */
2257 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002258vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 if (name == NULL)
2261 return FALSE;
2262 return (STRNICMP(name, "xterm", 5) == 0
2263 || STRNICMP(name, "nxterm", 6) == 0
2264 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002265 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 || STRNICMP(name, "rxvt", 4) == 0
2267 || STRCMP(name, "builtin_xterm") == 0);
2268}
2269
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002270#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2271/*
2272 * Return TRUE if "name" appears to be that of a terminal
2273 * known to support the xterm-style mouse protocol.
2274 * Relies on term_is_xterm having been set to its correct value.
2275 */
2276 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002277use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002278{
2279 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002280 && (term_is_xterm
2281 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002282 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002283 || STRICMP(name, "st") == 0
2284 || STRNICMP(name, "st-", 3) == 0
2285 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002286}
2287#endif
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2290/*
2291 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2292 * Return 1 for "xterm".
2293 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002294 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002295 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 */
2297 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002298use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002300 if (ttym_flags == TTYM_SGR)
2301 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002302 if (ttym_flags == TTYM_URXVT)
2303 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (ttym_flags == TTYM_XTERM2)
2305 return 2;
2306 if (ttym_flags == TTYM_XTERM)
2307 return 1;
2308 return 0;
2309}
2310#endif
2311
2312 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002313vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315 if (name == NULL)
2316 return FALSE;
2317 return (STRNICMP(name, "iris-ansi", 9) == 0
2318 || STRCMP(name, "builtin_iris-ansi") == 0);
2319}
2320
2321 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002322vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
2324 if (name == NULL)
2325 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002326 /* catch VT100 - VT5xx */
2327 return ((STRNICMP(name, "vt", 2) == 0
2328 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 || STRCMP(name, "builtin_vt320") == 0);
2330}
2331
2332/*
2333 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2334 * This should include all windowed terminal emulators.
2335 */
2336 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002337vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 if (name == NULL)
2340 return FALSE;
2341 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2342 return TRUE;
2343 return ( STRNICMP(name, "hpterm", 6) == 0
2344 || STRNICMP(name, "sun-cmd", 7) == 0
2345 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002346 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 || STRNICMP(name, "dtterm", 6) == 0);
2348}
2349
2350/*
2351 * Insert user name in s[len].
2352 * Return OK if a name found.
2353 */
2354 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002355mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
2357#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002358 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return OK;
2360#else
2361 return mch_get_uname(getuid(), s, len);
2362#endif
2363}
2364
2365/*
2366 * Insert user name for "uid" in s[len].
2367 * Return OK if a name found.
2368 */
2369 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002370mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2373 struct passwd *pw;
2374
2375 if ((pw = getpwuid(uid)) != NULL
2376 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2377 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002378 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return OK;
2380 }
2381#endif
2382 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2383 return FAIL; /* a number is not a name */
2384}
2385
2386/*
2387 * Insert host name is s[len].
2388 */
2389
2390#ifdef HAVE_SYS_UTSNAME_H
2391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002392mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394 struct utsname vutsname;
2395
2396 if (uname(&vutsname) < 0)
2397 *s = NUL;
2398 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002399 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400}
2401#else /* HAVE_SYS_UTSNAME_H */
2402
2403# ifdef HAVE_SYS_SYSTEMINFO_H
2404# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2405# endif
2406
2407 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002408mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410# ifdef VAXC
2411 vaxc$gethostname((char *)s, len);
2412# else
2413 gethostname((char *)s, len);
2414# endif
2415 s[len - 1] = NUL; /* make sure it's terminated */
2416}
2417#endif /* HAVE_SYS_UTSNAME_H */
2418
2419/*
2420 * return process ID
2421 */
2422 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002423mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425 return (long)getpid();
2426}
2427
2428#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002429static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002432strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 extern int sys_nerr;
2435 extern char *sys_errlist[];
2436 static char er[20];
2437
2438 if (err > 0 && err < sys_nerr)
2439 return (sys_errlist[err]);
2440 sprintf(er, "Error %d", err);
2441 return er;
2442}
2443#endif
2444
2445/*
2446 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2447 * Return OK for success, FAIL for failure.
2448 */
2449 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002450mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452#if defined(USE_GETCWD)
2453 if (getcwd((char *)buf, len) == NULL)
2454 {
2455 STRCPY(buf, strerror(errno));
2456 return FAIL;
2457 }
2458 return OK;
2459#else
2460 return (getwd((char *)buf) != NULL ? OK : FAIL);
2461#endif
2462}
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002465 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 *
2467 * return FAIL for failure, OK for success
2468 */
2469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002470mch_FullName(
2471 char_u *fname,
2472 char_u *buf,
2473 int len,
2474 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002477#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 int fd = -1;
2479 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 char_u olddir[MAXPATHL];
2482 char_u *p;
2483 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002484#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002485 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2486 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002487#endif
2488
Bram Moolenaar38323e42007-03-06 19:22:53 +00002489#ifdef VMS
2490 fname = vms_fixfilename(fname);
2491#endif
2492
Bram Moolenaara2442432007-04-26 14:26:37 +00002493#ifdef __CYGWIN__
2494 /*
2495 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2496 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002497# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002498 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2499 * a forward slash. */
2500 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2501 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002502# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002503 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002504# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002505 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002506#endif
2507
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002508 /* Expand it if forced or not an absolute path.
2509 * Do not do it for "/file", the result is always "/". */
2510 if ((force || !mch_isFullName(fname))
2511 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 /*
2514 * If the file name has a path, change to that directory for a moment,
2515 * and then do the getwd() (and get back to where we were).
2516 * This will get the correct path name with "../" things.
2517 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002518 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002520#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 /*
2522 * Use fchdir() if possible, it's said to be faster and more
2523 * reliable. But on SunOS 4 it might not work. Check this by
2524 * doing a fchdir() right now.
2525 */
2526 if (!dont_fchdir)
2527 {
2528 fd = open(".", O_RDONLY | O_EXTRA, 0);
2529 if (fd >= 0 && fchdir(fd) < 0)
2530 {
2531 close(fd);
2532 fd = -1;
2533 dont_fchdir = TRUE; /* don't try again */
2534 }
2535 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
2538 /* Only change directory when we are sure we can return to where
2539 * we are now. After doing "su" chdir(".") might not work. */
2540 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002541#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 (mch_dirname(olddir, MAXPATHL) == FAIL
2545 || mch_chdir((char *)olddir) != 0))
2546 {
2547 p = NULL; /* can't get current dir: don't chdir */
2548 retval = FAIL;
2549 }
2550 else
2551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /* The directory is copied into buf[], to be able to remove
2553 * the file name without changing it (could be a string in
2554 * read-only memory) */
2555 if (p - fname >= len)
2556 retval = FAIL;
2557 else
2558 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002559 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (mch_chdir((char *)buf))
2561 retval = FAIL;
2562 else
2563 fname = p + 1;
2564 *buf = NUL;
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 }
2568 if (mch_dirname(buf, len) == FAIL)
2569 {
2570 retval = FAIL;
2571 *buf = NUL;
2572 }
2573 if (p != NULL)
2574 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002575#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (fd >= 0)
2577 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002578 if (p_verbose >= 5)
2579 {
2580 verbose_enter();
2581 MSG("fchdir() to previous dir");
2582 verbose_leave();
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 l = fchdir(fd);
2585 close(fd);
2586 }
2587 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 l = mch_chdir((char *)olddir);
2590 if (l != 0)
2591 EMSG(_(e_prev_dir));
2592 }
2593
2594 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002595 if (l >= len - 1)
2596 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002597#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002598 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002600 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002605 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 return FAIL;
2607
2608 /* Do not append ".", "/dir/." is equal to "/dir". */
2609 if (STRCMP(fname, ".") != 0)
2610 STRCAT(buf, fname);
2611
2612 return OK;
2613}
2614
2615/*
2616 * Return TRUE if "fname" does not depend on the current directory.
2617 */
2618 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002619mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002621#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 return ( fname[0] == '/' || fname[0] == '.' ||
2623 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2624 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2625 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002626#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629}
2630
Bram Moolenaar24552be2005-12-10 20:17:30 +00002631#if defined(USE_FNAME_CASE) || defined(PROTO)
2632/*
2633 * Set the case of the file name, if it already exists. This will cause the
2634 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002635 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002636 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002638fname_case(
2639 char_u *name,
2640 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002641{
2642 struct stat st;
2643 char_u *slash, *tail;
2644 DIR *dirp;
2645 struct dirent *dp;
2646
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002647 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002648 {
2649 /* Open the directory where the file is located. */
2650 slash = vim_strrchr(name, '/');
2651 if (slash == NULL)
2652 {
2653 dirp = opendir(".");
2654 tail = name;
2655 }
2656 else
2657 {
2658 *slash = NUL;
2659 dirp = opendir((char *)name);
2660 *slash = '/';
2661 tail = slash + 1;
2662 }
2663
2664 if (dirp != NULL)
2665 {
2666 while ((dp = readdir(dirp)) != NULL)
2667 {
2668 /* Only accept names that differ in case and are the same byte
2669 * length. TODO: accept different length name. */
2670 if (STRICMP(tail, dp->d_name) == 0
2671 && STRLEN(tail) == STRLEN(dp->d_name))
2672 {
2673 char_u newname[MAXPATHL + 1];
2674 struct stat st2;
2675
2676 /* Verify the inode is equal. */
2677 vim_strncpy(newname, name, MAXPATHL);
2678 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2679 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002680 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002681 && st.st_ino == st2.st_ino
2682 && st.st_dev == st2.st_dev)
2683 {
2684 STRCPY(tail, dp->d_name);
2685 break;
2686 }
2687 }
2688 }
2689
2690 closedir(dirp);
2691 }
2692 }
2693}
2694#endif
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696/*
2697 * Get file permissions for 'name'.
2698 * Returns -1 when it doesn't exist.
2699 */
2700 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002701mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 struct stat statb;
2704
2705 /* Keep the #ifdef outside of stat(), it may be a macro. */
2706#ifdef VMS
2707 if (stat((char *)vms_fixfilename(name), &statb))
2708#else
2709 if (stat((char *)name, &statb))
2710#endif
2711 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002712#ifdef __INTERIX
2713 /* The top bit makes the value negative, which means the file doesn't
2714 * exist. Remove the bit, we don't use it. */
2715 return statb.st_mode & ~S_ADDACE;
2716#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721/*
2722 * set file permission for 'name' to 'perm'
2723 *
2724 * return FAIL for failure, OK otherwise
2725 */
2726 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002727mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 return (chmod((char *)
2730#ifdef VMS
2731 vms_fixfilename(name),
2732#else
2733 name,
2734#endif
2735 (mode_t)perm) == 0 ? OK : FAIL);
2736}
2737
2738#if defined(HAVE_ACL) || defined(PROTO)
2739# ifdef HAVE_SYS_ACL_H
2740# include <sys/acl.h>
2741# endif
2742# ifdef HAVE_SYS_ACCESS_H
2743# include <sys/access.h>
2744# endif
2745
2746# ifdef HAVE_SOLARIS_ACL
2747typedef struct vim_acl_solaris_T {
2748 int acl_cnt;
2749 aclent_t *acl_entry;
2750} vim_acl_solaris_T;
2751# endif
2752
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002753#if defined(HAVE_SELINUX) || defined(PROTO)
2754/*
2755 * Copy security info from "from_file" to "to_file".
2756 */
2757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002758mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002759{
2760 if (from_file == NULL)
2761 return;
2762
2763 if (selinux_enabled == -1)
2764 selinux_enabled = is_selinux_enabled();
2765
2766 if (selinux_enabled > 0)
2767 {
2768 security_context_t from_context = NULL;
2769 security_context_t to_context = NULL;
2770
2771 if (getfilecon((char *)from_file, &from_context) < 0)
2772 {
2773 /* If the filesystem doesn't support extended attributes,
2774 the original had no special security context and the
2775 target cannot have one either. */
2776 if (errno == EOPNOTSUPP)
2777 return;
2778
2779 MSG_PUTS(_("\nCould not get security context for "));
2780 msg_outtrans(from_file);
2781 msg_putchar('\n');
2782 return;
2783 }
2784 if (getfilecon((char *)to_file, &to_context) < 0)
2785 {
2786 MSG_PUTS(_("\nCould not get security context for "));
2787 msg_outtrans(to_file);
2788 msg_putchar('\n');
2789 freecon (from_context);
2790 return ;
2791 }
2792 if (strcmp(from_context, to_context) != 0)
2793 {
2794 if (setfilecon((char *)to_file, from_context) < 0)
2795 {
2796 MSG_PUTS(_("\nCould not set security context for "));
2797 msg_outtrans(to_file);
2798 msg_putchar('\n');
2799 }
2800 }
2801 freecon(to_context);
2802 freecon(from_context);
2803 }
2804}
2805#endif /* HAVE_SELINUX */
2806
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002807#if defined(HAVE_SMACK) && !defined(PROTO)
2808/*
2809 * Copy security info from "from_file" to "to_file".
2810 */
2811 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002812mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002813{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002814 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002815 {
2816 XATTR_NAME_SMACK,
2817 XATTR_NAME_SMACKEXEC,
2818 XATTR_NAME_SMACKMMAP
2819 };
2820
2821 char buffer[SMACK_LABEL_LEN];
2822 const char *name;
2823 int index;
2824 int ret;
2825 ssize_t size;
2826
2827 if (from_file == NULL)
2828 return;
2829
2830 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2831 / sizeof(smack_copied_attributes)[0]) ; index++)
2832 {
2833 /* get the name of the attribute to copy */
2834 name = smack_copied_attributes[index];
2835
2836 /* get the value of the attribute in buffer */
2837 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2838 if (size >= 0)
2839 {
2840 /* copy the attribute value of buffer */
2841 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2842 if (ret < 0)
2843 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002844 vim_snprintf((char *)IObuff, IOSIZE,
2845 _("Could not set security context %s for %s"),
2846 name, to_file);
2847 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002848 msg_putchar('\n');
2849 }
2850 }
2851 else
2852 {
2853 /* what reason of not having the attribute value? */
2854 switch (errno)
2855 {
2856 case ENOTSUP:
2857 /* extended attributes aren't supported or enabled */
2858 /* should a message be echoed? not sure... */
2859 return; /* leave because it isn't usefull to continue */
2860
2861 case ERANGE:
2862 default:
2863 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002864 vim_snprintf((char *)IObuff, IOSIZE,
2865 _("Could not get security context %s for %s. Removing it!"),
2866 name, from_file);
2867 msg_puts(IObuff);
2868 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002869 /* FALLTHROUGH to remove the attribute */
2870
2871 case ENODATA:
2872 /* no attribute of this name */
2873 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002874 /* Silently ignore errors, apparently this happens when
2875 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002876 break;
2877 }
2878 }
2879 }
2880}
2881#endif /* HAVE_SMACK */
2882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883/*
2884 * Return a pointer to the ACL of file "fname" in allocated memory.
2885 * Return NULL if the ACL is not available for whatever reason.
2886 */
2887 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002888mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 vim_acl_T ret = NULL;
2891#ifdef HAVE_POSIX_ACL
2892 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2893#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002894#ifdef HAVE_SOLARIS_ZFS_ACL
2895 acl_t *aclent;
2896
2897 if (acl_get((char *)fname, 0, &aclent) < 0)
2898 return NULL;
2899 ret = (vim_acl_T)aclent;
2900#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#ifdef HAVE_SOLARIS_ACL
2902 vim_acl_solaris_T *aclent;
2903
2904 aclent = malloc(sizeof(vim_acl_solaris_T));
2905 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2906 {
2907 free(aclent);
2908 return NULL;
2909 }
2910 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2911 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2912 {
2913 free(aclent->acl_entry);
2914 free(aclent);
2915 return NULL;
2916 }
2917 ret = (vim_acl_T)aclent;
2918#else
2919#if defined(HAVE_AIX_ACL)
2920 int aclsize;
2921 struct acl *aclent;
2922
2923 aclsize = sizeof(struct acl);
2924 aclent = malloc(aclsize);
2925 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2926 {
2927 if (errno == ENOSPC)
2928 {
2929 aclsize = aclent->acl_len;
2930 aclent = realloc(aclent, aclsize);
2931 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2932 {
2933 free(aclent);
2934 return NULL;
2935 }
2936 }
2937 else
2938 {
2939 free(aclent);
2940 return NULL;
2941 }
2942 }
2943 ret = (vim_acl_T)aclent;
2944#endif /* HAVE_AIX_ACL */
2945#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002946#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947#endif /* HAVE_POSIX_ACL */
2948 return ret;
2949}
2950
2951/*
2952 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2953 */
2954 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002955mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 if (aclent == NULL)
2958 return;
2959#ifdef HAVE_POSIX_ACL
2960 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2961#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002962#ifdef HAVE_SOLARIS_ZFS_ACL
2963 acl_set((char *)fname, (acl_t *)aclent);
2964#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965#ifdef HAVE_SOLARIS_ACL
2966 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
2967 ((vim_acl_solaris_T *)aclent)->acl_entry);
2968#else
2969#ifdef HAVE_AIX_ACL
2970 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
2971#endif /* HAVE_AIX_ACL */
2972#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002973#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#endif /* HAVE_POSIX_ACL */
2975}
2976
2977 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002978mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 if (aclent == NULL)
2981 return;
2982#ifdef HAVE_POSIX_ACL
2983 acl_free((acl_t)aclent);
2984#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002985#ifdef HAVE_SOLARIS_ZFS_ACL
2986 acl_free((acl_t *)aclent);
2987#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#ifdef HAVE_SOLARIS_ACL
2989 free(((vim_acl_solaris_T *)aclent)->acl_entry);
2990 free(aclent);
2991#else
2992#ifdef HAVE_AIX_ACL
2993 free(aclent);
2994#endif /* HAVE_AIX_ACL */
2995#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002996#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#endif /* HAVE_POSIX_ACL */
2998}
2999#endif
3000
3001/*
3002 * Set hidden flag for "name".
3003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003005mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 /* can't hide a file */
3008}
3009
3010/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003011 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 * return FALSE if "name" is not a directory
3013 * return FALSE for error
3014 */
3015 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003016mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 struct stat statb;
3019
3020 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3021 return FALSE;
3022 if (stat((char *)name, &statb))
3023 return FALSE;
3024#ifdef _POSIX_SOURCE
3025 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3026#else
3027 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3028#endif
3029}
3030
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003031/*
3032 * return TRUE if "name" is a directory, NOT a symlink to a directory
3033 * return FALSE if "name" is not a directory
3034 * return FALSE for error
3035 */
3036 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003037mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003038{
3039 struct stat statb;
3040
3041 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3042 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003043 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003044 return FALSE;
3045#ifdef _POSIX_SOURCE
3046 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3047#else
3048 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3049#endif
3050}
3051
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003052static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054/*
3055 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3056 */
3057 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003058executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 struct stat st;
3061
3062 if (stat((char *)name, &st))
3063 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003064#ifdef VMS
3065 /* Like on Unix system file can have executable rights but not necessarily
3066 * be an executable, but on Unix is not a default for an ordianry file to
3067 * have an executable flag - on VMS it is in most cases.
3068 * Therefore, this check does not have any sense - let keep us to the
3069 * conventions instead:
3070 * *.COM and *.EXE files are the executables - the rest are not. This is
3071 * not ideal but better then it was.
3072 */
3073 int vms_executable = 0;
3074 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3075 {
3076 if (strstr(vms_tolower((char*)name),".exe") != NULL
3077 || strstr(vms_tolower((char*)name),".com")!= NULL)
3078 vms_executable = 1;
3079 }
3080 return vms_executable;
3081#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084}
3085
3086/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003087 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003088 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * Return -1 if unknown.
3090 */
3091 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003092mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 char_u *buf;
3095 char_u *p, *e;
3096 int retval;
3097
Bram Moolenaarb5971142015-03-21 17:32:19 +01003098 /* When "use_path" is false and if it's an absolute or relative path don't
3099 * need to use $PATH. */
3100 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3101 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003102 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003103 /* There must be a path separator, files in the current directory
3104 * can't be executed. */
3105 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003106 {
3107 if (path != NULL)
3108 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003109 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003110 *path = FullName_save(name, TRUE);
3111 else
3112 *path = vim_strsave(name);
3113 }
3114 return TRUE;
3115 }
3116 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 p = (char_u *)getenv("PATH");
3120 if (p == NULL || *p == NUL)
3121 return -1;
3122 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3123 if (buf == NULL)
3124 return -1;
3125
3126 /*
3127 * Walk through all entries in $PATH to check if "name" exists there and
3128 * is an executable file.
3129 */
3130 for (;;)
3131 {
3132 e = (char_u *)strchr((char *)p, ':');
3133 if (e == NULL)
3134 e = p + STRLEN(p);
3135 if (e - p <= 1) /* empty entry means current dir */
3136 STRCPY(buf, "./");
3137 else
3138 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003139 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 add_pathsep(buf);
3141 }
3142 STRCAT(buf, name);
3143 retval = executable_file(buf);
3144 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003145 {
3146 if (path != NULL)
3147 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003148 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003149 *path = FullName_save(buf, TRUE);
3150 else
3151 *path = vim_strsave(buf);
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 if (*e != ':')
3157 break;
3158 p = e + 1;
3159 }
3160
3161 vim_free(buf);
3162 return retval;
3163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165/*
3166 * Check what "name" is:
3167 * NODE_NORMAL: file or directory (or doesn't exist)
3168 * NODE_WRITABLE: writable device, socket, fifo, etc.
3169 * NODE_OTHER: non-writable things
3170 */
3171 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003172mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 struct stat st;
3175
3176 if (stat((char *)name, &st))
3177 return NODE_NORMAL;
3178 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3179 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3181 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 /* Everything else is writable? */
3183 return NODE_WRITABLE;
3184}
3185
3186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003187mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189#ifdef HAVE_CHECK_STACK_GROWTH
3190 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 check_stack_growth((char *)&i);
3193
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003194# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 get_stack_limit();
3196# endif
3197
3198#endif
3199
3200 /*
3201 * Setup an alternative stack for signals. Helps to catch signals when
3202 * running out of stack space.
3203 * Use of sigaltstack() is preferred, it's more portable.
3204 * Ignore any errors.
3205 */
3206#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003207 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 init_signal_stack();
3209#endif
3210}
3211
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003212#if defined(EXITFREE) || defined(PROTO)
3213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003214mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003215{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003216# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3217 if (clip_star.owned)
3218 clip_lose_selection(&clip_star);
3219 if (clip_plus.owned)
3220 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003221# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003222# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003223 if (xterm_Shell != (Widget)0)
3224 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003225# ifndef LESSTIF_VERSION
3226 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003227 if (xterm_dpy != NULL)
3228 XtCloseDisplay(xterm_dpy);
3229 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003230 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003231 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003232# ifdef FEAT_X11
3233 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3234# endif
3235 }
3236# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003237# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003238# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003239 if (x11_display != NULL
3240# ifdef FEAT_XCLIPBOARD
3241 && x11_display != xterm_dpy
3242# endif
3243 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 XCloseDisplay(x11_display);
3245# endif
3246# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3247 vim_free(signal_stack);
3248 signal_stack = NULL;
3249# endif
3250# ifdef FEAT_TITLE
3251 vim_free(oldtitle);
3252 vim_free(oldicon);
3253# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003254}
3255#endif
3256
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003257static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259/*
3260 * Output a newline when exiting.
3261 * Make sure the newline goes to the same stream as the text.
3262 */
3263 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003264exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003266 if (silent_mode)
3267 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 if (newline_on_exit || msg_didout)
3269 {
3270 if (msg_use_printf())
3271 {
3272 if (info_message)
3273 mch_msg("\n");
3274 else
3275 mch_errmsg("\r\n");
3276 }
3277 else
3278 out_char('\n');
3279 }
3280 else
3281 {
3282 restore_cterm_colors(); /* get original colors back */
3283 msg_clr_eos_force(); /* clear the rest of the display */
3284 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3285 }
3286}
3287
3288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003289mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 exiting = TRUE;
3292
3293#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3294 x11_export_final_selection();
3295#endif
3296
3297#ifdef FEAT_GUI
3298 if (!gui.in_use)
3299#endif
3300 {
3301 settmode(TMODE_COOK);
3302#ifdef FEAT_TITLE
3303 mch_restore_title(3); /* restore xterm title and icon name */
3304#endif
3305 /*
3306 * When t_ti is not empty but it doesn't cause swapping terminal
3307 * pages, need to output a newline when msg_didout is set. But when
3308 * t_ti does swap pages it should not go to the shell page. Do this
3309 * before stoptermcap().
3310 */
3311 if (swapping_screen() && !newline_on_exit)
3312 exit_scroll();
3313
3314 /* Stop termcap: May need to check for T_CRV response, which
3315 * requires RAW mode. */
3316 stoptermcap();
3317
3318 /*
3319 * A newline is only required after a message in the alternate screen.
3320 * This is set to TRUE by wait_return().
3321 */
3322 if (!swapping_screen() || newline_on_exit)
3323 exit_scroll();
3324
3325 /* Cursor may have been switched off without calling starttermcap()
3326 * when doing "vim -u vimrc" and vimrc contains ":q". */
3327 if (full_screen)
3328 cursor_on();
3329 }
3330 out_flush();
3331 ml_close_all(TRUE); /* remove all memfiles */
3332 may_core_dump();
3333#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 gui_exit(r);
3336#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003337
Bram Moolenaar56718732006-03-15 22:53:57 +00003338#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003339 mac_conv_cleanup();
3340#endif
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#ifdef __QNX__
3343 /* A core dump won't be created if the signal handler
3344 * doesn't return, so we can't call exit() */
3345 if (deadly_signal != 0)
3346 return;
3347#endif
3348
Bram Moolenaar009b2592004-10-24 19:18:58 +00003349#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003350 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003351#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003352
3353#ifdef EXITFREE
3354 free_all_mem();
3355#endif
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 exit(r);
3358}
3359
3360 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003361may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 if (deadly_signal != 0)
3364 {
3365 signal(deadly_signal, SIG_DFL);
3366 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3367 }
3368}
3369
3370#ifndef VMS
3371
3372 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003373mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 static int first = TRUE;
3376
3377 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3378#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3379 /*
3380 * for "new" tty systems
3381 */
3382# ifdef HAVE_TERMIOS_H
3383 static struct termios told;
3384 struct termios tnew;
3385# else
3386 static struct termio told;
3387 struct termio tnew;
3388# endif
3389
3390 if (first)
3391 {
3392 first = FALSE;
3393# if defined(HAVE_TERMIOS_H)
3394 tcgetattr(read_cmd_fd, &told);
3395# else
3396 ioctl(read_cmd_fd, TCGETA, &told);
3397# endif
3398 }
3399
3400 tnew = told;
3401 if (tmode == TMODE_RAW)
3402 {
3403 /*
3404 * ~ICRNL enables typing ^V^M
3405 */
3406 tnew.c_iflag &= ~ICRNL;
3407 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3408# if defined(IEXTEN) && !defined(__MINT__)
3409 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3410 /* but it breaks function keys on MINT */
3411# endif
3412 );
3413# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3414 tnew.c_oflag &= ~ONLCR;
3415# endif
3416 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3417 tnew.c_cc[VTIME] = 0; /* don't wait */
3418 }
3419 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003420 {
3421 /* Also reset ICANON here, otherwise on Solaris select() won't see
3422 * typeahead characters. */
3423 tnew.c_lflag &= ~(ICANON | ECHO);
3424 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3425 tnew.c_cc[VTIME] = 0; /* don't wait */
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428# if defined(HAVE_TERMIOS_H)
3429 {
3430 int n = 10;
3431
3432 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3433 * few times. */
3434 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3435 && errno == EINTR && n > 0)
3436 --n;
3437 }
3438# else
3439 ioctl(read_cmd_fd, TCSETA, &tnew);
3440# endif
3441
3442#else
3443
3444 /*
3445 * for "old" tty systems
3446 */
3447# ifndef TIOCSETN
3448# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3449# endif
3450 static struct sgttyb ttybold;
3451 struct sgttyb ttybnew;
3452
3453 if (first)
3454 {
3455 first = FALSE;
3456 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3457 }
3458
3459 ttybnew = ttybold;
3460 if (tmode == TMODE_RAW)
3461 {
3462 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3463 ttybnew.sg_flags |= RAW;
3464 }
3465 else if (tmode == TMODE_SLEEP)
3466 ttybnew.sg_flags &= ~(ECHO);
3467 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3468#endif
3469 curr_tmode = tmode;
3470}
3471
3472/*
3473 * Try to get the code for "t_kb" from the stty setting
3474 *
3475 * Even if termcap claims a backspace key, the user's setting *should*
3476 * prevail. stty knows more about reality than termcap does, and if
3477 * somebody's usual erase key is DEL (which, for most BSD users, it will
3478 * be), they're going to get really annoyed if their erase key starts
3479 * doing forward deletes for no reason. (Eric Fischer)
3480 */
3481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003482get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 char_u buf[2];
3485 char_u *p;
3486
3487 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3488#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3489 /* for "new" tty systems */
3490# ifdef HAVE_TERMIOS_H
3491 struct termios keys;
3492# else
3493 struct termio keys;
3494# endif
3495
3496# if defined(HAVE_TERMIOS_H)
3497 if (tcgetattr(read_cmd_fd, &keys) != -1)
3498# else
3499 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3500# endif
3501 {
3502 buf[0] = keys.c_cc[VERASE];
3503 intr_char = keys.c_cc[VINTR];
3504#else
3505 /* for "old" tty systems */
3506 struct sgttyb keys;
3507
3508 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3509 {
3510 buf[0] = keys.sg_erase;
3511 intr_char = keys.sg_kill;
3512#endif
3513 buf[1] = NUL;
3514 add_termcode((char_u *)"kb", buf, FALSE);
3515
3516 /*
3517 * If <BS> and <DEL> are now the same, redefine <DEL>.
3518 */
3519 p = find_termcode((char_u *)"kD");
3520 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3521 do_fixdel(NULL);
3522 }
3523#if 0
3524 } /* to keep cindent happy */
3525#endif
3526}
3527
3528#endif /* VMS */
3529
3530#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3531/*
3532 * Set mouse clicks on or off.
3533 */
3534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003535mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 static int ison = FALSE;
3538 int xterm_mouse_vers;
3539
3540 if (on == ison) /* return quickly if nothing to do */
3541 return;
3542
3543 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003544
3545# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003546 if (ttym_flags == TTYM_URXVT)
3547 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003548 out_str_nf((char_u *)
3549 (on
3550 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3551 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3552 ison = on;
3553 }
3554# endif
3555
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003556# ifdef FEAT_MOUSE_SGR
3557 if (ttym_flags == TTYM_SGR)
3558 {
3559 out_str_nf((char_u *)
3560 (on
3561 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3562 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3563 ison = on;
3564 }
3565# endif
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 if (xterm_mouse_vers > 0)
3568 {
3569 if (on) /* enable mouse events, use mouse tracking if available */
3570 out_str_nf((char_u *)
3571 (xterm_mouse_vers > 1
3572 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3573 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3574 else /* disable mouse events, could probably always send the same */
3575 out_str_nf((char_u *)
3576 (xterm_mouse_vers > 1
3577 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3578 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3579 ison = on;
3580 }
3581
3582# ifdef FEAT_MOUSE_DEC
3583 else if (ttym_flags == TTYM_DEC)
3584 {
3585 if (on) /* enable mouse events */
3586 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3587 else /* disable mouse events */
3588 out_str_nf((char_u *)"\033['z");
3589 ison = on;
3590 }
3591# endif
3592
3593# ifdef FEAT_MOUSE_GPM
3594 else
3595 {
3596 if (on)
3597 {
3598 if (gpm_open())
3599 ison = TRUE;
3600 }
3601 else
3602 {
3603 gpm_close();
3604 ison = FALSE;
3605 }
3606 }
3607# endif
3608
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003609# ifdef FEAT_SYSMOUSE
3610 else
3611 {
3612 if (on)
3613 {
3614 if (sysmouse_open() == OK)
3615 ison = TRUE;
3616 }
3617 else
3618 {
3619 sysmouse_close();
3620 ison = FALSE;
3621 }
3622 }
3623# endif
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625# ifdef FEAT_MOUSE_JSB
3626 else
3627 {
3628 if (on)
3629 {
3630 /* D - Enable Mouse up/down messages
3631 * L - Enable Left Button Reporting
3632 * M - Enable Middle Button Reporting
3633 * R - Enable Right Button Reporting
3634 * K - Enable SHIFT and CTRL key Reporting
3635 * + - Enable Advanced messaging of mouse moves and up/down messages
3636 * Q - Quiet No Ack
3637 * # - Numeric value of mouse pointer required
3638 * 0 = Multiview 2000 cursor, used as standard
3639 * 1 = Windows Arrow
3640 * 2 = Windows I Beam
3641 * 3 = Windows Hour Glass
3642 * 4 = Windows Cross Hair
3643 * 5 = Windows UP Arrow
3644 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003645# ifdef JSBTERM_MOUSE_NONADVANCED
3646 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3648 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003649# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3651 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 ison = TRUE;
3654 }
3655 else
3656 {
3657 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3658 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3659 ison = FALSE;
3660 }
3661 }
3662# endif
3663# ifdef FEAT_MOUSE_PTERM
3664 else
3665 {
3666 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3667 if (on)
3668 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3669 else
3670 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3671 ison = on;
3672 }
3673# endif
3674}
3675
3676/*
3677 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3678 */
3679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003680check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682# ifdef FEAT_MOUSE_XTERM
3683 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003684# ifdef FEAT_MOUSE_URXVT
3685 && use_xterm_mouse() != 3
3686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687# ifdef FEAT_GUI
3688 && !gui.in_use
3689# endif
3690 )
3691 {
3692 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003693 ? IF_EB("\233M", CSI_STR "M")
3694 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 if (*p_mouse != NUL)
3696 {
3697 /* force mouse off and maybe on to send possibly new mouse
3698 * activation sequence to the xterm, with(out) drag tracing. */
3699 mch_setmouse(FALSE);
3700 setmouse();
3701 }
3702 }
3703 else
3704 del_mouse_termcode(KS_MOUSE);
3705# endif
3706
3707# ifdef FEAT_MOUSE_GPM
3708 if (!use_xterm_mouse()
3709# ifdef FEAT_GUI
3710 && !gui.in_use
3711# endif
3712 )
3713 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3714# endif
3715
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003716# ifdef FEAT_SYSMOUSE
3717 if (!use_xterm_mouse()
3718# ifdef FEAT_GUI
3719 && !gui.in_use
3720# endif
3721 )
3722 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3723# endif
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003726 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (!use_xterm_mouse()
3728# ifdef FEAT_GUI
3729 && !gui.in_use
3730# endif
3731 )
3732 set_mouse_termcode(KS_JSBTERM_MOUSE,
3733 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3734 else
3735 del_mouse_termcode(KS_JSBTERM_MOUSE);
3736# endif
3737
3738# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003739 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 * define it in the GUI or when using an xterm. */
3741 if (!use_xterm_mouse()
3742# ifdef FEAT_GUI
3743 && !gui.in_use
3744# endif
3745 )
3746 set_mouse_termcode(KS_NETTERM_MOUSE,
3747 (char_u *)IF_EB("\033}", ESC_STR "}"));
3748 else
3749 del_mouse_termcode(KS_NETTERM_MOUSE);
3750# endif
3751
3752# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003753 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003754 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755# ifdef FEAT_GUI
3756 && !gui.in_use
3757# endif
3758 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003759 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3760 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 else
3762 del_mouse_termcode(KS_DEC_MOUSE);
3763# endif
3764# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003765 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003766 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767# ifdef FEAT_GUI
3768 && !gui.in_use
3769# endif
3770 )
3771 set_mouse_termcode(KS_PTERM_MOUSE,
3772 (char_u *) IF_EB("\033[", ESC_STR "["));
3773 else
3774 del_mouse_termcode(KS_PTERM_MOUSE);
3775# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003776# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003777 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003778# ifdef FEAT_GUI
3779 && !gui.in_use
3780# endif
3781 )
3782 {
3783 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003784 ? IF_EB("\233*M", CSI_STR "*M")
3785 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003786
3787 if (*p_mouse != NUL)
3788 {
3789 mch_setmouse(FALSE);
3790 setmouse();
3791 }
3792 }
3793 else
3794 del_mouse_termcode(KS_URXVT_MOUSE);
3795# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003796# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003797 if (use_xterm_mouse() == 4
3798# ifdef FEAT_GUI
3799 && !gui.in_use
3800# endif
3801 )
3802 {
3803 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003804 ? IF_EB("\233<*M", CSI_STR "<*M")
3805 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3806
3807 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3808 ? IF_EB("\233<*m", CSI_STR "<*m")
3809 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003810
3811 if (*p_mouse != NUL)
3812 {
3813 mch_setmouse(FALSE);
3814 setmouse();
3815 }
3816 }
3817 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003818 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003819 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003820 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3821 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823}
3824#endif
3825
3826/*
3827 * set screen mode, always fails.
3828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003830mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832 EMSG(_(e_screenmode));
3833 return FAIL;
3834}
3835
3836#ifndef VMS
3837
3838/*
3839 * Try to get the current window size:
3840 * 1. with an ioctl(), most accurate method
3841 * 2. from the environment variables LINES and COLUMNS
3842 * 3. from the termcap
3843 * 4. keep using the old values
3844 * Return OK when size could be determined, FAIL otherwise.
3845 */
3846 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003847mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849 long rows = 0;
3850 long columns = 0;
3851 char_u *p;
3852
3853 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 * 1. try using an ioctl. It is the most accurate method.
3855 *
3856 * Try using TIOCGWINSZ first, some systems that have it also define
3857 * TIOCGSIZE but don't have a struct ttysize.
3858 */
3859# ifdef TIOCGWINSZ
3860 {
3861 struct winsize ws;
3862 int fd = 1;
3863
3864 /* When stdout is not a tty, use stdin for the ioctl(). */
3865 if (!isatty(fd) && isatty(read_cmd_fd))
3866 fd = read_cmd_fd;
3867 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3868 {
3869 columns = ws.ws_col;
3870 rows = ws.ws_row;
3871 }
3872 }
3873# else /* TIOCGWINSZ */
3874# ifdef TIOCGSIZE
3875 {
3876 struct ttysize ts;
3877 int fd = 1;
3878
3879 /* When stdout is not a tty, use stdin for the ioctl(). */
3880 if (!isatty(fd) && isatty(read_cmd_fd))
3881 fd = read_cmd_fd;
3882 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3883 {
3884 columns = ts.ts_cols;
3885 rows = ts.ts_lines;
3886 }
3887 }
3888# endif /* TIOCGSIZE */
3889# endif /* TIOCGWINSZ */
3890
3891 /*
3892 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003893 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3894 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003896 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898 if ((p = (char_u *)getenv("LINES")))
3899 rows = atoi((char *)p);
3900 if ((p = (char_u *)getenv("COLUMNS")))
3901 columns = atoi((char *)p);
3902 }
3903
3904#ifdef HAVE_TGETENT
3905 /*
3906 * 3. try reading "co" and "li" entries from termcap
3907 */
3908 if (columns == 0 || rows == 0)
3909 getlinecol(&columns, &rows);
3910#endif
3911
3912 /*
3913 * 4. If everything fails, use the old values
3914 */
3915 if (columns <= 0 || rows <= 0)
3916 return FAIL;
3917
3918 Rows = rows;
3919 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003920 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return OK;
3922}
3923
Bram Moolenaarb13501f2017-07-22 22:32:56 +02003924#if defined(FEAT_TERMINAL) || defined(PROTO)
3925/*
3926 * Report the windows size "rows" and "cols" to tty "fd".
3927 */
3928 int
3929mch_report_winsize(int fd, int rows, int cols)
3930{
3931# ifdef TIOCSWINSZ
3932 struct winsize ws;
3933
3934 ws.ws_col = cols;
3935 ws.ws_row = rows;
3936 ws.ws_xpixel = cols * 5;
3937 ws.ws_ypixel = rows * 10;
3938 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
3939 {
3940 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
3941 return OK;
3942 }
3943 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
3944# else
3945# ifdef TIOCSSIZE
3946 struct ttysize ts;
3947
3948 ts.ts_cols = cols;
3949 ts.ts_lines = rows;
3950 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
3951 {
3952 ch_log(NULL, "ioctl(TIOCSSIZE) success");
3953 return OK;
3954 }
3955 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
3956# endif
3957# endif
3958 return FAIL;
3959}
3960#endif
3961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962/*
3963 * Try to set the window size to Rows and Columns.
3964 */
3965 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003966mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
3968 if (*T_CWS)
3969 {
3970 /*
3971 * NOTE: if you get an error here that term_set_winsize() is
3972 * undefined, check the output of configure. It could probably not
3973 * find a ncurses, termcap or termlib library.
3974 */
3975 term_set_winsize((int)Rows, (int)Columns);
3976 out_flush();
3977 screen_start(); /* don't know where cursor is now */
3978 }
3979}
3980
3981#endif /* VMS */
3982
3983/*
3984 * Rows and/or Columns has changed.
3985 */
3986 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003987mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
3989 /* Nothing to do. */
3990}
3991
Bram Moolenaar205b8862011-09-07 15:04:31 +02003992/*
3993 * Wait for process "child" to end.
3994 * Return "child" if it exited properly, <= 0 on error.
3995 */
3996 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01003997wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02003998{
3999 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004000 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004001
4002 while (wait_pid != child)
4003 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004004 /* When compiled with Python threads are probably used, in which case
4005 * wait() sometimes hangs for no obvious reason. Use waitpid()
4006 * instead and loop (like the GUI). Also needed for other interfaces,
4007 * they might call system(). */
4008# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004009 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004010# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004011 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004012# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004013 if (wait_pid == 0)
4014 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004015 /* Wait for 1 to 10 msec before trying again. */
4016 mch_delay(delay_msec, TRUE);
4017 if (++delay_msec > 10)
4018 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004019 continue;
4020 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004021 if (wait_pid <= 0
4022# ifdef ECHILD
4023 && errno == ECHILD
4024# endif
4025 )
4026 break;
4027 }
4028 return wait_pid;
4029}
4030
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004031#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01004032/*
4033 * Parse "cmd" and put the white-separated parts in "argv".
4034 * "argv" is an allocated array with "argc" entries.
4035 * Returns FAIL when out of memory.
4036 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004037 int
4038mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
4039{
4040 int i;
4041 char_u *p;
4042 int inquote;
4043
4044 /*
4045 * Do this loop twice:
4046 * 1: find number of arguments
4047 * 2: separate them and build argv[]
4048 */
4049 for (i = 0; i < 2; ++i)
4050 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02004051 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004052 inquote = FALSE;
4053 *argc = 0;
4054 for (;;)
4055 {
4056 if (i == 1)
4057 (*argv)[*argc] = (char *)p;
4058 ++*argc;
4059 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4060 {
4061 if (*p == '"')
4062 inquote = !inquote;
4063 ++p;
4064 }
4065 if (*p == NUL)
4066 break;
4067 if (i == 1)
4068 *p++ = NUL;
4069 p = skipwhite(p);
4070 }
4071 if (*argv == NULL)
4072 {
4073 if (use_shcf)
4074 {
4075 /* Account for possible multiple args in p_shcf. */
4076 p = p_shcf;
4077 for (;;)
4078 {
4079 p = skiptowhite(p);
4080 if (*p == NUL)
4081 break;
4082 ++*argc;
4083 p = skipwhite(p);
4084 }
4085 }
4086
4087 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4088 if (*argv == NULL) /* out of memory */
4089 return FAIL;
4090 }
4091 }
4092 return OK;
4093}
4094#endif
4095
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004096#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004097 static void
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004098set_child_environment(long rows, long columns, char *term)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004099{
4100# ifdef HAVE_SETENV
4101 char envbuf[50];
4102# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004103 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004104 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004105 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004106 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004107 static char envbuf_Colors[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004108# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004109 long colors =
4110# ifdef FEAT_GUI
4111 gui.in_use ? 256*256*256 :
4112# endif
4113 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004114
4115 /* Simulate to have a dumb terminal (for now) */
4116# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004117 setenv("TERM", term, 1);
4118 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004119 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004120 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004121 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004122 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004123 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004124 sprintf((char *)envbuf, "%ld", colors);
4125 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004126# else
4127 /*
4128 * Putenv does not copy the string, it has to remain valid.
4129 * Use a static array to avoid losing allocated memory.
4130 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004131 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4132 putenv(envbuf_Term);
4133 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004134 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004135 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4136 putenv(envbuf_Lines);
4137 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4138 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004139 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004140 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4141 putenv(envbuf_Colors);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004142# endif
4143}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004144
4145 static void
4146set_default_child_environment(void)
4147{
4148 set_child_environment(Rows, Columns, "dumb");
4149}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004150#endif
4151
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004152#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004153/*
4154 * Open a PTY, with FD for the master and slave side.
4155 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4156 * When successful both file descriptors are stored.
4157 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004158 static void
4159open_pty(int *pty_master_fd, int *pty_slave_fd)
4160{
4161 char *tty_name;
4162
4163 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4164 if (*pty_master_fd >= 0)
4165 {
4166 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4167 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4168 * adding O_NOCTTY always works when defined. */
4169#ifdef O_NOCTTY
4170 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4171#else
4172 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4173#endif
4174 if (*pty_slave_fd < 0)
4175 {
4176 close(*pty_master_fd);
4177 *pty_master_fd = -1;
4178 }
4179 }
4180}
4181#endif
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004184mch_call_shell(
4185 char_u *cmd,
4186 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188#ifdef VMS
4189 char *ifn = NULL;
4190 char *ofn = NULL;
4191#endif
4192 int tmode = cur_tmode;
4193#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004194 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004195 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197 out_flush();
4198
4199 if (options & SHELL_COOKED)
4200 settmode(TMODE_COOK); /* set to normal mode */
4201
Bram Moolenaar62b42182010-09-21 22:09:37 +02004202# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004203 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004204 loose_clipboard();
4205# endif
4206
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (cmd == NULL)
4208 x = system((char *)p_sh);
4209 else
4210 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004211# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (ofn = strchr((char *)cmd, '>'))
4213 *ofn++ = '\0';
4214 if (ifn = strchr((char *)cmd, '<'))
4215 {
4216 char *p;
4217
4218 *ifn++ = '\0';
4219 p = strchr(ifn,' '); /* chop off any trailing spaces */
4220 if (p)
4221 *p = '\0';
4222 }
4223 if (ofn)
4224 x = vms_sys((char *)cmd, ofn, ifn);
4225 else
4226 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004227# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 newcmd = lalloc(STRLEN(p_sh)
4229 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4230 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4231 if (newcmd == NULL)
4232 x = 0;
4233 else
4234 {
4235 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4236 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4237 (char *)p_shcf,
4238 (char *)cmd);
4239 x = system((char *)newcmd);
4240 vim_free(newcmd);
4241 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244# ifdef VMS
4245 x = vms_sys_status(x);
4246# endif
4247 if (emsg_silent)
4248 ;
4249 else if (x == 127)
4250 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 else if (x && !(options & SHELL_SILENT))
4252 {
4253 MSG_PUTS(_("\nshell returned "));
4254 msg_outnum((long)x);
4255 msg_putchar('\n');
4256 }
4257
4258 if (tmode == TMODE_RAW)
4259 settmode(TMODE_RAW); /* set to raw mode */
4260# ifdef FEAT_TITLE
4261 resettitle();
4262# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004263# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4264 restore_clipboard();
4265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return x;
4267
4268#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4269
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4271 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
Bram Moolenaar835dc632016-02-07 14:27:38 +01004273 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004275 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 pid_t wait_pid = 0;
4277# ifdef HAVE_UNION_WAIT
4278 union wait status;
4279# else
4280 int status = -1;
4281# endif
4282 int retval = -1;
4283 char **argv = NULL;
4284 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004285 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 int i;
4287 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004289# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004291# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004292 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int fd_fromshell[2];
4294 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004295 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar62b42182010-09-21 22:09:37 +02004297 newcmd = vim_strsave(p_sh);
4298 if (newcmd == NULL) /* out of memory */
4299 goto error;
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 out_flush();
4302 if (options & SHELL_COOKED)
4303 settmode(TMODE_COOK); /* set to normal mode */
4304
Bram Moolenaar835dc632016-02-07 14:27:38 +01004305 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4306 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 if (cmd != NULL)
4309 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004310 char_u *s;
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (extra_shell_arg != NULL)
4313 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004314
4315 /* Break 'shellcmdflag' into white separated parts. This doesn't
4316 * handle quoted strings, they are very unlikely to appear. */
4317 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4318 if (p_shcf_copy == NULL) /* out of memory */
4319 goto error;
4320 s = p_shcf_copy;
4321 p = p_shcf;
4322 while (*p != NUL)
4323 {
4324 argv[argc++] = (char *)s;
4325 while (*p && *p != ' ' && *p != TAB)
4326 *s++ = *p++;
4327 *s++ = NUL;
4328 p = skipwhite(p);
4329 }
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 argv[argc++] = (char *)cmd;
4332 }
4333 argv[argc] = NULL;
4334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004336 * For the GUI, when writing the output into the buffer and when reading
4337 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4338 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004340 if ((options & (SHELL_READ|SHELL_WRITE))
4341# ifdef FEAT_GUI
4342 || (gui.in_use && show_shell_mess)
4343# endif
4344 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004346# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 /*
4348 * Try to open a master pty.
4349 * If this works, open the slave pty.
4350 * If the slave can't be opened, close the master pty.
4351 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004352 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004353 open_pty(&pty_master_fd, &pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /*
4355 * If not opening a pty or it didn't work, try using pipes.
4356 */
4357 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
4360 pipe_error = (pipe(fd_toshell) < 0);
4361 if (!pipe_error) /* pipe create OK */
4362 {
4363 pipe_error = (pipe(fd_fromshell) < 0);
4364 if (pipe_error) /* pipe create failed */
4365 {
4366 close(fd_toshell[0]);
4367 close(fd_toshell[1]);
4368 }
4369 }
4370 if (pipe_error)
4371 {
4372 MSG_PUTS(_("\nCannot create pipes\n"));
4373 out_flush();
4374 }
4375 }
4376 }
4377
4378 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004380 SIGSET_DECL(curset)
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382# ifdef __BEOS__
4383 beos_cleanup_read_thread();
4384# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004385
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004386 BLOCK_SIGNALS(&curset);
4387 pid = fork(); /* maybe we should use vfork() */
4388 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004390 UNBLOCK_SIGNALS(&curset);
4391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004393 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004395 || (gui.in_use && show_shell_mess)
4396# endif
4397 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004399# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (pty_master_fd >= 0) /* close the pseudo tty */
4401 {
4402 close(pty_master_fd);
4403 close(pty_slave_fd);
4404 }
4405 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
4408 close(fd_toshell[0]);
4409 close(fd_toshell[1]);
4410 close(fd_fromshell[0]);
4411 close(fd_fromshell[1]);
4412 }
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 else if (pid == 0) /* child */
4416 {
4417 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004418 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 if (!show_shell_mess || (options & SHELL_EXPAND))
4421 {
4422 int fd;
4423
4424 /*
4425 * Don't want to show any message from the shell. Can't just
4426 * close stdout and stderr though, because some systems will
4427 * break if you try to write to them after that, so we must
4428 * use dup() to replace them with something else -- webb
4429 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4430 * waiting for input.
4431 */
4432 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4433 fclose(stdin);
4434 fclose(stdout);
4435 fclose(stderr);
4436
4437 /*
4438 * If any of these open()'s and dup()'s fail, we just continue
4439 * anyway. It's not fatal, and on most systems it will make
4440 * no difference at all. On a few it will cause the execvp()
4441 * to exit with a non-zero status even when the completion
4442 * could be done, which is nothing too serious. If the open()
4443 * or dup() failed we'd just do the same thing ourselves
4444 * anyway -- webb
4445 */
4446 if (fd >= 0)
4447 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004448 ignored = dup(fd); /* To replace stdin (fd 0) */
4449 ignored = dup(fd); /* To replace stdout (fd 1) */
4450 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 /* Don't need this now that we've duplicated it */
4453 close(fd);
4454 }
4455 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004456 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004458 || gui.in_use
4459# endif
4460 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
4462
Bram Moolenaardf177f62005-02-22 08:39:57 +00004463# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004464 /* Create our own process group, so that the child and all its
4465 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004466 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004467 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004468 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004469 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004470# if defined(SIGHUP)
4471 /* When doing "!xterm&" and 'shell' is bash: the shell
4472 * will exit and send SIGHUP to all processes in its
4473 * group, killing the just started process. Ignore SIGHUP
4474 * to avoid that. (suggested by Simon Schubert)
4475 */
4476 signal(SIGHUP, SIG_IGN);
4477# endif
4478 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004479# endif
4480# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004481 if (pty_slave_fd >= 0)
4482 {
4483 /* push stream discipline modules */
4484 if (options & SHELL_COOKED)
4485 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004487 /* Try to become controlling tty (probably doesn't work,
4488 * unless run by root) */
4489 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004491 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004492# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004493 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaara5792f52005-11-23 21:25:05 +00004495 /*
4496 * stderr is only redirected when using the GUI, so that a
4497 * program like gpg can still access the terminal to get a
4498 * passphrase using stderr.
4499 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004500# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (pty_master_fd >= 0)
4502 {
4503 close(pty_master_fd); /* close master side of pty */
4504
4505 /* set up stdin/stdout/stderr for the child */
4506 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004507 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004509 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004510 if (gui.in_use)
4511 {
4512 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004513 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 close(pty_slave_fd); /* has been dupped, close it now */
4517 }
4518 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 /* set up stdin for the child */
4522 close(fd_toshell[1]);
4523 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004524 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 close(fd_toshell[0]);
4526
4527 /* set up stdout for the child */
4528 close(fd_fromshell[0]);
4529 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004530 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 close(fd_fromshell[1]);
4532
Bram Moolenaara5792f52005-11-23 21:25:05 +00004533# ifdef FEAT_GUI
4534 if (gui.in_use)
4535 {
4536 /* set up stderr for the child */
4537 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004538 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004539 }
4540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 /*
4545 * There is no type cast for the argv, because the type may be
4546 * different on different machines. This may cause a warning
4547 * message with strict compilers, don't worry about it.
4548 * Call _exit() instead of exit() to avoid closing the connection
4549 * to the X server (esp. with GTK, which uses atexit()).
4550 */
4551 execvp(argv[0], argv);
4552 _exit(EXEC_FAILED); /* exec failed, return failure code */
4553 }
4554 else /* parent */
4555 {
4556 /*
4557 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004558 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 */
4560 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004561 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004562 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004563# ifdef FEAT_JOB_CHANNEL
4564 ++dont_check_job_ended;
4565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 /*
4567 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004568 * This is also used to pipe stdin/stdout to/from the external
4569 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004571 if ((options & (SHELL_READ|SHELL_WRITE))
4572# ifdef FEAT_GUI
4573 || (gui.in_use && show_shell_mess)
4574# endif
4575 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4583 int ta_len = 0; /* valid bytes in ta_buf[] */
4584 int len;
4585 int p_more_save;
4586 int old_State;
4587 int c;
4588 int toshell_fd;
4589 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590 garray_T ga;
4591 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004592# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4593 struct timeval start_tv;
4594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (pty_master_fd >= 0)
4598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 fromshell_fd = pty_master_fd;
4600 toshell_fd = dup(pty_master_fd);
4601 }
4602 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
4605 close(fd_toshell[0]);
4606 close(fd_fromshell[1]);
4607 toshell_fd = fd_toshell[1];
4608 fromshell_fd = fd_fromshell[0];
4609 }
4610
4611 /*
4612 * Write to the child if there are typed characters.
4613 * Read from the child if there are characters available.
4614 * Repeat the reading a few times if more characters are
4615 * available. Need to check for typed keys now and then, but
4616 * not too often (delays when no chars are available).
4617 * This loop is quit if no characters can be read from the pty
4618 * (WaitForChar detected special condition), or there are no
4619 * characters available and the child has exited.
4620 * Only check if the child has exited when there is no more
4621 * output. The child may exit before all the output has
4622 * been printed.
4623 *
4624 * Currently this busy loops!
4625 * This can probably dead-lock when the write blocks!
4626 */
4627 p_more_save = p_more;
4628 p_more = FALSE;
4629 old_State = State;
4630 State = EXTERNCMD; /* don't redraw at window resize */
4631
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004632 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004633 {
4634 /* Fork a process that will write the lines to the
4635 * external program. */
4636 if ((wpid = fork()) == -1)
4637 {
4638 MSG_PUTS(_("\nCannot fork\n"));
4639 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004640 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004641 {
4642 linenr_T lnum = curbuf->b_op_start.lnum;
4643 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004644 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004645 size_t l;
4646
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004647 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004648 for (;;)
4649 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004650 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004651 if (l == 0)
4652 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004653 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004654 /* NL -> NUL translation */
4655 len = write(toshell_fd, "", (size_t)1);
4656 else
4657 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004658 char_u *s = vim_strchr(lp + written, NL);
4659
Bram Moolenaar89d40322006-08-29 15:30:07 +00004660 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004661 s == NULL ? l
4662 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004663 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004664 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004665 {
4666 /* Finished a line, add a NL, unless this line
4667 * should not have one. */
4668 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004669 || (!curbuf->b_p_bin
4670 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004671 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004672 && (lnum !=
4673 curbuf->b_ml.ml_line_count
4674 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004675 ignored = write(toshell_fd, "\n",
4676 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004677 ++lnum;
4678 if (lnum > curbuf->b_op_end.lnum)
4679 {
4680 /* finished all the lines, close pipe */
4681 close(toshell_fd);
4682 toshell_fd = -1;
4683 break;
4684 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004685 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004686 written = 0;
4687 }
4688 else if (len > 0)
4689 written += len;
4690 }
4691 _exit(0);
4692 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004693 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004694 {
4695 close(toshell_fd);
4696 toshell_fd = -1;
4697 }
4698 }
4699
4700 if (options & SHELL_READ)
4701 ga_init2(&ga, 1, BUFLEN);
4702
4703 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004704# ifdef ELAPSED_FUNC
4705 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 for (;;)
4708 {
4709 /*
4710 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004711 * if there are any.
4712 * Don't do this if we are expanding wild cards (would eat
4713 * typeahead).
4714 * Don't do this when filtering and terminal is in cooked
4715 * mode, the shell command will handle the I/O. Avoids
4716 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004717 * Don't get characters when the child has already
4718 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004719 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004720 * while (noread_cnt > 4), avoids that ":r !ls" eats
4721 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 */
4723 len = 0;
4724 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004725 && ((options &
4726 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4727 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004728# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004729 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004730# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004731 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004732 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004733 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004735 if (ta_len == 0)
4736 {
4737 /* Get extra characters when we don't have any.
4738 * Reset the counter and timer. */
4739 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004740# ifdef ELAPSED_FUNC
4741 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004742# endif
4743 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4744 }
4745 if (ta_len > 0 || len > 0)
4746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /*
4748 * For pipes:
4749 * Check for CTRL-C: send interrupt signal to child.
4750 * Check for CTRL-D: EOF, close pipe to child.
4751 */
4752 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4753 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004754# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 /*
4756 * Send SIGINT to the child's group or all
4757 * processes in our group.
4758 */
4759 if (ta_buf[ta_len] == Ctrl_C
4760 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004761 {
4762# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004767 if (wpid > 0)
4768 kill(wpid, SIGINT);
4769 }
4770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (pty_master_fd < 0 && toshell_fd >= 0
4772 && ta_buf[ta_len] == Ctrl_D)
4773 {
4774 close(toshell_fd);
4775 toshell_fd = -1;
4776 }
4777 }
4778
4779 /* replace K_BS by <BS> and K_DEL by <DEL> */
4780 for (i = ta_len; i < ta_len + len; ++i)
4781 {
4782 if (ta_buf[i] == CSI && len - i > 2)
4783 {
4784 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4785 if (c == K_DEL || c == K_KDEL || c == K_BS)
4786 {
4787 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4788 (size_t)(len - i - 2));
4789 if (c == K_DEL || c == K_KDEL)
4790 ta_buf[i] = DEL;
4791 else
4792 ta_buf[i] = Ctrl_H;
4793 len -= 2;
4794 }
4795 }
4796 else if (ta_buf[i] == '\r')
4797 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004798# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004800 i += (*mb_ptr2len_len)(ta_buf + i,
4801 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804
4805 /*
4806 * For pipes: echo the typed characters.
4807 * For a pty this does not seem to work.
4808 */
4809 if (pty_master_fd < 0)
4810 {
4811 for (i = ta_len; i < ta_len + len; ++i)
4812 {
4813 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4814 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 else if (has_mbyte)
4817 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004818 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 msg_outtrans_len(ta_buf + i, l);
4821 i += l - 1;
4822 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 else
4825 msg_outtrans_len(ta_buf + i, 1);
4826 }
4827 windgoto(msg_row, msg_col);
4828 out_flush();
4829 }
4830
4831 ta_len += len;
4832
4833 /*
4834 * Write the characters to the child, unless EOF has
4835 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004836 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004837 * When writing buffer lines, drop the typed
4838 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840 if (options & SHELL_WRITE)
4841 ta_len = 0;
4842 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4845 if (len > 0)
4846 {
4847 ta_len -= len;
4848 mch_memmove(ta_buf, ta_buf + len, ta_len);
4849 }
4850 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853
Bram Moolenaardf177f62005-02-22 08:39:57 +00004854 if (got_int)
4855 {
4856 /* CTRL-C sends a signal to the child, we ignore it
4857 * ourselves */
4858# ifdef HAVE_SETSID
4859 kill(-pid, SIGINT);
4860# else
4861 kill(0, SIGINT);
4862# endif
4863 if (wpid > 0)
4864 kill(wpid, SIGINT);
4865 got_int = FALSE;
4866 }
4867
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 /*
4869 * Check if the child has any characters to be printed.
4870 * Read them and write them to our window. Repeat this as
4871 * long as there is something to do, avoid the 10ms wait
4872 * for mch_inchar(), or sending typeahead characters to
4873 * the external process.
4874 * TODO: This should handle escape sequences, compatible
4875 * to some terminal (vt52?).
4876 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01004878 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004880 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 );
4887 if (len <= 0) /* end of file or error */
4888 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004889
4890 noread_cnt = 0;
4891 if (options & SHELL_READ)
4892 {
4893 /* Do NUL -> NL translation, append NL separated
4894 * lines to the current buffer. */
4895 for (i = 0; i < len; ++i)
4896 {
4897 if (buffer[i] == NL)
4898 append_ga_line(&ga);
4899 else if (buffer[i] == NUL)
4900 ga_append(&ga, NL);
4901 else
4902 ga_append(&ga, buffer[i]);
4903 }
4904 }
4905# ifdef FEAT_MBYTE
4906 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 int l;
4909
Bram Moolenaardf177f62005-02-22 08:39:57 +00004910 len += buffer_off;
4911 buffer[len] = NUL;
4912
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 /* Check if the last character in buffer[] is
4914 * incomplete, keep these bytes for the next
4915 * round. */
4916 for (p = buffer; p < buffer + len; p += l)
4917 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004918 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (l == 0)
4920 l = 1; /* NUL byte? */
4921 else if (MB_BYTE2LEN(*p) != l)
4922 break;
4923 }
4924 if (p == buffer) /* no complete character */
4925 {
4926 /* avoid getting stuck at an illegal byte */
4927 if (len >= 12)
4928 ++p;
4929 else
4930 {
4931 buffer_off = len;
4932 continue;
4933 }
4934 }
4935 c = *p;
4936 *p = NUL;
4937 msg_puts(buffer);
4938 if (p < buffer + len)
4939 {
4940 *p = c;
4941 buffer_off = (buffer + len) - p;
4942 mch_memmove(buffer, p, buffer_off);
4943 continue;
4944 }
4945 buffer_off = 0;
4946 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004947# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
4950 buffer[len] = NUL;
4951 msg_puts(buffer);
4952 }
4953
4954 windgoto(msg_row, msg_col);
4955 cursor_on();
4956 out_flush();
4957 if (got_int)
4958 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004959
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004960# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004961 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004962 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004963 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004964
4965 /* Avoid that we keep looping here without
4966 * checking for a CTRL-C for a long time. Don't
4967 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004968 if (msec > 2000)
4969 {
4970 noread_cnt = 5;
4971 break;
4972 }
4973 }
4974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004977 /* If we already detected the child has finished, continue
4978 * reading output for a short while. Some text may be
4979 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004980 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004981 {
4982 if (noread_cnt < 5)
4983 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004984 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004985 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004986
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 /*
4988 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004989 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004991# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004992 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004993# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4997 || (wait_pid == pid && WIFEXITED(status)))
4998 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004999 /* Don't break the loop yet, try reading more
5000 * characters from "fromshell_fd" first. When using
5001 * pipes there might still be something to read and
5002 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005005 else
5006 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005007
Bram Moolenaar95a51352013-03-21 22:53:50 +01005008# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005009 /* Handle any X events, e.g. serving the clipboard. */
5010 clip_update();
5011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013finished:
5014 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005015 if (options & SHELL_READ)
5016 {
5017 if (ga.ga_len > 0)
5018 {
5019 append_ga_line(&ga);
5020 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005021 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005022 }
5023 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005024 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005025 ga_clear(&ga);
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 /*
5029 * Give all typeahead that wasn't used back to ui_inchar().
5030 */
5031 if (ta_len)
5032 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 State = old_State;
5034 if (toshell_fd >= 0)
5035 close(toshell_fd);
5036 close(fromshell_fd);
5037 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005038# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005039 else
5040 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005041 long delay_msec = 1;
5042
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005043 /*
5044 * Similar to the loop above, but only handle X events, no
5045 * I/O.
5046 */
5047 for (;;)
5048 {
5049 if (got_int)
5050 {
5051 /* CTRL-C sends a signal to the child, we ignore it
5052 * ourselves */
5053# ifdef HAVE_SETSID
5054 kill(-pid, SIGINT);
5055# else
5056 kill(0, SIGINT);
5057# endif
5058 got_int = FALSE;
5059 }
5060# ifdef __NeXT__
5061 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5062# else
5063 wait_pid = waitpid(pid, &status, WNOHANG);
5064# endif
5065 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5066 || (wait_pid == pid && WIFEXITED(status)))
5067 {
5068 wait_pid = pid;
5069 break;
5070 }
5071
5072 /* Handle any X events, e.g. serving the clipboard. */
5073 clip_update();
5074
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005075 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5076 * less time. */
5077 mch_delay(delay_msec, TRUE);
5078 if (++delay_msec > 10)
5079 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005080 }
5081 }
5082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * Wait until our child has exited.
5086 * Ignore wait() returning pids of other children and returning
5087 * because of some signal like SIGWINCH.
5088 * Don't wait if wait_pid was already set above, indicating the
5089 * child already exited.
5090 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005091 if (wait_pid != pid)
5092 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
Bram Moolenaar624891f2010-10-13 16:22:09 +02005094# ifdef FEAT_GUI
5095 /* Close slave side of pty. Only do this after the child has
5096 * exited, otherwise the child may hang when it tries to write on
5097 * the pty. */
5098 if (pty_master_fd >= 0)
5099 close(pty_slave_fd);
5100# endif
5101
Bram Moolenaardf177f62005-02-22 08:39:57 +00005102 /* Make sure the child that writes to the external program is
5103 * dead. */
5104 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005105 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005106 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005107 wait4pid(wpid, NULL);
5108 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005109
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005110# ifdef FEAT_JOB_CHANNEL
5111 --dont_check_job_ended;
5112# endif
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 /*
5115 * Set to raw mode right now, otherwise a CTRL-C after
5116 * catch_signals() will kill Vim.
5117 */
5118 if (tmode == TMODE_RAW)
5119 settmode(TMODE_RAW);
5120 did_settmode = TRUE;
5121 set_signals();
5122
5123 if (WIFEXITED(status))
5124 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005125 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005127 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 if (retval == EXEC_FAILED)
5130 {
5131 MSG_PUTS(_("\nCannot execute shell "));
5132 msg_outtrans(p_sh);
5133 msg_putchar('\n');
5134 }
5135 else if (!(options & SHELL_SILENT))
5136 {
5137 MSG_PUTS(_("\nshell returned "));
5138 msg_outnum((long)retval);
5139 msg_putchar('\n');
5140 }
5141 }
5142 }
5143 else
5144 MSG_PUTS(_("\nCommand terminated\n"));
5145 }
5146 }
5147 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005148 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150error:
5151 if (!did_settmode)
5152 if (tmode == TMODE_RAW)
5153 settmode(TMODE_RAW); /* set to raw mode */
5154# ifdef FEAT_TITLE
5155 resettitle();
5156# endif
5157 vim_free(newcmd);
5158
5159 return retval;
5160
5161#endif /* USE_SYSTEM */
5162}
5163
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005164#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005165 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005166mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005167{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005168 pid_t pid;
5169 int fd_in[2]; /* for stdin */
5170 int fd_out[2]; /* for stdout */
5171 int fd_err[2]; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005172 int pty_master_fd = -1;
5173 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005174 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005175 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5176 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5177 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005178 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005179 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5180 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005181 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005182 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005183
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005184 if (use_out_for_err && use_null_for_out)
5185 use_null_for_err = TRUE;
5186
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005187 /* default is to fail */
5188 job->jv_status = JOB_FAILED;
5189 fd_in[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005190 fd_in[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005191 fd_out[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005192 fd_out[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005193 fd_err[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005194 fd_err[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005195
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005196 if (options->jo_pty)
5197 open_pty(&pty_master_fd, &pty_slave_fd);
5198
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005199 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005200 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005201 if (use_file_for_in)
5202 {
5203 char_u *fname = options->jo_io_name[PART_IN];
5204
5205 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5206 if (fd_in[0] < 0)
5207 {
5208 EMSG2(_(e_notopen), fname);
5209 goto failed;
5210 }
5211 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005212 else if (!use_null_for_in && pty_master_fd < 0 && pipe(fd_in) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005213 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005214
5215 if (use_file_for_out)
5216 {
5217 char_u *fname = options->jo_io_name[PART_OUT];
5218
5219 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5220 if (fd_out[1] < 0)
5221 {
5222 EMSG2(_(e_notopen), fname);
5223 goto failed;
5224 }
5225 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005226 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005227 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005228
5229 if (use_file_for_err)
5230 {
5231 char_u *fname = options->jo_io_name[PART_ERR];
5232
5233 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5234 if (fd_err[1] < 0)
5235 {
5236 EMSG2(_(e_notopen), fname);
5237 goto failed;
5238 }
5239 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005240 else if (!use_out_for_err && !use_null_for_err
5241 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005242 goto failed;
5243
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005244 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5245 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005246 if (options->jo_set & JO_CHANNEL)
5247 {
5248 channel = options->jo_channel;
5249 if (channel != NULL)
5250 ++channel->ch_refcount;
5251 }
5252 else
5253 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005254 if (channel == NULL)
5255 goto failed;
5256 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005257
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005258 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005259 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005260 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005261 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005262 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005263 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005264 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005265 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005266 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005267 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005268 int null_fd = -1;
5269 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005270
Bram Moolenaar835dc632016-02-07 14:27:38 +01005271 /* child */
5272 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005273 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005274
5275# ifdef HAVE_SETSID
5276 /* Create our own process group, so that the child and all its
5277 * children can be kill()ed. Don't do this when using pipes,
5278 * because stdin is not a tty, we would lose /dev/tty. */
5279 (void)setsid();
5280# endif
5281
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005282# ifdef FEAT_TERMINAL
5283 if (options->jo_term_rows > 0)
5284 set_child_environment(
5285 (long)options->jo_term_rows,
5286 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005287 STRNCMP(T_NAME, "xterm", 5) == 0
5288 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005289 else
5290# endif
5291 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005292
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005293 if (use_null_for_in || use_null_for_out || use_null_for_err)
5294 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5295
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005296 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005297 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005298 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005299 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005300 else if (fd_in[0] < 0)
5301 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005302 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005303 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005304
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005305 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005306 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005307 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005308 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005309 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005310 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005311 }
5312 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005313 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005314 else if (fd_err[1] < 0)
5315 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005316 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005317 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005318
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005319 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005320 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005321 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005322 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005323 else if (fd_out[1] < 0)
5324 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005325 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005326 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005327
5328 if (fd_in[0] >= 0)
5329 close(fd_in[0]);
5330 if (fd_in[1] >= 0)
5331 close(fd_in[1]);
5332 if (fd_out[0] >= 0)
5333 close(fd_out[0]);
5334 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005335 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005336 if (fd_err[0] >= 0)
5337 close(fd_err[0]);
5338 if (fd_err[1] >= 0)
5339 close(fd_err[1]);
5340 if (pty_master_fd >= 0)
5341 {
5342 close(pty_master_fd); /* not used */
5343 close(pty_slave_fd); /* duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005344 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005345
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005346 if (null_fd >= 0)
5347 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005348
Bram Moolenaar835dc632016-02-07 14:27:38 +01005349 /* See above for type of argv. */
5350 execvp(argv[0], argv);
5351
Bram Moolenaar4694a172016-04-21 14:05:23 +02005352 if (stderr_works)
5353 perror("executing job failed");
Bram Moolenaarcda77642016-06-04 13:32:35 +02005354#ifdef EXITFREE
5355 /* calling free_all_mem() here causes problems. Ignore valgrind
5356 * reporting possibly leaked memory. */
5357#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005358 _exit(EXEC_FAILED); /* exec failed, return failure code */
5359 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005360
5361 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005362 UNBLOCK_SIGNALS(&curset);
5363
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005364 job->jv_pid = pid;
5365 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005366 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005367
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005368 if (pty_master_fd >= 0)
5369 close(pty_slave_fd); /* duped above */
5370 /* close child stdin, stdout and stderr */
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005371 if (!use_file_for_in && fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005372 close(fd_in[0]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005373 if (!use_file_for_out && fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005374 close(fd_out[1]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005375 if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005376 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005377 if (channel != NULL)
5378 {
5379 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005380 use_file_for_in || use_null_for_in
5381 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5382 use_file_for_out || use_null_for_out
5383 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5384 use_out_for_err || use_file_for_err || use_null_for_err
5385 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005386 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005387 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005388 else
5389 {
5390 if (fd_in[1] >= 0)
5391 close(fd_in[1]);
5392 if (fd_out[0] >= 0)
5393 close(fd_out[0]);
5394 if (fd_err[0] >= 0)
5395 close(fd_err[0]);
5396 if (pty_master_fd >= 0)
5397 close(pty_master_fd);
5398 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005399
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005400 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005401 return;
5402
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005403failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005404 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005405 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005406 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005407 if (fd_in[1] >= 0)
5408 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005409 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005410 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005411 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005412 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005413 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005414 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005415 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005416 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005417 if (pty_master_fd >= 0)
5418 close(pty_master_fd);
5419 if (pty_slave_fd >= 0)
5420 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005421}
5422
5423 char *
5424mch_job_status(job_T *job)
5425{
5426# ifdef HAVE_UNION_WAIT
5427 union wait status;
5428# else
5429 int status = -1;
5430# endif
5431 pid_t wait_pid = 0;
5432
5433# ifdef __NeXT__
5434 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5435# else
5436 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5437# endif
5438 if (wait_pid == -1)
5439 {
5440 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005441 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005442 }
5443 if (wait_pid == 0)
5444 return "run";
5445 if (WIFEXITED(status))
5446 {
5447 /* LINTED avoid "bitwise operation on signed value" */
5448 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005449 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005450 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005451 if (WIFSIGNALED(status))
5452 {
5453 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005454 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005455 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005456 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005457
5458return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005459 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005460 {
5461 ch_log(job->jv_channel, "Job ended");
5462 job->jv_status = JOB_ENDED;
5463 }
5464 return "dead";
5465}
5466
5467 job_T *
5468mch_detect_ended_job(job_T *job_list)
5469{
5470# ifdef HAVE_UNION_WAIT
5471 union wait status;
5472# else
5473 int status = -1;
5474# endif
5475 pid_t wait_pid = 0;
5476 job_T *job;
5477
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005478# ifndef USE_SYSTEM
5479 /* Do not do this when waiting for a shell command to finish, we would get
5480 * the exit value here (and discard it), the exit value obtained there
5481 * would then be wrong. */
5482 if (dont_check_job_ended > 0)
5483 return NULL;
5484# endif
5485
Bram Moolenaar97792de2016-10-15 18:36:49 +02005486# ifdef __NeXT__
5487 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5488# else
5489 wait_pid = waitpid(-1, &status, WNOHANG);
5490# endif
5491 if (wait_pid <= 0)
5492 /* no process ended */
5493 return NULL;
5494 for (job = job_list; job != NULL; job = job->jv_next)
5495 {
5496 if (job->jv_pid == wait_pid)
5497 {
5498 if (WIFEXITED(status))
5499 /* LINTED avoid "bitwise operation on signed value" */
5500 job->jv_exitval = WEXITSTATUS(status);
5501 else if (WIFSIGNALED(status))
5502 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005503 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005504 {
5505 ch_log(job->jv_channel, "Job ended");
5506 job->jv_status = JOB_ENDED;
5507 }
5508 return job;
5509 }
5510 }
5511 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512}
5513
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005514/*
5515 * Send a (deadly) signal to "job".
5516 * Return FAIL if "how" is not a valid name.
5517 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005518 int
5519mch_stop_job(job_T *job, char_u *how)
5520{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005521 int sig = -1;
5522 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005523
Bram Moolenaar923d9262016-02-25 20:56:01 +01005524 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005525 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005526 else if (STRCMP(how, "hup") == 0)
5527 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005528 else if (STRCMP(how, "quit") == 0)
5529 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005530 else if (STRCMP(how, "int") == 0)
5531 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005532 else if (STRCMP(how, "kill") == 0)
5533 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005534#ifdef SIGWINCH
5535 else if (STRCMP(how, "winch") == 0)
5536 sig = SIGWINCH;
5537#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005538 else if (isdigit(*how))
5539 sig = atoi((char *)how);
5540 else
5541 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005542
Bram Moolenaar72801402016-02-09 11:37:50 +01005543 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005544 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005545#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005546 if (job_pid == getpgid(job_pid))
5547 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005548#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005549
Bram Moolenaar61a66052017-07-22 18:39:00 +02005550 /* Never kill ourselves! */
5551 if (job_pid != 0)
5552 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005553
Bram Moolenaar835dc632016-02-07 14:27:38 +01005554 return OK;
5555}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005556
5557/*
5558 * Clear the data related to "job".
5559 */
5560 void
5561mch_clear_job(job_T *job)
5562{
5563 /* call waitpid because child process may become zombie */
5564# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005565 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005566# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005567 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005568# endif
5569}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005570#endif
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572/*
5573 * Check for CTRL-C typed by reading all available characters.
5574 * In cooked mode we should get SIGINT, no need to check.
5575 */
5576 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005577mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005579 if ((curr_tmode == TMODE_RAW || force)
5580 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 fill_input_buf(FALSE);
5582}
5583
5584/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005585 * Wait "msec" msec until a character is available from the mouse, keyboard,
5586 * from inbuf[].
5587 * "msec" == -1 will block forever.
5588 * Invokes timer callbacks when needed.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005589 * "interrupted" (if not NULL) is set to TRUE when no character is available
5590 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005591 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005592 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 */
5594 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02005595WaitForChar(long msec, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005597#ifdef FEAT_TIMERS
5598 long due_time;
5599 long remaining = msec;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005600 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005601
5602 /* When waiting very briefly don't trigger timers. */
5603 if (msec >= 0 && msec < 10L)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005604 return WaitForCharOrMouse(msec, NULL);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005605
5606 while (msec < 0 || remaining > 0)
5607 {
5608 /* Trigger timers and then get the time in msec until the next one is
5609 * due. Wait up to that time. */
5610 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005611 if (typebuf.tb_change_cnt != tb_change_cnt)
5612 {
5613 /* timer may have used feedkeys() */
5614 return FALSE;
5615 }
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005616 if (due_time <= 0 || (msec > 0 && due_time > remaining))
5617 due_time = remaining;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005618 if (WaitForCharOrMouse(due_time, interrupted))
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005619 return TRUE;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005620 if (interrupted != NULL && *interrupted)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005621 /* Nothing available, but need to return so that side effects get
5622 * handled, such as handling a message on a channel. */
5623 return FALSE;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005624 if (msec > 0)
5625 remaining -= due_time;
5626 }
5627 return FALSE;
5628#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005629 return WaitForCharOrMouse(msec, interrupted);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005630#endif
5631}
5632
5633/*
5634 * Wait "msec" msec until a character is available from the mouse or keyboard
5635 * or from inbuf[].
5636 * "msec" == -1 will block forever.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005637 * "interrupted" (if not NULL) is set to TRUE when no character is available
5638 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005639 * When a GUI is being used, this will never get called -- webb
5640 */
5641 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02005642WaitForCharOrMouse(long msec, int *interrupted)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005643{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#ifdef FEAT_MOUSE_GPM
5645 int gpm_process_wanted;
5646#endif
5647#ifdef FEAT_XCLIPBOARD
5648 int rest;
5649#endif
5650 int avail;
5651
5652 if (input_available()) /* something in inbuf[] */
5653 return 1;
5654
5655#if defined(FEAT_MOUSE_DEC)
5656 /* May need to query the mouse position. */
5657 if (WantQueryMouse)
5658 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005659 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5661 }
5662#endif
5663
5664 /*
5665 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5666 * events. This is a bit complicated, because they might both be defined.
5667 */
5668#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5669# ifdef FEAT_XCLIPBOARD
5670 rest = 0;
5671 if (do_xterm_trace())
5672 rest = msec;
5673# endif
5674 do
5675 {
5676# ifdef FEAT_XCLIPBOARD
5677 if (rest != 0)
5678 {
5679 msec = XT_TRACE_DELAY;
5680 if (rest >= 0 && rest < XT_TRACE_DELAY)
5681 msec = rest;
5682 if (rest >= 0)
5683 rest -= msec;
5684 }
5685# endif
5686# ifdef FEAT_MOUSE_GPM
5687 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005688 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005689 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005691 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692# endif
5693 if (!avail)
5694 {
5695 if (input_available())
5696 return 1;
5697# ifdef FEAT_XCLIPBOARD
5698 if (rest == 0 || !do_xterm_trace())
5699# endif
5700 break;
5701 }
5702 }
5703 while (FALSE
5704# ifdef FEAT_MOUSE_GPM
5705 || (gpm_process_wanted && mch_gpm_process() == 0)
5706# endif
5707# ifdef FEAT_XCLIPBOARD
5708 || (!avail && rest != 0)
5709# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005710 )
5711 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005714 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715#endif
5716 return avail;
5717}
5718
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005719#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720/*
5721 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005722 * "msec" == 0 will check for characters once.
5723 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005726 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005727 * "interrupted" (if not NULL) is set to TRUE when no character is available
5728 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730#if defined(__BEOS__)
5731 int
5732#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005733 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005735RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005738 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005739#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 static int busy = FALSE;
5741
5742 /* May retry getting characters after an event was handled. */
5743# define MAY_LOOP
5744
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005745# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 /* Remember at what time we started, so that we know how much longer we
5747 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005748 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005749 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005751 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005752 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753# endif
5754
5755 /* Handle being called recursively. This may happen for the session
5756 * manager stuff, it may save the file, which does a breakcheck. */
5757 if (busy)
5758 return 0;
5759#endif
5760
5761#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005762 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#endif
5764 {
5765#ifdef MAY_LOOP
5766 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005767# ifdef FEAT_MZSCHEME
5768 int mzquantum_used = FALSE;
5769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770#endif
5771#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005772 /* each channel may use in, out and err */
5773 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 int nfd;
5775# ifdef FEAT_XCLIPBOARD
5776 int xterm_idx = -1;
5777# endif
5778# ifdef FEAT_MOUSE_GPM
5779 int gpm_idx = -1;
5780# endif
5781# ifdef USE_XSMP
5782 int xsmp_idx = -1;
5783# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005784 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005786# ifdef FEAT_MZSCHEME
5787 mzvim_check_threads();
5788 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5789 {
5790 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5791 mzquantum_used = TRUE;
5792 }
5793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 fds[0].fd = fd;
5795 fds[0].events = POLLIN;
5796 nfd = 1;
5797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005799 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (xterm_Shell != (Widget)0)
5801 {
5802 xterm_idx = nfd;
5803 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5804 fds[nfd].events = POLLIN;
5805 nfd++;
5806 }
5807# endif
5808# ifdef FEAT_MOUSE_GPM
5809 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5810 {
5811 gpm_idx = nfd;
5812 fds[nfd].fd = gpm_fd;
5813 fds[nfd].events = POLLIN;
5814 nfd++;
5815 }
5816# endif
5817# ifdef USE_XSMP
5818 if (xsmp_icefd != -1)
5819 {
5820 xsmp_idx = nfd;
5821 fds[nfd].fd = xsmp_icefd;
5822 fds[nfd].events = POLLIN;
5823 nfd++;
5824 }
5825# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005826#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005827 nfd = channel_poll_setup(nfd, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005828#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005829 if (interrupted != NULL)
5830 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005832 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005833
5834 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02005835 if (result == 0 && interrupted != NULL && ret > 0)
5836 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005837
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005838# ifdef FEAT_MZSCHEME
5839 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005840 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005841 finished = FALSE;
5842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844# ifdef FEAT_XCLIPBOARD
5845 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5846 {
5847 xterm_update(); /* Maybe we should hand out clipboard */
5848 if (--ret == 0 && !input_available())
5849 /* Try again */
5850 finished = FALSE;
5851 }
5852# endif
5853# ifdef FEAT_MOUSE_GPM
5854 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5855 {
5856 *check_for_gpm = 1;
5857 }
5858# endif
5859# ifdef USE_XSMP
5860 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5861 {
5862 if (fds[xsmp_idx].revents & POLLIN)
5863 {
5864 busy = TRUE;
5865 xsmp_handle_requests();
5866 busy = FALSE;
5867 }
5868 else if (fds[xsmp_idx].revents & POLLHUP)
5869 {
5870 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005871 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 xsmp_close();
5873 }
5874 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005875 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005878#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005879 if (ret > 0)
5880 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#else /* HAVE_SELECT */
5884
5885 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005886 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005887 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005889 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005891# ifdef FEAT_MZSCHEME
5892 mzvim_check_threads();
5893 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5894 {
5895 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5896 mzquantum_used = TRUE;
5897 }
5898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005900 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005902 tv.tv_sec = towait / 1000;
5903 tv.tv_usec = (towait % 1000) * (1000000/1000);
5904 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005906 else
5907 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
5909 /*
5910 * Select on ready for reading and exceptional condition (end of file).
5911 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005912select_eintr:
5913 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005914 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 FD_ZERO(&efds);
5916 FD_SET(fd, &rfds);
5917# if !defined(__QNX__) && !defined(__CYGWIN32__)
5918 /* For QNX select() always returns 1 if this is set. Why? */
5919 FD_SET(fd, &efds);
5920# endif
5921 maxfd = fd;
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005924 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (xterm_Shell != (Widget)0)
5926 {
5927 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5928 if (maxfd < ConnectionNumber(xterm_dpy))
5929 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005930
5931 /* An event may have already been read but not handled. In
5932 * particulary, XFlush may cause this. */
5933 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935# endif
5936# ifdef FEAT_MOUSE_GPM
5937 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5938 {
5939 FD_SET(gpm_fd, &rfds);
5940 FD_SET(gpm_fd, &efds);
5941 if (maxfd < gpm_fd)
5942 maxfd = gpm_fd;
5943 }
5944# endif
5945# ifdef USE_XSMP
5946 if (xsmp_icefd != -1)
5947 {
5948 FD_SET(xsmp_icefd, &rfds);
5949 FD_SET(xsmp_icefd, &efds);
5950 if (maxfd < xsmp_icefd)
5951 maxfd = xsmp_icefd;
5952 }
5953# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005954# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005955 maxfd = channel_select_setup(maxfd, &rfds, &wfds);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005956# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005957 if (interrupted != NULL)
5958 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005960 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005961 result = ret > 0 && FD_ISSET(fd, &rfds);
5962 if (result)
5963 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005964 else if (interrupted != NULL && ret > 0)
5965 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005966
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005967# ifdef EINTR
5968 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005969 {
5970 /* Check whether window has been resized, EINTR may be caused by
5971 * SIGWINCH. */
5972 if (do_resize)
5973 handle_resize();
5974
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005975 /* Interrupted by a signal, need to try again. We ignore msec
5976 * here, because we do want to check even after a timeout if
5977 * characters are available. Needed for reading output of an
5978 * external command after the process has finished. */
5979 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005980 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005981# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005982# ifdef __TANDEM
5983 if (ret == -1 && errno == ENOTSUP)
5984 {
5985 FD_ZERO(&rfds);
5986 FD_ZERO(&efds);
5987 ret = 0;
5988 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005989# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005990# ifdef FEAT_MZSCHEME
5991 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005992 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005993 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994# endif
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996# ifdef FEAT_XCLIPBOARD
5997 if (ret > 0 && xterm_Shell != (Widget)0
5998 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5999 {
6000 xterm_update(); /* Maybe we should hand out clipboard */
6001 /* continue looping when we only got the X event and the input
6002 * buffer is empty */
6003 if (--ret == 0 && !input_available())
6004 {
6005 /* Try again */
6006 finished = FALSE;
6007 }
6008 }
6009# endif
6010# ifdef FEAT_MOUSE_GPM
6011 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6012 {
6013 if (FD_ISSET(gpm_fd, &efds))
6014 gpm_close();
6015 else if (FD_ISSET(gpm_fd, &rfds))
6016 *check_for_gpm = 1;
6017 }
6018# endif
6019# ifdef USE_XSMP
6020 if (ret > 0 && xsmp_icefd != -1)
6021 {
6022 if (FD_ISSET(xsmp_icefd, &efds))
6023 {
6024 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006025 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 xsmp_close();
6027 if (--ret == 0)
6028 finished = FALSE; /* keep going if event was only one */
6029 }
6030 else if (FD_ISSET(xsmp_icefd, &rfds))
6031 {
6032 busy = TRUE;
6033 xsmp_handle_requests();
6034 busy = FALSE;
6035 if (--ret == 0)
6036 finished = FALSE; /* keep going if event was only one */
6037 }
6038 }
6039# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006040#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01006041 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006042 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045#endif /* HAVE_SELECT */
6046
6047#ifdef MAY_LOOP
6048 if (finished || msec == 0)
6049 break;
6050
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006051# ifdef FEAT_CLIENTSERVER
6052 if (server_waiting())
6053 break;
6054# endif
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 /* We're going to loop around again, find out for how long */
6057 if (msec > 0)
6058 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006059# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006061 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062# else
6063 /* Guess we got interrupted halfway. */
6064 msec = msec / 2;
6065# endif
6066 if (msec <= 0)
6067 break; /* waited long enough */
6068 }
6069#endif
6070 }
6071
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006072 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073}
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006077 * Expand a path into all matching files and/or directories. Handles "*",
6078 * "?", "[a-z]", "**", etc.
6079 * "path" has backslashes before chars that are not to be expanded.
6080 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 */
6082 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006083mch_expandpath(
6084 garray_T *gap,
6085 char_u *path,
6086 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
Bram Moolenaar02743632005-07-25 20:42:36 +00006088 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089}
6090#endif
6091
6092/*
6093 * mch_expand_wildcards() - this code does wild-card pattern matching using
6094 * the shell
6095 *
6096 * return OK for success, FAIL for error (you may lose some memory) and put
6097 * an error message in *file.
6098 *
6099 * num_pat is number of input patterns
6100 * pat is array of pointers to input patterns
6101 * num_file is pointer to number of matched file names
6102 * file is pointer to array of pointers to matched file names
6103 */
6104
6105#ifndef SEEK_SET
6106# define SEEK_SET 0
6107#endif
6108#ifndef SEEK_END
6109# define SEEK_END 2
6110#endif
6111
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006112#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006113
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006115mch_expand_wildcards(
6116 int num_pat,
6117 char_u **pat,
6118 int *num_file,
6119 char_u ***file,
6120 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 int i;
6123 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006124 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 char_u *p;
6126 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
Bram Moolenaarc7247912008-01-13 12:54:11 +00006128 /*
6129 * This is the non-OS/2 implementation (really Unix).
6130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int j;
6132 char_u *tempname;
6133 char_u *command;
6134 FILE *fd;
6135 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006136#define STYLE_ECHO 0 /* use "echo", the default */
6137#define STYLE_GLOB 1 /* use "glob", for csh */
6138#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6139#define STYLE_PRINT 3 /* use "print -N", for zsh */
6140#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6141 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 int shell_style = STYLE_ECHO;
6143 int check_spaces;
6144 static int did_find_nul = FALSE;
6145 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006146 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006147 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149 *num_file = 0; /* default: no files found */
6150 *file = NULL;
6151
6152 /*
6153 * If there are no wildcards, just copy the names to allocated memory.
6154 * Saves a lot of time, because we don't have to start a new shell.
6155 */
6156 if (!have_wildcard(num_pat, pat))
6157 return save_patterns(num_pat, pat, num_file, file);
6158
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006159# ifdef HAVE_SANDBOX
6160 /* Don't allow any shell command in the sandbox. */
6161 if (sandbox != 0 && check_secure())
6162 return FAIL;
6163# endif
6164
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 /*
6166 * Don't allow the use of backticks in secure and restricted mode.
6167 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006168 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 for (i = 0; i < num_pat; ++i)
6170 if (vim_strchr(pat[i], '`') != NULL
6171 && (check_restricted() || check_secure()))
6172 return FAIL;
6173
6174 /*
6175 * get a name for the temp file
6176 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006177 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
6179 EMSG(_(e_notmp));
6180 return FAIL;
6181 }
6182
6183 /*
6184 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006185 * file.
6186 * STYLE_BT: NL separated
6187 * If expanding `cmd` execute it directly.
6188 * STYLE_GLOB: NUL separated
6189 * If we use *csh, "glob" will work better than "echo".
6190 * STYLE_PRINT: NL or NUL separated
6191 * If we use *zsh, "print -N" will work better than "glob".
6192 * STYLE_VIMGLOB: NL separated
6193 * If we use *sh*, we define "vimglob()".
6194 * STYLE_ECHO: space separated.
6195 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 */
6197 if (num_pat == 1 && *pat[0] == '`'
6198 && (len = STRLEN(pat[0])) > 2
6199 && *(pat[0] + len - 1) == '`')
6200 shell_style = STYLE_BT;
6201 else if ((len = STRLEN(p_sh)) >= 3)
6202 {
6203 if (STRCMP(p_sh + len - 3, "csh") == 0)
6204 shell_style = STYLE_GLOB;
6205 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6206 shell_style = STYLE_PRINT;
6207 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006208 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6209 "sh") != NULL)
6210 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaarc7247912008-01-13 12:54:11 +00006212 /* Compute the length of the command. We need 2 extra bytes: for the
6213 * optional '&' and for the NUL.
6214 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006216 if (shell_style == STYLE_VIMGLOB)
6217 len += STRLEN(sh_vimglob_func);
6218
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006219 for (i = 0; i < num_pat; ++i)
6220 {
6221 /* Count the length of the patterns in the same way as they are put in
6222 * "command" below. */
6223#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006225#else
6226 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006227 for (j = 0; pat[i][j] != NUL; ++j)
6228 {
6229 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6230 ++len; /* may add a backslash */
6231 ++len;
6232 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006233#endif
6234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 command = alloc(len);
6236 if (command == NULL)
6237 {
6238 /* out of memory */
6239 vim_free(tempname);
6240 return FAIL;
6241 }
6242
6243 /*
6244 * Build the shell command:
6245 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6246 * recognizes this).
6247 * - Add the shell command to print the expanded names.
6248 * - Add the temp file name.
6249 * - Add the file name patterns.
6250 */
6251 if (shell_style == STYLE_BT)
6252 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006253 /* change `command; command& ` to (command; command ) */
6254 STRCPY(command, "(");
6255 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006257 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006258 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 --p;
6260 if (*p == '&') /* remove trailing '&' */
6261 {
6262 ampersent = TRUE;
6263 *p = ' ';
6264 }
6265 STRCAT(command, ">");
6266 }
6267 else
6268 {
6269 if (flags & EW_NOTFOUND)
6270 STRCPY(command, "set nonomatch; ");
6271 else
6272 STRCPY(command, "unset nonomatch; ");
6273 if (shell_style == STYLE_GLOB)
6274 STRCAT(command, "glob >");
6275 else if (shell_style == STYLE_PRINT)
6276 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006277 else if (shell_style == STYLE_VIMGLOB)
6278 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 else
6280 STRCAT(command, "echo >");
6281 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (shell_style != STYLE_BT)
6286 for (i = 0; i < num_pat; ++i)
6287 {
6288 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006289 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006290 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291#ifdef USE_SYSTEM
6292 STRCAT(command, " \"");
6293 STRCAT(command, pat[i]);
6294 STRCAT(command, "\"");
6295#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006296 int intick = FALSE;
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 p = command + STRLEN(command);
6299 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006300 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006301 {
6302 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006303 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006304 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6305 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006306 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006307 * backslash inside backticks, before a special character
6308 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006309 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006310 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6311 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006312 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006313 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006314 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006315 else if (!intick
6316 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6317 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006318 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006319 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6320 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006321 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006322
6323 /* Copy one character. */
6324 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 *p = NUL;
6327#endif
6328 }
6329 if (flags & EW_SILENT)
6330 show_shell_mess = FALSE;
6331 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006332 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 /*
6335 * Using zsh -G: If a pattern has no matches, it is just deleted from
6336 * the argument list, otherwise zsh gives an error message and doesn't
6337 * expand any other pattern.
6338 */
6339 if (shell_style == STYLE_PRINT)
6340 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6341
6342 /*
6343 * If we use -f then shell variables set in .cshrc won't get expanded.
6344 * vi can do it, so we will too, but it is only necessary if there is a "$"
6345 * in one of the patterns, otherwise we can still use the fast option.
6346 */
6347 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6348 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6349
6350 /*
6351 * execute the shell command
6352 */
6353 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6354
6355 /* When running in the background, give it some time to create the temp
6356 * file, but don't wait for it to finish. */
6357 if (ampersent)
6358 mch_delay(10L, TRUE);
6359
6360 extra_shell_arg = NULL; /* cleanup */
6361 show_shell_mess = TRUE;
6362 vim_free(command);
6363
Bram Moolenaarc7247912008-01-13 12:54:11 +00006364 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 {
6366 mch_remove(tempname);
6367 vim_free(tempname);
6368 /*
6369 * With interactive completion, the error message is not printed.
6370 * However with USE_SYSTEM, I don't know how to turn off error messages
6371 * from the shell, so screen may still get messed up -- webb.
6372 */
6373#ifndef USE_SYSTEM
6374 if (!(flags & EW_SILENT))
6375#endif
6376 {
6377 redraw_later_clear(); /* probably messed up screen */
6378 msg_putchar('\n'); /* clear bottom line quickly */
6379 cmdline_row = Rows - 1; /* continue on last line */
6380#ifdef USE_SYSTEM
6381 if (!(flags & EW_SILENT))
6382#endif
6383 {
6384 MSG(_(e_wildexpand));
6385 msg_start(); /* don't overwrite this message */
6386 }
6387 }
6388 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6389 * EW_NOTFOUND is given */
6390 if (shell_style == STYLE_BT)
6391 return FAIL;
6392 goto notfound;
6393 }
6394
6395 /*
6396 * read the names from the file into memory
6397 */
6398 fd = fopen((char *)tempname, READBIN);
6399 if (fd == NULL)
6400 {
6401 /* Something went wrong, perhaps a file name with a special char. */
6402 if (!(flags & EW_SILENT))
6403 {
6404 MSG(_(e_wildexpand));
6405 msg_start(); /* don't overwrite this message */
6406 }
6407 vim_free(tempname);
6408 goto notfound;
6409 }
6410 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006411 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006413 if (llen < 0)
6414 /* just in case ftell() would fail */
6415 buffer = NULL;
6416 else
6417 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (buffer == NULL)
6419 {
6420 /* out of memory */
6421 mch_remove(tempname);
6422 vim_free(tempname);
6423 fclose(fd);
6424 return FAIL;
6425 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006426 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 i = fread((char *)buffer, 1, len, fd);
6428 fclose(fd);
6429 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006430 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
6432 /* unexpected read error */
6433 EMSG2(_(e_notread), tempname);
6434 vim_free(tempname);
6435 vim_free(buffer);
6436 return FAIL;
6437 }
6438 vim_free(tempname);
6439
Bram Moolenaarc7247912008-01-13 12:54:11 +00006440# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6442 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006443 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6445 *p++ = buffer[i];
6446 len = p - buffer;
6447# endif
6448
6449
6450 /* file names are separated with Space */
6451 if (shell_style == STYLE_ECHO)
6452 {
6453 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6454 p = buffer;
6455 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6456 {
6457 while (*p != ' ' && *p != '\n')
6458 ++p;
6459 p = skipwhite(p); /* skip to next entry */
6460 }
6461 }
6462 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006463 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6466 p = buffer;
6467 for (i = 0; *p != NUL; ++i) /* count number of entries */
6468 {
6469 while (*p != '\n' && *p != NUL)
6470 ++p;
6471 if (*p != NUL)
6472 ++p;
6473 p = skipwhite(p); /* skip leading white space */
6474 }
6475 }
6476 /* file names are separated with NUL */
6477 else
6478 {
6479 /*
6480 * Some versions of zsh use spaces instead of NULs to separate
6481 * results. Only do this when there is no NUL before the end of the
6482 * buffer, otherwise we would never be able to use file names with
6483 * embedded spaces when zsh does use NULs.
6484 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6485 * don't check for spaces again.
6486 */
6487 check_spaces = FALSE;
6488 if (shell_style == STYLE_PRINT && !did_find_nul)
6489 {
6490 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006491 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006492 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 did_find_nul = TRUE;
6494 else
6495 check_spaces = TRUE;
6496 }
6497
6498 /*
6499 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6500 * already is one, for STYLE_GLOB it needs to be added.
6501 */
6502 if (len && buffer[len - 1] == NUL)
6503 --len;
6504 else
6505 buffer[len] = NUL;
6506 i = 0;
6507 for (p = buffer; p < buffer + len; ++p)
6508 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6509 {
6510 ++i;
6511 *p = NUL;
6512 }
6513 if (len)
6514 ++i; /* count last entry */
6515 }
6516 if (i == 0)
6517 {
6518 /*
6519 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6520 * /bin/sh will happily expand it to nothing rather than returning an
6521 * error; and hey, it's good to check anyway -- webb.
6522 */
6523 vim_free(buffer);
6524 goto notfound;
6525 }
6526 *num_file = i;
6527 *file = (char_u **)alloc(sizeof(char_u *) * i);
6528 if (*file == NULL)
6529 {
6530 /* out of memory */
6531 vim_free(buffer);
6532 return FAIL;
6533 }
6534
6535 /*
6536 * Isolate the individual file names.
6537 */
6538 p = buffer;
6539 for (i = 0; i < *num_file; ++i)
6540 {
6541 (*file)[i] = p;
6542 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006543 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6544 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006546 while (!(shell_style == STYLE_ECHO && *p == ' ')
6547 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 ++p;
6549 if (p == buffer + len) /* last entry */
6550 *p = NUL;
6551 else
6552 {
6553 *p++ = NUL;
6554 p = skipwhite(p); /* skip to next entry */
6555 }
6556 }
6557 else /* NUL separates */
6558 {
6559 while (*p && p < buffer + len) /* skip entry */
6560 ++p;
6561 ++p; /* skip NUL */
6562 }
6563 }
6564
6565 /*
6566 * Move the file names to allocated memory.
6567 */
6568 for (j = 0, i = 0; i < *num_file; ++i)
6569 {
6570 /* Require the files to exist. Helps when using /bin/sh */
6571 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6572 continue;
6573
6574 /* check if this entry should be included */
6575 dir = (mch_isdir((*file)[i]));
6576 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6577 continue;
6578
Bram Moolenaara2031822006-03-07 22:29:51 +00006579 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006580 if (!dir && (flags & EW_EXEC)
6581 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006582 continue;
6583
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6585 if (p)
6586 {
6587 STRCPY(p, (*file)[i]);
6588 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006589 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 (*file)[j++] = p;
6591 }
6592 }
6593 vim_free(buffer);
6594 *num_file = j;
6595
6596 if (*num_file == 0) /* rejected all entries */
6597 {
6598 vim_free(*file);
6599 *file = NULL;
6600 goto notfound;
6601 }
6602
6603 return OK;
6604
6605notfound:
6606 if (flags & EW_NOTFOUND)
6607 return save_patterns(num_pat, pat, num_file, file);
6608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
6610
6611#endif /* VMS */
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006614save_patterns(
6615 int num_pat,
6616 char_u **pat,
6617 int *num_file,
6618 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006621 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
6623 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6624 if (*file == NULL)
6625 return FAIL;
6626 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006627 {
6628 s = vim_strsave(pat[i]);
6629 if (s != NULL)
6630 /* Be compatible with expand_filename(): halve the number of
6631 * backslashes. */
6632 backslash_halve(s);
6633 (*file)[i] = s;
6634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 *num_file = num_pat;
6636 return OK;
6637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639/*
6640 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6641 * expand.
6642 */
6643 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006644mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006646 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (*p == '\\' && p[1] != NUL)
6649 ++p;
6650 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (vim_strchr((char_u *)
6652#ifdef VMS
6653 "*?%"
6654#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656#endif
6657 , *p) != NULL)
6658 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 }
6660 return FALSE;
6661}
6662
6663/*
6664 * Return TRUE if the string "p" contains a wildcard.
6665 * Don't recognize '~' at the end as a wildcard.
6666 */
6667 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006668mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006670 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (*p == '\\' && p[1] != NUL)
6673 ++p;
6674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (vim_strchr((char_u *)
6676#ifdef VMS
6677 "*?%$"
6678#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680#endif
6681 , *p) != NULL
6682 || (*p == '~' && p[1] != NUL))
6683 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 }
6685 return FALSE;
6686}
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006689have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 int i;
6692
6693 for (i = 0; i < num; i++)
6694 if (mch_has_wildcard(file[i]))
6695 return 1;
6696 return 0;
6697}
6698
6699 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006700have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 int i;
6703
6704 for (i = 0; i < num; i++)
6705 if (vim_strchr(file[i], '$') != NULL)
6706 return TRUE;
6707 return FALSE;
6708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006710#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
6712 * Scaled-down version of rename(), which is missing in Xenix.
6713 * This version can only move regular files and will fail if the
6714 * destination exists.
6715 */
6716 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006717mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 struct stat st;
6720
6721 if (stat(dest, &st) >= 0) /* fail if destination exists */
6722 return -1;
6723 if (link(src, dest) != 0) /* link file to new name */
6724 return -1;
6725 if (mch_remove(src) == 0) /* delete link to old name */
6726 return 0;
6727 return -1;
6728}
6729#endif /* !HAVE_RENAME */
6730
6731#ifdef FEAT_MOUSE_GPM
6732/*
6733 * Initializes connection with gpm (if it isn't already opened)
6734 * Return 1 if succeeded (or connection already opened), 0 if failed
6735 */
6736 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006737gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6740
6741 if (!gpm_flag)
6742 {
6743 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6744 gpm_connect.defaultMask = ~GPM_HARD;
6745 /* Default handling for mouse move*/
6746 gpm_connect.minMod = 0; /* Handle any modifier keys */
6747 gpm_connect.maxMod = 0xffff;
6748 if (Gpm_Open(&gpm_connect, 0) > 0)
6749 {
6750 /* gpm library tries to handling TSTP causes
6751 * problems. Anyways, we close connection to Gpm whenever
6752 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006753 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006755# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 return 1; /* succeed */
6759 }
6760 if (gpm_fd == -2)
6761 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6762 return 0;
6763 }
6764 return 1; /* already open */
6765}
6766
6767/*
6768 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 */
6770 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006771gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 if (gpm_flag && gpm_fd >= 0) /* if Open */
6774 Gpm_Close();
6775}
6776
6777/* Reads gpm event and adds special keys to input buf. Returns length of
6778 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006779 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 */
6781 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006782mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 int button;
6785 static Gpm_Event gpm_event;
6786 char_u string[6];
6787 int_u vim_modifiers;
6788 int row,col;
6789 unsigned char buttons_mask;
6790 unsigned char gpm_modifiers;
6791 static unsigned char old_buttons = 0;
6792
6793 Gpm_GetEvent(&gpm_event);
6794
6795#ifdef FEAT_GUI
6796 /* Don't put events in the input queue now. */
6797 if (hold_gui_events)
6798 return 0;
6799#endif
6800
6801 row = gpm_event.y - 1;
6802 col = gpm_event.x - 1;
6803
6804 string[0] = ESC; /* Our termcode */
6805 string[1] = 'M';
6806 string[2] = 'G';
6807 switch (GPM_BARE_EVENTS(gpm_event.type))
6808 {
6809 case GPM_DRAG:
6810 string[3] = MOUSE_DRAG;
6811 break;
6812 case GPM_DOWN:
6813 buttons_mask = gpm_event.buttons & ~old_buttons;
6814 old_buttons = gpm_event.buttons;
6815 switch (buttons_mask)
6816 {
6817 case GPM_B_LEFT:
6818 button = MOUSE_LEFT;
6819 break;
6820 case GPM_B_MIDDLE:
6821 button = MOUSE_MIDDLE;
6822 break;
6823 case GPM_B_RIGHT:
6824 button = MOUSE_RIGHT;
6825 break;
6826 default:
6827 return 0;
6828 /*Don't know what to do. Can more than one button be
6829 * reported in one event? */
6830 }
6831 string[3] = (char_u)(button | 0x20);
6832 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6833 break;
6834 case GPM_UP:
6835 string[3] = MOUSE_RELEASE;
6836 old_buttons &= ~gpm_event.buttons;
6837 break;
6838 default:
6839 return 0;
6840 }
6841 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6842 gpm_modifiers = gpm_event.modifiers;
6843 vim_modifiers = 0x0;
6844 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6845 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6846 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6847 */
6848 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6849 vim_modifiers |= MOUSE_SHIFT;
6850
6851 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6852 vim_modifiers |= MOUSE_CTRL;
6853 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6854 vim_modifiers |= MOUSE_ALT;
6855 string[3] |= vim_modifiers;
6856 string[4] = (char_u)(col + ' ' + 1);
6857 string[5] = (char_u)(row + ' ' + 1);
6858 add_to_input_buf(string, 6);
6859 return 6;
6860}
6861#endif /* FEAT_MOUSE_GPM */
6862
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006863#ifdef FEAT_SYSMOUSE
6864/*
6865 * Initialize connection with sysmouse.
6866 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6867 * output, any sysmouse output than will be processed via sig_sysmouse().
6868 * Return OK if succeeded, FAIL if failed.
6869 */
6870 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006871sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006872{
6873 struct mouse_info mouse;
6874
6875 mouse.operation = MOUSE_MODE;
6876 mouse.u.mode.mode = 0;
6877 mouse.u.mode.signal = SIGUSR2;
6878 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6879 {
6880 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6881 mouse.operation = MOUSE_SHOW;
6882 ioctl(1, CONS_MOUSECTL, &mouse);
6883 return OK;
6884 }
6885 return FAIL;
6886}
6887
6888/*
6889 * Stop processing SIGUSR2 signals, and also make sure that
6890 * virtual console do not send us any sysmouse related signal.
6891 */
6892 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006893sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006894{
6895 struct mouse_info mouse;
6896
6897 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6898 mouse.operation = MOUSE_MODE;
6899 mouse.u.mode.mode = 0;
6900 mouse.u.mode.signal = 0;
6901 ioctl(1, CONS_MOUSECTL, &mouse);
6902}
6903
6904/*
6905 * Gets info from sysmouse and adds special keys to input buf.
6906 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006907 static RETSIGTYPE
6908sig_sysmouse SIGDEFARG(sigarg)
6909{
6910 struct mouse_info mouse;
6911 struct video_info video;
6912 char_u string[6];
6913 int row, col;
6914 int button;
6915 int buttons;
6916 static int oldbuttons = 0;
6917
6918#ifdef FEAT_GUI
6919 /* Don't put events in the input queue now. */
6920 if (hold_gui_events)
6921 return;
6922#endif
6923
6924 mouse.operation = MOUSE_GETINFO;
6925 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6926 && ioctl(1, FBIO_MODEINFO, &video) != -1
6927 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6928 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6929 {
6930 row = mouse.u.data.y / video.vi_cheight;
6931 col = mouse.u.data.x / video.vi_cwidth;
6932 buttons = mouse.u.data.buttons;
6933 string[0] = ESC; /* Our termcode */
6934 string[1] = 'M';
6935 string[2] = 'S';
6936 if (oldbuttons == buttons && buttons != 0)
6937 {
6938 button = MOUSE_DRAG;
6939 }
6940 else
6941 {
6942 switch (buttons)
6943 {
6944 case 0:
6945 button = MOUSE_RELEASE;
6946 break;
6947 case 1:
6948 button = MOUSE_LEFT;
6949 break;
6950 case 2:
6951 button = MOUSE_MIDDLE;
6952 break;
6953 case 4:
6954 button = MOUSE_RIGHT;
6955 break;
6956 default:
6957 return;
6958 }
6959 oldbuttons = buttons;
6960 }
6961 string[3] = (char_u)(button);
6962 string[4] = (char_u)(col + ' ' + 1);
6963 string[5] = (char_u)(row + ' ' + 1);
6964 add_to_input_buf(string, 6);
6965 }
6966 return;
6967}
6968#endif /* FEAT_SYSMOUSE */
6969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01006971typedef char_u * (*STRPROCSTR)(char_u *);
6972typedef char_u * (*INTPROCSTR)(int);
6973typedef int (*STRPROCINT)(char_u *);
6974typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976/*
6977 * Call a DLL routine which takes either a string or int param
6978 * and returns an allocated string.
6979 */
6980 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006981mch_libcall(
6982 char_u *libname,
6983 char_u *funcname,
6984 char_u *argstring, /* NULL when using a argint */
6985 int argint,
6986 char_u **string_result,/* NULL when using number_result */
6987 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989# if defined(USE_DLOPEN)
6990 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006991 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992# else
6993 shl_t hinstLib;
6994# endif
6995 STRPROCSTR ProcAdd;
6996 INTPROCSTR ProcAddI;
6997 char_u *retval_str = NULL;
6998 int retval_int = 0;
6999 int success = FALSE;
7000
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007001 /*
7002 * Get a handle to the DLL module.
7003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007005 /* First clear any error, it's not cleared by the dlopen() call. */
7006 (void)dlerror();
7007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 hinstLib = dlopen((char *)libname, RTLD_LAZY
7009# ifdef RTLD_LOCAL
7010 | RTLD_LOCAL
7011# endif
7012 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007013 if (hinstLib == NULL)
7014 {
7015 /* "dlerr" must be used before dlclose() */
7016 dlerr = (char *)dlerror();
7017 if (dlerr != NULL)
7018 EMSG2(_("dlerror = \"%s\""), dlerr);
7019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020# else
7021 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7022# endif
7023
7024 /* If the handle is valid, try to get the function address. */
7025 if (hinstLib != NULL)
7026 {
7027# ifdef HAVE_SETJMP_H
7028 /*
7029 * Catch a crash when calling the library function. For example when
7030 * using a number where a string pointer is expected.
7031 */
7032 mch_startjmp();
7033 if (SETJMP(lc_jump_env) != 0)
7034 {
7035 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007036# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007037 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 mch_didjmp();
7040 }
7041 else
7042# endif
7043 {
7044 retval_str = NULL;
7045 retval_int = 0;
7046
7047 if (argstring != NULL)
7048 {
7049# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007050 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007051 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052# else
7053 if (shl_findsym(&hinstLib, (const char *)funcname,
7054 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7055 ProcAdd = NULL;
7056# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007057 if ((success = (ProcAdd != NULL
7058# if defined(USE_DLOPEN)
7059 && dlerr == NULL
7060# endif
7061 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
7063 if (string_result == NULL)
7064 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7065 else
7066 retval_str = (ProcAdd)(argstring);
7067 }
7068 }
7069 else
7070 {
7071# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007072 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007073 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074# else
7075 if (shl_findsym(&hinstLib, (const char *)funcname,
7076 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7077 ProcAddI = NULL;
7078# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007079 if ((success = (ProcAddI != NULL
7080# if defined(USE_DLOPEN)
7081 && dlerr == NULL
7082# endif
7083 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {
7085 if (string_result == NULL)
7086 retval_int = ((INTPROCINT)ProcAddI)(argint);
7087 else
7088 retval_str = (ProcAddI)(argint);
7089 }
7090 }
7091
7092 /* Save the string before we free the library. */
7093 /* Assume that a "1" or "-1" result is an illegal pointer. */
7094 if (string_result == NULL)
7095 *number_result = retval_int;
7096 else if (retval_str != NULL
7097 && retval_str != (char_u *)1
7098 && retval_str != (char_u *)-1)
7099 *string_result = vim_strsave(retval_str);
7100 }
7101
7102# ifdef HAVE_SETJMP_H
7103 mch_endjmp();
7104# ifdef SIGHASARG
7105 if (lc_signal != 0)
7106 {
7107 int i;
7108
7109 /* try to find the name of this signal */
7110 for (i = 0; signal_info[i].sig != -1; i++)
7111 if (lc_signal == signal_info[i].sig)
7112 break;
7113 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7114 }
7115# endif
7116# endif
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007119 /* "dlerr" must be used before dlclose() */
7120 if (dlerr != NULL)
7121 EMSG2(_("dlerror = \"%s\""), dlerr);
7122
7123 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 (void)dlclose(hinstLib);
7125# else
7126 (void)shl_unload(hinstLib);
7127# endif
7128 }
7129
7130 if (!success)
7131 {
7132 EMSG2(_(e_libcall), funcname);
7133 return FAIL;
7134 }
7135
7136 return OK;
7137}
7138#endif
7139
7140#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7141static int xterm_trace = -1; /* default: disabled */
7142static int xterm_button;
7143
7144/*
7145 * Setup a dummy window for X selections in a terminal.
7146 */
7147 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007148setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 int z = 0;
7151 char *strp = "";
7152 Widget AppShell;
7153
7154 if (!x_connect_to_server())
7155 return;
7156
7157 open_app_context();
7158 if (app_context != NULL && xterm_Shell == (Widget)0)
7159 {
7160 int (*oldhandler)();
7161#if defined(HAVE_SETJMP_H)
7162 int (*oldIOhandler)();
7163#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007164# ifdef ELAPSED_FUNC
7165 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007168 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169# endif
7170
7171 /* Ignore X errors while opening the display */
7172 oldhandler = XSetErrorHandler(x_error_check);
7173
7174#if defined(HAVE_SETJMP_H)
7175 /* Ignore X IO errors while opening the display */
7176 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7177 mch_startjmp();
7178 if (SETJMP(lc_jump_env) != 0)
7179 {
7180 mch_didjmp();
7181 xterm_dpy = NULL;
7182 }
7183 else
7184#endif
7185 {
7186 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7187 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7188#if defined(HAVE_SETJMP_H)
7189 mch_endjmp();
7190#endif
7191 }
7192
7193#if defined(HAVE_SETJMP_H)
7194 /* Now handle X IO errors normally. */
7195 (void)XSetIOErrorHandler(oldIOhandler);
7196#endif
7197 /* Now handle X errors normally. */
7198 (void)XSetErrorHandler(oldhandler);
7199
7200 if (xterm_dpy == NULL)
7201 {
7202 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007203 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 return;
7205 }
7206
7207 /* Catch terminating error of the X server connection. */
7208 (void)XSetIOErrorHandler(x_IOerror_handler);
7209
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007210# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007212 {
7213 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007214 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007215 verbose_leave();
7216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217# endif
7218
7219 /* Create a Shell to make converters work. */
7220 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7221 applicationShellWidgetClass, xterm_dpy,
7222 NULL);
7223 if (AppShell == (Widget)0)
7224 return;
7225 xterm_Shell = XtVaCreatePopupShell("VIM",
7226 topLevelShellWidgetClass, AppShell,
7227 XtNmappedWhenManaged, 0,
7228 XtNwidth, 1,
7229 XtNheight, 1,
7230 NULL);
7231 if (xterm_Shell == (Widget)0)
7232 return;
7233
7234 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007235 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 if (x11_display == NULL)
7237 x11_display = xterm_dpy;
7238
7239 XtRealizeWidget(xterm_Shell);
7240 XSync(xterm_dpy, False);
7241 xterm_update();
7242 }
7243 if (xterm_Shell != (Widget)0)
7244 {
7245 clip_init(TRUE);
7246 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7247 x11_window = (Window)atol(strp);
7248 /* Check if $WINDOWID is valid. */
7249 if (test_x11_window(xterm_dpy) == FAIL)
7250 x11_window = 0;
7251 if (x11_window != 0)
7252 xterm_trace = 0;
7253 }
7254}
7255
7256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007257start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7260 return;
7261 xterm_trace = 1;
7262 xterm_button = button;
7263 do_xterm_trace();
7264}
7265
7266
7267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007268stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270 if (xterm_trace < 0)
7271 return;
7272 xterm_trace = 0;
7273}
7274
7275/*
7276 * Query the xterm pointer and generate mouse termcodes if necessary
7277 * return TRUE if dragging is active, else FALSE
7278 */
7279 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007280do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 Window root, child;
7283 int root_x, root_y;
7284 int win_x, win_y;
7285 int row, col;
7286 int_u mask_return;
7287 char_u buf[50];
7288 char_u *strp;
7289 long got_hints;
7290 static char_u *mouse_code;
7291 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7292 static int prev_row = 0, prev_col = 0;
7293 static XSizeHints xterm_hints;
7294
7295 if (xterm_trace <= 0)
7296 return FALSE;
7297
7298 if (xterm_trace == 1)
7299 {
7300 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007301 * have changed recently. */
7302 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7303 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 || xterm_hints.width_inc <= 1
7305 || xterm_hints.height_inc <= 1)
7306 {
7307 xterm_trace = -1; /* Not enough data -- disable tracing */
7308 return FALSE;
7309 }
7310
7311 /* Rely on the same mouse code for the duration of this */
7312 mouse_code = find_termcode(mouse_name);
7313 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007314 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 xterm_trace = 2;
7316
7317 /* Find the offset of the chars, there might be a scrollbar on the
7318 * left of the window and/or a menu on the top (eterm etc.) */
7319 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7320 &win_x, &win_y, &mask_return);
7321 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7322 - (xterm_hints.height_inc / 2);
7323 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7324 xterm_hints.y = 2;
7325 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7326 - (xterm_hints.width_inc / 2);
7327 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7328 xterm_hints.x = 2;
7329 return TRUE;
7330 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007331 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
7333 xterm_trace = 0;
7334 return FALSE;
7335 }
7336
7337 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7338 &win_x, &win_y, &mask_return);
7339
7340 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7341 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7342 if (row == prev_row && col == prev_col)
7343 return TRUE;
7344
7345 STRCPY(buf, mouse_code);
7346 strp = buf + STRLEN(buf);
7347 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7348 *strp++ = (char_u)(col + ' ' + 1);
7349 *strp++ = (char_u)(row + ' ' + 1);
7350 *strp = 0;
7351 add_to_input_buf(buf, STRLEN(buf));
7352
7353 prev_row = row;
7354 prev_col = col;
7355 return TRUE;
7356}
7357
7358# if defined(FEAT_GUI) || defined(PROTO)
7359/*
7360 * Destroy the display, window and app_context. Required for GTK.
7361 */
7362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007363clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 if (xterm_Shell != (Widget)0)
7366 {
7367 XtDestroyWidget(xterm_Shell);
7368 xterm_Shell = (Widget)0;
7369 }
7370 if (xterm_dpy != NULL)
7371 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007372# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 /* Lesstif and Solaris crash here, lose some memory */
7374 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (x11_display == xterm_dpy)
7377 x11_display = NULL;
7378 xterm_dpy = NULL;
7379 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007380# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 if (app_context != (XtAppContext)NULL)
7382 {
7383 /* Lesstif and Solaris crash here, lose some memory */
7384 XtDestroyApplicationContext(app_context);
7385 app_context = (XtAppContext)NULL;
7386 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388}
7389# endif
7390
7391/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007392 * Catch up with GUI or X events.
7393 */
7394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007395clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007396{
7397# ifdef FEAT_GUI
7398 if (gui.in_use)
7399 gui_mch_update();
7400 else
7401# endif
7402 if (xterm_Shell != (Widget)0)
7403 xterm_update();
7404}
7405
7406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * Catch up with any queued X events. This may put keyboard input into the
7408 * input buffer, call resize call-backs, trigger timers etc. If there is
7409 * nothing in the X event queue (& no timers pending), then we return
7410 * immediately.
7411 */
7412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007413xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
7415 XEvent event;
7416
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007417 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007419 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007421 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007422 break;
7423
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007424 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007425 {
7426 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007427 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007428#ifdef FEAT_CLIENTSERVER
7429 {
7430 XPropertyEvent *e = (XPropertyEvent *)&event;
7431
7432 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007434 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007437 XtDispatchEvent(&event);
7438 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007439 else
7440 {
7441 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007442 XtAppProcessEvent(app_context, mask);
7443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 }
7445}
7446
7447 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007448clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450 if (xterm_Shell != (Widget)0)
7451 return clip_x11_own_selection(xterm_Shell, cbd);
7452 return FAIL;
7453}
7454
7455 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007456clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
7458 if (xterm_Shell != (Widget)0)
7459 clip_x11_lose_selection(xterm_Shell, cbd);
7460}
7461
7462 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007463clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 if (xterm_Shell != (Widget)0)
7466 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7467}
7468
7469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007470clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471{
7472 clip_x11_set_selection(cbd);
7473}
7474#endif
7475
7476
7477#if defined(USE_XSMP) || defined(PROTO)
7478/*
7479 * Code for X Session Management Protocol.
7480 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007481static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7482static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7483static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7484static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7485static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
7487
7488# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007489static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
7491/*
7492 * This is our chance to ask the user if they want to save,
7493 * or abort the logout
7494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007496xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 cmdmod_T save_cmdmod;
7499 int cancel_shutdown = False;
7500
7501 save_cmdmod = cmdmod;
7502 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007503 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 /* Mustn't logout */
7505 cancel_shutdown = True;
7506 cmdmod = save_cmdmod;
7507 setcursor(); /* position cursor */
7508 out_flush();
7509
7510 /* Done interaction */
7511 SmcInteractDone(smc_conn, cancel_shutdown);
7512
7513 /* Finish off
7514 * Only end save-yourself here if we're not cancelling shutdown;
7515 * we'll get a cancelled callback later in which we'll end it.
7516 * Hopefully get around glitchy SMs (like GNOME-1)
7517 */
7518 if (!cancel_shutdown)
7519 {
7520 xsmp.save_yourself = False;
7521 SmcSaveYourselfDone(smc_conn, True);
7522 }
7523}
7524# endif
7525
7526/*
7527 * Callback that starts save-yourself.
7528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007530xsmp_handle_save_yourself(
7531 SmcConn smc_conn,
7532 SmPointer client_data UNUSED,
7533 int save_type UNUSED,
7534 Bool shutdown,
7535 int interact_style UNUSED,
7536 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 /* Handle already being in saveyourself */
7539 if (xsmp.save_yourself)
7540 SmcSaveYourselfDone(smc_conn, True);
7541 xsmp.save_yourself = True;
7542 xsmp.shutdown = shutdown;
7543
7544 /* First up, preserve all files */
7545 out_flush();
7546 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7547
7548 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007549 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
7551# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7552 /* Now see if we can ask about unsaved files */
7553 if (shutdown && !fast && gui.in_use)
7554 /* Need to interact with user, but need SM's permission */
7555 SmcInteractRequest(smc_conn, SmDialogError,
7556 xsmp_handle_interaction, client_data);
7557 else
7558# endif
7559 {
7560 /* Can stop the cycle here */
7561 SmcSaveYourselfDone(smc_conn, True);
7562 xsmp.save_yourself = False;
7563 }
7564}
7565
7566
7567/*
7568 * Callback to warn us of imminent death.
7569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007571xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 xsmp_close();
7574
7575 /* quit quickly leaving swapfiles for modified buffers behind */
7576 getout_preserve_modified(0);
7577}
7578
7579
7580/*
7581 * Callback to tell us that save-yourself has completed.
7582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007584xsmp_save_complete(
7585 SmcConn smc_conn UNUSED,
7586 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
7588 xsmp.save_yourself = False;
7589}
7590
7591
7592/*
7593 * Callback to tell us that an instigated shutdown was cancelled
7594 * (maybe even by us)
7595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007597xsmp_shutdown_cancelled(
7598 SmcConn smc_conn,
7599 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 if (xsmp.save_yourself)
7602 SmcSaveYourselfDone(smc_conn, True);
7603 xsmp.save_yourself = False;
7604 xsmp.shutdown = False;
7605}
7606
7607
7608/*
7609 * Callback to tell us that a new ICE connection has been established.
7610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007612xsmp_ice_connection(
7613 IceConn iceConn,
7614 IcePointer clientData UNUSED,
7615 Bool opening,
7616 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 /* Intercept creation of ICE connection fd */
7619 if (opening)
7620 {
7621 xsmp_icefd = IceConnectionNumber(iceConn);
7622 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7623 }
7624}
7625
7626
7627/* Handle any ICE processing that's required; return FAIL if SM lost */
7628 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007629xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 Bool rep;
7632
7633 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7634 == IceProcessMessagesIOError)
7635 {
7636 /* Lost ICE */
7637 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007638 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 xsmp_close();
7640 return FAIL;
7641 }
7642 else
7643 return OK;
7644}
7645
7646static int dummy;
7647
7648/* Set up X Session Management Protocol */
7649 void
7650xsmp_init(void)
7651{
7652 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 SmcCallbacks smcallbacks;
7654#if 0
7655 SmPropValue smname;
7656 SmProp smnameprop;
7657 SmProp *smprops[1];
7658#endif
7659
7660 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007661 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
7663 xsmp.save_yourself = xsmp.shutdown = False;
7664
7665 /* Set up SM callbacks - must have all, even if they're not used */
7666 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7667 smcallbacks.save_yourself.client_data = NULL;
7668 smcallbacks.die.callback = xsmp_die;
7669 smcallbacks.die.client_data = NULL;
7670 smcallbacks.save_complete.callback = xsmp_save_complete;
7671 smcallbacks.save_complete.client_data = NULL;
7672 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7673 smcallbacks.shutdown_cancelled.client_data = NULL;
7674
7675 /* Set up a watch on ICE connection creations. The "dummy" argument is
7676 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7677 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7678 {
7679 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007680 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 return;
7682 }
7683
7684 /* Create an SM connection */
7685 xsmp.smcconn = SmcOpenConnection(
7686 NULL,
7687 NULL,
7688 SmProtoMajor,
7689 SmProtoMinor,
7690 SmcSaveYourselfProcMask | SmcDieProcMask
7691 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7692 &smcallbacks,
7693 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007694 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 sizeof(errorstring),
7696 errorstring);
7697 if (xsmp.smcconn == NULL)
7698 {
7699 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007700
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007702 {
7703 vim_snprintf(errorreport, sizeof(errorreport),
7704 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7705 verb_msg((char_u *)errorreport);
7706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 return;
7708 }
7709 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7710
7711#if 0
7712 /* ID ourselves */
7713 smname.value = "vim";
7714 smname.length = 3;
7715 smnameprop.name = "SmProgram";
7716 smnameprop.type = "SmARRAY8";
7717 smnameprop.num_vals = 1;
7718 smnameprop.vals = &smname;
7719
7720 smprops[0] = &smnameprop;
7721 SmcSetProperties(xsmp.smcconn, 1, smprops);
7722#endif
7723}
7724
7725
7726/* Shut down XSMP comms. */
7727 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007728xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 if (xsmp_icefd != -1)
7731 {
7732 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007733 if (xsmp.clientid != NULL)
7734 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007735 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 xsmp_icefd = -1;
7737 }
7738}
7739#endif /* USE_XSMP */
7740
7741
7742#ifdef EBCDIC
7743/* Translate character to its CTRL- value */
7744char CtrlTable[] =
7745{
7746/* 00 - 5E */
7747 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7748 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7749 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7750 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7751 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7752 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7753/* ^ */ 0x1E,
7754/* - */ 0x1F,
7755/* 61 - 6C */
7756 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7757/* _ */ 0x1F,
7758/* 6E - 80 */
7759 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7760/* a */ 0x01,
7761/* b */ 0x02,
7762/* c */ 0x03,
7763/* d */ 0x37,
7764/* e */ 0x2D,
7765/* f */ 0x2E,
7766/* g */ 0x2F,
7767/* h */ 0x16,
7768/* i */ 0x05,
7769/* 8A - 90 */
7770 0, 0, 0, 0, 0, 0, 0,
7771/* j */ 0x15,
7772/* k */ 0x0B,
7773/* l */ 0x0C,
7774/* m */ 0x0D,
7775/* n */ 0x0E,
7776/* o */ 0x0F,
7777/* p */ 0x10,
7778/* q */ 0x11,
7779/* r */ 0x12,
7780/* 9A - A1 */
7781 0, 0, 0, 0, 0, 0, 0, 0,
7782/* s */ 0x13,
7783/* t */ 0x3C,
7784/* u */ 0x3D,
7785/* v */ 0x32,
7786/* w */ 0x26,
7787/* x */ 0x18,
7788/* y */ 0x19,
7789/* z */ 0x3F,
7790/* AA - AC */
7791 0, 0, 0,
7792/* [ */ 0x27,
7793/* AE - BC */
7794 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7795/* ] */ 0x1D,
7796/* BE - C0 */ 0, 0, 0,
7797/* A */ 0x01,
7798/* B */ 0x02,
7799/* C */ 0x03,
7800/* D */ 0x37,
7801/* E */ 0x2D,
7802/* F */ 0x2E,
7803/* G */ 0x2F,
7804/* H */ 0x16,
7805/* I */ 0x05,
7806/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7807/* J */ 0x15,
7808/* K */ 0x0B,
7809/* L */ 0x0C,
7810/* M */ 0x0D,
7811/* N */ 0x0E,
7812/* O */ 0x0F,
7813/* P */ 0x10,
7814/* Q */ 0x11,
7815/* R */ 0x12,
7816/* DA - DF */ 0, 0, 0, 0, 0, 0,
7817/* \ */ 0x1C,
7818/* E1 */ 0,
7819/* S */ 0x13,
7820/* T */ 0x3C,
7821/* U */ 0x3D,
7822/* V */ 0x32,
7823/* W */ 0x26,
7824/* X */ 0x18,
7825/* Y */ 0x19,
7826/* Z */ 0x3F,
7827/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7828 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7829};
7830
7831char MetaCharTable[]=
7832{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7833 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7834 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7835 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7836 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7837};
7838
7839
7840/* TODO: Use characters NOT numbers!!! */
7841char CtrlCharTable[]=
7842{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7843 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7844 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7845 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7846 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7847};
7848
7849
7850#endif