blob: 8f51e68cace6da6c65e94d92dee98725e8a0db97 [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 Moolenaar67c53842010-05-22 18:28:27 +0200415#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200416 if (wtime < 0 && did_start_blocking)
417 /* blocking and already waited for p_ut */
418 wait_time = -1;
419 else
420 {
421 if (wtime >= 0)
422 wait_time = wtime;
423 else
424 /* going to block after p_ut */
425 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100426#ifdef ELAPSED_FUNC
427 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200428#endif
429 wait_time -= elapsed_time;
430 if (wait_time < 0)
431 {
432 if (wtime >= 0)
433 /* no character available within "wtime" */
434 return 0;
435
436 if (wtime < 0)
437 {
438 /* no character available within 'updatetime' */
439 did_start_blocking = TRUE;
440#ifdef FEAT_AUTOCMD
441 if (trigger_cursorhold() && maxlen >= 3
442 && !typebuf_changed(tb_change_cnt))
443 {
444 buf[0] = K_SPECIAL;
445 buf[1] = KS_EXTRA;
446 buf[2] = (int)KE_CURSORHOLD;
447 return 3;
448 }
449#endif
450 /*
451 * If there is no character available within 'updatetime'
452 * seconds flush all the swap files to disk.
453 * Also done when interrupted by SIGWINCH.
454 */
455 before_blocking();
456 continue;
457 }
458 }
459 }
460
461#ifdef FEAT_JOB_CHANNEL
462 /* Checking if a job ended requires polling. Do this every 100 msec. */
463 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
464 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100465 /* If there is readahead then parse_queued_messages() timed out and we
466 * should call it again soon. */
467 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
468 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200469#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100470#ifdef FEAT_BEVAL
471 if (p_beval && wait_time > 100L)
472 /* The 'balloonexpr' may indirectly invoke a callback while waiting
473 * for a character, need to check often. */
474 wait_time = 100L;
475#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200478 * We want to be interrupted by the winch signal
479 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 */
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200481 if (WaitForChar(wait_time, &interrupted))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200482 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200483 /* If input was put directly in typeahead buffer bail out here. */
484 if (typebuf_changed(tb_change_cnt))
485 return 0;
486
487 /*
488 * For some terminals we only get one character at a time.
489 * We want the get all available characters, so we could keep on
490 * trying until none is available
491 * For some other terminals this is quite slow, that's why we don't
492 * do it.
493 */
494 len = read_from_input_buf(buf, (long)maxlen);
495 if (len > 0)
496 return len;
497 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200500 /* no character available */
501#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
502 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100503 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200506 if (do_resize /* interrupted by SIGWINCH signal */
507#ifdef FEAT_CLIENTSERVER
508 || server_waiting()
509#endif
510#ifdef MESSAGE_QUEUE
511 || interrupted
512#endif
513 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100514 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200515 continue;
516
517 /* no character available or interrupted */
518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200520 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521}
522
523 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100524handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 do_resize = FALSE;
527 shell_resized();
528}
529
530/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200531 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
533 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100534mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaarcda77642016-06-04 13:32:35 +0200536 return WaitForChar(0L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537}
538
539#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
540# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000541# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542# endif
543# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
544# include <sys/sysctl.h>
545# endif
546# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
547# include <sys/sysinfo.h>
548# endif
549
550/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000551 * Return total amount of memory available in Kbyte.
552 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100555mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000558 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200560# ifdef HAVE_SYSCTL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 int mib[2], physmem;
562 size_t len;
563
564 /* BSD way of getting the amount of RAM available. */
565 mib[0] = CTL_HW;
566 mib[1] = HW_USERMEM;
567 len = sizeof(physmem);
568 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
569 mem = (long_u)physmem;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200572# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (mem == 0)
574 {
575 struct sysinfo sinfo;
576
577 /* Linux way of getting amount of RAM available */
578 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000579 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200580# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000581 /* avoid overflow as much as possible */
582 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
583 {
584 sinfo.mem_unit = sinfo.mem_unit >> 1;
585 --shiftright;
586 }
587 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200588# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200590# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200595# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (mem == 0)
597 {
598 long pagesize, pagecount;
599
600 /* Solaris way of getting amount of RAM available */
601 pagesize = sysconf(_SC_PAGESIZE);
602 pagecount = sysconf(_SC_PHYS_PAGES);
603 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000604 {
605 /* avoid overflow as much as possible */
606 while (shiftright > 0 && (pagesize & 1) == 0)
607 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000608 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000609 --shiftright;
610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /* Return the minimum of the physical memory and the user limit, because
617 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200618# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
620 struct rlimit rlp;
621
622 if (getrlimit(RLIMIT_DATA, &rlp) == 0
623 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200624# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200626# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000627 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000629 {
630 mem = (long_u)rlp.rlim_cur;
631 shiftright = 10;
632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000637 return mem >> shiftright;
638 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639}
640#endif
641
642 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100643mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000646#ifdef FEAT_MZSCHEME
647 long total = msec; /* remember original value */
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 if (ignoreinput)
651 {
652 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000653 * here. But we don't want QUIT to kill us (CTRL-\ used in a
654 * shell may produce SIGQUIT). */
655 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 old_tmode = curr_tmode;
657 if (curr_tmode == TMODE_RAW)
658 settmode(TMODE_SLEEP);
659
660 /*
661 * Everybody sleeps in a different way...
662 * Prefer nanosleep(), some versions of usleep() can only sleep up to
663 * one second.
664 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000665#ifdef FEAT_MZSCHEME
666 do
667 {
668 /* if total is large enough, wait by portions in p_mzq */
669 if (total > p_mzq)
670 msec = p_mzq;
671 else
672 msec = total;
673 total -= msec;
674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef HAVE_NANOSLEEP
676 {
677 struct timespec ts;
678
679 ts.tv_sec = msec / 1000;
680 ts.tv_nsec = (msec % 1000) * 1000000;
681 (void)nanosleep(&ts, NULL);
682 }
683#else
684# ifdef HAVE_USLEEP
685 while (msec >= 1000)
686 {
687 usleep((unsigned int)(999 * 1000));
688 msec -= 999;
689 }
690 usleep((unsigned int)(msec * 1000));
691# else
692# ifndef HAVE_SELECT
693 poll(NULL, 0, (int)msec);
694# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
696 struct timeval tv;
697
698 tv.tv_sec = msec / 1000;
699 tv.tv_usec = (msec % 1000) * 1000;
700 /*
701 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
702 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
703 */
704 select(0, NULL, NULL, NULL, &tv);
705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706# endif /* HAVE_SELECT */
707# endif /* HAVE_NANOSLEEP */
708#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000709#ifdef FEAT_MZSCHEME
710 }
711 while (total > 0);
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
714 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000715 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717 else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200718 WaitForChar(msec, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719}
720
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000721#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
723# define HAVE_CHECK_STACK_GROWTH
724/*
725 * Support for checking for an almost-out-of-stack-space situation.
726 */
727
728/*
729 * Return a pointer to an item on the stack. Used to find out if the stack
730 * grows up or down.
731 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100732static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733static int stack_grows_downwards;
734
735/*
736 * Find out if the stack grows upwards or downwards.
737 * "p" points to a variable on the stack of the caller.
738 */
739 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100740check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741{
742 int i;
743
744 stack_grows_downwards = (p > (char *)&i);
745}
746#endif
747
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000748#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749static char *stack_limit = NULL;
750
751#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
752# include <pthread.h>
753# include <pthread_np.h>
754#endif
755
756/*
757 * Find out until how var the stack can grow without getting into trouble.
758 * Called when starting up and when switching to the signal stack in
759 * deathtrap().
760 */
761 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100762get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 struct rlimit rlp;
765 int i;
766 long lim;
767
768 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
769 * limit doesn't fit in a long (rlim_cur might be "long long"). */
770 if (getrlimit(RLIMIT_STACK, &rlp) == 0
771 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
772# ifdef RLIM_INFINITY
773 && rlp.rlim_cur != RLIM_INFINITY
774# endif
775 )
776 {
777 lim = (long)rlp.rlim_cur;
778#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
779 {
780 pthread_attr_t attr;
781 size_t size;
782
783 /* On FreeBSD the initial thread always has a fixed stack size, no
784 * matter what the limits are set to. Normally it's 1 Mbyte. */
785 pthread_attr_init(&attr);
786 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
787 {
788 pthread_attr_getstacksize(&attr, &size);
789 if (lim > (long)size)
790 lim = (long)size;
791 }
792 pthread_attr_destroy(&attr);
793 }
794#endif
795 if (stack_grows_downwards)
796 {
797 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
798 if (stack_limit >= (char *)&i)
799 /* overflow, set to 1/16 of current stack position */
800 stack_limit = (char *)((long)&i / 16L);
801 }
802 else
803 {
804 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
805 if (stack_limit <= (char *)&i)
806 stack_limit = NULL; /* overflow */
807 }
808 }
809}
810
811/*
812 * Return FAIL when running out of stack space.
813 * "p" must point to any variable local to the caller that's on the stack.
814 */
815 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100816mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 if (stack_limit != NULL)
819 {
820 if (stack_grows_downwards)
821 {
822 if (p < stack_limit)
823 return FAIL;
824 }
825 else if (p > stack_limit)
826 return FAIL;
827 }
828 return OK;
829}
830#endif
831
832#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
833/*
834 * Support for using the signal stack.
835 * This helps when we run out of stack space, which causes a SIGSEGV. The
836 * signal handler then must run on another stack, since the normal stack is
837 * completely full.
838 */
839
Bram Moolenaar39766a72013-11-03 00:41:00 +0100840#if defined(HAVE_AVAILABILITYMACROS_H)
Bram Moolenaar4cc95d12013-11-02 21:49:32 +0100841# include <AvailabilityMacros.h>
842#endif
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844#ifndef SIGSTKSZ
845# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
846#endif
847
848# ifdef HAVE_SIGALTSTACK
849static stack_t sigstk; /* for sigaltstack() */
850# else
851static struct sigstack sigstk; /* for sigstack() */
852# endif
853
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100854static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855static char *signal_stack;
856
857 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100858init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
860 if (signal_stack != NULL)
861 {
862# ifdef HAVE_SIGALTSTACK
Bram Moolenaar1a3d0862007-08-30 09:47:38 +0000863# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
864 || MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 /* missing prototype. Adding it to osdef?.h.in doesn't work, because
866 * "struct sigaltstack" needs to be declared. */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100867 extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868# endif
869
870# ifdef HAVE_SS_BASE
871 sigstk.ss_base = signal_stack;
872# else
873 sigstk.ss_sp = signal_stack;
874# endif
875 sigstk.ss_size = SIGSTKSZ;
876 sigstk.ss_flags = 0;
877 (void)sigaltstack(&sigstk, NULL);
878# else
879 sigstk.ss_sp = signal_stack;
880 if (stack_grows_downwards)
881 sigstk.ss_sp += SIGSTKSZ - 1;
882 sigstk.ss_onstack = 0;
883 (void)sigstack(&sigstk, NULL);
884# endif
885 }
886}
887#endif
888
889/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000890 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * will barf when the second argument to signal() is ``wrong''.
892 * Let me try it with a few tricky defines from my own osdef.h (jw).
893 */
894#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 static RETSIGTYPE
896sig_winch SIGDEFARG(sigarg)
897{
898 /* this is not required on all systems, but it doesn't hurt anybody */
899 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
900 do_resize = TRUE;
901 SIGRETURN;
902}
903#endif
904
905#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 static RETSIGTYPE
907catch_sigint SIGDEFARG(sigarg)
908{
909 /* this is not required on all systems, but it doesn't hurt anybody */
910 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
911 got_int = TRUE;
912 SIGRETURN;
913}
914#endif
915
916#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 static RETSIGTYPE
918catch_sigpwr SIGDEFARG(sigarg)
919{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000920 /* this is not required on all systems, but it doesn't hurt anybody */
921 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 /*
923 * I'm not sure we get the SIGPWR signal when the system is really going
924 * down or when the batteries are almost empty. Just preserve the swap
925 * files and don't exit, that can't do any harm.
926 */
927 ml_sync_all(FALSE, FALSE);
928 SIGRETURN;
929}
930#endif
931
932#ifdef SET_SIG_ALARM
933/*
934 * signal function for alarm().
935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 static RETSIGTYPE
937sig_alarm SIGDEFARG(sigarg)
938{
939 /* doesn't do anything, just to break a system call */
940 sig_alarm_called = TRUE;
941 SIGRETURN;
942}
943#endif
944
Bram Moolenaar44ecf652005-03-07 23:09:59 +0000945#if (defined(HAVE_SETJMP_H) \
946 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
947 || defined(FEAT_LIBCALL))) \
948 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949/*
950 * A simplistic version of setjmp() that only allows one level of using.
951 * Don't call twice before calling mch_endjmp()!.
952 * Usage:
953 * mch_startjmp();
954 * if (SETJMP(lc_jump_env) != 0)
955 * {
956 * mch_didjmp();
957 * EMSG("crash!");
958 * }
959 * else
960 * {
961 * do_the_work;
962 * mch_endjmp();
963 * }
964 * Note: Can't move SETJMP() here, because a function calling setjmp() must
965 * not return before the saved environment is used.
966 * Returns OK for normal return, FAIL when the protected code caused a
967 * problem and LONGJMP() was used.
968 */
969 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100970mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
972#ifdef SIGHASARG
973 lc_signal = 0;
974#endif
975 lc_active = TRUE;
976}
977
978 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100979mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 lc_active = FALSE;
982}
983
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
988 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
989 * otherwise catching the signal only works once. */
990 init_signal_stack();
991# endif
992}
993#endif
994
995/*
996 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200997 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200999 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1000 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 */
1002 static RETSIGTYPE
1003deathtrap SIGDEFARG(sigarg)
1004{
1005 static int entered = 0; /* count the number of times we got here.
1006 Note: when memory has been corrupted
1007 this may get an arbitrary value! */
1008#ifdef SIGHASARG
1009 int i;
1010#endif
1011
1012#if defined(HAVE_SETJMP_H)
1013 /*
1014 * Catch a crash in protected code.
1015 * Restores the environment saved in lc_jump_env, which looks like
1016 * SETJMP() returns 1.
1017 */
1018 if (lc_active)
1019 {
1020# if defined(SIGHASARG)
1021 lc_signal = sigarg;
1022# endif
1023 lc_active = FALSE; /* don't jump again */
1024 LONGJMP(lc_jump_env, 1);
1025 /* NOTREACHED */
1026 }
1027#endif
1028
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001029#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001030# ifdef SIGQUIT
1031 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1032 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1033 * pressing CTRL-\, but we don't want Vim to exit then. */
1034 if (in_mch_delay && sigarg == SIGQUIT)
1035 SIGRETURN;
1036# endif
1037
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001038 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1039 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1040 * free(). Calling free() again may then cause a crash. */
1041 if (entered == 0
1042 && (0
1043# ifdef SIGHUP
1044 || sigarg == SIGHUP
1045# endif
1046# ifdef SIGQUIT
1047 || sigarg == SIGQUIT
1048# endif
1049# ifdef SIGTERM
1050 || sigarg == SIGTERM
1051# endif
1052# ifdef SIGPWR
1053 || sigarg == SIGPWR
1054# endif
1055# ifdef SIGUSR1
1056 || sigarg == SIGUSR1
1057# endif
1058# ifdef SIGUSR2
1059 || sigarg == SIGUSR2
1060# endif
1061 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001062 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001063 SIGRETURN;
1064#endif
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 /* Remember how often we have been called. */
1067 ++entered;
1068
Bram Moolenaare429e702016-06-10 19:49:14 +02001069#ifdef FEAT_AUTOCMD
1070 /* Executing autocommands is likely to use more stack space than we have
1071 * available in the signal stack. */
1072 block_autocmds();
1073#endif
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#ifdef FEAT_EVAL
1076 /* Set the v:dying variable. */
1077 set_vim_var_nr(VV_DYING, (long)entered);
1078#endif
1079
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001080#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 /* Since we are now using the signal stack, need to reset the stack
1082 * limit. Otherwise using a regexp will fail. */
1083 get_stack_limit();
1084#endif
1085
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001086#if 0
1087 /* This is for opening gdb the moment Vim crashes.
1088 * You need to manually adjust the file name and Vim executable name.
1089 * Suggested by SungHyun Nam. */
1090 {
1091# define VI_GDB_FILE "/tmp/vimgdb"
1092# define VIM_NAME "/usr/bin/vim"
1093 FILE *fp = fopen(VI_GDB_FILE, "w");
1094 if (fp)
1095 {
1096 fprintf(fp,
1097 "file %s\n"
1098 "attach %d\n"
1099 "set height 1000\n"
1100 "bt full\n"
1101 , VIM_NAME, getpid());
1102 fclose(fp);
1103 system("xterm -e gdb -x "VI_GDB_FILE);
1104 unlink(VI_GDB_FILE);
1105 }
1106 }
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109#ifdef SIGHASARG
1110 /* try to find the name of this signal */
1111 for (i = 0; signal_info[i].sig != -1; i++)
1112 if (sigarg == signal_info[i].sig)
1113 break;
1114 deadly_signal = sigarg;
1115#endif
1116
1117 full_screen = FALSE; /* don't write message to the GUI, it might be
1118 * part of the problem... */
1119 /*
1120 * If something goes wrong after entering here, we may get here again.
1121 * When this happens, give a message and try to exit nicely (resetting the
1122 * terminal mode, etc.)
1123 * When this happens twice, just exit, don't even try to give a message,
1124 * stack may be corrupt or something weird.
1125 * When this still happens again (or memory was corrupted in such a way
1126 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1127 */
1128 if (entered >= 3)
1129 {
1130 reset_signals(); /* don't catch any signals anymore */
1131 may_core_dump();
1132 if (entered >= 4)
1133 _exit(8);
1134 exit(7);
1135 }
1136 if (entered == 2)
1137 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001138 /* No translation, it may call malloc(). */
1139 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 out_flush();
1141 getout(1);
1142 }
1143
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001144 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001146 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 signal_info[i].name);
1148#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001149 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001151
1152 /* Preserve files and exit. This sets the really_exiting flag to prevent
1153 * calling free(). */
1154 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
Bram Moolenaare429e702016-06-10 19:49:14 +02001156 /* NOTREACHED */
1157
Bram Moolenaar009b2592004-10-24 19:18:58 +00001158#ifdef NBDEBUG
1159 reset_signals();
1160 may_core_dump();
1161 abort();
1162#endif
1163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 SIGRETURN;
1165}
1166
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001167#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168/*
1169 * On Solaris with multi-threading, suspending might not work immediately.
1170 * Catch the SIGCONT signal, which will be used as an indication whether the
1171 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001172 *
1173 * On Linux, signal is not always handled immediately either.
1174 * See https://bugs.launchpad.net/bugs/291373
1175 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001176 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001178static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001179static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
1181/*
1182 * signal handler for SIGCONT
1183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 static RETSIGTYPE
1185sigcont_handler SIGDEFARG(sigarg)
1186{
1187 sigcont_received = TRUE;
1188 SIGRETURN;
1189}
1190#endif
1191
Bram Moolenaar62b42182010-09-21 22:09:37 +02001192# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001193static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001194# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001195static void save_clipboard(void);
1196static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001197
1198static void *clip_star_save = NULL;
1199static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001200# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001201
1202/*
1203 * Called when Vim is going to sleep or execute a shell command.
1204 * We can't respond to requests for the X selections. Lose them, otherwise
1205 * other applications will hang. But first copy the text to cut buffer 0.
1206 */
1207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001208loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001209{
1210 if (clip_star.owned || clip_plus.owned)
1211 {
1212 x11_export_final_selection();
1213 if (clip_star.owned)
1214 clip_lose_selection(&clip_star);
1215 if (clip_plus.owned)
1216 clip_lose_selection(&clip_plus);
1217 if (x11_display != NULL)
1218 XFlush(x11_display);
1219 }
1220}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001221
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001222# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001223/*
1224 * Save clipboard text to restore later.
1225 */
1226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001227save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001228{
1229 if (clip_star.owned)
1230 clip_star_save = get_register('*', TRUE);
1231 if (clip_plus.owned)
1232 clip_plus_save = get_register('+', TRUE);
1233}
1234
1235/*
1236 * Restore clipboard text if no one own the X selection.
1237 */
1238 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001239restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001240{
1241 if (clip_star_save != NULL)
1242 {
1243 if (!clip_gen_owner_exists(&clip_star))
1244 put_register('*', clip_star_save);
1245 else
1246 free_register(clip_star_save);
1247 clip_star_save = NULL;
1248 }
1249 if (clip_plus_save != NULL)
1250 {
1251 if (!clip_gen_owner_exists(&clip_plus))
1252 put_register('+', clip_plus_save);
1253 else
1254 free_register(clip_plus_save);
1255 clip_plus_save = NULL;
1256 }
1257}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001258# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001259#endif
1260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261/*
1262 * If the machine has job control, use it to suspend the program,
1263 * otherwise fake it by starting a new shell.
1264 */
1265 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001266mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
1268 /* BeOS does have SIGTSTP, but it doesn't work. */
1269#if defined(SIGTSTP) && !defined(__BEOS__)
1270 out_flush(); /* needed to make cursor visible on some systems */
1271 settmode(TMODE_COOK);
1272 out_flush(); /* needed to disable mouse on some systems */
1273
1274# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001275 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# endif
1277
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001278# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 sigcont_received = FALSE;
1280# endif
1281 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001282# if defined(_REENTRANT) && defined(SIGCONT)
1283 /*
1284 * Wait for the SIGCONT signal to be handled. It generally happens
1285 * immediately, but somehow not all the time. Do not call pause()
1286 * because there would be race condition which would hang Vim if
1287 * signal happened in between the test of sigcont_received and the
1288 * call to pause(). If signal is not yet received, call sleep(0)
1289 * to just yield CPU. Signal should then be received. If somehow
1290 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1291 * further if signal is not received after 1+2+3+4 ms (not expected
1292 * to happen).
1293 */
1294 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001295 long wait_time;
1296 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001297 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001298 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300# endif
1301
1302# ifdef FEAT_TITLE
1303 /*
1304 * Set oldtitle to NULL, so the current title is obtained again.
1305 */
1306 vim_free(oldtitle);
1307 oldtitle = NULL;
1308# endif
1309 settmode(TMODE_RAW);
1310 need_check_timestamps = TRUE;
1311 did_check_timestamps = FALSE;
1312#else
1313 suspend_shell();
1314#endif
1315}
1316
1317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001318mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 Columns = 80;
1321 Rows = 24;
1322
1323 out_flush();
1324 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001325
Bram Moolenaar56718732006-03-15 22:53:57 +00001326#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001327 mac_conv_init();
1328#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001329#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1330 win_clip_init();
1331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332}
1333
1334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001335set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337#if defined(SIGWINCH)
1338 /*
1339 * WINDOW CHANGE signal is handled with sig_winch().
1340 */
1341 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1342#endif
1343
1344 /*
1345 * We want the STOP signal to work, to make mch_suspend() work.
1346 * For "rvim" the STOP signal is ignored.
1347 */
1348#ifdef SIGTSTP
1349 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1350#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001351#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 signal(SIGCONT, sigcont_handler);
1353#endif
1354
1355 /*
1356 * We want to ignore breaking of PIPEs.
1357 */
1358#ifdef SIGPIPE
1359 signal(SIGPIPE, SIG_IGN);
1360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001363 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#endif
1365
1366 /*
1367 * Ignore alarm signals (Perl's alarm() generates it).
1368 */
1369#ifdef SIGALRM
1370 signal(SIGALRM, SIG_IGN);
1371#endif
1372
1373 /*
1374 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1375 * work will be lost.
1376 */
1377#ifdef SIGPWR
1378 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1379#endif
1380
1381 /*
1382 * Arrange for other signals to gracefully shutdown Vim.
1383 */
1384 catch_signals(deathtrap, SIG_ERR);
1385
1386#if defined(FEAT_GUI) && defined(SIGHUP)
1387 /*
1388 * When the GUI is running, ignore the hangup signal.
1389 */
1390 if (gui.in_use)
1391 signal(SIGHUP, SIG_IGN);
1392#endif
1393}
1394
Bram Moolenaardf177f62005-02-22 08:39:57 +00001395#if defined(SIGINT) || defined(PROTO)
1396/*
1397 * Catch CTRL-C (only works while in Cooked mode).
1398 */
1399 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001400catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001401{
1402 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1403}
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001407reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001410#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /* SIGCONT isn't in the list, because its default action is ignore */
1412 signal(SIGCONT, SIG_DFL);
1413#endif
1414}
1415
1416 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001417catch_signals(
1418 RETSIGTYPE (*func_deadly)(),
1419 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 int i;
1422
1423 for (i = 0; signal_info[i].sig != -1; i++)
1424 if (signal_info[i].deadly)
1425 {
1426#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1427 struct sigaction sa;
1428
1429 /* Setup to use the alternate stack for the signal function. */
1430 sa.sa_handler = func_deadly;
1431 sigemptyset(&sa.sa_mask);
1432# if defined(__linux__) && defined(_REENTRANT)
1433 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1434 * thread handling in combination with using the alternate stack:
1435 * pthread library functions try to use the stack pointer to
1436 * identify the current thread, causing a SEGV signal, which
1437 * recursively calls deathtrap() and hangs. */
1438 sa.sa_flags = 0;
1439# else
1440 sa.sa_flags = SA_ONSTACK;
1441# endif
1442 sigaction(signal_info[i].sig, &sa, NULL);
1443#else
1444# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1445 struct sigvec sv;
1446
1447 /* Setup to use the alternate stack for the signal function. */
1448 sv.sv_handler = func_deadly;
1449 sv.sv_mask = 0;
1450 sv.sv_flags = SV_ONSTACK;
1451 sigvec(signal_info[i].sig, &sv, NULL);
1452# else
1453 signal(signal_info[i].sig, func_deadly);
1454# endif
1455#endif
1456 }
1457 else if (func_other != SIG_ERR)
1458 signal(signal_info[i].sig, func_other);
1459}
1460
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001461#ifdef HAVE_SIGPROCMASK
1462 static void
1463block_signals(sigset_t *set)
1464{
1465 sigset_t newset;
1466 int i;
1467
1468 sigemptyset(&newset);
1469
1470 for (i = 0; signal_info[i].sig != -1; i++)
1471 sigaddset(&newset, signal_info[i].sig);
1472
1473# if defined(_REENTRANT) && defined(SIGCONT)
1474 /* SIGCONT isn't in the list, because its default action is ignore */
1475 sigaddset(&newset, SIGCONT);
1476# endif
1477
1478 sigprocmask(SIG_BLOCK, &newset, set);
1479}
1480
1481 static void
1482unblock_signals(sigset_t *set)
1483{
1484 sigprocmask(SIG_SETMASK, set, NULL);
1485}
1486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001489 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001490 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1491 * return TRUE
1492 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1493 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001494 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001495 * Returns TRUE when Vim should exit.
1496 */
1497 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001498vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001499{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001500 static int got_signal = 0;
1501 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001502
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001503 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001504 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001505 case SIGNAL_BLOCK: blocked = TRUE;
1506 break;
1507
1508 case SIGNAL_UNBLOCK: blocked = FALSE;
1509 if (got_signal != 0)
1510 {
1511 kill(getpid(), got_signal);
1512 got_signal = 0;
1513 }
1514 break;
1515
1516 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001517 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001518 got_signal = sig;
1519#ifdef SIGPWR
1520 if (sig != SIGPWR)
1521#endif
1522 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001523 break;
1524 }
1525 return FALSE;
1526}
1527
1528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 * Check_win checks whether we have an interactive stdout.
1530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001532mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (isatty(1))
1535 return OK;
1536 return FAIL;
1537}
1538
1539/*
1540 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1541 */
1542 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001543mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 if (isatty(read_cmd_fd))
1546 return TRUE;
1547 return FALSE;
1548}
1549
1550#ifdef FEAT_X11
1551
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001552# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
1556 * Give a message about the elapsed time for opening the X window.
1557 */
1558 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001559xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001561 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562}
1563# endif
1564#endif
1565
1566#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1567/*
1568 * A few functions shared by X11 title and clipboard code.
1569 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001570static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1571static int x_error_check(Display *dpy, XErrorEvent *error_event);
1572static int x_connect_to_server(void);
1573static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575static int got_x_error = FALSE;
1576
1577/*
1578 * X Error handler, otherwise X just exits! (very rude) -- webb
1579 */
1580 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001581x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001583 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 STRCAT(IObuff, _("\nVim: Got X error\n"));
1585
1586 /* We cannot print a message and continue, because no X calls are allowed
1587 * here (causes my system to hang). Silently continuing might be an
1588 * alternative... */
1589 preserve_exit(); /* preserve files and exit */
1590
1591 return 0; /* NOTREACHED */
1592}
1593
1594/*
1595 * Another X Error handler, just used to check for errors.
1596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001598x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
1600 got_x_error = TRUE;
1601 return 0;
1602}
1603
1604#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1605# if defined(HAVE_SETJMP_H)
1606/*
1607 * An X IO Error handler, used to catch error while opening the display.
1608 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001609static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001612x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 /* This function should not return, it causes exit(). Longjump instead. */
1615 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001616# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001617 return 0; /* avoid the compiler complains about missing return value */
1618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619}
1620# endif
1621
1622/*
1623 * An X IO Error handler, used to catch terminal errors.
1624 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001625static int x_IOerror_handler(Display *dpy);
1626static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001627static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001630x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001633 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 x11_window = 0;
1635 x11_display = NULL;
1636 xterm_Shell = (Widget)0;
1637
1638 /* This function should not return, it causes exit(). Longjump instead. */
1639 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001640# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001641 return 0; /* avoid the compiler complains about missing return value */
1642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001644
1645/*
1646 * If the X11 connection was lost try to restore it.
1647 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001648 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001649 */
1650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001651may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001652{
1653 if (xterm_dpy_was_reset)
1654 {
1655 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001656
1657# ifndef LESSTIF_VERSION
1658 /* This has been reported to avoid Vim getting stuck. */
1659 if (app_context != (XtAppContext)NULL)
1660 {
1661 XtDestroyApplicationContext(app_context);
1662 app_context = (XtAppContext)NULL;
1663 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1664 }
1665# endif
1666
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001667 setup_term_clip();
1668 get_x11_title(FALSE);
1669 }
1670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671#endif
1672
1673/*
1674 * Return TRUE when connection to the X server is desired.
1675 */
1676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001677x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#if defined(FEAT_CLIENTSERVER)
1680 if (x_force_connect)
1681 return TRUE;
1682#endif
1683 if (x_no_connect)
1684 return FALSE;
1685
1686 /* Check for a match with "exclude:" from 'clipboard'. */
1687 if (clip_exclude_prog != NULL)
1688 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001689 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 return FALSE;
1691 }
1692 return TRUE;
1693}
1694
1695/*
1696 * Test if "dpy" and x11_window are valid by getting the window title.
1697 * I don't actually want it yet, so there may be a simpler call to use, but
1698 * this will cause the error handler x_error_check() to be called if anything
1699 * is wrong, such as the window pointer being invalid (as can happen when the
1700 * user changes his DISPLAY, but not his WINDOWID) -- webb
1701 */
1702 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001703test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 int (*old_handler)();
1706 XTextProperty text_prop;
1707
1708 old_handler = XSetErrorHandler(x_error_check);
1709 got_x_error = FALSE;
1710 if (XGetWMName(dpy, x11_window, &text_prop))
1711 XFree((void *)text_prop.value);
1712 XSync(dpy, False);
1713 (void)XSetErrorHandler(old_handler);
1714
1715 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001716 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718 return (got_x_error ? FAIL : OK);
1719}
1720#endif
1721
1722#ifdef FEAT_TITLE
1723
1724#ifdef FEAT_X11
1725
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001726static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728/*
1729 * try to get x11 window and display
1730 *
1731 * return FAIL for failure, OK otherwise
1732 */
1733 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001734get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
1736 char *winid;
1737 static int result = -1;
1738#define XD_NONE 0 /* x11_display not set here */
1739#define XD_HERE 1 /* x11_display opened here */
1740#define XD_GUI 2 /* x11_display used from gui.dpy */
1741#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1742 static int x11_display_from = XD_NONE;
1743 static int did_set_error_handler = FALSE;
1744
1745 if (!did_set_error_handler)
1746 {
1747 /* X just exits if it finds an error otherwise! */
1748 (void)XSetErrorHandler(x_error_handler);
1749 did_set_error_handler = TRUE;
1750 }
1751
Bram Moolenaar9372a112005-12-06 19:59:18 +00001752#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (gui.in_use)
1754 {
1755 /*
1756 * If the X11 display was opened here before, for the window where Vim
1757 * was started, close that one now to avoid a memory leak.
1758 */
1759 if (x11_display_from == XD_HERE && x11_display != NULL)
1760 {
1761 XCloseDisplay(x11_display);
1762 x11_display_from = XD_NONE;
1763 }
1764 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1765 {
1766 x11_display_from = XD_GUI;
1767 return OK;
1768 }
1769 x11_display = NULL;
1770 return FAIL;
1771 }
1772 else if (x11_display_from == XD_GUI)
1773 {
1774 /* GUI must have stopped somehow, clear x11_display */
1775 x11_window = 0;
1776 x11_display = NULL;
1777 x11_display_from = XD_NONE;
1778 }
1779#endif
1780
1781 /* When started with the "-X" argument, don't try connecting. */
1782 if (!x_connect_to_server())
1783 return FAIL;
1784
1785 /*
1786 * If WINDOWID not set, should try another method to find out
1787 * what the current window number is. The only code I know for
1788 * this is very complicated.
1789 * We assume that zero is invalid for WINDOWID.
1790 */
1791 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1792 x11_window = (Window)atol(winid);
1793
1794#ifdef FEAT_XCLIPBOARD
1795 if (xterm_dpy != NULL && x11_window != 0)
1796 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001797 /* We may have checked it already, but Gnome terminal can move us to
1798 * another window, so we need to check every time. */
1799 if (x11_display_from != XD_XTERM)
1800 {
1801 /*
1802 * If the X11 display was opened here before, for the window where
1803 * Vim was started, close that one now to avoid a memory leak.
1804 */
1805 if (x11_display_from == XD_HERE && x11_display != NULL)
1806 XCloseDisplay(x11_display);
1807 x11_display = xterm_dpy;
1808 x11_display_from = XD_XTERM;
1809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (test_x11_window(x11_display) == FAIL)
1811 {
1812 /* probably bad $WINDOWID */
1813 x11_window = 0;
1814 x11_display = NULL;
1815 x11_display_from = XD_NONE;
1816 return FAIL;
1817 }
1818 return OK;
1819 }
1820#endif
1821
1822 if (x11_window == 0 || x11_display == NULL)
1823 result = -1;
1824
1825 if (result != -1) /* Have already been here and set this */
1826 return result; /* Don't do all these X calls again */
1827
1828 if (x11_window != 0 && x11_display == NULL)
1829 {
1830#ifdef SET_SIG_ALARM
1831 RETSIGTYPE (*sig_save)();
1832#endif
1833#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1834 struct timeval start_tv;
1835
1836 if (p_verbose > 0)
1837 gettimeofday(&start_tv, NULL);
1838#endif
1839
1840#ifdef SET_SIG_ALARM
1841 /*
1842 * Opening the Display may hang if the DISPLAY setting is wrong, or
1843 * the network connection is bad. Set an alarm timer to get out.
1844 */
1845 sig_alarm_called = FALSE;
1846 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1847 (RETSIGTYPE (*)())sig_alarm);
1848 alarm(2);
1849#endif
1850 x11_display = XOpenDisplay(NULL);
1851
1852#ifdef SET_SIG_ALARM
1853 alarm(0);
1854 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1855 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001856 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857#endif
1858 if (x11_display != NULL)
1859 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001860# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001862 {
1863 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001864 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001865 verbose_leave();
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867# endif
1868 if (test_x11_window(x11_display) == FAIL)
1869 {
1870 /* Maybe window id is bad */
1871 x11_window = 0;
1872 XCloseDisplay(x11_display);
1873 x11_display = NULL;
1874 }
1875 else
1876 x11_display_from = XD_HERE;
1877 }
1878 }
1879 if (x11_window == 0 || x11_display == NULL)
1880 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001881
1882# ifdef FEAT_EVAL
1883 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1884# endif
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 return (result = OK);
1887}
1888
1889/*
1890 * Determine original x11 Window Title
1891 */
1892 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001893get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001895 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896}
1897
1898/*
1899 * Determine original x11 Window icon
1900 */
1901 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001902get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 int retval = FALSE;
1905
1906 retval = get_x11_thing(FALSE, test_only);
1907
1908 /* could not get old icon, use terminal name */
1909 if (oldicon == NULL && !test_only)
1910 {
1911 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001912 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001914 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916
1917 return retval;
1918}
1919
1920 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001921get_x11_thing(
1922 int get_title, /* get title string */
1923 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924{
1925 XTextProperty text_prop;
1926 int retval = FALSE;
1927 Status status;
1928
1929 if (get_x11_windis() == OK)
1930 {
1931 /* Get window/icon name if any */
1932 if (get_title)
1933 status = XGetWMName(x11_display, x11_window, &text_prop);
1934 else
1935 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1936
1937 /*
1938 * If terminal is xterm, then x11_window may be a child window of the
1939 * outer xterm window that actually contains the window/icon name, so
1940 * keep traversing up the tree until a window with a title/icon is
1941 * found.
1942 */
1943 /* Previously this was only done for xterm and alikes. I don't see a
1944 * reason why it would fail for other terminal emulators.
1945 * if (term_is_xterm) */
1946 {
1947 Window root;
1948 Window parent;
1949 Window win = x11_window;
1950 Window *children;
1951 unsigned int num_children;
1952
1953 while (!status || text_prop.value == NULL)
1954 {
1955 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1956 &num_children))
1957 break;
1958 if (children)
1959 XFree((void *)children);
1960 if (parent == root || parent == 0)
1961 break;
1962
1963 win = parent;
1964 if (get_title)
1965 status = XGetWMName(x11_display, win, &text_prop);
1966 else
1967 status = XGetWMIconName(x11_display, win, &text_prop);
1968 }
1969 }
1970 if (status && text_prop.value != NULL)
1971 {
1972 retval = TRUE;
1973 if (!test_only)
1974 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001975#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
1976 if (text_prop.encoding == XA_STRING
1977# ifdef FEAT_MBYTE
1978 && !has_mbyte
1979# endif
1980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
1982#endif
1983 if (get_title)
1984 oldtitle = vim_strsave((char_u *)text_prop.value);
1985 else
1986 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02001987#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989 else
1990 {
1991 char **cl;
1992 Status transform_status;
1993 int n = 0;
1994
1995 transform_status = XmbTextPropertyToTextList(x11_display,
1996 &text_prop,
1997 &cl, &n);
1998 if (transform_status >= Success && n > 0 && cl[0])
1999 {
2000 if (get_title)
2001 oldtitle = vim_strsave((char_u *) cl[0]);
2002 else
2003 oldicon = vim_strsave((char_u *) cl[0]);
2004 XFreeStringList(cl);
2005 }
2006 else
2007 {
2008 if (get_title)
2009 oldtitle = vim_strsave((char_u *)text_prop.value);
2010 else
2011 oldicon = vim_strsave((char_u *)text_prop.value);
2012 }
2013 }
2014#endif
2015 }
2016 XFree((void *)text_prop.value);
2017 }
2018 }
2019 return retval;
2020}
2021
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002022/* Xutf8 functions are not avaialble on older systems. Note that on some
2023 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2024 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2025 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002027# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028# define USE_UTF8_STRING
2029# endif
2030#endif
2031
2032/*
2033 * Set x11 Window Title
2034 *
2035 * get_x11_windis() must be called before this and have returned OK
2036 */
2037 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002038set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2041 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2042 * supported everywhere and STRING doesn't work for multi-byte titles.
2043 */
2044#ifdef USE_UTF8_STRING
2045 if (enc_utf8)
2046 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2047 NULL, NULL, 0, NULL, NULL, NULL);
2048 else
2049#endif
2050 {
2051#if XtSpecificationRelease >= 4
2052# ifdef FEAT_XFONTSET
2053 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2054 NULL, NULL, 0, NULL, NULL, NULL);
2055# else
2056 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002057 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002060 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 XSetWMProperties(x11_display, x11_window, &text_prop,
2062 NULL, NULL, 0, NULL, NULL, NULL);
2063# endif
2064#else
2065 XStoreName(x11_display, x11_window, (char *)title);
2066#endif
2067 }
2068 XFlush(x11_display);
2069}
2070
2071/*
2072 * Set x11 Window icon
2073 *
2074 * get_x11_windis() must be called before this and have returned OK
2075 */
2076 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002077set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079 /* See above for comments about using X*SetWMProperties(). */
2080#ifdef USE_UTF8_STRING
2081 if (enc_utf8)
2082 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2083 NULL, 0, NULL, NULL, NULL);
2084 else
2085#endif
2086 {
2087#if XtSpecificationRelease >= 4
2088# ifdef FEAT_XFONTSET
2089 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2090 NULL, 0, NULL, NULL, NULL);
2091# else
2092 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002093 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002095 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2097 NULL, 0, NULL, NULL, NULL);
2098# endif
2099#else
2100 XSetIconName(x11_display, x11_window, (char *)icon);
2101#endif
2102 }
2103 XFlush(x11_display);
2104}
2105
2106#else /* FEAT_X11 */
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002109get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 return FALSE;
2112}
2113
2114 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002115get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 if (!test_only)
2118 {
2119 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002120 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002122 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124 return FALSE;
2125}
2126
2127#endif /* FEAT_X11 */
2128
2129 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002130mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 return get_x11_title(TRUE);
2133}
2134
2135 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002136mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 return get_x11_icon(TRUE);
2139}
2140
2141/*
2142 * Set the window title and icon.
2143 */
2144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002145mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147 int type = 0;
2148 static int recursive = 0;
2149
2150 if (T_NAME == NULL) /* no terminal name (yet) */
2151 return;
2152 if (title == NULL && icon == NULL) /* nothing to do */
2153 return;
2154
2155 /* When one of the X11 functions causes a deadly signal, we get here again
2156 * recursively. Avoid hanging then (something is probably locked). */
2157 if (recursive)
2158 return;
2159 ++recursive;
2160
2161 /*
2162 * if the window ID and the display is known, we may use X11 calls
2163 */
2164#ifdef FEAT_X11
2165 if (get_x11_windis() == OK)
2166 type = 1;
2167#else
2168# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2169 if (gui.in_use)
2170 type = 1;
2171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#endif
2173
2174 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002175 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 * than x11 calls, because the x11 calls don't always work
2177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if ((type || *T_TS != NUL) && title != NULL)
2179 {
2180 if (oldtitle == NULL
2181#ifdef FEAT_GUI
2182 && !gui.in_use
2183#endif
2184 ) /* first call but not in GUI, save title */
2185 (void)get_x11_title(FALSE);
2186
2187 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2188 term_settitle(title);
2189#ifdef FEAT_X11
2190 else
2191# ifdef FEAT_GUI_GTK
2192 if (!gui.in_use) /* don't do this if GTK+ is running */
2193# endif
2194 set_x11_title(title); /* x11 */
2195#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002196#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2198 else
2199 gui_mch_settitle(title, icon);
2200#endif
2201 did_set_title = TRUE;
2202 }
2203
2204 if ((type || *T_CIS != NUL) && icon != NULL)
2205 {
2206 if (oldicon == NULL
2207#ifdef FEAT_GUI
2208 && !gui.in_use
2209#endif
2210 ) /* first call, save icon */
2211 get_x11_icon(FALSE);
2212
2213 if (*T_CIS != NUL)
2214 {
2215 out_str(T_CIS); /* set icon start */
2216 out_str_nf(icon);
2217 out_str(T_CIE); /* set icon end */
2218 out_flush();
2219 }
2220#ifdef FEAT_X11
2221 else
2222# ifdef FEAT_GUI_GTK
2223 if (!gui.in_use) /* don't do this if GTK+ is running */
2224# endif
2225 set_x11_icon(icon); /* x11 */
2226#endif
2227 did_set_icon = TRUE;
2228 }
2229 --recursive;
2230}
2231
2232/*
2233 * Restore the window/icon title.
2234 * "which" is one of:
2235 * 1 only restore title
2236 * 2 only restore icon
2237 * 3 restore title and icon
2238 */
2239 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002240mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 /* only restore the title or icon when it has been set */
2243 mch_settitle(((which & 1) && did_set_title) ?
2244 (oldtitle ? oldtitle : p_titleold) : NULL,
2245 ((which & 2) && did_set_icon) ? oldicon : NULL);
2246}
2247
2248#endif /* FEAT_TITLE */
2249
2250/*
2251 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002252 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 */
2254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002255vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 if (name == NULL)
2258 return FALSE;
2259 return (STRNICMP(name, "xterm", 5) == 0
2260 || STRNICMP(name, "nxterm", 6) == 0
2261 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002262 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 || STRNICMP(name, "rxvt", 4) == 0
2264 || STRCMP(name, "builtin_xterm") == 0);
2265}
2266
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002267#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2268/*
2269 * Return TRUE if "name" appears to be that of a terminal
2270 * known to support the xterm-style mouse protocol.
2271 * Relies on term_is_xterm having been set to its correct value.
2272 */
2273 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002274use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002275{
2276 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002277 && (term_is_xterm
2278 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002279 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002280 || STRICMP(name, "st") == 0
2281 || STRNICMP(name, "st-", 3) == 0
2282 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002283}
2284#endif
2285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2287/*
2288 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2289 * Return 1 for "xterm".
2290 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002291 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002292 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 */
2294 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002295use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002297 if (ttym_flags == TTYM_SGR)
2298 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002299 if (ttym_flags == TTYM_URXVT)
2300 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (ttym_flags == TTYM_XTERM2)
2302 return 2;
2303 if (ttym_flags == TTYM_XTERM)
2304 return 1;
2305 return 0;
2306}
2307#endif
2308
2309 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002310vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 if (name == NULL)
2313 return FALSE;
2314 return (STRNICMP(name, "iris-ansi", 9) == 0
2315 || STRCMP(name, "builtin_iris-ansi") == 0);
2316}
2317
2318 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002319vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 if (name == NULL)
2322 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002323 /* catch VT100 - VT5xx */
2324 return ((STRNICMP(name, "vt", 2) == 0
2325 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 || STRCMP(name, "builtin_vt320") == 0);
2327}
2328
2329/*
2330 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2331 * This should include all windowed terminal emulators.
2332 */
2333 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002334vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 if (name == NULL)
2337 return FALSE;
2338 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2339 return TRUE;
2340 return ( STRNICMP(name, "hpterm", 6) == 0
2341 || STRNICMP(name, "sun-cmd", 7) == 0
2342 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002343 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 || STRNICMP(name, "dtterm", 6) == 0);
2345}
2346
2347/*
2348 * Insert user name in s[len].
2349 * Return OK if a name found.
2350 */
2351 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002352mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002355 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 return OK;
2357#else
2358 return mch_get_uname(getuid(), s, len);
2359#endif
2360}
2361
2362/*
2363 * Insert user name for "uid" in s[len].
2364 * Return OK if a name found.
2365 */
2366 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002367mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2370 struct passwd *pw;
2371
2372 if ((pw = getpwuid(uid)) != NULL
2373 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2374 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002375 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 return OK;
2377 }
2378#endif
2379 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2380 return FAIL; /* a number is not a name */
2381}
2382
2383/*
2384 * Insert host name is s[len].
2385 */
2386
2387#ifdef HAVE_SYS_UTSNAME_H
2388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002389mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 struct utsname vutsname;
2392
2393 if (uname(&vutsname) < 0)
2394 *s = NUL;
2395 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002396 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397}
2398#else /* HAVE_SYS_UTSNAME_H */
2399
2400# ifdef HAVE_SYS_SYSTEMINFO_H
2401# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2402# endif
2403
2404 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002405mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407# ifdef VAXC
2408 vaxc$gethostname((char *)s, len);
2409# else
2410 gethostname((char *)s, len);
2411# endif
2412 s[len - 1] = NUL; /* make sure it's terminated */
2413}
2414#endif /* HAVE_SYS_UTSNAME_H */
2415
2416/*
2417 * return process ID
2418 */
2419 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002420mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 return (long)getpid();
2423}
2424
2425#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002426static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002429strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
2431 extern int sys_nerr;
2432 extern char *sys_errlist[];
2433 static char er[20];
2434
2435 if (err > 0 && err < sys_nerr)
2436 return (sys_errlist[err]);
2437 sprintf(er, "Error %d", err);
2438 return er;
2439}
2440#endif
2441
2442/*
2443 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2444 * Return OK for success, FAIL for failure.
2445 */
2446 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002447mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449#if defined(USE_GETCWD)
2450 if (getcwd((char *)buf, len) == NULL)
2451 {
2452 STRCPY(buf, strerror(errno));
2453 return FAIL;
2454 }
2455 return OK;
2456#else
2457 return (getwd((char *)buf) != NULL ? OK : FAIL);
2458#endif
2459}
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002462 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 *
2464 * return FAIL for failure, OK for success
2465 */
2466 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002467mch_FullName(
2468 char_u *fname,
2469 char_u *buf,
2470 int len,
2471 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002474#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 int fd = -1;
2476 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 char_u olddir[MAXPATHL];
2479 char_u *p;
2480 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002481#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002482 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2483 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002484#endif
2485
Bram Moolenaar38323e42007-03-06 19:22:53 +00002486#ifdef VMS
2487 fname = vms_fixfilename(fname);
2488#endif
2489
Bram Moolenaara2442432007-04-26 14:26:37 +00002490#ifdef __CYGWIN__
2491 /*
2492 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2493 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002494# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002495 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2496 * a forward slash. */
2497 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2498 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002499# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002500 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002501# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002502 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002503#endif
2504
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002505 /* Expand it if forced or not an absolute path.
2506 * Do not do it for "/file", the result is always "/". */
2507 if ((force || !mch_isFullName(fname))
2508 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 /*
2511 * If the file name has a path, change to that directory for a moment,
2512 * and then do the getwd() (and get back to where we were).
2513 * This will get the correct path name with "../" things.
2514 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002515 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002517#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * Use fchdir() if possible, it's said to be faster and more
2520 * reliable. But on SunOS 4 it might not work. Check this by
2521 * doing a fchdir() right now.
2522 */
2523 if (!dont_fchdir)
2524 {
2525 fd = open(".", O_RDONLY | O_EXTRA, 0);
2526 if (fd >= 0 && fchdir(fd) < 0)
2527 {
2528 close(fd);
2529 fd = -1;
2530 dont_fchdir = TRUE; /* don't try again */
2531 }
2532 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 /* Only change directory when we are sure we can return to where
2536 * we are now. After doing "su" chdir(".") might not work. */
2537 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002538#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 (mch_dirname(olddir, MAXPATHL) == FAIL
2542 || mch_chdir((char *)olddir) != 0))
2543 {
2544 p = NULL; /* can't get current dir: don't chdir */
2545 retval = FAIL;
2546 }
2547 else
2548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 /* The directory is copied into buf[], to be able to remove
2550 * the file name without changing it (could be a string in
2551 * read-only memory) */
2552 if (p - fname >= len)
2553 retval = FAIL;
2554 else
2555 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002556 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (mch_chdir((char *)buf))
2558 retval = FAIL;
2559 else
2560 fname = p + 1;
2561 *buf = NUL;
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 }
2565 if (mch_dirname(buf, len) == FAIL)
2566 {
2567 retval = FAIL;
2568 *buf = NUL;
2569 }
2570 if (p != NULL)
2571 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002572#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (fd >= 0)
2574 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002575 if (p_verbose >= 5)
2576 {
2577 verbose_enter();
2578 MSG("fchdir() to previous dir");
2579 verbose_leave();
2580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 l = fchdir(fd);
2582 close(fd);
2583 }
2584 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 l = mch_chdir((char *)olddir);
2587 if (l != 0)
2588 EMSG(_(e_prev_dir));
2589 }
2590
2591 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002592 if (l >= len - 1)
2593 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002594#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002595 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002597 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002602 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 return FAIL;
2604
2605 /* Do not append ".", "/dir/." is equal to "/dir". */
2606 if (STRCMP(fname, ".") != 0)
2607 STRCAT(buf, fname);
2608
2609 return OK;
2610}
2611
2612/*
2613 * Return TRUE if "fname" does not depend on the current directory.
2614 */
2615 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002616mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002618#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 return ( fname[0] == '/' || fname[0] == '.' ||
2620 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2621 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2622 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002623#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625#endif
2626}
2627
Bram Moolenaar24552be2005-12-10 20:17:30 +00002628#if defined(USE_FNAME_CASE) || defined(PROTO)
2629/*
2630 * Set the case of the file name, if it already exists. This will cause the
2631 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002632 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002633 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002634 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002635fname_case(
2636 char_u *name,
2637 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002638{
2639 struct stat st;
2640 char_u *slash, *tail;
2641 DIR *dirp;
2642 struct dirent *dp;
2643
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002644 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002645 {
2646 /* Open the directory where the file is located. */
2647 slash = vim_strrchr(name, '/');
2648 if (slash == NULL)
2649 {
2650 dirp = opendir(".");
2651 tail = name;
2652 }
2653 else
2654 {
2655 *slash = NUL;
2656 dirp = opendir((char *)name);
2657 *slash = '/';
2658 tail = slash + 1;
2659 }
2660
2661 if (dirp != NULL)
2662 {
2663 while ((dp = readdir(dirp)) != NULL)
2664 {
2665 /* Only accept names that differ in case and are the same byte
2666 * length. TODO: accept different length name. */
2667 if (STRICMP(tail, dp->d_name) == 0
2668 && STRLEN(tail) == STRLEN(dp->d_name))
2669 {
2670 char_u newname[MAXPATHL + 1];
2671 struct stat st2;
2672
2673 /* Verify the inode is equal. */
2674 vim_strncpy(newname, name, MAXPATHL);
2675 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2676 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002677 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002678 && st.st_ino == st2.st_ino
2679 && st.st_dev == st2.st_dev)
2680 {
2681 STRCPY(tail, dp->d_name);
2682 break;
2683 }
2684 }
2685 }
2686
2687 closedir(dirp);
2688 }
2689 }
2690}
2691#endif
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693/*
2694 * Get file permissions for 'name'.
2695 * Returns -1 when it doesn't exist.
2696 */
2697 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002698mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 struct stat statb;
2701
2702 /* Keep the #ifdef outside of stat(), it may be a macro. */
2703#ifdef VMS
2704 if (stat((char *)vms_fixfilename(name), &statb))
2705#else
2706 if (stat((char *)name, &statb))
2707#endif
2708 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002709#ifdef __INTERIX
2710 /* The top bit makes the value negative, which means the file doesn't
2711 * exist. Remove the bit, we don't use it. */
2712 return statb.st_mode & ~S_ADDACE;
2713#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716}
2717
2718/*
2719 * set file permission for 'name' to 'perm'
2720 *
2721 * return FAIL for failure, OK otherwise
2722 */
2723 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002724mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 return (chmod((char *)
2727#ifdef VMS
2728 vms_fixfilename(name),
2729#else
2730 name,
2731#endif
2732 (mode_t)perm) == 0 ? OK : FAIL);
2733}
2734
2735#if defined(HAVE_ACL) || defined(PROTO)
2736# ifdef HAVE_SYS_ACL_H
2737# include <sys/acl.h>
2738# endif
2739# ifdef HAVE_SYS_ACCESS_H
2740# include <sys/access.h>
2741# endif
2742
2743# ifdef HAVE_SOLARIS_ACL
2744typedef struct vim_acl_solaris_T {
2745 int acl_cnt;
2746 aclent_t *acl_entry;
2747} vim_acl_solaris_T;
2748# endif
2749
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002750#if defined(HAVE_SELINUX) || defined(PROTO)
2751/*
2752 * Copy security info from "from_file" to "to_file".
2753 */
2754 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002755mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002756{
2757 if (from_file == NULL)
2758 return;
2759
2760 if (selinux_enabled == -1)
2761 selinux_enabled = is_selinux_enabled();
2762
2763 if (selinux_enabled > 0)
2764 {
2765 security_context_t from_context = NULL;
2766 security_context_t to_context = NULL;
2767
2768 if (getfilecon((char *)from_file, &from_context) < 0)
2769 {
2770 /* If the filesystem doesn't support extended attributes,
2771 the original had no special security context and the
2772 target cannot have one either. */
2773 if (errno == EOPNOTSUPP)
2774 return;
2775
2776 MSG_PUTS(_("\nCould not get security context for "));
2777 msg_outtrans(from_file);
2778 msg_putchar('\n');
2779 return;
2780 }
2781 if (getfilecon((char *)to_file, &to_context) < 0)
2782 {
2783 MSG_PUTS(_("\nCould not get security context for "));
2784 msg_outtrans(to_file);
2785 msg_putchar('\n');
2786 freecon (from_context);
2787 return ;
2788 }
2789 if (strcmp(from_context, to_context) != 0)
2790 {
2791 if (setfilecon((char *)to_file, from_context) < 0)
2792 {
2793 MSG_PUTS(_("\nCould not set security context for "));
2794 msg_outtrans(to_file);
2795 msg_putchar('\n');
2796 }
2797 }
2798 freecon(to_context);
2799 freecon(from_context);
2800 }
2801}
2802#endif /* HAVE_SELINUX */
2803
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002804#if defined(HAVE_SMACK) && !defined(PROTO)
2805/*
2806 * Copy security info from "from_file" to "to_file".
2807 */
2808 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002809mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002810{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002811 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002812 {
2813 XATTR_NAME_SMACK,
2814 XATTR_NAME_SMACKEXEC,
2815 XATTR_NAME_SMACKMMAP
2816 };
2817
2818 char buffer[SMACK_LABEL_LEN];
2819 const char *name;
2820 int index;
2821 int ret;
2822 ssize_t size;
2823
2824 if (from_file == NULL)
2825 return;
2826
2827 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2828 / sizeof(smack_copied_attributes)[0]) ; index++)
2829 {
2830 /* get the name of the attribute to copy */
2831 name = smack_copied_attributes[index];
2832
2833 /* get the value of the attribute in buffer */
2834 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2835 if (size >= 0)
2836 {
2837 /* copy the attribute value of buffer */
2838 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2839 if (ret < 0)
2840 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002841 vim_snprintf((char *)IObuff, IOSIZE,
2842 _("Could not set security context %s for %s"),
2843 name, to_file);
2844 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002845 msg_putchar('\n');
2846 }
2847 }
2848 else
2849 {
2850 /* what reason of not having the attribute value? */
2851 switch (errno)
2852 {
2853 case ENOTSUP:
2854 /* extended attributes aren't supported or enabled */
2855 /* should a message be echoed? not sure... */
2856 return; /* leave because it isn't usefull to continue */
2857
2858 case ERANGE:
2859 default:
2860 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002861 vim_snprintf((char *)IObuff, IOSIZE,
2862 _("Could not get security context %s for %s. Removing it!"),
2863 name, from_file);
2864 msg_puts(IObuff);
2865 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002866 /* FALLTHROUGH to remove the attribute */
2867
2868 case ENODATA:
2869 /* no attribute of this name */
2870 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002871 /* Silently ignore errors, apparently this happens when
2872 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002873 break;
2874 }
2875 }
2876 }
2877}
2878#endif /* HAVE_SMACK */
2879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880/*
2881 * Return a pointer to the ACL of file "fname" in allocated memory.
2882 * Return NULL if the ACL is not available for whatever reason.
2883 */
2884 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002885mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 vim_acl_T ret = NULL;
2888#ifdef HAVE_POSIX_ACL
2889 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2890#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002891#ifdef HAVE_SOLARIS_ZFS_ACL
2892 acl_t *aclent;
2893
2894 if (acl_get((char *)fname, 0, &aclent) < 0)
2895 return NULL;
2896 ret = (vim_acl_T)aclent;
2897#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898#ifdef HAVE_SOLARIS_ACL
2899 vim_acl_solaris_T *aclent;
2900
2901 aclent = malloc(sizeof(vim_acl_solaris_T));
2902 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2903 {
2904 free(aclent);
2905 return NULL;
2906 }
2907 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2908 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2909 {
2910 free(aclent->acl_entry);
2911 free(aclent);
2912 return NULL;
2913 }
2914 ret = (vim_acl_T)aclent;
2915#else
2916#if defined(HAVE_AIX_ACL)
2917 int aclsize;
2918 struct acl *aclent;
2919
2920 aclsize = sizeof(struct acl);
2921 aclent = malloc(aclsize);
2922 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2923 {
2924 if (errno == ENOSPC)
2925 {
2926 aclsize = aclent->acl_len;
2927 aclent = realloc(aclent, aclsize);
2928 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2929 {
2930 free(aclent);
2931 return NULL;
2932 }
2933 }
2934 else
2935 {
2936 free(aclent);
2937 return NULL;
2938 }
2939 }
2940 ret = (vim_acl_T)aclent;
2941#endif /* HAVE_AIX_ACL */
2942#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002943#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944#endif /* HAVE_POSIX_ACL */
2945 return ret;
2946}
2947
2948/*
2949 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2950 */
2951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002952mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 if (aclent == NULL)
2955 return;
2956#ifdef HAVE_POSIX_ACL
2957 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
2958#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002959#ifdef HAVE_SOLARIS_ZFS_ACL
2960 acl_set((char *)fname, (acl_t *)aclent);
2961#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962#ifdef HAVE_SOLARIS_ACL
2963 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
2964 ((vim_acl_solaris_T *)aclent)->acl_entry);
2965#else
2966#ifdef HAVE_AIX_ACL
2967 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
2968#endif /* HAVE_AIX_ACL */
2969#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002970#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#endif /* HAVE_POSIX_ACL */
2972}
2973
2974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002975mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 if (aclent == NULL)
2978 return;
2979#ifdef HAVE_POSIX_ACL
2980 acl_free((acl_t)aclent);
2981#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002982#ifdef HAVE_SOLARIS_ZFS_ACL
2983 acl_free((acl_t *)aclent);
2984#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#ifdef HAVE_SOLARIS_ACL
2986 free(((vim_acl_solaris_T *)aclent)->acl_entry);
2987 free(aclent);
2988#else
2989#ifdef HAVE_AIX_ACL
2990 free(aclent);
2991#endif /* HAVE_AIX_ACL */
2992#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002993#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#endif /* HAVE_POSIX_ACL */
2995}
2996#endif
2997
2998/*
2999 * Set hidden flag for "name".
3000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003002mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 /* can't hide a file */
3005}
3006
3007/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003008 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 * return FALSE if "name" is not a directory
3010 * return FALSE for error
3011 */
3012 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003013mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 struct stat statb;
3016
3017 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3018 return FALSE;
3019 if (stat((char *)name, &statb))
3020 return FALSE;
3021#ifdef _POSIX_SOURCE
3022 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3023#else
3024 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3025#endif
3026}
3027
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003028/*
3029 * return TRUE if "name" is a directory, NOT a symlink to a directory
3030 * return FALSE if "name" is not a directory
3031 * return FALSE for error
3032 */
3033 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003034mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003035{
3036 struct stat statb;
3037
3038 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3039 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003040 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003041 return FALSE;
3042#ifdef _POSIX_SOURCE
3043 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3044#else
3045 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3046#endif
3047}
3048
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003049static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051/*
3052 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3053 */
3054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003055executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 struct stat st;
3058
3059 if (stat((char *)name, &st))
3060 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003061#ifdef VMS
3062 /* Like on Unix system file can have executable rights but not necessarily
3063 * be an executable, but on Unix is not a default for an ordianry file to
3064 * have an executable flag - on VMS it is in most cases.
3065 * Therefore, this check does not have any sense - let keep us to the
3066 * conventions instead:
3067 * *.COM and *.EXE files are the executables - the rest are not. This is
3068 * not ideal but better then it was.
3069 */
3070 int vms_executable = 0;
3071 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3072 {
3073 if (strstr(vms_tolower((char*)name),".exe") != NULL
3074 || strstr(vms_tolower((char*)name),".com")!= NULL)
3075 vms_executable = 1;
3076 }
3077 return vms_executable;
3078#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081}
3082
3083/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003084 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003085 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * Return -1 if unknown.
3087 */
3088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003089mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 char_u *buf;
3092 char_u *p, *e;
3093 int retval;
3094
Bram Moolenaarb5971142015-03-21 17:32:19 +01003095 /* When "use_path" is false and if it's an absolute or relative path don't
3096 * need to use $PATH. */
3097 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3098 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003099 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003100 /* There must be a path separator, files in the current directory
3101 * can't be executed. */
3102 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003103 {
3104 if (path != NULL)
3105 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003106 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003107 *path = FullName_save(name, TRUE);
3108 else
3109 *path = vim_strsave(name);
3110 }
3111 return TRUE;
3112 }
3113 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 p = (char_u *)getenv("PATH");
3117 if (p == NULL || *p == NUL)
3118 return -1;
3119 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3120 if (buf == NULL)
3121 return -1;
3122
3123 /*
3124 * Walk through all entries in $PATH to check if "name" exists there and
3125 * is an executable file.
3126 */
3127 for (;;)
3128 {
3129 e = (char_u *)strchr((char *)p, ':');
3130 if (e == NULL)
3131 e = p + STRLEN(p);
3132 if (e - p <= 1) /* empty entry means current dir */
3133 STRCPY(buf, "./");
3134 else
3135 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003136 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 add_pathsep(buf);
3138 }
3139 STRCAT(buf, name);
3140 retval = executable_file(buf);
3141 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003142 {
3143 if (path != NULL)
3144 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003145 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003146 *path = FullName_save(buf, TRUE);
3147 else
3148 *path = vim_strsave(buf);
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (*e != ':')
3154 break;
3155 p = e + 1;
3156 }
3157
3158 vim_free(buf);
3159 return retval;
3160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162/*
3163 * Check what "name" is:
3164 * NODE_NORMAL: file or directory (or doesn't exist)
3165 * NODE_WRITABLE: writable device, socket, fifo, etc.
3166 * NODE_OTHER: non-writable things
3167 */
3168 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003169mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 struct stat st;
3172
3173 if (stat((char *)name, &st))
3174 return NODE_NORMAL;
3175 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3176 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3178 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 /* Everything else is writable? */
3180 return NODE_WRITABLE;
3181}
3182
3183 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003184mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186#ifdef HAVE_CHECK_STACK_GROWTH
3187 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 check_stack_growth((char *)&i);
3190
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003191# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 get_stack_limit();
3193# endif
3194
3195#endif
3196
3197 /*
3198 * Setup an alternative stack for signals. Helps to catch signals when
3199 * running out of stack space.
3200 * Use of sigaltstack() is preferred, it's more portable.
3201 * Ignore any errors.
3202 */
3203#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003204 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 init_signal_stack();
3206#endif
3207}
3208
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003209#if defined(EXITFREE) || defined(PROTO)
3210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003211mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003212{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3214 if (clip_star.owned)
3215 clip_lose_selection(&clip_star);
3216 if (clip_plus.owned)
3217 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003218# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003219# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003220 if (xterm_Shell != (Widget)0)
3221 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003222# ifndef LESSTIF_VERSION
3223 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003224 if (xterm_dpy != NULL)
3225 XtCloseDisplay(xterm_dpy);
3226 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003227 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003228 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003229# ifdef FEAT_X11
3230 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3231# endif
3232 }
3233# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003234# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003235# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003236 if (x11_display != NULL
3237# ifdef FEAT_XCLIPBOARD
3238 && x11_display != xterm_dpy
3239# endif
3240 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003241 XCloseDisplay(x11_display);
3242# endif
3243# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
3244 vim_free(signal_stack);
3245 signal_stack = NULL;
3246# endif
3247# ifdef FEAT_TITLE
3248 vim_free(oldtitle);
3249 vim_free(oldicon);
3250# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003251}
3252#endif
3253
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003254static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256/*
3257 * Output a newline when exiting.
3258 * Make sure the newline goes to the same stream as the text.
3259 */
3260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003261exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003263 if (silent_mode)
3264 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (newline_on_exit || msg_didout)
3266 {
3267 if (msg_use_printf())
3268 {
3269 if (info_message)
3270 mch_msg("\n");
3271 else
3272 mch_errmsg("\r\n");
3273 }
3274 else
3275 out_char('\n');
3276 }
3277 else
3278 {
3279 restore_cterm_colors(); /* get original colors back */
3280 msg_clr_eos_force(); /* clear the rest of the display */
3281 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3282 }
3283}
3284
3285 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003286mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 exiting = TRUE;
3289
3290#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3291 x11_export_final_selection();
3292#endif
3293
3294#ifdef FEAT_GUI
3295 if (!gui.in_use)
3296#endif
3297 {
3298 settmode(TMODE_COOK);
3299#ifdef FEAT_TITLE
3300 mch_restore_title(3); /* restore xterm title and icon name */
3301#endif
3302 /*
3303 * When t_ti is not empty but it doesn't cause swapping terminal
3304 * pages, need to output a newline when msg_didout is set. But when
3305 * t_ti does swap pages it should not go to the shell page. Do this
3306 * before stoptermcap().
3307 */
3308 if (swapping_screen() && !newline_on_exit)
3309 exit_scroll();
3310
3311 /* Stop termcap: May need to check for T_CRV response, which
3312 * requires RAW mode. */
3313 stoptermcap();
3314
3315 /*
3316 * A newline is only required after a message in the alternate screen.
3317 * This is set to TRUE by wait_return().
3318 */
3319 if (!swapping_screen() || newline_on_exit)
3320 exit_scroll();
3321
3322 /* Cursor may have been switched off without calling starttermcap()
3323 * when doing "vim -u vimrc" and vimrc contains ":q". */
3324 if (full_screen)
3325 cursor_on();
3326 }
3327 out_flush();
3328 ml_close_all(TRUE); /* remove all memfiles */
3329 may_core_dump();
3330#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 gui_exit(r);
3333#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003334
Bram Moolenaar56718732006-03-15 22:53:57 +00003335#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003336 mac_conv_cleanup();
3337#endif
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#ifdef __QNX__
3340 /* A core dump won't be created if the signal handler
3341 * doesn't return, so we can't call exit() */
3342 if (deadly_signal != 0)
3343 return;
3344#endif
3345
Bram Moolenaar009b2592004-10-24 19:18:58 +00003346#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003347 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003348#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003349
3350#ifdef EXITFREE
3351 free_all_mem();
3352#endif
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 exit(r);
3355}
3356
3357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003358may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 if (deadly_signal != 0)
3361 {
3362 signal(deadly_signal, SIG_DFL);
3363 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3364 }
3365}
3366
3367#ifndef VMS
3368
3369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003370mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 static int first = TRUE;
3373
3374 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3375#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3376 /*
3377 * for "new" tty systems
3378 */
3379# ifdef HAVE_TERMIOS_H
3380 static struct termios told;
3381 struct termios tnew;
3382# else
3383 static struct termio told;
3384 struct termio tnew;
3385# endif
3386
3387 if (first)
3388 {
3389 first = FALSE;
3390# if defined(HAVE_TERMIOS_H)
3391 tcgetattr(read_cmd_fd, &told);
3392# else
3393 ioctl(read_cmd_fd, TCGETA, &told);
3394# endif
3395 }
3396
3397 tnew = told;
3398 if (tmode == TMODE_RAW)
3399 {
3400 /*
3401 * ~ICRNL enables typing ^V^M
3402 */
3403 tnew.c_iflag &= ~ICRNL;
3404 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3405# if defined(IEXTEN) && !defined(__MINT__)
3406 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3407 /* but it breaks function keys on MINT */
3408# endif
3409 );
3410# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3411 tnew.c_oflag &= ~ONLCR;
3412# endif
3413 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3414 tnew.c_cc[VTIME] = 0; /* don't wait */
3415 }
3416 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003417 {
3418 /* Also reset ICANON here, otherwise on Solaris select() won't see
3419 * typeahead characters. */
3420 tnew.c_lflag &= ~(ICANON | ECHO);
3421 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3422 tnew.c_cc[VTIME] = 0; /* don't wait */
3423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425# if defined(HAVE_TERMIOS_H)
3426 {
3427 int n = 10;
3428
3429 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3430 * few times. */
3431 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3432 && errno == EINTR && n > 0)
3433 --n;
3434 }
3435# else
3436 ioctl(read_cmd_fd, TCSETA, &tnew);
3437# endif
3438
3439#else
3440
3441 /*
3442 * for "old" tty systems
3443 */
3444# ifndef TIOCSETN
3445# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3446# endif
3447 static struct sgttyb ttybold;
3448 struct sgttyb ttybnew;
3449
3450 if (first)
3451 {
3452 first = FALSE;
3453 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3454 }
3455
3456 ttybnew = ttybold;
3457 if (tmode == TMODE_RAW)
3458 {
3459 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3460 ttybnew.sg_flags |= RAW;
3461 }
3462 else if (tmode == TMODE_SLEEP)
3463 ttybnew.sg_flags &= ~(ECHO);
3464 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3465#endif
3466 curr_tmode = tmode;
3467}
3468
3469/*
3470 * Try to get the code for "t_kb" from the stty setting
3471 *
3472 * Even if termcap claims a backspace key, the user's setting *should*
3473 * prevail. stty knows more about reality than termcap does, and if
3474 * somebody's usual erase key is DEL (which, for most BSD users, it will
3475 * be), they're going to get really annoyed if their erase key starts
3476 * doing forward deletes for no reason. (Eric Fischer)
3477 */
3478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003479get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 char_u buf[2];
3482 char_u *p;
3483
3484 /* Why is NeXT excluded here (and not in os_unixx.h)? */
3485#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
3486 /* for "new" tty systems */
3487# ifdef HAVE_TERMIOS_H
3488 struct termios keys;
3489# else
3490 struct termio keys;
3491# endif
3492
3493# if defined(HAVE_TERMIOS_H)
3494 if (tcgetattr(read_cmd_fd, &keys) != -1)
3495# else
3496 if (ioctl(read_cmd_fd, TCGETA, &keys) != -1)
3497# endif
3498 {
3499 buf[0] = keys.c_cc[VERASE];
3500 intr_char = keys.c_cc[VINTR];
3501#else
3502 /* for "old" tty systems */
3503 struct sgttyb keys;
3504
3505 if (ioctl(read_cmd_fd, TIOCGETP, &keys) != -1)
3506 {
3507 buf[0] = keys.sg_erase;
3508 intr_char = keys.sg_kill;
3509#endif
3510 buf[1] = NUL;
3511 add_termcode((char_u *)"kb", buf, FALSE);
3512
3513 /*
3514 * If <BS> and <DEL> are now the same, redefine <DEL>.
3515 */
3516 p = find_termcode((char_u *)"kD");
3517 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3518 do_fixdel(NULL);
3519 }
3520#if 0
3521 } /* to keep cindent happy */
3522#endif
3523}
3524
3525#endif /* VMS */
3526
3527#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
3528/*
3529 * Set mouse clicks on or off.
3530 */
3531 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003532mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 static int ison = FALSE;
3535 int xterm_mouse_vers;
3536
3537 if (on == ison) /* return quickly if nothing to do */
3538 return;
3539
3540 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003541
3542# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003543 if (ttym_flags == TTYM_URXVT)
3544 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003545 out_str_nf((char_u *)
3546 (on
3547 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3548 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
3549 ison = on;
3550 }
3551# endif
3552
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003553# ifdef FEAT_MOUSE_SGR
3554 if (ttym_flags == TTYM_SGR)
3555 {
3556 out_str_nf((char_u *)
3557 (on
3558 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3559 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
3560 ison = on;
3561 }
3562# endif
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (xterm_mouse_vers > 0)
3565 {
3566 if (on) /* enable mouse events, use mouse tracking if available */
3567 out_str_nf((char_u *)
3568 (xterm_mouse_vers > 1
3569 ? IF_EB("\033[?1002h", ESC_STR "[?1002h")
3570 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3571 else /* disable mouse events, could probably always send the same */
3572 out_str_nf((char_u *)
3573 (xterm_mouse_vers > 1
3574 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3575 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
3576 ison = on;
3577 }
3578
3579# ifdef FEAT_MOUSE_DEC
3580 else if (ttym_flags == TTYM_DEC)
3581 {
3582 if (on) /* enable mouse events */
3583 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3584 else /* disable mouse events */
3585 out_str_nf((char_u *)"\033['z");
3586 ison = on;
3587 }
3588# endif
3589
3590# ifdef FEAT_MOUSE_GPM
3591 else
3592 {
3593 if (on)
3594 {
3595 if (gpm_open())
3596 ison = TRUE;
3597 }
3598 else
3599 {
3600 gpm_close();
3601 ison = FALSE;
3602 }
3603 }
3604# endif
3605
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003606# ifdef FEAT_SYSMOUSE
3607 else
3608 {
3609 if (on)
3610 {
3611 if (sysmouse_open() == OK)
3612 ison = TRUE;
3613 }
3614 else
3615 {
3616 sysmouse_close();
3617 ison = FALSE;
3618 }
3619 }
3620# endif
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622# ifdef FEAT_MOUSE_JSB
3623 else
3624 {
3625 if (on)
3626 {
3627 /* D - Enable Mouse up/down messages
3628 * L - Enable Left Button Reporting
3629 * M - Enable Middle Button Reporting
3630 * R - Enable Right Button Reporting
3631 * K - Enable SHIFT and CTRL key Reporting
3632 * + - Enable Advanced messaging of mouse moves and up/down messages
3633 * Q - Quiet No Ack
3634 * # - Numeric value of mouse pointer required
3635 * 0 = Multiview 2000 cursor, used as standard
3636 * 1 = Windows Arrow
3637 * 2 = Windows I Beam
3638 * 3 = Windows Hour Glass
3639 * 4 = Windows Cross Hair
3640 * 5 = Windows UP Arrow
3641 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003642# ifdef JSBTERM_MOUSE_NONADVANCED
3643 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3645 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003646# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3648 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 ison = TRUE;
3651 }
3652 else
3653 {
3654 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3655 ESC_STR "[0~ZwQ" ESC_STR "\\"));
3656 ison = FALSE;
3657 }
3658 }
3659# endif
3660# ifdef FEAT_MOUSE_PTERM
3661 else
3662 {
3663 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3664 if (on)
3665 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3666 else
3667 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
3668 ison = on;
3669 }
3670# endif
3671}
3672
3673/*
3674 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3675 */
3676 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003677check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679# ifdef FEAT_MOUSE_XTERM
3680 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003681# ifdef FEAT_MOUSE_URXVT
3682 && use_xterm_mouse() != 3
3683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684# ifdef FEAT_GUI
3685 && !gui.in_use
3686# endif
3687 )
3688 {
3689 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003690 ? IF_EB("\233M", CSI_STR "M")
3691 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (*p_mouse != NUL)
3693 {
3694 /* force mouse off and maybe on to send possibly new mouse
3695 * activation sequence to the xterm, with(out) drag tracing. */
3696 mch_setmouse(FALSE);
3697 setmouse();
3698 }
3699 }
3700 else
3701 del_mouse_termcode(KS_MOUSE);
3702# endif
3703
3704# ifdef FEAT_MOUSE_GPM
3705 if (!use_xterm_mouse()
3706# ifdef FEAT_GUI
3707 && !gui.in_use
3708# endif
3709 )
3710 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3711# endif
3712
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003713# ifdef FEAT_SYSMOUSE
3714 if (!use_xterm_mouse()
3715# ifdef FEAT_GUI
3716 && !gui.in_use
3717# endif
3718 )
3719 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3720# endif
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003723 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (!use_xterm_mouse()
3725# ifdef FEAT_GUI
3726 && !gui.in_use
3727# endif
3728 )
3729 set_mouse_termcode(KS_JSBTERM_MOUSE,
3730 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3731 else
3732 del_mouse_termcode(KS_JSBTERM_MOUSE);
3733# endif
3734
3735# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003736 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * define it in the GUI or when using an xterm. */
3738 if (!use_xterm_mouse()
3739# ifdef FEAT_GUI
3740 && !gui.in_use
3741# endif
3742 )
3743 set_mouse_termcode(KS_NETTERM_MOUSE,
3744 (char_u *)IF_EB("\033}", ESC_STR "}"));
3745 else
3746 del_mouse_termcode(KS_NETTERM_MOUSE);
3747# endif
3748
3749# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003750 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003751 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752# ifdef FEAT_GUI
3753 && !gui.in_use
3754# endif
3755 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003756 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3757 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else
3759 del_mouse_termcode(KS_DEC_MOUSE);
3760# endif
3761# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003762 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003763 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764# ifdef FEAT_GUI
3765 && !gui.in_use
3766# endif
3767 )
3768 set_mouse_termcode(KS_PTERM_MOUSE,
3769 (char_u *) IF_EB("\033[", ESC_STR "["));
3770 else
3771 del_mouse_termcode(KS_PTERM_MOUSE);
3772# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003773# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003774 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003775# ifdef FEAT_GUI
3776 && !gui.in_use
3777# endif
3778 )
3779 {
3780 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003781 ? IF_EB("\233*M", CSI_STR "*M")
3782 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003783
3784 if (*p_mouse != NUL)
3785 {
3786 mch_setmouse(FALSE);
3787 setmouse();
3788 }
3789 }
3790 else
3791 del_mouse_termcode(KS_URXVT_MOUSE);
3792# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003793# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003794 if (use_xterm_mouse() == 4
3795# ifdef FEAT_GUI
3796 && !gui.in_use
3797# endif
3798 )
3799 {
3800 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003801 ? IF_EB("\233<*M", CSI_STR "<*M")
3802 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3803
3804 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3805 ? IF_EB("\233<*m", CSI_STR "<*m")
3806 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003807
3808 if (*p_mouse != NUL)
3809 {
3810 mch_setmouse(FALSE);
3811 setmouse();
3812 }
3813 }
3814 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003815 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003816 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003817 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3818 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820}
3821#endif
3822
3823/*
3824 * set screen mode, always fails.
3825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003827mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 EMSG(_(e_screenmode));
3830 return FAIL;
3831}
3832
3833#ifndef VMS
3834
3835/*
3836 * Try to get the current window size:
3837 * 1. with an ioctl(), most accurate method
3838 * 2. from the environment variables LINES and COLUMNS
3839 * 3. from the termcap
3840 * 4. keep using the old values
3841 * Return OK when size could be determined, FAIL otherwise.
3842 */
3843 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003844mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 long rows = 0;
3847 long columns = 0;
3848 char_u *p;
3849
3850 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 * 1. try using an ioctl. It is the most accurate method.
3852 *
3853 * Try using TIOCGWINSZ first, some systems that have it also define
3854 * TIOCGSIZE but don't have a struct ttysize.
3855 */
3856# ifdef TIOCGWINSZ
3857 {
3858 struct winsize ws;
3859 int fd = 1;
3860
3861 /* When stdout is not a tty, use stdin for the ioctl(). */
3862 if (!isatty(fd) && isatty(read_cmd_fd))
3863 fd = read_cmd_fd;
3864 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3865 {
3866 columns = ws.ws_col;
3867 rows = ws.ws_row;
3868 }
3869 }
3870# else /* TIOCGWINSZ */
3871# ifdef TIOCGSIZE
3872 {
3873 struct ttysize ts;
3874 int fd = 1;
3875
3876 /* When stdout is not a tty, use stdin for the ioctl(). */
3877 if (!isatty(fd) && isatty(read_cmd_fd))
3878 fd = read_cmd_fd;
3879 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
3880 {
3881 columns = ts.ts_cols;
3882 rows = ts.ts_lines;
3883 }
3884 }
3885# endif /* TIOCGSIZE */
3886# endif /* TIOCGWINSZ */
3887
3888 /*
3889 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003890 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
3891 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003893 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
3895 if ((p = (char_u *)getenv("LINES")))
3896 rows = atoi((char *)p);
3897 if ((p = (char_u *)getenv("COLUMNS")))
3898 columns = atoi((char *)p);
3899 }
3900
3901#ifdef HAVE_TGETENT
3902 /*
3903 * 3. try reading "co" and "li" entries from termcap
3904 */
3905 if (columns == 0 || rows == 0)
3906 getlinecol(&columns, &rows);
3907#endif
3908
3909 /*
3910 * 4. If everything fails, use the old values
3911 */
3912 if (columns <= 0 || rows <= 0)
3913 return FAIL;
3914
3915 Rows = rows;
3916 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02003917 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 return OK;
3919}
3920
Bram Moolenaarb13501f2017-07-22 22:32:56 +02003921#if defined(FEAT_TERMINAL) || defined(PROTO)
3922/*
3923 * Report the windows size "rows" and "cols" to tty "fd".
3924 */
3925 int
3926mch_report_winsize(int fd, int rows, int cols)
3927{
3928# ifdef TIOCSWINSZ
3929 struct winsize ws;
3930
3931 ws.ws_col = cols;
3932 ws.ws_row = rows;
3933 ws.ws_xpixel = cols * 5;
3934 ws.ws_ypixel = rows * 10;
3935 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
3936 {
3937 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
3938 return OK;
3939 }
3940 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
3941# else
3942# ifdef TIOCSSIZE
3943 struct ttysize ts;
3944
3945 ts.ts_cols = cols;
3946 ts.ts_lines = rows;
3947 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
3948 {
3949 ch_log(NULL, "ioctl(TIOCSSIZE) success");
3950 return OK;
3951 }
3952 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
3953# endif
3954# endif
3955 return FAIL;
3956}
3957#endif
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959/*
3960 * Try to set the window size to Rows and Columns.
3961 */
3962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003963mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 if (*T_CWS)
3966 {
3967 /*
3968 * NOTE: if you get an error here that term_set_winsize() is
3969 * undefined, check the output of configure. It could probably not
3970 * find a ncurses, termcap or termlib library.
3971 */
3972 term_set_winsize((int)Rows, (int)Columns);
3973 out_flush();
3974 screen_start(); /* don't know where cursor is now */
3975 }
3976}
3977
3978#endif /* VMS */
3979
3980/*
3981 * Rows and/or Columns has changed.
3982 */
3983 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003984mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
3986 /* Nothing to do. */
3987}
3988
Bram Moolenaar205b8862011-09-07 15:04:31 +02003989/*
3990 * Wait for process "child" to end.
3991 * Return "child" if it exited properly, <= 0 on error.
3992 */
3993 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01003994wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02003995{
3996 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02003997 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02003998
3999 while (wait_pid != child)
4000 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004001 /* When compiled with Python threads are probably used, in which case
4002 * wait() sometimes hangs for no obvious reason. Use waitpid()
4003 * instead and loop (like the GUI). Also needed for other interfaces,
4004 * they might call system(). */
4005# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004006 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004007# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004008 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004009# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004010 if (wait_pid == 0)
4011 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004012 /* Wait for 1 to 10 msec before trying again. */
4013 mch_delay(delay_msec, TRUE);
4014 if (++delay_msec > 10)
4015 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004016 continue;
4017 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004018 if (wait_pid <= 0
4019# ifdef ECHILD
4020 && errno == ECHILD
4021# endif
4022 )
4023 break;
4024 }
4025 return wait_pid;
4026}
4027
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004028#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01004029/*
4030 * Parse "cmd" and put the white-separated parts in "argv".
4031 * "argv" is an allocated array with "argc" entries.
4032 * Returns FAIL when out of memory.
4033 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004034 int
4035mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
4036{
4037 int i;
4038 char_u *p;
4039 int inquote;
4040
4041 /*
4042 * Do this loop twice:
4043 * 1: find number of arguments
4044 * 2: separate them and build argv[]
4045 */
4046 for (i = 0; i < 2; ++i)
4047 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02004048 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004049 inquote = FALSE;
4050 *argc = 0;
4051 for (;;)
4052 {
4053 if (i == 1)
4054 (*argv)[*argc] = (char *)p;
4055 ++*argc;
4056 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4057 {
4058 if (*p == '"')
4059 inquote = !inquote;
4060 ++p;
4061 }
4062 if (*p == NUL)
4063 break;
4064 if (i == 1)
4065 *p++ = NUL;
4066 p = skipwhite(p);
4067 }
4068 if (*argv == NULL)
4069 {
4070 if (use_shcf)
4071 {
4072 /* Account for possible multiple args in p_shcf. */
4073 p = p_shcf;
4074 for (;;)
4075 {
4076 p = skiptowhite(p);
4077 if (*p == NUL)
4078 break;
4079 ++*argc;
4080 p = skipwhite(p);
4081 }
4082 }
4083
4084 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4085 if (*argv == NULL) /* out of memory */
4086 return FAIL;
4087 }
4088 }
4089 return OK;
4090}
4091#endif
4092
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004093#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004094 static void
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004095set_child_environment(long rows, long columns, char *term)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004096{
4097# ifdef HAVE_SETENV
4098 char envbuf[50];
4099# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004100 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004101 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004102 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004103 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004104 static char envbuf_Colors[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004105# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004106 long colors =
4107# ifdef FEAT_GUI
4108 gui.in_use ? 256*256*256 :
4109# endif
4110 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004111
4112 /* Simulate to have a dumb terminal (for now) */
4113# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004114 setenv("TERM", term, 1);
4115 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004116 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004117 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004118 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004119 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004120 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004121 sprintf((char *)envbuf, "%ld", colors);
4122 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004123# else
4124 /*
4125 * Putenv does not copy the string, it has to remain valid.
4126 * Use a static array to avoid losing allocated memory.
4127 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004128 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4129 putenv(envbuf_Term);
4130 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004131 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004132 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4133 putenv(envbuf_Lines);
4134 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4135 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004136 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004137 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4138 putenv(envbuf_Colors);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004139# endif
4140}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004141
4142 static void
4143set_default_child_environment(void)
4144{
4145 set_child_environment(Rows, Columns, "dumb");
4146}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004147#endif
4148
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004149#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
4150 static void
4151open_pty(int *pty_master_fd, int *pty_slave_fd)
4152{
4153 char *tty_name;
4154
4155 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4156 if (*pty_master_fd >= 0)
4157 {
4158 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4159 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4160 * adding O_NOCTTY always works when defined. */
4161#ifdef O_NOCTTY
4162 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4163#else
4164 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4165#endif
4166 if (*pty_slave_fd < 0)
4167 {
4168 close(*pty_master_fd);
4169 *pty_master_fd = -1;
4170 }
4171 }
4172}
4173#endif
4174
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004176mch_call_shell(
4177 char_u *cmd,
4178 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180#ifdef VMS
4181 char *ifn = NULL;
4182 char *ofn = NULL;
4183#endif
4184 int tmode = cur_tmode;
4185#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004186 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004187 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 out_flush();
4190
4191 if (options & SHELL_COOKED)
4192 settmode(TMODE_COOK); /* set to normal mode */
4193
Bram Moolenaar62b42182010-09-21 22:09:37 +02004194# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004195 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004196 loose_clipboard();
4197# endif
4198
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 if (cmd == NULL)
4200 x = system((char *)p_sh);
4201 else
4202 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004203# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (ofn = strchr((char *)cmd, '>'))
4205 *ofn++ = '\0';
4206 if (ifn = strchr((char *)cmd, '<'))
4207 {
4208 char *p;
4209
4210 *ifn++ = '\0';
4211 p = strchr(ifn,' '); /* chop off any trailing spaces */
4212 if (p)
4213 *p = '\0';
4214 }
4215 if (ofn)
4216 x = vms_sys((char *)cmd, ofn, ifn);
4217 else
4218 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004219# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 newcmd = lalloc(STRLEN(p_sh)
4221 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4222 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4223 if (newcmd == NULL)
4224 x = 0;
4225 else
4226 {
4227 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4228 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4229 (char *)p_shcf,
4230 (char *)cmd);
4231 x = system((char *)newcmd);
4232 vim_free(newcmd);
4233 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236# ifdef VMS
4237 x = vms_sys_status(x);
4238# endif
4239 if (emsg_silent)
4240 ;
4241 else if (x == 127)
4242 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 else if (x && !(options & SHELL_SILENT))
4244 {
4245 MSG_PUTS(_("\nshell returned "));
4246 msg_outnum((long)x);
4247 msg_putchar('\n');
4248 }
4249
4250 if (tmode == TMODE_RAW)
4251 settmode(TMODE_RAW); /* set to raw mode */
4252# ifdef FEAT_TITLE
4253 resettitle();
4254# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004255# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4256 restore_clipboard();
4257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return x;
4259
4260#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4261
Bram Moolenaardf177f62005-02-22 08:39:57 +00004262# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4263 127, some shells use that already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
Bram Moolenaar835dc632016-02-07 14:27:38 +01004265 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004267 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 pid_t wait_pid = 0;
4269# ifdef HAVE_UNION_WAIT
4270 union wait status;
4271# else
4272 int status = -1;
4273# endif
4274 int retval = -1;
4275 char **argv = NULL;
4276 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004277 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int i;
4279 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004281# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004283# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004284 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int fd_fromshell[2];
4286 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004287 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaar62b42182010-09-21 22:09:37 +02004289 newcmd = vim_strsave(p_sh);
4290 if (newcmd == NULL) /* out of memory */
4291 goto error;
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 out_flush();
4294 if (options & SHELL_COOKED)
4295 settmode(TMODE_COOK); /* set to normal mode */
4296
Bram Moolenaar835dc632016-02-07 14:27:38 +01004297 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4298 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (cmd != NULL)
4301 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004302 char_u *s;
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (extra_shell_arg != NULL)
4305 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004306
4307 /* Break 'shellcmdflag' into white separated parts. This doesn't
4308 * handle quoted strings, they are very unlikely to appear. */
4309 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4310 if (p_shcf_copy == NULL) /* out of memory */
4311 goto error;
4312 s = p_shcf_copy;
4313 p = p_shcf;
4314 while (*p != NUL)
4315 {
4316 argv[argc++] = (char *)s;
4317 while (*p && *p != ' ' && *p != TAB)
4318 *s++ = *p++;
4319 *s++ = NUL;
4320 p = skipwhite(p);
4321 }
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 argv[argc++] = (char *)cmd;
4324 }
4325 argv[argc] = NULL;
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004328 * For the GUI, when writing the output into the buffer and when reading
4329 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4330 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004332 if ((options & (SHELL_READ|SHELL_WRITE))
4333# ifdef FEAT_GUI
4334 || (gui.in_use && show_shell_mess)
4335# endif
4336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004338# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 /*
4340 * Try to open a master pty.
4341 * If this works, open the slave pty.
4342 * If the slave can't be opened, close the master pty.
4343 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004344 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004345 open_pty(&pty_master_fd, &pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /*
4347 * If not opening a pty or it didn't work, try using pipes.
4348 */
4349 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
4352 pipe_error = (pipe(fd_toshell) < 0);
4353 if (!pipe_error) /* pipe create OK */
4354 {
4355 pipe_error = (pipe(fd_fromshell) < 0);
4356 if (pipe_error) /* pipe create failed */
4357 {
4358 close(fd_toshell[0]);
4359 close(fd_toshell[1]);
4360 }
4361 }
4362 if (pipe_error)
4363 {
4364 MSG_PUTS(_("\nCannot create pipes\n"));
4365 out_flush();
4366 }
4367 }
4368 }
4369
4370 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004372 SIGSET_DECL(curset)
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374# ifdef __BEOS__
4375 beos_cleanup_read_thread();
4376# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004377
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004378 BLOCK_SIGNALS(&curset);
4379 pid = fork(); /* maybe we should use vfork() */
4380 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004382 UNBLOCK_SIGNALS(&curset);
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004385 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004387 || (gui.in_use && show_shell_mess)
4388# endif
4389 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004391# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 if (pty_master_fd >= 0) /* close the pseudo tty */
4393 {
4394 close(pty_master_fd);
4395 close(pty_slave_fd);
4396 }
4397 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 close(fd_toshell[0]);
4401 close(fd_toshell[1]);
4402 close(fd_fromshell[0]);
4403 close(fd_fromshell[1]);
4404 }
4405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407 else if (pid == 0) /* child */
4408 {
4409 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004410 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 if (!show_shell_mess || (options & SHELL_EXPAND))
4413 {
4414 int fd;
4415
4416 /*
4417 * Don't want to show any message from the shell. Can't just
4418 * close stdout and stderr though, because some systems will
4419 * break if you try to write to them after that, so we must
4420 * use dup() to replace them with something else -- webb
4421 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4422 * waiting for input.
4423 */
4424 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4425 fclose(stdin);
4426 fclose(stdout);
4427 fclose(stderr);
4428
4429 /*
4430 * If any of these open()'s and dup()'s fail, we just continue
4431 * anyway. It's not fatal, and on most systems it will make
4432 * no difference at all. On a few it will cause the execvp()
4433 * to exit with a non-zero status even when the completion
4434 * could be done, which is nothing too serious. If the open()
4435 * or dup() failed we'd just do the same thing ourselves
4436 * anyway -- webb
4437 */
4438 if (fd >= 0)
4439 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004440 ignored = dup(fd); /* To replace stdin (fd 0) */
4441 ignored = dup(fd); /* To replace stdout (fd 1) */
4442 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 /* Don't need this now that we've duplicated it */
4445 close(fd);
4446 }
4447 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004448 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004450 || gui.in_use
4451# endif
4452 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454
Bram Moolenaardf177f62005-02-22 08:39:57 +00004455# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004456 /* Create our own process group, so that the child and all its
4457 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004458 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004459 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004460 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004461 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004462# if defined(SIGHUP)
4463 /* When doing "!xterm&" and 'shell' is bash: the shell
4464 * will exit and send SIGHUP to all processes in its
4465 * group, killing the just started process. Ignore SIGHUP
4466 * to avoid that. (suggested by Simon Schubert)
4467 */
4468 signal(SIGHUP, SIG_IGN);
4469# endif
4470 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004471# endif
4472# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004473 if (pty_slave_fd >= 0)
4474 {
4475 /* push stream discipline modules */
4476 if (options & SHELL_COOKED)
4477 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004479 /* Try to become controlling tty (probably doesn't work,
4480 * unless run by root) */
4481 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004483 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004484# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004485 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
Bram Moolenaara5792f52005-11-23 21:25:05 +00004487 /*
4488 * stderr is only redirected when using the GUI, so that a
4489 * program like gpg can still access the terminal to get a
4490 * passphrase using stderr.
4491 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004492# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (pty_master_fd >= 0)
4494 {
4495 close(pty_master_fd); /* close master side of pty */
4496
4497 /* set up stdin/stdout/stderr for the child */
4498 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004499 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004501 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004502 if (gui.in_use)
4503 {
4504 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004505 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508 close(pty_slave_fd); /* has been dupped, close it now */
4509 }
4510 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
4513 /* set up stdin for the child */
4514 close(fd_toshell[1]);
4515 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004516 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 close(fd_toshell[0]);
4518
4519 /* set up stdout for the child */
4520 close(fd_fromshell[0]);
4521 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004522 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 close(fd_fromshell[1]);
4524
Bram Moolenaara5792f52005-11-23 21:25:05 +00004525# ifdef FEAT_GUI
4526 if (gui.in_use)
4527 {
4528 /* set up stderr for the child */
4529 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004530 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004531 }
4532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 /*
4537 * There is no type cast for the argv, because the type may be
4538 * different on different machines. This may cause a warning
4539 * message with strict compilers, don't worry about it.
4540 * Call _exit() instead of exit() to avoid closing the connection
4541 * to the X server (esp. with GTK, which uses atexit()).
4542 */
4543 execvp(argv[0], argv);
4544 _exit(EXEC_FAILED); /* exec failed, return failure code */
4545 }
4546 else /* parent */
4547 {
4548 /*
4549 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004550 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
4552 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004553 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004554 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004555# ifdef FEAT_JOB_CHANNEL
4556 ++dont_check_job_ended;
4557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 /*
4559 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004560 * This is also used to pipe stdin/stdout to/from the external
4561 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004563 if ((options & (SHELL_READ|SHELL_WRITE))
4564# ifdef FEAT_GUI
4565 || (gui.in_use && show_shell_mess)
4566# endif
4567 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004571# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4575 int ta_len = 0; /* valid bytes in ta_buf[] */
4576 int len;
4577 int p_more_save;
4578 int old_State;
4579 int c;
4580 int toshell_fd;
4581 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004582 garray_T ga;
4583 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004584# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4585 struct timeval start_tv;
4586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
Bram Moolenaardf177f62005-02-22 08:39:57 +00004588# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (pty_master_fd >= 0)
4590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 fromshell_fd = pty_master_fd;
4592 toshell_fd = dup(pty_master_fd);
4593 }
4594 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
4597 close(fd_toshell[0]);
4598 close(fd_fromshell[1]);
4599 toshell_fd = fd_toshell[1];
4600 fromshell_fd = fd_fromshell[0];
4601 }
4602
4603 /*
4604 * Write to the child if there are typed characters.
4605 * Read from the child if there are characters available.
4606 * Repeat the reading a few times if more characters are
4607 * available. Need to check for typed keys now and then, but
4608 * not too often (delays when no chars are available).
4609 * This loop is quit if no characters can be read from the pty
4610 * (WaitForChar detected special condition), or there are no
4611 * characters available and the child has exited.
4612 * Only check if the child has exited when there is no more
4613 * output. The child may exit before all the output has
4614 * been printed.
4615 *
4616 * Currently this busy loops!
4617 * This can probably dead-lock when the write blocks!
4618 */
4619 p_more_save = p_more;
4620 p_more = FALSE;
4621 old_State = State;
4622 State = EXTERNCMD; /* don't redraw at window resize */
4623
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004624 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004625 {
4626 /* Fork a process that will write the lines to the
4627 * external program. */
4628 if ((wpid = fork()) == -1)
4629 {
4630 MSG_PUTS(_("\nCannot fork\n"));
4631 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004632 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004633 {
4634 linenr_T lnum = curbuf->b_op_start.lnum;
4635 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004636 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004637 size_t l;
4638
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004639 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640 for (;;)
4641 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004642 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 if (l == 0)
4644 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004645 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004646 /* NL -> NUL translation */
4647 len = write(toshell_fd, "", (size_t)1);
4648 else
4649 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004650 char_u *s = vim_strchr(lp + written, NL);
4651
Bram Moolenaar89d40322006-08-29 15:30:07 +00004652 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004653 s == NULL ? l
4654 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004656 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 {
4658 /* Finished a line, add a NL, unless this line
4659 * should not have one. */
4660 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004661 || (!curbuf->b_p_bin
4662 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004663 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004664 && (lnum !=
4665 curbuf->b_ml.ml_line_count
4666 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004667 ignored = write(toshell_fd, "\n",
4668 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669 ++lnum;
4670 if (lnum > curbuf->b_op_end.lnum)
4671 {
4672 /* finished all the lines, close pipe */
4673 close(toshell_fd);
4674 toshell_fd = -1;
4675 break;
4676 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004677 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004678 written = 0;
4679 }
4680 else if (len > 0)
4681 written += len;
4682 }
4683 _exit(0);
4684 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004685 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004686 {
4687 close(toshell_fd);
4688 toshell_fd = -1;
4689 }
4690 }
4691
4692 if (options & SHELL_READ)
4693 ga_init2(&ga, 1, BUFLEN);
4694
4695 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004696# ifdef ELAPSED_FUNC
4697 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 for (;;)
4700 {
4701 /*
4702 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004703 * if there are any.
4704 * Don't do this if we are expanding wild cards (would eat
4705 * typeahead).
4706 * Don't do this when filtering and terminal is in cooked
4707 * mode, the shell command will handle the I/O. Avoids
4708 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004709 * Don't get characters when the child has already
4710 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004711 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004712 * while (noread_cnt > 4), avoids that ":r !ls" eats
4713 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 */
4715 len = 0;
4716 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004717 && ((options &
4718 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4719 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004720# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004721 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004722# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004723 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004724 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004725 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004727 if (ta_len == 0)
4728 {
4729 /* Get extra characters when we don't have any.
4730 * Reset the counter and timer. */
4731 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004732# ifdef ELAPSED_FUNC
4733 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004734# endif
4735 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4736 }
4737 if (ta_len > 0 || len > 0)
4738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /*
4740 * For pipes:
4741 * Check for CTRL-C: send interrupt signal to child.
4742 * Check for CTRL-D: EOF, close pipe to child.
4743 */
4744 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4745 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004746# ifdef SIGINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /*
4748 * Send SIGINT to the child's group or all
4749 * processes in our group.
4750 */
4751 if (ta_buf[ta_len] == Ctrl_C
4752 || ta_buf[ta_len] == intr_char)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004753 {
4754# ifdef HAVE_SETSID
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 kill(-pid, SIGINT);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004756# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 kill(0, SIGINT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758# endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00004759 if (wpid > 0)
4760 kill(wpid, SIGINT);
4761 }
4762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (pty_master_fd < 0 && toshell_fd >= 0
4764 && ta_buf[ta_len] == Ctrl_D)
4765 {
4766 close(toshell_fd);
4767 toshell_fd = -1;
4768 }
4769 }
4770
4771 /* replace K_BS by <BS> and K_DEL by <DEL> */
4772 for (i = ta_len; i < ta_len + len; ++i)
4773 {
4774 if (ta_buf[i] == CSI && len - i > 2)
4775 {
4776 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4777 if (c == K_DEL || c == K_KDEL || c == K_BS)
4778 {
4779 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4780 (size_t)(len - i - 2));
4781 if (c == K_DEL || c == K_KDEL)
4782 ta_buf[i] = DEL;
4783 else
4784 ta_buf[i] = Ctrl_H;
4785 len -= 2;
4786 }
4787 }
4788 else if (ta_buf[i] == '\r')
4789 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004790# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004792 i += (*mb_ptr2len_len)(ta_buf + i,
4793 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796
4797 /*
4798 * For pipes: echo the typed characters.
4799 * For a pty this does not seem to work.
4800 */
4801 if (pty_master_fd < 0)
4802 {
4803 for (i = ta_len; i < ta_len + len; ++i)
4804 {
4805 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4806 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else if (has_mbyte)
4809 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004810 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 msg_outtrans_len(ta_buf + i, l);
4813 i += l - 1;
4814 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 else
4817 msg_outtrans_len(ta_buf + i, 1);
4818 }
4819 windgoto(msg_row, msg_col);
4820 out_flush();
4821 }
4822
4823 ta_len += len;
4824
4825 /*
4826 * Write the characters to the child, unless EOF has
4827 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004828 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004829 * When writing buffer lines, drop the typed
4830 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832 if (options & SHELL_WRITE)
4833 ta_len = 0;
4834 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
4837 if (len > 0)
4838 {
4839 ta_len -= len;
4840 mch_memmove(ta_buf, ta_buf + len, ta_len);
4841 }
4842 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845
Bram Moolenaardf177f62005-02-22 08:39:57 +00004846 if (got_int)
4847 {
4848 /* CTRL-C sends a signal to the child, we ignore it
4849 * ourselves */
4850# ifdef HAVE_SETSID
4851 kill(-pid, SIGINT);
4852# else
4853 kill(0, SIGINT);
4854# endif
4855 if (wpid > 0)
4856 kill(wpid, SIGINT);
4857 got_int = FALSE;
4858 }
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 /*
4861 * Check if the child has any characters to be printed.
4862 * Read them and write them to our window. Repeat this as
4863 * long as there is something to do, avoid the 10ms wait
4864 * for mch_inchar(), or sending typeahead characters to
4865 * the external process.
4866 * TODO: This should handle escape sequences, compatible
4867 * to some terminal (vt52?).
4868 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004869 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01004870 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004872 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004875# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 );
4879 if (len <= 0) /* end of file or error */
4880 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881
4882 noread_cnt = 0;
4883 if (options & SHELL_READ)
4884 {
4885 /* Do NUL -> NL translation, append NL separated
4886 * lines to the current buffer. */
4887 for (i = 0; i < len; ++i)
4888 {
4889 if (buffer[i] == NL)
4890 append_ga_line(&ga);
4891 else if (buffer[i] == NUL)
4892 ga_append(&ga, NL);
4893 else
4894 ga_append(&ga, buffer[i]);
4895 }
4896 }
4897# ifdef FEAT_MBYTE
4898 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
4900 int l;
4901
Bram Moolenaardf177f62005-02-22 08:39:57 +00004902 len += buffer_off;
4903 buffer[len] = NUL;
4904
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 /* Check if the last character in buffer[] is
4906 * incomplete, keep these bytes for the next
4907 * round. */
4908 for (p = buffer; p < buffer + len; p += l)
4909 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02004910 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (l == 0)
4912 l = 1; /* NUL byte? */
4913 else if (MB_BYTE2LEN(*p) != l)
4914 break;
4915 }
4916 if (p == buffer) /* no complete character */
4917 {
4918 /* avoid getting stuck at an illegal byte */
4919 if (len >= 12)
4920 ++p;
4921 else
4922 {
4923 buffer_off = len;
4924 continue;
4925 }
4926 }
4927 c = *p;
4928 *p = NUL;
4929 msg_puts(buffer);
4930 if (p < buffer + len)
4931 {
4932 *p = c;
4933 buffer_off = (buffer + len) - p;
4934 mch_memmove(buffer, p, buffer_off);
4935 continue;
4936 }
4937 buffer_off = 0;
4938 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004939# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 buffer[len] = NUL;
4943 msg_puts(buffer);
4944 }
4945
4946 windgoto(msg_row, msg_col);
4947 cursor_on();
4948 out_flush();
4949 if (got_int)
4950 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004951
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004952# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004953 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004954 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004955 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004956
4957 /* Avoid that we keep looping here without
4958 * checking for a CTRL-C for a long time. Don't
4959 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004960 if (msec > 2000)
4961 {
4962 noread_cnt = 5;
4963 break;
4964 }
4965 }
4966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004969 /* If we already detected the child has finished, continue
4970 * reading output for a short while. Some text may be
4971 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004972 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004973 {
4974 if (noread_cnt < 5)
4975 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004976 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02004977 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 /*
4980 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004981 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004984 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004985# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
4989 || (wait_pid == pid && WIFEXITED(status)))
4990 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004991 /* Don't break the loop yet, try reading more
4992 * characters from "fromshell_fd" first. When using
4993 * pipes there might still be something to read and
4994 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004997 else
4998 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01004999
Bram Moolenaar95a51352013-03-21 22:53:50 +01005000# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005001 /* Handle any X events, e.g. serving the clipboard. */
5002 clip_update();
5003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005finished:
5006 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005007 if (options & SHELL_READ)
5008 {
5009 if (ga.ga_len > 0)
5010 {
5011 append_ga_line(&ga);
5012 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005013 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014 }
5015 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005016 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005017 ga_clear(&ga);
5018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 /*
5021 * Give all typeahead that wasn't used back to ui_inchar().
5022 */
5023 if (ta_len)
5024 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 State = old_State;
5026 if (toshell_fd >= 0)
5027 close(toshell_fd);
5028 close(fromshell_fd);
5029 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005030# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005031 else
5032 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005033 long delay_msec = 1;
5034
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005035 /*
5036 * Similar to the loop above, but only handle X events, no
5037 * I/O.
5038 */
5039 for (;;)
5040 {
5041 if (got_int)
5042 {
5043 /* CTRL-C sends a signal to the child, we ignore it
5044 * ourselves */
5045# ifdef HAVE_SETSID
5046 kill(-pid, SIGINT);
5047# else
5048 kill(0, SIGINT);
5049# endif
5050 got_int = FALSE;
5051 }
5052# ifdef __NeXT__
5053 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5054# else
5055 wait_pid = waitpid(pid, &status, WNOHANG);
5056# endif
5057 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5058 || (wait_pid == pid && WIFEXITED(status)))
5059 {
5060 wait_pid = pid;
5061 break;
5062 }
5063
5064 /* Handle any X events, e.g. serving the clipboard. */
5065 clip_update();
5066
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005067 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5068 * less time. */
5069 mch_delay(delay_msec, TRUE);
5070 if (++delay_msec > 10)
5071 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005072 }
5073 }
5074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
5076 /*
5077 * Wait until our child has exited.
5078 * Ignore wait() returning pids of other children and returning
5079 * because of some signal like SIGWINCH.
5080 * Don't wait if wait_pid was already set above, indicating the
5081 * child already exited.
5082 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005083 if (wait_pid != pid)
5084 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaar624891f2010-10-13 16:22:09 +02005086# ifdef FEAT_GUI
5087 /* Close slave side of pty. Only do this after the child has
5088 * exited, otherwise the child may hang when it tries to write on
5089 * the pty. */
5090 if (pty_master_fd >= 0)
5091 close(pty_slave_fd);
5092# endif
5093
Bram Moolenaardf177f62005-02-22 08:39:57 +00005094 /* Make sure the child that writes to the external program is
5095 * dead. */
5096 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005097 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005098 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005099 wait4pid(wpid, NULL);
5100 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005102# ifdef FEAT_JOB_CHANNEL
5103 --dont_check_job_ended;
5104# endif
5105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 /*
5107 * Set to raw mode right now, otherwise a CTRL-C after
5108 * catch_signals() will kill Vim.
5109 */
5110 if (tmode == TMODE_RAW)
5111 settmode(TMODE_RAW);
5112 did_settmode = TRUE;
5113 set_signals();
5114
5115 if (WIFEXITED(status))
5116 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005117 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005119 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 if (retval == EXEC_FAILED)
5122 {
5123 MSG_PUTS(_("\nCannot execute shell "));
5124 msg_outtrans(p_sh);
5125 msg_putchar('\n');
5126 }
5127 else if (!(options & SHELL_SILENT))
5128 {
5129 MSG_PUTS(_("\nshell returned "));
5130 msg_outnum((long)retval);
5131 msg_putchar('\n');
5132 }
5133 }
5134 }
5135 else
5136 MSG_PUTS(_("\nCommand terminated\n"));
5137 }
5138 }
5139 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005140 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142error:
5143 if (!did_settmode)
5144 if (tmode == TMODE_RAW)
5145 settmode(TMODE_RAW); /* set to raw mode */
5146# ifdef FEAT_TITLE
5147 resettitle();
5148# endif
5149 vim_free(newcmd);
5150
5151 return retval;
5152
5153#endif /* USE_SYSTEM */
5154}
5155
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005156#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005157 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005158mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005159{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005160 pid_t pid;
5161 int fd_in[2]; /* for stdin */
5162 int fd_out[2]; /* for stdout */
5163 int fd_err[2]; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005164 int pty_master_fd = -1;
5165 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005166 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005167 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5168 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5169 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005170 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005171 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5172 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005173 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005174 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005175
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005176 if (use_out_for_err && use_null_for_out)
5177 use_null_for_err = TRUE;
5178
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005179 /* default is to fail */
5180 job->jv_status = JOB_FAILED;
5181 fd_in[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005182 fd_in[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005183 fd_out[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005184 fd_out[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005185 fd_err[0] = -1;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005186 fd_err[1] = -1;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005187
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005188 if (options->jo_pty)
5189 open_pty(&pty_master_fd, &pty_slave_fd);
5190
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005191 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005192 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005193 if (use_file_for_in)
5194 {
5195 char_u *fname = options->jo_io_name[PART_IN];
5196
5197 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5198 if (fd_in[0] < 0)
5199 {
5200 EMSG2(_(e_notopen), fname);
5201 goto failed;
5202 }
5203 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005204 else if (!use_null_for_in && pty_master_fd < 0 && pipe(fd_in) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005205 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005206
5207 if (use_file_for_out)
5208 {
5209 char_u *fname = options->jo_io_name[PART_OUT];
5210
5211 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5212 if (fd_out[1] < 0)
5213 {
5214 EMSG2(_(e_notopen), fname);
5215 goto failed;
5216 }
5217 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005218 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005219 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005220
5221 if (use_file_for_err)
5222 {
5223 char_u *fname = options->jo_io_name[PART_ERR];
5224
5225 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5226 if (fd_err[1] < 0)
5227 {
5228 EMSG2(_(e_notopen), fname);
5229 goto failed;
5230 }
5231 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005232 else if (!use_out_for_err && !use_null_for_err
5233 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005234 goto failed;
5235
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005236 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5237 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005238 if (options->jo_set & JO_CHANNEL)
5239 {
5240 channel = options->jo_channel;
5241 if (channel != NULL)
5242 ++channel->ch_refcount;
5243 }
5244 else
5245 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005246 if (channel == NULL)
5247 goto failed;
5248 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005249
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005250 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005251 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005252 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005253 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005254 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005255 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005256 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005257 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005258 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005259 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005260 int null_fd = -1;
5261 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005262
Bram Moolenaar835dc632016-02-07 14:27:38 +01005263 /* child */
5264 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005265 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005266
5267# ifdef HAVE_SETSID
5268 /* Create our own process group, so that the child and all its
5269 * children can be kill()ed. Don't do this when using pipes,
5270 * because stdin is not a tty, we would lose /dev/tty. */
5271 (void)setsid();
5272# endif
5273
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005274# ifdef FEAT_TERMINAL
5275 if (options->jo_term_rows > 0)
5276 set_child_environment(
5277 (long)options->jo_term_rows,
5278 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005279 STRNCMP(T_NAME, "xterm", 5) == 0
5280 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005281 else
5282# endif
5283 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005284
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005285 if (use_null_for_in || use_null_for_out || use_null_for_err)
5286 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
5287
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005288 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005289 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005290 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005291 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005292 else if (fd_in[0] < 0)
5293 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005294 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005295 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005296
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005297 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005298 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005299 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005300 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005301 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005302 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005303 }
5304 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005305 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005306 else if (fd_err[1] < 0)
5307 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005308 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005309 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005310
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005311 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005312 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005313 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005314 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005315 else if (fd_out[1] < 0)
5316 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005317 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005318 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005319
5320 if (fd_in[0] >= 0)
5321 close(fd_in[0]);
5322 if (fd_in[1] >= 0)
5323 close(fd_in[1]);
5324 if (fd_out[0] >= 0)
5325 close(fd_out[0]);
5326 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005327 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005328 if (fd_err[0] >= 0)
5329 close(fd_err[0]);
5330 if (fd_err[1] >= 0)
5331 close(fd_err[1]);
5332 if (pty_master_fd >= 0)
5333 {
5334 close(pty_master_fd); /* not used */
5335 close(pty_slave_fd); /* duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005336 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005337
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005338 if (null_fd >= 0)
5339 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005340
Bram Moolenaar835dc632016-02-07 14:27:38 +01005341 /* See above for type of argv. */
5342 execvp(argv[0], argv);
5343
Bram Moolenaar4694a172016-04-21 14:05:23 +02005344 if (stderr_works)
5345 perror("executing job failed");
Bram Moolenaarcda77642016-06-04 13:32:35 +02005346#ifdef EXITFREE
5347 /* calling free_all_mem() here causes problems. Ignore valgrind
5348 * reporting possibly leaked memory. */
5349#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005350 _exit(EXEC_FAILED); /* exec failed, return failure code */
5351 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005352
5353 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005354 UNBLOCK_SIGNALS(&curset);
5355
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005356 job->jv_pid = pid;
5357 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005358 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005359
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005360 if (pty_master_fd >= 0)
5361 close(pty_slave_fd); /* duped above */
5362 /* close child stdin, stdout and stderr */
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005363 if (!use_file_for_in && fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005364 close(fd_in[0]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005365 if (!use_file_for_out && fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005366 close(fd_out[1]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005367 if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005368 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005369 if (channel != NULL)
5370 {
5371 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005372 use_file_for_in || use_null_for_in
5373 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5374 use_file_for_out || use_null_for_out
5375 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5376 use_out_for_err || use_file_for_err || use_null_for_err
5377 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005378 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005379 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005380
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005381 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005382 return;
5383
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005384failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005385 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005386 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005387 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005388 if (fd_in[1] >= 0)
5389 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005390 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005391 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005392 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005393 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005394 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005395 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005396 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005397 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005398 if (pty_master_fd >= 0)
5399 close(pty_master_fd);
5400 if (pty_slave_fd >= 0)
5401 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005402}
5403
5404 char *
5405mch_job_status(job_T *job)
5406{
5407# ifdef HAVE_UNION_WAIT
5408 union wait status;
5409# else
5410 int status = -1;
5411# endif
5412 pid_t wait_pid = 0;
5413
5414# ifdef __NeXT__
5415 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5416# else
5417 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5418# endif
5419 if (wait_pid == -1)
5420 {
5421 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005422 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005423 }
5424 if (wait_pid == 0)
5425 return "run";
5426 if (WIFEXITED(status))
5427 {
5428 /* LINTED avoid "bitwise operation on signed value" */
5429 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005430 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005431 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005432 if (WIFSIGNALED(status))
5433 {
5434 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005435 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005436 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005437 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005438
5439return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005440 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005441 {
5442 ch_log(job->jv_channel, "Job ended");
5443 job->jv_status = JOB_ENDED;
5444 }
5445 return "dead";
5446}
5447
5448 job_T *
5449mch_detect_ended_job(job_T *job_list)
5450{
5451# ifdef HAVE_UNION_WAIT
5452 union wait status;
5453# else
5454 int status = -1;
5455# endif
5456 pid_t wait_pid = 0;
5457 job_T *job;
5458
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005459# ifndef USE_SYSTEM
5460 /* Do not do this when waiting for a shell command to finish, we would get
5461 * the exit value here (and discard it), the exit value obtained there
5462 * would then be wrong. */
5463 if (dont_check_job_ended > 0)
5464 return NULL;
5465# endif
5466
Bram Moolenaar97792de2016-10-15 18:36:49 +02005467# ifdef __NeXT__
5468 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5469# else
5470 wait_pid = waitpid(-1, &status, WNOHANG);
5471# endif
5472 if (wait_pid <= 0)
5473 /* no process ended */
5474 return NULL;
5475 for (job = job_list; job != NULL; job = job->jv_next)
5476 {
5477 if (job->jv_pid == wait_pid)
5478 {
5479 if (WIFEXITED(status))
5480 /* LINTED avoid "bitwise operation on signed value" */
5481 job->jv_exitval = WEXITSTATUS(status);
5482 else if (WIFSIGNALED(status))
5483 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005484 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005485 {
5486 ch_log(job->jv_channel, "Job ended");
5487 job->jv_status = JOB_ENDED;
5488 }
5489 return job;
5490 }
5491 }
5492 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493}
5494
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005495/*
5496 * Send a (deadly) signal to "job".
5497 * Return FAIL if "how" is not a valid name.
5498 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005499 int
5500mch_stop_job(job_T *job, char_u *how)
5501{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005502 int sig = -1;
5503 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005504
Bram Moolenaar923d9262016-02-25 20:56:01 +01005505 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005507 else if (STRCMP(how, "hup") == 0)
5508 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005509 else if (STRCMP(how, "quit") == 0)
5510 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005511 else if (STRCMP(how, "int") == 0)
5512 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005513 else if (STRCMP(how, "kill") == 0)
5514 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005515#ifdef SIGWINCH
5516 else if (STRCMP(how, "winch") == 0)
5517 sig = SIGWINCH;
5518#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005519 else if (isdigit(*how))
5520 sig = atoi((char *)how);
5521 else
5522 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005523
Bram Moolenaar72801402016-02-09 11:37:50 +01005524 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005525 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005526#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005527 if (job_pid == getpgid(job_pid))
5528 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005529#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005530
Bram Moolenaar61a66052017-07-22 18:39:00 +02005531 /* Never kill ourselves! */
5532 if (job_pid != 0)
5533 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005534
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 return OK;
5536}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005537
5538/*
5539 * Clear the data related to "job".
5540 */
5541 void
5542mch_clear_job(job_T *job)
5543{
5544 /* call waitpid because child process may become zombie */
5545# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005546 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005547# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005548 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005549# endif
5550}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005551#endif
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553/*
5554 * Check for CTRL-C typed by reading all available characters.
5555 * In cooked mode we should get SIGINT, no need to check.
5556 */
5557 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005558mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005560 if ((curr_tmode == TMODE_RAW || force)
5561 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 fill_input_buf(FALSE);
5563}
5564
5565/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005566 * Wait "msec" msec until a character is available from the mouse, keyboard,
5567 * from inbuf[].
5568 * "msec" == -1 will block forever.
5569 * Invokes timer callbacks when needed.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005570 * "interrupted" (if not NULL) is set to TRUE when no character is available
5571 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005572 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005573 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
5575 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02005576WaitForChar(long msec, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005578#ifdef FEAT_TIMERS
5579 long due_time;
5580 long remaining = msec;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005581 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005582
5583 /* When waiting very briefly don't trigger timers. */
5584 if (msec >= 0 && msec < 10L)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005585 return WaitForCharOrMouse(msec, NULL);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005586
5587 while (msec < 0 || remaining > 0)
5588 {
5589 /* Trigger timers and then get the time in msec until the next one is
5590 * due. Wait up to that time. */
5591 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005592 if (typebuf.tb_change_cnt != tb_change_cnt)
5593 {
5594 /* timer may have used feedkeys() */
5595 return FALSE;
5596 }
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005597 if (due_time <= 0 || (msec > 0 && due_time > remaining))
5598 due_time = remaining;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005599 if (WaitForCharOrMouse(due_time, interrupted))
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005600 return TRUE;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005601 if (interrupted != NULL && *interrupted)
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005602 /* Nothing available, but need to return so that side effects get
5603 * handled, such as handling a message on a channel. */
5604 return FALSE;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005605 if (msec > 0)
5606 remaining -= due_time;
5607 }
5608 return FALSE;
5609#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005610 return WaitForCharOrMouse(msec, interrupted);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005611#endif
5612}
5613
5614/*
5615 * Wait "msec" msec until a character is available from the mouse or keyboard
5616 * or from inbuf[].
5617 * "msec" == -1 will block forever.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005618 * "interrupted" (if not NULL) is set to TRUE when no character is available
5619 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005620 * When a GUI is being used, this will never get called -- webb
5621 */
5622 static int
Bram Moolenaarcda77642016-06-04 13:32:35 +02005623WaitForCharOrMouse(long msec, int *interrupted)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005624{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625#ifdef FEAT_MOUSE_GPM
5626 int gpm_process_wanted;
5627#endif
5628#ifdef FEAT_XCLIPBOARD
5629 int rest;
5630#endif
5631 int avail;
5632
5633 if (input_available()) /* something in inbuf[] */
5634 return 1;
5635
5636#if defined(FEAT_MOUSE_DEC)
5637 /* May need to query the mouse position. */
5638 if (WantQueryMouse)
5639 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005640 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5642 }
5643#endif
5644
5645 /*
5646 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5647 * events. This is a bit complicated, because they might both be defined.
5648 */
5649#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5650# ifdef FEAT_XCLIPBOARD
5651 rest = 0;
5652 if (do_xterm_trace())
5653 rest = msec;
5654# endif
5655 do
5656 {
5657# ifdef FEAT_XCLIPBOARD
5658 if (rest != 0)
5659 {
5660 msec = XT_TRACE_DELAY;
5661 if (rest >= 0 && rest < XT_TRACE_DELAY)
5662 msec = rest;
5663 if (rest >= 0)
5664 rest -= msec;
5665 }
5666# endif
5667# ifdef FEAT_MOUSE_GPM
5668 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005669 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005670 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005672 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673# endif
5674 if (!avail)
5675 {
5676 if (input_available())
5677 return 1;
5678# ifdef FEAT_XCLIPBOARD
5679 if (rest == 0 || !do_xterm_trace())
5680# endif
5681 break;
5682 }
5683 }
5684 while (FALSE
5685# ifdef FEAT_MOUSE_GPM
5686 || (gpm_process_wanted && mch_gpm_process() == 0)
5687# endif
5688# ifdef FEAT_XCLIPBOARD
5689 || (!avail && rest != 0)
5690# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005691 )
5692 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005695 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696#endif
5697 return avail;
5698}
5699
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005700#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701/*
5702 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005703 * "msec" == 0 will check for characters once.
5704 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005707 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005708 * "interrupted" (if not NULL) is set to TRUE when no character is available
5709 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711#if defined(__BEOS__)
5712 int
5713#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005714 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005716RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005719 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005720#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 static int busy = FALSE;
5722
5723 /* May retry getting characters after an event was handled. */
5724# define MAY_LOOP
5725
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005726# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 /* Remember at what time we started, so that we know how much longer we
5728 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005729 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005730 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005732 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005733 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734# endif
5735
5736 /* Handle being called recursively. This may happen for the session
5737 * manager stuff, it may save the file, which does a breakcheck. */
5738 if (busy)
5739 return 0;
5740#endif
5741
5742#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005743 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744#endif
5745 {
5746#ifdef MAY_LOOP
5747 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005748# ifdef FEAT_MZSCHEME
5749 int mzquantum_used = FALSE;
5750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#endif
5752#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005753 /* each channel may use in, out and err */
5754 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 int nfd;
5756# ifdef FEAT_XCLIPBOARD
5757 int xterm_idx = -1;
5758# endif
5759# ifdef FEAT_MOUSE_GPM
5760 int gpm_idx = -1;
5761# endif
5762# ifdef USE_XSMP
5763 int xsmp_idx = -1;
5764# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005765 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005767# ifdef FEAT_MZSCHEME
5768 mzvim_check_threads();
5769 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5770 {
5771 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
5772 mzquantum_used = TRUE;
5773 }
5774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 fds[0].fd = fd;
5776 fds[0].events = POLLIN;
5777 nfd = 1;
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005780 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (xterm_Shell != (Widget)0)
5782 {
5783 xterm_idx = nfd;
5784 fds[nfd].fd = ConnectionNumber(xterm_dpy);
5785 fds[nfd].events = POLLIN;
5786 nfd++;
5787 }
5788# endif
5789# ifdef FEAT_MOUSE_GPM
5790 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5791 {
5792 gpm_idx = nfd;
5793 fds[nfd].fd = gpm_fd;
5794 fds[nfd].events = POLLIN;
5795 nfd++;
5796 }
5797# endif
5798# ifdef USE_XSMP
5799 if (xsmp_icefd != -1)
5800 {
5801 xsmp_idx = nfd;
5802 fds[nfd].fd = xsmp_icefd;
5803 fds[nfd].events = POLLIN;
5804 nfd++;
5805 }
5806# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005807#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005808 nfd = channel_poll_setup(nfd, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005809#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005810 if (interrupted != NULL)
5811 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005813 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005814
5815 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02005816 if (result == 0 && interrupted != NULL && ret > 0)
5817 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005818
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005819# ifdef FEAT_MZSCHEME
5820 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005821 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005822 finished = FALSE;
5823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825# ifdef FEAT_XCLIPBOARD
5826 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
5827 {
5828 xterm_update(); /* Maybe we should hand out clipboard */
5829 if (--ret == 0 && !input_available())
5830 /* Try again */
5831 finished = FALSE;
5832 }
5833# endif
5834# ifdef FEAT_MOUSE_GPM
5835 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
5836 {
5837 *check_for_gpm = 1;
5838 }
5839# endif
5840# ifdef USE_XSMP
5841 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
5842 {
5843 if (fds[xsmp_idx].revents & POLLIN)
5844 {
5845 busy = TRUE;
5846 xsmp_handle_requests();
5847 busy = FALSE;
5848 }
5849 else if (fds[xsmp_idx].revents & POLLHUP)
5850 {
5851 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00005852 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 xsmp_close();
5854 }
5855 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005856 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005859#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01005860 if (ret > 0)
5861 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02005862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#else /* HAVE_SELECT */
5865
5866 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005867 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005868 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005870 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005872# ifdef FEAT_MZSCHEME
5873 mzvim_check_threads();
5874 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
5875 {
5876 towait = p_mzq; /* don't wait longer than 'mzquantum' */
5877 mzquantum_used = TRUE;
5878 }
5879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005881 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005883 tv.tv_sec = towait / 1000;
5884 tv.tv_usec = (towait % 1000) * (1000000/1000);
5885 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005887 else
5888 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 /*
5891 * Select on ready for reading and exceptional condition (end of file).
5892 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005893select_eintr:
5894 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005895 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 FD_ZERO(&efds);
5897 FD_SET(fd, &rfds);
5898# if !defined(__QNX__) && !defined(__CYGWIN32__)
5899 /* For QNX select() always returns 1 if this is set. Why? */
5900 FD_SET(fd, &efds);
5901# endif
5902 maxfd = fd;
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01005905 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (xterm_Shell != (Widget)0)
5907 {
5908 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
5909 if (maxfd < ConnectionNumber(xterm_dpy))
5910 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005911
5912 /* An event may have already been read but not handled. In
5913 * particulary, XFlush may cause this. */
5914 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
5916# endif
5917# ifdef FEAT_MOUSE_GPM
5918 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
5919 {
5920 FD_SET(gpm_fd, &rfds);
5921 FD_SET(gpm_fd, &efds);
5922 if (maxfd < gpm_fd)
5923 maxfd = gpm_fd;
5924 }
5925# endif
5926# ifdef USE_XSMP
5927 if (xsmp_icefd != -1)
5928 {
5929 FD_SET(xsmp_icefd, &rfds);
5930 FD_SET(xsmp_icefd, &efds);
5931 if (maxfd < xsmp_icefd)
5932 maxfd = xsmp_icefd;
5933 }
5934# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005935# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005936 maxfd = channel_select_setup(maxfd, &rfds, &wfds);
Bram Moolenaardd82d692012-08-15 17:26:57 +02005937# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005938 if (interrupted != NULL)
5939 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005941 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005942 result = ret > 0 && FD_ISSET(fd, &rfds);
5943 if (result)
5944 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02005945 else if (interrupted != NULL && ret > 0)
5946 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005947
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005948# ifdef EINTR
5949 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005950 {
5951 /* Check whether window has been resized, EINTR may be caused by
5952 * SIGWINCH. */
5953 if (do_resize)
5954 handle_resize();
5955
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005956 /* Interrupted by a signal, need to try again. We ignore msec
5957 * here, because we do want to check even after a timeout if
5958 * characters are available. Needed for reading output of an
5959 * external command after the process has finished. */
5960 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02005961 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005962# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00005963# ifdef __TANDEM
5964 if (ret == -1 && errno == ENOTSUP)
5965 {
5966 FD_ZERO(&rfds);
5967 FD_ZERO(&efds);
5968 ret = 0;
5969 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005970# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005971# ifdef FEAT_MZSCHEME
5972 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00005973 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005974 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975# endif
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977# ifdef FEAT_XCLIPBOARD
5978 if (ret > 0 && xterm_Shell != (Widget)0
5979 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
5980 {
5981 xterm_update(); /* Maybe we should hand out clipboard */
5982 /* continue looping when we only got the X event and the input
5983 * buffer is empty */
5984 if (--ret == 0 && !input_available())
5985 {
5986 /* Try again */
5987 finished = FALSE;
5988 }
5989 }
5990# endif
5991# ifdef FEAT_MOUSE_GPM
5992 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
5993 {
5994 if (FD_ISSET(gpm_fd, &efds))
5995 gpm_close();
5996 else if (FD_ISSET(gpm_fd, &rfds))
5997 *check_for_gpm = 1;
5998 }
5999# endif
6000# ifdef USE_XSMP
6001 if (ret > 0 && xsmp_icefd != -1)
6002 {
6003 if (FD_ISSET(xsmp_icefd, &efds))
6004 {
6005 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006006 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 xsmp_close();
6008 if (--ret == 0)
6009 finished = FALSE; /* keep going if event was only one */
6010 }
6011 else if (FD_ISSET(xsmp_icefd, &rfds))
6012 {
6013 busy = TRUE;
6014 xsmp_handle_requests();
6015 busy = FALSE;
6016 if (--ret == 0)
6017 finished = FALSE; /* keep going if event was only one */
6018 }
6019 }
6020# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006021#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +01006022 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006023 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025
6026#endif /* HAVE_SELECT */
6027
6028#ifdef MAY_LOOP
6029 if (finished || msec == 0)
6030 break;
6031
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006032# ifdef FEAT_CLIENTSERVER
6033 if (server_waiting())
6034 break;
6035# endif
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 /* We're going to loop around again, find out for how long */
6038 if (msec > 0)
6039 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006040# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006042 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043# else
6044 /* Guess we got interrupted halfway. */
6045 msec = msec / 2;
6046# endif
6047 if (msec <= 0)
6048 break; /* waited long enough */
6049 }
6050#endif
6051 }
6052
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006053 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054}
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006058 * Expand a path into all matching files and/or directories. Handles "*",
6059 * "?", "[a-z]", "**", etc.
6060 * "path" has backslashes before chars that are not to be expanded.
6061 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 */
6063 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006064mch_expandpath(
6065 garray_T *gap,
6066 char_u *path,
6067 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
Bram Moolenaar02743632005-07-25 20:42:36 +00006069 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070}
6071#endif
6072
6073/*
6074 * mch_expand_wildcards() - this code does wild-card pattern matching using
6075 * the shell
6076 *
6077 * return OK for success, FAIL for error (you may lose some memory) and put
6078 * an error message in *file.
6079 *
6080 * num_pat is number of input patterns
6081 * pat is array of pointers to input patterns
6082 * num_file is pointer to number of matched file names
6083 * file is pointer to array of pointers to matched file names
6084 */
6085
6086#ifndef SEEK_SET
6087# define SEEK_SET 0
6088#endif
6089#ifndef SEEK_END
6090# define SEEK_END 2
6091#endif
6092
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006093#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006096mch_expand_wildcards(
6097 int num_pat,
6098 char_u **pat,
6099 int *num_file,
6100 char_u ***file,
6101 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 int i;
6104 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006105 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 char_u *p;
6107 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
Bram Moolenaarc7247912008-01-13 12:54:11 +00006109 /*
6110 * This is the non-OS/2 implementation (really Unix).
6111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int j;
6113 char_u *tempname;
6114 char_u *command;
6115 FILE *fd;
6116 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006117#define STYLE_ECHO 0 /* use "echo", the default */
6118#define STYLE_GLOB 1 /* use "glob", for csh */
6119#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6120#define STYLE_PRINT 3 /* use "print -N", for zsh */
6121#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6122 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 int shell_style = STYLE_ECHO;
6124 int check_spaces;
6125 static int did_find_nul = FALSE;
6126 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006127 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006128 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 *num_file = 0; /* default: no files found */
6131 *file = NULL;
6132
6133 /*
6134 * If there are no wildcards, just copy the names to allocated memory.
6135 * Saves a lot of time, because we don't have to start a new shell.
6136 */
6137 if (!have_wildcard(num_pat, pat))
6138 return save_patterns(num_pat, pat, num_file, file);
6139
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006140# ifdef HAVE_SANDBOX
6141 /* Don't allow any shell command in the sandbox. */
6142 if (sandbox != 0 && check_secure())
6143 return FAIL;
6144# endif
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 /*
6147 * Don't allow the use of backticks in secure and restricted mode.
6148 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006149 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 for (i = 0; i < num_pat; ++i)
6151 if (vim_strchr(pat[i], '`') != NULL
6152 && (check_restricted() || check_secure()))
6153 return FAIL;
6154
6155 /*
6156 * get a name for the temp file
6157 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006158 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
6160 EMSG(_(e_notmp));
6161 return FAIL;
6162 }
6163
6164 /*
6165 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006166 * file.
6167 * STYLE_BT: NL separated
6168 * If expanding `cmd` execute it directly.
6169 * STYLE_GLOB: NUL separated
6170 * If we use *csh, "glob" will work better than "echo".
6171 * STYLE_PRINT: NL or NUL separated
6172 * If we use *zsh, "print -N" will work better than "glob".
6173 * STYLE_VIMGLOB: NL separated
6174 * If we use *sh*, we define "vimglob()".
6175 * STYLE_ECHO: space separated.
6176 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 */
6178 if (num_pat == 1 && *pat[0] == '`'
6179 && (len = STRLEN(pat[0])) > 2
6180 && *(pat[0] + len - 1) == '`')
6181 shell_style = STYLE_BT;
6182 else if ((len = STRLEN(p_sh)) >= 3)
6183 {
6184 if (STRCMP(p_sh + len - 3, "csh") == 0)
6185 shell_style = STYLE_GLOB;
6186 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6187 shell_style = STYLE_PRINT;
6188 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006189 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6190 "sh") != NULL)
6191 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
Bram Moolenaarc7247912008-01-13 12:54:11 +00006193 /* Compute the length of the command. We need 2 extra bytes: for the
6194 * optional '&' and for the NUL.
6195 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006197 if (shell_style == STYLE_VIMGLOB)
6198 len += STRLEN(sh_vimglob_func);
6199
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006200 for (i = 0; i < num_pat; ++i)
6201 {
6202 /* Count the length of the patterns in the same way as they are put in
6203 * "command" below. */
6204#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006206#else
6207 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006208 for (j = 0; pat[i][j] != NUL; ++j)
6209 {
6210 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6211 ++len; /* may add a backslash */
6212 ++len;
6213 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006214#endif
6215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 command = alloc(len);
6217 if (command == NULL)
6218 {
6219 /* out of memory */
6220 vim_free(tempname);
6221 return FAIL;
6222 }
6223
6224 /*
6225 * Build the shell command:
6226 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6227 * recognizes this).
6228 * - Add the shell command to print the expanded names.
6229 * - Add the temp file name.
6230 * - Add the file name patterns.
6231 */
6232 if (shell_style == STYLE_BT)
6233 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006234 /* change `command; command& ` to (command; command ) */
6235 STRCPY(command, "(");
6236 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006238 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006239 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 --p;
6241 if (*p == '&') /* remove trailing '&' */
6242 {
6243 ampersent = TRUE;
6244 *p = ' ';
6245 }
6246 STRCAT(command, ">");
6247 }
6248 else
6249 {
6250 if (flags & EW_NOTFOUND)
6251 STRCPY(command, "set nonomatch; ");
6252 else
6253 STRCPY(command, "unset nonomatch; ");
6254 if (shell_style == STYLE_GLOB)
6255 STRCAT(command, "glob >");
6256 else if (shell_style == STYLE_PRINT)
6257 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006258 else if (shell_style == STYLE_VIMGLOB)
6259 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 else
6261 STRCAT(command, "echo >");
6262 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 if (shell_style != STYLE_BT)
6267 for (i = 0; i < num_pat; ++i)
6268 {
6269 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006270 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006271 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272#ifdef USE_SYSTEM
6273 STRCAT(command, " \"");
6274 STRCAT(command, pat[i]);
6275 STRCAT(command, "\"");
6276#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006277 int intick = FALSE;
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 p = command + STRLEN(command);
6280 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006281 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006282 {
6283 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006284 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006285 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6286 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006287 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006288 * backslash inside backticks, before a special character
6289 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006290 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006291 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6292 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006293 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006294 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006295 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006296 else if (!intick
6297 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6298 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006299 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006300 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6301 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006302 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006303
6304 /* Copy one character. */
6305 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 *p = NUL;
6308#endif
6309 }
6310 if (flags & EW_SILENT)
6311 show_shell_mess = FALSE;
6312 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006313 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 /*
6316 * Using zsh -G: If a pattern has no matches, it is just deleted from
6317 * the argument list, otherwise zsh gives an error message and doesn't
6318 * expand any other pattern.
6319 */
6320 if (shell_style == STYLE_PRINT)
6321 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6322
6323 /*
6324 * If we use -f then shell variables set in .cshrc won't get expanded.
6325 * vi can do it, so we will too, but it is only necessary if there is a "$"
6326 * in one of the patterns, otherwise we can still use the fast option.
6327 */
6328 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6329 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6330
6331 /*
6332 * execute the shell command
6333 */
6334 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6335
6336 /* When running in the background, give it some time to create the temp
6337 * file, but don't wait for it to finish. */
6338 if (ampersent)
6339 mch_delay(10L, TRUE);
6340
6341 extra_shell_arg = NULL; /* cleanup */
6342 show_shell_mess = TRUE;
6343 vim_free(command);
6344
Bram Moolenaarc7247912008-01-13 12:54:11 +00006345 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
6347 mch_remove(tempname);
6348 vim_free(tempname);
6349 /*
6350 * With interactive completion, the error message is not printed.
6351 * However with USE_SYSTEM, I don't know how to turn off error messages
6352 * from the shell, so screen may still get messed up -- webb.
6353 */
6354#ifndef USE_SYSTEM
6355 if (!(flags & EW_SILENT))
6356#endif
6357 {
6358 redraw_later_clear(); /* probably messed up screen */
6359 msg_putchar('\n'); /* clear bottom line quickly */
6360 cmdline_row = Rows - 1; /* continue on last line */
6361#ifdef USE_SYSTEM
6362 if (!(flags & EW_SILENT))
6363#endif
6364 {
6365 MSG(_(e_wildexpand));
6366 msg_start(); /* don't overwrite this message */
6367 }
6368 }
6369 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6370 * EW_NOTFOUND is given */
6371 if (shell_style == STYLE_BT)
6372 return FAIL;
6373 goto notfound;
6374 }
6375
6376 /*
6377 * read the names from the file into memory
6378 */
6379 fd = fopen((char *)tempname, READBIN);
6380 if (fd == NULL)
6381 {
6382 /* Something went wrong, perhaps a file name with a special char. */
6383 if (!(flags & EW_SILENT))
6384 {
6385 MSG(_(e_wildexpand));
6386 msg_start(); /* don't overwrite this message */
6387 }
6388 vim_free(tempname);
6389 goto notfound;
6390 }
6391 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006392 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006394 if (llen < 0)
6395 /* just in case ftell() would fail */
6396 buffer = NULL;
6397 else
6398 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (buffer == NULL)
6400 {
6401 /* out of memory */
6402 mch_remove(tempname);
6403 vim_free(tempname);
6404 fclose(fd);
6405 return FAIL;
6406 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006407 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 i = fread((char *)buffer, 1, len, fd);
6409 fclose(fd);
6410 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006411 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 {
6413 /* unexpected read error */
6414 EMSG2(_(e_notread), tempname);
6415 vim_free(tempname);
6416 vim_free(buffer);
6417 return FAIL;
6418 }
6419 vim_free(tempname);
6420
Bram Moolenaarc7247912008-01-13 12:54:11 +00006421# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6423 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006424 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6426 *p++ = buffer[i];
6427 len = p - buffer;
6428# endif
6429
6430
6431 /* file names are separated with Space */
6432 if (shell_style == STYLE_ECHO)
6433 {
6434 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6435 p = buffer;
6436 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6437 {
6438 while (*p != ' ' && *p != '\n')
6439 ++p;
6440 p = skipwhite(p); /* skip to next entry */
6441 }
6442 }
6443 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006444 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 {
6446 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6447 p = buffer;
6448 for (i = 0; *p != NUL; ++i) /* count number of entries */
6449 {
6450 while (*p != '\n' && *p != NUL)
6451 ++p;
6452 if (*p != NUL)
6453 ++p;
6454 p = skipwhite(p); /* skip leading white space */
6455 }
6456 }
6457 /* file names are separated with NUL */
6458 else
6459 {
6460 /*
6461 * Some versions of zsh use spaces instead of NULs to separate
6462 * results. Only do this when there is no NUL before the end of the
6463 * buffer, otherwise we would never be able to use file names with
6464 * embedded spaces when zsh does use NULs.
6465 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6466 * don't check for spaces again.
6467 */
6468 check_spaces = FALSE;
6469 if (shell_style == STYLE_PRINT && !did_find_nul)
6470 {
6471 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006472 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006473 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 did_find_nul = TRUE;
6475 else
6476 check_spaces = TRUE;
6477 }
6478
6479 /*
6480 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6481 * already is one, for STYLE_GLOB it needs to be added.
6482 */
6483 if (len && buffer[len - 1] == NUL)
6484 --len;
6485 else
6486 buffer[len] = NUL;
6487 i = 0;
6488 for (p = buffer; p < buffer + len; ++p)
6489 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6490 {
6491 ++i;
6492 *p = NUL;
6493 }
6494 if (len)
6495 ++i; /* count last entry */
6496 }
6497 if (i == 0)
6498 {
6499 /*
6500 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6501 * /bin/sh will happily expand it to nothing rather than returning an
6502 * error; and hey, it's good to check anyway -- webb.
6503 */
6504 vim_free(buffer);
6505 goto notfound;
6506 }
6507 *num_file = i;
6508 *file = (char_u **)alloc(sizeof(char_u *) * i);
6509 if (*file == NULL)
6510 {
6511 /* out of memory */
6512 vim_free(buffer);
6513 return FAIL;
6514 }
6515
6516 /*
6517 * Isolate the individual file names.
6518 */
6519 p = buffer;
6520 for (i = 0; i < *num_file; ++i)
6521 {
6522 (*file)[i] = p;
6523 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006524 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6525 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006527 while (!(shell_style == STYLE_ECHO && *p == ' ')
6528 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 ++p;
6530 if (p == buffer + len) /* last entry */
6531 *p = NUL;
6532 else
6533 {
6534 *p++ = NUL;
6535 p = skipwhite(p); /* skip to next entry */
6536 }
6537 }
6538 else /* NUL separates */
6539 {
6540 while (*p && p < buffer + len) /* skip entry */
6541 ++p;
6542 ++p; /* skip NUL */
6543 }
6544 }
6545
6546 /*
6547 * Move the file names to allocated memory.
6548 */
6549 for (j = 0, i = 0; i < *num_file; ++i)
6550 {
6551 /* Require the files to exist. Helps when using /bin/sh */
6552 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6553 continue;
6554
6555 /* check if this entry should be included */
6556 dir = (mch_isdir((*file)[i]));
6557 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6558 continue;
6559
Bram Moolenaara2031822006-03-07 22:29:51 +00006560 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006561 if (!dir && (flags & EW_EXEC)
6562 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006563 continue;
6564
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6566 if (p)
6567 {
6568 STRCPY(p, (*file)[i]);
6569 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006570 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 (*file)[j++] = p;
6572 }
6573 }
6574 vim_free(buffer);
6575 *num_file = j;
6576
6577 if (*num_file == 0) /* rejected all entries */
6578 {
6579 vim_free(*file);
6580 *file = NULL;
6581 goto notfound;
6582 }
6583
6584 return OK;
6585
6586notfound:
6587 if (flags & EW_NOTFOUND)
6588 return save_patterns(num_pat, pat, num_file, file);
6589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590}
6591
6592#endif /* VMS */
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006595save_patterns(
6596 int num_pat,
6597 char_u **pat,
6598 int *num_file,
6599 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006602 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6605 if (*file == NULL)
6606 return FAIL;
6607 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006608 {
6609 s = vim_strsave(pat[i]);
6610 if (s != NULL)
6611 /* Be compatible with expand_filename(): halve the number of
6612 * backslashes. */
6613 backslash_halve(s);
6614 (*file)[i] = s;
6615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 *num_file = num_pat;
6617 return OK;
6618}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
6621 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6622 * expand.
6623 */
6624 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006625mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006627 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 if (*p == '\\' && p[1] != NUL)
6630 ++p;
6631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (vim_strchr((char_u *)
6633#ifdef VMS
6634 "*?%"
6635#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#endif
6638 , *p) != NULL)
6639 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641 return FALSE;
6642}
6643
6644/*
6645 * Return TRUE if the string "p" contains a wildcard.
6646 * Don't recognize '~' at the end as a wildcard.
6647 */
6648 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006649mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006651 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (*p == '\\' && p[1] != NUL)
6654 ++p;
6655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (vim_strchr((char_u *)
6657#ifdef VMS
6658 "*?%$"
6659#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661#endif
6662 , *p) != NULL
6663 || (*p == '~' && p[1] != NUL))
6664 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 }
6666 return FALSE;
6667}
6668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006670have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int i;
6673
6674 for (i = 0; i < num; i++)
6675 if (mch_has_wildcard(file[i]))
6676 return 1;
6677 return 0;
6678}
6679
6680 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006681have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 int i;
6684
6685 for (i = 0; i < num; i++)
6686 if (vim_strchr(file[i], '$') != NULL)
6687 return TRUE;
6688 return FALSE;
6689}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006691#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692/*
6693 * Scaled-down version of rename(), which is missing in Xenix.
6694 * This version can only move regular files and will fail if the
6695 * destination exists.
6696 */
6697 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006698mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 struct stat st;
6701
6702 if (stat(dest, &st) >= 0) /* fail if destination exists */
6703 return -1;
6704 if (link(src, dest) != 0) /* link file to new name */
6705 return -1;
6706 if (mch_remove(src) == 0) /* delete link to old name */
6707 return 0;
6708 return -1;
6709}
6710#endif /* !HAVE_RENAME */
6711
6712#ifdef FEAT_MOUSE_GPM
6713/*
6714 * Initializes connection with gpm (if it isn't already opened)
6715 * Return 1 if succeeded (or connection already opened), 0 if failed
6716 */
6717 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006718gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6721
6722 if (!gpm_flag)
6723 {
6724 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6725 gpm_connect.defaultMask = ~GPM_HARD;
6726 /* Default handling for mouse move*/
6727 gpm_connect.minMod = 0; /* Handle any modifier keys */
6728 gpm_connect.maxMod = 0xffff;
6729 if (Gpm_Open(&gpm_connect, 0) > 0)
6730 {
6731 /* gpm library tries to handling TSTP causes
6732 * problems. Anyways, we close connection to Gpm whenever
6733 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006734 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006736# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 return 1; /* succeed */
6740 }
6741 if (gpm_fd == -2)
6742 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6743 return 0;
6744 }
6745 return 1; /* already open */
6746}
6747
6748/*
6749 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 */
6751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006752gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 if (gpm_flag && gpm_fd >= 0) /* if Open */
6755 Gpm_Close();
6756}
6757
6758/* Reads gpm event and adds special keys to input buf. Returns length of
6759 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02006760 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 */
6762 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006763mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 int button;
6766 static Gpm_Event gpm_event;
6767 char_u string[6];
6768 int_u vim_modifiers;
6769 int row,col;
6770 unsigned char buttons_mask;
6771 unsigned char gpm_modifiers;
6772 static unsigned char old_buttons = 0;
6773
6774 Gpm_GetEvent(&gpm_event);
6775
6776#ifdef FEAT_GUI
6777 /* Don't put events in the input queue now. */
6778 if (hold_gui_events)
6779 return 0;
6780#endif
6781
6782 row = gpm_event.y - 1;
6783 col = gpm_event.x - 1;
6784
6785 string[0] = ESC; /* Our termcode */
6786 string[1] = 'M';
6787 string[2] = 'G';
6788 switch (GPM_BARE_EVENTS(gpm_event.type))
6789 {
6790 case GPM_DRAG:
6791 string[3] = MOUSE_DRAG;
6792 break;
6793 case GPM_DOWN:
6794 buttons_mask = gpm_event.buttons & ~old_buttons;
6795 old_buttons = gpm_event.buttons;
6796 switch (buttons_mask)
6797 {
6798 case GPM_B_LEFT:
6799 button = MOUSE_LEFT;
6800 break;
6801 case GPM_B_MIDDLE:
6802 button = MOUSE_MIDDLE;
6803 break;
6804 case GPM_B_RIGHT:
6805 button = MOUSE_RIGHT;
6806 break;
6807 default:
6808 return 0;
6809 /*Don't know what to do. Can more than one button be
6810 * reported in one event? */
6811 }
6812 string[3] = (char_u)(button | 0x20);
6813 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
6814 break;
6815 case GPM_UP:
6816 string[3] = MOUSE_RELEASE;
6817 old_buttons &= ~gpm_event.buttons;
6818 break;
6819 default:
6820 return 0;
6821 }
6822 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
6823 gpm_modifiers = gpm_event.modifiers;
6824 vim_modifiers = 0x0;
6825 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
6826 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
6827 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
6828 */
6829 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
6830 vim_modifiers |= MOUSE_SHIFT;
6831
6832 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
6833 vim_modifiers |= MOUSE_CTRL;
6834 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
6835 vim_modifiers |= MOUSE_ALT;
6836 string[3] |= vim_modifiers;
6837 string[4] = (char_u)(col + ' ' + 1);
6838 string[5] = (char_u)(row + ' ' + 1);
6839 add_to_input_buf(string, 6);
6840 return 6;
6841}
6842#endif /* FEAT_MOUSE_GPM */
6843
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006844#ifdef FEAT_SYSMOUSE
6845/*
6846 * Initialize connection with sysmouse.
6847 * Let virtual console inform us with SIGUSR2 for pending sysmouse
6848 * output, any sysmouse output than will be processed via sig_sysmouse().
6849 * Return OK if succeeded, FAIL if failed.
6850 */
6851 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006852sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006853{
6854 struct mouse_info mouse;
6855
6856 mouse.operation = MOUSE_MODE;
6857 mouse.u.mode.mode = 0;
6858 mouse.u.mode.signal = SIGUSR2;
6859 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
6860 {
6861 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
6862 mouse.operation = MOUSE_SHOW;
6863 ioctl(1, CONS_MOUSECTL, &mouse);
6864 return OK;
6865 }
6866 return FAIL;
6867}
6868
6869/*
6870 * Stop processing SIGUSR2 signals, and also make sure that
6871 * virtual console do not send us any sysmouse related signal.
6872 */
6873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006874sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006875{
6876 struct mouse_info mouse;
6877
6878 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
6879 mouse.operation = MOUSE_MODE;
6880 mouse.u.mode.mode = 0;
6881 mouse.u.mode.signal = 0;
6882 ioctl(1, CONS_MOUSECTL, &mouse);
6883}
6884
6885/*
6886 * Gets info from sysmouse and adds special keys to input buf.
6887 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006888 static RETSIGTYPE
6889sig_sysmouse SIGDEFARG(sigarg)
6890{
6891 struct mouse_info mouse;
6892 struct video_info video;
6893 char_u string[6];
6894 int row, col;
6895 int button;
6896 int buttons;
6897 static int oldbuttons = 0;
6898
6899#ifdef FEAT_GUI
6900 /* Don't put events in the input queue now. */
6901 if (hold_gui_events)
6902 return;
6903#endif
6904
6905 mouse.operation = MOUSE_GETINFO;
6906 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
6907 && ioctl(1, FBIO_MODEINFO, &video) != -1
6908 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
6909 && video.vi_cheight > 0 && video.vi_cwidth > 0)
6910 {
6911 row = mouse.u.data.y / video.vi_cheight;
6912 col = mouse.u.data.x / video.vi_cwidth;
6913 buttons = mouse.u.data.buttons;
6914 string[0] = ESC; /* Our termcode */
6915 string[1] = 'M';
6916 string[2] = 'S';
6917 if (oldbuttons == buttons && buttons != 0)
6918 {
6919 button = MOUSE_DRAG;
6920 }
6921 else
6922 {
6923 switch (buttons)
6924 {
6925 case 0:
6926 button = MOUSE_RELEASE;
6927 break;
6928 case 1:
6929 button = MOUSE_LEFT;
6930 break;
6931 case 2:
6932 button = MOUSE_MIDDLE;
6933 break;
6934 case 4:
6935 button = MOUSE_RIGHT;
6936 break;
6937 default:
6938 return;
6939 }
6940 oldbuttons = buttons;
6941 }
6942 string[3] = (char_u)(button);
6943 string[4] = (char_u)(col + ' ' + 1);
6944 string[5] = (char_u)(row + ' ' + 1);
6945 add_to_input_buf(string, 6);
6946 }
6947 return;
6948}
6949#endif /* FEAT_SYSMOUSE */
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01006952typedef char_u * (*STRPROCSTR)(char_u *);
6953typedef char_u * (*INTPROCSTR)(int);
6954typedef int (*STRPROCINT)(char_u *);
6955typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
6957/*
6958 * Call a DLL routine which takes either a string or int param
6959 * and returns an allocated string.
6960 */
6961 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006962mch_libcall(
6963 char_u *libname,
6964 char_u *funcname,
6965 char_u *argstring, /* NULL when using a argint */
6966 int argint,
6967 char_u **string_result,/* NULL when using number_result */
6968 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970# if defined(USE_DLOPEN)
6971 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006972 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973# else
6974 shl_t hinstLib;
6975# endif
6976 STRPROCSTR ProcAdd;
6977 INTPROCSTR ProcAddI;
6978 char_u *retval_str = NULL;
6979 int retval_int = 0;
6980 int success = FALSE;
6981
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006982 /*
6983 * Get a handle to the DLL module.
6984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00006986 /* First clear any error, it's not cleared by the dlopen() call. */
6987 (void)dlerror();
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 hinstLib = dlopen((char *)libname, RTLD_LAZY
6990# ifdef RTLD_LOCAL
6991 | RTLD_LOCAL
6992# endif
6993 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006994 if (hinstLib == NULL)
6995 {
6996 /* "dlerr" must be used before dlclose() */
6997 dlerr = (char *)dlerror();
6998 if (dlerr != NULL)
6999 EMSG2(_("dlerror = \"%s\""), dlerr);
7000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001# else
7002 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7003# endif
7004
7005 /* If the handle is valid, try to get the function address. */
7006 if (hinstLib != NULL)
7007 {
7008# ifdef HAVE_SETJMP_H
7009 /*
7010 * Catch a crash when calling the library function. For example when
7011 * using a number where a string pointer is expected.
7012 */
7013 mch_startjmp();
7014 if (SETJMP(lc_jump_env) != 0)
7015 {
7016 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007017# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007018 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 mch_didjmp();
7021 }
7022 else
7023# endif
7024 {
7025 retval_str = NULL;
7026 retval_int = 0;
7027
7028 if (argstring != NULL)
7029 {
7030# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007031 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007032 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033# else
7034 if (shl_findsym(&hinstLib, (const char *)funcname,
7035 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7036 ProcAdd = NULL;
7037# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007038 if ((success = (ProcAdd != NULL
7039# if defined(USE_DLOPEN)
7040 && dlerr == NULL
7041# endif
7042 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 if (string_result == NULL)
7045 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7046 else
7047 retval_str = (ProcAdd)(argstring);
7048 }
7049 }
7050 else
7051 {
7052# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007053 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007054 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055# else
7056 if (shl_findsym(&hinstLib, (const char *)funcname,
7057 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7058 ProcAddI = NULL;
7059# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007060 if ((success = (ProcAddI != NULL
7061# if defined(USE_DLOPEN)
7062 && dlerr == NULL
7063# endif
7064 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
7066 if (string_result == NULL)
7067 retval_int = ((INTPROCINT)ProcAddI)(argint);
7068 else
7069 retval_str = (ProcAddI)(argint);
7070 }
7071 }
7072
7073 /* Save the string before we free the library. */
7074 /* Assume that a "1" or "-1" result is an illegal pointer. */
7075 if (string_result == NULL)
7076 *number_result = retval_int;
7077 else if (retval_str != NULL
7078 && retval_str != (char_u *)1
7079 && retval_str != (char_u *)-1)
7080 *string_result = vim_strsave(retval_str);
7081 }
7082
7083# ifdef HAVE_SETJMP_H
7084 mch_endjmp();
7085# ifdef SIGHASARG
7086 if (lc_signal != 0)
7087 {
7088 int i;
7089
7090 /* try to find the name of this signal */
7091 for (i = 0; signal_info[i].sig != -1; i++)
7092 if (lc_signal == signal_info[i].sig)
7093 break;
7094 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7095 }
7096# endif
7097# endif
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007100 /* "dlerr" must be used before dlclose() */
7101 if (dlerr != NULL)
7102 EMSG2(_("dlerror = \"%s\""), dlerr);
7103
7104 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 (void)dlclose(hinstLib);
7106# else
7107 (void)shl_unload(hinstLib);
7108# endif
7109 }
7110
7111 if (!success)
7112 {
7113 EMSG2(_(e_libcall), funcname);
7114 return FAIL;
7115 }
7116
7117 return OK;
7118}
7119#endif
7120
7121#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7122static int xterm_trace = -1; /* default: disabled */
7123static int xterm_button;
7124
7125/*
7126 * Setup a dummy window for X selections in a terminal.
7127 */
7128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007129setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 int z = 0;
7132 char *strp = "";
7133 Widget AppShell;
7134
7135 if (!x_connect_to_server())
7136 return;
7137
7138 open_app_context();
7139 if (app_context != NULL && xterm_Shell == (Widget)0)
7140 {
7141 int (*oldhandler)();
7142#if defined(HAVE_SETJMP_H)
7143 int (*oldIOhandler)();
7144#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007145# ifdef ELAPSED_FUNC
7146 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007149 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150# endif
7151
7152 /* Ignore X errors while opening the display */
7153 oldhandler = XSetErrorHandler(x_error_check);
7154
7155#if defined(HAVE_SETJMP_H)
7156 /* Ignore X IO errors while opening the display */
7157 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7158 mch_startjmp();
7159 if (SETJMP(lc_jump_env) != 0)
7160 {
7161 mch_didjmp();
7162 xterm_dpy = NULL;
7163 }
7164 else
7165#endif
7166 {
7167 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7168 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7169#if defined(HAVE_SETJMP_H)
7170 mch_endjmp();
7171#endif
7172 }
7173
7174#if defined(HAVE_SETJMP_H)
7175 /* Now handle X IO errors normally. */
7176 (void)XSetIOErrorHandler(oldIOhandler);
7177#endif
7178 /* Now handle X errors normally. */
7179 (void)XSetErrorHandler(oldhandler);
7180
7181 if (xterm_dpy == NULL)
7182 {
7183 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007184 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 return;
7186 }
7187
7188 /* Catch terminating error of the X server connection. */
7189 (void)XSetIOErrorHandler(x_IOerror_handler);
7190
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007191# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007193 {
7194 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007195 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007196 verbose_leave();
7197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198# endif
7199
7200 /* Create a Shell to make converters work. */
7201 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7202 applicationShellWidgetClass, xterm_dpy,
7203 NULL);
7204 if (AppShell == (Widget)0)
7205 return;
7206 xterm_Shell = XtVaCreatePopupShell("VIM",
7207 topLevelShellWidgetClass, AppShell,
7208 XtNmappedWhenManaged, 0,
7209 XtNwidth, 1,
7210 XtNheight, 1,
7211 NULL);
7212 if (xterm_Shell == (Widget)0)
7213 return;
7214
7215 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007216 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (x11_display == NULL)
7218 x11_display = xterm_dpy;
7219
7220 XtRealizeWidget(xterm_Shell);
7221 XSync(xterm_dpy, False);
7222 xterm_update();
7223 }
7224 if (xterm_Shell != (Widget)0)
7225 {
7226 clip_init(TRUE);
7227 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7228 x11_window = (Window)atol(strp);
7229 /* Check if $WINDOWID is valid. */
7230 if (test_x11_window(xterm_dpy) == FAIL)
7231 x11_window = 0;
7232 if (x11_window != 0)
7233 xterm_trace = 0;
7234 }
7235}
7236
7237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007238start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
7240 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7241 return;
7242 xterm_trace = 1;
7243 xterm_button = button;
7244 do_xterm_trace();
7245}
7246
7247
7248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007249stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
7251 if (xterm_trace < 0)
7252 return;
7253 xterm_trace = 0;
7254}
7255
7256/*
7257 * Query the xterm pointer and generate mouse termcodes if necessary
7258 * return TRUE if dragging is active, else FALSE
7259 */
7260 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007261do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 Window root, child;
7264 int root_x, root_y;
7265 int win_x, win_y;
7266 int row, col;
7267 int_u mask_return;
7268 char_u buf[50];
7269 char_u *strp;
7270 long got_hints;
7271 static char_u *mouse_code;
7272 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7273 static int prev_row = 0, prev_col = 0;
7274 static XSizeHints xterm_hints;
7275
7276 if (xterm_trace <= 0)
7277 return FALSE;
7278
7279 if (xterm_trace == 1)
7280 {
7281 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007282 * have changed recently. */
7283 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7284 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 || xterm_hints.width_inc <= 1
7286 || xterm_hints.height_inc <= 1)
7287 {
7288 xterm_trace = -1; /* Not enough data -- disable tracing */
7289 return FALSE;
7290 }
7291
7292 /* Rely on the same mouse code for the duration of this */
7293 mouse_code = find_termcode(mouse_name);
7294 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007295 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 xterm_trace = 2;
7297
7298 /* Find the offset of the chars, there might be a scrollbar on the
7299 * left of the window and/or a menu on the top (eterm etc.) */
7300 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7301 &win_x, &win_y, &mask_return);
7302 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7303 - (xterm_hints.height_inc / 2);
7304 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7305 xterm_hints.y = 2;
7306 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7307 - (xterm_hints.width_inc / 2);
7308 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7309 xterm_hints.x = 2;
7310 return TRUE;
7311 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007312 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 {
7314 xterm_trace = 0;
7315 return FALSE;
7316 }
7317
7318 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7319 &win_x, &win_y, &mask_return);
7320
7321 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7322 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7323 if (row == prev_row && col == prev_col)
7324 return TRUE;
7325
7326 STRCPY(buf, mouse_code);
7327 strp = buf + STRLEN(buf);
7328 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7329 *strp++ = (char_u)(col + ' ' + 1);
7330 *strp++ = (char_u)(row + ' ' + 1);
7331 *strp = 0;
7332 add_to_input_buf(buf, STRLEN(buf));
7333
7334 prev_row = row;
7335 prev_col = col;
7336 return TRUE;
7337}
7338
7339# if defined(FEAT_GUI) || defined(PROTO)
7340/*
7341 * Destroy the display, window and app_context. Required for GTK.
7342 */
7343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007344clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
7346 if (xterm_Shell != (Widget)0)
7347 {
7348 XtDestroyWidget(xterm_Shell);
7349 xterm_Shell = (Widget)0;
7350 }
7351 if (xterm_dpy != NULL)
7352 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007353# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 /* Lesstif and Solaris crash here, lose some memory */
7355 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 if (x11_display == xterm_dpy)
7358 x11_display = NULL;
7359 xterm_dpy = NULL;
7360 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007361# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (app_context != (XtAppContext)NULL)
7363 {
7364 /* Lesstif and Solaris crash here, lose some memory */
7365 XtDestroyApplicationContext(app_context);
7366 app_context = (XtAppContext)NULL;
7367 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369}
7370# endif
7371
7372/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007373 * Catch up with GUI or X events.
7374 */
7375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007376clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007377{
7378# ifdef FEAT_GUI
7379 if (gui.in_use)
7380 gui_mch_update();
7381 else
7382# endif
7383 if (xterm_Shell != (Widget)0)
7384 xterm_update();
7385}
7386
7387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 * Catch up with any queued X events. This may put keyboard input into the
7389 * input buffer, call resize call-backs, trigger timers etc. If there is
7390 * nothing in the X event queue (& no timers pending), then we return
7391 * immediately.
7392 */
7393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007394xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 XEvent event;
7397
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007398 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007400 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007402 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007403 break;
7404
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007405 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007406 {
7407 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007408 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007409#ifdef FEAT_CLIENTSERVER
7410 {
7411 XPropertyEvent *e = (XPropertyEvent *)&event;
7412
7413 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007415 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007418 XtDispatchEvent(&event);
7419 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007420 else
7421 {
7422 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007423 XtAppProcessEvent(app_context, mask);
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426}
7427
7428 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007429clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
7431 if (xterm_Shell != (Widget)0)
7432 return clip_x11_own_selection(xterm_Shell, cbd);
7433 return FAIL;
7434}
7435
7436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007437clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 if (xterm_Shell != (Widget)0)
7440 clip_x11_lose_selection(xterm_Shell, cbd);
7441}
7442
7443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007444clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 if (xterm_Shell != (Widget)0)
7447 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7448}
7449
7450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007451clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 clip_x11_set_selection(cbd);
7454}
7455#endif
7456
7457
7458#if defined(USE_XSMP) || defined(PROTO)
7459/*
7460 * Code for X Session Management Protocol.
7461 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007462static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7463static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7464static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7465static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7466static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468
7469# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007470static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472/*
7473 * This is our chance to ask the user if they want to save,
7474 * or abort the logout
7475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007477xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479 cmdmod_T save_cmdmod;
7480 int cancel_shutdown = False;
7481
7482 save_cmdmod = cmdmod;
7483 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007484 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 /* Mustn't logout */
7486 cancel_shutdown = True;
7487 cmdmod = save_cmdmod;
7488 setcursor(); /* position cursor */
7489 out_flush();
7490
7491 /* Done interaction */
7492 SmcInteractDone(smc_conn, cancel_shutdown);
7493
7494 /* Finish off
7495 * Only end save-yourself here if we're not cancelling shutdown;
7496 * we'll get a cancelled callback later in which we'll end it.
7497 * Hopefully get around glitchy SMs (like GNOME-1)
7498 */
7499 if (!cancel_shutdown)
7500 {
7501 xsmp.save_yourself = False;
7502 SmcSaveYourselfDone(smc_conn, True);
7503 }
7504}
7505# endif
7506
7507/*
7508 * Callback that starts save-yourself.
7509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007511xsmp_handle_save_yourself(
7512 SmcConn smc_conn,
7513 SmPointer client_data UNUSED,
7514 int save_type UNUSED,
7515 Bool shutdown,
7516 int interact_style UNUSED,
7517 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
7519 /* Handle already being in saveyourself */
7520 if (xsmp.save_yourself)
7521 SmcSaveYourselfDone(smc_conn, True);
7522 xsmp.save_yourself = True;
7523 xsmp.shutdown = shutdown;
7524
7525 /* First up, preserve all files */
7526 out_flush();
7527 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7528
7529 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007530 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531
7532# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7533 /* Now see if we can ask about unsaved files */
7534 if (shutdown && !fast && gui.in_use)
7535 /* Need to interact with user, but need SM's permission */
7536 SmcInteractRequest(smc_conn, SmDialogError,
7537 xsmp_handle_interaction, client_data);
7538 else
7539# endif
7540 {
7541 /* Can stop the cycle here */
7542 SmcSaveYourselfDone(smc_conn, True);
7543 xsmp.save_yourself = False;
7544 }
7545}
7546
7547
7548/*
7549 * Callback to warn us of imminent death.
7550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007552xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 xsmp_close();
7555
7556 /* quit quickly leaving swapfiles for modified buffers behind */
7557 getout_preserve_modified(0);
7558}
7559
7560
7561/*
7562 * Callback to tell us that save-yourself has completed.
7563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007565xsmp_save_complete(
7566 SmcConn smc_conn UNUSED,
7567 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569 xsmp.save_yourself = False;
7570}
7571
7572
7573/*
7574 * Callback to tell us that an instigated shutdown was cancelled
7575 * (maybe even by us)
7576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007578xsmp_shutdown_cancelled(
7579 SmcConn smc_conn,
7580 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 if (xsmp.save_yourself)
7583 SmcSaveYourselfDone(smc_conn, True);
7584 xsmp.save_yourself = False;
7585 xsmp.shutdown = False;
7586}
7587
7588
7589/*
7590 * Callback to tell us that a new ICE connection has been established.
7591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007593xsmp_ice_connection(
7594 IceConn iceConn,
7595 IcePointer clientData UNUSED,
7596 Bool opening,
7597 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598{
7599 /* Intercept creation of ICE connection fd */
7600 if (opening)
7601 {
7602 xsmp_icefd = IceConnectionNumber(iceConn);
7603 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7604 }
7605}
7606
7607
7608/* Handle any ICE processing that's required; return FAIL if SM lost */
7609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007610xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 Bool rep;
7613
7614 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7615 == IceProcessMessagesIOError)
7616 {
7617 /* Lost ICE */
7618 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007619 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 xsmp_close();
7621 return FAIL;
7622 }
7623 else
7624 return OK;
7625}
7626
7627static int dummy;
7628
7629/* Set up X Session Management Protocol */
7630 void
7631xsmp_init(void)
7632{
7633 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 SmcCallbacks smcallbacks;
7635#if 0
7636 SmPropValue smname;
7637 SmProp smnameprop;
7638 SmProp *smprops[1];
7639#endif
7640
7641 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007642 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644 xsmp.save_yourself = xsmp.shutdown = False;
7645
7646 /* Set up SM callbacks - must have all, even if they're not used */
7647 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7648 smcallbacks.save_yourself.client_data = NULL;
7649 smcallbacks.die.callback = xsmp_die;
7650 smcallbacks.die.client_data = NULL;
7651 smcallbacks.save_complete.callback = xsmp_save_complete;
7652 smcallbacks.save_complete.client_data = NULL;
7653 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7654 smcallbacks.shutdown_cancelled.client_data = NULL;
7655
7656 /* Set up a watch on ICE connection creations. The "dummy" argument is
7657 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7658 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7659 {
7660 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007661 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 return;
7663 }
7664
7665 /* Create an SM connection */
7666 xsmp.smcconn = SmcOpenConnection(
7667 NULL,
7668 NULL,
7669 SmProtoMajor,
7670 SmProtoMinor,
7671 SmcSaveYourselfProcMask | SmcDieProcMask
7672 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7673 &smcallbacks,
7674 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007675 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 sizeof(errorstring),
7677 errorstring);
7678 if (xsmp.smcconn == NULL)
7679 {
7680 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007681
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007683 {
7684 vim_snprintf(errorreport, sizeof(errorreport),
7685 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7686 verb_msg((char_u *)errorreport);
7687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 return;
7689 }
7690 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7691
7692#if 0
7693 /* ID ourselves */
7694 smname.value = "vim";
7695 smname.length = 3;
7696 smnameprop.name = "SmProgram";
7697 smnameprop.type = "SmARRAY8";
7698 smnameprop.num_vals = 1;
7699 smnameprop.vals = &smname;
7700
7701 smprops[0] = &smnameprop;
7702 SmcSetProperties(xsmp.smcconn, 1, smprops);
7703#endif
7704}
7705
7706
7707/* Shut down XSMP comms. */
7708 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007709xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 if (xsmp_icefd != -1)
7712 {
7713 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007714 if (xsmp.clientid != NULL)
7715 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007716 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 xsmp_icefd = -1;
7718 }
7719}
7720#endif /* USE_XSMP */
7721
7722
7723#ifdef EBCDIC
7724/* Translate character to its CTRL- value */
7725char CtrlTable[] =
7726{
7727/* 00 - 5E */
7728 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7729 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7730 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7731 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7732 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7733 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7734/* ^ */ 0x1E,
7735/* - */ 0x1F,
7736/* 61 - 6C */
7737 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7738/* _ */ 0x1F,
7739/* 6E - 80 */
7740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7741/* a */ 0x01,
7742/* b */ 0x02,
7743/* c */ 0x03,
7744/* d */ 0x37,
7745/* e */ 0x2D,
7746/* f */ 0x2E,
7747/* g */ 0x2F,
7748/* h */ 0x16,
7749/* i */ 0x05,
7750/* 8A - 90 */
7751 0, 0, 0, 0, 0, 0, 0,
7752/* j */ 0x15,
7753/* k */ 0x0B,
7754/* l */ 0x0C,
7755/* m */ 0x0D,
7756/* n */ 0x0E,
7757/* o */ 0x0F,
7758/* p */ 0x10,
7759/* q */ 0x11,
7760/* r */ 0x12,
7761/* 9A - A1 */
7762 0, 0, 0, 0, 0, 0, 0, 0,
7763/* s */ 0x13,
7764/* t */ 0x3C,
7765/* u */ 0x3D,
7766/* v */ 0x32,
7767/* w */ 0x26,
7768/* x */ 0x18,
7769/* y */ 0x19,
7770/* z */ 0x3F,
7771/* AA - AC */
7772 0, 0, 0,
7773/* [ */ 0x27,
7774/* AE - BC */
7775 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7776/* ] */ 0x1D,
7777/* BE - C0 */ 0, 0, 0,
7778/* A */ 0x01,
7779/* B */ 0x02,
7780/* C */ 0x03,
7781/* D */ 0x37,
7782/* E */ 0x2D,
7783/* F */ 0x2E,
7784/* G */ 0x2F,
7785/* H */ 0x16,
7786/* I */ 0x05,
7787/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
7788/* J */ 0x15,
7789/* K */ 0x0B,
7790/* L */ 0x0C,
7791/* M */ 0x0D,
7792/* N */ 0x0E,
7793/* O */ 0x0F,
7794/* P */ 0x10,
7795/* Q */ 0x11,
7796/* R */ 0x12,
7797/* DA - DF */ 0, 0, 0, 0, 0, 0,
7798/* \ */ 0x1C,
7799/* E1 */ 0,
7800/* S */ 0x13,
7801/* T */ 0x3C,
7802/* U */ 0x3D,
7803/* V */ 0x32,
7804/* W */ 0x26,
7805/* X */ 0x18,
7806/* Y */ 0x19,
7807/* Z */ 0x3F,
7808/* EA - FF*/ 0, 0, 0, 0, 0, 0,
7809 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7810};
7811
7812char MetaCharTable[]=
7813{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7814 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
7815 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
7816 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
7817 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
7818};
7819
7820
7821/* TODO: Use characters NOT numbers!!! */
7822char CtrlCharTable[]=
7823{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
7824 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
7825 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
7826 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
7827 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
7828};
7829
7830
7831#endif