blob: 45fd26176479cb107252541609112fcf9de87793 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
15 * Also for BeOS and Atari MiNT.
16 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
21/*
22 * Some systems have a prototype for select() that has (int *) instead of
23 * (fd_set *), which is wrong. This define removes that prototype. We define
24 * our own prototype below.
25 * Don't use it for the Mac, it causes a warning for precompiled headers.
26 * TODO: use a configure check for precompiled headers?
27 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000028#if !defined(__APPLE__) && !defined(__TANDEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +000029# define select select_declared_wrong
30#endif
31
32#include "vim.h"
33
Bram Moolenaar325b7a22004-07-05 15:58:32 +000034#ifdef FEAT_MZSCHEME
35# include "if_mzsch.h"
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "os_unixx.h" /* unix includes for os_unix.c only */
39
40#ifdef USE_XSMP
41# include <X11/SM/SMlib.h>
42#endif
43
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000044#ifdef HAVE_SELINUX
45# include <selinux/selinux.h>
46static int selinux_enabled = -1;
47#endif
48
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020049#ifdef HAVE_SMACK
50# include <attr/xattr.h>
51# include <linux/xattr.h>
52# ifndef SMACK_LABEL_LEN
53# define SMACK_LABEL_LEN 1024
54# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * Use this prototype for select, some include files have a wrong prototype
59 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000060#ifndef __TANDEM
61# undef select
62# ifdef __BEOS__
63# define select beos_select
64# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
66
Bram Moolenaara2442432007-04-26 14:26:37 +000067#ifdef __CYGWIN__
68# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000069# include <cygwin/version.h>
70# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
71 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010072# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
73# define WIN32_LEAN_AND_MEAN
74# include <windows.h>
75# include "winclip.pro"
76# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000077# endif
78#endif
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#if defined(HAVE_SELECT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
83
84#ifdef FEAT_MOUSE_GPM
85# include <gpm.h>
86/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
87 * I just copied relevant defines here. A cleaner solution would be to put gpm
88 * code into separate file and include there linux/keyboard.h
89 */
90/* #include <linux/keyboard.h> */
91# define KG_SHIFT 0
92# define KG_CTRL 2
93# define KG_ALT 3
94# define KG_ALTGR 1
95# define KG_SHIFTL 4
96# define KG_SHIFTR 5
97# define KG_CTRLL 6
98# define KG_CTRLR 7
99# define KG_CAPSSHIFT 8
100
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void gpm_close(void);
102static int gpm_open(void);
103static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000106#ifdef FEAT_SYSMOUSE
107# include <sys/consio.h>
108# include <sys/fbio.h>
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static int sysmouse_open(void);
111static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +0100112static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000113#endif
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115/*
116 * end of autoconf section. To be extended...
117 */
118
119/* Are the following #ifdefs still required? And why? Is that for X11? */
120
121#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
122# ifdef SIGWINCH
123# undef SIGWINCH
124# endif
125# ifdef TIOCGWINSZ
126# undef TIOCGWINSZ
127# endif
128#endif
129
130#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
131# define SIGWINCH SIGWINDOW
132#endif
133
134#ifdef FEAT_X11
135# include <X11/Xlib.h>
136# include <X11/Xutil.h>
137# include <X11/Xatom.h>
138# ifdef FEAT_XCLIPBOARD
139# include <X11/Intrinsic.h>
140# include <X11/Shell.h>
141# include <X11/StringDefs.h>
142static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void clip_update(void);
144static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
146
147# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
148Window x11_window = 0;
149# endif
150Display *x11_display = NULL;
151
152# ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static int get_x11_windis(void);
154static void set_x11_title(char_u *);
155static void set_x11_icon(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156# endif
157#endif
158
159#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int get_x11_title(int);
161static int get_x11_icon(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163static char_u *oldtitle = NULL;
164static int did_set_title = FALSE;
165static char_u *oldicon = NULL;
166static int did_set_icon = FALSE;
167#endif
168
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
Bram Moolenaar205b8862011-09-07 15:04:31 +0200171#ifdef HAVE_UNION_WAIT
172typedef union wait waitstatus;
173#else
174typedef int waitstatus;
175#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static pid_t wait4pid(pid_t, waitstatus *);
Bram Moolenaar205b8862011-09-07 15:04:31 +0200177
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200178static int WaitForChar(long msec, int *interrupted, int ignore_input);
179static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100180#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200181int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200183static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
185
186#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100187static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000188# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100191static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100194static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100197static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#endif
199#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100200static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202#if defined(SIGALRM) && defined(FEAT_X11) \
203 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
204# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100205static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000206/* volatile because it is used in signal handler sig_alarm(). */
207static volatile int sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100209static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100211static void catch_int_signal(void);
212static void set_signals(void);
213static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200214#ifdef HAVE_SIGPROCMASK
215# define SIGSET_DECL(set) sigset_t set;
216# define BLOCK_SIGNALS(set) block_signals(set)
217# define UNBLOCK_SIGNALS(set) unblock_signals(set)
218#else
219# define SIGSET_DECL(set)
220# define BLOCK_SIGNALS(set) do { /**/ } while (0)
221# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
222#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100223static int have_wildcard(int, char_u **);
224static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100226static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000229# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#endif
231
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000232/* volatile because it is used in signal handler sig_winch(). */
233static volatile int do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234static char_u *extra_shell_arg = NULL;
235static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000236/* volatile because it is used in signal handler deathtrap(). */
237static volatile int deadly_signal = 0; /* The signal we caught */
238/* volatile because it is used in signal handler deathtrap(). */
239static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100241#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
242static int dont_check_job_ended = 0;
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
246
247#ifdef USE_XSMP
248typedef struct
249{
250 SmcConn smcconn; /* The SM connection ID */
251 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200252 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 Bool save_yourself; /* If we're in the middle of a save_yourself */
254 Bool shutdown; /* If we're in shutdown mode */
255} xsmp_config_T;
256
257static xsmp_config_T xsmp;
258#endif
259
260#ifdef SYS_SIGLIST_DECLARED
261/*
262 * I have seen
263 * extern char *_sys_siglist[NSIG];
264 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
265 * that describe the signals. That is nearly what we want here. But
266 * autoconf does only check for sys_siglist (without the underscore), I
267 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100268 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 */
270#endif
271
272static struct signalinfo
273{
274 int sig; /* Signal number, eg. SIGSEGV etc */
275 char *name; /* Signal name (not char_u!). */
276 char deadly; /* Catch as a deadly signal? */
277} signal_info[] =
278{
279#ifdef SIGHUP
280 {SIGHUP, "HUP", TRUE},
281#endif
282#ifdef SIGQUIT
283 {SIGQUIT, "QUIT", TRUE},
284#endif
285#ifdef SIGILL
286 {SIGILL, "ILL", TRUE},
287#endif
288#ifdef SIGTRAP
289 {SIGTRAP, "TRAP", TRUE},
290#endif
291#ifdef SIGABRT
292 {SIGABRT, "ABRT", TRUE},
293#endif
294#ifdef SIGEMT
295 {SIGEMT, "EMT", TRUE},
296#endif
297#ifdef SIGFPE
298 {SIGFPE, "FPE", TRUE},
299#endif
300#ifdef SIGBUS
301 {SIGBUS, "BUS", TRUE},
302#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100303#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
304 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {SIGSEGV, "SEGV", TRUE},
306#endif
307#ifdef SIGSYS
308 {SIGSYS, "SYS", TRUE},
309#endif
310#ifdef SIGALRM
311 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
312#endif
313#ifdef SIGTERM
314 {SIGTERM, "TERM", TRUE},
315#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100316#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {SIGVTALRM, "VTALRM", TRUE},
318#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000319#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
320 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
321 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 {SIGPROF, "PROF", TRUE},
323#endif
324#ifdef SIGXCPU
325 {SIGXCPU, "XCPU", TRUE},
326#endif
327#ifdef SIGXFSZ
328 {SIGXFSZ, "XFSZ", TRUE},
329#endif
330#ifdef SIGUSR1
331 {SIGUSR1, "USR1", TRUE},
332#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000333#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
334 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {SIGUSR2, "USR2", TRUE},
336#endif
337#ifdef SIGINT
338 {SIGINT, "INT", FALSE},
339#endif
340#ifdef SIGWINCH
341 {SIGWINCH, "WINCH", FALSE},
342#endif
343#ifdef SIGTSTP
344 {SIGTSTP, "TSTP", FALSE},
345#endif
346#ifdef SIGPIPE
347 {SIGPIPE, "PIPE", FALSE},
348#endif
349 {-1, "Unknown!", FALSE}
350};
351
Bram Moolenaar25724922009-07-14 15:38:41 +0000352 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100353mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000354{
355 if (p_verbose >= 5)
356 {
357 verbose_enter();
358 smsg((char_u *)"chdir(%s)", path);
359 verbose_leave();
360 }
361# ifdef VMS
362 return chdir(vms_fixfilename(path));
363# else
364 return chdir(path);
365# endif
366}
367
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200368/* Why is NeXT excluded here (and not in os_unixx.h)? */
369#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
370# define NEW_TTY_SYSTEM
371#endif
372
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000373/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100374 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100377mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000379 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100381 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382}
383
384/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000385 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 * Get a characters from the keyboard.
387 * Return the number of characters that are available.
388 * If wtime == 0 do not wait for characters.
389 * If wtime == n wait a short time for characters.
390 * If wtime == -1 wait forever for characters.
391 */
392 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100393mch_inchar(
394 char_u *buf,
395 int maxlen,
396 long wtime, /* don't use "time", MIPS cannot handle it */
397 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
399 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200400 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200401 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200402 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200403 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100404#ifdef ELAPSED_FUNC
405 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200406
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100407 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200410 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200411 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200413 /* Check if window changed size while we were busy, perhaps the ":set
414 * columns=99" command was used. */
415 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200417
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200418#ifdef MESSAGE_QUEUE
419 parse_queued_messages();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200420 /* If input was put directly in typeahead buffer bail out here. */
421 if (typebuf_changed(tb_change_cnt))
422 return 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200423#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200424 if (wtime < 0 && did_start_blocking)
425 /* blocking and already waited for p_ut */
426 wait_time = -1;
427 else
428 {
429 if (wtime >= 0)
430 wait_time = wtime;
431 else
432 /* going to block after p_ut */
433 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100434#ifdef ELAPSED_FUNC
435 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200436#endif
437 wait_time -= elapsed_time;
438 if (wait_time < 0)
439 {
440 if (wtime >= 0)
441 /* no character available within "wtime" */
442 return 0;
443
444 if (wtime < 0)
445 {
446 /* no character available within 'updatetime' */
447 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200448 if (trigger_cursorhold() && maxlen >= 3
449 && !typebuf_changed(tb_change_cnt))
450 {
451 buf[0] = K_SPECIAL;
452 buf[1] = KS_EXTRA;
453 buf[2] = (int)KE_CURSORHOLD;
454 return 3;
455 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200456 /*
457 * If there is no character available within 'updatetime'
458 * seconds flush all the swap files to disk.
459 * Also done when interrupted by SIGWINCH.
460 */
461 before_blocking();
462 continue;
463 }
464 }
465 }
466
467#ifdef FEAT_JOB_CHANNEL
468 /* Checking if a job ended requires polling. Do this every 100 msec. */
469 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
470 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100471 /* If there is readahead then parse_queued_messages() timed out and we
472 * should call it again soon. */
473 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
474 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200475#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100476#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100477 if (p_beval && wait_time > 100L)
478 /* The 'balloonexpr' may indirectly invoke a callback while waiting
479 * for a character, need to check often. */
480 wait_time = 100L;
481#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200482
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200484 * We want to be interrupted by the winch signal
485 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200487 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200488 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200489 /* If input was put directly in typeahead buffer bail out here. */
490 if (typebuf_changed(tb_change_cnt))
491 return 0;
492
493 /*
494 * For some terminals we only get one character at a time.
495 * We want the get all available characters, so we could keep on
496 * trying until none is available
497 * For some other terminals this is quite slow, that's why we don't
498 * do it.
499 */
500 len = read_from_input_buf(buf, (long)maxlen);
501 if (len > 0)
502 return len;
503 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200506 /* no character available */
507#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
508 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100509 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200512 if (do_resize /* interrupted by SIGWINCH signal */
513#ifdef FEAT_CLIENTSERVER
514 || server_waiting()
515#endif
516#ifdef MESSAGE_QUEUE
517 || interrupted
518#endif
519 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100520 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200521 continue;
522
523 /* no character available or interrupted */
524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200526 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100530handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 do_resize = FALSE;
533 shell_resized();
534}
535
536/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200537 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100540mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200542 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543}
544
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200545#if defined(FEAT_TERMINAL) || defined(PROTO)
546/*
547 * Check for any pending input or messages.
548 */
549 int
550mch_check_messages(void)
551{
552 return WaitForChar(0L, NULL, TRUE);
553}
554#endif
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
557# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000558# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559# endif
560# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
561# include <sys/sysctl.h>
562# endif
563# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
564# include <sys/sysinfo.h>
565# endif
Bram Moolenaar988615f2018-02-27 17:58:20 +0100566# ifdef MACOS_X_DARWIN
567# include <mach/mach_host.h>
568# include <mach/mach_port.h>
569# include <mach/vm_page_size.h>
570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000573 * Return total amount of memory available in Kbyte.
574 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100577mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000580 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar988615f2018-02-27 17:58:20 +0100582# ifdef MACOS_X_DARWIN
583 {
584 /* Mac (Darwin) way of getting the amount of RAM available */
585 mach_port_t host = mach_host_self();
586 kern_return_t kret;
587# ifdef HOST_VM_INFO64
588 struct vm_statistics64 vm_stat;
589 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar988615f2018-02-27 17:58:20 +0100591 kret = host_statistics64(host, HOST_VM_INFO64,
592 (host_info64_t)&vm_stat, &count);
593# else
594 struct vm_statistics vm_stat;
595 natural_t count = HOST_VM_INFO_COUNT;
596
597 kret = host_statistics(host, HOST_VM_INFO,
598 (host_info_t)&vm_stat, &count);
599# endif
600 if (kret == KERN_SUCCESS)
601 /* get the amount of user memory by summing each usage */
602 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
603 + vm_stat.inactive_count
604# ifdef MAC_OS_X_VERSION_10_9
605 + vm_stat.compressor_page_count
606# endif
607 ) * getpagesize();
608 mach_port_deallocate(mach_task_self(), host);
609 }
610# endif
611
612# ifdef HAVE_SYSCTL
613 if (mem == 0)
614 {
615 /* BSD way of getting the amount of RAM available. */
616 int mib[2];
617 size_t len = sizeof(long_u);
618# ifdef HW_USERMEM64
619 long_u physmem;
620# else
621 /* sysctl() may return 32 bit or 64 bit, accept both */
622 union {
623 int_u u32;
624 long_u u64;
625 } physmem;
626# endif
627
628 mib[0] = CTL_HW;
629# ifdef HW_USERMEM64
630 mib[1] = HW_USERMEM64;
631# else
632 mib[1] = HW_USERMEM;
633# endif
634 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
635 {
636# ifdef HW_USERMEM64
637 mem = (long_u)physmem;
638# else
639 if (len == sizeof(physmem.u64))
640 mem = (long_u)physmem.u64;
641 else
642 mem = (long_u)physmem.u32;
643# endif
644 }
645 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (mem == 0)
650 {
651 struct sysinfo sinfo;
652
653 /* Linux way of getting amount of RAM available */
654 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200656# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000657 /* avoid overflow as much as possible */
658 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
659 {
660 sinfo.mem_unit = sinfo.mem_unit >> 1;
661 --shiftright;
662 }
663 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200664# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200666# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200671# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (mem == 0)
673 {
674 long pagesize, pagecount;
675
676 /* Solaris way of getting amount of RAM available */
677 pagesize = sysconf(_SC_PAGESIZE);
678 pagecount = sysconf(_SC_PHYS_PAGES);
679 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 {
681 /* avoid overflow as much as possible */
682 while (shiftright > 0 && (pagesize & 1) == 0)
683 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000684 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000685 --shiftright;
686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* Return the minimum of the physical memory and the user limit, because
693 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200694# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
696 struct rlimit rlp;
697
698 if (getrlimit(RLIMIT_DATA, &rlp) == 0
699 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200700# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200702# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000703 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000705 {
706 mem = (long_u)rlp.rlim_cur;
707 shiftright = 10;
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000713 return mem >> shiftright;
714 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715}
716#endif
717
718 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100719mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000722#ifdef FEAT_MZSCHEME
723 long total = msec; /* remember original value */
724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
726 if (ignoreinput)
727 {
728 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000729 * here. But we don't want QUIT to kill us (CTRL-\ used in a
730 * shell may produce SIGQUIT). */
731 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 old_tmode = curr_tmode;
733 if (curr_tmode == TMODE_RAW)
734 settmode(TMODE_SLEEP);
735
736 /*
737 * Everybody sleeps in a different way...
738 * Prefer nanosleep(), some versions of usleep() can only sleep up to
739 * one second.
740 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000741#ifdef FEAT_MZSCHEME
742 do
743 {
744 /* if total is large enough, wait by portions in p_mzq */
745 if (total > p_mzq)
746 msec = p_mzq;
747 else
748 msec = total;
749 total -= msec;
750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#ifdef HAVE_NANOSLEEP
752 {
753 struct timespec ts;
754
755 ts.tv_sec = msec / 1000;
756 ts.tv_nsec = (msec % 1000) * 1000000;
757 (void)nanosleep(&ts, NULL);
758 }
759#else
760# ifdef HAVE_USLEEP
761 while (msec >= 1000)
762 {
763 usleep((unsigned int)(999 * 1000));
764 msec -= 999;
765 }
766 usleep((unsigned int)(msec * 1000));
767# else
768# ifndef HAVE_SELECT
769 poll(NULL, 0, (int)msec);
770# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 struct timeval tv;
773
774 tv.tv_sec = msec / 1000;
775 tv.tv_usec = (msec % 1000) * 1000;
776 /*
777 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
778 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
779 */
780 select(0, NULL, NULL, NULL, &tv);
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782# endif /* HAVE_SELECT */
783# endif /* HAVE_NANOSLEEP */
784#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000785#ifdef FEAT_MZSCHEME
786 }
787 while (total > 0);
788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000791 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200794 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000797#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
799# define HAVE_CHECK_STACK_GROWTH
800/*
801 * Support for checking for an almost-out-of-stack-space situation.
802 */
803
804/*
805 * Return a pointer to an item on the stack. Used to find out if the stack
806 * grows up or down.
807 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100808static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809static int stack_grows_downwards;
810
811/*
812 * Find out if the stack grows upwards or downwards.
813 * "p" points to a variable on the stack of the caller.
814 */
815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100816check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 int i;
819
820 stack_grows_downwards = (p > (char *)&i);
821}
822#endif
823
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000824#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825static char *stack_limit = NULL;
826
827#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
828# include <pthread.h>
829# include <pthread_np.h>
830#endif
831
832/*
833 * Find out until how var the stack can grow without getting into trouble.
834 * Called when starting up and when switching to the signal stack in
835 * deathtrap().
836 */
837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100838get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 struct rlimit rlp;
841 int i;
842 long lim;
843
844 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
845 * limit doesn't fit in a long (rlim_cur might be "long long"). */
846 if (getrlimit(RLIMIT_STACK, &rlp) == 0
847 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
848# ifdef RLIM_INFINITY
849 && rlp.rlim_cur != RLIM_INFINITY
850# endif
851 )
852 {
853 lim = (long)rlp.rlim_cur;
854#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
855 {
856 pthread_attr_t attr;
857 size_t size;
858
859 /* On FreeBSD the initial thread always has a fixed stack size, no
860 * matter what the limits are set to. Normally it's 1 Mbyte. */
861 pthread_attr_init(&attr);
862 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
863 {
864 pthread_attr_getstacksize(&attr, &size);
865 if (lim > (long)size)
866 lim = (long)size;
867 }
868 pthread_attr_destroy(&attr);
869 }
870#endif
871 if (stack_grows_downwards)
872 {
873 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
874 if (stack_limit >= (char *)&i)
875 /* overflow, set to 1/16 of current stack position */
876 stack_limit = (char *)((long)&i / 16L);
877 }
878 else
879 {
880 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
881 if (stack_limit <= (char *)&i)
882 stack_limit = NULL; /* overflow */
883 }
884 }
885}
886
887/*
888 * Return FAIL when running out of stack space.
889 * "p" must point to any variable local to the caller that's on the stack.
890 */
891 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100892mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 if (stack_limit != NULL)
895 {
896 if (stack_grows_downwards)
897 {
898 if (p < stack_limit)
899 return FAIL;
900 }
901 else if (p > stack_limit)
902 return FAIL;
903 }
904 return OK;
905}
906#endif
907
908#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
909/*
910 * Support for using the signal stack.
911 * This helps when we run out of stack space, which causes a SIGSEGV. The
912 * signal handler then must run on another stack, since the normal stack is
913 * completely full.
914 */
915
916#ifndef SIGSTKSZ
917# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
918#endif
919
920# ifdef HAVE_SIGALTSTACK
921static stack_t sigstk; /* for sigaltstack() */
922# else
923static struct sigstack sigstk; /* for sigstack() */
924# endif
925
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100926static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927static char *signal_stack;
928
929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100930init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 if (signal_stack != NULL)
933 {
934# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935# ifdef HAVE_SS_BASE
936 sigstk.ss_base = signal_stack;
937# else
938 sigstk.ss_sp = signal_stack;
939# endif
940 sigstk.ss_size = SIGSTKSZ;
941 sigstk.ss_flags = 0;
942 (void)sigaltstack(&sigstk, NULL);
943# else
944 sigstk.ss_sp = signal_stack;
945 if (stack_grows_downwards)
946 sigstk.ss_sp += SIGSTKSZ - 1;
947 sigstk.ss_onstack = 0;
948 (void)sigstack(&sigstk, NULL);
949# endif
950 }
951}
952#endif
953
954/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000955 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * will barf when the second argument to signal() is ``wrong''.
957 * Let me try it with a few tricky defines from my own osdef.h (jw).
958 */
959#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 static RETSIGTYPE
961sig_winch SIGDEFARG(sigarg)
962{
963 /* this is not required on all systems, but it doesn't hurt anybody */
964 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
965 do_resize = TRUE;
966 SIGRETURN;
967}
968#endif
969
970#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 static RETSIGTYPE
972catch_sigint SIGDEFARG(sigarg)
973{
974 /* this is not required on all systems, but it doesn't hurt anybody */
975 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
976 got_int = TRUE;
977 SIGRETURN;
978}
979#endif
980
981#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 static RETSIGTYPE
983catch_sigpwr SIGDEFARG(sigarg)
984{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000985 /* this is not required on all systems, but it doesn't hurt anybody */
986 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 /*
988 * I'm not sure we get the SIGPWR signal when the system is really going
989 * down or when the batteries are almost empty. Just preserve the swap
990 * files and don't exit, that can't do any harm.
991 */
992 ml_sync_all(FALSE, FALSE);
993 SIGRETURN;
994}
995#endif
996
997#ifdef SET_SIG_ALARM
998/*
999 * signal function for alarm().
1000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 static RETSIGTYPE
1002sig_alarm SIGDEFARG(sigarg)
1003{
1004 /* doesn't do anything, just to break a system call */
1005 sig_alarm_called = TRUE;
1006 SIGRETURN;
1007}
1008#endif
1009
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001010#if (defined(HAVE_SETJMP_H) \
1011 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1012 || defined(FEAT_LIBCALL))) \
1013 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/*
1015 * A simplistic version of setjmp() that only allows one level of using.
1016 * Don't call twice before calling mch_endjmp()!.
1017 * Usage:
1018 * mch_startjmp();
1019 * if (SETJMP(lc_jump_env) != 0)
1020 * {
1021 * mch_didjmp();
1022 * EMSG("crash!");
1023 * }
1024 * else
1025 * {
1026 * do_the_work;
1027 * mch_endjmp();
1028 * }
1029 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1030 * not return before the saved environment is used.
1031 * Returns OK for normal return, FAIL when the protected code caused a
1032 * problem and LONGJMP() was used.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037#ifdef SIGHASARG
1038 lc_signal = 0;
1039#endif
1040 lc_active = TRUE;
1041}
1042
1043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001044mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 lc_active = FALSE;
1047}
1048
1049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001050mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1053 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1054 * otherwise catching the signal only works once. */
1055 init_signal_stack();
1056# endif
1057}
1058#endif
1059
1060/*
1061 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001062 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001064 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1065 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 static RETSIGTYPE
1068deathtrap SIGDEFARG(sigarg)
1069{
1070 static int entered = 0; /* count the number of times we got here.
1071 Note: when memory has been corrupted
1072 this may get an arbitrary value! */
1073#ifdef SIGHASARG
1074 int i;
1075#endif
1076
1077#if defined(HAVE_SETJMP_H)
1078 /*
1079 * Catch a crash in protected code.
1080 * Restores the environment saved in lc_jump_env, which looks like
1081 * SETJMP() returns 1.
1082 */
1083 if (lc_active)
1084 {
1085# if defined(SIGHASARG)
1086 lc_signal = sigarg;
1087# endif
1088 lc_active = FALSE; /* don't jump again */
1089 LONGJMP(lc_jump_env, 1);
1090 /* NOTREACHED */
1091 }
1092#endif
1093
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001094#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001095# ifdef SIGQUIT
1096 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1097 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1098 * pressing CTRL-\, but we don't want Vim to exit then. */
1099 if (in_mch_delay && sigarg == SIGQUIT)
1100 SIGRETURN;
1101# endif
1102
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001103 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1104 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1105 * free(). Calling free() again may then cause a crash. */
1106 if (entered == 0
1107 && (0
1108# ifdef SIGHUP
1109 || sigarg == SIGHUP
1110# endif
1111# ifdef SIGQUIT
1112 || sigarg == SIGQUIT
1113# endif
1114# ifdef SIGTERM
1115 || sigarg == SIGTERM
1116# endif
1117# ifdef SIGPWR
1118 || sigarg == SIGPWR
1119# endif
1120# ifdef SIGUSR1
1121 || sigarg == SIGUSR1
1122# endif
1123# ifdef SIGUSR2
1124 || sigarg == SIGUSR2
1125# endif
1126 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001127 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001128 SIGRETURN;
1129#endif
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /* Remember how often we have been called. */
1132 ++entered;
1133
Bram Moolenaare429e702016-06-10 19:49:14 +02001134 /* Executing autocommands is likely to use more stack space than we have
1135 * available in the signal stack. */
1136 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_EVAL
1139 /* Set the v:dying variable. */
1140 set_vim_var_nr(VV_DYING, (long)entered);
1141#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001142 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001144#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /* Since we are now using the signal stack, need to reset the stack
1146 * limit. Otherwise using a regexp will fail. */
1147 get_stack_limit();
1148#endif
1149
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001150#if 0
1151 /* This is for opening gdb the moment Vim crashes.
1152 * You need to manually adjust the file name and Vim executable name.
1153 * Suggested by SungHyun Nam. */
1154 {
1155# define VI_GDB_FILE "/tmp/vimgdb"
1156# define VIM_NAME "/usr/bin/vim"
1157 FILE *fp = fopen(VI_GDB_FILE, "w");
1158 if (fp)
1159 {
1160 fprintf(fp,
1161 "file %s\n"
1162 "attach %d\n"
1163 "set height 1000\n"
1164 "bt full\n"
1165 , VIM_NAME, getpid());
1166 fclose(fp);
1167 system("xterm -e gdb -x "VI_GDB_FILE);
1168 unlink(VI_GDB_FILE);
1169 }
1170 }
1171#endif
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#ifdef SIGHASARG
1174 /* try to find the name of this signal */
1175 for (i = 0; signal_info[i].sig != -1; i++)
1176 if (sigarg == signal_info[i].sig)
1177 break;
1178 deadly_signal = sigarg;
1179#endif
1180
1181 full_screen = FALSE; /* don't write message to the GUI, it might be
1182 * part of the problem... */
1183 /*
1184 * If something goes wrong after entering here, we may get here again.
1185 * When this happens, give a message and try to exit nicely (resetting the
1186 * terminal mode, etc.)
1187 * When this happens twice, just exit, don't even try to give a message,
1188 * stack may be corrupt or something weird.
1189 * When this still happens again (or memory was corrupted in such a way
1190 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1191 */
1192 if (entered >= 3)
1193 {
1194 reset_signals(); /* don't catch any signals anymore */
1195 may_core_dump();
1196 if (entered >= 4)
1197 _exit(8);
1198 exit(7);
1199 }
1200 if (entered == 2)
1201 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001202 /* No translation, it may call malloc(). */
1203 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 out_flush();
1205 getout(1);
1206 }
1207
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001208 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001210 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 signal_info[i].name);
1212#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001213 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001215
1216 /* Preserve files and exit. This sets the really_exiting flag to prevent
1217 * calling free(). */
1218 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaare429e702016-06-10 19:49:14 +02001220 /* NOTREACHED */
1221
Bram Moolenaar009b2592004-10-24 19:18:58 +00001222#ifdef NBDEBUG
1223 reset_signals();
1224 may_core_dump();
1225 abort();
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 SIGRETURN;
1229}
1230
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001231#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * On Solaris with multi-threading, suspending might not work immediately.
1234 * Catch the SIGCONT signal, which will be used as an indication whether the
1235 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001236 *
1237 * On Linux, signal is not always handled immediately either.
1238 * See https://bugs.launchpad.net/bugs/291373
1239 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001240 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001242static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001243static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245/*
1246 * signal handler for SIGCONT
1247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 static RETSIGTYPE
1249sigcont_handler SIGDEFARG(sigarg)
1250{
1251 sigcont_received = TRUE;
1252 SIGRETURN;
1253}
1254#endif
1255
Bram Moolenaar62b42182010-09-21 22:09:37 +02001256# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001257static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001258# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001259static void save_clipboard(void);
1260static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001261
1262static void *clip_star_save = NULL;
1263static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001264# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001265
1266/*
1267 * Called when Vim is going to sleep or execute a shell command.
1268 * We can't respond to requests for the X selections. Lose them, otherwise
1269 * other applications will hang. But first copy the text to cut buffer 0.
1270 */
1271 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001272loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001273{
1274 if (clip_star.owned || clip_plus.owned)
1275 {
1276 x11_export_final_selection();
1277 if (clip_star.owned)
1278 clip_lose_selection(&clip_star);
1279 if (clip_plus.owned)
1280 clip_lose_selection(&clip_plus);
1281 if (x11_display != NULL)
1282 XFlush(x11_display);
1283 }
1284}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001285
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001286# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001287/*
1288 * Save clipboard text to restore later.
1289 */
1290 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001291save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001292{
1293 if (clip_star.owned)
1294 clip_star_save = get_register('*', TRUE);
1295 if (clip_plus.owned)
1296 clip_plus_save = get_register('+', TRUE);
1297}
1298
1299/*
1300 * Restore clipboard text if no one own the X selection.
1301 */
1302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001303restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001304{
1305 if (clip_star_save != NULL)
1306 {
1307 if (!clip_gen_owner_exists(&clip_star))
1308 put_register('*', clip_star_save);
1309 else
1310 free_register(clip_star_save);
1311 clip_star_save = NULL;
1312 }
1313 if (clip_plus_save != NULL)
1314 {
1315 if (!clip_gen_owner_exists(&clip_plus))
1316 put_register('+', clip_plus_save);
1317 else
1318 free_register(clip_plus_save);
1319 clip_plus_save = NULL;
1320 }
1321}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001322# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001323#endif
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325/*
1326 * If the machine has job control, use it to suspend the program,
1327 * otherwise fake it by starting a new shell.
1328 */
1329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001330mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 /* BeOS does have SIGTSTP, but it doesn't work. */
1333#if defined(SIGTSTP) && !defined(__BEOS__)
1334 out_flush(); /* needed to make cursor visible on some systems */
1335 settmode(TMODE_COOK);
1336 out_flush(); /* needed to disable mouse on some systems */
1337
1338# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001339 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340# endif
1341
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001342# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 sigcont_received = FALSE;
1344# endif
1345 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001346# if defined(_REENTRANT) && defined(SIGCONT)
1347 /*
1348 * Wait for the SIGCONT signal to be handled. It generally happens
1349 * immediately, but somehow not all the time. Do not call pause()
1350 * because there would be race condition which would hang Vim if
1351 * signal happened in between the test of sigcont_received and the
1352 * call to pause(). If signal is not yet received, call sleep(0)
1353 * to just yield CPU. Signal should then be received. If somehow
1354 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1355 * further if signal is not received after 1+2+3+4 ms (not expected
1356 * to happen).
1357 */
1358 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001359 long wait_time;
1360 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001361 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001362 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364# endif
1365
1366# ifdef FEAT_TITLE
1367 /*
1368 * Set oldtitle to NULL, so the current title is obtained again.
1369 */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001370 VIM_CLEAR(oldtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371# endif
1372 settmode(TMODE_RAW);
1373 need_check_timestamps = TRUE;
1374 did_check_timestamps = FALSE;
1375#else
1376 suspend_shell();
1377#endif
1378}
1379
1380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001381mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 Columns = 80;
1384 Rows = 24;
1385
1386 out_flush();
1387 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001388
Bram Moolenaar56718732006-03-15 22:53:57 +00001389#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001390 mac_conv_init();
1391#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001392#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1393 win_clip_init();
1394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395}
1396
1397 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001398set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400#if defined(SIGWINCH)
1401 /*
1402 * WINDOW CHANGE signal is handled with sig_winch().
1403 */
1404 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1405#endif
1406
1407 /*
1408 * We want the STOP signal to work, to make mch_suspend() work.
1409 * For "rvim" the STOP signal is ignored.
1410 */
1411#ifdef SIGTSTP
1412 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1413#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001414#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 signal(SIGCONT, sigcont_handler);
1416#endif
1417
1418 /*
1419 * We want to ignore breaking of PIPEs.
1420 */
1421#ifdef SIGPIPE
1422 signal(SIGPIPE, SIG_IGN);
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001426 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#endif
1428
1429 /*
1430 * Ignore alarm signals (Perl's alarm() generates it).
1431 */
1432#ifdef SIGALRM
1433 signal(SIGALRM, SIG_IGN);
1434#endif
1435
1436 /*
1437 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1438 * work will be lost.
1439 */
1440#ifdef SIGPWR
1441 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1442#endif
1443
1444 /*
1445 * Arrange for other signals to gracefully shutdown Vim.
1446 */
1447 catch_signals(deathtrap, SIG_ERR);
1448
1449#if defined(FEAT_GUI) && defined(SIGHUP)
1450 /*
1451 * When the GUI is running, ignore the hangup signal.
1452 */
1453 if (gui.in_use)
1454 signal(SIGHUP, SIG_IGN);
1455#endif
1456}
1457
Bram Moolenaardf177f62005-02-22 08:39:57 +00001458#if defined(SIGINT) || defined(PROTO)
1459/*
1460 * Catch CTRL-C (only works while in Cooked mode).
1461 */
1462 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001463catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001464{
1465 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1466}
1467#endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001470reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001473#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /* SIGCONT isn't in the list, because its default action is ignore */
1475 signal(SIGCONT, SIG_DFL);
1476#endif
1477}
1478
1479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001480catch_signals(
1481 RETSIGTYPE (*func_deadly)(),
1482 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 int i;
1485
1486 for (i = 0; signal_info[i].sig != -1; i++)
1487 if (signal_info[i].deadly)
1488 {
1489#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1490 struct sigaction sa;
1491
1492 /* Setup to use the alternate stack for the signal function. */
1493 sa.sa_handler = func_deadly;
1494 sigemptyset(&sa.sa_mask);
1495# if defined(__linux__) && defined(_REENTRANT)
1496 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1497 * thread handling in combination with using the alternate stack:
1498 * pthread library functions try to use the stack pointer to
1499 * identify the current thread, causing a SEGV signal, which
1500 * recursively calls deathtrap() and hangs. */
1501 sa.sa_flags = 0;
1502# else
1503 sa.sa_flags = SA_ONSTACK;
1504# endif
1505 sigaction(signal_info[i].sig, &sa, NULL);
1506#else
1507# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1508 struct sigvec sv;
1509
1510 /* Setup to use the alternate stack for the signal function. */
1511 sv.sv_handler = func_deadly;
1512 sv.sv_mask = 0;
1513 sv.sv_flags = SV_ONSTACK;
1514 sigvec(signal_info[i].sig, &sv, NULL);
1515# else
1516 signal(signal_info[i].sig, func_deadly);
1517# endif
1518#endif
1519 }
1520 else if (func_other != SIG_ERR)
1521 signal(signal_info[i].sig, func_other);
1522}
1523
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001524#ifdef HAVE_SIGPROCMASK
1525 static void
1526block_signals(sigset_t *set)
1527{
1528 sigset_t newset;
1529 int i;
1530
1531 sigemptyset(&newset);
1532
1533 for (i = 0; signal_info[i].sig != -1; i++)
1534 sigaddset(&newset, signal_info[i].sig);
1535
1536# if defined(_REENTRANT) && defined(SIGCONT)
1537 /* SIGCONT isn't in the list, because its default action is ignore */
1538 sigaddset(&newset, SIGCONT);
1539# endif
1540
1541 sigprocmask(SIG_BLOCK, &newset, set);
1542}
1543
1544 static void
1545unblock_signals(sigset_t *set)
1546{
1547 sigprocmask(SIG_SETMASK, set, NULL);
1548}
1549#endif
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001552 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001553 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1554 * return TRUE
1555 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1556 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001557 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001558 * Returns TRUE when Vim should exit.
1559 */
1560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001561vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001562{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 static int got_signal = 0;
1564 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001565
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001566 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001567 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001568 case SIGNAL_BLOCK: blocked = TRUE;
1569 break;
1570
1571 case SIGNAL_UNBLOCK: blocked = FALSE;
1572 if (got_signal != 0)
1573 {
1574 kill(getpid(), got_signal);
1575 got_signal = 0;
1576 }
1577 break;
1578
1579 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001580 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001581 got_signal = sig;
1582#ifdef SIGPWR
1583 if (sig != SIGPWR)
1584#endif
1585 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001586 break;
1587 }
1588 return FALSE;
1589}
1590
1591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * Check_win checks whether we have an interactive stdout.
1593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001595mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (isatty(1))
1598 return OK;
1599 return FAIL;
1600}
1601
1602/*
1603 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1604 */
1605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001606mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 if (isatty(read_cmd_fd))
1609 return TRUE;
1610 return FALSE;
1611}
1612
1613#ifdef FEAT_X11
1614
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001615# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
1619 * Give a message about the elapsed time for opening the X window.
1620 */
1621 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001622xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001624 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625}
1626# endif
1627#endif
1628
1629#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1630/*
1631 * A few functions shared by X11 title and clipboard code.
1632 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001633static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1634static int x_error_check(Display *dpy, XErrorEvent *error_event);
1635static int x_connect_to_server(void);
1636static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638static int got_x_error = FALSE;
1639
1640/*
1641 * X Error handler, otherwise X just exits! (very rude) -- webb
1642 */
1643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001644x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001646 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 STRCAT(IObuff, _("\nVim: Got X error\n"));
1648
1649 /* We cannot print a message and continue, because no X calls are allowed
1650 * here (causes my system to hang). Silently continuing might be an
1651 * alternative... */
1652 preserve_exit(); /* preserve files and exit */
1653
1654 return 0; /* NOTREACHED */
1655}
1656
1657/*
1658 * Another X Error handler, just used to check for errors.
1659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001661x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 got_x_error = TRUE;
1664 return 0;
1665}
1666
1667#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1668# if defined(HAVE_SETJMP_H)
1669/*
1670 * An X IO Error handler, used to catch error while opening the display.
1671 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001672static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001675x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 /* This function should not return, it causes exit(). Longjump instead. */
1678 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001679# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001680 return 0; /* avoid the compiler complains about missing return value */
1681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
1683# endif
1684
1685/*
1686 * An X IO Error handler, used to catch terminal errors.
1687 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001688static int x_IOerror_handler(Display *dpy);
1689static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001690static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001693x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001696 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 x11_window = 0;
1698 x11_display = NULL;
1699 xterm_Shell = (Widget)0;
1700
1701 /* This function should not return, it causes exit(). Longjump instead. */
1702 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001703# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001704 return 0; /* avoid the compiler complains about missing return value */
1705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001707
1708/*
1709 * If the X11 connection was lost try to restore it.
1710 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001711 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001712 */
1713 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001714may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001715{
1716 if (xterm_dpy_was_reset)
1717 {
1718 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001719
1720# ifndef LESSTIF_VERSION
1721 /* This has been reported to avoid Vim getting stuck. */
1722 if (app_context != (XtAppContext)NULL)
1723 {
1724 XtDestroyApplicationContext(app_context);
1725 app_context = (XtAppContext)NULL;
1726 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1727 }
1728# endif
1729
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001730 setup_term_clip();
1731 get_x11_title(FALSE);
1732 }
1733}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#endif
1735
1736/*
1737 * Return TRUE when connection to the X server is desired.
1738 */
1739 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001740x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#if defined(FEAT_CLIENTSERVER)
1743 if (x_force_connect)
1744 return TRUE;
1745#endif
1746 if (x_no_connect)
1747 return FALSE;
1748
1749 /* Check for a match with "exclude:" from 'clipboard'. */
1750 if (clip_exclude_prog != NULL)
1751 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001752 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 return FALSE;
1754 }
1755 return TRUE;
1756}
1757
1758/*
1759 * Test if "dpy" and x11_window are valid by getting the window title.
1760 * I don't actually want it yet, so there may be a simpler call to use, but
1761 * this will cause the error handler x_error_check() to be called if anything
1762 * is wrong, such as the window pointer being invalid (as can happen when the
1763 * user changes his DISPLAY, but not his WINDOWID) -- webb
1764 */
1765 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001766test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 int (*old_handler)();
1769 XTextProperty text_prop;
1770
1771 old_handler = XSetErrorHandler(x_error_check);
1772 got_x_error = FALSE;
1773 if (XGetWMName(dpy, x11_window, &text_prop))
1774 XFree((void *)text_prop.value);
1775 XSync(dpy, False);
1776 (void)XSetErrorHandler(old_handler);
1777
1778 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001779 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 return (got_x_error ? FAIL : OK);
1782}
1783#endif
1784
1785#ifdef FEAT_TITLE
1786
1787#ifdef FEAT_X11
1788
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001789static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791/*
1792 * try to get x11 window and display
1793 *
1794 * return FAIL for failure, OK otherwise
1795 */
1796 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001797get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 char *winid;
1800 static int result = -1;
1801#define XD_NONE 0 /* x11_display not set here */
1802#define XD_HERE 1 /* x11_display opened here */
1803#define XD_GUI 2 /* x11_display used from gui.dpy */
1804#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1805 static int x11_display_from = XD_NONE;
1806 static int did_set_error_handler = FALSE;
1807
1808 if (!did_set_error_handler)
1809 {
1810 /* X just exits if it finds an error otherwise! */
1811 (void)XSetErrorHandler(x_error_handler);
1812 did_set_error_handler = TRUE;
1813 }
1814
Bram Moolenaar9372a112005-12-06 19:59:18 +00001815#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (gui.in_use)
1817 {
1818 /*
1819 * If the X11 display was opened here before, for the window where Vim
1820 * was started, close that one now to avoid a memory leak.
1821 */
1822 if (x11_display_from == XD_HERE && x11_display != NULL)
1823 {
1824 XCloseDisplay(x11_display);
1825 x11_display_from = XD_NONE;
1826 }
1827 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1828 {
1829 x11_display_from = XD_GUI;
1830 return OK;
1831 }
1832 x11_display = NULL;
1833 return FAIL;
1834 }
1835 else if (x11_display_from == XD_GUI)
1836 {
1837 /* GUI must have stopped somehow, clear x11_display */
1838 x11_window = 0;
1839 x11_display = NULL;
1840 x11_display_from = XD_NONE;
1841 }
1842#endif
1843
1844 /* When started with the "-X" argument, don't try connecting. */
1845 if (!x_connect_to_server())
1846 return FAIL;
1847
1848 /*
1849 * If WINDOWID not set, should try another method to find out
1850 * what the current window number is. The only code I know for
1851 * this is very complicated.
1852 * We assume that zero is invalid for WINDOWID.
1853 */
1854 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1855 x11_window = (Window)atol(winid);
1856
1857#ifdef FEAT_XCLIPBOARD
1858 if (xterm_dpy != NULL && x11_window != 0)
1859 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001860 /* We may have checked it already, but Gnome terminal can move us to
1861 * another window, so we need to check every time. */
1862 if (x11_display_from != XD_XTERM)
1863 {
1864 /*
1865 * If the X11 display was opened here before, for the window where
1866 * Vim was started, close that one now to avoid a memory leak.
1867 */
1868 if (x11_display_from == XD_HERE && x11_display != NULL)
1869 XCloseDisplay(x11_display);
1870 x11_display = xterm_dpy;
1871 x11_display_from = XD_XTERM;
1872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (test_x11_window(x11_display) == FAIL)
1874 {
1875 /* probably bad $WINDOWID */
1876 x11_window = 0;
1877 x11_display = NULL;
1878 x11_display_from = XD_NONE;
1879 return FAIL;
1880 }
1881 return OK;
1882 }
1883#endif
1884
1885 if (x11_window == 0 || x11_display == NULL)
1886 result = -1;
1887
1888 if (result != -1) /* Have already been here and set this */
1889 return result; /* Don't do all these X calls again */
1890
1891 if (x11_window != 0 && x11_display == NULL)
1892 {
1893#ifdef SET_SIG_ALARM
1894 RETSIGTYPE (*sig_save)();
1895#endif
1896#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1897 struct timeval start_tv;
1898
1899 if (p_verbose > 0)
1900 gettimeofday(&start_tv, NULL);
1901#endif
1902
1903#ifdef SET_SIG_ALARM
1904 /*
1905 * Opening the Display may hang if the DISPLAY setting is wrong, or
1906 * the network connection is bad. Set an alarm timer to get out.
1907 */
1908 sig_alarm_called = FALSE;
1909 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1910 (RETSIGTYPE (*)())sig_alarm);
1911 alarm(2);
1912#endif
1913 x11_display = XOpenDisplay(NULL);
1914
1915#ifdef SET_SIG_ALARM
1916 alarm(0);
1917 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1918 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001919 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
1921 if (x11_display != NULL)
1922 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001923# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001925 {
1926 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001927 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001928 verbose_leave();
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930# endif
1931 if (test_x11_window(x11_display) == FAIL)
1932 {
1933 /* Maybe window id is bad */
1934 x11_window = 0;
1935 XCloseDisplay(x11_display);
1936 x11_display = NULL;
1937 }
1938 else
1939 x11_display_from = XD_HERE;
1940 }
1941 }
1942 if (x11_window == 0 || x11_display == NULL)
1943 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001944
1945# ifdef FEAT_EVAL
1946 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1947# endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 return (result = OK);
1950}
1951
1952/*
1953 * Determine original x11 Window Title
1954 */
1955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001956get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001958 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959}
1960
1961/*
1962 * Determine original x11 Window icon
1963 */
1964 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001965get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 int retval = FALSE;
1968
1969 retval = get_x11_thing(FALSE, test_only);
1970
1971 /* could not get old icon, use terminal name */
1972 if (oldicon == NULL && !test_only)
1973 {
1974 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001975 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001977 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979
1980 return retval;
1981}
1982
1983 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001984get_x11_thing(
1985 int get_title, /* get title string */
1986 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 XTextProperty text_prop;
1989 int retval = FALSE;
1990 Status status;
1991
1992 if (get_x11_windis() == OK)
1993 {
1994 /* Get window/icon name if any */
1995 if (get_title)
1996 status = XGetWMName(x11_display, x11_window, &text_prop);
1997 else
1998 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1999
2000 /*
2001 * If terminal is xterm, then x11_window may be a child window of the
2002 * outer xterm window that actually contains the window/icon name, so
2003 * keep traversing up the tree until a window with a title/icon is
2004 * found.
2005 */
2006 /* Previously this was only done for xterm and alikes. I don't see a
2007 * reason why it would fail for other terminal emulators.
2008 * if (term_is_xterm) */
2009 {
2010 Window root;
2011 Window parent;
2012 Window win = x11_window;
2013 Window *children;
2014 unsigned int num_children;
2015
2016 while (!status || text_prop.value == NULL)
2017 {
2018 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2019 &num_children))
2020 break;
2021 if (children)
2022 XFree((void *)children);
2023 if (parent == root || parent == 0)
2024 break;
2025
2026 win = parent;
2027 if (get_title)
2028 status = XGetWMName(x11_display, win, &text_prop);
2029 else
2030 status = XGetWMIconName(x11_display, win, &text_prop);
2031 }
2032 }
2033 if (status && text_prop.value != NULL)
2034 {
2035 retval = TRUE;
2036 if (!test_only)
2037 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002038#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2039 if (text_prop.encoding == XA_STRING
2040# ifdef FEAT_MBYTE
2041 && !has_mbyte
2042# endif
2043 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
2045#endif
2046 if (get_title)
2047 oldtitle = vim_strsave((char_u *)text_prop.value);
2048 else
2049 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002050#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 else
2053 {
2054 char **cl;
2055 Status transform_status;
2056 int n = 0;
2057
2058 transform_status = XmbTextPropertyToTextList(x11_display,
2059 &text_prop,
2060 &cl, &n);
2061 if (transform_status >= Success && n > 0 && cl[0])
2062 {
2063 if (get_title)
2064 oldtitle = vim_strsave((char_u *) cl[0]);
2065 else
2066 oldicon = vim_strsave((char_u *) cl[0]);
2067 XFreeStringList(cl);
2068 }
2069 else
2070 {
2071 if (get_title)
2072 oldtitle = vim_strsave((char_u *)text_prop.value);
2073 else
2074 oldicon = vim_strsave((char_u *)text_prop.value);
2075 }
2076 }
2077#endif
2078 }
2079 XFree((void *)text_prop.value);
2080 }
2081 }
2082 return retval;
2083}
2084
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002085/* Xutf8 functions are not avaialble on older systems. Note that on some
2086 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2087 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2088 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002090# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091# define USE_UTF8_STRING
2092# endif
2093#endif
2094
2095/*
2096 * Set x11 Window Title
2097 *
2098 * get_x11_windis() must be called before this and have returned OK
2099 */
2100 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002101set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2104 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2105 * supported everywhere and STRING doesn't work for multi-byte titles.
2106 */
2107#ifdef USE_UTF8_STRING
2108 if (enc_utf8)
2109 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2110 NULL, NULL, 0, NULL, NULL, NULL);
2111 else
2112#endif
2113 {
2114#if XtSpecificationRelease >= 4
2115# ifdef FEAT_XFONTSET
2116 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2117 NULL, NULL, 0, NULL, NULL, NULL);
2118# else
2119 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002120 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002123 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 XSetWMProperties(x11_display, x11_window, &text_prop,
2125 NULL, NULL, 0, NULL, NULL, NULL);
2126# endif
2127#else
2128 XStoreName(x11_display, x11_window, (char *)title);
2129#endif
2130 }
2131 XFlush(x11_display);
2132}
2133
2134/*
2135 * Set x11 Window icon
2136 *
2137 * get_x11_windis() must be called before this and have returned OK
2138 */
2139 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002140set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 /* See above for comments about using X*SetWMProperties(). */
2143#ifdef USE_UTF8_STRING
2144 if (enc_utf8)
2145 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2146 NULL, 0, NULL, NULL, NULL);
2147 else
2148#endif
2149 {
2150#if XtSpecificationRelease >= 4
2151# ifdef FEAT_XFONTSET
2152 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2153 NULL, 0, NULL, NULL, NULL);
2154# else
2155 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002156 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002158 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2160 NULL, 0, NULL, NULL, NULL);
2161# endif
2162#else
2163 XSetIconName(x11_display, x11_window, (char *)icon);
2164#endif
2165 }
2166 XFlush(x11_display);
2167}
2168
2169#else /* FEAT_X11 */
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002172get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 return FALSE;
2175}
2176
2177 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002178get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 if (!test_only)
2181 {
2182 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002183 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002185 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 return FALSE;
2188}
2189
2190#endif /* FEAT_X11 */
2191
2192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002193mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 return get_x11_title(TRUE);
2196}
2197
2198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002199mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 return get_x11_icon(TRUE);
2202}
2203
2204/*
2205 * Set the window title and icon.
2206 */
2207 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002208mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 int type = 0;
2211 static int recursive = 0;
2212
2213 if (T_NAME == NULL) /* no terminal name (yet) */
2214 return;
2215 if (title == NULL && icon == NULL) /* nothing to do */
2216 return;
2217
2218 /* When one of the X11 functions causes a deadly signal, we get here again
2219 * recursively. Avoid hanging then (something is probably locked). */
2220 if (recursive)
2221 return;
2222 ++recursive;
2223
2224 /*
2225 * if the window ID and the display is known, we may use X11 calls
2226 */
2227#ifdef FEAT_X11
2228 if (get_x11_windis() == OK)
2229 type = 1;
2230#else
2231# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2232 if (gui.in_use)
2233 type = 1;
2234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#endif
2236
2237 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002238 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 * than x11 calls, because the x11 calls don't always work
2240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if ((type || *T_TS != NUL) && title != NULL)
2242 {
2243 if (oldtitle == NULL
2244#ifdef FEAT_GUI
2245 && !gui.in_use
2246#endif
2247 ) /* first call but not in GUI, save title */
2248 (void)get_x11_title(FALSE);
2249
2250 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2251 term_settitle(title);
2252#ifdef FEAT_X11
2253 else
2254# ifdef FEAT_GUI_GTK
2255 if (!gui.in_use) /* don't do this if GTK+ is running */
2256# endif
2257 set_x11_title(title); /* x11 */
2258#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002259#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2261 else
2262 gui_mch_settitle(title, icon);
2263#endif
2264 did_set_title = TRUE;
2265 }
2266
2267 if ((type || *T_CIS != NUL) && icon != NULL)
2268 {
2269 if (oldicon == NULL
2270#ifdef FEAT_GUI
2271 && !gui.in_use
2272#endif
2273 ) /* first call, save icon */
2274 get_x11_icon(FALSE);
2275
2276 if (*T_CIS != NUL)
2277 {
2278 out_str(T_CIS); /* set icon start */
2279 out_str_nf(icon);
2280 out_str(T_CIE); /* set icon end */
2281 out_flush();
2282 }
2283#ifdef FEAT_X11
2284 else
2285# ifdef FEAT_GUI_GTK
2286 if (!gui.in_use) /* don't do this if GTK+ is running */
2287# endif
2288 set_x11_icon(icon); /* x11 */
2289#endif
2290 did_set_icon = TRUE;
2291 }
2292 --recursive;
2293}
2294
2295/*
2296 * Restore the window/icon title.
2297 * "which" is one of:
2298 * 1 only restore title
2299 * 2 only restore icon
2300 * 3 restore title and icon
2301 */
2302 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002303mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 /* only restore the title or icon when it has been set */
2306 mch_settitle(((which & 1) && did_set_title) ?
2307 (oldtitle ? oldtitle : p_titleold) : NULL,
2308 ((which & 2) && did_set_icon) ? oldicon : NULL);
2309}
2310
2311#endif /* FEAT_TITLE */
2312
2313/*
2314 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002315 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
2317 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002318vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 if (name == NULL)
2321 return FALSE;
2322 return (STRNICMP(name, "xterm", 5) == 0
2323 || STRNICMP(name, "nxterm", 6) == 0
2324 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002325 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002327 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 || STRCMP(name, "builtin_xterm") == 0);
2329}
2330
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002331#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2332/*
2333 * Return TRUE if "name" appears to be that of a terminal
2334 * known to support the xterm-style mouse protocol.
2335 * Relies on term_is_xterm having been set to its correct value.
2336 */
2337 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002338use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002339{
2340 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002341 && (term_is_xterm
2342 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002343 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002344 || STRICMP(name, "st") == 0
2345 || STRNICMP(name, "st-", 3) == 0
2346 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002347}
2348#endif
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2351/*
2352 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2353 * Return 1 for "xterm".
2354 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002355 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002356 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 */
2358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002359use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002361 if (ttym_flags == TTYM_SGR)
2362 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002363 if (ttym_flags == TTYM_URXVT)
2364 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (ttym_flags == TTYM_XTERM2)
2366 return 2;
2367 if (ttym_flags == TTYM_XTERM)
2368 return 1;
2369 return 0;
2370}
2371#endif
2372
2373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002374vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
2376 if (name == NULL)
2377 return FALSE;
2378 return (STRNICMP(name, "iris-ansi", 9) == 0
2379 || STRCMP(name, "builtin_iris-ansi") == 0);
2380}
2381
2382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002383vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 if (name == NULL)
2386 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002387 /* catch VT100 - VT5xx */
2388 return ((STRNICMP(name, "vt", 2) == 0
2389 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 || STRCMP(name, "builtin_vt320") == 0);
2391}
2392
2393/*
2394 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2395 * This should include all windowed terminal emulators.
2396 */
2397 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002398vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 if (name == NULL)
2401 return FALSE;
2402 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2403 return TRUE;
2404 return ( STRNICMP(name, "hpterm", 6) == 0
2405 || STRNICMP(name, "sun-cmd", 7) == 0
2406 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002407 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 || STRNICMP(name, "dtterm", 6) == 0);
2409}
2410
2411/*
2412 * Insert user name in s[len].
2413 * Return OK if a name found.
2414 */
2415 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002416mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002419 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return OK;
2421#else
2422 return mch_get_uname(getuid(), s, len);
2423#endif
2424}
2425
2426/*
2427 * Insert user name for "uid" in s[len].
2428 * Return OK if a name found.
2429 */
2430 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002431mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2434 struct passwd *pw;
2435
2436 if ((pw = getpwuid(uid)) != NULL
2437 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2438 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002439 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return OK;
2441 }
2442#endif
2443 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2444 return FAIL; /* a number is not a name */
2445}
2446
2447/*
2448 * Insert host name is s[len].
2449 */
2450
2451#ifdef HAVE_SYS_UTSNAME_H
2452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002453mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 struct utsname vutsname;
2456
2457 if (uname(&vutsname) < 0)
2458 *s = NUL;
2459 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002460 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461}
2462#else /* HAVE_SYS_UTSNAME_H */
2463
2464# ifdef HAVE_SYS_SYSTEMINFO_H
2465# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2466# endif
2467
2468 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002469mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471# ifdef VAXC
2472 vaxc$gethostname((char *)s, len);
2473# else
2474 gethostname((char *)s, len);
2475# endif
2476 s[len - 1] = NUL; /* make sure it's terminated */
2477}
2478#endif /* HAVE_SYS_UTSNAME_H */
2479
2480/*
2481 * return process ID
2482 */
2483 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002484mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 return (long)getpid();
2487}
2488
2489#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002490static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002493strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
2495 extern int sys_nerr;
2496 extern char *sys_errlist[];
2497 static char er[20];
2498
2499 if (err > 0 && err < sys_nerr)
2500 return (sys_errlist[err]);
2501 sprintf(er, "Error %d", err);
2502 return er;
2503}
2504#endif
2505
2506/*
2507 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2508 * Return OK for success, FAIL for failure.
2509 */
2510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002511mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513#if defined(USE_GETCWD)
2514 if (getcwd((char *)buf, len) == NULL)
2515 {
2516 STRCPY(buf, strerror(errno));
2517 return FAIL;
2518 }
2519 return OK;
2520#else
2521 return (getwd((char *)buf) != NULL ? OK : FAIL);
2522#endif
2523}
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002526 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 *
2528 * return FAIL for failure, OK for success
2529 */
2530 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002531mch_FullName(
2532 char_u *fname,
2533 char_u *buf,
2534 int len,
2535 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002538#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 int fd = -1;
2540 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 char_u olddir[MAXPATHL];
2543 char_u *p;
2544 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002545#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002546 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2547 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002548#endif
2549
Bram Moolenaar38323e42007-03-06 19:22:53 +00002550#ifdef VMS
2551 fname = vms_fixfilename(fname);
2552#endif
2553
Bram Moolenaara2442432007-04-26 14:26:37 +00002554#ifdef __CYGWIN__
2555 /*
2556 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2557 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002558# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002559 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2560 * a forward slash. */
2561 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2562 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002563# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002564 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002565# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002566 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002567#endif
2568
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002569 /* Expand it if forced or not an absolute path.
2570 * Do not do it for "/file", the result is always "/". */
2571 if ((force || !mch_isFullName(fname))
2572 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /*
2575 * If the file name has a path, change to that directory for a moment,
2576 * and then do the getwd() (and get back to where we were).
2577 * This will get the correct path name with "../" things.
2578 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002579 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002581#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * Use fchdir() if possible, it's said to be faster and more
2584 * reliable. But on SunOS 4 it might not work. Check this by
2585 * doing a fchdir() right now.
2586 */
2587 if (!dont_fchdir)
2588 {
2589 fd = open(".", O_RDONLY | O_EXTRA, 0);
2590 if (fd >= 0 && fchdir(fd) < 0)
2591 {
2592 close(fd);
2593 fd = -1;
2594 dont_fchdir = TRUE; /* don't try again */
2595 }
2596 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 /* Only change directory when we are sure we can return to where
2600 * we are now. After doing "su" chdir(".") might not work. */
2601 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002602#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 (mch_dirname(olddir, MAXPATHL) == FAIL
2606 || mch_chdir((char *)olddir) != 0))
2607 {
2608 p = NULL; /* can't get current dir: don't chdir */
2609 retval = FAIL;
2610 }
2611 else
2612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 /* The directory is copied into buf[], to be able to remove
2614 * the file name without changing it (could be a string in
2615 * read-only memory) */
2616 if (p - fname >= len)
2617 retval = FAIL;
2618 else
2619 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002620 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (mch_chdir((char *)buf))
2622 retval = FAIL;
2623 else
2624 fname = p + 1;
2625 *buf = NUL;
2626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628 }
2629 if (mch_dirname(buf, len) == FAIL)
2630 {
2631 retval = FAIL;
2632 *buf = NUL;
2633 }
2634 if (p != NULL)
2635 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002636#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 if (fd >= 0)
2638 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002639 if (p_verbose >= 5)
2640 {
2641 verbose_enter();
2642 MSG("fchdir() to previous dir");
2643 verbose_leave();
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 l = fchdir(fd);
2646 close(fd);
2647 }
2648 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 l = mch_chdir((char *)olddir);
2651 if (l != 0)
2652 EMSG(_(e_prev_dir));
2653 }
2654
2655 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002656 if (l >= len - 1)
2657 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002658#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002659 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002661 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002666 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 return FAIL;
2668
2669 /* Do not append ".", "/dir/." is equal to "/dir". */
2670 if (STRCMP(fname, ".") != 0)
2671 STRCAT(buf, fname);
2672
2673 return OK;
2674}
2675
2676/*
2677 * Return TRUE if "fname" does not depend on the current directory.
2678 */
2679 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002680mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002682#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 return ( fname[0] == '/' || fname[0] == '.' ||
2684 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2685 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2686 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002687#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689#endif
2690}
2691
Bram Moolenaar24552be2005-12-10 20:17:30 +00002692#if defined(USE_FNAME_CASE) || defined(PROTO)
2693/*
2694 * Set the case of the file name, if it already exists. This will cause the
2695 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002696 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002697 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002698 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002699fname_case(
2700 char_u *name,
2701 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002702{
2703 struct stat st;
2704 char_u *slash, *tail;
2705 DIR *dirp;
2706 struct dirent *dp;
2707
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002708 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002709 {
2710 /* Open the directory where the file is located. */
2711 slash = vim_strrchr(name, '/');
2712 if (slash == NULL)
2713 {
2714 dirp = opendir(".");
2715 tail = name;
2716 }
2717 else
2718 {
2719 *slash = NUL;
2720 dirp = opendir((char *)name);
2721 *slash = '/';
2722 tail = slash + 1;
2723 }
2724
2725 if (dirp != NULL)
2726 {
2727 while ((dp = readdir(dirp)) != NULL)
2728 {
2729 /* Only accept names that differ in case and are the same byte
2730 * length. TODO: accept different length name. */
2731 if (STRICMP(tail, dp->d_name) == 0
2732 && STRLEN(tail) == STRLEN(dp->d_name))
2733 {
2734 char_u newname[MAXPATHL + 1];
2735 struct stat st2;
2736
2737 /* Verify the inode is equal. */
2738 vim_strncpy(newname, name, MAXPATHL);
2739 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2740 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002741 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002742 && st.st_ino == st2.st_ino
2743 && st.st_dev == st2.st_dev)
2744 {
2745 STRCPY(tail, dp->d_name);
2746 break;
2747 }
2748 }
2749 }
2750
2751 closedir(dirp);
2752 }
2753 }
2754}
2755#endif
2756
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757/*
2758 * Get file permissions for 'name'.
2759 * Returns -1 when it doesn't exist.
2760 */
2761 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002762mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 struct stat statb;
2765
2766 /* Keep the #ifdef outside of stat(), it may be a macro. */
2767#ifdef VMS
2768 if (stat((char *)vms_fixfilename(name), &statb))
2769#else
2770 if (stat((char *)name, &statb))
2771#endif
2772 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002773#ifdef __INTERIX
2774 /* The top bit makes the value negative, which means the file doesn't
2775 * exist. Remove the bit, we don't use it. */
2776 return statb.st_mode & ~S_ADDACE;
2777#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
2782/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002783 * Set file permission for "name" to "perm".
2784 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 */
2786 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002787mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 return (chmod((char *)
2790#ifdef VMS
2791 vms_fixfilename(name),
2792#else
2793 name,
2794#endif
2795 (mode_t)perm) == 0 ? OK : FAIL);
2796}
2797
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002798#if defined(HAVE_FCHMOD) || defined(PROTO)
2799/*
2800 * Set file permission for open file "fd" to "perm".
2801 * Return FAIL for failure, OK otherwise.
2802 */
2803 int
2804mch_fsetperm(int fd, long perm)
2805{
2806 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2807}
2808#endif
2809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810#if defined(HAVE_ACL) || defined(PROTO)
2811# ifdef HAVE_SYS_ACL_H
2812# include <sys/acl.h>
2813# endif
2814# ifdef HAVE_SYS_ACCESS_H
2815# include <sys/access.h>
2816# endif
2817
2818# ifdef HAVE_SOLARIS_ACL
2819typedef struct vim_acl_solaris_T {
2820 int acl_cnt;
2821 aclent_t *acl_entry;
2822} vim_acl_solaris_T;
2823# endif
2824
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002825#if defined(HAVE_SELINUX) || defined(PROTO)
2826/*
2827 * Copy security info from "from_file" to "to_file".
2828 */
2829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002830mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002831{
2832 if (from_file == NULL)
2833 return;
2834
2835 if (selinux_enabled == -1)
2836 selinux_enabled = is_selinux_enabled();
2837
2838 if (selinux_enabled > 0)
2839 {
2840 security_context_t from_context = NULL;
2841 security_context_t to_context = NULL;
2842
2843 if (getfilecon((char *)from_file, &from_context) < 0)
2844 {
2845 /* If the filesystem doesn't support extended attributes,
2846 the original had no special security context and the
2847 target cannot have one either. */
2848 if (errno == EOPNOTSUPP)
2849 return;
2850
2851 MSG_PUTS(_("\nCould not get security context for "));
2852 msg_outtrans(from_file);
2853 msg_putchar('\n');
2854 return;
2855 }
2856 if (getfilecon((char *)to_file, &to_context) < 0)
2857 {
2858 MSG_PUTS(_("\nCould not get security context for "));
2859 msg_outtrans(to_file);
2860 msg_putchar('\n');
2861 freecon (from_context);
2862 return ;
2863 }
2864 if (strcmp(from_context, to_context) != 0)
2865 {
2866 if (setfilecon((char *)to_file, from_context) < 0)
2867 {
2868 MSG_PUTS(_("\nCould not set security context for "));
2869 msg_outtrans(to_file);
2870 msg_putchar('\n');
2871 }
2872 }
2873 freecon(to_context);
2874 freecon(from_context);
2875 }
2876}
2877#endif /* HAVE_SELINUX */
2878
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002879#if defined(HAVE_SMACK) && !defined(PROTO)
2880/*
2881 * Copy security info from "from_file" to "to_file".
2882 */
2883 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002884mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002885{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002886 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002887 {
2888 XATTR_NAME_SMACK,
2889 XATTR_NAME_SMACKEXEC,
2890 XATTR_NAME_SMACKMMAP
2891 };
2892
2893 char buffer[SMACK_LABEL_LEN];
2894 const char *name;
2895 int index;
2896 int ret;
2897 ssize_t size;
2898
2899 if (from_file == NULL)
2900 return;
2901
2902 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2903 / sizeof(smack_copied_attributes)[0]) ; index++)
2904 {
2905 /* get the name of the attribute to copy */
2906 name = smack_copied_attributes[index];
2907
2908 /* get the value of the attribute in buffer */
2909 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2910 if (size >= 0)
2911 {
2912 /* copy the attribute value of buffer */
2913 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2914 if (ret < 0)
2915 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002916 vim_snprintf((char *)IObuff, IOSIZE,
2917 _("Could not set security context %s for %s"),
2918 name, to_file);
2919 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002920 msg_putchar('\n');
2921 }
2922 }
2923 else
2924 {
2925 /* what reason of not having the attribute value? */
2926 switch (errno)
2927 {
2928 case ENOTSUP:
2929 /* extended attributes aren't supported or enabled */
2930 /* should a message be echoed? not sure... */
2931 return; /* leave because it isn't usefull to continue */
2932
2933 case ERANGE:
2934 default:
2935 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002936 vim_snprintf((char *)IObuff, IOSIZE,
2937 _("Could not get security context %s for %s. Removing it!"),
2938 name, from_file);
2939 msg_puts(IObuff);
2940 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002941 /* FALLTHROUGH to remove the attribute */
2942
2943 case ENODATA:
2944 /* no attribute of this name */
2945 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002946 /* Silently ignore errors, apparently this happens when
2947 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002948 break;
2949 }
2950 }
2951 }
2952}
2953#endif /* HAVE_SMACK */
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955/*
2956 * Return a pointer to the ACL of file "fname" in allocated memory.
2957 * Return NULL if the ACL is not available for whatever reason.
2958 */
2959 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002960mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 vim_acl_T ret = NULL;
2963#ifdef HAVE_POSIX_ACL
2964 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2965#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002966#ifdef HAVE_SOLARIS_ZFS_ACL
2967 acl_t *aclent;
2968
2969 if (acl_get((char *)fname, 0, &aclent) < 0)
2970 return NULL;
2971 ret = (vim_acl_T)aclent;
2972#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#ifdef HAVE_SOLARIS_ACL
2974 vim_acl_solaris_T *aclent;
2975
2976 aclent = malloc(sizeof(vim_acl_solaris_T));
2977 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2978 {
2979 free(aclent);
2980 return NULL;
2981 }
2982 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2983 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2984 {
2985 free(aclent->acl_entry);
2986 free(aclent);
2987 return NULL;
2988 }
2989 ret = (vim_acl_T)aclent;
2990#else
2991#if defined(HAVE_AIX_ACL)
2992 int aclsize;
2993 struct acl *aclent;
2994
2995 aclsize = sizeof(struct acl);
2996 aclent = malloc(aclsize);
2997 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2998 {
2999 if (errno == ENOSPC)
3000 {
3001 aclsize = aclent->acl_len;
3002 aclent = realloc(aclent, aclsize);
3003 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3004 {
3005 free(aclent);
3006 return NULL;
3007 }
3008 }
3009 else
3010 {
3011 free(aclent);
3012 return NULL;
3013 }
3014 }
3015 ret = (vim_acl_T)aclent;
3016#endif /* HAVE_AIX_ACL */
3017#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003018#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#endif /* HAVE_POSIX_ACL */
3020 return ret;
3021}
3022
3023/*
3024 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3025 */
3026 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003027mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 if (aclent == NULL)
3030 return;
3031#ifdef HAVE_POSIX_ACL
3032 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3033#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003034#ifdef HAVE_SOLARIS_ZFS_ACL
3035 acl_set((char *)fname, (acl_t *)aclent);
3036#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037#ifdef HAVE_SOLARIS_ACL
3038 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3039 ((vim_acl_solaris_T *)aclent)->acl_entry);
3040#else
3041#ifdef HAVE_AIX_ACL
3042 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3043#endif /* HAVE_AIX_ACL */
3044#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003045#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif /* HAVE_POSIX_ACL */
3047}
3048
3049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003050mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (aclent == NULL)
3053 return;
3054#ifdef HAVE_POSIX_ACL
3055 acl_free((acl_t)aclent);
3056#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003057#ifdef HAVE_SOLARIS_ZFS_ACL
3058 acl_free((acl_t *)aclent);
3059#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#ifdef HAVE_SOLARIS_ACL
3061 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3062 free(aclent);
3063#else
3064#ifdef HAVE_AIX_ACL
3065 free(aclent);
3066#endif /* HAVE_AIX_ACL */
3067#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003068#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#endif /* HAVE_POSIX_ACL */
3070}
3071#endif
3072
3073/*
3074 * Set hidden flag for "name".
3075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003077mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 /* can't hide a file */
3080}
3081
3082/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003083 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * return FALSE if "name" is not a directory
3085 * return FALSE for error
3086 */
3087 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003088mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 struct stat statb;
3091
3092 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3093 return FALSE;
3094 if (stat((char *)name, &statb))
3095 return FALSE;
3096#ifdef _POSIX_SOURCE
3097 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3098#else
3099 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3100#endif
3101}
3102
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003103/*
3104 * return TRUE if "name" is a directory, NOT a symlink to a directory
3105 * return FALSE if "name" is not a directory
3106 * return FALSE for error
3107 */
3108 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003109mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003110{
3111 struct stat statb;
3112
3113 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3114 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003115 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003116 return FALSE;
3117#ifdef _POSIX_SOURCE
3118 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3119#else
3120 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3121#endif
3122}
3123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003124static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126/*
3127 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3128 */
3129 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003130executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 struct stat st;
3133
3134 if (stat((char *)name, &st))
3135 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003136#ifdef VMS
3137 /* Like on Unix system file can have executable rights but not necessarily
3138 * be an executable, but on Unix is not a default for an ordianry file to
3139 * have an executable flag - on VMS it is in most cases.
3140 * Therefore, this check does not have any sense - let keep us to the
3141 * conventions instead:
3142 * *.COM and *.EXE files are the executables - the rest are not. This is
3143 * not ideal but better then it was.
3144 */
3145 int vms_executable = 0;
3146 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3147 {
3148 if (strstr(vms_tolower((char*)name),".exe") != NULL
3149 || strstr(vms_tolower((char*)name),".com")!= NULL)
3150 vms_executable = 1;
3151 }
3152 return vms_executable;
3153#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003159 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003160 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * Return -1 if unknown.
3162 */
3163 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003164mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 char_u *buf;
3167 char_u *p, *e;
3168 int retval;
3169
Bram Moolenaarb5971142015-03-21 17:32:19 +01003170 /* When "use_path" is false and if it's an absolute or relative path don't
3171 * need to use $PATH. */
3172 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3173 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003174 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003175 /* There must be a path separator, files in the current directory
3176 * can't be executed. */
3177 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003178 {
3179 if (path != NULL)
3180 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003181 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003182 *path = FullName_save(name, TRUE);
3183 else
3184 *path = vim_strsave(name);
3185 }
3186 return TRUE;
3187 }
3188 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191 p = (char_u *)getenv("PATH");
3192 if (p == NULL || *p == NUL)
3193 return -1;
3194 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3195 if (buf == NULL)
3196 return -1;
3197
3198 /*
3199 * Walk through all entries in $PATH to check if "name" exists there and
3200 * is an executable file.
3201 */
3202 for (;;)
3203 {
3204 e = (char_u *)strchr((char *)p, ':');
3205 if (e == NULL)
3206 e = p + STRLEN(p);
3207 if (e - p <= 1) /* empty entry means current dir */
3208 STRCPY(buf, "./");
3209 else
3210 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003211 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 add_pathsep(buf);
3213 }
3214 STRCAT(buf, name);
3215 retval = executable_file(buf);
3216 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003217 {
3218 if (path != NULL)
3219 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003220 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003221 *path = FullName_save(buf, TRUE);
3222 else
3223 *path = vim_strsave(buf);
3224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 if (*e != ':')
3229 break;
3230 p = e + 1;
3231 }
3232
3233 vim_free(buf);
3234 return retval;
3235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237/*
3238 * Check what "name" is:
3239 * NODE_NORMAL: file or directory (or doesn't exist)
3240 * NODE_WRITABLE: writable device, socket, fifo, etc.
3241 * NODE_OTHER: non-writable things
3242 */
3243 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003244mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 struct stat st;
3247
3248 if (stat((char *)name, &st))
3249 return NODE_NORMAL;
3250 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3251 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3253 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 /* Everything else is writable? */
3255 return NODE_WRITABLE;
3256}
3257
3258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003259mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261#ifdef HAVE_CHECK_STACK_GROWTH
3262 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 check_stack_growth((char *)&i);
3265
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003266# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 get_stack_limit();
3268# endif
3269
3270#endif
3271
3272 /*
3273 * Setup an alternative stack for signals. Helps to catch signals when
3274 * running out of stack space.
3275 * Use of sigaltstack() is preferred, it's more portable.
3276 * Ignore any errors.
3277 */
3278#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003279 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 init_signal_stack();
3281#endif
3282}
3283
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003284#if defined(EXITFREE) || defined(PROTO)
3285 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003286mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003287{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003288# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3289 if (clip_star.owned)
3290 clip_lose_selection(&clip_star);
3291 if (clip_plus.owned)
3292 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003293# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003294# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003295 if (xterm_Shell != (Widget)0)
3296 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003297# ifndef LESSTIF_VERSION
3298 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003299 if (xterm_dpy != NULL)
3300 XtCloseDisplay(xterm_dpy);
3301 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003302 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003303 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003304# ifdef FEAT_X11
3305 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3306# endif
3307 }
3308# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003309# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003310# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003311 if (x11_display != NULL
3312# ifdef FEAT_XCLIPBOARD
3313 && x11_display != xterm_dpy
3314# endif
3315 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 XCloseDisplay(x11_display);
3317# endif
3318# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003319 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320# endif
3321# ifdef FEAT_TITLE
3322 vim_free(oldtitle);
3323 vim_free(oldicon);
3324# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003325}
3326#endif
3327
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003328static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
3330/*
3331 * Output a newline when exiting.
3332 * Make sure the newline goes to the same stream as the text.
3333 */
3334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003335exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003337 if (silent_mode)
3338 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (newline_on_exit || msg_didout)
3340 {
3341 if (msg_use_printf())
3342 {
3343 if (info_message)
3344 mch_msg("\n");
3345 else
3346 mch_errmsg("\r\n");
3347 }
3348 else
3349 out_char('\n');
3350 }
3351 else
3352 {
3353 restore_cterm_colors(); /* get original colors back */
3354 msg_clr_eos_force(); /* clear the rest of the display */
3355 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3356 }
3357}
3358
3359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003360mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 exiting = TRUE;
3363
3364#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3365 x11_export_final_selection();
3366#endif
3367
3368#ifdef FEAT_GUI
3369 if (!gui.in_use)
3370#endif
3371 {
3372 settmode(TMODE_COOK);
3373#ifdef FEAT_TITLE
3374 mch_restore_title(3); /* restore xterm title and icon name */
3375#endif
3376 /*
3377 * When t_ti is not empty but it doesn't cause swapping terminal
3378 * pages, need to output a newline when msg_didout is set. But when
3379 * t_ti does swap pages it should not go to the shell page. Do this
3380 * before stoptermcap().
3381 */
3382 if (swapping_screen() && !newline_on_exit)
3383 exit_scroll();
3384
3385 /* Stop termcap: May need to check for T_CRV response, which
3386 * requires RAW mode. */
3387 stoptermcap();
3388
3389 /*
3390 * A newline is only required after a message in the alternate screen.
3391 * This is set to TRUE by wait_return().
3392 */
3393 if (!swapping_screen() || newline_on_exit)
3394 exit_scroll();
3395
3396 /* Cursor may have been switched off without calling starttermcap()
3397 * when doing "vim -u vimrc" and vimrc contains ":q". */
3398 if (full_screen)
3399 cursor_on();
3400 }
3401 out_flush();
3402 ml_close_all(TRUE); /* remove all memfiles */
3403 may_core_dump();
3404#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 gui_exit(r);
3407#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003408
Bram Moolenaar56718732006-03-15 22:53:57 +00003409#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003410 mac_conv_cleanup();
3411#endif
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#ifdef __QNX__
3414 /* A core dump won't be created if the signal handler
3415 * doesn't return, so we can't call exit() */
3416 if (deadly_signal != 0)
3417 return;
3418#endif
3419
Bram Moolenaar009b2592004-10-24 19:18:58 +00003420#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003421 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003422#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003423
3424#ifdef EXITFREE
3425 free_all_mem();
3426#endif
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 exit(r);
3429}
3430
3431 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003432may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 if (deadly_signal != 0)
3435 {
3436 signal(deadly_signal, SIG_DFL);
3437 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3438 }
3439}
3440
3441#ifndef VMS
3442
3443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003444mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 static int first = TRUE;
3447
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003448#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449# ifdef HAVE_TERMIOS_H
3450 static struct termios told;
3451 struct termios tnew;
3452# else
3453 static struct termio told;
3454 struct termio tnew;
3455# endif
3456
3457 if (first)
3458 {
3459 first = FALSE;
3460# if defined(HAVE_TERMIOS_H)
3461 tcgetattr(read_cmd_fd, &told);
3462# else
3463 ioctl(read_cmd_fd, TCGETA, &told);
3464# endif
3465 }
3466
3467 tnew = told;
3468 if (tmode == TMODE_RAW)
3469 {
3470 /*
3471 * ~ICRNL enables typing ^V^M
3472 */
3473 tnew.c_iflag &= ~ICRNL;
3474 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3475# if defined(IEXTEN) && !defined(__MINT__)
3476 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3477 /* but it breaks function keys on MINT */
3478# endif
3479 );
3480# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3481 tnew.c_oflag &= ~ONLCR;
3482# endif
3483 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3484 tnew.c_cc[VTIME] = 0; /* don't wait */
3485 }
3486 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003487 {
3488 /* Also reset ICANON here, otherwise on Solaris select() won't see
3489 * typeahead characters. */
3490 tnew.c_lflag &= ~(ICANON | ECHO);
3491 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3492 tnew.c_cc[VTIME] = 0; /* don't wait */
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495# if defined(HAVE_TERMIOS_H)
3496 {
3497 int n = 10;
3498
3499 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3500 * few times. */
3501 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3502 && errno == EINTR && n > 0)
3503 --n;
3504 }
3505# else
3506 ioctl(read_cmd_fd, TCSETA, &tnew);
3507# endif
3508
3509#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /*
3511 * for "old" tty systems
3512 */
3513# ifndef TIOCSETN
3514# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3515# endif
3516 static struct sgttyb ttybold;
3517 struct sgttyb ttybnew;
3518
3519 if (first)
3520 {
3521 first = FALSE;
3522 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3523 }
3524
3525 ttybnew = ttybold;
3526 if (tmode == TMODE_RAW)
3527 {
3528 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3529 ttybnew.sg_flags |= RAW;
3530 }
3531 else if (tmode == TMODE_SLEEP)
3532 ttybnew.sg_flags &= ~(ECHO);
3533 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3534#endif
3535 curr_tmode = tmode;
3536}
3537
3538/*
3539 * Try to get the code for "t_kb" from the stty setting
3540 *
3541 * Even if termcap claims a backspace key, the user's setting *should*
3542 * prevail. stty knows more about reality than termcap does, and if
3543 * somebody's usual erase key is DEL (which, for most BSD users, it will
3544 * be), they're going to get really annoyed if their erase key starts
3545 * doing forward deletes for no reason. (Eric Fischer)
3546 */
3547 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003548get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003550 ttyinfo_T info;
3551 char_u buf[2];
3552 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003554 if (get_tty_info(read_cmd_fd, &info) == OK)
3555 {
3556 intr_char = info.interrupt;
3557 buf[0] = info.backspace;
3558 buf[1] = NUL;
3559 add_termcode((char_u *)"kb", buf, FALSE);
3560
3561 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3562 p = find_termcode((char_u *)"kD");
3563 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3564 do_fixdel(NULL);
3565 }
3566}
3567
3568/*
3569 * Obtain the characters that Backspace and Enter produce on "fd".
3570 * Returns OK or FAIL.
3571 */
3572 int
3573get_tty_info(int fd, ttyinfo_T *info)
3574{
3575#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576# ifdef HAVE_TERMIOS_H
3577 struct termios keys;
3578# else
3579 struct termio keys;
3580# endif
3581
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003582 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003584 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003586 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003588 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003590 info->backspace = keys.c_cc[VERASE];
3591 info->interrupt = keys.c_cc[VINTR];
3592 if (keys.c_iflag & ICRNL)
3593 info->enter = NL;
3594 else
3595 info->enter = CAR;
3596 if (keys.c_oflag & ONLCR)
3597 info->nl_does_cr = TRUE;
3598 else
3599 info->nl_does_cr = FALSE;
3600 return OK;
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#else
3603 /* for "old" tty systems */
3604 struct sgttyb keys;
3605
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003606 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003608 info->backspace = keys.sg_erase;
3609 info->interrupt = keys.sg_kill;
3610 info->enter = CAR;
3611 info->nl_does_cr = TRUE;
3612 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616}
3617
3618#endif /* VMS */
3619
3620#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003621static int mouse_ison = FALSE;
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
3624 * Set mouse clicks on or off.
3625 */
3626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003627mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003629# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003630 static int bevalterm_ison = FALSE;
3631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 int xterm_mouse_vers;
3633
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003634 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003635# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003636 && p_bevalterm == bevalterm_ison
3637# endif
3638 )
3639 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 return;
3641
3642 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003643
3644# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003645 if (ttym_flags == TTYM_URXVT)
3646 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003647 out_str_nf((char_u *)
3648 (on
3649 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3650 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003651 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003652 }
3653# endif
3654
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003655# ifdef FEAT_MOUSE_SGR
3656 if (ttym_flags == TTYM_SGR)
3657 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003658 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003659 out_str_nf((char_u *)
3660 (on
3661 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3662 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003663 mouse_ison = on;
3664 }
3665# endif
3666
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003667# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003668 if (bevalterm_ison != (p_bevalterm && on))
3669 {
3670 bevalterm_ison = (p_bevalterm && on);
3671 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3672 /* disable mouse movement events, enabling is below */
3673 out_str_nf((char_u *)
3674 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003675 }
3676# endif
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 if (xterm_mouse_vers > 0)
3679 {
3680 if (on) /* enable mouse events, use mouse tracking if available */
3681 out_str_nf((char_u *)
3682 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003683 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003684# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003685 bevalterm_ison
3686 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3687# endif
3688 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3690 else /* disable mouse events, could probably always send the same */
3691 out_str_nf((char_u *)
3692 (xterm_mouse_vers > 1
3693 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3694 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003695 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697
3698# ifdef FEAT_MOUSE_DEC
3699 else if (ttym_flags == TTYM_DEC)
3700 {
3701 if (on) /* enable mouse events */
3702 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3703 else /* disable mouse events */
3704 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003705 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
3707# endif
3708
3709# ifdef FEAT_MOUSE_GPM
3710 else
3711 {
3712 if (on)
3713 {
3714 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003715 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 else
3718 {
3719 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003720 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 }
3723# endif
3724
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003725# ifdef FEAT_SYSMOUSE
3726 else
3727 {
3728 if (on)
3729 {
3730 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003731 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003732 }
3733 else
3734 {
3735 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003736 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003737 }
3738 }
3739# endif
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741# ifdef FEAT_MOUSE_JSB
3742 else
3743 {
3744 if (on)
3745 {
3746 /* D - Enable Mouse up/down messages
3747 * L - Enable Left Button Reporting
3748 * M - Enable Middle Button Reporting
3749 * R - Enable Right Button Reporting
3750 * K - Enable SHIFT and CTRL key Reporting
3751 * + - Enable Advanced messaging of mouse moves and up/down messages
3752 * Q - Quiet No Ack
3753 * # - Numeric value of mouse pointer required
3754 * 0 = Multiview 2000 cursor, used as standard
3755 * 1 = Windows Arrow
3756 * 2 = Windows I Beam
3757 * 3 = Windows Hour Glass
3758 * 4 = Windows Cross Hair
3759 * 5 = Windows UP Arrow
3760 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003761# ifdef JSBTERM_MOUSE_NONADVANCED
3762 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3764 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003765# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3767 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003768# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003769 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 else
3772 {
3773 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3774 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003775 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777 }
3778# endif
3779# ifdef FEAT_MOUSE_PTERM
3780 else
3781 {
3782 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3783 if (on)
3784 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3785 else
3786 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003787 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789# endif
3790}
3791
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003792#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003793/*
3794 * Called when 'balloonevalterm' changed.
3795 */
3796 void
3797mch_bevalterm_changed(void)
3798{
3799 mch_setmouse(mouse_ison);
3800}
3801#endif
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803/*
3804 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3805 */
3806 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003807check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
3809# ifdef FEAT_MOUSE_XTERM
3810 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003811# ifdef FEAT_MOUSE_URXVT
3812 && use_xterm_mouse() != 3
3813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814# ifdef FEAT_GUI
3815 && !gui.in_use
3816# endif
3817 )
3818 {
3819 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003820 ? IF_EB("\233M", CSI_STR "M")
3821 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (*p_mouse != NUL)
3823 {
3824 /* force mouse off and maybe on to send possibly new mouse
3825 * activation sequence to the xterm, with(out) drag tracing. */
3826 mch_setmouse(FALSE);
3827 setmouse();
3828 }
3829 }
3830 else
3831 del_mouse_termcode(KS_MOUSE);
3832# endif
3833
3834# ifdef FEAT_MOUSE_GPM
3835 if (!use_xterm_mouse()
3836# ifdef FEAT_GUI
3837 && !gui.in_use
3838# endif
3839 )
3840 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3841# endif
3842
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003843# ifdef FEAT_SYSMOUSE
3844 if (!use_xterm_mouse()
3845# ifdef FEAT_GUI
3846 && !gui.in_use
3847# endif
3848 )
3849 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3850# endif
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003853 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (!use_xterm_mouse()
3855# ifdef FEAT_GUI
3856 && !gui.in_use
3857# endif
3858 )
3859 set_mouse_termcode(KS_JSBTERM_MOUSE,
3860 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3861 else
3862 del_mouse_termcode(KS_JSBTERM_MOUSE);
3863# endif
3864
3865# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003866 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * define it in the GUI or when using an xterm. */
3868 if (!use_xterm_mouse()
3869# ifdef FEAT_GUI
3870 && !gui.in_use
3871# endif
3872 )
3873 set_mouse_termcode(KS_NETTERM_MOUSE,
3874 (char_u *)IF_EB("\033}", ESC_STR "}"));
3875 else
3876 del_mouse_termcode(KS_NETTERM_MOUSE);
3877# endif
3878
3879# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003880 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003881 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882# ifdef FEAT_GUI
3883 && !gui.in_use
3884# endif
3885 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003886 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3887 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 else
3889 del_mouse_termcode(KS_DEC_MOUSE);
3890# endif
3891# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003892 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003893 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894# ifdef FEAT_GUI
3895 && !gui.in_use
3896# endif
3897 )
3898 set_mouse_termcode(KS_PTERM_MOUSE,
3899 (char_u *) IF_EB("\033[", ESC_STR "["));
3900 else
3901 del_mouse_termcode(KS_PTERM_MOUSE);
3902# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003903# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003904 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003905# ifdef FEAT_GUI
3906 && !gui.in_use
3907# endif
3908 )
3909 {
3910 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003911 ? IF_EB("\233*M", CSI_STR "*M")
3912 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003913
3914 if (*p_mouse != NUL)
3915 {
3916 mch_setmouse(FALSE);
3917 setmouse();
3918 }
3919 }
3920 else
3921 del_mouse_termcode(KS_URXVT_MOUSE);
3922# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003923# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003924 if (use_xterm_mouse() == 4
3925# ifdef FEAT_GUI
3926 && !gui.in_use
3927# endif
3928 )
3929 {
3930 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003931 ? IF_EB("\233<*M", CSI_STR "<*M")
3932 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3933
3934 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3935 ? IF_EB("\233<*m", CSI_STR "<*m")
3936 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003937
3938 if (*p_mouse != NUL)
3939 {
3940 mch_setmouse(FALSE);
3941 setmouse();
3942 }
3943 }
3944 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003945 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003946 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003947 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3948 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950}
3951#endif
3952
3953/*
3954 * set screen mode, always fails.
3955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003957mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 EMSG(_(e_screenmode));
3960 return FAIL;
3961}
3962
3963#ifndef VMS
3964
3965/*
3966 * Try to get the current window size:
3967 * 1. with an ioctl(), most accurate method
3968 * 2. from the environment variables LINES and COLUMNS
3969 * 3. from the termcap
3970 * 4. keep using the old values
3971 * Return OK when size could be determined, FAIL otherwise.
3972 */
3973 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003974mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 long rows = 0;
3977 long columns = 0;
3978 char_u *p;
3979
3980 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 * 1. try using an ioctl. It is the most accurate method.
3982 *
3983 * Try using TIOCGWINSZ first, some systems that have it also define
3984 * TIOCGSIZE but don't have a struct ttysize.
3985 */
3986# ifdef TIOCGWINSZ
3987 {
3988 struct winsize ws;
3989 int fd = 1;
3990
3991 /* When stdout is not a tty, use stdin for the ioctl(). */
3992 if (!isatty(fd) && isatty(read_cmd_fd))
3993 fd = read_cmd_fd;
3994 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3995 {
3996 columns = ws.ws_col;
3997 rows = ws.ws_row;
3998 }
3999 }
4000# else /* TIOCGWINSZ */
4001# ifdef TIOCGSIZE
4002 {
4003 struct ttysize ts;
4004 int fd = 1;
4005
4006 /* When stdout is not a tty, use stdin for the ioctl(). */
4007 if (!isatty(fd) && isatty(read_cmd_fd))
4008 fd = read_cmd_fd;
4009 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4010 {
4011 columns = ts.ts_cols;
4012 rows = ts.ts_lines;
4013 }
4014 }
4015# endif /* TIOCGSIZE */
4016# endif /* TIOCGWINSZ */
4017
4018 /*
4019 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004020 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4021 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004023 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025 if ((p = (char_u *)getenv("LINES")))
4026 rows = atoi((char *)p);
4027 if ((p = (char_u *)getenv("COLUMNS")))
4028 columns = atoi((char *)p);
4029 }
4030
4031#ifdef HAVE_TGETENT
4032 /*
4033 * 3. try reading "co" and "li" entries from termcap
4034 */
4035 if (columns == 0 || rows == 0)
4036 getlinecol(&columns, &rows);
4037#endif
4038
4039 /*
4040 * 4. If everything fails, use the old values
4041 */
4042 if (columns <= 0 || rows <= 0)
4043 return FAIL;
4044
4045 Rows = rows;
4046 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004047 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return OK;
4049}
4050
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004051#if defined(FEAT_TERMINAL) || defined(PROTO)
4052/*
4053 * Report the windows size "rows" and "cols" to tty "fd".
4054 */
4055 int
4056mch_report_winsize(int fd, int rows, int cols)
4057{
4058# ifdef TIOCSWINSZ
4059 struct winsize ws;
4060
4061 ws.ws_col = cols;
4062 ws.ws_row = rows;
4063 ws.ws_xpixel = cols * 5;
4064 ws.ws_ypixel = rows * 10;
4065 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4066 {
4067 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4068 return OK;
4069 }
4070 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4071# else
4072# ifdef TIOCSSIZE
4073 struct ttysize ts;
4074
4075 ts.ts_cols = cols;
4076 ts.ts_lines = rows;
4077 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4078 {
4079 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4080 return OK;
4081 }
4082 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4083# endif
4084# endif
4085 return FAIL;
4086}
4087#endif
4088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089/*
4090 * Try to set the window size to Rows and Columns.
4091 */
4092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004093mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 if (*T_CWS)
4096 {
4097 /*
4098 * NOTE: if you get an error here that term_set_winsize() is
4099 * undefined, check the output of configure. It could probably not
4100 * find a ncurses, termcap or termlib library.
4101 */
4102 term_set_winsize((int)Rows, (int)Columns);
4103 out_flush();
4104 screen_start(); /* don't know where cursor is now */
4105 }
4106}
4107
4108#endif /* VMS */
4109
4110/*
4111 * Rows and/or Columns has changed.
4112 */
4113 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004114mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 /* Nothing to do. */
4117}
4118
Bram Moolenaar205b8862011-09-07 15:04:31 +02004119/*
4120 * Wait for process "child" to end.
4121 * Return "child" if it exited properly, <= 0 on error.
4122 */
4123 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004124wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004125{
4126 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004127 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004128
4129 while (wait_pid != child)
4130 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004131 /* When compiled with Python threads are probably used, in which case
4132 * wait() sometimes hangs for no obvious reason. Use waitpid()
4133 * instead and loop (like the GUI). Also needed for other interfaces,
4134 * they might call system(). */
4135# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004136 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004137# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004138 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004139# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004140 if (wait_pid == 0)
4141 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004142 /* Wait for 1 to 10 msec before trying again. */
4143 mch_delay(delay_msec, TRUE);
4144 if (++delay_msec > 10)
4145 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004146 continue;
4147 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004148 if (wait_pid <= 0
4149# ifdef ECHILD
4150 && errno == ECHILD
4151# endif
4152 )
4153 break;
4154 }
4155 return wait_pid;
4156}
4157
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004158#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01004159/*
4160 * Parse "cmd" and put the white-separated parts in "argv".
4161 * "argv" is an allocated array with "argc" entries.
4162 * Returns FAIL when out of memory.
4163 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004164 int
4165mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
4166{
4167 int i;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004168 char_u *p, *d;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004169 int inquote;
4170
4171 /*
4172 * Do this loop twice:
4173 * 1: find number of arguments
4174 * 2: separate them and build argv[]
4175 */
4176 for (i = 0; i < 2; ++i)
4177 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02004178 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004179 inquote = FALSE;
4180 *argc = 0;
4181 for (;;)
4182 {
4183 if (i == 1)
4184 (*argv)[*argc] = (char *)p;
4185 ++*argc;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004186 d = p;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004187 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4188 {
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004189 if (p[0] == '"')
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004190 /* quotes surrounding an argument and are dropped */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004191 inquote = !inquote;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004192 else
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004193 {
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004194 if (p[0] == '\\' && p[1] != NUL)
4195 {
4196 /* First pass: skip over "\ " and "\"".
4197 * Second pass: Remove the backslash. */
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004198 ++p;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004199 }
4200 if (i == 1)
4201 *d++ = *p;
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004202 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01004203 ++p;
4204 }
4205 if (*p == NUL)
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004206 {
4207 if (i == 1)
4208 *d++ = NUL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004209 break;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004210 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01004211 if (i == 1)
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004212 *d++ = NUL;
4213 p = skipwhite(p + 1);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004214 }
4215 if (*argv == NULL)
4216 {
4217 if (use_shcf)
4218 {
4219 /* Account for possible multiple args in p_shcf. */
4220 p = p_shcf;
4221 for (;;)
4222 {
4223 p = skiptowhite(p);
4224 if (*p == NUL)
4225 break;
4226 ++*argc;
4227 p = skipwhite(p);
4228 }
4229 }
4230
4231 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4232 if (*argv == NULL) /* out of memory */
4233 return FAIL;
4234 }
4235 }
4236 return OK;
4237}
4238#endif
4239
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004240#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004241/*
4242 * Set the environment for a child process.
4243 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004244 static void
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004245set_child_environment(long rows, long columns, char *term)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004246{
4247# ifdef HAVE_SETENV
4248 char envbuf[50];
4249# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004250 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004251 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004252 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004253 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004254 static char envbuf_Colors[20];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004255# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004256 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004257# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004259 long colors =
4260# ifdef FEAT_GUI
4261 gui.in_use ? 256*256*256 :
4262# endif
4263 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004264
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004265# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004266 setenv("TERM", term, 1);
4267 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004269 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004270 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004271 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004272 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004273 sprintf((char *)envbuf, "%ld", colors);
4274 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004275# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004276 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004277# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004278# else
4279 /*
4280 * Putenv does not copy the string, it has to remain valid.
4281 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004282 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004283 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004284 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4285 putenv(envbuf_Term);
4286 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004287 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004288 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4289 putenv(envbuf_Lines);
4290 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4291 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004292 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004293 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4294 putenv(envbuf_Colors);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004295# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004296 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4297 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4298 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004299# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004300# endif
4301}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004302
4303 static void
4304set_default_child_environment(void)
4305{
4306 set_child_environment(Rows, Columns, "dumb");
4307}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004308#endif
4309
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004310#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004311/*
4312 * Open a PTY, with FD for the master and slave side.
4313 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4314 * When successful both file descriptors are stored.
4315 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004316 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004317open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004318{
4319 char *tty_name;
4320
4321 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4322 if (*pty_master_fd >= 0)
4323 {
4324 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4325 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4326 * adding O_NOCTTY always works when defined. */
4327#ifdef O_NOCTTY
4328 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4329#else
4330 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4331#endif
4332 if (*pty_slave_fd < 0)
4333 {
4334 close(*pty_master_fd);
4335 *pty_master_fd = -1;
4336 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004337 else if (namep != NULL)
4338 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004339 }
4340}
4341#endif
4342
Bram Moolenaarfae42832017-08-01 22:24:26 +02004343/*
4344 * Send SIGINT to a child process if "c" is an interrupt character.
4345 */
4346 void
4347may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4348{
4349# ifdef SIGINT
4350 if (c == Ctrl_C || c == intr_char)
4351 {
4352# ifdef HAVE_SETSID
4353 kill(-pid, SIGINT);
4354# else
4355 kill(0, SIGINT);
4356# endif
4357 if (wpid > 0)
4358 kill(wpid, SIGINT);
4359 }
4360# endif
4361}
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004364mch_call_shell(
4365 char_u *cmd,
4366 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367{
4368#ifdef VMS
4369 char *ifn = NULL;
4370 char *ofn = NULL;
4371#endif
4372 int tmode = cur_tmode;
4373#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004374 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004375 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 out_flush();
4378
4379 if (options & SHELL_COOKED)
4380 settmode(TMODE_COOK); /* set to normal mode */
4381
Bram Moolenaar62b42182010-09-21 22:09:37 +02004382# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004383 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004384 loose_clipboard();
4385# endif
4386
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (cmd == NULL)
4388 x = system((char *)p_sh);
4389 else
4390 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004391# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 if (ofn = strchr((char *)cmd, '>'))
4393 *ofn++ = '\0';
4394 if (ifn = strchr((char *)cmd, '<'))
4395 {
4396 char *p;
4397
4398 *ifn++ = '\0';
4399 p = strchr(ifn,' '); /* chop off any trailing spaces */
4400 if (p)
4401 *p = '\0';
4402 }
4403 if (ofn)
4404 x = vms_sys((char *)cmd, ofn, ifn);
4405 else
4406 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004407# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 newcmd = lalloc(STRLEN(p_sh)
4409 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4410 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4411 if (newcmd == NULL)
4412 x = 0;
4413 else
4414 {
4415 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4416 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4417 (char *)p_shcf,
4418 (char *)cmd);
4419 x = system((char *)newcmd);
4420 vim_free(newcmd);
4421 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424# ifdef VMS
4425 x = vms_sys_status(x);
4426# endif
4427 if (emsg_silent)
4428 ;
4429 else if (x == 127)
4430 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 else if (x && !(options & SHELL_SILENT))
4432 {
4433 MSG_PUTS(_("\nshell returned "));
4434 msg_outnum((long)x);
4435 msg_putchar('\n');
4436 }
4437
4438 if (tmode == TMODE_RAW)
4439 settmode(TMODE_RAW); /* set to raw mode */
4440# ifdef FEAT_TITLE
4441 resettitle();
4442# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004443# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4444 restore_clipboard();
4445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return x;
4447
4448#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4449
Bram Moolenaardf177f62005-02-22 08:39:57 +00004450# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4451 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004452# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar835dc632016-02-07 14:27:38 +01004454 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004456 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 pid_t wait_pid = 0;
4458# ifdef HAVE_UNION_WAIT
4459 union wait status;
4460# else
4461 int status = -1;
4462# endif
4463 int retval = -1;
4464 char **argv = NULL;
4465 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004466 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 int i;
4468 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004470# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004472# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004473 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 int fd_fromshell[2];
4475 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004476 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar62b42182010-09-21 22:09:37 +02004478 newcmd = vim_strsave(p_sh);
4479 if (newcmd == NULL) /* out of memory */
4480 goto error;
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 out_flush();
4483 if (options & SHELL_COOKED)
4484 settmode(TMODE_COOK); /* set to normal mode */
4485
Bram Moolenaar835dc632016-02-07 14:27:38 +01004486 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4487 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (cmd != NULL)
4490 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004491 char_u *s;
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (extra_shell_arg != NULL)
4494 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004495
4496 /* Break 'shellcmdflag' into white separated parts. This doesn't
4497 * handle quoted strings, they are very unlikely to appear. */
4498 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4499 if (p_shcf_copy == NULL) /* out of memory */
4500 goto error;
4501 s = p_shcf_copy;
4502 p = p_shcf;
4503 while (*p != NUL)
4504 {
4505 argv[argc++] = (char *)s;
4506 while (*p && *p != ' ' && *p != TAB)
4507 *s++ = *p++;
4508 *s++ = NUL;
4509 p = skipwhite(p);
4510 }
4511
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 argv[argc++] = (char *)cmd;
4513 }
4514 argv[argc] = NULL;
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004517 * For the GUI, when writing the output into the buffer and when reading
4518 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4519 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004521 if ((options & (SHELL_READ|SHELL_WRITE))
4522# ifdef FEAT_GUI
4523 || (gui.in_use && show_shell_mess)
4524# endif
4525 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004527# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /*
4529 * Try to open a master pty.
4530 * If this works, open the slave pty.
4531 * If the slave can't be opened, close the master pty.
4532 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004533 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004534 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 /*
4536 * If not opening a pty or it didn't work, try using pipes.
4537 */
4538 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
4541 pipe_error = (pipe(fd_toshell) < 0);
4542 if (!pipe_error) /* pipe create OK */
4543 {
4544 pipe_error = (pipe(fd_fromshell) < 0);
4545 if (pipe_error) /* pipe create failed */
4546 {
4547 close(fd_toshell[0]);
4548 close(fd_toshell[1]);
4549 }
4550 }
4551 if (pipe_error)
4552 {
4553 MSG_PUTS(_("\nCannot create pipes\n"));
4554 out_flush();
4555 }
4556 }
4557 }
4558
4559 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004561 SIGSET_DECL(curset)
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563# ifdef __BEOS__
4564 beos_cleanup_read_thread();
4565# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004566
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004567 BLOCK_SIGNALS(&curset);
4568 pid = fork(); /* maybe we should use vfork() */
4569 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004571 UNBLOCK_SIGNALS(&curset);
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004574 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004576 || (gui.in_use && show_shell_mess)
4577# endif
4578 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004580# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (pty_master_fd >= 0) /* close the pseudo tty */
4582 {
4583 close(pty_master_fd);
4584 close(pty_slave_fd);
4585 }
4586 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
4589 close(fd_toshell[0]);
4590 close(fd_toshell[1]);
4591 close(fd_fromshell[0]);
4592 close(fd_fromshell[1]);
4593 }
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 else if (pid == 0) /* child */
4597 {
4598 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004599 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaar819524702018-02-27 19:10:00 +01004601# ifdef FEAT_JOB_CHANNEL
4602 if (ch_log_active())
4603 /* close the log file in the child */
4604 ch_logfile((char_u *)"", (char_u *)"");
4605# endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (!show_shell_mess || (options & SHELL_EXPAND))
4608 {
4609 int fd;
4610
4611 /*
4612 * Don't want to show any message from the shell. Can't just
4613 * close stdout and stderr though, because some systems will
4614 * break if you try to write to them after that, so we must
4615 * use dup() to replace them with something else -- webb
4616 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4617 * waiting for input.
4618 */
4619 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4620 fclose(stdin);
4621 fclose(stdout);
4622 fclose(stderr);
4623
4624 /*
4625 * If any of these open()'s and dup()'s fail, we just continue
4626 * anyway. It's not fatal, and on most systems it will make
4627 * no difference at all. On a few it will cause the execvp()
4628 * to exit with a non-zero status even when the completion
4629 * could be done, which is nothing too serious. If the open()
4630 * or dup() failed we'd just do the same thing ourselves
4631 * anyway -- webb
4632 */
4633 if (fd >= 0)
4634 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004635 ignored = dup(fd); /* To replace stdin (fd 0) */
4636 ignored = dup(fd); /* To replace stdout (fd 1) */
4637 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
4639 /* Don't need this now that we've duplicated it */
4640 close(fd);
4641 }
4642 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004645 || gui.in_use
4646# endif
4647 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
4649
Bram Moolenaardf177f62005-02-22 08:39:57 +00004650# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004651 /* Create our own process group, so that the child and all its
4652 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004653 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004654 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004655 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004656 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004657# if defined(SIGHUP)
4658 /* When doing "!xterm&" and 'shell' is bash: the shell
4659 * will exit and send SIGHUP to all processes in its
4660 * group, killing the just started process. Ignore SIGHUP
4661 * to avoid that. (suggested by Simon Schubert)
4662 */
4663 signal(SIGHUP, SIG_IGN);
4664# endif
4665 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004666# endif
4667# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004668 if (pty_slave_fd >= 0)
4669 {
4670 /* push stream discipline modules */
4671 if (options & SHELL_COOKED)
4672 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004674 /* Try to become controlling tty (probably doesn't work,
4675 * unless run by root) */
4676 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004678 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004679# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004680 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaara5792f52005-11-23 21:25:05 +00004682 /*
4683 * stderr is only redirected when using the GUI, so that a
4684 * program like gpg can still access the terminal to get a
4685 * passphrase using stderr.
4686 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004687# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (pty_master_fd >= 0)
4689 {
4690 close(pty_master_fd); /* close master side of pty */
4691
4692 /* set up stdin/stdout/stderr for the child */
4693 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004694 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004696 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004697 if (gui.in_use)
4698 {
4699 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004700 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 close(pty_slave_fd); /* has been dupped, close it now */
4704 }
4705 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708 /* set up stdin for the child */
4709 close(fd_toshell[1]);
4710 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004711 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 close(fd_toshell[0]);
4713
4714 /* set up stdout for the child */
4715 close(fd_fromshell[0]);
4716 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004717 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 close(fd_fromshell[1]);
4719
Bram Moolenaara5792f52005-11-23 21:25:05 +00004720# ifdef FEAT_GUI
4721 if (gui.in_use)
4722 {
4723 /* set up stderr for the child */
4724 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004725 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004726 }
4727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 /*
4732 * There is no type cast for the argv, because the type may be
4733 * different on different machines. This may cause a warning
4734 * message with strict compilers, don't worry about it.
4735 * Call _exit() instead of exit() to avoid closing the connection
4736 * to the X server (esp. with GTK, which uses atexit()).
4737 */
4738 execvp(argv[0], argv);
4739 _exit(EXEC_FAILED); /* exec failed, return failure code */
4740 }
4741 else /* parent */
4742 {
4743 /*
4744 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004745 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 */
4747 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004748 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004749 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004750# ifdef FEAT_JOB_CHANNEL
4751 ++dont_check_job_ended;
4752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 /*
4754 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755 * This is also used to pipe stdin/stdout to/from the external
4756 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758 if ((options & (SHELL_READ|SHELL_WRITE))
4759# ifdef FEAT_GUI
4760 || (gui.in_use && show_shell_mess)
4761# endif
4762 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4770 int ta_len = 0; /* valid bytes in ta_buf[] */
4771 int len;
4772 int p_more_save;
4773 int old_State;
4774 int c;
4775 int toshell_fd;
4776 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004777 garray_T ga;
4778 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004779# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4780 struct timeval start_tv;
4781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaardf177f62005-02-22 08:39:57 +00004783# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (pty_master_fd >= 0)
4785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 fromshell_fd = pty_master_fd;
4787 toshell_fd = dup(pty_master_fd);
4788 }
4789 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
4792 close(fd_toshell[0]);
4793 close(fd_fromshell[1]);
4794 toshell_fd = fd_toshell[1];
4795 fromshell_fd = fd_fromshell[0];
4796 }
4797
4798 /*
4799 * Write to the child if there are typed characters.
4800 * Read from the child if there are characters available.
4801 * Repeat the reading a few times if more characters are
4802 * available. Need to check for typed keys now and then, but
4803 * not too often (delays when no chars are available).
4804 * This loop is quit if no characters can be read from the pty
4805 * (WaitForChar detected special condition), or there are no
4806 * characters available and the child has exited.
4807 * Only check if the child has exited when there is no more
4808 * output. The child may exit before all the output has
4809 * been printed.
4810 *
4811 * Currently this busy loops!
4812 * This can probably dead-lock when the write blocks!
4813 */
4814 p_more_save = p_more;
4815 p_more = FALSE;
4816 old_State = State;
4817 State = EXTERNCMD; /* don't redraw at window resize */
4818
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004819 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004820 {
4821 /* Fork a process that will write the lines to the
4822 * external program. */
4823 if ((wpid = fork()) == -1)
4824 {
4825 MSG_PUTS(_("\nCannot fork\n"));
4826 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004827 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004828 {
4829 linenr_T lnum = curbuf->b_op_start.lnum;
4830 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004831 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832 size_t l;
4833
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004834 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004835 for (;;)
4836 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004837 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838 if (l == 0)
4839 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004840 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004841 /* NL -> NUL translation */
4842 len = write(toshell_fd, "", (size_t)1);
4843 else
4844 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004845 char_u *s = vim_strchr(lp + written, NL);
4846
Bram Moolenaar89d40322006-08-29 15:30:07 +00004847 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004848 s == NULL ? l
4849 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004850 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004851 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004852 {
4853 /* Finished a line, add a NL, unless this line
4854 * should not have one. */
4855 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004856 || (!curbuf->b_p_bin
4857 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004858 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004859 && (lnum !=
4860 curbuf->b_ml.ml_line_count
4861 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004862 ignored = write(toshell_fd, "\n",
4863 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004864 ++lnum;
4865 if (lnum > curbuf->b_op_end.lnum)
4866 {
4867 /* finished all the lines, close pipe */
4868 close(toshell_fd);
4869 toshell_fd = -1;
4870 break;
4871 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004872 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004873 written = 0;
4874 }
4875 else if (len > 0)
4876 written += len;
4877 }
4878 _exit(0);
4879 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004880 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 {
4882 close(toshell_fd);
4883 toshell_fd = -1;
4884 }
4885 }
4886
4887 if (options & SHELL_READ)
4888 ga_init2(&ga, 1, BUFLEN);
4889
4890 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004891# ifdef ELAPSED_FUNC
4892 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 for (;;)
4895 {
4896 /*
4897 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004898 * if there are any.
4899 * Don't do this if we are expanding wild cards (would eat
4900 * typeahead).
4901 * Don't do this when filtering and terminal is in cooked
4902 * mode, the shell command will handle the I/O. Avoids
4903 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004904 * Don't get characters when the child has already
4905 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004906 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004907 * while (noread_cnt > 4), avoids that ":r !ls" eats
4908 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 */
4910 len = 0;
4911 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004912 && ((options &
4913 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4914 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004915# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004916 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004917# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004918 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004919 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004920 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004922 if (ta_len == 0)
4923 {
4924 /* Get extra characters when we don't have any.
4925 * Reset the counter and timer. */
4926 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004927# ifdef ELAPSED_FUNC
4928 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004929# endif
4930 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4931 }
4932 if (ta_len > 0 || len > 0)
4933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 /*
4935 * For pipes:
4936 * Check for CTRL-C: send interrupt signal to child.
4937 * Check for CTRL-D: EOF, close pipe to child.
4938 */
4939 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 /*
4942 * Send SIGINT to the child's group or all
4943 * processes in our group.
4944 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004945 may_send_sigint(ta_buf[ta_len], pid, wpid);
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if (pty_master_fd < 0 && toshell_fd >= 0
4948 && ta_buf[ta_len] == Ctrl_D)
4949 {
4950 close(toshell_fd);
4951 toshell_fd = -1;
4952 }
4953 }
4954
4955 /* replace K_BS by <BS> and K_DEL by <DEL> */
4956 for (i = ta_len; i < ta_len + len; ++i)
4957 {
4958 if (ta_buf[i] == CSI && len - i > 2)
4959 {
4960 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4961 if (c == K_DEL || c == K_KDEL || c == K_BS)
4962 {
4963 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4964 (size_t)(len - i - 2));
4965 if (c == K_DEL || c == K_KDEL)
4966 ta_buf[i] = DEL;
4967 else
4968 ta_buf[i] = Ctrl_H;
4969 len -= 2;
4970 }
4971 }
4972 else if (ta_buf[i] == '\r')
4973 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004974# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004976 i += (*mb_ptr2len_len)(ta_buf + i,
4977 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980
4981 /*
4982 * For pipes: echo the typed characters.
4983 * For a pty this does not seem to work.
4984 */
4985 if (pty_master_fd < 0)
4986 {
4987 for (i = ta_len; i < ta_len + len; ++i)
4988 {
4989 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4990 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004991# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 else if (has_mbyte)
4993 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004994 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 msg_outtrans_len(ta_buf + i, l);
4997 i += l - 1;
4998 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
5001 msg_outtrans_len(ta_buf + i, 1);
5002 }
5003 windgoto(msg_row, msg_col);
5004 out_flush();
5005 }
5006
5007 ta_len += len;
5008
5009 /*
5010 * Write the characters to the child, unless EOF has
5011 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005012 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005013 * When writing buffer lines, drop the typed
5014 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 if (options & SHELL_WRITE)
5017 ta_len = 0;
5018 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5021 if (len > 0)
5022 {
5023 ta_len -= len;
5024 mch_memmove(ta_buf, ta_buf + len, ta_len);
5025 }
5026 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029
Bram Moolenaardf177f62005-02-22 08:39:57 +00005030 if (got_int)
5031 {
5032 /* CTRL-C sends a signal to the child, we ignore it
5033 * ourselves */
5034# ifdef HAVE_SETSID
5035 kill(-pid, SIGINT);
5036# else
5037 kill(0, SIGINT);
5038# endif
5039 if (wpid > 0)
5040 kill(wpid, SIGINT);
5041 got_int = FALSE;
5042 }
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 /*
5045 * Check if the child has any characters to be printed.
5046 * Read them and write them to our window. Repeat this as
5047 * long as there is something to do, avoid the 10ms wait
5048 * for mch_inchar(), or sending typeahead characters to
5049 * the external process.
5050 * TODO: This should handle escape sequences, compatible
5051 * to some terminal (vt52?).
5052 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005053 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005054 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005056 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005059# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 );
5063 if (len <= 0) /* end of file or error */
5064 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005065
5066 noread_cnt = 0;
5067 if (options & SHELL_READ)
5068 {
5069 /* Do NUL -> NL translation, append NL separated
5070 * lines to the current buffer. */
5071 for (i = 0; i < len; ++i)
5072 {
5073 if (buffer[i] == NL)
5074 append_ga_line(&ga);
5075 else if (buffer[i] == NUL)
5076 ga_append(&ga, NL);
5077 else
5078 ga_append(&ga, buffer[i]);
5079 }
5080 }
5081# ifdef FEAT_MBYTE
5082 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 int l;
5085
Bram Moolenaardf177f62005-02-22 08:39:57 +00005086 len += buffer_off;
5087 buffer[len] = NUL;
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 /* Check if the last character in buffer[] is
5090 * incomplete, keep these bytes for the next
5091 * round. */
5092 for (p = buffer; p < buffer + len; p += l)
5093 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005094 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (l == 0)
5096 l = 1; /* NUL byte? */
5097 else if (MB_BYTE2LEN(*p) != l)
5098 break;
5099 }
5100 if (p == buffer) /* no complete character */
5101 {
5102 /* avoid getting stuck at an illegal byte */
5103 if (len >= 12)
5104 ++p;
5105 else
5106 {
5107 buffer_off = len;
5108 continue;
5109 }
5110 }
5111 c = *p;
5112 *p = NUL;
5113 msg_puts(buffer);
5114 if (p < buffer + len)
5115 {
5116 *p = c;
5117 buffer_off = (buffer + len) - p;
5118 mch_memmove(buffer, p, buffer_off);
5119 continue;
5120 }
5121 buffer_off = 0;
5122 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005123# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 buffer[len] = NUL;
5127 msg_puts(buffer);
5128 }
5129
5130 windgoto(msg_row, msg_col);
5131 cursor_on();
5132 out_flush();
5133 if (got_int)
5134 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005135
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005136# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005137 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005138 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005139 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005140
5141 /* Avoid that we keep looping here without
5142 * checking for a CTRL-C for a long time. Don't
5143 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005144 if (msec > 2000)
5145 {
5146 noread_cnt = 5;
5147 break;
5148 }
5149 }
5150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005153 /* If we already detected the child has finished, continue
5154 * reading output for a short while. Some text may be
5155 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005156 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005157 {
5158 if (noread_cnt < 5)
5159 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005160 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005161 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 /*
5164 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005165 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005167# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005168 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005169# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5173 || (wait_pid == pid && WIFEXITED(status)))
5174 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005175 /* Don't break the loop yet, try reading more
5176 * characters from "fromshell_fd" first. When using
5177 * pipes there might still be something to read and
5178 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005181 else
5182 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005183
Bram Moolenaar95a51352013-03-21 22:53:50 +01005184# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005185 /* Handle any X events, e.g. serving the clipboard. */
5186 clip_update();
5187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189finished:
5190 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005191 if (options & SHELL_READ)
5192 {
5193 if (ga.ga_len > 0)
5194 {
5195 append_ga_line(&ga);
5196 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005197 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005198 }
5199 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005200 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005201 ga_clear(&ga);
5202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /*
5205 * Give all typeahead that wasn't used back to ui_inchar().
5206 */
5207 if (ta_len)
5208 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 State = old_State;
5210 if (toshell_fd >= 0)
5211 close(toshell_fd);
5212 close(fromshell_fd);
5213 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005214# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005215 else
5216 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005217 long delay_msec = 1;
5218
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005219 /*
5220 * Similar to the loop above, but only handle X events, no
5221 * I/O.
5222 */
5223 for (;;)
5224 {
5225 if (got_int)
5226 {
5227 /* CTRL-C sends a signal to the child, we ignore it
5228 * ourselves */
5229# ifdef HAVE_SETSID
5230 kill(-pid, SIGINT);
5231# else
5232 kill(0, SIGINT);
5233# endif
5234 got_int = FALSE;
5235 }
5236# ifdef __NeXT__
5237 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5238# else
5239 wait_pid = waitpid(pid, &status, WNOHANG);
5240# endif
5241 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5242 || (wait_pid == pid && WIFEXITED(status)))
5243 {
5244 wait_pid = pid;
5245 break;
5246 }
5247
5248 /* Handle any X events, e.g. serving the clipboard. */
5249 clip_update();
5250
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005251 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5252 * less time. */
5253 mch_delay(delay_msec, TRUE);
5254 if (++delay_msec > 10)
5255 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005256 }
5257 }
5258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 /*
5261 * Wait until our child has exited.
5262 * Ignore wait() returning pids of other children and returning
5263 * because of some signal like SIGWINCH.
5264 * Don't wait if wait_pid was already set above, indicating the
5265 * child already exited.
5266 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005267 if (wait_pid != pid)
5268 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
Bram Moolenaar624891f2010-10-13 16:22:09 +02005270# ifdef FEAT_GUI
5271 /* Close slave side of pty. Only do this after the child has
5272 * exited, otherwise the child may hang when it tries to write on
5273 * the pty. */
5274 if (pty_master_fd >= 0)
5275 close(pty_slave_fd);
5276# endif
5277
Bram Moolenaardf177f62005-02-22 08:39:57 +00005278 /* Make sure the child that writes to the external program is
5279 * dead. */
5280 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005281 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005282 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005283 wait4pid(wpid, NULL);
5284 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005285
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005286# ifdef FEAT_JOB_CHANNEL
5287 --dont_check_job_ended;
5288# endif
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 /*
5291 * Set to raw mode right now, otherwise a CTRL-C after
5292 * catch_signals() will kill Vim.
5293 */
5294 if (tmode == TMODE_RAW)
5295 settmode(TMODE_RAW);
5296 did_settmode = TRUE;
5297 set_signals();
5298
5299 if (WIFEXITED(status))
5300 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005301 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005303 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
5305 if (retval == EXEC_FAILED)
5306 {
5307 MSG_PUTS(_("\nCannot execute shell "));
5308 msg_outtrans(p_sh);
5309 msg_putchar('\n');
5310 }
5311 else if (!(options & SHELL_SILENT))
5312 {
5313 MSG_PUTS(_("\nshell returned "));
5314 msg_outnum((long)retval);
5315 msg_putchar('\n');
5316 }
5317 }
5318 }
5319 else
5320 MSG_PUTS(_("\nCommand terminated\n"));
5321 }
5322 }
5323 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005324 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326error:
5327 if (!did_settmode)
5328 if (tmode == TMODE_RAW)
5329 settmode(TMODE_RAW); /* set to raw mode */
5330# ifdef FEAT_TITLE
5331 resettitle();
5332# endif
5333 vim_free(newcmd);
5334
5335 return retval;
5336
5337#endif /* USE_SYSTEM */
5338}
5339
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005340#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005341 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005342mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005343{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005344 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005345 int fd_in[2] = {-1, -1}; /* for stdin */
5346 int fd_out[2] = {-1, -1}; /* for stdout */
5347 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005348 int pty_master_fd = -1;
5349 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005350 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005351 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5352 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5353 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005354 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005355 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5356 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005357 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005358 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005359 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005360
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005361 if (use_out_for_err && use_null_for_out)
5362 use_null_for_err = TRUE;
5363
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005364 /* default is to fail */
5365 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005366
Bram Moolenaarb2412082017-08-20 18:09:14 +02005367 if (options->jo_pty
5368 && (!(use_file_for_in || use_null_for_in)
5369 || !(use_file_for_in || use_null_for_out)
5370 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005371 {
5372 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5373 if (job->jv_tty_out != NULL)
5374 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5375 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005376
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005377 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005378 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005379 if (use_file_for_in)
5380 {
5381 char_u *fname = options->jo_io_name[PART_IN];
5382
5383 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5384 if (fd_in[0] < 0)
5385 {
5386 EMSG2(_(e_notopen), fname);
5387 goto failed;
5388 }
5389 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005390 else
5391 /* When writing buffer lines to the input don't use the pty, so that
5392 * the pipe can be closed when all lines were written. */
5393 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5394 && pipe(fd_in) < 0)
5395 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005396
5397 if (use_file_for_out)
5398 {
5399 char_u *fname = options->jo_io_name[PART_OUT];
5400
5401 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5402 if (fd_out[1] < 0)
5403 {
5404 EMSG2(_(e_notopen), fname);
5405 goto failed;
5406 }
5407 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005408 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005409 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005410
5411 if (use_file_for_err)
5412 {
5413 char_u *fname = options->jo_io_name[PART_ERR];
5414
5415 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5416 if (fd_err[1] < 0)
5417 {
5418 EMSG2(_(e_notopen), fname);
5419 goto failed;
5420 }
5421 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005422 else if (!use_out_for_err && !use_null_for_err
5423 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005424 goto failed;
5425
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005426 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5427 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005428 if (options->jo_set & JO_CHANNEL)
5429 {
5430 channel = options->jo_channel;
5431 if (channel != NULL)
5432 ++channel->ch_refcount;
5433 }
5434 else
5435 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005436 if (channel == NULL)
5437 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005438 if (job->jv_tty_out != NULL)
5439 ch_log(channel, "using pty %s on fd %d",
5440 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005441 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005442
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005443 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005444 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005445 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005446 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005447 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005448 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005449 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005450 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005451 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005452 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005453 int null_fd = -1;
5454 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005455
Bram Moolenaar835dc632016-02-07 14:27:38 +01005456 /* child */
5457 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005458 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005459
Bram Moolenaar819524702018-02-27 19:10:00 +01005460# ifdef FEAT_JOB_CHANNEL
5461 if (ch_log_active())
5462 /* close the log file in the child */
5463 ch_logfile((char_u *)"", (char_u *)"");
5464# endif
5465
Bram Moolenaar835dc632016-02-07 14:27:38 +01005466# ifdef HAVE_SETSID
5467 /* Create our own process group, so that the child and all its
5468 * children can be kill()ed. Don't do this when using pipes,
5469 * because stdin is not a tty, we would lose /dev/tty. */
5470 (void)setsid();
5471# endif
5472
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005473# ifdef FEAT_TERMINAL
5474 if (options->jo_term_rows > 0)
5475 set_child_environment(
5476 (long)options->jo_term_rows,
5477 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005478 STRNCMP(T_NAME, "xterm", 5) == 0
5479 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005480 else
5481# endif
5482 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005483
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005484 if (options->jo_env != NULL)
5485 {
5486 dict_T *dict = options->jo_env;
5487 hashitem_T *hi;
5488 int todo = (int)dict->dv_hashtab.ht_used;
5489
5490 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5491 if (!HASHITEM_EMPTY(hi))
5492 {
5493 typval_T *item = &dict_lookup(hi)->di_tv;
5494
5495 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5496 --todo;
5497 }
5498 }
5499
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005500 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005501 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005502 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005503 if (null_fd < 0)
5504 {
5505 perror("opening /dev/null failed");
5506 _exit(OPEN_NULL_FAILED);
5507 }
5508 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005509
Bram Moolenaar223896d2017-08-02 22:33:28 +02005510 if (pty_slave_fd >= 0)
5511 {
5512 /* push stream discipline modules */
5513 SetupSlavePTY(pty_slave_fd);
5514# ifdef TIOCSCTTY
5515 /* Try to become controlling tty (probably doesn't work,
5516 * unless run by root) */
5517 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5518# endif
5519 }
5520
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005521 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005522 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005523 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005524 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005525 else if (fd_in[0] < 0)
5526 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005527 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005528 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005529
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005530 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005531 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005532 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005533 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005534 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005535 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005536 }
5537 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005538 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005539 else if (fd_err[1] < 0)
5540 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005541 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005542 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005543
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005544 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005545 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005546 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005547 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005548 else if (fd_out[1] < 0)
5549 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005550 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005551 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005552
5553 if (fd_in[0] >= 0)
5554 close(fd_in[0]);
5555 if (fd_in[1] >= 0)
5556 close(fd_in[1]);
5557 if (fd_out[0] >= 0)
5558 close(fd_out[0]);
5559 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005560 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005561 if (fd_err[0] >= 0)
5562 close(fd_err[0]);
5563 if (fd_err[1] >= 0)
5564 close(fd_err[1]);
5565 if (pty_master_fd >= 0)
5566 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005567 close(pty_master_fd); /* not used in the child */
5568 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005569 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005570
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005571 if (null_fd >= 0)
5572 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005573
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005574 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5575 _exit(EXEC_FAILED);
5576
Bram Moolenaar835dc632016-02-07 14:27:38 +01005577 /* See above for type of argv. */
5578 execvp(argv[0], argv);
5579
Bram Moolenaar4694a172016-04-21 14:05:23 +02005580 if (stderr_works)
5581 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005582# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005583 /* calling free_all_mem() here causes problems. Ignore valgrind
5584 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005585# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005586 _exit(EXEC_FAILED); /* exec failed, return failure code */
5587 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005588
5589 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005590 UNBLOCK_SIGNALS(&curset);
5591
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005592 job->jv_pid = pid;
5593 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005594 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005595
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005596 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005597 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005598 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005599 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005600 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005601 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005602 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005603 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005604 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 if (channel != NULL)
5606 {
5607 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005608 use_file_for_in || use_null_for_in
5609 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5610 use_file_for_out || use_null_for_out
5611 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5612 use_out_for_err || use_file_for_err || use_null_for_err
5613 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005614 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005615 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005616 else
5617 {
5618 if (fd_in[1] >= 0)
5619 close(fd_in[1]);
5620 if (fd_out[0] >= 0)
5621 close(fd_out[0]);
5622 if (fd_err[0] >= 0)
5623 close(fd_err[0]);
5624 if (pty_master_fd >= 0)
5625 close(pty_master_fd);
5626 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005627
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005628 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005629 return;
5630
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005631failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005632 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005633 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005634 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005635 if (fd_in[1] >= 0)
5636 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005637 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005638 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005639 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005640 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005641 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005642 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005643 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005644 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005645 if (pty_master_fd >= 0)
5646 close(pty_master_fd);
5647 if (pty_slave_fd >= 0)
5648 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005649}
5650
5651 char *
5652mch_job_status(job_T *job)
5653{
5654# ifdef HAVE_UNION_WAIT
5655 union wait status;
5656# else
5657 int status = -1;
5658# endif
5659 pid_t wait_pid = 0;
5660
5661# ifdef __NeXT__
5662 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5663# else
5664 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5665# endif
5666 if (wait_pid == -1)
5667 {
5668 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005669 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005670 }
5671 if (wait_pid == 0)
5672 return "run";
5673 if (WIFEXITED(status))
5674 {
5675 /* LINTED avoid "bitwise operation on signed value" */
5676 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005677 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005678 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005679 if (WIFSIGNALED(status))
5680 {
5681 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005682 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005683 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005684 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005685
5686return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005687 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005688 {
5689 ch_log(job->jv_channel, "Job ended");
5690 job->jv_status = JOB_ENDED;
5691 }
5692 return "dead";
5693}
5694
5695 job_T *
5696mch_detect_ended_job(job_T *job_list)
5697{
5698# ifdef HAVE_UNION_WAIT
5699 union wait status;
5700# else
5701 int status = -1;
5702# endif
5703 pid_t wait_pid = 0;
5704 job_T *job;
5705
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005706# ifndef USE_SYSTEM
5707 /* Do not do this when waiting for a shell command to finish, we would get
5708 * the exit value here (and discard it), the exit value obtained there
5709 * would then be wrong. */
5710 if (dont_check_job_ended > 0)
5711 return NULL;
5712# endif
5713
Bram Moolenaar97792de2016-10-15 18:36:49 +02005714# ifdef __NeXT__
5715 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5716# else
5717 wait_pid = waitpid(-1, &status, WNOHANG);
5718# endif
5719 if (wait_pid <= 0)
5720 /* no process ended */
5721 return NULL;
5722 for (job = job_list; job != NULL; job = job->jv_next)
5723 {
5724 if (job->jv_pid == wait_pid)
5725 {
5726 if (WIFEXITED(status))
5727 /* LINTED avoid "bitwise operation on signed value" */
5728 job->jv_exitval = WEXITSTATUS(status);
5729 else if (WIFSIGNALED(status))
5730 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005731 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005732 {
5733 ch_log(job->jv_channel, "Job ended");
5734 job->jv_status = JOB_ENDED;
5735 }
5736 return job;
5737 }
5738 }
5739 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005740}
5741
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005742/*
5743 * Send a (deadly) signal to "job".
5744 * Return FAIL if "how" is not a valid name.
5745 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005746 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005747mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005748{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005749 int sig = -1;
5750 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005751
Bram Moolenaar923d9262016-02-25 20:56:01 +01005752 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005753 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005754 else if (STRCMP(how, "hup") == 0)
5755 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005756 else if (STRCMP(how, "quit") == 0)
5757 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005758 else if (STRCMP(how, "int") == 0)
5759 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005760 else if (STRCMP(how, "kill") == 0)
5761 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005762#ifdef SIGWINCH
5763 else if (STRCMP(how, "winch") == 0)
5764 sig = SIGWINCH;
5765#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005766 else if (isdigit(*how))
5767 sig = atoi((char *)how);
5768 else
5769 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005770
Bram Moolenaar72801402016-02-09 11:37:50 +01005771 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005772 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005773#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005774 if (job_pid == getpgid(job_pid))
5775 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005776#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005777
Bram Moolenaar61a66052017-07-22 18:39:00 +02005778 /* Never kill ourselves! */
5779 if (job_pid != 0)
5780 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005781
Bram Moolenaar835dc632016-02-07 14:27:38 +01005782 return OK;
5783}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005784
5785/*
5786 * Clear the data related to "job".
5787 */
5788 void
5789mch_clear_job(job_T *job)
5790{
5791 /* call waitpid because child process may become zombie */
5792# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005793 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005794# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005795 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005796# endif
5797}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005798#endif
5799
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005800#if defined(FEAT_TERMINAL) || defined(PROTO)
5801 int
5802mch_create_pty_channel(job_T *job, jobopt_T *options)
5803{
5804 int pty_master_fd = -1;
5805 int pty_slave_fd = -1;
5806 channel_T *channel;
5807
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005808 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5809 if (job->jv_tty_out != NULL)
5810 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005811 close(pty_slave_fd);
5812
5813 channel = add_channel();
5814 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005815 {
5816 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005817 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005818 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005819 if (job->jv_tty_out != NULL)
5820 ch_log(channel, "using pty %s on fd %d",
5821 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005822 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5823 channel->ch_keep_open = TRUE;
5824
5825 channel_set_pipes(channel, pty_master_fd, pty_master_fd, pty_master_fd);
5826 channel_set_job(channel, job, options);
5827 return OK;
5828}
5829#endif
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831/*
5832 * Check for CTRL-C typed by reading all available characters.
5833 * In cooked mode we should get SIGINT, no need to check.
5834 */
5835 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005836mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005838 if ((curr_tmode == TMODE_RAW || force)
5839 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 fill_input_buf(FALSE);
5841}
5842
5843/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005844 * Wait "msec" msec until a character is available from the mouse, keyboard,
5845 * from inbuf[].
5846 * "msec" == -1 will block forever.
5847 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005848 * When "ignore_input" is TRUE even check for pending input when input is
5849 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005850 * "interrupted" (if not NULL) is set to TRUE when no character is available
5851 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005852 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005853 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 */
5855 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005856WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005858#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005859 return ui_wait_for_chars_or_timer(
5860 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005861#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005862 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005863#endif
5864}
5865
5866/*
5867 * Wait "msec" msec until a character is available from the mouse or keyboard
5868 * or from inbuf[].
5869 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005870 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005871 * "interrupted" (if not NULL) is set to TRUE when no character is available
5872 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005873 * When a GUI is being used, this will never get called -- webb
5874 */
5875 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005876WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005877{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878#ifdef FEAT_MOUSE_GPM
5879 int gpm_process_wanted;
5880#endif
5881#ifdef FEAT_XCLIPBOARD
5882 int rest;
5883#endif
5884 int avail;
5885
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005886 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 return 1;
5888
5889#if defined(FEAT_MOUSE_DEC)
5890 /* May need to query the mouse position. */
5891 if (WantQueryMouse)
5892 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005893 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5895 }
5896#endif
5897
5898 /*
5899 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5900 * events. This is a bit complicated, because they might both be defined.
5901 */
5902#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5903# ifdef FEAT_XCLIPBOARD
5904 rest = 0;
5905 if (do_xterm_trace())
5906 rest = msec;
5907# endif
5908 do
5909 {
5910# ifdef FEAT_XCLIPBOARD
5911 if (rest != 0)
5912 {
5913 msec = XT_TRACE_DELAY;
5914 if (rest >= 0 && rest < XT_TRACE_DELAY)
5915 msec = rest;
5916 if (rest >= 0)
5917 rest -= msec;
5918 }
5919# endif
5920# ifdef FEAT_MOUSE_GPM
5921 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005922 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005923 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005925 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926# endif
5927 if (!avail)
5928 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005929 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 return 1;
5931# ifdef FEAT_XCLIPBOARD
5932 if (rest == 0 || !do_xterm_trace())
5933# endif
5934 break;
5935 }
5936 }
5937 while (FALSE
5938# ifdef FEAT_MOUSE_GPM
5939 || (gpm_process_wanted && mch_gpm_process() == 0)
5940# endif
5941# ifdef FEAT_XCLIPBOARD
5942 || (!avail && rest != 0)
5943# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005944 )
5945 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
5947#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005948 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#endif
5950 return avail;
5951}
5952
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005953#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954/*
5955 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005956 * "msec" == 0 will check for characters once.
5957 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005960 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005961 * "interrupted" (if not NULL) is set to TRUE when no character is available
5962 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964#if defined(__BEOS__)
5965 int
5966#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005967 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005969RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005972 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005973#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 static int busy = FALSE;
5975
5976 /* May retry getting characters after an event was handled. */
5977# define MAY_LOOP
5978
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005979# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 /* Remember at what time we started, so that we know how much longer we
5981 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005982 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005983 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005985 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005986 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987# endif
5988
5989 /* Handle being called recursively. This may happen for the session
5990 * manager stuff, it may save the file, which does a breakcheck. */
5991 if (busy)
5992 return 0;
5993#endif
5994
5995#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005996 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997#endif
5998 {
5999#ifdef MAY_LOOP
6000 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006001# ifdef FEAT_MZSCHEME
6002 int mzquantum_used = FALSE;
6003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004#endif
6005#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006006 /* each channel may use in, out and err */
6007 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 int nfd;
6009# ifdef FEAT_XCLIPBOARD
6010 int xterm_idx = -1;
6011# endif
6012# ifdef FEAT_MOUSE_GPM
6013 int gpm_idx = -1;
6014# endif
6015# ifdef USE_XSMP
6016 int xsmp_idx = -1;
6017# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006018 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006020# ifdef FEAT_MZSCHEME
6021 mzvim_check_threads();
6022 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6023 {
6024 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6025 mzquantum_used = TRUE;
6026 }
6027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 fds[0].fd = fd;
6029 fds[0].events = POLLIN;
6030 nfd = 1;
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006033 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (xterm_Shell != (Widget)0)
6035 {
6036 xterm_idx = nfd;
6037 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6038 fds[nfd].events = POLLIN;
6039 nfd++;
6040 }
6041# endif
6042# ifdef FEAT_MOUSE_GPM
6043 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6044 {
6045 gpm_idx = nfd;
6046 fds[nfd].fd = gpm_fd;
6047 fds[nfd].events = POLLIN;
6048 nfd++;
6049 }
6050# endif
6051# ifdef USE_XSMP
6052 if (xsmp_icefd != -1)
6053 {
6054 xsmp_idx = nfd;
6055 fds[nfd].fd = xsmp_icefd;
6056 fds[nfd].events = POLLIN;
6057 nfd++;
6058 }
6059# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006060#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006061 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006062#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006063 if (interrupted != NULL)
6064 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006066 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006067
6068 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006069 if (result == 0 && interrupted != NULL && ret > 0)
6070 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006071
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006072# ifdef FEAT_MZSCHEME
6073 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006074 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006075 finished = FALSE;
6076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078# ifdef FEAT_XCLIPBOARD
6079 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6080 {
6081 xterm_update(); /* Maybe we should hand out clipboard */
6082 if (--ret == 0 && !input_available())
6083 /* Try again */
6084 finished = FALSE;
6085 }
6086# endif
6087# ifdef FEAT_MOUSE_GPM
6088 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6089 {
6090 *check_for_gpm = 1;
6091 }
6092# endif
6093# ifdef USE_XSMP
6094 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6095 {
6096 if (fds[xsmp_idx].revents & POLLIN)
6097 {
6098 busy = TRUE;
6099 xsmp_handle_requests();
6100 busy = FALSE;
6101 }
6102 else if (fds[xsmp_idx].revents & POLLHUP)
6103 {
6104 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006105 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 xsmp_close();
6107 }
6108 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006109 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 }
6111# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006112#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006113 /* also call when ret == 0, we may be polling a keep-open channel */
6114 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006115 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118#else /* HAVE_SELECT */
6119
6120 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006121 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006122 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006124 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006126# ifdef FEAT_MZSCHEME
6127 mzvim_check_threads();
6128 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6129 {
6130 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6131 mzquantum_used = TRUE;
6132 }
6133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006135 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006137 tv.tv_sec = towait / 1000;
6138 tv.tv_usec = (towait % 1000) * (1000000/1000);
6139 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006141 else
6142 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 /*
6145 * Select on ready for reading and exceptional condition (end of file).
6146 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006147select_eintr:
6148 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006149 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 FD_ZERO(&efds);
6151 FD_SET(fd, &rfds);
6152# if !defined(__QNX__) && !defined(__CYGWIN32__)
6153 /* For QNX select() always returns 1 if this is set. Why? */
6154 FD_SET(fd, &efds);
6155# endif
6156 maxfd = fd;
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006159 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (xterm_Shell != (Widget)0)
6161 {
6162 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6163 if (maxfd < ConnectionNumber(xterm_dpy))
6164 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006165
6166 /* An event may have already been read but not handled. In
6167 * particulary, XFlush may cause this. */
6168 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 }
6170# endif
6171# ifdef FEAT_MOUSE_GPM
6172 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6173 {
6174 FD_SET(gpm_fd, &rfds);
6175 FD_SET(gpm_fd, &efds);
6176 if (maxfd < gpm_fd)
6177 maxfd = gpm_fd;
6178 }
6179# endif
6180# ifdef USE_XSMP
6181 if (xsmp_icefd != -1)
6182 {
6183 FD_SET(xsmp_icefd, &rfds);
6184 FD_SET(xsmp_icefd, &efds);
6185 if (maxfd < xsmp_icefd)
6186 maxfd = xsmp_icefd;
6187 }
6188# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006189# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006190 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006191# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006192 if (interrupted != NULL)
6193 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006195 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006196 result = ret > 0 && FD_ISSET(fd, &rfds);
6197 if (result)
6198 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006199 else if (interrupted != NULL && ret > 0)
6200 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006201
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006202# ifdef EINTR
6203 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006204 {
6205 /* Check whether window has been resized, EINTR may be caused by
6206 * SIGWINCH. */
6207 if (do_resize)
6208 handle_resize();
6209
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006210 /* Interrupted by a signal, need to try again. We ignore msec
6211 * here, because we do want to check even after a timeout if
6212 * characters are available. Needed for reading output of an
6213 * external command after the process has finished. */
6214 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006215 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006216# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006217# ifdef __TANDEM
6218 if (ret == -1 && errno == ENOTSUP)
6219 {
6220 FD_ZERO(&rfds);
6221 FD_ZERO(&efds);
6222 ret = 0;
6223 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006224# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006225# ifdef FEAT_MZSCHEME
6226 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006227 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229# endif
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231# ifdef FEAT_XCLIPBOARD
6232 if (ret > 0 && xterm_Shell != (Widget)0
6233 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6234 {
6235 xterm_update(); /* Maybe we should hand out clipboard */
6236 /* continue looping when we only got the X event and the input
6237 * buffer is empty */
6238 if (--ret == 0 && !input_available())
6239 {
6240 /* Try again */
6241 finished = FALSE;
6242 }
6243 }
6244# endif
6245# ifdef FEAT_MOUSE_GPM
6246 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6247 {
6248 if (FD_ISSET(gpm_fd, &efds))
6249 gpm_close();
6250 else if (FD_ISSET(gpm_fd, &rfds))
6251 *check_for_gpm = 1;
6252 }
6253# endif
6254# ifdef USE_XSMP
6255 if (ret > 0 && xsmp_icefd != -1)
6256 {
6257 if (FD_ISSET(xsmp_icefd, &efds))
6258 {
6259 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006260 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 xsmp_close();
6262 if (--ret == 0)
6263 finished = FALSE; /* keep going if event was only one */
6264 }
6265 else if (FD_ISSET(xsmp_icefd, &rfds))
6266 {
6267 busy = TRUE;
6268 xsmp_handle_requests();
6269 busy = FALSE;
6270 if (--ret == 0)
6271 finished = FALSE; /* keep going if event was only one */
6272 }
6273 }
6274# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006275#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006276 /* also call when ret == 0, we may be polling a keep-open channel */
6277 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006278 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281#endif /* HAVE_SELECT */
6282
6283#ifdef MAY_LOOP
6284 if (finished || msec == 0)
6285 break;
6286
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006287# ifdef FEAT_CLIENTSERVER
6288 if (server_waiting())
6289 break;
6290# endif
6291
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 /* We're going to loop around again, find out for how long */
6293 if (msec > 0)
6294 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006295# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006297 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298# else
6299 /* Guess we got interrupted halfway. */
6300 msec = msec / 2;
6301# endif
6302 if (msec <= 0)
6303 break; /* waited long enough */
6304 }
6305#endif
6306 }
6307
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006308 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309}
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006313 * Expand a path into all matching files and/or directories. Handles "*",
6314 * "?", "[a-z]", "**", etc.
6315 * "path" has backslashes before chars that are not to be expanded.
6316 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 */
6318 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006319mch_expandpath(
6320 garray_T *gap,
6321 char_u *path,
6322 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
Bram Moolenaar02743632005-07-25 20:42:36 +00006324 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325}
6326#endif
6327
6328/*
6329 * mch_expand_wildcards() - this code does wild-card pattern matching using
6330 * the shell
6331 *
6332 * return OK for success, FAIL for error (you may lose some memory) and put
6333 * an error message in *file.
6334 *
6335 * num_pat is number of input patterns
6336 * pat is array of pointers to input patterns
6337 * num_file is pointer to number of matched file names
6338 * file is pointer to array of pointers to matched file names
6339 */
6340
6341#ifndef SEEK_SET
6342# define SEEK_SET 0
6343#endif
6344#ifndef SEEK_END
6345# define SEEK_END 2
6346#endif
6347
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006348#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006349
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006351mch_expand_wildcards(
6352 int num_pat,
6353 char_u **pat,
6354 int *num_file,
6355 char_u ***file,
6356 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 int i;
6359 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006360 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 char_u *p;
6362 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
Bram Moolenaarc7247912008-01-13 12:54:11 +00006364 /*
6365 * This is the non-OS/2 implementation (really Unix).
6366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 int j;
6368 char_u *tempname;
6369 char_u *command;
6370 FILE *fd;
6371 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006372#define STYLE_ECHO 0 /* use "echo", the default */
6373#define STYLE_GLOB 1 /* use "glob", for csh */
6374#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6375#define STYLE_PRINT 3 /* use "print -N", for zsh */
6376#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6377 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 int shell_style = STYLE_ECHO;
6379 int check_spaces;
6380 static int did_find_nul = FALSE;
6381 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006382 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006383 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
6385 *num_file = 0; /* default: no files found */
6386 *file = NULL;
6387
6388 /*
6389 * If there are no wildcards, just copy the names to allocated memory.
6390 * Saves a lot of time, because we don't have to start a new shell.
6391 */
6392 if (!have_wildcard(num_pat, pat))
6393 return save_patterns(num_pat, pat, num_file, file);
6394
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006395# ifdef HAVE_SANDBOX
6396 /* Don't allow any shell command in the sandbox. */
6397 if (sandbox != 0 && check_secure())
6398 return FAIL;
6399# endif
6400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 /*
6402 * Don't allow the use of backticks in secure and restricted mode.
6403 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006404 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 for (i = 0; i < num_pat; ++i)
6406 if (vim_strchr(pat[i], '`') != NULL
6407 && (check_restricted() || check_secure()))
6408 return FAIL;
6409
6410 /*
6411 * get a name for the temp file
6412 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006413 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 {
6415 EMSG(_(e_notmp));
6416 return FAIL;
6417 }
6418
6419 /*
6420 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006421 * file.
6422 * STYLE_BT: NL separated
6423 * If expanding `cmd` execute it directly.
6424 * STYLE_GLOB: NUL separated
6425 * If we use *csh, "glob" will work better than "echo".
6426 * STYLE_PRINT: NL or NUL separated
6427 * If we use *zsh, "print -N" will work better than "glob".
6428 * STYLE_VIMGLOB: NL separated
6429 * If we use *sh*, we define "vimglob()".
6430 * STYLE_ECHO: space separated.
6431 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 */
6433 if (num_pat == 1 && *pat[0] == '`'
6434 && (len = STRLEN(pat[0])) > 2
6435 && *(pat[0] + len - 1) == '`')
6436 shell_style = STYLE_BT;
6437 else if ((len = STRLEN(p_sh)) >= 3)
6438 {
6439 if (STRCMP(p_sh + len - 3, "csh") == 0)
6440 shell_style = STYLE_GLOB;
6441 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6442 shell_style = STYLE_PRINT;
6443 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006444 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6445 "sh") != NULL)
6446 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447
Bram Moolenaarc7247912008-01-13 12:54:11 +00006448 /* Compute the length of the command. We need 2 extra bytes: for the
6449 * optional '&' and for the NUL.
6450 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006452 if (shell_style == STYLE_VIMGLOB)
6453 len += STRLEN(sh_vimglob_func);
6454
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006455 for (i = 0; i < num_pat; ++i)
6456 {
6457 /* Count the length of the patterns in the same way as they are put in
6458 * "command" below. */
6459#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006461#else
6462 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006463 for (j = 0; pat[i][j] != NUL; ++j)
6464 {
6465 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6466 ++len; /* may add a backslash */
6467 ++len;
6468 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006469#endif
6470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 command = alloc(len);
6472 if (command == NULL)
6473 {
6474 /* out of memory */
6475 vim_free(tempname);
6476 return FAIL;
6477 }
6478
6479 /*
6480 * Build the shell command:
6481 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6482 * recognizes this).
6483 * - Add the shell command to print the expanded names.
6484 * - Add the temp file name.
6485 * - Add the file name patterns.
6486 */
6487 if (shell_style == STYLE_BT)
6488 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006489 /* change `command; command& ` to (command; command ) */
6490 STRCPY(command, "(");
6491 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006493 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006494 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 --p;
6496 if (*p == '&') /* remove trailing '&' */
6497 {
6498 ampersent = TRUE;
6499 *p = ' ';
6500 }
6501 STRCAT(command, ">");
6502 }
6503 else
6504 {
6505 if (flags & EW_NOTFOUND)
6506 STRCPY(command, "set nonomatch; ");
6507 else
6508 STRCPY(command, "unset nonomatch; ");
6509 if (shell_style == STYLE_GLOB)
6510 STRCAT(command, "glob >");
6511 else if (shell_style == STYLE_PRINT)
6512 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006513 else if (shell_style == STYLE_VIMGLOB)
6514 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 else
6516 STRCAT(command, "echo >");
6517 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006518
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if (shell_style != STYLE_BT)
6522 for (i = 0; i < num_pat; ++i)
6523 {
6524 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006525 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006526 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527#ifdef USE_SYSTEM
6528 STRCAT(command, " \"");
6529 STRCAT(command, pat[i]);
6530 STRCAT(command, "\"");
6531#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006532 int intick = FALSE;
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 p = command + STRLEN(command);
6535 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006536 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006537 {
6538 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006539 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006540 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6541 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006542 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006543 * backslash inside backticks, before a special character
6544 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006545 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006546 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6547 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006548 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006549 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006550 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006551 else if (!intick
6552 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6553 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006554 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006555 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6556 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006557 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006558
6559 /* Copy one character. */
6560 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 *p = NUL;
6563#endif
6564 }
6565 if (flags & EW_SILENT)
6566 show_shell_mess = FALSE;
6567 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006568 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 /*
6571 * Using zsh -G: If a pattern has no matches, it is just deleted from
6572 * the argument list, otherwise zsh gives an error message and doesn't
6573 * expand any other pattern.
6574 */
6575 if (shell_style == STYLE_PRINT)
6576 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6577
6578 /*
6579 * If we use -f then shell variables set in .cshrc won't get expanded.
6580 * vi can do it, so we will too, but it is only necessary if there is a "$"
6581 * in one of the patterns, otherwise we can still use the fast option.
6582 */
6583 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6584 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6585
6586 /*
6587 * execute the shell command
6588 */
6589 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6590
6591 /* When running in the background, give it some time to create the temp
6592 * file, but don't wait for it to finish. */
6593 if (ampersent)
6594 mch_delay(10L, TRUE);
6595
6596 extra_shell_arg = NULL; /* cleanup */
6597 show_shell_mess = TRUE;
6598 vim_free(command);
6599
Bram Moolenaarc7247912008-01-13 12:54:11 +00006600 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
6602 mch_remove(tempname);
6603 vim_free(tempname);
6604 /*
6605 * With interactive completion, the error message is not printed.
6606 * However with USE_SYSTEM, I don't know how to turn off error messages
6607 * from the shell, so screen may still get messed up -- webb.
6608 */
6609#ifndef USE_SYSTEM
6610 if (!(flags & EW_SILENT))
6611#endif
6612 {
6613 redraw_later_clear(); /* probably messed up screen */
6614 msg_putchar('\n'); /* clear bottom line quickly */
6615 cmdline_row = Rows - 1; /* continue on last line */
6616#ifdef USE_SYSTEM
6617 if (!(flags & EW_SILENT))
6618#endif
6619 {
6620 MSG(_(e_wildexpand));
6621 msg_start(); /* don't overwrite this message */
6622 }
6623 }
6624 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6625 * EW_NOTFOUND is given */
6626 if (shell_style == STYLE_BT)
6627 return FAIL;
6628 goto notfound;
6629 }
6630
6631 /*
6632 * read the names from the file into memory
6633 */
6634 fd = fopen((char *)tempname, READBIN);
6635 if (fd == NULL)
6636 {
6637 /* Something went wrong, perhaps a file name with a special char. */
6638 if (!(flags & EW_SILENT))
6639 {
6640 MSG(_(e_wildexpand));
6641 msg_start(); /* don't overwrite this message */
6642 }
6643 vim_free(tempname);
6644 goto notfound;
6645 }
6646 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006647 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006649 if (llen < 0)
6650 /* just in case ftell() would fail */
6651 buffer = NULL;
6652 else
6653 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (buffer == NULL)
6655 {
6656 /* out of memory */
6657 mch_remove(tempname);
6658 vim_free(tempname);
6659 fclose(fd);
6660 return FAIL;
6661 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006662 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 i = fread((char *)buffer, 1, len, fd);
6664 fclose(fd);
6665 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006666 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 /* unexpected read error */
6669 EMSG2(_(e_notread), tempname);
6670 vim_free(tempname);
6671 vim_free(buffer);
6672 return FAIL;
6673 }
6674 vim_free(tempname);
6675
Bram Moolenaarc7247912008-01-13 12:54:11 +00006676# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6678 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006679 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6681 *p++ = buffer[i];
6682 len = p - buffer;
6683# endif
6684
6685
6686 /* file names are separated with Space */
6687 if (shell_style == STYLE_ECHO)
6688 {
6689 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6690 p = buffer;
6691 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6692 {
6693 while (*p != ' ' && *p != '\n')
6694 ++p;
6695 p = skipwhite(p); /* skip to next entry */
6696 }
6697 }
6698 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006699 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
6701 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6702 p = buffer;
6703 for (i = 0; *p != NUL; ++i) /* count number of entries */
6704 {
6705 while (*p != '\n' && *p != NUL)
6706 ++p;
6707 if (*p != NUL)
6708 ++p;
6709 p = skipwhite(p); /* skip leading white space */
6710 }
6711 }
6712 /* file names are separated with NUL */
6713 else
6714 {
6715 /*
6716 * Some versions of zsh use spaces instead of NULs to separate
6717 * results. Only do this when there is no NUL before the end of the
6718 * buffer, otherwise we would never be able to use file names with
6719 * embedded spaces when zsh does use NULs.
6720 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6721 * don't check for spaces again.
6722 */
6723 check_spaces = FALSE;
6724 if (shell_style == STYLE_PRINT && !did_find_nul)
6725 {
6726 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006727 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006728 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 did_find_nul = TRUE;
6730 else
6731 check_spaces = TRUE;
6732 }
6733
6734 /*
6735 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6736 * already is one, for STYLE_GLOB it needs to be added.
6737 */
6738 if (len && buffer[len - 1] == NUL)
6739 --len;
6740 else
6741 buffer[len] = NUL;
6742 i = 0;
6743 for (p = buffer; p < buffer + len; ++p)
6744 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6745 {
6746 ++i;
6747 *p = NUL;
6748 }
6749 if (len)
6750 ++i; /* count last entry */
6751 }
6752 if (i == 0)
6753 {
6754 /*
6755 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6756 * /bin/sh will happily expand it to nothing rather than returning an
6757 * error; and hey, it's good to check anyway -- webb.
6758 */
6759 vim_free(buffer);
6760 goto notfound;
6761 }
6762 *num_file = i;
6763 *file = (char_u **)alloc(sizeof(char_u *) * i);
6764 if (*file == NULL)
6765 {
6766 /* out of memory */
6767 vim_free(buffer);
6768 return FAIL;
6769 }
6770
6771 /*
6772 * Isolate the individual file names.
6773 */
6774 p = buffer;
6775 for (i = 0; i < *num_file; ++i)
6776 {
6777 (*file)[i] = p;
6778 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006779 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6780 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006782 while (!(shell_style == STYLE_ECHO && *p == ' ')
6783 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 ++p;
6785 if (p == buffer + len) /* last entry */
6786 *p = NUL;
6787 else
6788 {
6789 *p++ = NUL;
6790 p = skipwhite(p); /* skip to next entry */
6791 }
6792 }
6793 else /* NUL separates */
6794 {
6795 while (*p && p < buffer + len) /* skip entry */
6796 ++p;
6797 ++p; /* skip NUL */
6798 }
6799 }
6800
6801 /*
6802 * Move the file names to allocated memory.
6803 */
6804 for (j = 0, i = 0; i < *num_file; ++i)
6805 {
6806 /* Require the files to exist. Helps when using /bin/sh */
6807 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6808 continue;
6809
6810 /* check if this entry should be included */
6811 dir = (mch_isdir((*file)[i]));
6812 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6813 continue;
6814
Bram Moolenaara2031822006-03-07 22:29:51 +00006815 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006816 if (!dir && (flags & EW_EXEC)
6817 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006818 continue;
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6821 if (p)
6822 {
6823 STRCPY(p, (*file)[i]);
6824 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006825 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 (*file)[j++] = p;
6827 }
6828 }
6829 vim_free(buffer);
6830 *num_file = j;
6831
6832 if (*num_file == 0) /* rejected all entries */
6833 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006834 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 goto notfound;
6836 }
6837
6838 return OK;
6839
6840notfound:
6841 if (flags & EW_NOTFOUND)
6842 return save_patterns(num_pat, pat, num_file, file);
6843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844}
6845
6846#endif /* VMS */
6847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006849save_patterns(
6850 int num_pat,
6851 char_u **pat,
6852 int *num_file,
6853 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006856 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
6858 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6859 if (*file == NULL)
6860 return FAIL;
6861 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006862 {
6863 s = vim_strsave(pat[i]);
6864 if (s != NULL)
6865 /* Be compatible with expand_filename(): halve the number of
6866 * backslashes. */
6867 backslash_halve(s);
6868 (*file)[i] = s;
6869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 *num_file = num_pat;
6871 return OK;
6872}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874/*
6875 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6876 * expand.
6877 */
6878 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006879mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006881 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (*p == '\\' && p[1] != NUL)
6884 ++p;
6885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (vim_strchr((char_u *)
6887#ifdef VMS
6888 "*?%"
6889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#endif
6892 , *p) != NULL)
6893 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
6895 return FALSE;
6896}
6897
6898/*
6899 * Return TRUE if the string "p" contains a wildcard.
6900 * Don't recognize '~' at the end as a wildcard.
6901 */
6902 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006903mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006905 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (*p == '\\' && p[1] != NUL)
6908 ++p;
6909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 if (vim_strchr((char_u *)
6911#ifdef VMS
6912 "*?%$"
6913#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915#endif
6916 , *p) != NULL
6917 || (*p == '~' && p[1] != NUL))
6918 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 }
6920 return FALSE;
6921}
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006924have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 int i;
6927
6928 for (i = 0; i < num; i++)
6929 if (mch_has_wildcard(file[i]))
6930 return 1;
6931 return 0;
6932}
6933
6934 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006935have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int i;
6938
6939 for (i = 0; i < num; i++)
6940 if (vim_strchr(file[i], '$') != NULL)
6941 return TRUE;
6942 return FALSE;
6943}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006945#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946/*
6947 * Scaled-down version of rename(), which is missing in Xenix.
6948 * This version can only move regular files and will fail if the
6949 * destination exists.
6950 */
6951 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006952mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 struct stat st;
6955
6956 if (stat(dest, &st) >= 0) /* fail if destination exists */
6957 return -1;
6958 if (link(src, dest) != 0) /* link file to new name */
6959 return -1;
6960 if (mch_remove(src) == 0) /* delete link to old name */
6961 return 0;
6962 return -1;
6963}
6964#endif /* !HAVE_RENAME */
6965
6966#ifdef FEAT_MOUSE_GPM
6967/*
6968 * Initializes connection with gpm (if it isn't already opened)
6969 * Return 1 if succeeded (or connection already opened), 0 if failed
6970 */
6971 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006972gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6975
6976 if (!gpm_flag)
6977 {
6978 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6979 gpm_connect.defaultMask = ~GPM_HARD;
6980 /* Default handling for mouse move*/
6981 gpm_connect.minMod = 0; /* Handle any modifier keys */
6982 gpm_connect.maxMod = 0xffff;
6983 if (Gpm_Open(&gpm_connect, 0) > 0)
6984 {
6985 /* gpm library tries to handling TSTP causes
6986 * problems. Anyways, we close connection to Gpm whenever
6987 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006988 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006990# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 return 1; /* succeed */
6994 }
6995 if (gpm_fd == -2)
6996 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6997 return 0;
6998 }
6999 return 1; /* already open */
7000}
7001
7002/*
7003 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 */
7005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007006gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 if (gpm_flag && gpm_fd >= 0) /* if Open */
7009 Gpm_Close();
7010}
7011
7012/* Reads gpm event and adds special keys to input buf. Returns length of
7013 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007014 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 */
7016 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007017mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 int button;
7020 static Gpm_Event gpm_event;
7021 char_u string[6];
7022 int_u vim_modifiers;
7023 int row,col;
7024 unsigned char buttons_mask;
7025 unsigned char gpm_modifiers;
7026 static unsigned char old_buttons = 0;
7027
7028 Gpm_GetEvent(&gpm_event);
7029
7030#ifdef FEAT_GUI
7031 /* Don't put events in the input queue now. */
7032 if (hold_gui_events)
7033 return 0;
7034#endif
7035
7036 row = gpm_event.y - 1;
7037 col = gpm_event.x - 1;
7038
7039 string[0] = ESC; /* Our termcode */
7040 string[1] = 'M';
7041 string[2] = 'G';
7042 switch (GPM_BARE_EVENTS(gpm_event.type))
7043 {
7044 case GPM_DRAG:
7045 string[3] = MOUSE_DRAG;
7046 break;
7047 case GPM_DOWN:
7048 buttons_mask = gpm_event.buttons & ~old_buttons;
7049 old_buttons = gpm_event.buttons;
7050 switch (buttons_mask)
7051 {
7052 case GPM_B_LEFT:
7053 button = MOUSE_LEFT;
7054 break;
7055 case GPM_B_MIDDLE:
7056 button = MOUSE_MIDDLE;
7057 break;
7058 case GPM_B_RIGHT:
7059 button = MOUSE_RIGHT;
7060 break;
7061 default:
7062 return 0;
7063 /*Don't know what to do. Can more than one button be
7064 * reported in one event? */
7065 }
7066 string[3] = (char_u)(button | 0x20);
7067 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7068 break;
7069 case GPM_UP:
7070 string[3] = MOUSE_RELEASE;
7071 old_buttons &= ~gpm_event.buttons;
7072 break;
7073 default:
7074 return 0;
7075 }
7076 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7077 gpm_modifiers = gpm_event.modifiers;
7078 vim_modifiers = 0x0;
7079 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7080 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7081 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7082 */
7083 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7084 vim_modifiers |= MOUSE_SHIFT;
7085
7086 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7087 vim_modifiers |= MOUSE_CTRL;
7088 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7089 vim_modifiers |= MOUSE_ALT;
7090 string[3] |= vim_modifiers;
7091 string[4] = (char_u)(col + ' ' + 1);
7092 string[5] = (char_u)(row + ' ' + 1);
7093 add_to_input_buf(string, 6);
7094 return 6;
7095}
7096#endif /* FEAT_MOUSE_GPM */
7097
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007098#ifdef FEAT_SYSMOUSE
7099/*
7100 * Initialize connection with sysmouse.
7101 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7102 * output, any sysmouse output than will be processed via sig_sysmouse().
7103 * Return OK if succeeded, FAIL if failed.
7104 */
7105 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007106sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007107{
7108 struct mouse_info mouse;
7109
7110 mouse.operation = MOUSE_MODE;
7111 mouse.u.mode.mode = 0;
7112 mouse.u.mode.signal = SIGUSR2;
7113 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7114 {
7115 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7116 mouse.operation = MOUSE_SHOW;
7117 ioctl(1, CONS_MOUSECTL, &mouse);
7118 return OK;
7119 }
7120 return FAIL;
7121}
7122
7123/*
7124 * Stop processing SIGUSR2 signals, and also make sure that
7125 * virtual console do not send us any sysmouse related signal.
7126 */
7127 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007128sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007129{
7130 struct mouse_info mouse;
7131
7132 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7133 mouse.operation = MOUSE_MODE;
7134 mouse.u.mode.mode = 0;
7135 mouse.u.mode.signal = 0;
7136 ioctl(1, CONS_MOUSECTL, &mouse);
7137}
7138
7139/*
7140 * Gets info from sysmouse and adds special keys to input buf.
7141 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007142 static RETSIGTYPE
7143sig_sysmouse SIGDEFARG(sigarg)
7144{
7145 struct mouse_info mouse;
7146 struct video_info video;
7147 char_u string[6];
7148 int row, col;
7149 int button;
7150 int buttons;
7151 static int oldbuttons = 0;
7152
7153#ifdef FEAT_GUI
7154 /* Don't put events in the input queue now. */
7155 if (hold_gui_events)
7156 return;
7157#endif
7158
7159 mouse.operation = MOUSE_GETINFO;
7160 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7161 && ioctl(1, FBIO_MODEINFO, &video) != -1
7162 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7163 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7164 {
7165 row = mouse.u.data.y / video.vi_cheight;
7166 col = mouse.u.data.x / video.vi_cwidth;
7167 buttons = mouse.u.data.buttons;
7168 string[0] = ESC; /* Our termcode */
7169 string[1] = 'M';
7170 string[2] = 'S';
7171 if (oldbuttons == buttons && buttons != 0)
7172 {
7173 button = MOUSE_DRAG;
7174 }
7175 else
7176 {
7177 switch (buttons)
7178 {
7179 case 0:
7180 button = MOUSE_RELEASE;
7181 break;
7182 case 1:
7183 button = MOUSE_LEFT;
7184 break;
7185 case 2:
7186 button = MOUSE_MIDDLE;
7187 break;
7188 case 4:
7189 button = MOUSE_RIGHT;
7190 break;
7191 default:
7192 return;
7193 }
7194 oldbuttons = buttons;
7195 }
7196 string[3] = (char_u)(button);
7197 string[4] = (char_u)(col + ' ' + 1);
7198 string[5] = (char_u)(row + ' ' + 1);
7199 add_to_input_buf(string, 6);
7200 }
7201 return;
7202}
7203#endif /* FEAT_SYSMOUSE */
7204
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007206typedef char_u * (*STRPROCSTR)(char_u *);
7207typedef char_u * (*INTPROCSTR)(int);
7208typedef int (*STRPROCINT)(char_u *);
7209typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
7211/*
7212 * Call a DLL routine which takes either a string or int param
7213 * and returns an allocated string.
7214 */
7215 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007216mch_libcall(
7217 char_u *libname,
7218 char_u *funcname,
7219 char_u *argstring, /* NULL when using a argint */
7220 int argint,
7221 char_u **string_result,/* NULL when using number_result */
7222 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224# if defined(USE_DLOPEN)
7225 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007226 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227# else
7228 shl_t hinstLib;
7229# endif
7230 STRPROCSTR ProcAdd;
7231 INTPROCSTR ProcAddI;
7232 char_u *retval_str = NULL;
7233 int retval_int = 0;
7234 int success = FALSE;
7235
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007236 /*
7237 * Get a handle to the DLL module.
7238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007240 /* First clear any error, it's not cleared by the dlopen() call. */
7241 (void)dlerror();
7242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 hinstLib = dlopen((char *)libname, RTLD_LAZY
7244# ifdef RTLD_LOCAL
7245 | RTLD_LOCAL
7246# endif
7247 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007248 if (hinstLib == NULL)
7249 {
7250 /* "dlerr" must be used before dlclose() */
7251 dlerr = (char *)dlerror();
7252 if (dlerr != NULL)
7253 EMSG2(_("dlerror = \"%s\""), dlerr);
7254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255# else
7256 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7257# endif
7258
7259 /* If the handle is valid, try to get the function address. */
7260 if (hinstLib != NULL)
7261 {
7262# ifdef HAVE_SETJMP_H
7263 /*
7264 * Catch a crash when calling the library function. For example when
7265 * using a number where a string pointer is expected.
7266 */
7267 mch_startjmp();
7268 if (SETJMP(lc_jump_env) != 0)
7269 {
7270 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007271# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007272 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 mch_didjmp();
7275 }
7276 else
7277# endif
7278 {
7279 retval_str = NULL;
7280 retval_int = 0;
7281
7282 if (argstring != NULL)
7283 {
7284# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007285 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007286 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287# else
7288 if (shl_findsym(&hinstLib, (const char *)funcname,
7289 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7290 ProcAdd = NULL;
7291# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007292 if ((success = (ProcAdd != NULL
7293# if defined(USE_DLOPEN)
7294 && dlerr == NULL
7295# endif
7296 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 if (string_result == NULL)
7299 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7300 else
7301 retval_str = (ProcAdd)(argstring);
7302 }
7303 }
7304 else
7305 {
7306# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007307 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007308 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309# else
7310 if (shl_findsym(&hinstLib, (const char *)funcname,
7311 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7312 ProcAddI = NULL;
7313# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007314 if ((success = (ProcAddI != NULL
7315# if defined(USE_DLOPEN)
7316 && dlerr == NULL
7317# endif
7318 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
7320 if (string_result == NULL)
7321 retval_int = ((INTPROCINT)ProcAddI)(argint);
7322 else
7323 retval_str = (ProcAddI)(argint);
7324 }
7325 }
7326
7327 /* Save the string before we free the library. */
7328 /* Assume that a "1" or "-1" result is an illegal pointer. */
7329 if (string_result == NULL)
7330 *number_result = retval_int;
7331 else if (retval_str != NULL
7332 && retval_str != (char_u *)1
7333 && retval_str != (char_u *)-1)
7334 *string_result = vim_strsave(retval_str);
7335 }
7336
7337# ifdef HAVE_SETJMP_H
7338 mch_endjmp();
7339# ifdef SIGHASARG
7340 if (lc_signal != 0)
7341 {
7342 int i;
7343
7344 /* try to find the name of this signal */
7345 for (i = 0; signal_info[i].sig != -1; i++)
7346 if (lc_signal == signal_info[i].sig)
7347 break;
7348 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7349 }
7350# endif
7351# endif
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007354 /* "dlerr" must be used before dlclose() */
7355 if (dlerr != NULL)
7356 EMSG2(_("dlerror = \"%s\""), dlerr);
7357
7358 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 (void)dlclose(hinstLib);
7360# else
7361 (void)shl_unload(hinstLib);
7362# endif
7363 }
7364
7365 if (!success)
7366 {
7367 EMSG2(_(e_libcall), funcname);
7368 return FAIL;
7369 }
7370
7371 return OK;
7372}
7373#endif
7374
7375#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7376static int xterm_trace = -1; /* default: disabled */
7377static int xterm_button;
7378
7379/*
7380 * Setup a dummy window for X selections in a terminal.
7381 */
7382 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007383setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
7385 int z = 0;
7386 char *strp = "";
7387 Widget AppShell;
7388
7389 if (!x_connect_to_server())
7390 return;
7391
7392 open_app_context();
7393 if (app_context != NULL && xterm_Shell == (Widget)0)
7394 {
7395 int (*oldhandler)();
7396#if defined(HAVE_SETJMP_H)
7397 int (*oldIOhandler)();
7398#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007399# ifdef ELAPSED_FUNC
7400 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007403 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404# endif
7405
7406 /* Ignore X errors while opening the display */
7407 oldhandler = XSetErrorHandler(x_error_check);
7408
7409#if defined(HAVE_SETJMP_H)
7410 /* Ignore X IO errors while opening the display */
7411 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7412 mch_startjmp();
7413 if (SETJMP(lc_jump_env) != 0)
7414 {
7415 mch_didjmp();
7416 xterm_dpy = NULL;
7417 }
7418 else
7419#endif
7420 {
7421 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7422 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7423#if defined(HAVE_SETJMP_H)
7424 mch_endjmp();
7425#endif
7426 }
7427
7428#if defined(HAVE_SETJMP_H)
7429 /* Now handle X IO errors normally. */
7430 (void)XSetIOErrorHandler(oldIOhandler);
7431#endif
7432 /* Now handle X errors normally. */
7433 (void)XSetErrorHandler(oldhandler);
7434
7435 if (xterm_dpy == NULL)
7436 {
7437 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007438 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 return;
7440 }
7441
7442 /* Catch terminating error of the X server connection. */
7443 (void)XSetIOErrorHandler(x_IOerror_handler);
7444
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007445# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007447 {
7448 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007449 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007450 verbose_leave();
7451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452# endif
7453
7454 /* Create a Shell to make converters work. */
7455 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7456 applicationShellWidgetClass, xterm_dpy,
7457 NULL);
7458 if (AppShell == (Widget)0)
7459 return;
7460 xterm_Shell = XtVaCreatePopupShell("VIM",
7461 topLevelShellWidgetClass, AppShell,
7462 XtNmappedWhenManaged, 0,
7463 XtNwidth, 1,
7464 XtNheight, 1,
7465 NULL);
7466 if (xterm_Shell == (Widget)0)
7467 return;
7468
7469 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007470 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (x11_display == NULL)
7472 x11_display = xterm_dpy;
7473
7474 XtRealizeWidget(xterm_Shell);
7475 XSync(xterm_dpy, False);
7476 xterm_update();
7477 }
7478 if (xterm_Shell != (Widget)0)
7479 {
7480 clip_init(TRUE);
7481 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7482 x11_window = (Window)atol(strp);
7483 /* Check if $WINDOWID is valid. */
7484 if (test_x11_window(xterm_dpy) == FAIL)
7485 x11_window = 0;
7486 if (x11_window != 0)
7487 xterm_trace = 0;
7488 }
7489}
7490
7491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007492start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7495 return;
7496 xterm_trace = 1;
7497 xterm_button = button;
7498 do_xterm_trace();
7499}
7500
7501
7502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007503stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
7505 if (xterm_trace < 0)
7506 return;
7507 xterm_trace = 0;
7508}
7509
7510/*
7511 * Query the xterm pointer and generate mouse termcodes if necessary
7512 * return TRUE if dragging is active, else FALSE
7513 */
7514 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007515do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 Window root, child;
7518 int root_x, root_y;
7519 int win_x, win_y;
7520 int row, col;
7521 int_u mask_return;
7522 char_u buf[50];
7523 char_u *strp;
7524 long got_hints;
7525 static char_u *mouse_code;
7526 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7527 static int prev_row = 0, prev_col = 0;
7528 static XSizeHints xterm_hints;
7529
7530 if (xterm_trace <= 0)
7531 return FALSE;
7532
7533 if (xterm_trace == 1)
7534 {
7535 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007536 * have changed recently. */
7537 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7538 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 || xterm_hints.width_inc <= 1
7540 || xterm_hints.height_inc <= 1)
7541 {
7542 xterm_trace = -1; /* Not enough data -- disable tracing */
7543 return FALSE;
7544 }
7545
7546 /* Rely on the same mouse code for the duration of this */
7547 mouse_code = find_termcode(mouse_name);
7548 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007549 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 xterm_trace = 2;
7551
7552 /* Find the offset of the chars, there might be a scrollbar on the
7553 * left of the window and/or a menu on the top (eterm etc.) */
7554 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7555 &win_x, &win_y, &mask_return);
7556 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7557 - (xterm_hints.height_inc / 2);
7558 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7559 xterm_hints.y = 2;
7560 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7561 - (xterm_hints.width_inc / 2);
7562 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7563 xterm_hints.x = 2;
7564 return TRUE;
7565 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007566 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {
7568 xterm_trace = 0;
7569 return FALSE;
7570 }
7571
7572 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7573 &win_x, &win_y, &mask_return);
7574
7575 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7576 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7577 if (row == prev_row && col == prev_col)
7578 return TRUE;
7579
7580 STRCPY(buf, mouse_code);
7581 strp = buf + STRLEN(buf);
7582 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7583 *strp++ = (char_u)(col + ' ' + 1);
7584 *strp++ = (char_u)(row + ' ' + 1);
7585 *strp = 0;
7586 add_to_input_buf(buf, STRLEN(buf));
7587
7588 prev_row = row;
7589 prev_col = col;
7590 return TRUE;
7591}
7592
7593# if defined(FEAT_GUI) || defined(PROTO)
7594/*
7595 * Destroy the display, window and app_context. Required for GTK.
7596 */
7597 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007598clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
7600 if (xterm_Shell != (Widget)0)
7601 {
7602 XtDestroyWidget(xterm_Shell);
7603 xterm_Shell = (Widget)0;
7604 }
7605 if (xterm_dpy != NULL)
7606 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007607# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 /* Lesstif and Solaris crash here, lose some memory */
7609 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 if (x11_display == xterm_dpy)
7612 x11_display = NULL;
7613 xterm_dpy = NULL;
7614 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007615# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 if (app_context != (XtAppContext)NULL)
7617 {
7618 /* Lesstif and Solaris crash here, lose some memory */
7619 XtDestroyApplicationContext(app_context);
7620 app_context = (XtAppContext)NULL;
7621 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624# endif
7625
7626/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007627 * Catch up with GUI or X events.
7628 */
7629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007630clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007631{
7632# ifdef FEAT_GUI
7633 if (gui.in_use)
7634 gui_mch_update();
7635 else
7636# endif
7637 if (xterm_Shell != (Widget)0)
7638 xterm_update();
7639}
7640
7641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 * Catch up with any queued X events. This may put keyboard input into the
7643 * input buffer, call resize call-backs, trigger timers etc. If there is
7644 * nothing in the X event queue (& no timers pending), then we return
7645 * immediately.
7646 */
7647 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007648xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 XEvent event;
7651
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007652 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007654 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007656 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007657 break;
7658
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007659 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007660 {
7661 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007662 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007663#ifdef FEAT_CLIENTSERVER
7664 {
7665 XPropertyEvent *e = (XPropertyEvent *)&event;
7666
7667 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007669 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007672 XtDispatchEvent(&event);
7673 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007674 else
7675 {
7676 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007677 XtAppProcessEvent(app_context, mask);
7678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680}
7681
7682 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007683clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 if (xterm_Shell != (Widget)0)
7686 return clip_x11_own_selection(xterm_Shell, cbd);
7687 return FAIL;
7688}
7689
7690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007691clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 if (xterm_Shell != (Widget)0)
7694 clip_x11_lose_selection(xterm_Shell, cbd);
7695}
7696
7697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007698clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 if (xterm_Shell != (Widget)0)
7701 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7702}
7703
7704 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007705clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 clip_x11_set_selection(cbd);
7708}
7709#endif
7710
7711
7712#if defined(USE_XSMP) || defined(PROTO)
7713/*
7714 * Code for X Session Management Protocol.
7715 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007716static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7717static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7718static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7719static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7720static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
7722
7723# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007724static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726/*
7727 * This is our chance to ask the user if they want to save,
7728 * or abort the logout
7729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007731xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 cmdmod_T save_cmdmod;
7734 int cancel_shutdown = False;
7735
7736 save_cmdmod = cmdmod;
7737 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007738 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 /* Mustn't logout */
7740 cancel_shutdown = True;
7741 cmdmod = save_cmdmod;
7742 setcursor(); /* position cursor */
7743 out_flush();
7744
7745 /* Done interaction */
7746 SmcInteractDone(smc_conn, cancel_shutdown);
7747
7748 /* Finish off
7749 * Only end save-yourself here if we're not cancelling shutdown;
7750 * we'll get a cancelled callback later in which we'll end it.
7751 * Hopefully get around glitchy SMs (like GNOME-1)
7752 */
7753 if (!cancel_shutdown)
7754 {
7755 xsmp.save_yourself = False;
7756 SmcSaveYourselfDone(smc_conn, True);
7757 }
7758}
7759# endif
7760
7761/*
7762 * Callback that starts save-yourself.
7763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007765xsmp_handle_save_yourself(
7766 SmcConn smc_conn,
7767 SmPointer client_data UNUSED,
7768 int save_type UNUSED,
7769 Bool shutdown,
7770 int interact_style UNUSED,
7771 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 /* Handle already being in saveyourself */
7774 if (xsmp.save_yourself)
7775 SmcSaveYourselfDone(smc_conn, True);
7776 xsmp.save_yourself = True;
7777 xsmp.shutdown = shutdown;
7778
7779 /* First up, preserve all files */
7780 out_flush();
7781 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7782
7783 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007784 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
7786# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7787 /* Now see if we can ask about unsaved files */
7788 if (shutdown && !fast && gui.in_use)
7789 /* Need to interact with user, but need SM's permission */
7790 SmcInteractRequest(smc_conn, SmDialogError,
7791 xsmp_handle_interaction, client_data);
7792 else
7793# endif
7794 {
7795 /* Can stop the cycle here */
7796 SmcSaveYourselfDone(smc_conn, True);
7797 xsmp.save_yourself = False;
7798 }
7799}
7800
7801
7802/*
7803 * Callback to warn us of imminent death.
7804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007806xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 xsmp_close();
7809
7810 /* quit quickly leaving swapfiles for modified buffers behind */
7811 getout_preserve_modified(0);
7812}
7813
7814
7815/*
7816 * Callback to tell us that save-yourself has completed.
7817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007819xsmp_save_complete(
7820 SmcConn smc_conn UNUSED,
7821 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822{
7823 xsmp.save_yourself = False;
7824}
7825
7826
7827/*
7828 * Callback to tell us that an instigated shutdown was cancelled
7829 * (maybe even by us)
7830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007832xsmp_shutdown_cancelled(
7833 SmcConn smc_conn,
7834 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 if (xsmp.save_yourself)
7837 SmcSaveYourselfDone(smc_conn, True);
7838 xsmp.save_yourself = False;
7839 xsmp.shutdown = False;
7840}
7841
7842
7843/*
7844 * Callback to tell us that a new ICE connection has been established.
7845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007847xsmp_ice_connection(
7848 IceConn iceConn,
7849 IcePointer clientData UNUSED,
7850 Bool opening,
7851 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 /* Intercept creation of ICE connection fd */
7854 if (opening)
7855 {
7856 xsmp_icefd = IceConnectionNumber(iceConn);
7857 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7858 }
7859}
7860
7861
7862/* Handle any ICE processing that's required; return FAIL if SM lost */
7863 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007864xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 Bool rep;
7867
7868 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7869 == IceProcessMessagesIOError)
7870 {
7871 /* Lost ICE */
7872 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007873 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 xsmp_close();
7875 return FAIL;
7876 }
7877 else
7878 return OK;
7879}
7880
7881static int dummy;
7882
7883/* Set up X Session Management Protocol */
7884 void
7885xsmp_init(void)
7886{
7887 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 SmcCallbacks smcallbacks;
7889#if 0
7890 SmPropValue smname;
7891 SmProp smnameprop;
7892 SmProp *smprops[1];
7893#endif
7894
7895 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007896 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
7898 xsmp.save_yourself = xsmp.shutdown = False;
7899
7900 /* Set up SM callbacks - must have all, even if they're not used */
7901 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7902 smcallbacks.save_yourself.client_data = NULL;
7903 smcallbacks.die.callback = xsmp_die;
7904 smcallbacks.die.client_data = NULL;
7905 smcallbacks.save_complete.callback = xsmp_save_complete;
7906 smcallbacks.save_complete.client_data = NULL;
7907 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7908 smcallbacks.shutdown_cancelled.client_data = NULL;
7909
7910 /* Set up a watch on ICE connection creations. The "dummy" argument is
7911 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7912 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7913 {
7914 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007915 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 return;
7917 }
7918
7919 /* Create an SM connection */
7920 xsmp.smcconn = SmcOpenConnection(
7921 NULL,
7922 NULL,
7923 SmProtoMajor,
7924 SmProtoMinor,
7925 SmcSaveYourselfProcMask | SmcDieProcMask
7926 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7927 &smcallbacks,
7928 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007929 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 sizeof(errorstring),
7931 errorstring);
7932 if (xsmp.smcconn == NULL)
7933 {
7934 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007935
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007937 {
7938 vim_snprintf(errorreport, sizeof(errorreport),
7939 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7940 verb_msg((char_u *)errorreport);
7941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 return;
7943 }
7944 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7945
7946#if 0
7947 /* ID ourselves */
7948 smname.value = "vim";
7949 smname.length = 3;
7950 smnameprop.name = "SmProgram";
7951 smnameprop.type = "SmARRAY8";
7952 smnameprop.num_vals = 1;
7953 smnameprop.vals = &smname;
7954
7955 smprops[0] = &smnameprop;
7956 SmcSetProperties(xsmp.smcconn, 1, smprops);
7957#endif
7958}
7959
7960
7961/* Shut down XSMP comms. */
7962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007963xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 if (xsmp_icefd != -1)
7966 {
7967 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007968 if (xsmp.clientid != NULL)
7969 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007970 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 xsmp_icefd = -1;
7972 }
7973}
7974#endif /* USE_XSMP */
7975
7976
7977#ifdef EBCDIC
7978/* Translate character to its CTRL- value */
7979char CtrlTable[] =
7980{
7981/* 00 - 5E */
7982 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7983 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7984 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7985 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7986 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7987 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7988/* ^ */ 0x1E,
7989/* - */ 0x1F,
7990/* 61 - 6C */
7991 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7992/* _ */ 0x1F,
7993/* 6E - 80 */
7994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7995/* a */ 0x01,
7996/* b */ 0x02,
7997/* c */ 0x03,
7998/* d */ 0x37,
7999/* e */ 0x2D,
8000/* f */ 0x2E,
8001/* g */ 0x2F,
8002/* h */ 0x16,
8003/* i */ 0x05,
8004/* 8A - 90 */
8005 0, 0, 0, 0, 0, 0, 0,
8006/* j */ 0x15,
8007/* k */ 0x0B,
8008/* l */ 0x0C,
8009/* m */ 0x0D,
8010/* n */ 0x0E,
8011/* o */ 0x0F,
8012/* p */ 0x10,
8013/* q */ 0x11,
8014/* r */ 0x12,
8015/* 9A - A1 */
8016 0, 0, 0, 0, 0, 0, 0, 0,
8017/* s */ 0x13,
8018/* t */ 0x3C,
8019/* u */ 0x3D,
8020/* v */ 0x32,
8021/* w */ 0x26,
8022/* x */ 0x18,
8023/* y */ 0x19,
8024/* z */ 0x3F,
8025/* AA - AC */
8026 0, 0, 0,
8027/* [ */ 0x27,
8028/* AE - BC */
8029 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8030/* ] */ 0x1D,
8031/* BE - C0 */ 0, 0, 0,
8032/* A */ 0x01,
8033/* B */ 0x02,
8034/* C */ 0x03,
8035/* D */ 0x37,
8036/* E */ 0x2D,
8037/* F */ 0x2E,
8038/* G */ 0x2F,
8039/* H */ 0x16,
8040/* I */ 0x05,
8041/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8042/* J */ 0x15,
8043/* K */ 0x0B,
8044/* L */ 0x0C,
8045/* M */ 0x0D,
8046/* N */ 0x0E,
8047/* O */ 0x0F,
8048/* P */ 0x10,
8049/* Q */ 0x11,
8050/* R */ 0x12,
8051/* DA - DF */ 0, 0, 0, 0, 0, 0,
8052/* \ */ 0x1C,
8053/* E1 */ 0,
8054/* S */ 0x13,
8055/* T */ 0x3C,
8056/* U */ 0x3D,
8057/* V */ 0x32,
8058/* W */ 0x26,
8059/* X */ 0x18,
8060/* Y */ 0x19,
8061/* Z */ 0x3F,
8062/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8063 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8064};
8065
8066char MetaCharTable[]=
8067{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8068 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8069 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8070 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8071 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8072};
8073
8074
8075/* TODO: Use characters NOT numbers!!! */
8076char CtrlCharTable[]=
8077{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8078 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8079 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8080 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8081 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8082};
8083
8084
8085#endif