blob: ac89955f252f71b096c3ad0dc0d7b53aa96a0aab [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;
448#ifdef FEAT_AUTOCMD
449 if (trigger_cursorhold() && maxlen >= 3
450 && !typebuf_changed(tb_change_cnt))
451 {
452 buf[0] = K_SPECIAL;
453 buf[1] = KS_EXTRA;
454 buf[2] = (int)KE_CURSORHOLD;
455 return 3;
456 }
457#endif
458 /*
459 * If there is no character available within 'updatetime'
460 * seconds flush all the swap files to disk.
461 * Also done when interrupted by SIGWINCH.
462 */
463 before_blocking();
464 continue;
465 }
466 }
467 }
468
469#ifdef FEAT_JOB_CHANNEL
470 /* Checking if a job ended requires polling. Do this every 100 msec. */
471 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
472 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100473 /* If there is readahead then parse_queued_messages() timed out and we
474 * should call it again soon. */
475 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
476 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200477#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100478#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100479 if (p_beval && wait_time > 100L)
480 /* The 'balloonexpr' may indirectly invoke a callback while waiting
481 * for a character, need to check often. */
482 wait_time = 100L;
483#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200484
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200486 * We want to be interrupted by the winch signal
487 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200489 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200490 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200491 /* If input was put directly in typeahead buffer bail out here. */
492 if (typebuf_changed(tb_change_cnt))
493 return 0;
494
495 /*
496 * For some terminals we only get one character at a time.
497 * We want the get all available characters, so we could keep on
498 * trying until none is available
499 * For some other terminals this is quite slow, that's why we don't
500 * do it.
501 */
502 len = read_from_input_buf(buf, (long)maxlen);
503 if (len > 0)
504 return len;
505 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200508 /* no character available */
509#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
510 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100511 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200514 if (do_resize /* interrupted by SIGWINCH signal */
515#ifdef FEAT_CLIENTSERVER
516 || server_waiting()
517#endif
518#ifdef MESSAGE_QUEUE
519 || interrupted
520#endif
521 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100522 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200523 continue;
524
525 /* no character available or interrupted */
526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200528 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529}
530
531 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100532handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 do_resize = FALSE;
535 shell_resized();
536}
537
538/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200539 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
541 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100542mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200544 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545}
546
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200547#if defined(FEAT_TERMINAL) || defined(PROTO)
548/*
549 * Check for any pending input or messages.
550 */
551 int
552mch_check_messages(void)
553{
554 return WaitForChar(0L, NULL, TRUE);
555}
556#endif
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
559# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000560# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561# endif
562# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
563# include <sys/sysctl.h>
564# endif
565# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
566# include <sys/sysinfo.h>
567# endif
Bram Moolenaar988615f2018-02-27 17:58:20 +0100568# ifdef MACOS_X_DARWIN
569# include <mach/mach_host.h>
570# include <mach/mach_port.h>
571# include <mach/vm_page_size.h>
572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000575 * Return total amount of memory available in Kbyte.
576 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100579mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000582 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
Bram Moolenaar988615f2018-02-27 17:58:20 +0100584# ifdef MACOS_X_DARWIN
585 {
586 /* Mac (Darwin) way of getting the amount of RAM available */
587 mach_port_t host = mach_host_self();
588 kern_return_t kret;
589# ifdef HOST_VM_INFO64
590 struct vm_statistics64 vm_stat;
591 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar988615f2018-02-27 17:58:20 +0100593 kret = host_statistics64(host, HOST_VM_INFO64,
594 (host_info64_t)&vm_stat, &count);
595# else
596 struct vm_statistics vm_stat;
597 natural_t count = HOST_VM_INFO_COUNT;
598
599 kret = host_statistics(host, HOST_VM_INFO,
600 (host_info_t)&vm_stat, &count);
601# endif
602 if (kret == KERN_SUCCESS)
603 /* get the amount of user memory by summing each usage */
604 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
605 + vm_stat.inactive_count
606# ifdef MAC_OS_X_VERSION_10_9
607 + vm_stat.compressor_page_count
608# endif
609 ) * getpagesize();
610 mach_port_deallocate(mach_task_self(), host);
611 }
612# endif
613
614# ifdef HAVE_SYSCTL
615 if (mem == 0)
616 {
617 /* BSD way of getting the amount of RAM available. */
618 int mib[2];
619 size_t len = sizeof(long_u);
620# ifdef HW_USERMEM64
621 long_u physmem;
622# else
623 /* sysctl() may return 32 bit or 64 bit, accept both */
624 union {
625 int_u u32;
626 long_u u64;
627 } physmem;
628# endif
629
630 mib[0] = CTL_HW;
631# ifdef HW_USERMEM64
632 mib[1] = HW_USERMEM64;
633# else
634 mib[1] = HW_USERMEM;
635# endif
636 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
637 {
638# ifdef HW_USERMEM64
639 mem = (long_u)physmem;
640# else
641 if (len == sizeof(physmem.u64))
642 mem = (long_u)physmem.u64;
643 else
644 mem = (long_u)physmem.u32;
645# endif
646 }
647 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200650# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (mem == 0)
652 {
653 struct sysinfo sinfo;
654
655 /* Linux way of getting amount of RAM available */
656 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000657 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200658# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000659 /* avoid overflow as much as possible */
660 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
661 {
662 sinfo.mem_unit = sinfo.mem_unit >> 1;
663 --shiftright;
664 }
665 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200668# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200673# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 if (mem == 0)
675 {
676 long pagesize, pagecount;
677
678 /* Solaris way of getting amount of RAM available */
679 pagesize = sysconf(_SC_PAGESIZE);
680 pagecount = sysconf(_SC_PHYS_PAGES);
681 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000682 {
683 /* avoid overflow as much as possible */
684 while (shiftright > 0 && (pagesize & 1) == 0)
685 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000686 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000687 --shiftright;
688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 /* Return the minimum of the physical memory and the user limit, because
695 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200696# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
698 struct rlimit rlp;
699
700 if (getrlimit(RLIMIT_DATA, &rlp) == 0
701 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200702# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200704# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000705 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000707 {
708 mem = (long_u)rlp.rlim_cur;
709 shiftright = 10;
710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
714 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000715 return mem >> shiftright;
716 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718#endif
719
720 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100721mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000724#ifdef FEAT_MZSCHEME
725 long total = msec; /* remember original value */
726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728 if (ignoreinput)
729 {
730 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000731 * here. But we don't want QUIT to kill us (CTRL-\ used in a
732 * shell may produce SIGQUIT). */
733 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 old_tmode = curr_tmode;
735 if (curr_tmode == TMODE_RAW)
736 settmode(TMODE_SLEEP);
737
738 /*
739 * Everybody sleeps in a different way...
740 * Prefer nanosleep(), some versions of usleep() can only sleep up to
741 * one second.
742 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000743#ifdef FEAT_MZSCHEME
744 do
745 {
746 /* if total is large enough, wait by portions in p_mzq */
747 if (total > p_mzq)
748 msec = p_mzq;
749 else
750 msec = total;
751 total -= msec;
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#ifdef HAVE_NANOSLEEP
754 {
755 struct timespec ts;
756
757 ts.tv_sec = msec / 1000;
758 ts.tv_nsec = (msec % 1000) * 1000000;
759 (void)nanosleep(&ts, NULL);
760 }
761#else
762# ifdef HAVE_USLEEP
763 while (msec >= 1000)
764 {
765 usleep((unsigned int)(999 * 1000));
766 msec -= 999;
767 }
768 usleep((unsigned int)(msec * 1000));
769# else
770# ifndef HAVE_SELECT
771 poll(NULL, 0, (int)msec);
772# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
774 struct timeval tv;
775
776 tv.tv_sec = msec / 1000;
777 tv.tv_usec = (msec % 1000) * 1000;
778 /*
779 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
780 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
781 */
782 select(0, NULL, NULL, NULL, &tv);
783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784# endif /* HAVE_SELECT */
785# endif /* HAVE_NANOSLEEP */
786#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000787#ifdef FEAT_MZSCHEME
788 }
789 while (total > 0);
790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000793 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200796 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000799#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
801# define HAVE_CHECK_STACK_GROWTH
802/*
803 * Support for checking for an almost-out-of-stack-space situation.
804 */
805
806/*
807 * Return a pointer to an item on the stack. Used to find out if the stack
808 * grows up or down.
809 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100810static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811static int stack_grows_downwards;
812
813/*
814 * Find out if the stack grows upwards or downwards.
815 * "p" points to a variable on the stack of the caller.
816 */
817 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100818check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 int i;
821
822 stack_grows_downwards = (p > (char *)&i);
823}
824#endif
825
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000826#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827static char *stack_limit = NULL;
828
829#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
830# include <pthread.h>
831# include <pthread_np.h>
832#endif
833
834/*
835 * Find out until how var the stack can grow without getting into trouble.
836 * Called when starting up and when switching to the signal stack in
837 * deathtrap().
838 */
839 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100840get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
842 struct rlimit rlp;
843 int i;
844 long lim;
845
846 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
847 * limit doesn't fit in a long (rlim_cur might be "long long"). */
848 if (getrlimit(RLIMIT_STACK, &rlp) == 0
849 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
850# ifdef RLIM_INFINITY
851 && rlp.rlim_cur != RLIM_INFINITY
852# endif
853 )
854 {
855 lim = (long)rlp.rlim_cur;
856#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
857 {
858 pthread_attr_t attr;
859 size_t size;
860
861 /* On FreeBSD the initial thread always has a fixed stack size, no
862 * matter what the limits are set to. Normally it's 1 Mbyte. */
863 pthread_attr_init(&attr);
864 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
865 {
866 pthread_attr_getstacksize(&attr, &size);
867 if (lim > (long)size)
868 lim = (long)size;
869 }
870 pthread_attr_destroy(&attr);
871 }
872#endif
873 if (stack_grows_downwards)
874 {
875 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
876 if (stack_limit >= (char *)&i)
877 /* overflow, set to 1/16 of current stack position */
878 stack_limit = (char *)((long)&i / 16L);
879 }
880 else
881 {
882 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
883 if (stack_limit <= (char *)&i)
884 stack_limit = NULL; /* overflow */
885 }
886 }
887}
888
889/*
890 * Return FAIL when running out of stack space.
891 * "p" must point to any variable local to the caller that's on the stack.
892 */
893 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100894mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 if (stack_limit != NULL)
897 {
898 if (stack_grows_downwards)
899 {
900 if (p < stack_limit)
901 return FAIL;
902 }
903 else if (p > stack_limit)
904 return FAIL;
905 }
906 return OK;
907}
908#endif
909
910#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
911/*
912 * Support for using the signal stack.
913 * This helps when we run out of stack space, which causes a SIGSEGV. The
914 * signal handler then must run on another stack, since the normal stack is
915 * completely full.
916 */
917
918#ifndef SIGSTKSZ
919# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
920#endif
921
922# ifdef HAVE_SIGALTSTACK
923static stack_t sigstk; /* for sigaltstack() */
924# else
925static struct sigstack sigstk; /* for sigstack() */
926# endif
927
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100928static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929static char *signal_stack;
930
931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100932init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 if (signal_stack != NULL)
935 {
936# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937# ifdef HAVE_SS_BASE
938 sigstk.ss_base = signal_stack;
939# else
940 sigstk.ss_sp = signal_stack;
941# endif
942 sigstk.ss_size = SIGSTKSZ;
943 sigstk.ss_flags = 0;
944 (void)sigaltstack(&sigstk, NULL);
945# else
946 sigstk.ss_sp = signal_stack;
947 if (stack_grows_downwards)
948 sigstk.ss_sp += SIGSTKSZ - 1;
949 sigstk.ss_onstack = 0;
950 (void)sigstack(&sigstk, NULL);
951# endif
952 }
953}
954#endif
955
956/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000957 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 * will barf when the second argument to signal() is ``wrong''.
959 * Let me try it with a few tricky defines from my own osdef.h (jw).
960 */
961#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 static RETSIGTYPE
963sig_winch SIGDEFARG(sigarg)
964{
965 /* this is not required on all systems, but it doesn't hurt anybody */
966 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
967 do_resize = TRUE;
968 SIGRETURN;
969}
970#endif
971
972#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 static RETSIGTYPE
974catch_sigint SIGDEFARG(sigarg)
975{
976 /* this is not required on all systems, but it doesn't hurt anybody */
977 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
978 got_int = TRUE;
979 SIGRETURN;
980}
981#endif
982
983#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 static RETSIGTYPE
985catch_sigpwr SIGDEFARG(sigarg)
986{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000987 /* this is not required on all systems, but it doesn't hurt anybody */
988 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 /*
990 * I'm not sure we get the SIGPWR signal when the system is really going
991 * down or when the batteries are almost empty. Just preserve the swap
992 * files and don't exit, that can't do any harm.
993 */
994 ml_sync_all(FALSE, FALSE);
995 SIGRETURN;
996}
997#endif
998
999#ifdef SET_SIG_ALARM
1000/*
1001 * signal function for alarm().
1002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 static RETSIGTYPE
1004sig_alarm SIGDEFARG(sigarg)
1005{
1006 /* doesn't do anything, just to break a system call */
1007 sig_alarm_called = TRUE;
1008 SIGRETURN;
1009}
1010#endif
1011
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001012#if (defined(HAVE_SETJMP_H) \
1013 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1014 || defined(FEAT_LIBCALL))) \
1015 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/*
1017 * A simplistic version of setjmp() that only allows one level of using.
1018 * Don't call twice before calling mch_endjmp()!.
1019 * Usage:
1020 * mch_startjmp();
1021 * if (SETJMP(lc_jump_env) != 0)
1022 * {
1023 * mch_didjmp();
1024 * EMSG("crash!");
1025 * }
1026 * else
1027 * {
1028 * do_the_work;
1029 * mch_endjmp();
1030 * }
1031 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1032 * not return before the saved environment is used.
1033 * Returns OK for normal return, FAIL when the protected code caused a
1034 * problem and LONGJMP() was used.
1035 */
1036 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001037mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039#ifdef SIGHASARG
1040 lc_signal = 0;
1041#endif
1042 lc_active = TRUE;
1043}
1044
1045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001046mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 lc_active = FALSE;
1049}
1050
1051 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001052mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1055 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1056 * otherwise catching the signal only works once. */
1057 init_signal_stack();
1058# endif
1059}
1060#endif
1061
1062/*
1063 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001064 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001066 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1067 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 static RETSIGTYPE
1070deathtrap SIGDEFARG(sigarg)
1071{
1072 static int entered = 0; /* count the number of times we got here.
1073 Note: when memory has been corrupted
1074 this may get an arbitrary value! */
1075#ifdef SIGHASARG
1076 int i;
1077#endif
1078
1079#if defined(HAVE_SETJMP_H)
1080 /*
1081 * Catch a crash in protected code.
1082 * Restores the environment saved in lc_jump_env, which looks like
1083 * SETJMP() returns 1.
1084 */
1085 if (lc_active)
1086 {
1087# if defined(SIGHASARG)
1088 lc_signal = sigarg;
1089# endif
1090 lc_active = FALSE; /* don't jump again */
1091 LONGJMP(lc_jump_env, 1);
1092 /* NOTREACHED */
1093 }
1094#endif
1095
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001096#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001097# ifdef SIGQUIT
1098 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1099 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1100 * pressing CTRL-\, but we don't want Vim to exit then. */
1101 if (in_mch_delay && sigarg == SIGQUIT)
1102 SIGRETURN;
1103# endif
1104
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001105 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1106 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1107 * free(). Calling free() again may then cause a crash. */
1108 if (entered == 0
1109 && (0
1110# ifdef SIGHUP
1111 || sigarg == SIGHUP
1112# endif
1113# ifdef SIGQUIT
1114 || sigarg == SIGQUIT
1115# endif
1116# ifdef SIGTERM
1117 || sigarg == SIGTERM
1118# endif
1119# ifdef SIGPWR
1120 || sigarg == SIGPWR
1121# endif
1122# ifdef SIGUSR1
1123 || sigarg == SIGUSR1
1124# endif
1125# ifdef SIGUSR2
1126 || sigarg == SIGUSR2
1127# endif
1128 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001129 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001130 SIGRETURN;
1131#endif
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 /* Remember how often we have been called. */
1134 ++entered;
1135
Bram Moolenaare429e702016-06-10 19:49:14 +02001136#ifdef FEAT_AUTOCMD
1137 /* Executing autocommands is likely to use more stack space than we have
1138 * available in the signal stack. */
1139 block_autocmds();
1140#endif
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#ifdef FEAT_EVAL
1143 /* Set the v:dying variable. */
1144 set_vim_var_nr(VV_DYING, (long)entered);
1145#endif
1146
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001147#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /* Since we are now using the signal stack, need to reset the stack
1149 * limit. Otherwise using a regexp will fail. */
1150 get_stack_limit();
1151#endif
1152
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001153#if 0
1154 /* This is for opening gdb the moment Vim crashes.
1155 * You need to manually adjust the file name and Vim executable name.
1156 * Suggested by SungHyun Nam. */
1157 {
1158# define VI_GDB_FILE "/tmp/vimgdb"
1159# define VIM_NAME "/usr/bin/vim"
1160 FILE *fp = fopen(VI_GDB_FILE, "w");
1161 if (fp)
1162 {
1163 fprintf(fp,
1164 "file %s\n"
1165 "attach %d\n"
1166 "set height 1000\n"
1167 "bt full\n"
1168 , VIM_NAME, getpid());
1169 fclose(fp);
1170 system("xterm -e gdb -x "VI_GDB_FILE);
1171 unlink(VI_GDB_FILE);
1172 }
1173 }
1174#endif
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#ifdef SIGHASARG
1177 /* try to find the name of this signal */
1178 for (i = 0; signal_info[i].sig != -1; i++)
1179 if (sigarg == signal_info[i].sig)
1180 break;
1181 deadly_signal = sigarg;
1182#endif
1183
1184 full_screen = FALSE; /* don't write message to the GUI, it might be
1185 * part of the problem... */
1186 /*
1187 * If something goes wrong after entering here, we may get here again.
1188 * When this happens, give a message and try to exit nicely (resetting the
1189 * terminal mode, etc.)
1190 * When this happens twice, just exit, don't even try to give a message,
1191 * stack may be corrupt or something weird.
1192 * When this still happens again (or memory was corrupted in such a way
1193 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1194 */
1195 if (entered >= 3)
1196 {
1197 reset_signals(); /* don't catch any signals anymore */
1198 may_core_dump();
1199 if (entered >= 4)
1200 _exit(8);
1201 exit(7);
1202 }
1203 if (entered == 2)
1204 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001205 /* No translation, it may call malloc(). */
1206 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 out_flush();
1208 getout(1);
1209 }
1210
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001211 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001213 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 signal_info[i].name);
1215#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001216 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001218
1219 /* Preserve files and exit. This sets the really_exiting flag to prevent
1220 * calling free(). */
1221 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaare429e702016-06-10 19:49:14 +02001223 /* NOTREACHED */
1224
Bram Moolenaar009b2592004-10-24 19:18:58 +00001225#ifdef NBDEBUG
1226 reset_signals();
1227 may_core_dump();
1228 abort();
1229#endif
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 SIGRETURN;
1232}
1233
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001234#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
1236 * On Solaris with multi-threading, suspending might not work immediately.
1237 * Catch the SIGCONT signal, which will be used as an indication whether the
1238 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001239 *
1240 * On Linux, signal is not always handled immediately either.
1241 * See https://bugs.launchpad.net/bugs/291373
1242 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001243 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001245static volatile int sigcont_received;
Bram Moolenaard99df422016-01-29 23:20:40 +01001246static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248/*
1249 * signal handler for SIGCONT
1250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 static RETSIGTYPE
1252sigcont_handler SIGDEFARG(sigarg)
1253{
1254 sigcont_received = TRUE;
1255 SIGRETURN;
1256}
1257#endif
1258
Bram Moolenaar62b42182010-09-21 22:09:37 +02001259# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001260static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001261# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001262static void save_clipboard(void);
1263static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001264
1265static void *clip_star_save = NULL;
1266static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001267# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001268
1269/*
1270 * Called when Vim is going to sleep or execute a shell command.
1271 * We can't respond to requests for the X selections. Lose them, otherwise
1272 * other applications will hang. But first copy the text to cut buffer 0.
1273 */
1274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001275loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001276{
1277 if (clip_star.owned || clip_plus.owned)
1278 {
1279 x11_export_final_selection();
1280 if (clip_star.owned)
1281 clip_lose_selection(&clip_star);
1282 if (clip_plus.owned)
1283 clip_lose_selection(&clip_plus);
1284 if (x11_display != NULL)
1285 XFlush(x11_display);
1286 }
1287}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001288
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001289# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001290/*
1291 * Save clipboard text to restore later.
1292 */
1293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001294save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001295{
1296 if (clip_star.owned)
1297 clip_star_save = get_register('*', TRUE);
1298 if (clip_plus.owned)
1299 clip_plus_save = get_register('+', TRUE);
1300}
1301
1302/*
1303 * Restore clipboard text if no one own the X selection.
1304 */
1305 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001306restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001307{
1308 if (clip_star_save != NULL)
1309 {
1310 if (!clip_gen_owner_exists(&clip_star))
1311 put_register('*', clip_star_save);
1312 else
1313 free_register(clip_star_save);
1314 clip_star_save = NULL;
1315 }
1316 if (clip_plus_save != NULL)
1317 {
1318 if (!clip_gen_owner_exists(&clip_plus))
1319 put_register('+', clip_plus_save);
1320 else
1321 free_register(clip_plus_save);
1322 clip_plus_save = NULL;
1323 }
1324}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001325# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328/*
1329 * If the machine has job control, use it to suspend the program,
1330 * otherwise fake it by starting a new shell.
1331 */
1332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001333mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 /* BeOS does have SIGTSTP, but it doesn't work. */
1336#if defined(SIGTSTP) && !defined(__BEOS__)
1337 out_flush(); /* needed to make cursor visible on some systems */
1338 settmode(TMODE_COOK);
1339 out_flush(); /* needed to disable mouse on some systems */
1340
1341# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001342 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343# endif
1344
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001345# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 sigcont_received = FALSE;
1347# endif
1348 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001349# if defined(_REENTRANT) && defined(SIGCONT)
1350 /*
1351 * Wait for the SIGCONT signal to be handled. It generally happens
1352 * immediately, but somehow not all the time. Do not call pause()
1353 * because there would be race condition which would hang Vim if
1354 * signal happened in between the test of sigcont_received and the
1355 * call to pause(). If signal is not yet received, call sleep(0)
1356 * to just yield CPU. Signal should then be received. If somehow
1357 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1358 * further if signal is not received after 1+2+3+4 ms (not expected
1359 * to happen).
1360 */
1361 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001362 long wait_time;
1363 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001364 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001365 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367# endif
1368
1369# ifdef FEAT_TITLE
1370 /*
1371 * Set oldtitle to NULL, so the current title is obtained again.
1372 */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001373 VIM_CLEAR(oldtitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374# endif
1375 settmode(TMODE_RAW);
1376 need_check_timestamps = TRUE;
1377 did_check_timestamps = FALSE;
1378#else
1379 suspend_shell();
1380#endif
1381}
1382
1383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001384mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 Columns = 80;
1387 Rows = 24;
1388
1389 out_flush();
1390 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001391
Bram Moolenaar56718732006-03-15 22:53:57 +00001392#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001393 mac_conv_init();
1394#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001395#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1396 win_clip_init();
1397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398}
1399
1400 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001401set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403#if defined(SIGWINCH)
1404 /*
1405 * WINDOW CHANGE signal is handled with sig_winch().
1406 */
1407 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1408#endif
1409
1410 /*
1411 * We want the STOP signal to work, to make mch_suspend() work.
1412 * For "rvim" the STOP signal is ignored.
1413 */
1414#ifdef SIGTSTP
1415 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1416#endif
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001417#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 signal(SIGCONT, sigcont_handler);
1419#endif
1420
1421 /*
1422 * We want to ignore breaking of PIPEs.
1423 */
1424#ifdef SIGPIPE
1425 signal(SIGPIPE, SIG_IGN);
1426#endif
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001429 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430#endif
1431
1432 /*
1433 * Ignore alarm signals (Perl's alarm() generates it).
1434 */
1435#ifdef SIGALRM
1436 signal(SIGALRM, SIG_IGN);
1437#endif
1438
1439 /*
1440 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1441 * work will be lost.
1442 */
1443#ifdef SIGPWR
1444 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1445#endif
1446
1447 /*
1448 * Arrange for other signals to gracefully shutdown Vim.
1449 */
1450 catch_signals(deathtrap, SIG_ERR);
1451
1452#if defined(FEAT_GUI) && defined(SIGHUP)
1453 /*
1454 * When the GUI is running, ignore the hangup signal.
1455 */
1456 if (gui.in_use)
1457 signal(SIGHUP, SIG_IGN);
1458#endif
1459}
1460
Bram Moolenaardf177f62005-02-22 08:39:57 +00001461#if defined(SIGINT) || defined(PROTO)
1462/*
1463 * Catch CTRL-C (only works while in Cooked mode).
1464 */
1465 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001466catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001467{
1468 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1469}
1470#endif
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001473reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001476#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /* SIGCONT isn't in the list, because its default action is ignore */
1478 signal(SIGCONT, SIG_DFL);
1479#endif
1480}
1481
1482 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001483catch_signals(
1484 RETSIGTYPE (*func_deadly)(),
1485 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 int i;
1488
1489 for (i = 0; signal_info[i].sig != -1; i++)
1490 if (signal_info[i].deadly)
1491 {
1492#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1493 struct sigaction sa;
1494
1495 /* Setup to use the alternate stack for the signal function. */
1496 sa.sa_handler = func_deadly;
1497 sigemptyset(&sa.sa_mask);
1498# if defined(__linux__) && defined(_REENTRANT)
1499 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1500 * thread handling in combination with using the alternate stack:
1501 * pthread library functions try to use the stack pointer to
1502 * identify the current thread, causing a SEGV signal, which
1503 * recursively calls deathtrap() and hangs. */
1504 sa.sa_flags = 0;
1505# else
1506 sa.sa_flags = SA_ONSTACK;
1507# endif
1508 sigaction(signal_info[i].sig, &sa, NULL);
1509#else
1510# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1511 struct sigvec sv;
1512
1513 /* Setup to use the alternate stack for the signal function. */
1514 sv.sv_handler = func_deadly;
1515 sv.sv_mask = 0;
1516 sv.sv_flags = SV_ONSTACK;
1517 sigvec(signal_info[i].sig, &sv, NULL);
1518# else
1519 signal(signal_info[i].sig, func_deadly);
1520# endif
1521#endif
1522 }
1523 else if (func_other != SIG_ERR)
1524 signal(signal_info[i].sig, func_other);
1525}
1526
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001527#ifdef HAVE_SIGPROCMASK
1528 static void
1529block_signals(sigset_t *set)
1530{
1531 sigset_t newset;
1532 int i;
1533
1534 sigemptyset(&newset);
1535
1536 for (i = 0; signal_info[i].sig != -1; i++)
1537 sigaddset(&newset, signal_info[i].sig);
1538
1539# if defined(_REENTRANT) && defined(SIGCONT)
1540 /* SIGCONT isn't in the list, because its default action is ignore */
1541 sigaddset(&newset, SIGCONT);
1542# endif
1543
1544 sigprocmask(SIG_BLOCK, &newset, set);
1545}
1546
1547 static void
1548unblock_signals(sigset_t *set)
1549{
1550 sigprocmask(SIG_SETMASK, set, NULL);
1551}
1552#endif
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001555 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001556 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1557 * return TRUE
1558 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1559 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001560 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001561 * Returns TRUE when Vim should exit.
1562 */
1563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001564vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001565{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001566 static int got_signal = 0;
1567 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001568
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001569 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001570 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001571 case SIGNAL_BLOCK: blocked = TRUE;
1572 break;
1573
1574 case SIGNAL_UNBLOCK: blocked = FALSE;
1575 if (got_signal != 0)
1576 {
1577 kill(getpid(), got_signal);
1578 got_signal = 0;
1579 }
1580 break;
1581
1582 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001583 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001584 got_signal = sig;
1585#ifdef SIGPWR
1586 if (sig != SIGPWR)
1587#endif
1588 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001589 break;
1590 }
1591 return FALSE;
1592}
1593
1594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * Check_win checks whether we have an interactive stdout.
1596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001598mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (isatty(1))
1601 return OK;
1602 return FAIL;
1603}
1604
1605/*
1606 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1607 */
1608 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001609mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 if (isatty(read_cmd_fd))
1612 return TRUE;
1613 return FALSE;
1614}
1615
1616#ifdef FEAT_X11
1617
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001618# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621/*
1622 * Give a message about the elapsed time for opening the X window.
1623 */
1624 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001625xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001627 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628}
1629# endif
1630#endif
1631
1632#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1633/*
1634 * A few functions shared by X11 title and clipboard code.
1635 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001636static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1637static int x_error_check(Display *dpy, XErrorEvent *error_event);
1638static int x_connect_to_server(void);
1639static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641static int got_x_error = FALSE;
1642
1643/*
1644 * X Error handler, otherwise X just exits! (very rude) -- webb
1645 */
1646 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001647x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001649 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 STRCAT(IObuff, _("\nVim: Got X error\n"));
1651
1652 /* We cannot print a message and continue, because no X calls are allowed
1653 * here (causes my system to hang). Silently continuing might be an
1654 * alternative... */
1655 preserve_exit(); /* preserve files and exit */
1656
1657 return 0; /* NOTREACHED */
1658}
1659
1660/*
1661 * Another X Error handler, just used to check for errors.
1662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001664x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 got_x_error = TRUE;
1667 return 0;
1668}
1669
1670#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1671# if defined(HAVE_SETJMP_H)
1672/*
1673 * An X IO Error handler, used to catch error while opening the display.
1674 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001675static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001678x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 /* This function should not return, it causes exit(). Longjump instead. */
1681 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001682# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001683 return 0; /* avoid the compiler complains about missing return value */
1684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685}
1686# endif
1687
1688/*
1689 * An X IO Error handler, used to catch terminal errors.
1690 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001691static int x_IOerror_handler(Display *dpy);
1692static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001693static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001696x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001699 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 x11_window = 0;
1701 x11_display = NULL;
1702 xterm_Shell = (Widget)0;
1703
1704 /* This function should not return, it causes exit(). Longjump instead. */
1705 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001706# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001707 return 0; /* avoid the compiler complains about missing return value */
1708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001710
1711/*
1712 * If the X11 connection was lost try to restore it.
1713 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001714 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001715 */
1716 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001717may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001718{
1719 if (xterm_dpy_was_reset)
1720 {
1721 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001722
1723# ifndef LESSTIF_VERSION
1724 /* This has been reported to avoid Vim getting stuck. */
1725 if (app_context != (XtAppContext)NULL)
1726 {
1727 XtDestroyApplicationContext(app_context);
1728 app_context = (XtAppContext)NULL;
1729 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1730 }
1731# endif
1732
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001733 setup_term_clip();
1734 get_x11_title(FALSE);
1735 }
1736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
1738
1739/*
1740 * Return TRUE when connection to the X server is desired.
1741 */
1742 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001743x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#if defined(FEAT_CLIENTSERVER)
1746 if (x_force_connect)
1747 return TRUE;
1748#endif
1749 if (x_no_connect)
1750 return FALSE;
1751
1752 /* Check for a match with "exclude:" from 'clipboard'. */
1753 if (clip_exclude_prog != NULL)
1754 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001755 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 return FALSE;
1757 }
1758 return TRUE;
1759}
1760
1761/*
1762 * Test if "dpy" and x11_window are valid by getting the window title.
1763 * I don't actually want it yet, so there may be a simpler call to use, but
1764 * this will cause the error handler x_error_check() to be called if anything
1765 * is wrong, such as the window pointer being invalid (as can happen when the
1766 * user changes his DISPLAY, but not his WINDOWID) -- webb
1767 */
1768 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001769test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 int (*old_handler)();
1772 XTextProperty text_prop;
1773
1774 old_handler = XSetErrorHandler(x_error_check);
1775 got_x_error = FALSE;
1776 if (XGetWMName(dpy, x11_window, &text_prop))
1777 XFree((void *)text_prop.value);
1778 XSync(dpy, False);
1779 (void)XSetErrorHandler(old_handler);
1780
1781 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001782 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 return (got_x_error ? FAIL : OK);
1785}
1786#endif
1787
1788#ifdef FEAT_TITLE
1789
1790#ifdef FEAT_X11
1791
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001792static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794/*
1795 * try to get x11 window and display
1796 *
1797 * return FAIL for failure, OK otherwise
1798 */
1799 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001800get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 char *winid;
1803 static int result = -1;
1804#define XD_NONE 0 /* x11_display not set here */
1805#define XD_HERE 1 /* x11_display opened here */
1806#define XD_GUI 2 /* x11_display used from gui.dpy */
1807#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1808 static int x11_display_from = XD_NONE;
1809 static int did_set_error_handler = FALSE;
1810
1811 if (!did_set_error_handler)
1812 {
1813 /* X just exits if it finds an error otherwise! */
1814 (void)XSetErrorHandler(x_error_handler);
1815 did_set_error_handler = TRUE;
1816 }
1817
Bram Moolenaar9372a112005-12-06 19:59:18 +00001818#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (gui.in_use)
1820 {
1821 /*
1822 * If the X11 display was opened here before, for the window where Vim
1823 * was started, close that one now to avoid a memory leak.
1824 */
1825 if (x11_display_from == XD_HERE && x11_display != NULL)
1826 {
1827 XCloseDisplay(x11_display);
1828 x11_display_from = XD_NONE;
1829 }
1830 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1831 {
1832 x11_display_from = XD_GUI;
1833 return OK;
1834 }
1835 x11_display = NULL;
1836 return FAIL;
1837 }
1838 else if (x11_display_from == XD_GUI)
1839 {
1840 /* GUI must have stopped somehow, clear x11_display */
1841 x11_window = 0;
1842 x11_display = NULL;
1843 x11_display_from = XD_NONE;
1844 }
1845#endif
1846
1847 /* When started with the "-X" argument, don't try connecting. */
1848 if (!x_connect_to_server())
1849 return FAIL;
1850
1851 /*
1852 * If WINDOWID not set, should try another method to find out
1853 * what the current window number is. The only code I know for
1854 * this is very complicated.
1855 * We assume that zero is invalid for WINDOWID.
1856 */
1857 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1858 x11_window = (Window)atol(winid);
1859
1860#ifdef FEAT_XCLIPBOARD
1861 if (xterm_dpy != NULL && x11_window != 0)
1862 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001863 /* We may have checked it already, but Gnome terminal can move us to
1864 * another window, so we need to check every time. */
1865 if (x11_display_from != XD_XTERM)
1866 {
1867 /*
1868 * If the X11 display was opened here before, for the window where
1869 * Vim was started, close that one now to avoid a memory leak.
1870 */
1871 if (x11_display_from == XD_HERE && x11_display != NULL)
1872 XCloseDisplay(x11_display);
1873 x11_display = xterm_dpy;
1874 x11_display_from = XD_XTERM;
1875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (test_x11_window(x11_display) == FAIL)
1877 {
1878 /* probably bad $WINDOWID */
1879 x11_window = 0;
1880 x11_display = NULL;
1881 x11_display_from = XD_NONE;
1882 return FAIL;
1883 }
1884 return OK;
1885 }
1886#endif
1887
1888 if (x11_window == 0 || x11_display == NULL)
1889 result = -1;
1890
1891 if (result != -1) /* Have already been here and set this */
1892 return result; /* Don't do all these X calls again */
1893
1894 if (x11_window != 0 && x11_display == NULL)
1895 {
1896#ifdef SET_SIG_ALARM
1897 RETSIGTYPE (*sig_save)();
1898#endif
1899#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1900 struct timeval start_tv;
1901
1902 if (p_verbose > 0)
1903 gettimeofday(&start_tv, NULL);
1904#endif
1905
1906#ifdef SET_SIG_ALARM
1907 /*
1908 * Opening the Display may hang if the DISPLAY setting is wrong, or
1909 * the network connection is bad. Set an alarm timer to get out.
1910 */
1911 sig_alarm_called = FALSE;
1912 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1913 (RETSIGTYPE (*)())sig_alarm);
1914 alarm(2);
1915#endif
1916 x11_display = XOpenDisplay(NULL);
1917
1918#ifdef SET_SIG_ALARM
1919 alarm(0);
1920 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1921 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001922 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924 if (x11_display != NULL)
1925 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001926# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001928 {
1929 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001930 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001931 verbose_leave();
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933# endif
1934 if (test_x11_window(x11_display) == FAIL)
1935 {
1936 /* Maybe window id is bad */
1937 x11_window = 0;
1938 XCloseDisplay(x11_display);
1939 x11_display = NULL;
1940 }
1941 else
1942 x11_display_from = XD_HERE;
1943 }
1944 }
1945 if (x11_window == 0 || x11_display == NULL)
1946 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001947
1948# ifdef FEAT_EVAL
1949 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1950# endif
1951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 return (result = OK);
1953}
1954
1955/*
1956 * Determine original x11 Window Title
1957 */
1958 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001959get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001961 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962}
1963
1964/*
1965 * Determine original x11 Window icon
1966 */
1967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001968get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 int retval = FALSE;
1971
1972 retval = get_x11_thing(FALSE, test_only);
1973
1974 /* could not get old icon, use terminal name */
1975 if (oldicon == NULL && !test_only)
1976 {
1977 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001978 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001980 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983 return retval;
1984}
1985
1986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001987get_x11_thing(
1988 int get_title, /* get title string */
1989 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 XTextProperty text_prop;
1992 int retval = FALSE;
1993 Status status;
1994
1995 if (get_x11_windis() == OK)
1996 {
1997 /* Get window/icon name if any */
1998 if (get_title)
1999 status = XGetWMName(x11_display, x11_window, &text_prop);
2000 else
2001 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2002
2003 /*
2004 * If terminal is xterm, then x11_window may be a child window of the
2005 * outer xterm window that actually contains the window/icon name, so
2006 * keep traversing up the tree until a window with a title/icon is
2007 * found.
2008 */
2009 /* Previously this was only done for xterm and alikes. I don't see a
2010 * reason why it would fail for other terminal emulators.
2011 * if (term_is_xterm) */
2012 {
2013 Window root;
2014 Window parent;
2015 Window win = x11_window;
2016 Window *children;
2017 unsigned int num_children;
2018
2019 while (!status || text_prop.value == NULL)
2020 {
2021 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2022 &num_children))
2023 break;
2024 if (children)
2025 XFree((void *)children);
2026 if (parent == root || parent == 0)
2027 break;
2028
2029 win = parent;
2030 if (get_title)
2031 status = XGetWMName(x11_display, win, &text_prop);
2032 else
2033 status = XGetWMIconName(x11_display, win, &text_prop);
2034 }
2035 }
2036 if (status && text_prop.value != NULL)
2037 {
2038 retval = TRUE;
2039 if (!test_only)
2040 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002041#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2042 if (text_prop.encoding == XA_STRING
2043# ifdef FEAT_MBYTE
2044 && !has_mbyte
2045# endif
2046 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
2048#endif
2049 if (get_title)
2050 oldtitle = vim_strsave((char_u *)text_prop.value);
2051 else
2052 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002053#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 else
2056 {
2057 char **cl;
2058 Status transform_status;
2059 int n = 0;
2060
2061 transform_status = XmbTextPropertyToTextList(x11_display,
2062 &text_prop,
2063 &cl, &n);
2064 if (transform_status >= Success && n > 0 && cl[0])
2065 {
2066 if (get_title)
2067 oldtitle = vim_strsave((char_u *) cl[0]);
2068 else
2069 oldicon = vim_strsave((char_u *) cl[0]);
2070 XFreeStringList(cl);
2071 }
2072 else
2073 {
2074 if (get_title)
2075 oldtitle = vim_strsave((char_u *)text_prop.value);
2076 else
2077 oldicon = vim_strsave((char_u *)text_prop.value);
2078 }
2079 }
2080#endif
2081 }
2082 XFree((void *)text_prop.value);
2083 }
2084 }
2085 return retval;
2086}
2087
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002088/* Xutf8 functions are not avaialble on older systems. Note that on some
2089 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2090 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2091 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002093# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094# define USE_UTF8_STRING
2095# endif
2096#endif
2097
2098/*
2099 * Set x11 Window Title
2100 *
2101 * get_x11_windis() must be called before this and have returned OK
2102 */
2103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002104set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105{
2106 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2107 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2108 * supported everywhere and STRING doesn't work for multi-byte titles.
2109 */
2110#ifdef USE_UTF8_STRING
2111 if (enc_utf8)
2112 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2113 NULL, NULL, 0, NULL, NULL, NULL);
2114 else
2115#endif
2116 {
2117#if XtSpecificationRelease >= 4
2118# ifdef FEAT_XFONTSET
2119 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2120 NULL, NULL, 0, NULL, NULL, NULL);
2121# else
2122 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002123 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002126 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 XSetWMProperties(x11_display, x11_window, &text_prop,
2128 NULL, NULL, 0, NULL, NULL, NULL);
2129# endif
2130#else
2131 XStoreName(x11_display, x11_window, (char *)title);
2132#endif
2133 }
2134 XFlush(x11_display);
2135}
2136
2137/*
2138 * Set x11 Window icon
2139 *
2140 * get_x11_windis() must be called before this and have returned OK
2141 */
2142 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002143set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 /* See above for comments about using X*SetWMProperties(). */
2146#ifdef USE_UTF8_STRING
2147 if (enc_utf8)
2148 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2149 NULL, 0, NULL, NULL, NULL);
2150 else
2151#endif
2152 {
2153#if XtSpecificationRelease >= 4
2154# ifdef FEAT_XFONTSET
2155 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2156 NULL, 0, NULL, NULL, NULL);
2157# else
2158 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002159 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002161 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2163 NULL, 0, NULL, NULL, NULL);
2164# endif
2165#else
2166 XSetIconName(x11_display, x11_window, (char *)icon);
2167#endif
2168 }
2169 XFlush(x11_display);
2170}
2171
2172#else /* FEAT_X11 */
2173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002175get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 return FALSE;
2178}
2179
2180 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002181get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 if (!test_only)
2184 {
2185 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002186 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002188 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190 return FALSE;
2191}
2192
2193#endif /* FEAT_X11 */
2194
2195 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002196mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 return get_x11_title(TRUE);
2199}
2200
2201 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002202mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 return get_x11_icon(TRUE);
2205}
2206
2207/*
2208 * Set the window title and icon.
2209 */
2210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002211mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 int type = 0;
2214 static int recursive = 0;
2215
2216 if (T_NAME == NULL) /* no terminal name (yet) */
2217 return;
2218 if (title == NULL && icon == NULL) /* nothing to do */
2219 return;
2220
2221 /* When one of the X11 functions causes a deadly signal, we get here again
2222 * recursively. Avoid hanging then (something is probably locked). */
2223 if (recursive)
2224 return;
2225 ++recursive;
2226
2227 /*
2228 * if the window ID and the display is known, we may use X11 calls
2229 */
2230#ifdef FEAT_X11
2231 if (get_x11_windis() == OK)
2232 type = 1;
2233#else
2234# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2235 if (gui.in_use)
2236 type = 1;
2237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239
2240 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002241 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 * than x11 calls, because the x11 calls don't always work
2243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if ((type || *T_TS != NUL) && title != NULL)
2245 {
2246 if (oldtitle == NULL
2247#ifdef FEAT_GUI
2248 && !gui.in_use
2249#endif
2250 ) /* first call but not in GUI, save title */
2251 (void)get_x11_title(FALSE);
2252
2253 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2254 term_settitle(title);
2255#ifdef FEAT_X11
2256 else
2257# ifdef FEAT_GUI_GTK
2258 if (!gui.in_use) /* don't do this if GTK+ is running */
2259# endif
2260 set_x11_title(title); /* x11 */
2261#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002262#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2264 else
2265 gui_mch_settitle(title, icon);
2266#endif
2267 did_set_title = TRUE;
2268 }
2269
2270 if ((type || *T_CIS != NUL) && icon != NULL)
2271 {
2272 if (oldicon == NULL
2273#ifdef FEAT_GUI
2274 && !gui.in_use
2275#endif
2276 ) /* first call, save icon */
2277 get_x11_icon(FALSE);
2278
2279 if (*T_CIS != NUL)
2280 {
2281 out_str(T_CIS); /* set icon start */
2282 out_str_nf(icon);
2283 out_str(T_CIE); /* set icon end */
2284 out_flush();
2285 }
2286#ifdef FEAT_X11
2287 else
2288# ifdef FEAT_GUI_GTK
2289 if (!gui.in_use) /* don't do this if GTK+ is running */
2290# endif
2291 set_x11_icon(icon); /* x11 */
2292#endif
2293 did_set_icon = TRUE;
2294 }
2295 --recursive;
2296}
2297
2298/*
2299 * Restore the window/icon title.
2300 * "which" is one of:
2301 * 1 only restore title
2302 * 2 only restore icon
2303 * 3 restore title and icon
2304 */
2305 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002306mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 /* only restore the title or icon when it has been set */
2309 mch_settitle(((which & 1) && did_set_title) ?
2310 (oldtitle ? oldtitle : p_titleold) : NULL,
2311 ((which & 2) && did_set_icon) ? oldicon : NULL);
2312}
2313
2314#endif /* FEAT_TITLE */
2315
2316/*
2317 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002318 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
2320 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002321vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323 if (name == NULL)
2324 return FALSE;
2325 return (STRNICMP(name, "xterm", 5) == 0
2326 || STRNICMP(name, "nxterm", 6) == 0
2327 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002328 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002330 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 || STRCMP(name, "builtin_xterm") == 0);
2332}
2333
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002334#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2335/*
2336 * Return TRUE if "name" appears to be that of a terminal
2337 * known to support the xterm-style mouse protocol.
2338 * Relies on term_is_xterm having been set to its correct value.
2339 */
2340 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002341use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002342{
2343 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002344 && (term_is_xterm
2345 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002346 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002347 || STRICMP(name, "st") == 0
2348 || STRNICMP(name, "st-", 3) == 0
2349 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002350}
2351#endif
2352
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2354/*
2355 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2356 * Return 1 for "xterm".
2357 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002358 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002359 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002362use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002364 if (ttym_flags == TTYM_SGR)
2365 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002366 if (ttym_flags == TTYM_URXVT)
2367 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (ttym_flags == TTYM_XTERM2)
2369 return 2;
2370 if (ttym_flags == TTYM_XTERM)
2371 return 1;
2372 return 0;
2373}
2374#endif
2375
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 if (name == NULL)
2380 return FALSE;
2381 return (STRNICMP(name, "iris-ansi", 9) == 0
2382 || STRCMP(name, "builtin_iris-ansi") == 0);
2383}
2384
2385 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002386vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 if (name == NULL)
2389 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002390 /* catch VT100 - VT5xx */
2391 return ((STRNICMP(name, "vt", 2) == 0
2392 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 || STRCMP(name, "builtin_vt320") == 0);
2394}
2395
2396/*
2397 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2398 * This should include all windowed terminal emulators.
2399 */
2400 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002401vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 if (name == NULL)
2404 return FALSE;
2405 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2406 return TRUE;
2407 return ( STRNICMP(name, "hpterm", 6) == 0
2408 || STRNICMP(name, "sun-cmd", 7) == 0
2409 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002410 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 || STRNICMP(name, "dtterm", 6) == 0);
2412}
2413
2414/*
2415 * Insert user name in s[len].
2416 * Return OK if a name found.
2417 */
2418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002419mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002422 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 return OK;
2424#else
2425 return mch_get_uname(getuid(), s, len);
2426#endif
2427}
2428
2429/*
2430 * Insert user name for "uid" in s[len].
2431 * Return OK if a name found.
2432 */
2433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002434mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2437 struct passwd *pw;
2438
2439 if ((pw = getpwuid(uid)) != NULL
2440 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2441 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002442 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return OK;
2444 }
2445#endif
2446 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2447 return FAIL; /* a number is not a name */
2448}
2449
2450/*
2451 * Insert host name is s[len].
2452 */
2453
2454#ifdef HAVE_SYS_UTSNAME_H
2455 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002456mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 struct utsname vutsname;
2459
2460 if (uname(&vutsname) < 0)
2461 *s = NUL;
2462 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002463 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464}
2465#else /* HAVE_SYS_UTSNAME_H */
2466
2467# ifdef HAVE_SYS_SYSTEMINFO_H
2468# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2469# endif
2470
2471 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002472mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474# ifdef VAXC
2475 vaxc$gethostname((char *)s, len);
2476# else
2477 gethostname((char *)s, len);
2478# endif
2479 s[len - 1] = NUL; /* make sure it's terminated */
2480}
2481#endif /* HAVE_SYS_UTSNAME_H */
2482
2483/*
2484 * return process ID
2485 */
2486 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002487mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 return (long)getpid();
2490}
2491
2492#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002493static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002496strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 extern int sys_nerr;
2499 extern char *sys_errlist[];
2500 static char er[20];
2501
2502 if (err > 0 && err < sys_nerr)
2503 return (sys_errlist[err]);
2504 sprintf(er, "Error %d", err);
2505 return er;
2506}
2507#endif
2508
2509/*
2510 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2511 * Return OK for success, FAIL for failure.
2512 */
2513 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002514mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516#if defined(USE_GETCWD)
2517 if (getcwd((char *)buf, len) == NULL)
2518 {
2519 STRCPY(buf, strerror(errno));
2520 return FAIL;
2521 }
2522 return OK;
2523#else
2524 return (getwd((char *)buf) != NULL ? OK : FAIL);
2525#endif
2526}
2527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002529 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 *
2531 * return FAIL for failure, OK for success
2532 */
2533 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002534mch_FullName(
2535 char_u *fname,
2536 char_u *buf,
2537 int len,
2538 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002541#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 int fd = -1;
2543 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 char_u olddir[MAXPATHL];
2546 char_u *p;
2547 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002548#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002549 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2550 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002551#endif
2552
Bram Moolenaar38323e42007-03-06 19:22:53 +00002553#ifdef VMS
2554 fname = vms_fixfilename(fname);
2555#endif
2556
Bram Moolenaara2442432007-04-26 14:26:37 +00002557#ifdef __CYGWIN__
2558 /*
2559 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2560 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002561# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002562 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2563 * a forward slash. */
2564 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2565 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002566# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002567 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002568# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002569 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002570#endif
2571
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002572 /* Expand it if forced or not an absolute path.
2573 * Do not do it for "/file", the result is always "/". */
2574 if ((force || !mch_isFullName(fname))
2575 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /*
2578 * If the file name has a path, change to that directory for a moment,
2579 * and then do the getwd() (and get back to where we were).
2580 * This will get the correct path name with "../" things.
2581 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002582 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002584#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * Use fchdir() if possible, it's said to be faster and more
2587 * reliable. But on SunOS 4 it might not work. Check this by
2588 * doing a fchdir() right now.
2589 */
2590 if (!dont_fchdir)
2591 {
2592 fd = open(".", O_RDONLY | O_EXTRA, 0);
2593 if (fd >= 0 && fchdir(fd) < 0)
2594 {
2595 close(fd);
2596 fd = -1;
2597 dont_fchdir = TRUE; /* don't try again */
2598 }
2599 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602 /* Only change directory when we are sure we can return to where
2603 * we are now. After doing "su" chdir(".") might not work. */
2604 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002605#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 (mch_dirname(olddir, MAXPATHL) == FAIL
2609 || mch_chdir((char *)olddir) != 0))
2610 {
2611 p = NULL; /* can't get current dir: don't chdir */
2612 retval = FAIL;
2613 }
2614 else
2615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 /* The directory is copied into buf[], to be able to remove
2617 * the file name without changing it (could be a string in
2618 * read-only memory) */
2619 if (p - fname >= len)
2620 retval = FAIL;
2621 else
2622 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002623 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (mch_chdir((char *)buf))
2625 retval = FAIL;
2626 else
2627 fname = p + 1;
2628 *buf = NUL;
2629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 }
2632 if (mch_dirname(buf, len) == FAIL)
2633 {
2634 retval = FAIL;
2635 *buf = NUL;
2636 }
2637 if (p != NULL)
2638 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002639#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (fd >= 0)
2641 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002642 if (p_verbose >= 5)
2643 {
2644 verbose_enter();
2645 MSG("fchdir() to previous dir");
2646 verbose_leave();
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 l = fchdir(fd);
2649 close(fd);
2650 }
2651 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 l = mch_chdir((char *)olddir);
2654 if (l != 0)
2655 EMSG(_(e_prev_dir));
2656 }
2657
2658 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002659 if (l >= len - 1)
2660 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002661#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002662 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002664 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002669 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return FAIL;
2671
2672 /* Do not append ".", "/dir/." is equal to "/dir". */
2673 if (STRCMP(fname, ".") != 0)
2674 STRCAT(buf, fname);
2675
2676 return OK;
2677}
2678
2679/*
2680 * Return TRUE if "fname" does not depend on the current directory.
2681 */
2682 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002683mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002685#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return ( fname[0] == '/' || fname[0] == '.' ||
2687 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2688 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2689 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002690#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#endif
2693}
2694
Bram Moolenaar24552be2005-12-10 20:17:30 +00002695#if defined(USE_FNAME_CASE) || defined(PROTO)
2696/*
2697 * Set the case of the file name, if it already exists. This will cause the
2698 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002699 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002700 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002701 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002702fname_case(
2703 char_u *name,
2704 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002705{
2706 struct stat st;
2707 char_u *slash, *tail;
2708 DIR *dirp;
2709 struct dirent *dp;
2710
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002711 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002712 {
2713 /* Open the directory where the file is located. */
2714 slash = vim_strrchr(name, '/');
2715 if (slash == NULL)
2716 {
2717 dirp = opendir(".");
2718 tail = name;
2719 }
2720 else
2721 {
2722 *slash = NUL;
2723 dirp = opendir((char *)name);
2724 *slash = '/';
2725 tail = slash + 1;
2726 }
2727
2728 if (dirp != NULL)
2729 {
2730 while ((dp = readdir(dirp)) != NULL)
2731 {
2732 /* Only accept names that differ in case and are the same byte
2733 * length. TODO: accept different length name. */
2734 if (STRICMP(tail, dp->d_name) == 0
2735 && STRLEN(tail) == STRLEN(dp->d_name))
2736 {
2737 char_u newname[MAXPATHL + 1];
2738 struct stat st2;
2739
2740 /* Verify the inode is equal. */
2741 vim_strncpy(newname, name, MAXPATHL);
2742 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2743 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002744 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002745 && st.st_ino == st2.st_ino
2746 && st.st_dev == st2.st_dev)
2747 {
2748 STRCPY(tail, dp->d_name);
2749 break;
2750 }
2751 }
2752 }
2753
2754 closedir(dirp);
2755 }
2756 }
2757}
2758#endif
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760/*
2761 * Get file permissions for 'name'.
2762 * Returns -1 when it doesn't exist.
2763 */
2764 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002765mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 struct stat statb;
2768
2769 /* Keep the #ifdef outside of stat(), it may be a macro. */
2770#ifdef VMS
2771 if (stat((char *)vms_fixfilename(name), &statb))
2772#else
2773 if (stat((char *)name, &statb))
2774#endif
2775 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002776#ifdef __INTERIX
2777 /* The top bit makes the value negative, which means the file doesn't
2778 * exist. Remove the bit, we don't use it. */
2779 return statb.st_mode & ~S_ADDACE;
2780#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
2785/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002786 * Set file permission for "name" to "perm".
2787 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
2789 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002790mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 return (chmod((char *)
2793#ifdef VMS
2794 vms_fixfilename(name),
2795#else
2796 name,
2797#endif
2798 (mode_t)perm) == 0 ? OK : FAIL);
2799}
2800
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002801#if defined(HAVE_FCHMOD) || defined(PROTO)
2802/*
2803 * Set file permission for open file "fd" to "perm".
2804 * Return FAIL for failure, OK otherwise.
2805 */
2806 int
2807mch_fsetperm(int fd, long perm)
2808{
2809 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2810}
2811#endif
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#if defined(HAVE_ACL) || defined(PROTO)
2814# ifdef HAVE_SYS_ACL_H
2815# include <sys/acl.h>
2816# endif
2817# ifdef HAVE_SYS_ACCESS_H
2818# include <sys/access.h>
2819# endif
2820
2821# ifdef HAVE_SOLARIS_ACL
2822typedef struct vim_acl_solaris_T {
2823 int acl_cnt;
2824 aclent_t *acl_entry;
2825} vim_acl_solaris_T;
2826# endif
2827
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002828#if defined(HAVE_SELINUX) || defined(PROTO)
2829/*
2830 * Copy security info from "from_file" to "to_file".
2831 */
2832 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002834{
2835 if (from_file == NULL)
2836 return;
2837
2838 if (selinux_enabled == -1)
2839 selinux_enabled = is_selinux_enabled();
2840
2841 if (selinux_enabled > 0)
2842 {
2843 security_context_t from_context = NULL;
2844 security_context_t to_context = NULL;
2845
2846 if (getfilecon((char *)from_file, &from_context) < 0)
2847 {
2848 /* If the filesystem doesn't support extended attributes,
2849 the original had no special security context and the
2850 target cannot have one either. */
2851 if (errno == EOPNOTSUPP)
2852 return;
2853
2854 MSG_PUTS(_("\nCould not get security context for "));
2855 msg_outtrans(from_file);
2856 msg_putchar('\n');
2857 return;
2858 }
2859 if (getfilecon((char *)to_file, &to_context) < 0)
2860 {
2861 MSG_PUTS(_("\nCould not get security context for "));
2862 msg_outtrans(to_file);
2863 msg_putchar('\n');
2864 freecon (from_context);
2865 return ;
2866 }
2867 if (strcmp(from_context, to_context) != 0)
2868 {
2869 if (setfilecon((char *)to_file, from_context) < 0)
2870 {
2871 MSG_PUTS(_("\nCould not set security context for "));
2872 msg_outtrans(to_file);
2873 msg_putchar('\n');
2874 }
2875 }
2876 freecon(to_context);
2877 freecon(from_context);
2878 }
2879}
2880#endif /* HAVE_SELINUX */
2881
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002882#if defined(HAVE_SMACK) && !defined(PROTO)
2883/*
2884 * Copy security info from "from_file" to "to_file".
2885 */
2886 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002887mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002888{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002889 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002890 {
2891 XATTR_NAME_SMACK,
2892 XATTR_NAME_SMACKEXEC,
2893 XATTR_NAME_SMACKMMAP
2894 };
2895
2896 char buffer[SMACK_LABEL_LEN];
2897 const char *name;
2898 int index;
2899 int ret;
2900 ssize_t size;
2901
2902 if (from_file == NULL)
2903 return;
2904
2905 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2906 / sizeof(smack_copied_attributes)[0]) ; index++)
2907 {
2908 /* get the name of the attribute to copy */
2909 name = smack_copied_attributes[index];
2910
2911 /* get the value of the attribute in buffer */
2912 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2913 if (size >= 0)
2914 {
2915 /* copy the attribute value of buffer */
2916 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2917 if (ret < 0)
2918 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002919 vim_snprintf((char *)IObuff, IOSIZE,
2920 _("Could not set security context %s for %s"),
2921 name, to_file);
2922 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002923 msg_putchar('\n');
2924 }
2925 }
2926 else
2927 {
2928 /* what reason of not having the attribute value? */
2929 switch (errno)
2930 {
2931 case ENOTSUP:
2932 /* extended attributes aren't supported or enabled */
2933 /* should a message be echoed? not sure... */
2934 return; /* leave because it isn't usefull to continue */
2935
2936 case ERANGE:
2937 default:
2938 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002939 vim_snprintf((char *)IObuff, IOSIZE,
2940 _("Could not get security context %s for %s. Removing it!"),
2941 name, from_file);
2942 msg_puts(IObuff);
2943 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002944 /* FALLTHROUGH to remove the attribute */
2945
2946 case ENODATA:
2947 /* no attribute of this name */
2948 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002949 /* Silently ignore errors, apparently this happens when
2950 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002951 break;
2952 }
2953 }
2954 }
2955}
2956#endif /* HAVE_SMACK */
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958/*
2959 * Return a pointer to the ACL of file "fname" in allocated memory.
2960 * Return NULL if the ACL is not available for whatever reason.
2961 */
2962 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002963mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 vim_acl_T ret = NULL;
2966#ifdef HAVE_POSIX_ACL
2967 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2968#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002969#ifdef HAVE_SOLARIS_ZFS_ACL
2970 acl_t *aclent;
2971
2972 if (acl_get((char *)fname, 0, &aclent) < 0)
2973 return NULL;
2974 ret = (vim_acl_T)aclent;
2975#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#ifdef HAVE_SOLARIS_ACL
2977 vim_acl_solaris_T *aclent;
2978
2979 aclent = malloc(sizeof(vim_acl_solaris_T));
2980 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2981 {
2982 free(aclent);
2983 return NULL;
2984 }
2985 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2986 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2987 {
2988 free(aclent->acl_entry);
2989 free(aclent);
2990 return NULL;
2991 }
2992 ret = (vim_acl_T)aclent;
2993#else
2994#if defined(HAVE_AIX_ACL)
2995 int aclsize;
2996 struct acl *aclent;
2997
2998 aclsize = sizeof(struct acl);
2999 aclent = malloc(aclsize);
3000 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3001 {
3002 if (errno == ENOSPC)
3003 {
3004 aclsize = aclent->acl_len;
3005 aclent = realloc(aclent, aclsize);
3006 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3007 {
3008 free(aclent);
3009 return NULL;
3010 }
3011 }
3012 else
3013 {
3014 free(aclent);
3015 return NULL;
3016 }
3017 }
3018 ret = (vim_acl_T)aclent;
3019#endif /* HAVE_AIX_ACL */
3020#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003021#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022#endif /* HAVE_POSIX_ACL */
3023 return ret;
3024}
3025
3026/*
3027 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3028 */
3029 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003030mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 if (aclent == NULL)
3033 return;
3034#ifdef HAVE_POSIX_ACL
3035 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3036#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003037#ifdef HAVE_SOLARIS_ZFS_ACL
3038 acl_set((char *)fname, (acl_t *)aclent);
3039#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#ifdef HAVE_SOLARIS_ACL
3041 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3042 ((vim_acl_solaris_T *)aclent)->acl_entry);
3043#else
3044#ifdef HAVE_AIX_ACL
3045 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3046#endif /* HAVE_AIX_ACL */
3047#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003048#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#endif /* HAVE_POSIX_ACL */
3050}
3051
3052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003053mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 if (aclent == NULL)
3056 return;
3057#ifdef HAVE_POSIX_ACL
3058 acl_free((acl_t)aclent);
3059#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003060#ifdef HAVE_SOLARIS_ZFS_ACL
3061 acl_free((acl_t *)aclent);
3062#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#ifdef HAVE_SOLARIS_ACL
3064 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3065 free(aclent);
3066#else
3067#ifdef HAVE_AIX_ACL
3068 free(aclent);
3069#endif /* HAVE_AIX_ACL */
3070#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003071#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#endif /* HAVE_POSIX_ACL */
3073}
3074#endif
3075
3076/*
3077 * Set hidden flag for "name".
3078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003080mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 /* can't hide a file */
3083}
3084
3085/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003086 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * return FALSE if "name" is not a directory
3088 * return FALSE for error
3089 */
3090 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003091mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 struct stat statb;
3094
3095 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3096 return FALSE;
3097 if (stat((char *)name, &statb))
3098 return FALSE;
3099#ifdef _POSIX_SOURCE
3100 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3101#else
3102 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3103#endif
3104}
3105
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003106/*
3107 * return TRUE if "name" is a directory, NOT a symlink to a directory
3108 * return FALSE if "name" is not a directory
3109 * return FALSE for error
3110 */
3111 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003112mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003113{
3114 struct stat statb;
3115
3116 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3117 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003118 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003119 return FALSE;
3120#ifdef _POSIX_SOURCE
3121 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
3122#else
3123 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
3124#endif
3125}
3126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003127static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
3129/*
3130 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3131 */
3132 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003133executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 struct stat st;
3136
3137 if (stat((char *)name, &st))
3138 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003139#ifdef VMS
3140 /* Like on Unix system file can have executable rights but not necessarily
3141 * be an executable, but on Unix is not a default for an ordianry file to
3142 * have an executable flag - on VMS it is in most cases.
3143 * Therefore, this check does not have any sense - let keep us to the
3144 * conventions instead:
3145 * *.COM and *.EXE files are the executables - the rest are not. This is
3146 * not ideal but better then it was.
3147 */
3148 int vms_executable = 0;
3149 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3150 {
3151 if (strstr(vms_tolower((char*)name),".exe") != NULL
3152 || strstr(vms_tolower((char*)name),".com")!= NULL)
3153 vms_executable = 1;
3154 }
3155 return vms_executable;
3156#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159}
3160
3161/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003162 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003163 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 * Return -1 if unknown.
3165 */
3166 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003167mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 char_u *buf;
3170 char_u *p, *e;
3171 int retval;
3172
Bram Moolenaarb5971142015-03-21 17:32:19 +01003173 /* When "use_path" is false and if it's an absolute or relative path don't
3174 * need to use $PATH. */
3175 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3176 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003177 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003178 /* There must be a path separator, files in the current directory
3179 * can't be executed. */
3180 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003181 {
3182 if (path != NULL)
3183 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003184 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003185 *path = FullName_save(name, TRUE);
3186 else
3187 *path = vim_strsave(name);
3188 }
3189 return TRUE;
3190 }
3191 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 p = (char_u *)getenv("PATH");
3195 if (p == NULL || *p == NUL)
3196 return -1;
3197 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3198 if (buf == NULL)
3199 return -1;
3200
3201 /*
3202 * Walk through all entries in $PATH to check if "name" exists there and
3203 * is an executable file.
3204 */
3205 for (;;)
3206 {
3207 e = (char_u *)strchr((char *)p, ':');
3208 if (e == NULL)
3209 e = p + STRLEN(p);
3210 if (e - p <= 1) /* empty entry means current dir */
3211 STRCPY(buf, "./");
3212 else
3213 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003214 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 add_pathsep(buf);
3216 }
3217 STRCAT(buf, name);
3218 retval = executable_file(buf);
3219 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003220 {
3221 if (path != NULL)
3222 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003223 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003224 *path = FullName_save(buf, TRUE);
3225 else
3226 *path = vim_strsave(buf);
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 if (*e != ':')
3232 break;
3233 p = e + 1;
3234 }
3235
3236 vim_free(buf);
3237 return retval;
3238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240/*
3241 * Check what "name" is:
3242 * NODE_NORMAL: file or directory (or doesn't exist)
3243 * NODE_WRITABLE: writable device, socket, fifo, etc.
3244 * NODE_OTHER: non-writable things
3245 */
3246 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003247mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
3249 struct stat st;
3250
3251 if (stat((char *)name, &st))
3252 return NODE_NORMAL;
3253 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3254 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3256 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 /* Everything else is writable? */
3258 return NODE_WRITABLE;
3259}
3260
3261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003262mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264#ifdef HAVE_CHECK_STACK_GROWTH
3265 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 check_stack_growth((char *)&i);
3268
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003269# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 get_stack_limit();
3271# endif
3272
3273#endif
3274
3275 /*
3276 * Setup an alternative stack for signals. Helps to catch signals when
3277 * running out of stack space.
3278 * Use of sigaltstack() is preferred, it's more portable.
3279 * Ignore any errors.
3280 */
3281#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003282 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 init_signal_stack();
3284#endif
3285}
3286
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003287#if defined(EXITFREE) || defined(PROTO)
3288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003289mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003290{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003291# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3292 if (clip_star.owned)
3293 clip_lose_selection(&clip_star);
3294 if (clip_plus.owned)
3295 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003296# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003297# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003298 if (xterm_Shell != (Widget)0)
3299 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003300# ifndef LESSTIF_VERSION
3301 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003302 if (xterm_dpy != NULL)
3303 XtCloseDisplay(xterm_dpy);
3304 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003305 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003306 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003307# ifdef FEAT_X11
3308 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3309# endif
3310 }
3311# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003312# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003313# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003314 if (x11_display != NULL
3315# ifdef FEAT_XCLIPBOARD
3316 && x11_display != xterm_dpy
3317# endif
3318 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319 XCloseDisplay(x11_display);
3320# endif
3321# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003322 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323# endif
3324# ifdef FEAT_TITLE
3325 vim_free(oldtitle);
3326 vim_free(oldicon);
3327# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003328}
3329#endif
3330
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003331static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333/*
3334 * Output a newline when exiting.
3335 * Make sure the newline goes to the same stream as the text.
3336 */
3337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003338exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003340 if (silent_mode)
3341 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (newline_on_exit || msg_didout)
3343 {
3344 if (msg_use_printf())
3345 {
3346 if (info_message)
3347 mch_msg("\n");
3348 else
3349 mch_errmsg("\r\n");
3350 }
3351 else
3352 out_char('\n');
3353 }
3354 else
3355 {
3356 restore_cterm_colors(); /* get original colors back */
3357 msg_clr_eos_force(); /* clear the rest of the display */
3358 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3359 }
3360}
3361
3362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003363mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 exiting = TRUE;
3366
3367#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3368 x11_export_final_selection();
3369#endif
3370
3371#ifdef FEAT_GUI
3372 if (!gui.in_use)
3373#endif
3374 {
3375 settmode(TMODE_COOK);
3376#ifdef FEAT_TITLE
3377 mch_restore_title(3); /* restore xterm title and icon name */
3378#endif
3379 /*
3380 * When t_ti is not empty but it doesn't cause swapping terminal
3381 * pages, need to output a newline when msg_didout is set. But when
3382 * t_ti does swap pages it should not go to the shell page. Do this
3383 * before stoptermcap().
3384 */
3385 if (swapping_screen() && !newline_on_exit)
3386 exit_scroll();
3387
3388 /* Stop termcap: May need to check for T_CRV response, which
3389 * requires RAW mode. */
3390 stoptermcap();
3391
3392 /*
3393 * A newline is only required after a message in the alternate screen.
3394 * This is set to TRUE by wait_return().
3395 */
3396 if (!swapping_screen() || newline_on_exit)
3397 exit_scroll();
3398
3399 /* Cursor may have been switched off without calling starttermcap()
3400 * when doing "vim -u vimrc" and vimrc contains ":q". */
3401 if (full_screen)
3402 cursor_on();
3403 }
3404 out_flush();
3405 ml_close_all(TRUE); /* remove all memfiles */
3406 may_core_dump();
3407#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 gui_exit(r);
3410#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003411
Bram Moolenaar56718732006-03-15 22:53:57 +00003412#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003413 mac_conv_cleanup();
3414#endif
3415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#ifdef __QNX__
3417 /* A core dump won't be created if the signal handler
3418 * doesn't return, so we can't call exit() */
3419 if (deadly_signal != 0)
3420 return;
3421#endif
3422
Bram Moolenaar009b2592004-10-24 19:18:58 +00003423#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003424 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003425#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003426
3427#ifdef EXITFREE
3428 free_all_mem();
3429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 exit(r);
3432}
3433
3434 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003435may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 if (deadly_signal != 0)
3438 {
3439 signal(deadly_signal, SIG_DFL);
3440 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3441 }
3442}
3443
3444#ifndef VMS
3445
3446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003447mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 static int first = TRUE;
3450
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003451#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452# ifdef HAVE_TERMIOS_H
3453 static struct termios told;
3454 struct termios tnew;
3455# else
3456 static struct termio told;
3457 struct termio tnew;
3458# endif
3459
3460 if (first)
3461 {
3462 first = FALSE;
3463# if defined(HAVE_TERMIOS_H)
3464 tcgetattr(read_cmd_fd, &told);
3465# else
3466 ioctl(read_cmd_fd, TCGETA, &told);
3467# endif
3468 }
3469
3470 tnew = told;
3471 if (tmode == TMODE_RAW)
3472 {
3473 /*
3474 * ~ICRNL enables typing ^V^M
3475 */
3476 tnew.c_iflag &= ~ICRNL;
3477 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3478# if defined(IEXTEN) && !defined(__MINT__)
3479 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3480 /* but it breaks function keys on MINT */
3481# endif
3482 );
3483# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3484 tnew.c_oflag &= ~ONLCR;
3485# endif
3486 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3487 tnew.c_cc[VTIME] = 0; /* don't wait */
3488 }
3489 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003490 {
3491 /* Also reset ICANON here, otherwise on Solaris select() won't see
3492 * typeahead characters. */
3493 tnew.c_lflag &= ~(ICANON | ECHO);
3494 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3495 tnew.c_cc[VTIME] = 0; /* don't wait */
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498# if defined(HAVE_TERMIOS_H)
3499 {
3500 int n = 10;
3501
3502 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3503 * few times. */
3504 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3505 && errno == EINTR && n > 0)
3506 --n;
3507 }
3508# else
3509 ioctl(read_cmd_fd, TCSETA, &tnew);
3510# endif
3511
3512#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 /*
3514 * for "old" tty systems
3515 */
3516# ifndef TIOCSETN
3517# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3518# endif
3519 static struct sgttyb ttybold;
3520 struct sgttyb ttybnew;
3521
3522 if (first)
3523 {
3524 first = FALSE;
3525 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3526 }
3527
3528 ttybnew = ttybold;
3529 if (tmode == TMODE_RAW)
3530 {
3531 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3532 ttybnew.sg_flags |= RAW;
3533 }
3534 else if (tmode == TMODE_SLEEP)
3535 ttybnew.sg_flags &= ~(ECHO);
3536 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3537#endif
3538 curr_tmode = tmode;
3539}
3540
3541/*
3542 * Try to get the code for "t_kb" from the stty setting
3543 *
3544 * Even if termcap claims a backspace key, the user's setting *should*
3545 * prevail. stty knows more about reality than termcap does, and if
3546 * somebody's usual erase key is DEL (which, for most BSD users, it will
3547 * be), they're going to get really annoyed if their erase key starts
3548 * doing forward deletes for no reason. (Eric Fischer)
3549 */
3550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003551get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003553 ttyinfo_T info;
3554 char_u buf[2];
3555 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003557 if (get_tty_info(read_cmd_fd, &info) == OK)
3558 {
3559 intr_char = info.interrupt;
3560 buf[0] = info.backspace;
3561 buf[1] = NUL;
3562 add_termcode((char_u *)"kb", buf, FALSE);
3563
3564 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3565 p = find_termcode((char_u *)"kD");
3566 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3567 do_fixdel(NULL);
3568 }
3569}
3570
3571/*
3572 * Obtain the characters that Backspace and Enter produce on "fd".
3573 * Returns OK or FAIL.
3574 */
3575 int
3576get_tty_info(int fd, ttyinfo_T *info)
3577{
3578#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# ifdef HAVE_TERMIOS_H
3580 struct termios keys;
3581# else
3582 struct termio keys;
3583# endif
3584
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003585 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003589 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003591 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003593 info->backspace = keys.c_cc[VERASE];
3594 info->interrupt = keys.c_cc[VINTR];
3595 if (keys.c_iflag & ICRNL)
3596 info->enter = NL;
3597 else
3598 info->enter = CAR;
3599 if (keys.c_oflag & ONLCR)
3600 info->nl_does_cr = TRUE;
3601 else
3602 info->nl_does_cr = FALSE;
3603 return OK;
3604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605#else
3606 /* for "old" tty systems */
3607 struct sgttyb keys;
3608
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003609 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003611 info->backspace = keys.sg_erase;
3612 info->interrupt = keys.sg_kill;
3613 info->enter = CAR;
3614 info->nl_does_cr = TRUE;
3615 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003618 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619}
3620
3621#endif /* VMS */
3622
3623#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003624static int mouse_ison = FALSE;
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626/*
3627 * Set mouse clicks on or off.
3628 */
3629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003630mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003632# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003633 static int bevalterm_ison = FALSE;
3634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 int xterm_mouse_vers;
3636
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003637 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003638# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003639 && p_bevalterm == bevalterm_ison
3640# endif
3641 )
3642 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 return;
3644
3645 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003646
3647# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003648 if (ttym_flags == TTYM_URXVT)
3649 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003650 out_str_nf((char_u *)
3651 (on
3652 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3653 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003654 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003655 }
3656# endif
3657
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003658# ifdef FEAT_MOUSE_SGR
3659 if (ttym_flags == TTYM_SGR)
3660 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003661 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003662 out_str_nf((char_u *)
3663 (on
3664 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3665 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003666 mouse_ison = on;
3667 }
3668# endif
3669
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003670# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003671 if (bevalterm_ison != (p_bevalterm && on))
3672 {
3673 bevalterm_ison = (p_bevalterm && on);
3674 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3675 /* disable mouse movement events, enabling is below */
3676 out_str_nf((char_u *)
3677 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003678 }
3679# endif
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (xterm_mouse_vers > 0)
3682 {
3683 if (on) /* enable mouse events, use mouse tracking if available */
3684 out_str_nf((char_u *)
3685 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003687# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003688 bevalterm_ison
3689 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3690# endif
3691 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3693 else /* disable mouse events, could probably always send the same */
3694 out_str_nf((char_u *)
3695 (xterm_mouse_vers > 1
3696 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3697 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003698 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700
3701# ifdef FEAT_MOUSE_DEC
3702 else if (ttym_flags == TTYM_DEC)
3703 {
3704 if (on) /* enable mouse events */
3705 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3706 else /* disable mouse events */
3707 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003708 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710# endif
3711
3712# ifdef FEAT_MOUSE_GPM
3713 else
3714 {
3715 if (on)
3716 {
3717 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003718 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else
3721 {
3722 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003723 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 }
3726# endif
3727
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003728# ifdef FEAT_SYSMOUSE
3729 else
3730 {
3731 if (on)
3732 {
3733 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003734 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003735 }
3736 else
3737 {
3738 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003739 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003740 }
3741 }
3742# endif
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744# ifdef FEAT_MOUSE_JSB
3745 else
3746 {
3747 if (on)
3748 {
3749 /* D - Enable Mouse up/down messages
3750 * L - Enable Left Button Reporting
3751 * M - Enable Middle Button Reporting
3752 * R - Enable Right Button Reporting
3753 * K - Enable SHIFT and CTRL key Reporting
3754 * + - Enable Advanced messaging of mouse moves and up/down messages
3755 * Q - Quiet No Ack
3756 * # - Numeric value of mouse pointer required
3757 * 0 = Multiview 2000 cursor, used as standard
3758 * 1 = Windows Arrow
3759 * 2 = Windows I Beam
3760 * 3 = Windows Hour Glass
3761 * 4 = Windows Cross Hair
3762 * 5 = Windows UP Arrow
3763 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003764# ifdef JSBTERM_MOUSE_NONADVANCED
3765 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3767 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003768# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3770 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003771# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003772 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 else
3775 {
3776 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3777 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003778 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
3781# endif
3782# ifdef FEAT_MOUSE_PTERM
3783 else
3784 {
3785 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3786 if (on)
3787 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3788 else
3789 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792# endif
3793}
3794
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003795#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003796/*
3797 * Called when 'balloonevalterm' changed.
3798 */
3799 void
3800mch_bevalterm_changed(void)
3801{
3802 mch_setmouse(mouse_ison);
3803}
3804#endif
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806/*
3807 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3808 */
3809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003810check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812# ifdef FEAT_MOUSE_XTERM
3813 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003814# ifdef FEAT_MOUSE_URXVT
3815 && use_xterm_mouse() != 3
3816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817# ifdef FEAT_GUI
3818 && !gui.in_use
3819# endif
3820 )
3821 {
3822 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003823 ? IF_EB("\233M", CSI_STR "M")
3824 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (*p_mouse != NUL)
3826 {
3827 /* force mouse off and maybe on to send possibly new mouse
3828 * activation sequence to the xterm, with(out) drag tracing. */
3829 mch_setmouse(FALSE);
3830 setmouse();
3831 }
3832 }
3833 else
3834 del_mouse_termcode(KS_MOUSE);
3835# endif
3836
3837# ifdef FEAT_MOUSE_GPM
3838 if (!use_xterm_mouse()
3839# ifdef FEAT_GUI
3840 && !gui.in_use
3841# endif
3842 )
3843 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3844# endif
3845
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003846# ifdef FEAT_SYSMOUSE
3847 if (!use_xterm_mouse()
3848# ifdef FEAT_GUI
3849 && !gui.in_use
3850# endif
3851 )
3852 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3853# endif
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003856 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (!use_xterm_mouse()
3858# ifdef FEAT_GUI
3859 && !gui.in_use
3860# endif
3861 )
3862 set_mouse_termcode(KS_JSBTERM_MOUSE,
3863 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3864 else
3865 del_mouse_termcode(KS_JSBTERM_MOUSE);
3866# endif
3867
3868# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003869 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * define it in the GUI or when using an xterm. */
3871 if (!use_xterm_mouse()
3872# ifdef FEAT_GUI
3873 && !gui.in_use
3874# endif
3875 )
3876 set_mouse_termcode(KS_NETTERM_MOUSE,
3877 (char_u *)IF_EB("\033}", ESC_STR "}"));
3878 else
3879 del_mouse_termcode(KS_NETTERM_MOUSE);
3880# endif
3881
3882# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003883 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003884 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885# ifdef FEAT_GUI
3886 && !gui.in_use
3887# endif
3888 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003889 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3890 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else
3892 del_mouse_termcode(KS_DEC_MOUSE);
3893# endif
3894# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003895 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003896 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897# ifdef FEAT_GUI
3898 && !gui.in_use
3899# endif
3900 )
3901 set_mouse_termcode(KS_PTERM_MOUSE,
3902 (char_u *) IF_EB("\033[", ESC_STR "["));
3903 else
3904 del_mouse_termcode(KS_PTERM_MOUSE);
3905# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003906# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003907 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
3912 {
3913 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003914 ? IF_EB("\233*M", CSI_STR "*M")
3915 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003916
3917 if (*p_mouse != NUL)
3918 {
3919 mch_setmouse(FALSE);
3920 setmouse();
3921 }
3922 }
3923 else
3924 del_mouse_termcode(KS_URXVT_MOUSE);
3925# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003926# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003927 if (use_xterm_mouse() == 4
3928# ifdef FEAT_GUI
3929 && !gui.in_use
3930# endif
3931 )
3932 {
3933 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003934 ? IF_EB("\233<*M", CSI_STR "<*M")
3935 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3936
3937 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3938 ? IF_EB("\233<*m", CSI_STR "<*m")
3939 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003940
3941 if (*p_mouse != NUL)
3942 {
3943 mch_setmouse(FALSE);
3944 setmouse();
3945 }
3946 }
3947 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003948 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003950 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3951 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956/*
3957 * set screen mode, always fails.
3958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003960mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962 EMSG(_(e_screenmode));
3963 return FAIL;
3964}
3965
3966#ifndef VMS
3967
3968/*
3969 * Try to get the current window size:
3970 * 1. with an ioctl(), most accurate method
3971 * 2. from the environment variables LINES and COLUMNS
3972 * 3. from the termcap
3973 * 4. keep using the old values
3974 * Return OK when size could be determined, FAIL otherwise.
3975 */
3976 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003977mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 long rows = 0;
3980 long columns = 0;
3981 char_u *p;
3982
3983 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 * 1. try using an ioctl. It is the most accurate method.
3985 *
3986 * Try using TIOCGWINSZ first, some systems that have it also define
3987 * TIOCGSIZE but don't have a struct ttysize.
3988 */
3989# ifdef TIOCGWINSZ
3990 {
3991 struct winsize ws;
3992 int fd = 1;
3993
3994 /* When stdout is not a tty, use stdin for the ioctl(). */
3995 if (!isatty(fd) && isatty(read_cmd_fd))
3996 fd = read_cmd_fd;
3997 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3998 {
3999 columns = ws.ws_col;
4000 rows = ws.ws_row;
4001 }
4002 }
4003# else /* TIOCGWINSZ */
4004# ifdef TIOCGSIZE
4005 {
4006 struct ttysize ts;
4007 int fd = 1;
4008
4009 /* When stdout is not a tty, use stdin for the ioctl(). */
4010 if (!isatty(fd) && isatty(read_cmd_fd))
4011 fd = read_cmd_fd;
4012 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4013 {
4014 columns = ts.ts_cols;
4015 rows = ts.ts_lines;
4016 }
4017 }
4018# endif /* TIOCGSIZE */
4019# endif /* TIOCGWINSZ */
4020
4021 /*
4022 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004023 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4024 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004026 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
4028 if ((p = (char_u *)getenv("LINES")))
4029 rows = atoi((char *)p);
4030 if ((p = (char_u *)getenv("COLUMNS")))
4031 columns = atoi((char *)p);
4032 }
4033
4034#ifdef HAVE_TGETENT
4035 /*
4036 * 3. try reading "co" and "li" entries from termcap
4037 */
4038 if (columns == 0 || rows == 0)
4039 getlinecol(&columns, &rows);
4040#endif
4041
4042 /*
4043 * 4. If everything fails, use the old values
4044 */
4045 if (columns <= 0 || rows <= 0)
4046 return FAIL;
4047
4048 Rows = rows;
4049 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004050 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return OK;
4052}
4053
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004054#if defined(FEAT_TERMINAL) || defined(PROTO)
4055/*
4056 * Report the windows size "rows" and "cols" to tty "fd".
4057 */
4058 int
4059mch_report_winsize(int fd, int rows, int cols)
4060{
4061# ifdef TIOCSWINSZ
4062 struct winsize ws;
4063
4064 ws.ws_col = cols;
4065 ws.ws_row = rows;
4066 ws.ws_xpixel = cols * 5;
4067 ws.ws_ypixel = rows * 10;
4068 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4069 {
4070 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4071 return OK;
4072 }
4073 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4074# else
4075# ifdef TIOCSSIZE
4076 struct ttysize ts;
4077
4078 ts.ts_cols = cols;
4079 ts.ts_lines = rows;
4080 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4081 {
4082 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4083 return OK;
4084 }
4085 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4086# endif
4087# endif
4088 return FAIL;
4089}
4090#endif
4091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092/*
4093 * Try to set the window size to Rows and Columns.
4094 */
4095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004096mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
4098 if (*T_CWS)
4099 {
4100 /*
4101 * NOTE: if you get an error here that term_set_winsize() is
4102 * undefined, check the output of configure. It could probably not
4103 * find a ncurses, termcap or termlib library.
4104 */
4105 term_set_winsize((int)Rows, (int)Columns);
4106 out_flush();
4107 screen_start(); /* don't know where cursor is now */
4108 }
4109}
4110
4111#endif /* VMS */
4112
4113/*
4114 * Rows and/or Columns has changed.
4115 */
4116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004117mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 /* Nothing to do. */
4120}
4121
Bram Moolenaar205b8862011-09-07 15:04:31 +02004122/*
4123 * Wait for process "child" to end.
4124 * Return "child" if it exited properly, <= 0 on error.
4125 */
4126 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004127wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004128{
4129 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004130 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004131
4132 while (wait_pid != child)
4133 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004134 /* When compiled with Python threads are probably used, in which case
4135 * wait() sometimes hangs for no obvious reason. Use waitpid()
4136 * instead and loop (like the GUI). Also needed for other interfaces,
4137 * they might call system(). */
4138# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004139 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004140# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004141 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004142# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004143 if (wait_pid == 0)
4144 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004145 /* Wait for 1 to 10 msec before trying again. */
4146 mch_delay(delay_msec, TRUE);
4147 if (++delay_msec > 10)
4148 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004149 continue;
4150 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004151 if (wait_pid <= 0
4152# ifdef ECHILD
4153 && errno == ECHILD
4154# endif
4155 )
4156 break;
4157 }
4158 return wait_pid;
4159}
4160
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004161#if defined(FEAT_JOB_CHANNEL) || !defined(USE_SYSTEM) || defined(PROTO)
Bram Moolenaar72801402016-02-09 11:37:50 +01004162/*
4163 * Parse "cmd" and put the white-separated parts in "argv".
4164 * "argv" is an allocated array with "argc" entries.
4165 * Returns FAIL when out of memory.
4166 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004167 int
4168mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
4169{
4170 int i;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004171 char_u *p, *d;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004172 int inquote;
4173
4174 /*
4175 * Do this loop twice:
4176 * 1: find number of arguments
4177 * 2: separate them and build argv[]
4178 */
4179 for (i = 0; i < 2; ++i)
4180 {
Bram Moolenaar6231cb82016-04-26 19:42:42 +02004181 p = skipwhite(cmd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004182 inquote = FALSE;
4183 *argc = 0;
4184 for (;;)
4185 {
4186 if (i == 1)
4187 (*argv)[*argc] = (char *)p;
4188 ++*argc;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004189 d = p;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004190 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
4191 {
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004192 if (p[0] == '"')
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004193 /* quotes surrounding an argument and are dropped */
Bram Moolenaar835dc632016-02-07 14:27:38 +01004194 inquote = !inquote;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004195 else
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004196 {
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004197 if (p[0] == '\\' && p[1] != NUL)
4198 {
4199 /* First pass: skip over "\ " and "\"".
4200 * Second pass: Remove the backslash. */
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004201 ++p;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004202 }
4203 if (i == 1)
4204 *d++ = *p;
Bram Moolenaar9d654a82017-09-03 19:52:17 +02004205 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01004206 ++p;
4207 }
4208 if (*p == NUL)
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004209 {
4210 if (i == 1)
4211 *d++ = NUL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004212 break;
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004213 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01004214 if (i == 1)
Bram Moolenaard78f03f2017-10-06 01:07:41 +02004215 *d++ = NUL;
4216 p = skipwhite(p + 1);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004217 }
4218 if (*argv == NULL)
4219 {
4220 if (use_shcf)
4221 {
4222 /* Account for possible multiple args in p_shcf. */
4223 p = p_shcf;
4224 for (;;)
4225 {
4226 p = skiptowhite(p);
4227 if (*p == NUL)
4228 break;
4229 ++*argc;
4230 p = skipwhite(p);
4231 }
4232 }
4233
4234 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
4235 if (*argv == NULL) /* out of memory */
4236 return FAIL;
4237 }
4238 }
4239 return OK;
4240}
4241#endif
4242
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004243#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004244/*
4245 * Set the environment for a child process.
4246 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247 static void
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004248set_child_environment(long rows, long columns, char *term)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249{
4250# ifdef HAVE_SETENV
4251 char envbuf[50];
4252# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004253 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004254 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004255 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004256 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004257 static char envbuf_Colors[20];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004258# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004259 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004260# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004261# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004262 long colors =
4263# ifdef FEAT_GUI
4264 gui.in_use ? 256*256*256 :
4265# endif
4266 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004267
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004269 setenv("TERM", term, 1);
4270 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004271 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004272 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004274 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004275 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004276 sprintf((char *)envbuf, "%ld", colors);
4277 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004278# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004279 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004280# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004281# else
4282 /*
4283 * Putenv does not copy the string, it has to remain valid.
4284 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004285 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004286 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004287 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4288 putenv(envbuf_Term);
4289 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004290 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004291 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4292 putenv(envbuf_Lines);
4293 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4294 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004295 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004296 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4297 putenv(envbuf_Colors);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004298# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004299 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4300 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4301 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004302# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004303# endif
4304}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004305
4306 static void
4307set_default_child_environment(void)
4308{
4309 set_child_environment(Rows, Columns, "dumb");
4310}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004311#endif
4312
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004313#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004314/*
4315 * Open a PTY, with FD for the master and slave side.
4316 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4317 * When successful both file descriptors are stored.
4318 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004319 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004320open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004321{
4322 char *tty_name;
4323
4324 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4325 if (*pty_master_fd >= 0)
4326 {
4327 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4328 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4329 * adding O_NOCTTY always works when defined. */
4330#ifdef O_NOCTTY
4331 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4332#else
4333 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4334#endif
4335 if (*pty_slave_fd < 0)
4336 {
4337 close(*pty_master_fd);
4338 *pty_master_fd = -1;
4339 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004340 else if (namep != NULL)
4341 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004342 }
4343}
4344#endif
4345
Bram Moolenaarfae42832017-08-01 22:24:26 +02004346/*
4347 * Send SIGINT to a child process if "c" is an interrupt character.
4348 */
4349 void
4350may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4351{
4352# ifdef SIGINT
4353 if (c == Ctrl_C || c == intr_char)
4354 {
4355# ifdef HAVE_SETSID
4356 kill(-pid, SIGINT);
4357# else
4358 kill(0, SIGINT);
4359# endif
4360 if (wpid > 0)
4361 kill(wpid, SIGINT);
4362 }
4363# endif
4364}
4365
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004367mch_call_shell(
4368 char_u *cmd,
4369 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371#ifdef VMS
4372 char *ifn = NULL;
4373 char *ofn = NULL;
4374#endif
4375 int tmode = cur_tmode;
4376#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004377 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004378 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 out_flush();
4381
4382 if (options & SHELL_COOKED)
4383 settmode(TMODE_COOK); /* set to normal mode */
4384
Bram Moolenaar62b42182010-09-21 22:09:37 +02004385# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004386 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004387 loose_clipboard();
4388# endif
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (cmd == NULL)
4391 x = system((char *)p_sh);
4392 else
4393 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004394# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (ofn = strchr((char *)cmd, '>'))
4396 *ofn++ = '\0';
4397 if (ifn = strchr((char *)cmd, '<'))
4398 {
4399 char *p;
4400
4401 *ifn++ = '\0';
4402 p = strchr(ifn,' '); /* chop off any trailing spaces */
4403 if (p)
4404 *p = '\0';
4405 }
4406 if (ofn)
4407 x = vms_sys((char *)cmd, ofn, ifn);
4408 else
4409 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004410# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 newcmd = lalloc(STRLEN(p_sh)
4412 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4413 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4414 if (newcmd == NULL)
4415 x = 0;
4416 else
4417 {
4418 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4419 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4420 (char *)p_shcf,
4421 (char *)cmd);
4422 x = system((char *)newcmd);
4423 vim_free(newcmd);
4424 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427# ifdef VMS
4428 x = vms_sys_status(x);
4429# endif
4430 if (emsg_silent)
4431 ;
4432 else if (x == 127)
4433 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 else if (x && !(options & SHELL_SILENT))
4435 {
4436 MSG_PUTS(_("\nshell returned "));
4437 msg_outnum((long)x);
4438 msg_putchar('\n');
4439 }
4440
4441 if (tmode == TMODE_RAW)
4442 settmode(TMODE_RAW); /* set to raw mode */
4443# ifdef FEAT_TITLE
4444 resettitle();
4445# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004446# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4447 restore_clipboard();
4448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 return x;
4450
4451#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
4452
Bram Moolenaardf177f62005-02-22 08:39:57 +00004453# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4454 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004455# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaar835dc632016-02-07 14:27:38 +01004457 char_u *newcmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004459 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 pid_t wait_pid = 0;
4461# ifdef HAVE_UNION_WAIT
4462 union wait status;
4463# else
4464 int status = -1;
4465# endif
4466 int retval = -1;
4467 char **argv = NULL;
4468 int argc;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004469 char_u *p_shcf_copy = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 int i;
4471 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004473# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004475# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004476 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 int fd_fromshell[2];
4478 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004479 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar62b42182010-09-21 22:09:37 +02004481 newcmd = vim_strsave(p_sh);
4482 if (newcmd == NULL) /* out of memory */
4483 goto error;
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 out_flush();
4486 if (options & SHELL_COOKED)
4487 settmode(TMODE_COOK); /* set to normal mode */
4488
Bram Moolenaar835dc632016-02-07 14:27:38 +01004489 if (mch_parse_cmd(newcmd, TRUE, &argv, &argc) == FAIL)
4490 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004491
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (cmd != NULL)
4493 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004494 char_u *s;
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (extra_shell_arg != NULL)
4497 argv[argc++] = (char *)extra_shell_arg;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004498
4499 /* Break 'shellcmdflag' into white separated parts. This doesn't
4500 * handle quoted strings, they are very unlikely to appear. */
4501 p_shcf_copy = alloc((unsigned)STRLEN(p_shcf) + 1);
4502 if (p_shcf_copy == NULL) /* out of memory */
4503 goto error;
4504 s = p_shcf_copy;
4505 p = p_shcf;
4506 while (*p != NUL)
4507 {
4508 argv[argc++] = (char *)s;
4509 while (*p && *p != ' ' && *p != TAB)
4510 *s++ = *p++;
4511 *s++ = NUL;
4512 p = skipwhite(p);
4513 }
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 argv[argc++] = (char *)cmd;
4516 }
4517 argv[argc] = NULL;
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004520 * For the GUI, when writing the output into the buffer and when reading
4521 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4522 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004524 if ((options & (SHELL_READ|SHELL_WRITE))
4525# ifdef FEAT_GUI
4526 || (gui.in_use && show_shell_mess)
4527# endif
4528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004530# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * Try to open a master pty.
4533 * If this works, open the slave pty.
4534 * If the slave can't be opened, close the master pty.
4535 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004536 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004537 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 /*
4539 * If not opening a pty or it didn't work, try using pipes.
4540 */
4541 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
4544 pipe_error = (pipe(fd_toshell) < 0);
4545 if (!pipe_error) /* pipe create OK */
4546 {
4547 pipe_error = (pipe(fd_fromshell) < 0);
4548 if (pipe_error) /* pipe create failed */
4549 {
4550 close(fd_toshell[0]);
4551 close(fd_toshell[1]);
4552 }
4553 }
4554 if (pipe_error)
4555 {
4556 MSG_PUTS(_("\nCannot create pipes\n"));
4557 out_flush();
4558 }
4559 }
4560 }
4561
4562 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004564 SIGSET_DECL(curset)
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566# ifdef __BEOS__
4567 beos_cleanup_read_thread();
4568# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004569
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004570 BLOCK_SIGNALS(&curset);
4571 pid = fork(); /* maybe we should use vfork() */
4572 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004574 UNBLOCK_SIGNALS(&curset);
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579 || (gui.in_use && show_shell_mess)
4580# endif
4581 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004583# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (pty_master_fd >= 0) /* close the pseudo tty */
4585 {
4586 close(pty_master_fd);
4587 close(pty_slave_fd);
4588 }
4589 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 close(fd_toshell[0]);
4593 close(fd_toshell[1]);
4594 close(fd_fromshell[0]);
4595 close(fd_fromshell[1]);
4596 }
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599 else if (pid == 0) /* child */
4600 {
4601 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004602 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaar819524702018-02-27 19:10:00 +01004604# ifdef FEAT_JOB_CHANNEL
4605 if (ch_log_active())
4606 /* close the log file in the child */
4607 ch_logfile((char_u *)"", (char_u *)"");
4608# endif
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (!show_shell_mess || (options & SHELL_EXPAND))
4611 {
4612 int fd;
4613
4614 /*
4615 * Don't want to show any message from the shell. Can't just
4616 * close stdout and stderr though, because some systems will
4617 * break if you try to write to them after that, so we must
4618 * use dup() to replace them with something else -- webb
4619 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4620 * waiting for input.
4621 */
4622 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4623 fclose(stdin);
4624 fclose(stdout);
4625 fclose(stderr);
4626
4627 /*
4628 * If any of these open()'s and dup()'s fail, we just continue
4629 * anyway. It's not fatal, and on most systems it will make
4630 * no difference at all. On a few it will cause the execvp()
4631 * to exit with a non-zero status even when the completion
4632 * could be done, which is nothing too serious. If the open()
4633 * or dup() failed we'd just do the same thing ourselves
4634 * anyway -- webb
4635 */
4636 if (fd >= 0)
4637 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004638 ignored = dup(fd); /* To replace stdin (fd 0) */
4639 ignored = dup(fd); /* To replace stdout (fd 1) */
4640 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /* Don't need this now that we've duplicated it */
4643 close(fd);
4644 }
4645 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004646 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004648 || gui.in_use
4649# endif
4650 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652
Bram Moolenaardf177f62005-02-22 08:39:57 +00004653# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004654 /* Create our own process group, so that the child and all its
4655 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004656 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004657 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004658 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004659 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004660# if defined(SIGHUP)
4661 /* When doing "!xterm&" and 'shell' is bash: the shell
4662 * will exit and send SIGHUP to all processes in its
4663 * group, killing the just started process. Ignore SIGHUP
4664 * to avoid that. (suggested by Simon Schubert)
4665 */
4666 signal(SIGHUP, SIG_IGN);
4667# endif
4668 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004669# endif
4670# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004671 if (pty_slave_fd >= 0)
4672 {
4673 /* push stream discipline modules */
4674 if (options & SHELL_COOKED)
4675 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004677 /* Try to become controlling tty (probably doesn't work,
4678 * unless run by root) */
4679 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004681 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004682# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004683 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
Bram Moolenaara5792f52005-11-23 21:25:05 +00004685 /*
4686 * stderr is only redirected when using the GUI, so that a
4687 * program like gpg can still access the terminal to get a
4688 * passphrase using stderr.
4689 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004690# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (pty_master_fd >= 0)
4692 {
4693 close(pty_master_fd); /* close master side of pty */
4694
4695 /* set up stdin/stdout/stderr for the child */
4696 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004697 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004699 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004700 if (gui.in_use)
4701 {
4702 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004703 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 close(pty_slave_fd); /* has been dupped, close it now */
4707 }
4708 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 /* set up stdin for the child */
4712 close(fd_toshell[1]);
4713 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004714 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 close(fd_toshell[0]);
4716
4717 /* set up stdout for the child */
4718 close(fd_fromshell[0]);
4719 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004720 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 close(fd_fromshell[1]);
4722
Bram Moolenaara5792f52005-11-23 21:25:05 +00004723# ifdef FEAT_GUI
4724 if (gui.in_use)
4725 {
4726 /* set up stderr for the child */
4727 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004728 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004729 }
4730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004733
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 /*
4735 * There is no type cast for the argv, because the type may be
4736 * different on different machines. This may cause a warning
4737 * message with strict compilers, don't worry about it.
4738 * Call _exit() instead of exit() to avoid closing the connection
4739 * to the X server (esp. with GTK, which uses atexit()).
4740 */
4741 execvp(argv[0], argv);
4742 _exit(EXEC_FAILED); /* exec failed, return failure code */
4743 }
4744 else /* parent */
4745 {
4746 /*
4747 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004748 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 */
4750 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004751 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004752 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004753# ifdef FEAT_JOB_CHANNEL
4754 ++dont_check_job_ended;
4755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 /*
4757 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758 * This is also used to pipe stdin/stdout to/from the external
4759 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004761 if ((options & (SHELL_READ|SHELL_WRITE))
4762# ifdef FEAT_GUI
4763 || (gui.in_use && show_shell_mess)
4764# endif
4765 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004767# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004769# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4773 int ta_len = 0; /* valid bytes in ta_buf[] */
4774 int len;
4775 int p_more_save;
4776 int old_State;
4777 int c;
4778 int toshell_fd;
4779 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780 garray_T ga;
4781 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004782# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4783 struct timeval start_tv;
4784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaardf177f62005-02-22 08:39:57 +00004786# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (pty_master_fd >= 0)
4788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 fromshell_fd = pty_master_fd;
4790 toshell_fd = dup(pty_master_fd);
4791 }
4792 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795 close(fd_toshell[0]);
4796 close(fd_fromshell[1]);
4797 toshell_fd = fd_toshell[1];
4798 fromshell_fd = fd_fromshell[0];
4799 }
4800
4801 /*
4802 * Write to the child if there are typed characters.
4803 * Read from the child if there are characters available.
4804 * Repeat the reading a few times if more characters are
4805 * available. Need to check for typed keys now and then, but
4806 * not too often (delays when no chars are available).
4807 * This loop is quit if no characters can be read from the pty
4808 * (WaitForChar detected special condition), or there are no
4809 * characters available and the child has exited.
4810 * Only check if the child has exited when there is no more
4811 * output. The child may exit before all the output has
4812 * been printed.
4813 *
4814 * Currently this busy loops!
4815 * This can probably dead-lock when the write blocks!
4816 */
4817 p_more_save = p_more;
4818 p_more = FALSE;
4819 old_State = State;
4820 State = EXTERNCMD; /* don't redraw at window resize */
4821
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004822 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823 {
4824 /* Fork a process that will write the lines to the
4825 * external program. */
4826 if ((wpid = fork()) == -1)
4827 {
4828 MSG_PUTS(_("\nCannot fork\n"));
4829 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004830 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004831 {
4832 linenr_T lnum = curbuf->b_op_start.lnum;
4833 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004834 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004835 size_t l;
4836
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004837 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838 for (;;)
4839 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004840 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004841 if (l == 0)
4842 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004843 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004844 /* NL -> NUL translation */
4845 len = write(toshell_fd, "", (size_t)1);
4846 else
4847 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004848 char_u *s = vim_strchr(lp + written, NL);
4849
Bram Moolenaar89d40322006-08-29 15:30:07 +00004850 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004851 s == NULL ? l
4852 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004853 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004854 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004855 {
4856 /* Finished a line, add a NL, unless this line
4857 * should not have one. */
4858 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004859 || (!curbuf->b_p_bin
4860 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004861 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004862 && (lnum !=
4863 curbuf->b_ml.ml_line_count
4864 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004865 ignored = write(toshell_fd, "\n",
4866 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004867 ++lnum;
4868 if (lnum > curbuf->b_op_end.lnum)
4869 {
4870 /* finished all the lines, close pipe */
4871 close(toshell_fd);
4872 toshell_fd = -1;
4873 break;
4874 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004875 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004876 written = 0;
4877 }
4878 else if (len > 0)
4879 written += len;
4880 }
4881 _exit(0);
4882 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004883 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884 {
4885 close(toshell_fd);
4886 toshell_fd = -1;
4887 }
4888 }
4889
4890 if (options & SHELL_READ)
4891 ga_init2(&ga, 1, BUFLEN);
4892
4893 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004894# ifdef ELAPSED_FUNC
4895 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 for (;;)
4898 {
4899 /*
4900 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004901 * if there are any.
4902 * Don't do this if we are expanding wild cards (would eat
4903 * typeahead).
4904 * Don't do this when filtering and terminal is in cooked
4905 * mode, the shell command will handle the I/O. Avoids
4906 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004907 * Don't get characters when the child has already
4908 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004910 * while (noread_cnt > 4), avoids that ":r !ls" eats
4911 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 */
4913 len = 0;
4914 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004915 && ((options &
4916 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4917 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004918# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004919 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004920# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004921 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004922 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004923 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004925 if (ta_len == 0)
4926 {
4927 /* Get extra characters when we don't have any.
4928 * Reset the counter and timer. */
4929 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004930# ifdef ELAPSED_FUNC
4931 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004932# endif
4933 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4934 }
4935 if (ta_len > 0 || len > 0)
4936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 /*
4938 * For pipes:
4939 * Check for CTRL-C: send interrupt signal to child.
4940 * Check for CTRL-D: EOF, close pipe to child.
4941 */
4942 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 /*
4945 * Send SIGINT to the child's group or all
4946 * processes in our group.
4947 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004948 may_send_sigint(ta_buf[ta_len], pid, wpid);
4949
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (pty_master_fd < 0 && toshell_fd >= 0
4951 && ta_buf[ta_len] == Ctrl_D)
4952 {
4953 close(toshell_fd);
4954 toshell_fd = -1;
4955 }
4956 }
4957
4958 /* replace K_BS by <BS> and K_DEL by <DEL> */
4959 for (i = ta_len; i < ta_len + len; ++i)
4960 {
4961 if (ta_buf[i] == CSI && len - i > 2)
4962 {
4963 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4964 if (c == K_DEL || c == K_KDEL || c == K_BS)
4965 {
4966 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4967 (size_t)(len - i - 2));
4968 if (c == K_DEL || c == K_KDEL)
4969 ta_buf[i] = DEL;
4970 else
4971 ta_buf[i] = Ctrl_H;
4972 len -= 2;
4973 }
4974 }
4975 else if (ta_buf[i] == '\r')
4976 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004977# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004979 i += (*mb_ptr2len_len)(ta_buf + i,
4980 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983
4984 /*
4985 * For pipes: echo the typed characters.
4986 * For a pty this does not seem to work.
4987 */
4988 if (pty_master_fd < 0)
4989 {
4990 for (i = ta_len; i < ta_len + len; ++i)
4991 {
4992 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4993 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004994# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 else if (has_mbyte)
4996 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004997 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 msg_outtrans_len(ta_buf + i, l);
5000 i += l - 1;
5001 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
5004 msg_outtrans_len(ta_buf + i, 1);
5005 }
5006 windgoto(msg_row, msg_col);
5007 out_flush();
5008 }
5009
5010 ta_len += len;
5011
5012 /*
5013 * Write the characters to the child, unless EOF has
5014 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005015 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005016 * When writing buffer lines, drop the typed
5017 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005019 if (options & SHELL_WRITE)
5020 ta_len = 0;
5021 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
5023 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5024 if (len > 0)
5025 {
5026 ta_len -= len;
5027 mch_memmove(ta_buf, ta_buf + len, ta_len);
5028 }
5029 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032
Bram Moolenaardf177f62005-02-22 08:39:57 +00005033 if (got_int)
5034 {
5035 /* CTRL-C sends a signal to the child, we ignore it
5036 * ourselves */
5037# ifdef HAVE_SETSID
5038 kill(-pid, SIGINT);
5039# else
5040 kill(0, SIGINT);
5041# endif
5042 if (wpid > 0)
5043 kill(wpid, SIGINT);
5044 got_int = FALSE;
5045 }
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 /*
5048 * Check if the child has any characters to be printed.
5049 * Read them and write them to our window. Repeat this as
5050 * long as there is something to do, avoid the 10ms wait
5051 * for mch_inchar(), or sending typeahead characters to
5052 * the external process.
5053 * TODO: This should handle escape sequences, compatible
5054 * to some terminal (vt52?).
5055 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005056 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005057 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005059 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005060# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005062# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 );
5066 if (len <= 0) /* end of file or error */
5067 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005068
5069 noread_cnt = 0;
5070 if (options & SHELL_READ)
5071 {
5072 /* Do NUL -> NL translation, append NL separated
5073 * lines to the current buffer. */
5074 for (i = 0; i < len; ++i)
5075 {
5076 if (buffer[i] == NL)
5077 append_ga_line(&ga);
5078 else if (buffer[i] == NUL)
5079 ga_append(&ga, NL);
5080 else
5081 ga_append(&ga, buffer[i]);
5082 }
5083 }
5084# ifdef FEAT_MBYTE
5085 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 int l;
5088
Bram Moolenaardf177f62005-02-22 08:39:57 +00005089 len += buffer_off;
5090 buffer[len] = NUL;
5091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 /* Check if the last character in buffer[] is
5093 * incomplete, keep these bytes for the next
5094 * round. */
5095 for (p = buffer; p < buffer + len; p += l)
5096 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005097 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (l == 0)
5099 l = 1; /* NUL byte? */
5100 else if (MB_BYTE2LEN(*p) != l)
5101 break;
5102 }
5103 if (p == buffer) /* no complete character */
5104 {
5105 /* avoid getting stuck at an illegal byte */
5106 if (len >= 12)
5107 ++p;
5108 else
5109 {
5110 buffer_off = len;
5111 continue;
5112 }
5113 }
5114 c = *p;
5115 *p = NUL;
5116 msg_puts(buffer);
5117 if (p < buffer + len)
5118 {
5119 *p = c;
5120 buffer_off = (buffer + len) - p;
5121 mch_memmove(buffer, p, buffer_off);
5122 continue;
5123 }
5124 buffer_off = 0;
5125 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005126# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 buffer[len] = NUL;
5130 msg_puts(buffer);
5131 }
5132
5133 windgoto(msg_row, msg_col);
5134 cursor_on();
5135 out_flush();
5136 if (got_int)
5137 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005138
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005139# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005140 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005141 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005142 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005143
5144 /* Avoid that we keep looping here without
5145 * checking for a CTRL-C for a long time. Don't
5146 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005147 if (msec > 2000)
5148 {
5149 noread_cnt = 5;
5150 break;
5151 }
5152 }
5153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005156 /* If we already detected the child has finished, continue
5157 * reading output for a short while. Some text may be
5158 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005159 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005160 {
5161 if (noread_cnt < 5)
5162 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005163 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005164 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /*
5167 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005168 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005170# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005171 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005172# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5176 || (wait_pid == pid && WIFEXITED(status)))
5177 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005178 /* Don't break the loop yet, try reading more
5179 * characters from "fromshell_fd" first. When using
5180 * pipes there might still be something to read and
5181 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005184 else
5185 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005186
Bram Moolenaar95a51352013-03-21 22:53:50 +01005187# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005188 /* Handle any X events, e.g. serving the clipboard. */
5189 clip_update();
5190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192finished:
5193 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005194 if (options & SHELL_READ)
5195 {
5196 if (ga.ga_len > 0)
5197 {
5198 append_ga_line(&ga);
5199 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005200 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005201 }
5202 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005203 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005204 ga_clear(&ga);
5205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 /*
5208 * Give all typeahead that wasn't used back to ui_inchar().
5209 */
5210 if (ta_len)
5211 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 State = old_State;
5213 if (toshell_fd >= 0)
5214 close(toshell_fd);
5215 close(fromshell_fd);
5216 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005217# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005218 else
5219 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005220 long delay_msec = 1;
5221
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005222 /*
5223 * Similar to the loop above, but only handle X events, no
5224 * I/O.
5225 */
5226 for (;;)
5227 {
5228 if (got_int)
5229 {
5230 /* CTRL-C sends a signal to the child, we ignore it
5231 * ourselves */
5232# ifdef HAVE_SETSID
5233 kill(-pid, SIGINT);
5234# else
5235 kill(0, SIGINT);
5236# endif
5237 got_int = FALSE;
5238 }
5239# ifdef __NeXT__
5240 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5241# else
5242 wait_pid = waitpid(pid, &status, WNOHANG);
5243# endif
5244 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5245 || (wait_pid == pid && WIFEXITED(status)))
5246 {
5247 wait_pid = pid;
5248 break;
5249 }
5250
5251 /* Handle any X events, e.g. serving the clipboard. */
5252 clip_update();
5253
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005254 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5255 * less time. */
5256 mch_delay(delay_msec, TRUE);
5257 if (++delay_msec > 10)
5258 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005259 }
5260 }
5261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263 /*
5264 * Wait until our child has exited.
5265 * Ignore wait() returning pids of other children and returning
5266 * because of some signal like SIGWINCH.
5267 * Don't wait if wait_pid was already set above, indicating the
5268 * child already exited.
5269 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005270 if (wait_pid != pid)
5271 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar624891f2010-10-13 16:22:09 +02005273# ifdef FEAT_GUI
5274 /* Close slave side of pty. Only do this after the child has
5275 * exited, otherwise the child may hang when it tries to write on
5276 * the pty. */
5277 if (pty_master_fd >= 0)
5278 close(pty_slave_fd);
5279# endif
5280
Bram Moolenaardf177f62005-02-22 08:39:57 +00005281 /* Make sure the child that writes to the external program is
5282 * dead. */
5283 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005284 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005285 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005286 wait4pid(wpid, NULL);
5287 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005288
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005289# ifdef FEAT_JOB_CHANNEL
5290 --dont_check_job_ended;
5291# endif
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 /*
5294 * Set to raw mode right now, otherwise a CTRL-C after
5295 * catch_signals() will kill Vim.
5296 */
5297 if (tmode == TMODE_RAW)
5298 settmode(TMODE_RAW);
5299 did_settmode = TRUE;
5300 set_signals();
5301
5302 if (WIFEXITED(status))
5303 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005304 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005306 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 if (retval == EXEC_FAILED)
5309 {
5310 MSG_PUTS(_("\nCannot execute shell "));
5311 msg_outtrans(p_sh);
5312 msg_putchar('\n');
5313 }
5314 else if (!(options & SHELL_SILENT))
5315 {
5316 MSG_PUTS(_("\nshell returned "));
5317 msg_outnum((long)retval);
5318 msg_putchar('\n');
5319 }
5320 }
5321 }
5322 else
5323 MSG_PUTS(_("\nCommand terminated\n"));
5324 }
5325 }
5326 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005327 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329error:
5330 if (!did_settmode)
5331 if (tmode == TMODE_RAW)
5332 settmode(TMODE_RAW); /* set to raw mode */
5333# ifdef FEAT_TITLE
5334 resettitle();
5335# endif
5336 vim_free(newcmd);
5337
5338 return retval;
5339
5340#endif /* USE_SYSTEM */
5341}
5342
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005343#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005344 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005345mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005346{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005347 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005348 int fd_in[2] = {-1, -1}; /* for stdin */
5349 int fd_out[2] = {-1, -1}; /* for stdout */
5350 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005351 int pty_master_fd = -1;
5352 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005353 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005354 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5355 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5356 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005357 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005358 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5359 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005360 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005361 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005362 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005363
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005364 if (use_out_for_err && use_null_for_out)
5365 use_null_for_err = TRUE;
5366
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005367 /* default is to fail */
5368 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005369
Bram Moolenaarb2412082017-08-20 18:09:14 +02005370 if (options->jo_pty
5371 && (!(use_file_for_in || use_null_for_in)
5372 || !(use_file_for_in || use_null_for_out)
5373 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005374 {
5375 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5376 if (job->jv_tty_out != NULL)
5377 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5378 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005379
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005380 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005381 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005382 if (use_file_for_in)
5383 {
5384 char_u *fname = options->jo_io_name[PART_IN];
5385
5386 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5387 if (fd_in[0] < 0)
5388 {
5389 EMSG2(_(e_notopen), fname);
5390 goto failed;
5391 }
5392 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005393 else
5394 /* When writing buffer lines to the input don't use the pty, so that
5395 * the pipe can be closed when all lines were written. */
5396 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5397 && pipe(fd_in) < 0)
5398 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005399
5400 if (use_file_for_out)
5401 {
5402 char_u *fname = options->jo_io_name[PART_OUT];
5403
5404 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5405 if (fd_out[1] < 0)
5406 {
5407 EMSG2(_(e_notopen), fname);
5408 goto failed;
5409 }
5410 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005411 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005412 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005413
5414 if (use_file_for_err)
5415 {
5416 char_u *fname = options->jo_io_name[PART_ERR];
5417
5418 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5419 if (fd_err[1] < 0)
5420 {
5421 EMSG2(_(e_notopen), fname);
5422 goto failed;
5423 }
5424 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005425 else if (!use_out_for_err && !use_null_for_err
5426 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005427 goto failed;
5428
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005429 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5430 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005431 if (options->jo_set & JO_CHANNEL)
5432 {
5433 channel = options->jo_channel;
5434 if (channel != NULL)
5435 ++channel->ch_refcount;
5436 }
5437 else
5438 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005439 if (channel == NULL)
5440 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005441 if (job->jv_tty_out != NULL)
5442 ch_log(channel, "using pty %s on fd %d",
5443 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005444 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005445
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005446 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005447 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005448 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005449 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005450 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005451 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005452 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005453 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005454 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005455 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005456 int null_fd = -1;
5457 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005458
Bram Moolenaar835dc632016-02-07 14:27:38 +01005459 /* child */
5460 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005461 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005462
Bram Moolenaar819524702018-02-27 19:10:00 +01005463# ifdef FEAT_JOB_CHANNEL
5464 if (ch_log_active())
5465 /* close the log file in the child */
5466 ch_logfile((char_u *)"", (char_u *)"");
5467# endif
5468
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469# ifdef HAVE_SETSID
5470 /* Create our own process group, so that the child and all its
5471 * children can be kill()ed. Don't do this when using pipes,
5472 * because stdin is not a tty, we would lose /dev/tty. */
5473 (void)setsid();
5474# endif
5475
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005476# ifdef FEAT_TERMINAL
5477 if (options->jo_term_rows > 0)
5478 set_child_environment(
5479 (long)options->jo_term_rows,
5480 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005481 STRNCMP(T_NAME, "xterm", 5) == 0
5482 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005483 else
5484# endif
5485 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005486
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005487 if (options->jo_env != NULL)
5488 {
5489 dict_T *dict = options->jo_env;
5490 hashitem_T *hi;
5491 int todo = (int)dict->dv_hashtab.ht_used;
5492
5493 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5494 if (!HASHITEM_EMPTY(hi))
5495 {
5496 typval_T *item = &dict_lookup(hi)->di_tv;
5497
5498 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5499 --todo;
5500 }
5501 }
5502
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005503 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005504 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005505 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005506 if (null_fd < 0)
5507 {
5508 perror("opening /dev/null failed");
5509 _exit(OPEN_NULL_FAILED);
5510 }
5511 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005512
Bram Moolenaar223896d2017-08-02 22:33:28 +02005513 if (pty_slave_fd >= 0)
5514 {
5515 /* push stream discipline modules */
5516 SetupSlavePTY(pty_slave_fd);
5517# ifdef TIOCSCTTY
5518 /* Try to become controlling tty (probably doesn't work,
5519 * unless run by root) */
5520 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5521# endif
5522 }
5523
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005524 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005525 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005526 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005527 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005528 else if (fd_in[0] < 0)
5529 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005530 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005531 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005532
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005533 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005534 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005535 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005536 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005537 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005538 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005539 }
5540 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005541 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005542 else if (fd_err[1] < 0)
5543 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005544 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005545 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005546
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005547 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005548 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005549 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005550 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005551 else if (fd_out[1] < 0)
5552 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005553 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005554 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005555
5556 if (fd_in[0] >= 0)
5557 close(fd_in[0]);
5558 if (fd_in[1] >= 0)
5559 close(fd_in[1]);
5560 if (fd_out[0] >= 0)
5561 close(fd_out[0]);
5562 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005563 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005564 if (fd_err[0] >= 0)
5565 close(fd_err[0]);
5566 if (fd_err[1] >= 0)
5567 close(fd_err[1]);
5568 if (pty_master_fd >= 0)
5569 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005570 close(pty_master_fd); /* not used in the child */
5571 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005572 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005573
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005574 if (null_fd >= 0)
5575 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005576
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005577 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5578 _exit(EXEC_FAILED);
5579
Bram Moolenaar835dc632016-02-07 14:27:38 +01005580 /* See above for type of argv. */
5581 execvp(argv[0], argv);
5582
Bram Moolenaar4694a172016-04-21 14:05:23 +02005583 if (stderr_works)
5584 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005585# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005586 /* calling free_all_mem() here causes problems. Ignore valgrind
5587 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005588# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005589 _exit(EXEC_FAILED); /* exec failed, return failure code */
5590 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005591
5592 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005593 UNBLOCK_SIGNALS(&curset);
5594
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005595 job->jv_pid = pid;
5596 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005597 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005598
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005599 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005600 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005601 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005602 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005603 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005604 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005605 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005606 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005607 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005608 if (channel != NULL)
5609 {
5610 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005611 use_file_for_in || use_null_for_in
5612 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5613 use_file_for_out || use_null_for_out
5614 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5615 use_out_for_err || use_file_for_err || use_null_for_err
5616 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005617 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005618 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005619 else
5620 {
5621 if (fd_in[1] >= 0)
5622 close(fd_in[1]);
5623 if (fd_out[0] >= 0)
5624 close(fd_out[0]);
5625 if (fd_err[0] >= 0)
5626 close(fd_err[0]);
5627 if (pty_master_fd >= 0)
5628 close(pty_master_fd);
5629 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005630
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005631 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005632 return;
5633
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005634failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005635 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005636 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005637 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005638 if (fd_in[1] >= 0)
5639 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005640 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005641 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005642 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005643 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005644 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005645 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005646 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005647 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005648 if (pty_master_fd >= 0)
5649 close(pty_master_fd);
5650 if (pty_slave_fd >= 0)
5651 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005652}
5653
5654 char *
5655mch_job_status(job_T *job)
5656{
5657# ifdef HAVE_UNION_WAIT
5658 union wait status;
5659# else
5660 int status = -1;
5661# endif
5662 pid_t wait_pid = 0;
5663
5664# ifdef __NeXT__
5665 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5666# else
5667 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5668# endif
5669 if (wait_pid == -1)
5670 {
5671 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005672 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005673 }
5674 if (wait_pid == 0)
5675 return "run";
5676 if (WIFEXITED(status))
5677 {
5678 /* LINTED avoid "bitwise operation on signed value" */
5679 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005680 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005681 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005682 if (WIFSIGNALED(status))
5683 {
5684 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005685 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005686 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005688
5689return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005690 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005691 {
5692 ch_log(job->jv_channel, "Job ended");
5693 job->jv_status = JOB_ENDED;
5694 }
5695 return "dead";
5696}
5697
5698 job_T *
5699mch_detect_ended_job(job_T *job_list)
5700{
5701# ifdef HAVE_UNION_WAIT
5702 union wait status;
5703# else
5704 int status = -1;
5705# endif
5706 pid_t wait_pid = 0;
5707 job_T *job;
5708
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005709# ifndef USE_SYSTEM
5710 /* Do not do this when waiting for a shell command to finish, we would get
5711 * the exit value here (and discard it), the exit value obtained there
5712 * would then be wrong. */
5713 if (dont_check_job_ended > 0)
5714 return NULL;
5715# endif
5716
Bram Moolenaar97792de2016-10-15 18:36:49 +02005717# ifdef __NeXT__
5718 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5719# else
5720 wait_pid = waitpid(-1, &status, WNOHANG);
5721# endif
5722 if (wait_pid <= 0)
5723 /* no process ended */
5724 return NULL;
5725 for (job = job_list; job != NULL; job = job->jv_next)
5726 {
5727 if (job->jv_pid == wait_pid)
5728 {
5729 if (WIFEXITED(status))
5730 /* LINTED avoid "bitwise operation on signed value" */
5731 job->jv_exitval = WEXITSTATUS(status);
5732 else if (WIFSIGNALED(status))
5733 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005734 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005735 {
5736 ch_log(job->jv_channel, "Job ended");
5737 job->jv_status = JOB_ENDED;
5738 }
5739 return job;
5740 }
5741 }
5742 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005743}
5744
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005745/*
5746 * Send a (deadly) signal to "job".
5747 * Return FAIL if "how" is not a valid name.
5748 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005749 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005750mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005751{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005752 int sig = -1;
5753 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005754
Bram Moolenaar923d9262016-02-25 20:56:01 +01005755 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005756 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005757 else if (STRCMP(how, "hup") == 0)
5758 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005759 else if (STRCMP(how, "quit") == 0)
5760 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005761 else if (STRCMP(how, "int") == 0)
5762 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005763 else if (STRCMP(how, "kill") == 0)
5764 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005765#ifdef SIGWINCH
5766 else if (STRCMP(how, "winch") == 0)
5767 sig = SIGWINCH;
5768#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769 else if (isdigit(*how))
5770 sig = atoi((char *)how);
5771 else
5772 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005773
Bram Moolenaar72801402016-02-09 11:37:50 +01005774 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005775 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005776#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005777 if (job_pid == getpgid(job_pid))
5778 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005779#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005780
Bram Moolenaar61a66052017-07-22 18:39:00 +02005781 /* Never kill ourselves! */
5782 if (job_pid != 0)
5783 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005784
Bram Moolenaar835dc632016-02-07 14:27:38 +01005785 return OK;
5786}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005787
5788/*
5789 * Clear the data related to "job".
5790 */
5791 void
5792mch_clear_job(job_T *job)
5793{
5794 /* call waitpid because child process may become zombie */
5795# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005796 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005797# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005798 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005799# endif
5800}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005801#endif
5802
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005803#if defined(FEAT_TERMINAL) || defined(PROTO)
5804 int
5805mch_create_pty_channel(job_T *job, jobopt_T *options)
5806{
5807 int pty_master_fd = -1;
5808 int pty_slave_fd = -1;
5809 channel_T *channel;
5810
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005811 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5812 if (job->jv_tty_out != NULL)
5813 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005814 close(pty_slave_fd);
5815
5816 channel = add_channel();
5817 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005818 {
5819 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005820 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005821 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005822 if (job->jv_tty_out != NULL)
5823 ch_log(channel, "using pty %s on fd %d",
5824 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005825 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5826 channel->ch_keep_open = TRUE;
5827
5828 channel_set_pipes(channel, pty_master_fd, pty_master_fd, pty_master_fd);
5829 channel_set_job(channel, job, options);
5830 return OK;
5831}
5832#endif
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834/*
5835 * Check for CTRL-C typed by reading all available characters.
5836 * In cooked mode we should get SIGINT, no need to check.
5837 */
5838 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005839mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005841 if ((curr_tmode == TMODE_RAW || force)
5842 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 fill_input_buf(FALSE);
5844}
5845
5846/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005847 * Wait "msec" msec until a character is available from the mouse, keyboard,
5848 * from inbuf[].
5849 * "msec" == -1 will block forever.
5850 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005851 * When "ignore_input" is TRUE even check for pending input when input is
5852 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005853 * "interrupted" (if not NULL) is set to TRUE when no character is available
5854 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005855 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005856 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 */
5858 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005859WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005861#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005862 return ui_wait_for_chars_or_timer(
5863 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005864#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005865 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005866#endif
5867}
5868
5869/*
5870 * Wait "msec" msec until a character is available from the mouse or keyboard
5871 * or from inbuf[].
5872 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005873 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005874 * "interrupted" (if not NULL) is set to TRUE when no character is available
5875 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005876 * When a GUI is being used, this will never get called -- webb
5877 */
5878 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005879WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005880{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#ifdef FEAT_MOUSE_GPM
5882 int gpm_process_wanted;
5883#endif
5884#ifdef FEAT_XCLIPBOARD
5885 int rest;
5886#endif
5887 int avail;
5888
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005889 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 return 1;
5891
5892#if defined(FEAT_MOUSE_DEC)
5893 /* May need to query the mouse position. */
5894 if (WantQueryMouse)
5895 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005896 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5898 }
5899#endif
5900
5901 /*
5902 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5903 * events. This is a bit complicated, because they might both be defined.
5904 */
5905#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5906# ifdef FEAT_XCLIPBOARD
5907 rest = 0;
5908 if (do_xterm_trace())
5909 rest = msec;
5910# endif
5911 do
5912 {
5913# ifdef FEAT_XCLIPBOARD
5914 if (rest != 0)
5915 {
5916 msec = XT_TRACE_DELAY;
5917 if (rest >= 0 && rest < XT_TRACE_DELAY)
5918 msec = rest;
5919 if (rest >= 0)
5920 rest -= msec;
5921 }
5922# endif
5923# ifdef FEAT_MOUSE_GPM
5924 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005925 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005926 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005928 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929# endif
5930 if (!avail)
5931 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005932 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 return 1;
5934# ifdef FEAT_XCLIPBOARD
5935 if (rest == 0 || !do_xterm_trace())
5936# endif
5937 break;
5938 }
5939 }
5940 while (FALSE
5941# ifdef FEAT_MOUSE_GPM
5942 || (gpm_process_wanted && mch_gpm_process() == 0)
5943# endif
5944# ifdef FEAT_XCLIPBOARD
5945 || (!avail && rest != 0)
5946# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005947 )
5948 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005951 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#endif
5953 return avail;
5954}
5955
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005956#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957/*
5958 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005959 * "msec" == 0 will check for characters once.
5960 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005963 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005964 * "interrupted" (if not NULL) is set to TRUE when no character is available
5965 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967#if defined(__BEOS__)
5968 int
5969#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005970 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005972RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973{
5974 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005975 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005976#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 static int busy = FALSE;
5978
5979 /* May retry getting characters after an event was handled. */
5980# define MAY_LOOP
5981
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005982# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 /* Remember at what time we started, so that we know how much longer we
5984 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005985 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005986 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005988 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005989 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990# endif
5991
5992 /* Handle being called recursively. This may happen for the session
5993 * manager stuff, it may save the file, which does a breakcheck. */
5994 if (busy)
5995 return 0;
5996#endif
5997
5998#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005999 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000#endif
6001 {
6002#ifdef MAY_LOOP
6003 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006004# ifdef FEAT_MZSCHEME
6005 int mzquantum_used = FALSE;
6006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007#endif
6008#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006009 /* each channel may use in, out and err */
6010 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 int nfd;
6012# ifdef FEAT_XCLIPBOARD
6013 int xterm_idx = -1;
6014# endif
6015# ifdef FEAT_MOUSE_GPM
6016 int gpm_idx = -1;
6017# endif
6018# ifdef USE_XSMP
6019 int xsmp_idx = -1;
6020# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006021 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006023# ifdef FEAT_MZSCHEME
6024 mzvim_check_threads();
6025 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6026 {
6027 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6028 mzquantum_used = TRUE;
6029 }
6030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 fds[0].fd = fd;
6032 fds[0].events = POLLIN;
6033 nfd = 1;
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006036 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (xterm_Shell != (Widget)0)
6038 {
6039 xterm_idx = nfd;
6040 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6041 fds[nfd].events = POLLIN;
6042 nfd++;
6043 }
6044# endif
6045# ifdef FEAT_MOUSE_GPM
6046 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6047 {
6048 gpm_idx = nfd;
6049 fds[nfd].fd = gpm_fd;
6050 fds[nfd].events = POLLIN;
6051 nfd++;
6052 }
6053# endif
6054# ifdef USE_XSMP
6055 if (xsmp_icefd != -1)
6056 {
6057 xsmp_idx = nfd;
6058 fds[nfd].fd = xsmp_icefd;
6059 fds[nfd].events = POLLIN;
6060 nfd++;
6061 }
6062# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006063#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006064 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006065#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006066 if (interrupted != NULL)
6067 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006069 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006070
6071 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006072 if (result == 0 && interrupted != NULL && ret > 0)
6073 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006074
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006075# ifdef FEAT_MZSCHEME
6076 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006077 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006078 finished = FALSE;
6079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# ifdef FEAT_XCLIPBOARD
6082 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6083 {
6084 xterm_update(); /* Maybe we should hand out clipboard */
6085 if (--ret == 0 && !input_available())
6086 /* Try again */
6087 finished = FALSE;
6088 }
6089# endif
6090# ifdef FEAT_MOUSE_GPM
6091 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6092 {
6093 *check_for_gpm = 1;
6094 }
6095# endif
6096# ifdef USE_XSMP
6097 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6098 {
6099 if (fds[xsmp_idx].revents & POLLIN)
6100 {
6101 busy = TRUE;
6102 xsmp_handle_requests();
6103 busy = FALSE;
6104 }
6105 else if (fds[xsmp_idx].revents & POLLHUP)
6106 {
6107 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006108 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 xsmp_close();
6110 }
6111 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006112 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006115#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006116 /* also call when ret == 0, we may be polling a keep-open channel */
6117 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006118 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121#else /* HAVE_SELECT */
6122
6123 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006124 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006125 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006127 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006129# ifdef FEAT_MZSCHEME
6130 mzvim_check_threads();
6131 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6132 {
6133 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6134 mzquantum_used = TRUE;
6135 }
6136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006138 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006140 tv.tv_sec = towait / 1000;
6141 tv.tv_usec = (towait % 1000) * (1000000/1000);
6142 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006144 else
6145 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 /*
6148 * Select on ready for reading and exceptional condition (end of file).
6149 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006150select_eintr:
6151 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006152 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 FD_ZERO(&efds);
6154 FD_SET(fd, &rfds);
6155# if !defined(__QNX__) && !defined(__CYGWIN32__)
6156 /* For QNX select() always returns 1 if this is set. Why? */
6157 FD_SET(fd, &efds);
6158# endif
6159 maxfd = fd;
6160
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006162 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (xterm_Shell != (Widget)0)
6164 {
6165 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6166 if (maxfd < ConnectionNumber(xterm_dpy))
6167 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006168
6169 /* An event may have already been read but not handled. In
6170 * particulary, XFlush may cause this. */
6171 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 }
6173# endif
6174# ifdef FEAT_MOUSE_GPM
6175 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6176 {
6177 FD_SET(gpm_fd, &rfds);
6178 FD_SET(gpm_fd, &efds);
6179 if (maxfd < gpm_fd)
6180 maxfd = gpm_fd;
6181 }
6182# endif
6183# ifdef USE_XSMP
6184 if (xsmp_icefd != -1)
6185 {
6186 FD_SET(xsmp_icefd, &rfds);
6187 FD_SET(xsmp_icefd, &efds);
6188 if (maxfd < xsmp_icefd)
6189 maxfd = xsmp_icefd;
6190 }
6191# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006192# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006193 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006194# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006195 if (interrupted != NULL)
6196 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006198 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006199 result = ret > 0 && FD_ISSET(fd, &rfds);
6200 if (result)
6201 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006202 else if (interrupted != NULL && ret > 0)
6203 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006204
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006205# ifdef EINTR
6206 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006207 {
6208 /* Check whether window has been resized, EINTR may be caused by
6209 * SIGWINCH. */
6210 if (do_resize)
6211 handle_resize();
6212
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006213 /* Interrupted by a signal, need to try again. We ignore msec
6214 * here, because we do want to check even after a timeout if
6215 * characters are available. Needed for reading output of an
6216 * external command after the process has finished. */
6217 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006218 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006219# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006220# ifdef __TANDEM
6221 if (ret == -1 && errno == ENOTSUP)
6222 {
6223 FD_ZERO(&rfds);
6224 FD_ZERO(&efds);
6225 ret = 0;
6226 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006227# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228# ifdef FEAT_MZSCHEME
6229 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006230 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232# endif
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234# ifdef FEAT_XCLIPBOARD
6235 if (ret > 0 && xterm_Shell != (Widget)0
6236 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6237 {
6238 xterm_update(); /* Maybe we should hand out clipboard */
6239 /* continue looping when we only got the X event and the input
6240 * buffer is empty */
6241 if (--ret == 0 && !input_available())
6242 {
6243 /* Try again */
6244 finished = FALSE;
6245 }
6246 }
6247# endif
6248# ifdef FEAT_MOUSE_GPM
6249 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6250 {
6251 if (FD_ISSET(gpm_fd, &efds))
6252 gpm_close();
6253 else if (FD_ISSET(gpm_fd, &rfds))
6254 *check_for_gpm = 1;
6255 }
6256# endif
6257# ifdef USE_XSMP
6258 if (ret > 0 && xsmp_icefd != -1)
6259 {
6260 if (FD_ISSET(xsmp_icefd, &efds))
6261 {
6262 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006263 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 xsmp_close();
6265 if (--ret == 0)
6266 finished = FALSE; /* keep going if event was only one */
6267 }
6268 else if (FD_ISSET(xsmp_icefd, &rfds))
6269 {
6270 busy = TRUE;
6271 xsmp_handle_requests();
6272 busy = FALSE;
6273 if (--ret == 0)
6274 finished = FALSE; /* keep going if event was only one */
6275 }
6276 }
6277# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006278#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006279 /* also call when ret == 0, we may be polling a keep-open channel */
6280 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006281 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284#endif /* HAVE_SELECT */
6285
6286#ifdef MAY_LOOP
6287 if (finished || msec == 0)
6288 break;
6289
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006290# ifdef FEAT_CLIENTSERVER
6291 if (server_waiting())
6292 break;
6293# endif
6294
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 /* We're going to loop around again, find out for how long */
6296 if (msec > 0)
6297 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006298# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006300 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301# else
6302 /* Guess we got interrupted halfway. */
6303 msec = msec / 2;
6304# endif
6305 if (msec <= 0)
6306 break; /* waited long enough */
6307 }
6308#endif
6309 }
6310
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006311 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312}
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006316 * Expand a path into all matching files and/or directories. Handles "*",
6317 * "?", "[a-z]", "**", etc.
6318 * "path" has backslashes before chars that are not to be expanded.
6319 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 */
6321 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006322mch_expandpath(
6323 garray_T *gap,
6324 char_u *path,
6325 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaar02743632005-07-25 20:42:36 +00006327 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329#endif
6330
6331/*
6332 * mch_expand_wildcards() - this code does wild-card pattern matching using
6333 * the shell
6334 *
6335 * return OK for success, FAIL for error (you may lose some memory) and put
6336 * an error message in *file.
6337 *
6338 * num_pat is number of input patterns
6339 * pat is array of pointers to input patterns
6340 * num_file is pointer to number of matched file names
6341 * file is pointer to array of pointers to matched file names
6342 */
6343
6344#ifndef SEEK_SET
6345# define SEEK_SET 0
6346#endif
6347#ifndef SEEK_END
6348# define SEEK_END 2
6349#endif
6350
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006351#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006352
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006354mch_expand_wildcards(
6355 int num_pat,
6356 char_u **pat,
6357 int *num_file,
6358 char_u ***file,
6359 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 int i;
6362 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006363 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 char_u *p;
6365 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
Bram Moolenaarc7247912008-01-13 12:54:11 +00006367 /*
6368 * This is the non-OS/2 implementation (really Unix).
6369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 int j;
6371 char_u *tempname;
6372 char_u *command;
6373 FILE *fd;
6374 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006375#define STYLE_ECHO 0 /* use "echo", the default */
6376#define STYLE_GLOB 1 /* use "glob", for csh */
6377#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6378#define STYLE_PRINT 3 /* use "print -N", for zsh */
6379#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6380 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 int shell_style = STYLE_ECHO;
6382 int check_spaces;
6383 static int did_find_nul = FALSE;
6384 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006385 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006386 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 *num_file = 0; /* default: no files found */
6389 *file = NULL;
6390
6391 /*
6392 * If there are no wildcards, just copy the names to allocated memory.
6393 * Saves a lot of time, because we don't have to start a new shell.
6394 */
6395 if (!have_wildcard(num_pat, pat))
6396 return save_patterns(num_pat, pat, num_file, file);
6397
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006398# ifdef HAVE_SANDBOX
6399 /* Don't allow any shell command in the sandbox. */
6400 if (sandbox != 0 && check_secure())
6401 return FAIL;
6402# endif
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 /*
6405 * Don't allow the use of backticks in secure and restricted mode.
6406 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006407 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 for (i = 0; i < num_pat; ++i)
6409 if (vim_strchr(pat[i], '`') != NULL
6410 && (check_restricted() || check_secure()))
6411 return FAIL;
6412
6413 /*
6414 * get a name for the temp file
6415 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006416 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 {
6418 EMSG(_(e_notmp));
6419 return FAIL;
6420 }
6421
6422 /*
6423 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006424 * file.
6425 * STYLE_BT: NL separated
6426 * If expanding `cmd` execute it directly.
6427 * STYLE_GLOB: NUL separated
6428 * If we use *csh, "glob" will work better than "echo".
6429 * STYLE_PRINT: NL or NUL separated
6430 * If we use *zsh, "print -N" will work better than "glob".
6431 * STYLE_VIMGLOB: NL separated
6432 * If we use *sh*, we define "vimglob()".
6433 * STYLE_ECHO: space separated.
6434 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 */
6436 if (num_pat == 1 && *pat[0] == '`'
6437 && (len = STRLEN(pat[0])) > 2
6438 && *(pat[0] + len - 1) == '`')
6439 shell_style = STYLE_BT;
6440 else if ((len = STRLEN(p_sh)) >= 3)
6441 {
6442 if (STRCMP(p_sh + len - 3, "csh") == 0)
6443 shell_style = STYLE_GLOB;
6444 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6445 shell_style = STYLE_PRINT;
6446 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006447 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6448 "sh") != NULL)
6449 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
Bram Moolenaarc7247912008-01-13 12:54:11 +00006451 /* Compute the length of the command. We need 2 extra bytes: for the
6452 * optional '&' and for the NUL.
6453 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006455 if (shell_style == STYLE_VIMGLOB)
6456 len += STRLEN(sh_vimglob_func);
6457
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006458 for (i = 0; i < num_pat; ++i)
6459 {
6460 /* Count the length of the patterns in the same way as they are put in
6461 * "command" below. */
6462#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006464#else
6465 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006466 for (j = 0; pat[i][j] != NUL; ++j)
6467 {
6468 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6469 ++len; /* may add a backslash */
6470 ++len;
6471 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006472#endif
6473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 command = alloc(len);
6475 if (command == NULL)
6476 {
6477 /* out of memory */
6478 vim_free(tempname);
6479 return FAIL;
6480 }
6481
6482 /*
6483 * Build the shell command:
6484 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6485 * recognizes this).
6486 * - Add the shell command to print the expanded names.
6487 * - Add the temp file name.
6488 * - Add the file name patterns.
6489 */
6490 if (shell_style == STYLE_BT)
6491 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006492 /* change `command; command& ` to (command; command ) */
6493 STRCPY(command, "(");
6494 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006496 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006497 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 --p;
6499 if (*p == '&') /* remove trailing '&' */
6500 {
6501 ampersent = TRUE;
6502 *p = ' ';
6503 }
6504 STRCAT(command, ">");
6505 }
6506 else
6507 {
6508 if (flags & EW_NOTFOUND)
6509 STRCPY(command, "set nonomatch; ");
6510 else
6511 STRCPY(command, "unset nonomatch; ");
6512 if (shell_style == STYLE_GLOB)
6513 STRCAT(command, "glob >");
6514 else if (shell_style == STYLE_PRINT)
6515 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006516 else if (shell_style == STYLE_VIMGLOB)
6517 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 else
6519 STRCAT(command, "echo >");
6520 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006521
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006523
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (shell_style != STYLE_BT)
6525 for (i = 0; i < num_pat; ++i)
6526 {
6527 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006528 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006529 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530#ifdef USE_SYSTEM
6531 STRCAT(command, " \"");
6532 STRCAT(command, pat[i]);
6533 STRCAT(command, "\"");
6534#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006535 int intick = FALSE;
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 p = command + STRLEN(command);
6538 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006539 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006540 {
6541 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006542 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006543 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6544 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006545 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006546 * backslash inside backticks, before a special character
6547 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006548 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006549 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6550 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006551 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006552 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006553 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006554 else if (!intick
6555 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6556 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006557 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006558 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6559 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006560 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006561
6562 /* Copy one character. */
6563 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 *p = NUL;
6566#endif
6567 }
6568 if (flags & EW_SILENT)
6569 show_shell_mess = FALSE;
6570 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006571 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 /*
6574 * Using zsh -G: If a pattern has no matches, it is just deleted from
6575 * the argument list, otherwise zsh gives an error message and doesn't
6576 * expand any other pattern.
6577 */
6578 if (shell_style == STYLE_PRINT)
6579 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6580
6581 /*
6582 * If we use -f then shell variables set in .cshrc won't get expanded.
6583 * vi can do it, so we will too, but it is only necessary if there is a "$"
6584 * in one of the patterns, otherwise we can still use the fast option.
6585 */
6586 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6587 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6588
6589 /*
6590 * execute the shell command
6591 */
6592 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6593
6594 /* When running in the background, give it some time to create the temp
6595 * file, but don't wait for it to finish. */
6596 if (ampersent)
6597 mch_delay(10L, TRUE);
6598
6599 extra_shell_arg = NULL; /* cleanup */
6600 show_shell_mess = TRUE;
6601 vim_free(command);
6602
Bram Moolenaarc7247912008-01-13 12:54:11 +00006603 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
6605 mch_remove(tempname);
6606 vim_free(tempname);
6607 /*
6608 * With interactive completion, the error message is not printed.
6609 * However with USE_SYSTEM, I don't know how to turn off error messages
6610 * from the shell, so screen may still get messed up -- webb.
6611 */
6612#ifndef USE_SYSTEM
6613 if (!(flags & EW_SILENT))
6614#endif
6615 {
6616 redraw_later_clear(); /* probably messed up screen */
6617 msg_putchar('\n'); /* clear bottom line quickly */
6618 cmdline_row = Rows - 1; /* continue on last line */
6619#ifdef USE_SYSTEM
6620 if (!(flags & EW_SILENT))
6621#endif
6622 {
6623 MSG(_(e_wildexpand));
6624 msg_start(); /* don't overwrite this message */
6625 }
6626 }
6627 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6628 * EW_NOTFOUND is given */
6629 if (shell_style == STYLE_BT)
6630 return FAIL;
6631 goto notfound;
6632 }
6633
6634 /*
6635 * read the names from the file into memory
6636 */
6637 fd = fopen((char *)tempname, READBIN);
6638 if (fd == NULL)
6639 {
6640 /* Something went wrong, perhaps a file name with a special char. */
6641 if (!(flags & EW_SILENT))
6642 {
6643 MSG(_(e_wildexpand));
6644 msg_start(); /* don't overwrite this message */
6645 }
6646 vim_free(tempname);
6647 goto notfound;
6648 }
6649 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006650 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006652 if (llen < 0)
6653 /* just in case ftell() would fail */
6654 buffer = NULL;
6655 else
6656 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (buffer == NULL)
6658 {
6659 /* out of memory */
6660 mch_remove(tempname);
6661 vim_free(tempname);
6662 fclose(fd);
6663 return FAIL;
6664 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006665 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 i = fread((char *)buffer, 1, len, fd);
6667 fclose(fd);
6668 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006669 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
6671 /* unexpected read error */
6672 EMSG2(_(e_notread), tempname);
6673 vim_free(tempname);
6674 vim_free(buffer);
6675 return FAIL;
6676 }
6677 vim_free(tempname);
6678
Bram Moolenaarc7247912008-01-13 12:54:11 +00006679# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6681 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006682 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6684 *p++ = buffer[i];
6685 len = p - buffer;
6686# endif
6687
6688
6689 /* file names are separated with Space */
6690 if (shell_style == STYLE_ECHO)
6691 {
6692 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6693 p = buffer;
6694 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6695 {
6696 while (*p != ' ' && *p != '\n')
6697 ++p;
6698 p = skipwhite(p); /* skip to next entry */
6699 }
6700 }
6701 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006702 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {
6704 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6705 p = buffer;
6706 for (i = 0; *p != NUL; ++i) /* count number of entries */
6707 {
6708 while (*p != '\n' && *p != NUL)
6709 ++p;
6710 if (*p != NUL)
6711 ++p;
6712 p = skipwhite(p); /* skip leading white space */
6713 }
6714 }
6715 /* file names are separated with NUL */
6716 else
6717 {
6718 /*
6719 * Some versions of zsh use spaces instead of NULs to separate
6720 * results. Only do this when there is no NUL before the end of the
6721 * buffer, otherwise we would never be able to use file names with
6722 * embedded spaces when zsh does use NULs.
6723 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6724 * don't check for spaces again.
6725 */
6726 check_spaces = FALSE;
6727 if (shell_style == STYLE_PRINT && !did_find_nul)
6728 {
6729 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006730 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006731 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 did_find_nul = TRUE;
6733 else
6734 check_spaces = TRUE;
6735 }
6736
6737 /*
6738 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6739 * already is one, for STYLE_GLOB it needs to be added.
6740 */
6741 if (len && buffer[len - 1] == NUL)
6742 --len;
6743 else
6744 buffer[len] = NUL;
6745 i = 0;
6746 for (p = buffer; p < buffer + len; ++p)
6747 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6748 {
6749 ++i;
6750 *p = NUL;
6751 }
6752 if (len)
6753 ++i; /* count last entry */
6754 }
6755 if (i == 0)
6756 {
6757 /*
6758 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6759 * /bin/sh will happily expand it to nothing rather than returning an
6760 * error; and hey, it's good to check anyway -- webb.
6761 */
6762 vim_free(buffer);
6763 goto notfound;
6764 }
6765 *num_file = i;
6766 *file = (char_u **)alloc(sizeof(char_u *) * i);
6767 if (*file == NULL)
6768 {
6769 /* out of memory */
6770 vim_free(buffer);
6771 return FAIL;
6772 }
6773
6774 /*
6775 * Isolate the individual file names.
6776 */
6777 p = buffer;
6778 for (i = 0; i < *num_file; ++i)
6779 {
6780 (*file)[i] = p;
6781 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006782 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6783 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006785 while (!(shell_style == STYLE_ECHO && *p == ' ')
6786 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 ++p;
6788 if (p == buffer + len) /* last entry */
6789 *p = NUL;
6790 else
6791 {
6792 *p++ = NUL;
6793 p = skipwhite(p); /* skip to next entry */
6794 }
6795 }
6796 else /* NUL separates */
6797 {
6798 while (*p && p < buffer + len) /* skip entry */
6799 ++p;
6800 ++p; /* skip NUL */
6801 }
6802 }
6803
6804 /*
6805 * Move the file names to allocated memory.
6806 */
6807 for (j = 0, i = 0; i < *num_file; ++i)
6808 {
6809 /* Require the files to exist. Helps when using /bin/sh */
6810 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6811 continue;
6812
6813 /* check if this entry should be included */
6814 dir = (mch_isdir((*file)[i]));
6815 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6816 continue;
6817
Bram Moolenaara2031822006-03-07 22:29:51 +00006818 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006819 if (!dir && (flags & EW_EXEC)
6820 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006821 continue;
6822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6824 if (p)
6825 {
6826 STRCPY(p, (*file)[i]);
6827 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006828 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 (*file)[j++] = p;
6830 }
6831 }
6832 vim_free(buffer);
6833 *num_file = j;
6834
6835 if (*num_file == 0) /* rejected all entries */
6836 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006837 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 goto notfound;
6839 }
6840
6841 return OK;
6842
6843notfound:
6844 if (flags & EW_NOTFOUND)
6845 return save_patterns(num_pat, pat, num_file, file);
6846 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847}
6848
6849#endif /* VMS */
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006852save_patterns(
6853 int num_pat,
6854 char_u **pat,
6855 int *num_file,
6856 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006859 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
6861 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6862 if (*file == NULL)
6863 return FAIL;
6864 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006865 {
6866 s = vim_strsave(pat[i]);
6867 if (s != NULL)
6868 /* Be compatible with expand_filename(): halve the number of
6869 * backslashes. */
6870 backslash_halve(s);
6871 (*file)[i] = s;
6872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 *num_file = num_pat;
6874 return OK;
6875}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877/*
6878 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6879 * expand.
6880 */
6881 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006882mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006884 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (*p == '\\' && p[1] != NUL)
6887 ++p;
6888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 if (vim_strchr((char_u *)
6890#ifdef VMS
6891 "*?%"
6892#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894#endif
6895 , *p) != NULL)
6896 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
6898 return FALSE;
6899}
6900
6901/*
6902 * Return TRUE if the string "p" contains a wildcard.
6903 * Don't recognize '~' at the end as a wildcard.
6904 */
6905 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006906mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006908 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 if (*p == '\\' && p[1] != NUL)
6911 ++p;
6912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (vim_strchr((char_u *)
6914#ifdef VMS
6915 "*?%$"
6916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#endif
6919 , *p) != NULL
6920 || (*p == '~' && p[1] != NUL))
6921 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 }
6923 return FALSE;
6924}
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006927have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 int i;
6930
6931 for (i = 0; i < num; i++)
6932 if (mch_has_wildcard(file[i]))
6933 return 1;
6934 return 0;
6935}
6936
6937 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006938have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 int i;
6941
6942 for (i = 0; i < num; i++)
6943 if (vim_strchr(file[i], '$') != NULL)
6944 return TRUE;
6945 return FALSE;
6946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006948#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949/*
6950 * Scaled-down version of rename(), which is missing in Xenix.
6951 * This version can only move regular files and will fail if the
6952 * destination exists.
6953 */
6954 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006955mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
6957 struct stat st;
6958
6959 if (stat(dest, &st) >= 0) /* fail if destination exists */
6960 return -1;
6961 if (link(src, dest) != 0) /* link file to new name */
6962 return -1;
6963 if (mch_remove(src) == 0) /* delete link to old name */
6964 return 0;
6965 return -1;
6966}
6967#endif /* !HAVE_RENAME */
6968
6969#ifdef FEAT_MOUSE_GPM
6970/*
6971 * Initializes connection with gpm (if it isn't already opened)
6972 * Return 1 if succeeded (or connection already opened), 0 if failed
6973 */
6974 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006975gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6978
6979 if (!gpm_flag)
6980 {
6981 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6982 gpm_connect.defaultMask = ~GPM_HARD;
6983 /* Default handling for mouse move*/
6984 gpm_connect.minMod = 0; /* Handle any modifier keys */
6985 gpm_connect.maxMod = 0xffff;
6986 if (Gpm_Open(&gpm_connect, 0) > 0)
6987 {
6988 /* gpm library tries to handling TSTP causes
6989 * problems. Anyways, we close connection to Gpm whenever
6990 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006991 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006993# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 return 1; /* succeed */
6997 }
6998 if (gpm_fd == -2)
6999 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7000 return 0;
7001 }
7002 return 1; /* already open */
7003}
7004
7005/*
7006 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 */
7008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007009gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
7011 if (gpm_flag && gpm_fd >= 0) /* if Open */
7012 Gpm_Close();
7013}
7014
7015/* Reads gpm event and adds special keys to input buf. Returns length of
7016 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007017 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 */
7019 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007020mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 int button;
7023 static Gpm_Event gpm_event;
7024 char_u string[6];
7025 int_u vim_modifiers;
7026 int row,col;
7027 unsigned char buttons_mask;
7028 unsigned char gpm_modifiers;
7029 static unsigned char old_buttons = 0;
7030
7031 Gpm_GetEvent(&gpm_event);
7032
7033#ifdef FEAT_GUI
7034 /* Don't put events in the input queue now. */
7035 if (hold_gui_events)
7036 return 0;
7037#endif
7038
7039 row = gpm_event.y - 1;
7040 col = gpm_event.x - 1;
7041
7042 string[0] = ESC; /* Our termcode */
7043 string[1] = 'M';
7044 string[2] = 'G';
7045 switch (GPM_BARE_EVENTS(gpm_event.type))
7046 {
7047 case GPM_DRAG:
7048 string[3] = MOUSE_DRAG;
7049 break;
7050 case GPM_DOWN:
7051 buttons_mask = gpm_event.buttons & ~old_buttons;
7052 old_buttons = gpm_event.buttons;
7053 switch (buttons_mask)
7054 {
7055 case GPM_B_LEFT:
7056 button = MOUSE_LEFT;
7057 break;
7058 case GPM_B_MIDDLE:
7059 button = MOUSE_MIDDLE;
7060 break;
7061 case GPM_B_RIGHT:
7062 button = MOUSE_RIGHT;
7063 break;
7064 default:
7065 return 0;
7066 /*Don't know what to do. Can more than one button be
7067 * reported in one event? */
7068 }
7069 string[3] = (char_u)(button | 0x20);
7070 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7071 break;
7072 case GPM_UP:
7073 string[3] = MOUSE_RELEASE;
7074 old_buttons &= ~gpm_event.buttons;
7075 break;
7076 default:
7077 return 0;
7078 }
7079 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7080 gpm_modifiers = gpm_event.modifiers;
7081 vim_modifiers = 0x0;
7082 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7083 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7084 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7085 */
7086 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7087 vim_modifiers |= MOUSE_SHIFT;
7088
7089 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7090 vim_modifiers |= MOUSE_CTRL;
7091 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7092 vim_modifiers |= MOUSE_ALT;
7093 string[3] |= vim_modifiers;
7094 string[4] = (char_u)(col + ' ' + 1);
7095 string[5] = (char_u)(row + ' ' + 1);
7096 add_to_input_buf(string, 6);
7097 return 6;
7098}
7099#endif /* FEAT_MOUSE_GPM */
7100
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007101#ifdef FEAT_SYSMOUSE
7102/*
7103 * Initialize connection with sysmouse.
7104 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7105 * output, any sysmouse output than will be processed via sig_sysmouse().
7106 * Return OK if succeeded, FAIL if failed.
7107 */
7108 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007109sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007110{
7111 struct mouse_info mouse;
7112
7113 mouse.operation = MOUSE_MODE;
7114 mouse.u.mode.mode = 0;
7115 mouse.u.mode.signal = SIGUSR2;
7116 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7117 {
7118 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7119 mouse.operation = MOUSE_SHOW;
7120 ioctl(1, CONS_MOUSECTL, &mouse);
7121 return OK;
7122 }
7123 return FAIL;
7124}
7125
7126/*
7127 * Stop processing SIGUSR2 signals, and also make sure that
7128 * virtual console do not send us any sysmouse related signal.
7129 */
7130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007131sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007132{
7133 struct mouse_info mouse;
7134
7135 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7136 mouse.operation = MOUSE_MODE;
7137 mouse.u.mode.mode = 0;
7138 mouse.u.mode.signal = 0;
7139 ioctl(1, CONS_MOUSECTL, &mouse);
7140}
7141
7142/*
7143 * Gets info from sysmouse and adds special keys to input buf.
7144 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007145 static RETSIGTYPE
7146sig_sysmouse SIGDEFARG(sigarg)
7147{
7148 struct mouse_info mouse;
7149 struct video_info video;
7150 char_u string[6];
7151 int row, col;
7152 int button;
7153 int buttons;
7154 static int oldbuttons = 0;
7155
7156#ifdef FEAT_GUI
7157 /* Don't put events in the input queue now. */
7158 if (hold_gui_events)
7159 return;
7160#endif
7161
7162 mouse.operation = MOUSE_GETINFO;
7163 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7164 && ioctl(1, FBIO_MODEINFO, &video) != -1
7165 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7166 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7167 {
7168 row = mouse.u.data.y / video.vi_cheight;
7169 col = mouse.u.data.x / video.vi_cwidth;
7170 buttons = mouse.u.data.buttons;
7171 string[0] = ESC; /* Our termcode */
7172 string[1] = 'M';
7173 string[2] = 'S';
7174 if (oldbuttons == buttons && buttons != 0)
7175 {
7176 button = MOUSE_DRAG;
7177 }
7178 else
7179 {
7180 switch (buttons)
7181 {
7182 case 0:
7183 button = MOUSE_RELEASE;
7184 break;
7185 case 1:
7186 button = MOUSE_LEFT;
7187 break;
7188 case 2:
7189 button = MOUSE_MIDDLE;
7190 break;
7191 case 4:
7192 button = MOUSE_RIGHT;
7193 break;
7194 default:
7195 return;
7196 }
7197 oldbuttons = buttons;
7198 }
7199 string[3] = (char_u)(button);
7200 string[4] = (char_u)(col + ' ' + 1);
7201 string[5] = (char_u)(row + ' ' + 1);
7202 add_to_input_buf(string, 6);
7203 }
7204 return;
7205}
7206#endif /* FEAT_SYSMOUSE */
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007209typedef char_u * (*STRPROCSTR)(char_u *);
7210typedef char_u * (*INTPROCSTR)(int);
7211typedef int (*STRPROCINT)(char_u *);
7212typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214/*
7215 * Call a DLL routine which takes either a string or int param
7216 * and returns an allocated string.
7217 */
7218 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007219mch_libcall(
7220 char_u *libname,
7221 char_u *funcname,
7222 char_u *argstring, /* NULL when using a argint */
7223 int argint,
7224 char_u **string_result,/* NULL when using number_result */
7225 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
7227# if defined(USE_DLOPEN)
7228 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007229 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230# else
7231 shl_t hinstLib;
7232# endif
7233 STRPROCSTR ProcAdd;
7234 INTPROCSTR ProcAddI;
7235 char_u *retval_str = NULL;
7236 int retval_int = 0;
7237 int success = FALSE;
7238
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007239 /*
7240 * Get a handle to the DLL module.
7241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007243 /* First clear any error, it's not cleared by the dlopen() call. */
7244 (void)dlerror();
7245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 hinstLib = dlopen((char *)libname, RTLD_LAZY
7247# ifdef RTLD_LOCAL
7248 | RTLD_LOCAL
7249# endif
7250 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007251 if (hinstLib == NULL)
7252 {
7253 /* "dlerr" must be used before dlclose() */
7254 dlerr = (char *)dlerror();
7255 if (dlerr != NULL)
7256 EMSG2(_("dlerror = \"%s\""), dlerr);
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258# else
7259 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7260# endif
7261
7262 /* If the handle is valid, try to get the function address. */
7263 if (hinstLib != NULL)
7264 {
7265# ifdef HAVE_SETJMP_H
7266 /*
7267 * Catch a crash when calling the library function. For example when
7268 * using a number where a string pointer is expected.
7269 */
7270 mch_startjmp();
7271 if (SETJMP(lc_jump_env) != 0)
7272 {
7273 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007274# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007275 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 mch_didjmp();
7278 }
7279 else
7280# endif
7281 {
7282 retval_str = NULL;
7283 retval_int = 0;
7284
7285 if (argstring != NULL)
7286 {
7287# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007288 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007289 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290# else
7291 if (shl_findsym(&hinstLib, (const char *)funcname,
7292 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7293 ProcAdd = NULL;
7294# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007295 if ((success = (ProcAdd != NULL
7296# if defined(USE_DLOPEN)
7297 && dlerr == NULL
7298# endif
7299 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
7301 if (string_result == NULL)
7302 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7303 else
7304 retval_str = (ProcAdd)(argstring);
7305 }
7306 }
7307 else
7308 {
7309# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007310 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007311 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312# else
7313 if (shl_findsym(&hinstLib, (const char *)funcname,
7314 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7315 ProcAddI = NULL;
7316# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007317 if ((success = (ProcAddI != NULL
7318# if defined(USE_DLOPEN)
7319 && dlerr == NULL
7320# endif
7321 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
7323 if (string_result == NULL)
7324 retval_int = ((INTPROCINT)ProcAddI)(argint);
7325 else
7326 retval_str = (ProcAddI)(argint);
7327 }
7328 }
7329
7330 /* Save the string before we free the library. */
7331 /* Assume that a "1" or "-1" result is an illegal pointer. */
7332 if (string_result == NULL)
7333 *number_result = retval_int;
7334 else if (retval_str != NULL
7335 && retval_str != (char_u *)1
7336 && retval_str != (char_u *)-1)
7337 *string_result = vim_strsave(retval_str);
7338 }
7339
7340# ifdef HAVE_SETJMP_H
7341 mch_endjmp();
7342# ifdef SIGHASARG
7343 if (lc_signal != 0)
7344 {
7345 int i;
7346
7347 /* try to find the name of this signal */
7348 for (i = 0; signal_info[i].sig != -1; i++)
7349 if (lc_signal == signal_info[i].sig)
7350 break;
7351 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7352 }
7353# endif
7354# endif
7355
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007357 /* "dlerr" must be used before dlclose() */
7358 if (dlerr != NULL)
7359 EMSG2(_("dlerror = \"%s\""), dlerr);
7360
7361 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 (void)dlclose(hinstLib);
7363# else
7364 (void)shl_unload(hinstLib);
7365# endif
7366 }
7367
7368 if (!success)
7369 {
7370 EMSG2(_(e_libcall), funcname);
7371 return FAIL;
7372 }
7373
7374 return OK;
7375}
7376#endif
7377
7378#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7379static int xterm_trace = -1; /* default: disabled */
7380static int xterm_button;
7381
7382/*
7383 * Setup a dummy window for X selections in a terminal.
7384 */
7385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007386setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 int z = 0;
7389 char *strp = "";
7390 Widget AppShell;
7391
7392 if (!x_connect_to_server())
7393 return;
7394
7395 open_app_context();
7396 if (app_context != NULL && xterm_Shell == (Widget)0)
7397 {
7398 int (*oldhandler)();
7399#if defined(HAVE_SETJMP_H)
7400 int (*oldIOhandler)();
7401#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007402# ifdef ELAPSED_FUNC
7403 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
7405 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007406 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407# endif
7408
7409 /* Ignore X errors while opening the display */
7410 oldhandler = XSetErrorHandler(x_error_check);
7411
7412#if defined(HAVE_SETJMP_H)
7413 /* Ignore X IO errors while opening the display */
7414 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7415 mch_startjmp();
7416 if (SETJMP(lc_jump_env) != 0)
7417 {
7418 mch_didjmp();
7419 xterm_dpy = NULL;
7420 }
7421 else
7422#endif
7423 {
7424 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7425 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7426#if defined(HAVE_SETJMP_H)
7427 mch_endjmp();
7428#endif
7429 }
7430
7431#if defined(HAVE_SETJMP_H)
7432 /* Now handle X IO errors normally. */
7433 (void)XSetIOErrorHandler(oldIOhandler);
7434#endif
7435 /* Now handle X errors normally. */
7436 (void)XSetErrorHandler(oldhandler);
7437
7438 if (xterm_dpy == NULL)
7439 {
7440 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007441 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 return;
7443 }
7444
7445 /* Catch terminating error of the X server connection. */
7446 (void)XSetIOErrorHandler(x_IOerror_handler);
7447
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007448# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007450 {
7451 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007452 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007453 verbose_leave();
7454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455# endif
7456
7457 /* Create a Shell to make converters work. */
7458 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7459 applicationShellWidgetClass, xterm_dpy,
7460 NULL);
7461 if (AppShell == (Widget)0)
7462 return;
7463 xterm_Shell = XtVaCreatePopupShell("VIM",
7464 topLevelShellWidgetClass, AppShell,
7465 XtNmappedWhenManaged, 0,
7466 XtNwidth, 1,
7467 XtNheight, 1,
7468 NULL);
7469 if (xterm_Shell == (Widget)0)
7470 return;
7471
7472 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007473 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (x11_display == NULL)
7475 x11_display = xterm_dpy;
7476
7477 XtRealizeWidget(xterm_Shell);
7478 XSync(xterm_dpy, False);
7479 xterm_update();
7480 }
7481 if (xterm_Shell != (Widget)0)
7482 {
7483 clip_init(TRUE);
7484 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7485 x11_window = (Window)atol(strp);
7486 /* Check if $WINDOWID is valid. */
7487 if (test_x11_window(xterm_dpy) == FAIL)
7488 x11_window = 0;
7489 if (x11_window != 0)
7490 xterm_trace = 0;
7491 }
7492}
7493
7494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007495start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7498 return;
7499 xterm_trace = 1;
7500 xterm_button = button;
7501 do_xterm_trace();
7502}
7503
7504
7505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007506stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508 if (xterm_trace < 0)
7509 return;
7510 xterm_trace = 0;
7511}
7512
7513/*
7514 * Query the xterm pointer and generate mouse termcodes if necessary
7515 * return TRUE if dragging is active, else FALSE
7516 */
7517 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007518do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
7520 Window root, child;
7521 int root_x, root_y;
7522 int win_x, win_y;
7523 int row, col;
7524 int_u mask_return;
7525 char_u buf[50];
7526 char_u *strp;
7527 long got_hints;
7528 static char_u *mouse_code;
7529 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7530 static int prev_row = 0, prev_col = 0;
7531 static XSizeHints xterm_hints;
7532
7533 if (xterm_trace <= 0)
7534 return FALSE;
7535
7536 if (xterm_trace == 1)
7537 {
7538 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007539 * have changed recently. */
7540 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7541 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 || xterm_hints.width_inc <= 1
7543 || xterm_hints.height_inc <= 1)
7544 {
7545 xterm_trace = -1; /* Not enough data -- disable tracing */
7546 return FALSE;
7547 }
7548
7549 /* Rely on the same mouse code for the duration of this */
7550 mouse_code = find_termcode(mouse_name);
7551 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007552 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 xterm_trace = 2;
7554
7555 /* Find the offset of the chars, there might be a scrollbar on the
7556 * left of the window and/or a menu on the top (eterm etc.) */
7557 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7558 &win_x, &win_y, &mask_return);
7559 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7560 - (xterm_hints.height_inc / 2);
7561 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7562 xterm_hints.y = 2;
7563 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7564 - (xterm_hints.width_inc / 2);
7565 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7566 xterm_hints.x = 2;
7567 return TRUE;
7568 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007569 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 {
7571 xterm_trace = 0;
7572 return FALSE;
7573 }
7574
7575 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7576 &win_x, &win_y, &mask_return);
7577
7578 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7579 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7580 if (row == prev_row && col == prev_col)
7581 return TRUE;
7582
7583 STRCPY(buf, mouse_code);
7584 strp = buf + STRLEN(buf);
7585 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7586 *strp++ = (char_u)(col + ' ' + 1);
7587 *strp++ = (char_u)(row + ' ' + 1);
7588 *strp = 0;
7589 add_to_input_buf(buf, STRLEN(buf));
7590
7591 prev_row = row;
7592 prev_col = col;
7593 return TRUE;
7594}
7595
7596# if defined(FEAT_GUI) || defined(PROTO)
7597/*
7598 * Destroy the display, window and app_context. Required for GTK.
7599 */
7600 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007601clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603 if (xterm_Shell != (Widget)0)
7604 {
7605 XtDestroyWidget(xterm_Shell);
7606 xterm_Shell = (Widget)0;
7607 }
7608 if (xterm_dpy != NULL)
7609 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007610# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 /* Lesstif and Solaris crash here, lose some memory */
7612 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (x11_display == xterm_dpy)
7615 x11_display = NULL;
7616 xterm_dpy = NULL;
7617 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007618# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (app_context != (XtAppContext)NULL)
7620 {
7621 /* Lesstif and Solaris crash here, lose some memory */
7622 XtDestroyApplicationContext(app_context);
7623 app_context = (XtAppContext)NULL;
7624 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626}
7627# endif
7628
7629/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007630 * Catch up with GUI or X events.
7631 */
7632 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007633clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007634{
7635# ifdef FEAT_GUI
7636 if (gui.in_use)
7637 gui_mch_update();
7638 else
7639# endif
7640 if (xterm_Shell != (Widget)0)
7641 xterm_update();
7642}
7643
7644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 * Catch up with any queued X events. This may put keyboard input into the
7646 * input buffer, call resize call-backs, trigger timers etc. If there is
7647 * nothing in the X event queue (& no timers pending), then we return
7648 * immediately.
7649 */
7650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007651xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 XEvent event;
7654
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007655 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007657 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007659 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007660 break;
7661
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007662 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007663 {
7664 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007665 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007666#ifdef FEAT_CLIENTSERVER
7667 {
7668 XPropertyEvent *e = (XPropertyEvent *)&event;
7669
7670 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007672 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007675 XtDispatchEvent(&event);
7676 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007677 else
7678 {
7679 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007680 XtAppProcessEvent(app_context, mask);
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
7683}
7684
7685 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007686clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
7688 if (xterm_Shell != (Widget)0)
7689 return clip_x11_own_selection(xterm_Shell, cbd);
7690 return FAIL;
7691}
7692
7693 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007694clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 if (xterm_Shell != (Widget)0)
7697 clip_x11_lose_selection(xterm_Shell, cbd);
7698}
7699
7700 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007701clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703 if (xterm_Shell != (Widget)0)
7704 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7705}
7706
7707 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007708clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
7710 clip_x11_set_selection(cbd);
7711}
7712#endif
7713
7714
7715#if defined(USE_XSMP) || defined(PROTO)
7716/*
7717 * Code for X Session Management Protocol.
7718 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007719static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7720static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7721static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7722static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7723static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
7725
7726# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007727static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729/*
7730 * This is our chance to ask the user if they want to save,
7731 * or abort the logout
7732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007734xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 cmdmod_T save_cmdmod;
7737 int cancel_shutdown = False;
7738
7739 save_cmdmod = cmdmod;
7740 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007741 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 /* Mustn't logout */
7743 cancel_shutdown = True;
7744 cmdmod = save_cmdmod;
7745 setcursor(); /* position cursor */
7746 out_flush();
7747
7748 /* Done interaction */
7749 SmcInteractDone(smc_conn, cancel_shutdown);
7750
7751 /* Finish off
7752 * Only end save-yourself here if we're not cancelling shutdown;
7753 * we'll get a cancelled callback later in which we'll end it.
7754 * Hopefully get around glitchy SMs (like GNOME-1)
7755 */
7756 if (!cancel_shutdown)
7757 {
7758 xsmp.save_yourself = False;
7759 SmcSaveYourselfDone(smc_conn, True);
7760 }
7761}
7762# endif
7763
7764/*
7765 * Callback that starts save-yourself.
7766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007768xsmp_handle_save_yourself(
7769 SmcConn smc_conn,
7770 SmPointer client_data UNUSED,
7771 int save_type UNUSED,
7772 Bool shutdown,
7773 int interact_style UNUSED,
7774 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 /* Handle already being in saveyourself */
7777 if (xsmp.save_yourself)
7778 SmcSaveYourselfDone(smc_conn, True);
7779 xsmp.save_yourself = True;
7780 xsmp.shutdown = shutdown;
7781
7782 /* First up, preserve all files */
7783 out_flush();
7784 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7785
7786 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007787 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788
7789# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7790 /* Now see if we can ask about unsaved files */
7791 if (shutdown && !fast && gui.in_use)
7792 /* Need to interact with user, but need SM's permission */
7793 SmcInteractRequest(smc_conn, SmDialogError,
7794 xsmp_handle_interaction, client_data);
7795 else
7796# endif
7797 {
7798 /* Can stop the cycle here */
7799 SmcSaveYourselfDone(smc_conn, True);
7800 xsmp.save_yourself = False;
7801 }
7802}
7803
7804
7805/*
7806 * Callback to warn us of imminent death.
7807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007809xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 xsmp_close();
7812
7813 /* quit quickly leaving swapfiles for modified buffers behind */
7814 getout_preserve_modified(0);
7815}
7816
7817
7818/*
7819 * Callback to tell us that save-yourself has completed.
7820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007822xsmp_save_complete(
7823 SmcConn smc_conn UNUSED,
7824 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 xsmp.save_yourself = False;
7827}
7828
7829
7830/*
7831 * Callback to tell us that an instigated shutdown was cancelled
7832 * (maybe even by us)
7833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007835xsmp_shutdown_cancelled(
7836 SmcConn smc_conn,
7837 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 if (xsmp.save_yourself)
7840 SmcSaveYourselfDone(smc_conn, True);
7841 xsmp.save_yourself = False;
7842 xsmp.shutdown = False;
7843}
7844
7845
7846/*
7847 * Callback to tell us that a new ICE connection has been established.
7848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007850xsmp_ice_connection(
7851 IceConn iceConn,
7852 IcePointer clientData UNUSED,
7853 Bool opening,
7854 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 /* Intercept creation of ICE connection fd */
7857 if (opening)
7858 {
7859 xsmp_icefd = IceConnectionNumber(iceConn);
7860 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7861 }
7862}
7863
7864
7865/* Handle any ICE processing that's required; return FAIL if SM lost */
7866 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007867xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 Bool rep;
7870
7871 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7872 == IceProcessMessagesIOError)
7873 {
7874 /* Lost ICE */
7875 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007876 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 xsmp_close();
7878 return FAIL;
7879 }
7880 else
7881 return OK;
7882}
7883
7884static int dummy;
7885
7886/* Set up X Session Management Protocol */
7887 void
7888xsmp_init(void)
7889{
7890 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 SmcCallbacks smcallbacks;
7892#if 0
7893 SmPropValue smname;
7894 SmProp smnameprop;
7895 SmProp *smprops[1];
7896#endif
7897
7898 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007899 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
7901 xsmp.save_yourself = xsmp.shutdown = False;
7902
7903 /* Set up SM callbacks - must have all, even if they're not used */
7904 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7905 smcallbacks.save_yourself.client_data = NULL;
7906 smcallbacks.die.callback = xsmp_die;
7907 smcallbacks.die.client_data = NULL;
7908 smcallbacks.save_complete.callback = xsmp_save_complete;
7909 smcallbacks.save_complete.client_data = NULL;
7910 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7911 smcallbacks.shutdown_cancelled.client_data = NULL;
7912
7913 /* Set up a watch on ICE connection creations. The "dummy" argument is
7914 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7915 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7916 {
7917 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007918 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 return;
7920 }
7921
7922 /* Create an SM connection */
7923 xsmp.smcconn = SmcOpenConnection(
7924 NULL,
7925 NULL,
7926 SmProtoMajor,
7927 SmProtoMinor,
7928 SmcSaveYourselfProcMask | SmcDieProcMask
7929 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7930 &smcallbacks,
7931 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007932 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 sizeof(errorstring),
7934 errorstring);
7935 if (xsmp.smcconn == NULL)
7936 {
7937 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007938
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007940 {
7941 vim_snprintf(errorreport, sizeof(errorreport),
7942 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7943 verb_msg((char_u *)errorreport);
7944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 return;
7946 }
7947 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7948
7949#if 0
7950 /* ID ourselves */
7951 smname.value = "vim";
7952 smname.length = 3;
7953 smnameprop.name = "SmProgram";
7954 smnameprop.type = "SmARRAY8";
7955 smnameprop.num_vals = 1;
7956 smnameprop.vals = &smname;
7957
7958 smprops[0] = &smnameprop;
7959 SmcSetProperties(xsmp.smcconn, 1, smprops);
7960#endif
7961}
7962
7963
7964/* Shut down XSMP comms. */
7965 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007966xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
7968 if (xsmp_icefd != -1)
7969 {
7970 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007971 if (xsmp.clientid != NULL)
7972 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007973 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 xsmp_icefd = -1;
7975 }
7976}
7977#endif /* USE_XSMP */
7978
7979
7980#ifdef EBCDIC
7981/* Translate character to its CTRL- value */
7982char CtrlTable[] =
7983{
7984/* 00 - 5E */
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, 0,
7988 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7989 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7990 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7991/* ^ */ 0x1E,
7992/* - */ 0x1F,
7993/* 61 - 6C */
7994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7995/* _ */ 0x1F,
7996/* 6E - 80 */
7997 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7998/* a */ 0x01,
7999/* b */ 0x02,
8000/* c */ 0x03,
8001/* d */ 0x37,
8002/* e */ 0x2D,
8003/* f */ 0x2E,
8004/* g */ 0x2F,
8005/* h */ 0x16,
8006/* i */ 0x05,
8007/* 8A - 90 */
8008 0, 0, 0, 0, 0, 0, 0,
8009/* j */ 0x15,
8010/* k */ 0x0B,
8011/* l */ 0x0C,
8012/* m */ 0x0D,
8013/* n */ 0x0E,
8014/* o */ 0x0F,
8015/* p */ 0x10,
8016/* q */ 0x11,
8017/* r */ 0x12,
8018/* 9A - A1 */
8019 0, 0, 0, 0, 0, 0, 0, 0,
8020/* s */ 0x13,
8021/* t */ 0x3C,
8022/* u */ 0x3D,
8023/* v */ 0x32,
8024/* w */ 0x26,
8025/* x */ 0x18,
8026/* y */ 0x19,
8027/* z */ 0x3F,
8028/* AA - AC */
8029 0, 0, 0,
8030/* [ */ 0x27,
8031/* AE - BC */
8032 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8033/* ] */ 0x1D,
8034/* BE - C0 */ 0, 0, 0,
8035/* A */ 0x01,
8036/* B */ 0x02,
8037/* C */ 0x03,
8038/* D */ 0x37,
8039/* E */ 0x2D,
8040/* F */ 0x2E,
8041/* G */ 0x2F,
8042/* H */ 0x16,
8043/* I */ 0x05,
8044/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8045/* J */ 0x15,
8046/* K */ 0x0B,
8047/* L */ 0x0C,
8048/* M */ 0x0D,
8049/* N */ 0x0E,
8050/* O */ 0x0F,
8051/* P */ 0x10,
8052/* Q */ 0x11,
8053/* R */ 0x12,
8054/* DA - DF */ 0, 0, 0, 0, 0, 0,
8055/* \ */ 0x1C,
8056/* E1 */ 0,
8057/* S */ 0x13,
8058/* T */ 0x3C,
8059/* U */ 0x3D,
8060/* V */ 0x32,
8061/* W */ 0x26,
8062/* X */ 0x18,
8063/* Y */ 0x19,
8064/* Z */ 0x3F,
8065/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8066 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8067};
8068
8069char MetaCharTable[]=
8070{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8071 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8072 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8073 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8074 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8075};
8076
8077
8078/* TODO: Use characters NOT numbers!!! */
8079char CtrlCharTable[]=
8080{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8081 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8082 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8083 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8084 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8085};
8086
8087
8088#endif