blob: 59fe77ace036425f8fae7b6286699f7ef4d49676 [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
4604 if (!show_shell_mess || (options & SHELL_EXPAND))
4605 {
4606 int fd;
4607
4608 /*
4609 * Don't want to show any message from the shell. Can't just
4610 * close stdout and stderr though, because some systems will
4611 * break if you try to write to them after that, so we must
4612 * use dup() to replace them with something else -- webb
4613 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4614 * waiting for input.
4615 */
4616 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4617 fclose(stdin);
4618 fclose(stdout);
4619 fclose(stderr);
4620
4621 /*
4622 * If any of these open()'s and dup()'s fail, we just continue
4623 * anyway. It's not fatal, and on most systems it will make
4624 * no difference at all. On a few it will cause the execvp()
4625 * to exit with a non-zero status even when the completion
4626 * could be done, which is nothing too serious. If the open()
4627 * or dup() failed we'd just do the same thing ourselves
4628 * anyway -- webb
4629 */
4630 if (fd >= 0)
4631 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004632 ignored = dup(fd); /* To replace stdin (fd 0) */
4633 ignored = dup(fd); /* To replace stdout (fd 1) */
4634 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 /* Don't need this now that we've duplicated it */
4637 close(fd);
4638 }
4639 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004642 || gui.in_use
4643# endif
4644 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646
Bram Moolenaardf177f62005-02-22 08:39:57 +00004647# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004648 /* Create our own process group, so that the child and all its
4649 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004650 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004651 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004652 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004653 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004654# if defined(SIGHUP)
4655 /* When doing "!xterm&" and 'shell' is bash: the shell
4656 * will exit and send SIGHUP to all processes in its
4657 * group, killing the just started process. Ignore SIGHUP
4658 * to avoid that. (suggested by Simon Schubert)
4659 */
4660 signal(SIGHUP, SIG_IGN);
4661# endif
4662 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004663# endif
4664# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004665 if (pty_slave_fd >= 0)
4666 {
4667 /* push stream discipline modules */
4668 if (options & SHELL_COOKED)
4669 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004671 /* Try to become controlling tty (probably doesn't work,
4672 * unless run by root) */
4673 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004675 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004676# endif
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004677 set_default_child_environment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaara5792f52005-11-23 21:25:05 +00004679 /*
4680 * stderr is only redirected when using the GUI, so that a
4681 * program like gpg can still access the terminal to get a
4682 * passphrase using stderr.
4683 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (pty_master_fd >= 0)
4686 {
4687 close(pty_master_fd); /* close master side of pty */
4688
4689 /* set up stdin/stdout/stderr for the child */
4690 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004691 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004693 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004694 if (gui.in_use)
4695 {
4696 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004697 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
4700 close(pty_slave_fd); /* has been dupped, close it now */
4701 }
4702 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 /* set up stdin for the child */
4706 close(fd_toshell[1]);
4707 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004708 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 close(fd_toshell[0]);
4710
4711 /* set up stdout for the child */
4712 close(fd_fromshell[0]);
4713 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004714 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 close(fd_fromshell[1]);
4716
Bram Moolenaara5792f52005-11-23 21:25:05 +00004717# ifdef FEAT_GUI
4718 if (gui.in_use)
4719 {
4720 /* set up stderr for the child */
4721 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004722 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004723 }
4724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 /*
4729 * There is no type cast for the argv, because the type may be
4730 * different on different machines. This may cause a warning
4731 * message with strict compilers, don't worry about it.
4732 * Call _exit() instead of exit() to avoid closing the connection
4733 * to the X server (esp. with GTK, which uses atexit()).
4734 */
4735 execvp(argv[0], argv);
4736 _exit(EXEC_FAILED); /* exec failed, return failure code */
4737 }
4738 else /* parent */
4739 {
4740 /*
4741 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004742 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
4744 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004745 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004746 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004747# ifdef FEAT_JOB_CHANNEL
4748 ++dont_check_job_ended;
4749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 /*
4751 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004752 * This is also used to pipe stdin/stdout to/from the external
4753 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755 if ((options & (SHELL_READ|SHELL_WRITE))
4756# ifdef FEAT_GUI
4757 || (gui.in_use && show_shell_mess)
4758# endif
4759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004761# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004763# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4767 int ta_len = 0; /* valid bytes in ta_buf[] */
4768 int len;
4769 int p_more_save;
4770 int old_State;
4771 int c;
4772 int toshell_fd;
4773 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004774 garray_T ga;
4775 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004776# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4777 struct timeval start_tv;
4778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (pty_master_fd >= 0)
4782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 fromshell_fd = pty_master_fd;
4784 toshell_fd = dup(pty_master_fd);
4785 }
4786 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 close(fd_toshell[0]);
4790 close(fd_fromshell[1]);
4791 toshell_fd = fd_toshell[1];
4792 fromshell_fd = fd_fromshell[0];
4793 }
4794
4795 /*
4796 * Write to the child if there are typed characters.
4797 * Read from the child if there are characters available.
4798 * Repeat the reading a few times if more characters are
4799 * available. Need to check for typed keys now and then, but
4800 * not too often (delays when no chars are available).
4801 * This loop is quit if no characters can be read from the pty
4802 * (WaitForChar detected special condition), or there are no
4803 * characters available and the child has exited.
4804 * Only check if the child has exited when there is no more
4805 * output. The child may exit before all the output has
4806 * been printed.
4807 *
4808 * Currently this busy loops!
4809 * This can probably dead-lock when the write blocks!
4810 */
4811 p_more_save = p_more;
4812 p_more = FALSE;
4813 old_State = State;
4814 State = EXTERNCMD; /* don't redraw at window resize */
4815
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004816 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817 {
4818 /* Fork a process that will write the lines to the
4819 * external program. */
4820 if ((wpid = fork()) == -1)
4821 {
4822 MSG_PUTS(_("\nCannot fork\n"));
4823 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004824 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004825 {
4826 linenr_T lnum = curbuf->b_op_start.lnum;
4827 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004828 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004829 size_t l;
4830
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004831 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832 for (;;)
4833 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004834 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004835 if (l == 0)
4836 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004837 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838 /* NL -> NUL translation */
4839 len = write(toshell_fd, "", (size_t)1);
4840 else
4841 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004842 char_u *s = vim_strchr(lp + written, NL);
4843
Bram Moolenaar89d40322006-08-29 15:30:07 +00004844 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004845 s == NULL ? l
4846 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004847 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004848 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849 {
4850 /* Finished a line, add a NL, unless this line
4851 * should not have one. */
4852 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004853 || (!curbuf->b_p_bin
4854 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004855 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004856 && (lnum !=
4857 curbuf->b_ml.ml_line_count
4858 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004859 ignored = write(toshell_fd, "\n",
4860 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004861 ++lnum;
4862 if (lnum > curbuf->b_op_end.lnum)
4863 {
4864 /* finished all the lines, close pipe */
4865 close(toshell_fd);
4866 toshell_fd = -1;
4867 break;
4868 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004869 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004870 written = 0;
4871 }
4872 else if (len > 0)
4873 written += len;
4874 }
4875 _exit(0);
4876 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004877 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004878 {
4879 close(toshell_fd);
4880 toshell_fd = -1;
4881 }
4882 }
4883
4884 if (options & SHELL_READ)
4885 ga_init2(&ga, 1, BUFLEN);
4886
4887 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004888# ifdef ELAPSED_FUNC
4889 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 for (;;)
4892 {
4893 /*
4894 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004895 * if there are any.
4896 * Don't do this if we are expanding wild cards (would eat
4897 * typeahead).
4898 * Don't do this when filtering and terminal is in cooked
4899 * mode, the shell command will handle the I/O. Avoids
4900 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004901 * Don't get characters when the child has already
4902 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004903 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004904 * while (noread_cnt > 4), avoids that ":r !ls" eats
4905 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
4907 len = 0;
4908 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004909 && ((options &
4910 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4911 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004912# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004913 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004914# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004915 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004916 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004917 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004919 if (ta_len == 0)
4920 {
4921 /* Get extra characters when we don't have any.
4922 * Reset the counter and timer. */
4923 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004924# ifdef ELAPSED_FUNC
4925 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004926# endif
4927 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4928 }
4929 if (ta_len > 0 || len > 0)
4930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 /*
4932 * For pipes:
4933 * Check for CTRL-C: send interrupt signal to child.
4934 * Check for CTRL-D: EOF, close pipe to child.
4935 */
4936 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 /*
4939 * Send SIGINT to the child's group or all
4940 * processes in our group.
4941 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004942 may_send_sigint(ta_buf[ta_len], pid, wpid);
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (pty_master_fd < 0 && toshell_fd >= 0
4945 && ta_buf[ta_len] == Ctrl_D)
4946 {
4947 close(toshell_fd);
4948 toshell_fd = -1;
4949 }
4950 }
4951
4952 /* replace K_BS by <BS> and K_DEL by <DEL> */
4953 for (i = ta_len; i < ta_len + len; ++i)
4954 {
4955 if (ta_buf[i] == CSI && len - i > 2)
4956 {
4957 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4958 if (c == K_DEL || c == K_KDEL || c == K_BS)
4959 {
4960 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4961 (size_t)(len - i - 2));
4962 if (c == K_DEL || c == K_KDEL)
4963 ta_buf[i] = DEL;
4964 else
4965 ta_buf[i] = Ctrl_H;
4966 len -= 2;
4967 }
4968 }
4969 else if (ta_buf[i] == '\r')
4970 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00004971# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00004973 i += (*mb_ptr2len_len)(ta_buf + i,
4974 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977
4978 /*
4979 * For pipes: echo the typed characters.
4980 * For a pty this does not seem to work.
4981 */
4982 if (pty_master_fd < 0)
4983 {
4984 for (i = ta_len; i < ta_len + len; ++i)
4985 {
4986 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
4987 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004988# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 else if (has_mbyte)
4990 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004991 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 msg_outtrans_len(ta_buf + i, l);
4994 i += l - 1;
4995 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 else
4998 msg_outtrans_len(ta_buf + i, 1);
4999 }
5000 windgoto(msg_row, msg_col);
5001 out_flush();
5002 }
5003
5004 ta_len += len;
5005
5006 /*
5007 * Write the characters to the child, unless EOF has
5008 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005009 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005010 * When writing buffer lines, drop the typed
5011 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005013 if (options & SHELL_WRITE)
5014 ta_len = 0;
5015 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5018 if (len > 0)
5019 {
5020 ta_len -= len;
5021 mch_memmove(ta_buf, ta_buf + len, ta_len);
5022 }
5023 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026
Bram Moolenaardf177f62005-02-22 08:39:57 +00005027 if (got_int)
5028 {
5029 /* CTRL-C sends a signal to the child, we ignore it
5030 * ourselves */
5031# ifdef HAVE_SETSID
5032 kill(-pid, SIGINT);
5033# else
5034 kill(0, SIGINT);
5035# endif
5036 if (wpid > 0)
5037 kill(wpid, SIGINT);
5038 got_int = FALSE;
5039 }
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 /*
5042 * Check if the child has any characters to be printed.
5043 * Read them and write them to our window. Repeat this as
5044 * long as there is something to do, avoid the 10ms wait
5045 * for mch_inchar(), or sending typeahead characters to
5046 * the external process.
5047 * TODO: This should handle escape sequences, compatible
5048 * to some terminal (vt52?).
5049 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005050 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005051 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005053 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005054# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005056# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 );
5060 if (len <= 0) /* end of file or error */
5061 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005062
5063 noread_cnt = 0;
5064 if (options & SHELL_READ)
5065 {
5066 /* Do NUL -> NL translation, append NL separated
5067 * lines to the current buffer. */
5068 for (i = 0; i < len; ++i)
5069 {
5070 if (buffer[i] == NL)
5071 append_ga_line(&ga);
5072 else if (buffer[i] == NUL)
5073 ga_append(&ga, NL);
5074 else
5075 ga_append(&ga, buffer[i]);
5076 }
5077 }
5078# ifdef FEAT_MBYTE
5079 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 int l;
5082
Bram Moolenaardf177f62005-02-22 08:39:57 +00005083 len += buffer_off;
5084 buffer[len] = NUL;
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 /* Check if the last character in buffer[] is
5087 * incomplete, keep these bytes for the next
5088 * round. */
5089 for (p = buffer; p < buffer + len; p += l)
5090 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005091 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (l == 0)
5093 l = 1; /* NUL byte? */
5094 else if (MB_BYTE2LEN(*p) != l)
5095 break;
5096 }
5097 if (p == buffer) /* no complete character */
5098 {
5099 /* avoid getting stuck at an illegal byte */
5100 if (len >= 12)
5101 ++p;
5102 else
5103 {
5104 buffer_off = len;
5105 continue;
5106 }
5107 }
5108 c = *p;
5109 *p = NUL;
5110 msg_puts(buffer);
5111 if (p < buffer + len)
5112 {
5113 *p = c;
5114 buffer_off = (buffer + len) - p;
5115 mch_memmove(buffer, p, buffer_off);
5116 continue;
5117 }
5118 buffer_off = 0;
5119 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005120# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 buffer[len] = NUL;
5124 msg_puts(buffer);
5125 }
5126
5127 windgoto(msg_row, msg_col);
5128 cursor_on();
5129 out_flush();
5130 if (got_int)
5131 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005132
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005133# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005134 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005135 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005136 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005137
5138 /* Avoid that we keep looping here without
5139 * checking for a CTRL-C for a long time. Don't
5140 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005141 if (msec > 2000)
5142 {
5143 noread_cnt = 5;
5144 break;
5145 }
5146 }
5147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005150 /* If we already detected the child has finished, continue
5151 * reading output for a short while. Some text may be
5152 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005153 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005154 {
5155 if (noread_cnt < 5)
5156 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005157 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005158 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 /*
5161 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005162 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005164# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005165 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005166# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5170 || (wait_pid == pid && WIFEXITED(status)))
5171 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005172 /* Don't break the loop yet, try reading more
5173 * characters from "fromshell_fd" first. When using
5174 * pipes there might still be something to read and
5175 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005178 else
5179 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005180
Bram Moolenaar95a51352013-03-21 22:53:50 +01005181# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005182 /* Handle any X events, e.g. serving the clipboard. */
5183 clip_update();
5184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186finished:
5187 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005188 if (options & SHELL_READ)
5189 {
5190 if (ga.ga_len > 0)
5191 {
5192 append_ga_line(&ga);
5193 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005194 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005195 }
5196 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005197 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005198 ga_clear(&ga);
5199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 /*
5202 * Give all typeahead that wasn't used back to ui_inchar().
5203 */
5204 if (ta_len)
5205 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 State = old_State;
5207 if (toshell_fd >= 0)
5208 close(toshell_fd);
5209 close(fromshell_fd);
5210 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005211# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005212 else
5213 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005214 long delay_msec = 1;
5215
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005216 /*
5217 * Similar to the loop above, but only handle X events, no
5218 * I/O.
5219 */
5220 for (;;)
5221 {
5222 if (got_int)
5223 {
5224 /* CTRL-C sends a signal to the child, we ignore it
5225 * ourselves */
5226# ifdef HAVE_SETSID
5227 kill(-pid, SIGINT);
5228# else
5229 kill(0, SIGINT);
5230# endif
5231 got_int = FALSE;
5232 }
5233# ifdef __NeXT__
5234 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5235# else
5236 wait_pid = waitpid(pid, &status, WNOHANG);
5237# endif
5238 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5239 || (wait_pid == pid && WIFEXITED(status)))
5240 {
5241 wait_pid = pid;
5242 break;
5243 }
5244
5245 /* Handle any X events, e.g. serving the clipboard. */
5246 clip_update();
5247
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005248 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5249 * less time. */
5250 mch_delay(delay_msec, TRUE);
5251 if (++delay_msec > 10)
5252 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005253 }
5254 }
5255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 /*
5258 * Wait until our child has exited.
5259 * Ignore wait() returning pids of other children and returning
5260 * because of some signal like SIGWINCH.
5261 * Don't wait if wait_pid was already set above, indicating the
5262 * child already exited.
5263 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005264 if (wait_pid != pid)
5265 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaar624891f2010-10-13 16:22:09 +02005267# ifdef FEAT_GUI
5268 /* Close slave side of pty. Only do this after the child has
5269 * exited, otherwise the child may hang when it tries to write on
5270 * the pty. */
5271 if (pty_master_fd >= 0)
5272 close(pty_slave_fd);
5273# endif
5274
Bram Moolenaardf177f62005-02-22 08:39:57 +00005275 /* Make sure the child that writes to the external program is
5276 * dead. */
5277 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005278 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005279 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005280 wait4pid(wpid, NULL);
5281 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005282
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005283# ifdef FEAT_JOB_CHANNEL
5284 --dont_check_job_ended;
5285# endif
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 /*
5288 * Set to raw mode right now, otherwise a CTRL-C after
5289 * catch_signals() will kill Vim.
5290 */
5291 if (tmode == TMODE_RAW)
5292 settmode(TMODE_RAW);
5293 did_settmode = TRUE;
5294 set_signals();
5295
5296 if (WIFEXITED(status))
5297 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005298 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005300 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
5302 if (retval == EXEC_FAILED)
5303 {
5304 MSG_PUTS(_("\nCannot execute shell "));
5305 msg_outtrans(p_sh);
5306 msg_putchar('\n');
5307 }
5308 else if (!(options & SHELL_SILENT))
5309 {
5310 MSG_PUTS(_("\nshell returned "));
5311 msg_outnum((long)retval);
5312 msg_putchar('\n');
5313 }
5314 }
5315 }
5316 else
5317 MSG_PUTS(_("\nCommand terminated\n"));
5318 }
5319 }
5320 vim_free(argv);
Bram Moolenaarea35ef62011-08-04 22:59:28 +02005321 vim_free(p_shcf_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323error:
5324 if (!did_settmode)
5325 if (tmode == TMODE_RAW)
5326 settmode(TMODE_RAW); /* set to raw mode */
5327# ifdef FEAT_TITLE
5328 resettitle();
5329# endif
5330 vim_free(newcmd);
5331
5332 return retval;
5333
5334#endif /* USE_SYSTEM */
5335}
5336
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005337#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005338 void
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005339mch_job_start(char **argv, job_T *job, jobopt_T *options)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005340{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005341 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005342 int fd_in[2] = {-1, -1}; /* for stdin */
5343 int fd_out[2] = {-1, -1}; /* for stdout */
5344 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005345 int pty_master_fd = -1;
5346 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005347 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005348 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5349 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5350 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005351 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005352 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5353 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005354 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005355 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005356 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005357
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005358 if (use_out_for_err && use_null_for_out)
5359 use_null_for_err = TRUE;
5360
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005361 /* default is to fail */
5362 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005363
Bram Moolenaarb2412082017-08-20 18:09:14 +02005364 if (options->jo_pty
5365 && (!(use_file_for_in || use_null_for_in)
5366 || !(use_file_for_in || use_null_for_out)
5367 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005368 {
5369 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5370 if (job->jv_tty_out != NULL)
5371 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5372 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005373
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005374 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005375 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005376 if (use_file_for_in)
5377 {
5378 char_u *fname = options->jo_io_name[PART_IN];
5379
5380 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5381 if (fd_in[0] < 0)
5382 {
5383 EMSG2(_(e_notopen), fname);
5384 goto failed;
5385 }
5386 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005387 else
5388 /* When writing buffer lines to the input don't use the pty, so that
5389 * the pipe can be closed when all lines were written. */
5390 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5391 && pipe(fd_in) < 0)
5392 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005393
5394 if (use_file_for_out)
5395 {
5396 char_u *fname = options->jo_io_name[PART_OUT];
5397
5398 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5399 if (fd_out[1] < 0)
5400 {
5401 EMSG2(_(e_notopen), fname);
5402 goto failed;
5403 }
5404 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005405 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005406 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005407
5408 if (use_file_for_err)
5409 {
5410 char_u *fname = options->jo_io_name[PART_ERR];
5411
5412 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5413 if (fd_err[1] < 0)
5414 {
5415 EMSG2(_(e_notopen), fname);
5416 goto failed;
5417 }
5418 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005419 else if (!use_out_for_err && !use_null_for_err
5420 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005421 goto failed;
5422
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005423 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5424 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005425 if (options->jo_set & JO_CHANNEL)
5426 {
5427 channel = options->jo_channel;
5428 if (channel != NULL)
5429 ++channel->ch_refcount;
5430 }
5431 else
5432 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005433 if (channel == NULL)
5434 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005435 if (job->jv_tty_out != NULL)
5436 ch_log(channel, "using pty %s on fd %d",
5437 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005438 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005439
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005440 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005441 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005442 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005443 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005444 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005445 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005446 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005447 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005448 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005449 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005450 int null_fd = -1;
5451 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005452
Bram Moolenaar835dc632016-02-07 14:27:38 +01005453 /* child */
5454 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005455 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005456
5457# ifdef HAVE_SETSID
5458 /* Create our own process group, so that the child and all its
5459 * children can be kill()ed. Don't do this when using pipes,
5460 * because stdin is not a tty, we would lose /dev/tty. */
5461 (void)setsid();
5462# endif
5463
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005464# ifdef FEAT_TERMINAL
5465 if (options->jo_term_rows > 0)
5466 set_child_environment(
5467 (long)options->jo_term_rows,
5468 (long)options->jo_term_cols,
Bram Moolenaar93723a42017-07-28 15:55:32 +02005469 STRNCMP(T_NAME, "xterm", 5) == 0
5470 ? (char *)T_NAME : "xterm");
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005471 else
5472# endif
5473 set_default_child_environment();
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005474
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005475 if (options->jo_env != NULL)
5476 {
5477 dict_T *dict = options->jo_env;
5478 hashitem_T *hi;
5479 int todo = (int)dict->dv_hashtab.ht_used;
5480
5481 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5482 if (!HASHITEM_EMPTY(hi))
5483 {
5484 typval_T *item = &dict_lookup(hi)->di_tv;
5485
5486 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5487 --todo;
5488 }
5489 }
5490
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005491 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005492 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005493 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005494 if (null_fd < 0)
5495 {
5496 perror("opening /dev/null failed");
5497 _exit(OPEN_NULL_FAILED);
5498 }
5499 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005500
Bram Moolenaar223896d2017-08-02 22:33:28 +02005501 if (pty_slave_fd >= 0)
5502 {
5503 /* push stream discipline modules */
5504 SetupSlavePTY(pty_slave_fd);
5505# ifdef TIOCSCTTY
5506 /* Try to become controlling tty (probably doesn't work,
5507 * unless run by root) */
5508 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5509# endif
5510 }
5511
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005512 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005513 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005514 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005515 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005516 else if (fd_in[0] < 0)
5517 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005518 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005519 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005520
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005521 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005522 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005523 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005524 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005525 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005526 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005527 }
5528 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005529 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005530 else if (fd_err[1] < 0)
5531 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005532 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005533 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005534
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005535 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005536 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005537 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005538 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005539 else if (fd_out[1] < 0)
5540 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005541 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005542 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005543
5544 if (fd_in[0] >= 0)
5545 close(fd_in[0]);
5546 if (fd_in[1] >= 0)
5547 close(fd_in[1]);
5548 if (fd_out[0] >= 0)
5549 close(fd_out[0]);
5550 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005551 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005552 if (fd_err[0] >= 0)
5553 close(fd_err[0]);
5554 if (fd_err[1] >= 0)
5555 close(fd_err[1]);
5556 if (pty_master_fd >= 0)
5557 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005558 close(pty_master_fd); /* not used in the child */
5559 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005560 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005561
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005562 if (null_fd >= 0)
5563 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005564
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005565 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5566 _exit(EXEC_FAILED);
5567
Bram Moolenaar835dc632016-02-07 14:27:38 +01005568 /* See above for type of argv. */
5569 execvp(argv[0], argv);
5570
Bram Moolenaar4694a172016-04-21 14:05:23 +02005571 if (stderr_works)
5572 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005573# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005574 /* calling free_all_mem() here causes problems. Ignore valgrind
5575 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005576# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005577 _exit(EXEC_FAILED); /* exec failed, return failure code */
5578 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005579
5580 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005581 UNBLOCK_SIGNALS(&curset);
5582
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005583 job->jv_pid = pid;
5584 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005585 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005586
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005587 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005588 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005589 /* close child stdin, stdout and stderr */
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005590 if (!use_file_for_in && fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005591 close(fd_in[0]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005592 if (!use_file_for_out && fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005593 close(fd_out[1]);
Bram Moolenaarbe6aa462016-03-20 21:02:00 +01005594 if (!use_out_for_err && !use_file_for_err && fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005595 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005596 if (channel != NULL)
5597 {
5598 channel_set_pipes(channel,
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005599 use_file_for_in || use_null_for_in
5600 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1],
5601 use_file_for_out || use_null_for_out
5602 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0],
5603 use_out_for_err || use_file_for_err || use_null_for_err
5604 ? INVALID_FD : fd_err[0] < 0 ? pty_master_fd : fd_err[0]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005606 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005607 else
5608 {
5609 if (fd_in[1] >= 0)
5610 close(fd_in[1]);
5611 if (fd_out[0] >= 0)
5612 close(fd_out[0]);
5613 if (fd_err[0] >= 0)
5614 close(fd_err[0]);
5615 if (pty_master_fd >= 0)
5616 close(pty_master_fd);
5617 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005618
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005619 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005620 return;
5621
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005622failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005623 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005624 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005625 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005626 if (fd_in[1] >= 0)
5627 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005628 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005629 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005630 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005631 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005632 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005633 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005634 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005635 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005636 if (pty_master_fd >= 0)
5637 close(pty_master_fd);
5638 if (pty_slave_fd >= 0)
5639 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005640}
5641
5642 char *
5643mch_job_status(job_T *job)
5644{
5645# ifdef HAVE_UNION_WAIT
5646 union wait status;
5647# else
5648 int status = -1;
5649# endif
5650 pid_t wait_pid = 0;
5651
5652# ifdef __NeXT__
5653 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5654# else
5655 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5656# endif
5657 if (wait_pid == -1)
5658 {
5659 /* process must have exited */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005660 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005661 }
5662 if (wait_pid == 0)
5663 return "run";
5664 if (WIFEXITED(status))
5665 {
5666 /* LINTED avoid "bitwise operation on signed value" */
5667 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005668 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005669 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005670 if (WIFSIGNALED(status))
5671 {
5672 job->jv_exitval = -1;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005673 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005674 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005675 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005676
5677return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005678 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005679 {
5680 ch_log(job->jv_channel, "Job ended");
5681 job->jv_status = JOB_ENDED;
5682 }
5683 return "dead";
5684}
5685
5686 job_T *
5687mch_detect_ended_job(job_T *job_list)
5688{
5689# ifdef HAVE_UNION_WAIT
5690 union wait status;
5691# else
5692 int status = -1;
5693# endif
5694 pid_t wait_pid = 0;
5695 job_T *job;
5696
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005697# ifndef USE_SYSTEM
5698 /* Do not do this when waiting for a shell command to finish, we would get
5699 * the exit value here (and discard it), the exit value obtained there
5700 * would then be wrong. */
5701 if (dont_check_job_ended > 0)
5702 return NULL;
5703# endif
5704
Bram Moolenaar97792de2016-10-15 18:36:49 +02005705# ifdef __NeXT__
5706 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5707# else
5708 wait_pid = waitpid(-1, &status, WNOHANG);
5709# endif
5710 if (wait_pid <= 0)
5711 /* no process ended */
5712 return NULL;
5713 for (job = job_list; job != NULL; job = job->jv_next)
5714 {
5715 if (job->jv_pid == wait_pid)
5716 {
5717 if (WIFEXITED(status))
5718 /* LINTED avoid "bitwise operation on signed value" */
5719 job->jv_exitval = WEXITSTATUS(status);
5720 else if (WIFSIGNALED(status))
5721 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005722 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005723 {
5724 ch_log(job->jv_channel, "Job ended");
5725 job->jv_status = JOB_ENDED;
5726 }
5727 return job;
5728 }
5729 }
5730 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005731}
5732
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005733/*
5734 * Send a (deadly) signal to "job".
5735 * Return FAIL if "how" is not a valid name.
5736 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005737 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005738mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005739{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005740 int sig = -1;
5741 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005742
Bram Moolenaar923d9262016-02-25 20:56:01 +01005743 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005744 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005745 else if (STRCMP(how, "hup") == 0)
5746 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005747 else if (STRCMP(how, "quit") == 0)
5748 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005749 else if (STRCMP(how, "int") == 0)
5750 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005751 else if (STRCMP(how, "kill") == 0)
5752 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005753#ifdef SIGWINCH
5754 else if (STRCMP(how, "winch") == 0)
5755 sig = SIGWINCH;
5756#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005757 else if (isdigit(*how))
5758 sig = atoi((char *)how);
5759 else
5760 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005761
Bram Moolenaar72801402016-02-09 11:37:50 +01005762 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005763 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005764#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005765 if (job_pid == getpgid(job_pid))
5766 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005767#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005768
Bram Moolenaar61a66052017-07-22 18:39:00 +02005769 /* Never kill ourselves! */
5770 if (job_pid != 0)
5771 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005772
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773 return OK;
5774}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005775
5776/*
5777 * Clear the data related to "job".
5778 */
5779 void
5780mch_clear_job(job_T *job)
5781{
5782 /* call waitpid because child process may become zombie */
5783# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005784 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005785# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005786 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005787# endif
5788}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005789#endif
5790
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005791#if defined(FEAT_TERMINAL) || defined(PROTO)
5792 int
5793mch_create_pty_channel(job_T *job, jobopt_T *options)
5794{
5795 int pty_master_fd = -1;
5796 int pty_slave_fd = -1;
5797 channel_T *channel;
5798
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005799 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5800 if (job->jv_tty_out != NULL)
5801 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005802 close(pty_slave_fd);
5803
5804 channel = add_channel();
5805 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005806 {
5807 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005808 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005809 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005810 if (job->jv_tty_out != NULL)
5811 ch_log(channel, "using pty %s on fd %d",
5812 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005813 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5814 channel->ch_keep_open = TRUE;
5815
5816 channel_set_pipes(channel, pty_master_fd, pty_master_fd, pty_master_fd);
5817 channel_set_job(channel, job, options);
5818 return OK;
5819}
5820#endif
5821
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822/*
5823 * Check for CTRL-C typed by reading all available characters.
5824 * In cooked mode we should get SIGINT, no need to check.
5825 */
5826 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005827mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005829 if ((curr_tmode == TMODE_RAW || force)
5830 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 fill_input_buf(FALSE);
5832}
5833
5834/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005835 * Wait "msec" msec until a character is available from the mouse, keyboard,
5836 * from inbuf[].
5837 * "msec" == -1 will block forever.
5838 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005839 * When "ignore_input" is TRUE even check for pending input when input is
5840 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005841 * "interrupted" (if not NULL) is set to TRUE when no character is available
5842 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005843 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005844 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 */
5846 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005847WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005849#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005850 return ui_wait_for_chars_or_timer(
5851 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005852#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005853 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005854#endif
5855}
5856
5857/*
5858 * Wait "msec" msec until a character is available from the mouse or keyboard
5859 * or from inbuf[].
5860 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005861 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005862 * "interrupted" (if not NULL) is set to TRUE when no character is available
5863 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005864 * When a GUI is being used, this will never get called -- webb
5865 */
5866 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005867WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005868{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#ifdef FEAT_MOUSE_GPM
5870 int gpm_process_wanted;
5871#endif
5872#ifdef FEAT_XCLIPBOARD
5873 int rest;
5874#endif
5875 int avail;
5876
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005877 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 return 1;
5879
5880#if defined(FEAT_MOUSE_DEC)
5881 /* May need to query the mouse position. */
5882 if (WantQueryMouse)
5883 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005884 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5886 }
5887#endif
5888
5889 /*
5890 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5891 * events. This is a bit complicated, because they might both be defined.
5892 */
5893#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5894# ifdef FEAT_XCLIPBOARD
5895 rest = 0;
5896 if (do_xterm_trace())
5897 rest = msec;
5898# endif
5899 do
5900 {
5901# ifdef FEAT_XCLIPBOARD
5902 if (rest != 0)
5903 {
5904 msec = XT_TRACE_DELAY;
5905 if (rest >= 0 && rest < XT_TRACE_DELAY)
5906 msec = rest;
5907 if (rest >= 0)
5908 rest -= msec;
5909 }
5910# endif
5911# ifdef FEAT_MOUSE_GPM
5912 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005913 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02005914 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005916 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917# endif
5918 if (!avail)
5919 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005920 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 return 1;
5922# ifdef FEAT_XCLIPBOARD
5923 if (rest == 0 || !do_xterm_trace())
5924# endif
5925 break;
5926 }
5927 }
5928 while (FALSE
5929# ifdef FEAT_MOUSE_GPM
5930 || (gpm_process_wanted && mch_gpm_process() == 0)
5931# endif
5932# ifdef FEAT_XCLIPBOARD
5933 || (!avail && rest != 0)
5934# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005935 )
5936 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
5938#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02005939 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940#endif
5941 return avail;
5942}
5943
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01005944#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945/*
5946 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02005947 * "msec" == 0 will check for characters once.
5948 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005951 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005952 * "interrupted" (if not NULL) is set to TRUE when no character is available
5953 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#if defined(__BEOS__)
5956 int
5957#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005958 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02005960RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01005963 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005964#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 static int busy = FALSE;
5966
5967 /* May retry getting characters after an event was handled. */
5968# define MAY_LOOP
5969
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005970# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 /* Remember at what time we started, so that we know how much longer we
5972 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005973 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005974 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02005976 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005977 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978# endif
5979
5980 /* Handle being called recursively. This may happen for the session
5981 * manager stuff, it may save the file, which does a breakcheck. */
5982 if (busy)
5983 return 0;
5984#endif
5985
5986#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005987 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988#endif
5989 {
5990#ifdef MAY_LOOP
5991 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005992# ifdef FEAT_MZSCHEME
5993 int mzquantum_used = FALSE;
5994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995#endif
5996#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005997 /* each channel may use in, out and err */
5998 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 int nfd;
6000# ifdef FEAT_XCLIPBOARD
6001 int xterm_idx = -1;
6002# endif
6003# ifdef FEAT_MOUSE_GPM
6004 int gpm_idx = -1;
6005# endif
6006# ifdef USE_XSMP
6007 int xsmp_idx = -1;
6008# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006009 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006011# ifdef FEAT_MZSCHEME
6012 mzvim_check_threads();
6013 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6014 {
6015 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6016 mzquantum_used = TRUE;
6017 }
6018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 fds[0].fd = fd;
6020 fds[0].events = POLLIN;
6021 nfd = 1;
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006024 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if (xterm_Shell != (Widget)0)
6026 {
6027 xterm_idx = nfd;
6028 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6029 fds[nfd].events = POLLIN;
6030 nfd++;
6031 }
6032# endif
6033# ifdef FEAT_MOUSE_GPM
6034 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6035 {
6036 gpm_idx = nfd;
6037 fds[nfd].fd = gpm_fd;
6038 fds[nfd].events = POLLIN;
6039 nfd++;
6040 }
6041# endif
6042# ifdef USE_XSMP
6043 if (xsmp_icefd != -1)
6044 {
6045 xsmp_idx = nfd;
6046 fds[nfd].fd = xsmp_icefd;
6047 fds[nfd].events = POLLIN;
6048 nfd++;
6049 }
6050# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006051#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006052 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006053#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006054 if (interrupted != NULL)
6055 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006057 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006058
6059 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006060 if (result == 0 && interrupted != NULL && ret > 0)
6061 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006062
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006063# ifdef FEAT_MZSCHEME
6064 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006065 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006066 finished = FALSE;
6067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069# ifdef FEAT_XCLIPBOARD
6070 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6071 {
6072 xterm_update(); /* Maybe we should hand out clipboard */
6073 if (--ret == 0 && !input_available())
6074 /* Try again */
6075 finished = FALSE;
6076 }
6077# endif
6078# ifdef FEAT_MOUSE_GPM
6079 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6080 {
6081 *check_for_gpm = 1;
6082 }
6083# endif
6084# ifdef USE_XSMP
6085 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6086 {
6087 if (fds[xsmp_idx].revents & POLLIN)
6088 {
6089 busy = TRUE;
6090 xsmp_handle_requests();
6091 busy = FALSE;
6092 }
6093 else if (fds[xsmp_idx].revents & POLLHUP)
6094 {
6095 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006096 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 xsmp_close();
6098 }
6099 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006100 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
6102# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006103#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006104 /* also call when ret == 0, we may be polling a keep-open channel */
6105 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006106 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109#else /* HAVE_SELECT */
6110
6111 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006112 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006113 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006115 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006117# ifdef FEAT_MZSCHEME
6118 mzvim_check_threads();
6119 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6120 {
6121 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6122 mzquantum_used = TRUE;
6123 }
6124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006126 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006128 tv.tv_sec = towait / 1000;
6129 tv.tv_usec = (towait % 1000) * (1000000/1000);
6130 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006132 else
6133 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135 /*
6136 * Select on ready for reading and exceptional condition (end of file).
6137 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006138select_eintr:
6139 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006140 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 FD_ZERO(&efds);
6142 FD_SET(fd, &rfds);
6143# if !defined(__QNX__) && !defined(__CYGWIN32__)
6144 /* For QNX select() always returns 1 if this is set. Why? */
6145 FD_SET(fd, &efds);
6146# endif
6147 maxfd = fd;
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006150 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (xterm_Shell != (Widget)0)
6152 {
6153 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6154 if (maxfd < ConnectionNumber(xterm_dpy))
6155 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006156
6157 /* An event may have already been read but not handled. In
6158 * particulary, XFlush may cause this. */
6159 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161# endif
6162# ifdef FEAT_MOUSE_GPM
6163 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6164 {
6165 FD_SET(gpm_fd, &rfds);
6166 FD_SET(gpm_fd, &efds);
6167 if (maxfd < gpm_fd)
6168 maxfd = gpm_fd;
6169 }
6170# endif
6171# ifdef USE_XSMP
6172 if (xsmp_icefd != -1)
6173 {
6174 FD_SET(xsmp_icefd, &rfds);
6175 FD_SET(xsmp_icefd, &efds);
6176 if (maxfd < xsmp_icefd)
6177 maxfd = xsmp_icefd;
6178 }
6179# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006180# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006181 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006182# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006183 if (interrupted != NULL)
6184 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006186 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006187 result = ret > 0 && FD_ISSET(fd, &rfds);
6188 if (result)
6189 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006190 else if (interrupted != NULL && ret > 0)
6191 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006192
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006193# ifdef EINTR
6194 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006195 {
6196 /* Check whether window has been resized, EINTR may be caused by
6197 * SIGWINCH. */
6198 if (do_resize)
6199 handle_resize();
6200
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006201 /* Interrupted by a signal, need to try again. We ignore msec
6202 * here, because we do want to check even after a timeout if
6203 * characters are available. Needed for reading output of an
6204 * external command after the process has finished. */
6205 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006206 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006207# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006208# ifdef __TANDEM
6209 if (ret == -1 && errno == ENOTSUP)
6210 {
6211 FD_ZERO(&rfds);
6212 FD_ZERO(&efds);
6213 ret = 0;
6214 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006215# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006216# ifdef FEAT_MZSCHEME
6217 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006218 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006219 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220# endif
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222# ifdef FEAT_XCLIPBOARD
6223 if (ret > 0 && xterm_Shell != (Widget)0
6224 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6225 {
6226 xterm_update(); /* Maybe we should hand out clipboard */
6227 /* continue looping when we only got the X event and the input
6228 * buffer is empty */
6229 if (--ret == 0 && !input_available())
6230 {
6231 /* Try again */
6232 finished = FALSE;
6233 }
6234 }
6235# endif
6236# ifdef FEAT_MOUSE_GPM
6237 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6238 {
6239 if (FD_ISSET(gpm_fd, &efds))
6240 gpm_close();
6241 else if (FD_ISSET(gpm_fd, &rfds))
6242 *check_for_gpm = 1;
6243 }
6244# endif
6245# ifdef USE_XSMP
6246 if (ret > 0 && xsmp_icefd != -1)
6247 {
6248 if (FD_ISSET(xsmp_icefd, &efds))
6249 {
6250 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006251 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 xsmp_close();
6253 if (--ret == 0)
6254 finished = FALSE; /* keep going if event was only one */
6255 }
6256 else if (FD_ISSET(xsmp_icefd, &rfds))
6257 {
6258 busy = TRUE;
6259 xsmp_handle_requests();
6260 busy = FALSE;
6261 if (--ret == 0)
6262 finished = FALSE; /* keep going if event was only one */
6263 }
6264 }
6265# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006266#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006267 /* also call when ret == 0, we may be polling a keep-open channel */
6268 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006269 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
6272#endif /* HAVE_SELECT */
6273
6274#ifdef MAY_LOOP
6275 if (finished || msec == 0)
6276 break;
6277
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006278# ifdef FEAT_CLIENTSERVER
6279 if (server_waiting())
6280 break;
6281# endif
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 /* We're going to loop around again, find out for how long */
6284 if (msec > 0)
6285 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006286# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006288 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289# else
6290 /* Guess we got interrupted halfway. */
6291 msec = msec / 2;
6292# endif
6293 if (msec <= 0)
6294 break; /* waited long enough */
6295 }
6296#endif
6297 }
6298
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006299 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006304 * Expand a path into all matching files and/or directories. Handles "*",
6305 * "?", "[a-z]", "**", etc.
6306 * "path" has backslashes before chars that are not to be expanded.
6307 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 */
6309 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006310mch_expandpath(
6311 garray_T *gap,
6312 char_u *path,
6313 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
Bram Moolenaar02743632005-07-25 20:42:36 +00006315 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316}
6317#endif
6318
6319/*
6320 * mch_expand_wildcards() - this code does wild-card pattern matching using
6321 * the shell
6322 *
6323 * return OK for success, FAIL for error (you may lose some memory) and put
6324 * an error message in *file.
6325 *
6326 * num_pat is number of input patterns
6327 * pat is array of pointers to input patterns
6328 * num_file is pointer to number of matched file names
6329 * file is pointer to array of pointers to matched file names
6330 */
6331
6332#ifndef SEEK_SET
6333# define SEEK_SET 0
6334#endif
6335#ifndef SEEK_END
6336# define SEEK_END 2
6337#endif
6338
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006339#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006340
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006342mch_expand_wildcards(
6343 int num_pat,
6344 char_u **pat,
6345 int *num_file,
6346 char_u ***file,
6347 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
6349 int i;
6350 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006351 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 char_u *p;
6353 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
Bram Moolenaarc7247912008-01-13 12:54:11 +00006355 /*
6356 * This is the non-OS/2 implementation (really Unix).
6357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 int j;
6359 char_u *tempname;
6360 char_u *command;
6361 FILE *fd;
6362 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006363#define STYLE_ECHO 0 /* use "echo", the default */
6364#define STYLE_GLOB 1 /* use "glob", for csh */
6365#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6366#define STYLE_PRINT 3 /* use "print -N", for zsh */
6367#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6368 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 int shell_style = STYLE_ECHO;
6370 int check_spaces;
6371 static int did_find_nul = FALSE;
6372 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006373 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006374 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 *num_file = 0; /* default: no files found */
6377 *file = NULL;
6378
6379 /*
6380 * If there are no wildcards, just copy the names to allocated memory.
6381 * Saves a lot of time, because we don't have to start a new shell.
6382 */
6383 if (!have_wildcard(num_pat, pat))
6384 return save_patterns(num_pat, pat, num_file, file);
6385
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006386# ifdef HAVE_SANDBOX
6387 /* Don't allow any shell command in the sandbox. */
6388 if (sandbox != 0 && check_secure())
6389 return FAIL;
6390# endif
6391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 /*
6393 * Don't allow the use of backticks in secure and restricted mode.
6394 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006395 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 for (i = 0; i < num_pat; ++i)
6397 if (vim_strchr(pat[i], '`') != NULL
6398 && (check_restricted() || check_secure()))
6399 return FAIL;
6400
6401 /*
6402 * get a name for the temp file
6403 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006404 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 EMSG(_(e_notmp));
6407 return FAIL;
6408 }
6409
6410 /*
6411 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006412 * file.
6413 * STYLE_BT: NL separated
6414 * If expanding `cmd` execute it directly.
6415 * STYLE_GLOB: NUL separated
6416 * If we use *csh, "glob" will work better than "echo".
6417 * STYLE_PRINT: NL or NUL separated
6418 * If we use *zsh, "print -N" will work better than "glob".
6419 * STYLE_VIMGLOB: NL separated
6420 * If we use *sh*, we define "vimglob()".
6421 * STYLE_ECHO: space separated.
6422 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 */
6424 if (num_pat == 1 && *pat[0] == '`'
6425 && (len = STRLEN(pat[0])) > 2
6426 && *(pat[0] + len - 1) == '`')
6427 shell_style = STYLE_BT;
6428 else if ((len = STRLEN(p_sh)) >= 3)
6429 {
6430 if (STRCMP(p_sh + len - 3, "csh") == 0)
6431 shell_style = STYLE_GLOB;
6432 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6433 shell_style = STYLE_PRINT;
6434 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006435 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6436 "sh") != NULL)
6437 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438
Bram Moolenaarc7247912008-01-13 12:54:11 +00006439 /* Compute the length of the command. We need 2 extra bytes: for the
6440 * optional '&' and for the NUL.
6441 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006443 if (shell_style == STYLE_VIMGLOB)
6444 len += STRLEN(sh_vimglob_func);
6445
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006446 for (i = 0; i < num_pat; ++i)
6447 {
6448 /* Count the length of the patterns in the same way as they are put in
6449 * "command" below. */
6450#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006452#else
6453 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006454 for (j = 0; pat[i][j] != NUL; ++j)
6455 {
6456 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6457 ++len; /* may add a backslash */
6458 ++len;
6459 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006460#endif
6461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 command = alloc(len);
6463 if (command == NULL)
6464 {
6465 /* out of memory */
6466 vim_free(tempname);
6467 return FAIL;
6468 }
6469
6470 /*
6471 * Build the shell command:
6472 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6473 * recognizes this).
6474 * - Add the shell command to print the expanded names.
6475 * - Add the temp file name.
6476 * - Add the file name patterns.
6477 */
6478 if (shell_style == STYLE_BT)
6479 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006480 /* change `command; command& ` to (command; command ) */
6481 STRCPY(command, "(");
6482 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006484 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006485 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 --p;
6487 if (*p == '&') /* remove trailing '&' */
6488 {
6489 ampersent = TRUE;
6490 *p = ' ';
6491 }
6492 STRCAT(command, ">");
6493 }
6494 else
6495 {
6496 if (flags & EW_NOTFOUND)
6497 STRCPY(command, "set nonomatch; ");
6498 else
6499 STRCPY(command, "unset nonomatch; ");
6500 if (shell_style == STYLE_GLOB)
6501 STRCAT(command, "glob >");
6502 else if (shell_style == STYLE_PRINT)
6503 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006504 else if (shell_style == STYLE_VIMGLOB)
6505 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 else
6507 STRCAT(command, "echo >");
6508 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (shell_style != STYLE_BT)
6513 for (i = 0; i < num_pat; ++i)
6514 {
6515 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006516 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006517 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518#ifdef USE_SYSTEM
6519 STRCAT(command, " \"");
6520 STRCAT(command, pat[i]);
6521 STRCAT(command, "\"");
6522#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006523 int intick = FALSE;
6524
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 p = command + STRLEN(command);
6526 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006527 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006528 {
6529 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006530 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006531 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6532 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006533 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006534 * backslash inside backticks, before a special character
6535 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006536 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006537 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6538 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006539 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006540 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006541 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006542 else if (!intick
6543 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6544 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006545 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006546 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6547 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006548 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006549
6550 /* Copy one character. */
6551 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 *p = NUL;
6554#endif
6555 }
6556 if (flags & EW_SILENT)
6557 show_shell_mess = FALSE;
6558 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006559 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 /*
6562 * Using zsh -G: If a pattern has no matches, it is just deleted from
6563 * the argument list, otherwise zsh gives an error message and doesn't
6564 * expand any other pattern.
6565 */
6566 if (shell_style == STYLE_PRINT)
6567 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6568
6569 /*
6570 * If we use -f then shell variables set in .cshrc won't get expanded.
6571 * vi can do it, so we will too, but it is only necessary if there is a "$"
6572 * in one of the patterns, otherwise we can still use the fast option.
6573 */
6574 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6575 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6576
6577 /*
6578 * execute the shell command
6579 */
6580 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6581
6582 /* When running in the background, give it some time to create the temp
6583 * file, but don't wait for it to finish. */
6584 if (ampersent)
6585 mch_delay(10L, TRUE);
6586
6587 extra_shell_arg = NULL; /* cleanup */
6588 show_shell_mess = TRUE;
6589 vim_free(command);
6590
Bram Moolenaarc7247912008-01-13 12:54:11 +00006591 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
6593 mch_remove(tempname);
6594 vim_free(tempname);
6595 /*
6596 * With interactive completion, the error message is not printed.
6597 * However with USE_SYSTEM, I don't know how to turn off error messages
6598 * from the shell, so screen may still get messed up -- webb.
6599 */
6600#ifndef USE_SYSTEM
6601 if (!(flags & EW_SILENT))
6602#endif
6603 {
6604 redraw_later_clear(); /* probably messed up screen */
6605 msg_putchar('\n'); /* clear bottom line quickly */
6606 cmdline_row = Rows - 1; /* continue on last line */
6607#ifdef USE_SYSTEM
6608 if (!(flags & EW_SILENT))
6609#endif
6610 {
6611 MSG(_(e_wildexpand));
6612 msg_start(); /* don't overwrite this message */
6613 }
6614 }
6615 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6616 * EW_NOTFOUND is given */
6617 if (shell_style == STYLE_BT)
6618 return FAIL;
6619 goto notfound;
6620 }
6621
6622 /*
6623 * read the names from the file into memory
6624 */
6625 fd = fopen((char *)tempname, READBIN);
6626 if (fd == NULL)
6627 {
6628 /* Something went wrong, perhaps a file name with a special char. */
6629 if (!(flags & EW_SILENT))
6630 {
6631 MSG(_(e_wildexpand));
6632 msg_start(); /* don't overwrite this message */
6633 }
6634 vim_free(tempname);
6635 goto notfound;
6636 }
6637 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006638 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006640 if (llen < 0)
6641 /* just in case ftell() would fail */
6642 buffer = NULL;
6643 else
6644 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (buffer == NULL)
6646 {
6647 /* out of memory */
6648 mch_remove(tempname);
6649 vim_free(tempname);
6650 fclose(fd);
6651 return FAIL;
6652 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006653 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 i = fread((char *)buffer, 1, len, fd);
6655 fclose(fd);
6656 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006657 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
6659 /* unexpected read error */
6660 EMSG2(_(e_notread), tempname);
6661 vim_free(tempname);
6662 vim_free(buffer);
6663 return FAIL;
6664 }
6665 vim_free(tempname);
6666
Bram Moolenaarc7247912008-01-13 12:54:11 +00006667# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6669 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006670 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6672 *p++ = buffer[i];
6673 len = p - buffer;
6674# endif
6675
6676
6677 /* file names are separated with Space */
6678 if (shell_style == STYLE_ECHO)
6679 {
6680 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6681 p = buffer;
6682 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6683 {
6684 while (*p != ' ' && *p != '\n')
6685 ++p;
6686 p = skipwhite(p); /* skip to next entry */
6687 }
6688 }
6689 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006690 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {
6692 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6693 p = buffer;
6694 for (i = 0; *p != NUL; ++i) /* count number of entries */
6695 {
6696 while (*p != '\n' && *p != NUL)
6697 ++p;
6698 if (*p != NUL)
6699 ++p;
6700 p = skipwhite(p); /* skip leading white space */
6701 }
6702 }
6703 /* file names are separated with NUL */
6704 else
6705 {
6706 /*
6707 * Some versions of zsh use spaces instead of NULs to separate
6708 * results. Only do this when there is no NUL before the end of the
6709 * buffer, otherwise we would never be able to use file names with
6710 * embedded spaces when zsh does use NULs.
6711 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6712 * don't check for spaces again.
6713 */
6714 check_spaces = FALSE;
6715 if (shell_style == STYLE_PRINT && !did_find_nul)
6716 {
6717 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006718 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006719 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 did_find_nul = TRUE;
6721 else
6722 check_spaces = TRUE;
6723 }
6724
6725 /*
6726 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6727 * already is one, for STYLE_GLOB it needs to be added.
6728 */
6729 if (len && buffer[len - 1] == NUL)
6730 --len;
6731 else
6732 buffer[len] = NUL;
6733 i = 0;
6734 for (p = buffer; p < buffer + len; ++p)
6735 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6736 {
6737 ++i;
6738 *p = NUL;
6739 }
6740 if (len)
6741 ++i; /* count last entry */
6742 }
6743 if (i == 0)
6744 {
6745 /*
6746 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6747 * /bin/sh will happily expand it to nothing rather than returning an
6748 * error; and hey, it's good to check anyway -- webb.
6749 */
6750 vim_free(buffer);
6751 goto notfound;
6752 }
6753 *num_file = i;
6754 *file = (char_u **)alloc(sizeof(char_u *) * i);
6755 if (*file == NULL)
6756 {
6757 /* out of memory */
6758 vim_free(buffer);
6759 return FAIL;
6760 }
6761
6762 /*
6763 * Isolate the individual file names.
6764 */
6765 p = buffer;
6766 for (i = 0; i < *num_file; ++i)
6767 {
6768 (*file)[i] = p;
6769 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006770 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6771 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006773 while (!(shell_style == STYLE_ECHO && *p == ' ')
6774 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 ++p;
6776 if (p == buffer + len) /* last entry */
6777 *p = NUL;
6778 else
6779 {
6780 *p++ = NUL;
6781 p = skipwhite(p); /* skip to next entry */
6782 }
6783 }
6784 else /* NUL separates */
6785 {
6786 while (*p && p < buffer + len) /* skip entry */
6787 ++p;
6788 ++p; /* skip NUL */
6789 }
6790 }
6791
6792 /*
6793 * Move the file names to allocated memory.
6794 */
6795 for (j = 0, i = 0; i < *num_file; ++i)
6796 {
6797 /* Require the files to exist. Helps when using /bin/sh */
6798 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6799 continue;
6800
6801 /* check if this entry should be included */
6802 dir = (mch_isdir((*file)[i]));
6803 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6804 continue;
6805
Bram Moolenaara2031822006-03-07 22:29:51 +00006806 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006807 if (!dir && (flags & EW_EXEC)
6808 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006809 continue;
6810
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6812 if (p)
6813 {
6814 STRCPY(p, (*file)[i]);
6815 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006816 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 (*file)[j++] = p;
6818 }
6819 }
6820 vim_free(buffer);
6821 *num_file = j;
6822
6823 if (*num_file == 0) /* rejected all entries */
6824 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006825 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 goto notfound;
6827 }
6828
6829 return OK;
6830
6831notfound:
6832 if (flags & EW_NOTFOUND)
6833 return save_patterns(num_pat, pat, num_file, file);
6834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835}
6836
6837#endif /* VMS */
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006840save_patterns(
6841 int num_pat,
6842 char_u **pat,
6843 int *num_file,
6844 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006847 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
6849 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6850 if (*file == NULL)
6851 return FAIL;
6852 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006853 {
6854 s = vim_strsave(pat[i]);
6855 if (s != NULL)
6856 /* Be compatible with expand_filename(): halve the number of
6857 * backslashes. */
6858 backslash_halve(s);
6859 (*file)[i] = s;
6860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 *num_file = num_pat;
6862 return OK;
6863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865/*
6866 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6867 * expand.
6868 */
6869 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006870mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006872 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 if (*p == '\\' && p[1] != NUL)
6875 ++p;
6876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 if (vim_strchr((char_u *)
6878#ifdef VMS
6879 "*?%"
6880#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882#endif
6883 , *p) != NULL)
6884 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 return FALSE;
6887}
6888
6889/*
6890 * Return TRUE if the string "p" contains a wildcard.
6891 * Don't recognize '~' at the end as a wildcard.
6892 */
6893 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006894mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006896 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (*p == '\\' && p[1] != NUL)
6899 ++p;
6900 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (vim_strchr((char_u *)
6902#ifdef VMS
6903 "*?%$"
6904#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#endif
6907 , *p) != NULL
6908 || (*p == '~' && p[1] != NUL))
6909 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 }
6911 return FALSE;
6912}
6913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006915have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
6917 int i;
6918
6919 for (i = 0; i < num; i++)
6920 if (mch_has_wildcard(file[i]))
6921 return 1;
6922 return 0;
6923}
6924
6925 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006926have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
6928 int i;
6929
6930 for (i = 0; i < num; i++)
6931 if (vim_strchr(file[i], '$') != NULL)
6932 return TRUE;
6933 return FALSE;
6934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006936#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937/*
6938 * Scaled-down version of rename(), which is missing in Xenix.
6939 * This version can only move regular files and will fail if the
6940 * destination exists.
6941 */
6942 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01006943mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 struct stat st;
6946
6947 if (stat(dest, &st) >= 0) /* fail if destination exists */
6948 return -1;
6949 if (link(src, dest) != 0) /* link file to new name */
6950 return -1;
6951 if (mch_remove(src) == 0) /* delete link to old name */
6952 return 0;
6953 return -1;
6954}
6955#endif /* !HAVE_RENAME */
6956
6957#ifdef FEAT_MOUSE_GPM
6958/*
6959 * Initializes connection with gpm (if it isn't already opened)
6960 * Return 1 if succeeded (or connection already opened), 0 if failed
6961 */
6962 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006963gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
6966
6967 if (!gpm_flag)
6968 {
6969 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
6970 gpm_connect.defaultMask = ~GPM_HARD;
6971 /* Default handling for mouse move*/
6972 gpm_connect.minMod = 0; /* Handle any modifier keys */
6973 gpm_connect.maxMod = 0xffff;
6974 if (Gpm_Open(&gpm_connect, 0) > 0)
6975 {
6976 /* gpm library tries to handling TSTP causes
6977 * problems. Anyways, we close connection to Gpm whenever
6978 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006979 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006981# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00006983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 return 1; /* succeed */
6985 }
6986 if (gpm_fd == -2)
6987 Gpm_Close(); /* We don't want to talk to xterm via gpm */
6988 return 0;
6989 }
6990 return 1; /* already open */
6991}
6992
6993/*
6994 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 */
6996 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006997gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 if (gpm_flag && gpm_fd >= 0) /* if Open */
7000 Gpm_Close();
7001}
7002
7003/* Reads gpm event and adds special keys to input buf. Returns length of
7004 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007005 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 */
7007 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007008mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 int button;
7011 static Gpm_Event gpm_event;
7012 char_u string[6];
7013 int_u vim_modifiers;
7014 int row,col;
7015 unsigned char buttons_mask;
7016 unsigned char gpm_modifiers;
7017 static unsigned char old_buttons = 0;
7018
7019 Gpm_GetEvent(&gpm_event);
7020
7021#ifdef FEAT_GUI
7022 /* Don't put events in the input queue now. */
7023 if (hold_gui_events)
7024 return 0;
7025#endif
7026
7027 row = gpm_event.y - 1;
7028 col = gpm_event.x - 1;
7029
7030 string[0] = ESC; /* Our termcode */
7031 string[1] = 'M';
7032 string[2] = 'G';
7033 switch (GPM_BARE_EVENTS(gpm_event.type))
7034 {
7035 case GPM_DRAG:
7036 string[3] = MOUSE_DRAG;
7037 break;
7038 case GPM_DOWN:
7039 buttons_mask = gpm_event.buttons & ~old_buttons;
7040 old_buttons = gpm_event.buttons;
7041 switch (buttons_mask)
7042 {
7043 case GPM_B_LEFT:
7044 button = MOUSE_LEFT;
7045 break;
7046 case GPM_B_MIDDLE:
7047 button = MOUSE_MIDDLE;
7048 break;
7049 case GPM_B_RIGHT:
7050 button = MOUSE_RIGHT;
7051 break;
7052 default:
7053 return 0;
7054 /*Don't know what to do. Can more than one button be
7055 * reported in one event? */
7056 }
7057 string[3] = (char_u)(button | 0x20);
7058 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7059 break;
7060 case GPM_UP:
7061 string[3] = MOUSE_RELEASE;
7062 old_buttons &= ~gpm_event.buttons;
7063 break;
7064 default:
7065 return 0;
7066 }
7067 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7068 gpm_modifiers = gpm_event.modifiers;
7069 vim_modifiers = 0x0;
7070 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7071 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7072 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7073 */
7074 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7075 vim_modifiers |= MOUSE_SHIFT;
7076
7077 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7078 vim_modifiers |= MOUSE_CTRL;
7079 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7080 vim_modifiers |= MOUSE_ALT;
7081 string[3] |= vim_modifiers;
7082 string[4] = (char_u)(col + ' ' + 1);
7083 string[5] = (char_u)(row + ' ' + 1);
7084 add_to_input_buf(string, 6);
7085 return 6;
7086}
7087#endif /* FEAT_MOUSE_GPM */
7088
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007089#ifdef FEAT_SYSMOUSE
7090/*
7091 * Initialize connection with sysmouse.
7092 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7093 * output, any sysmouse output than will be processed via sig_sysmouse().
7094 * Return OK if succeeded, FAIL if failed.
7095 */
7096 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007097sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007098{
7099 struct mouse_info mouse;
7100
7101 mouse.operation = MOUSE_MODE;
7102 mouse.u.mode.mode = 0;
7103 mouse.u.mode.signal = SIGUSR2;
7104 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7105 {
7106 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7107 mouse.operation = MOUSE_SHOW;
7108 ioctl(1, CONS_MOUSECTL, &mouse);
7109 return OK;
7110 }
7111 return FAIL;
7112}
7113
7114/*
7115 * Stop processing SIGUSR2 signals, and also make sure that
7116 * virtual console do not send us any sysmouse related signal.
7117 */
7118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007119sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007120{
7121 struct mouse_info mouse;
7122
7123 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7124 mouse.operation = MOUSE_MODE;
7125 mouse.u.mode.mode = 0;
7126 mouse.u.mode.signal = 0;
7127 ioctl(1, CONS_MOUSECTL, &mouse);
7128}
7129
7130/*
7131 * Gets info from sysmouse and adds special keys to input buf.
7132 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007133 static RETSIGTYPE
7134sig_sysmouse SIGDEFARG(sigarg)
7135{
7136 struct mouse_info mouse;
7137 struct video_info video;
7138 char_u string[6];
7139 int row, col;
7140 int button;
7141 int buttons;
7142 static int oldbuttons = 0;
7143
7144#ifdef FEAT_GUI
7145 /* Don't put events in the input queue now. */
7146 if (hold_gui_events)
7147 return;
7148#endif
7149
7150 mouse.operation = MOUSE_GETINFO;
7151 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7152 && ioctl(1, FBIO_MODEINFO, &video) != -1
7153 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7154 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7155 {
7156 row = mouse.u.data.y / video.vi_cheight;
7157 col = mouse.u.data.x / video.vi_cwidth;
7158 buttons = mouse.u.data.buttons;
7159 string[0] = ESC; /* Our termcode */
7160 string[1] = 'M';
7161 string[2] = 'S';
7162 if (oldbuttons == buttons && buttons != 0)
7163 {
7164 button = MOUSE_DRAG;
7165 }
7166 else
7167 {
7168 switch (buttons)
7169 {
7170 case 0:
7171 button = MOUSE_RELEASE;
7172 break;
7173 case 1:
7174 button = MOUSE_LEFT;
7175 break;
7176 case 2:
7177 button = MOUSE_MIDDLE;
7178 break;
7179 case 4:
7180 button = MOUSE_RIGHT;
7181 break;
7182 default:
7183 return;
7184 }
7185 oldbuttons = buttons;
7186 }
7187 string[3] = (char_u)(button);
7188 string[4] = (char_u)(col + ' ' + 1);
7189 string[5] = (char_u)(row + ' ' + 1);
7190 add_to_input_buf(string, 6);
7191 }
7192 return;
7193}
7194#endif /* FEAT_SYSMOUSE */
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007197typedef char_u * (*STRPROCSTR)(char_u *);
7198typedef char_u * (*INTPROCSTR)(int);
7199typedef int (*STRPROCINT)(char_u *);
7200typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202/*
7203 * Call a DLL routine which takes either a string or int param
7204 * and returns an allocated string.
7205 */
7206 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007207mch_libcall(
7208 char_u *libname,
7209 char_u *funcname,
7210 char_u *argstring, /* NULL when using a argint */
7211 int argint,
7212 char_u **string_result,/* NULL when using number_result */
7213 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215# if defined(USE_DLOPEN)
7216 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007217 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218# else
7219 shl_t hinstLib;
7220# endif
7221 STRPROCSTR ProcAdd;
7222 INTPROCSTR ProcAddI;
7223 char_u *retval_str = NULL;
7224 int retval_int = 0;
7225 int success = FALSE;
7226
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007227 /*
7228 * Get a handle to the DLL module.
7229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007231 /* First clear any error, it's not cleared by the dlopen() call. */
7232 (void)dlerror();
7233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 hinstLib = dlopen((char *)libname, RTLD_LAZY
7235# ifdef RTLD_LOCAL
7236 | RTLD_LOCAL
7237# endif
7238 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007239 if (hinstLib == NULL)
7240 {
7241 /* "dlerr" must be used before dlclose() */
7242 dlerr = (char *)dlerror();
7243 if (dlerr != NULL)
7244 EMSG2(_("dlerror = \"%s\""), dlerr);
7245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246# else
7247 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7248# endif
7249
7250 /* If the handle is valid, try to get the function address. */
7251 if (hinstLib != NULL)
7252 {
7253# ifdef HAVE_SETJMP_H
7254 /*
7255 * Catch a crash when calling the library function. For example when
7256 * using a number where a string pointer is expected.
7257 */
7258 mch_startjmp();
7259 if (SETJMP(lc_jump_env) != 0)
7260 {
7261 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007262# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007263 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 mch_didjmp();
7266 }
7267 else
7268# endif
7269 {
7270 retval_str = NULL;
7271 retval_int = 0;
7272
7273 if (argstring != NULL)
7274 {
7275# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007276 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007277 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278# else
7279 if (shl_findsym(&hinstLib, (const char *)funcname,
7280 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7281 ProcAdd = NULL;
7282# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007283 if ((success = (ProcAdd != NULL
7284# if defined(USE_DLOPEN)
7285 && dlerr == NULL
7286# endif
7287 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
7289 if (string_result == NULL)
7290 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7291 else
7292 retval_str = (ProcAdd)(argstring);
7293 }
7294 }
7295 else
7296 {
7297# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007298 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007299 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300# else
7301 if (shl_findsym(&hinstLib, (const char *)funcname,
7302 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7303 ProcAddI = NULL;
7304# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007305 if ((success = (ProcAddI != NULL
7306# if defined(USE_DLOPEN)
7307 && dlerr == NULL
7308# endif
7309 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 {
7311 if (string_result == NULL)
7312 retval_int = ((INTPROCINT)ProcAddI)(argint);
7313 else
7314 retval_str = (ProcAddI)(argint);
7315 }
7316 }
7317
7318 /* Save the string before we free the library. */
7319 /* Assume that a "1" or "-1" result is an illegal pointer. */
7320 if (string_result == NULL)
7321 *number_result = retval_int;
7322 else if (retval_str != NULL
7323 && retval_str != (char_u *)1
7324 && retval_str != (char_u *)-1)
7325 *string_result = vim_strsave(retval_str);
7326 }
7327
7328# ifdef HAVE_SETJMP_H
7329 mch_endjmp();
7330# ifdef SIGHASARG
7331 if (lc_signal != 0)
7332 {
7333 int i;
7334
7335 /* try to find the name of this signal */
7336 for (i = 0; signal_info[i].sig != -1; i++)
7337 if (lc_signal == signal_info[i].sig)
7338 break;
7339 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7340 }
7341# endif
7342# endif
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007345 /* "dlerr" must be used before dlclose() */
7346 if (dlerr != NULL)
7347 EMSG2(_("dlerror = \"%s\""), dlerr);
7348
7349 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 (void)dlclose(hinstLib);
7351# else
7352 (void)shl_unload(hinstLib);
7353# endif
7354 }
7355
7356 if (!success)
7357 {
7358 EMSG2(_(e_libcall), funcname);
7359 return FAIL;
7360 }
7361
7362 return OK;
7363}
7364#endif
7365
7366#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7367static int xterm_trace = -1; /* default: disabled */
7368static int xterm_button;
7369
7370/*
7371 * Setup a dummy window for X selections in a terminal.
7372 */
7373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007374setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 int z = 0;
7377 char *strp = "";
7378 Widget AppShell;
7379
7380 if (!x_connect_to_server())
7381 return;
7382
7383 open_app_context();
7384 if (app_context != NULL && xterm_Shell == (Widget)0)
7385 {
7386 int (*oldhandler)();
7387#if defined(HAVE_SETJMP_H)
7388 int (*oldIOhandler)();
7389#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007390# ifdef ELAPSED_FUNC
7391 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
7393 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007394 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395# endif
7396
7397 /* Ignore X errors while opening the display */
7398 oldhandler = XSetErrorHandler(x_error_check);
7399
7400#if defined(HAVE_SETJMP_H)
7401 /* Ignore X IO errors while opening the display */
7402 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7403 mch_startjmp();
7404 if (SETJMP(lc_jump_env) != 0)
7405 {
7406 mch_didjmp();
7407 xterm_dpy = NULL;
7408 }
7409 else
7410#endif
7411 {
7412 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7413 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7414#if defined(HAVE_SETJMP_H)
7415 mch_endjmp();
7416#endif
7417 }
7418
7419#if defined(HAVE_SETJMP_H)
7420 /* Now handle X IO errors normally. */
7421 (void)XSetIOErrorHandler(oldIOhandler);
7422#endif
7423 /* Now handle X errors normally. */
7424 (void)XSetErrorHandler(oldhandler);
7425
7426 if (xterm_dpy == NULL)
7427 {
7428 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007429 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return;
7431 }
7432
7433 /* Catch terminating error of the X server connection. */
7434 (void)XSetIOErrorHandler(x_IOerror_handler);
7435
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007436# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007438 {
7439 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007440 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007441 verbose_leave();
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443# endif
7444
7445 /* Create a Shell to make converters work. */
7446 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7447 applicationShellWidgetClass, xterm_dpy,
7448 NULL);
7449 if (AppShell == (Widget)0)
7450 return;
7451 xterm_Shell = XtVaCreatePopupShell("VIM",
7452 topLevelShellWidgetClass, AppShell,
7453 XtNmappedWhenManaged, 0,
7454 XtNwidth, 1,
7455 XtNheight, 1,
7456 NULL);
7457 if (xterm_Shell == (Widget)0)
7458 return;
7459
7460 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007461 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (x11_display == NULL)
7463 x11_display = xterm_dpy;
7464
7465 XtRealizeWidget(xterm_Shell);
7466 XSync(xterm_dpy, False);
7467 xterm_update();
7468 }
7469 if (xterm_Shell != (Widget)0)
7470 {
7471 clip_init(TRUE);
7472 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7473 x11_window = (Window)atol(strp);
7474 /* Check if $WINDOWID is valid. */
7475 if (test_x11_window(xterm_dpy) == FAIL)
7476 x11_window = 0;
7477 if (x11_window != 0)
7478 xterm_trace = 0;
7479 }
7480}
7481
7482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007483start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7486 return;
7487 xterm_trace = 1;
7488 xterm_button = button;
7489 do_xterm_trace();
7490}
7491
7492
7493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007494stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 if (xterm_trace < 0)
7497 return;
7498 xterm_trace = 0;
7499}
7500
7501/*
7502 * Query the xterm pointer and generate mouse termcodes if necessary
7503 * return TRUE if dragging is active, else FALSE
7504 */
7505 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007506do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508 Window root, child;
7509 int root_x, root_y;
7510 int win_x, win_y;
7511 int row, col;
7512 int_u mask_return;
7513 char_u buf[50];
7514 char_u *strp;
7515 long got_hints;
7516 static char_u *mouse_code;
7517 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7518 static int prev_row = 0, prev_col = 0;
7519 static XSizeHints xterm_hints;
7520
7521 if (xterm_trace <= 0)
7522 return FALSE;
7523
7524 if (xterm_trace == 1)
7525 {
7526 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007527 * have changed recently. */
7528 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7529 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 || xterm_hints.width_inc <= 1
7531 || xterm_hints.height_inc <= 1)
7532 {
7533 xterm_trace = -1; /* Not enough data -- disable tracing */
7534 return FALSE;
7535 }
7536
7537 /* Rely on the same mouse code for the duration of this */
7538 mouse_code = find_termcode(mouse_name);
7539 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007540 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 xterm_trace = 2;
7542
7543 /* Find the offset of the chars, there might be a scrollbar on the
7544 * left of the window and/or a menu on the top (eterm etc.) */
7545 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7546 &win_x, &win_y, &mask_return);
7547 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7548 - (xterm_hints.height_inc / 2);
7549 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7550 xterm_hints.y = 2;
7551 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7552 - (xterm_hints.width_inc / 2);
7553 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7554 xterm_hints.x = 2;
7555 return TRUE;
7556 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007557 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {
7559 xterm_trace = 0;
7560 return FALSE;
7561 }
7562
7563 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7564 &win_x, &win_y, &mask_return);
7565
7566 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7567 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7568 if (row == prev_row && col == prev_col)
7569 return TRUE;
7570
7571 STRCPY(buf, mouse_code);
7572 strp = buf + STRLEN(buf);
7573 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7574 *strp++ = (char_u)(col + ' ' + 1);
7575 *strp++ = (char_u)(row + ' ' + 1);
7576 *strp = 0;
7577 add_to_input_buf(buf, STRLEN(buf));
7578
7579 prev_row = row;
7580 prev_col = col;
7581 return TRUE;
7582}
7583
7584# if defined(FEAT_GUI) || defined(PROTO)
7585/*
7586 * Destroy the display, window and app_context. Required for GTK.
7587 */
7588 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007589clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
7591 if (xterm_Shell != (Widget)0)
7592 {
7593 XtDestroyWidget(xterm_Shell);
7594 xterm_Shell = (Widget)0;
7595 }
7596 if (xterm_dpy != NULL)
7597 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007598# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 /* Lesstif and Solaris crash here, lose some memory */
7600 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (x11_display == xterm_dpy)
7603 x11_display = NULL;
7604 xterm_dpy = NULL;
7605 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007606# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (app_context != (XtAppContext)NULL)
7608 {
7609 /* Lesstif and Solaris crash here, lose some memory */
7610 XtDestroyApplicationContext(app_context);
7611 app_context = (XtAppContext)NULL;
7612 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614}
7615# endif
7616
7617/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007618 * Catch up with GUI or X events.
7619 */
7620 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007621clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007622{
7623# ifdef FEAT_GUI
7624 if (gui.in_use)
7625 gui_mch_update();
7626 else
7627# endif
7628 if (xterm_Shell != (Widget)0)
7629 xterm_update();
7630}
7631
7632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 * Catch up with any queued X events. This may put keyboard input into the
7634 * input buffer, call resize call-backs, trigger timers etc. If there is
7635 * nothing in the X event queue (& no timers pending), then we return
7636 * immediately.
7637 */
7638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007639xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 XEvent event;
7642
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007643 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007645 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007647 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007648 break;
7649
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007650 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007651 {
7652 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007653 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007654#ifdef FEAT_CLIENTSERVER
7655 {
7656 XPropertyEvent *e = (XPropertyEvent *)&event;
7657
7658 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007660 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007663 XtDispatchEvent(&event);
7664 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007665 else
7666 {
7667 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007668 XtAppProcessEvent(app_context, mask);
7669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671}
7672
7673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007674clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 if (xterm_Shell != (Widget)0)
7677 return clip_x11_own_selection(xterm_Shell, cbd);
7678 return FAIL;
7679}
7680
7681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 if (xterm_Shell != (Widget)0)
7685 clip_x11_lose_selection(xterm_Shell, cbd);
7686}
7687
7688 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007689clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
7691 if (xterm_Shell != (Widget)0)
7692 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7693}
7694
7695 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007696clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
7698 clip_x11_set_selection(cbd);
7699}
7700#endif
7701
7702
7703#if defined(USE_XSMP) || defined(PROTO)
7704/*
7705 * Code for X Session Management Protocol.
7706 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007707static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7708static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7709static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7710static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7711static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
7713
7714# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007715static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717/*
7718 * This is our chance to ask the user if they want to save,
7719 * or abort the logout
7720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007722xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
7724 cmdmod_T save_cmdmod;
7725 int cancel_shutdown = False;
7726
7727 save_cmdmod = cmdmod;
7728 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007729 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 /* Mustn't logout */
7731 cancel_shutdown = True;
7732 cmdmod = save_cmdmod;
7733 setcursor(); /* position cursor */
7734 out_flush();
7735
7736 /* Done interaction */
7737 SmcInteractDone(smc_conn, cancel_shutdown);
7738
7739 /* Finish off
7740 * Only end save-yourself here if we're not cancelling shutdown;
7741 * we'll get a cancelled callback later in which we'll end it.
7742 * Hopefully get around glitchy SMs (like GNOME-1)
7743 */
7744 if (!cancel_shutdown)
7745 {
7746 xsmp.save_yourself = False;
7747 SmcSaveYourselfDone(smc_conn, True);
7748 }
7749}
7750# endif
7751
7752/*
7753 * Callback that starts save-yourself.
7754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007756xsmp_handle_save_yourself(
7757 SmcConn smc_conn,
7758 SmPointer client_data UNUSED,
7759 int save_type UNUSED,
7760 Bool shutdown,
7761 int interact_style UNUSED,
7762 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764 /* Handle already being in saveyourself */
7765 if (xsmp.save_yourself)
7766 SmcSaveYourselfDone(smc_conn, True);
7767 xsmp.save_yourself = True;
7768 xsmp.shutdown = shutdown;
7769
7770 /* First up, preserve all files */
7771 out_flush();
7772 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7773
7774 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007775 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7778 /* Now see if we can ask about unsaved files */
7779 if (shutdown && !fast && gui.in_use)
7780 /* Need to interact with user, but need SM's permission */
7781 SmcInteractRequest(smc_conn, SmDialogError,
7782 xsmp_handle_interaction, client_data);
7783 else
7784# endif
7785 {
7786 /* Can stop the cycle here */
7787 SmcSaveYourselfDone(smc_conn, True);
7788 xsmp.save_yourself = False;
7789 }
7790}
7791
7792
7793/*
7794 * Callback to warn us of imminent death.
7795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007797xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 xsmp_close();
7800
7801 /* quit quickly leaving swapfiles for modified buffers behind */
7802 getout_preserve_modified(0);
7803}
7804
7805
7806/*
7807 * Callback to tell us that save-yourself has completed.
7808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810xsmp_save_complete(
7811 SmcConn smc_conn UNUSED,
7812 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 xsmp.save_yourself = False;
7815}
7816
7817
7818/*
7819 * Callback to tell us that an instigated shutdown was cancelled
7820 * (maybe even by us)
7821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007823xsmp_shutdown_cancelled(
7824 SmcConn smc_conn,
7825 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 if (xsmp.save_yourself)
7828 SmcSaveYourselfDone(smc_conn, True);
7829 xsmp.save_yourself = False;
7830 xsmp.shutdown = False;
7831}
7832
7833
7834/*
7835 * Callback to tell us that a new ICE connection has been established.
7836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007838xsmp_ice_connection(
7839 IceConn iceConn,
7840 IcePointer clientData UNUSED,
7841 Bool opening,
7842 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
7844 /* Intercept creation of ICE connection fd */
7845 if (opening)
7846 {
7847 xsmp_icefd = IceConnectionNumber(iceConn);
7848 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7849 }
7850}
7851
7852
7853/* Handle any ICE processing that's required; return FAIL if SM lost */
7854 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007855xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 Bool rep;
7858
7859 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7860 == IceProcessMessagesIOError)
7861 {
7862 /* Lost ICE */
7863 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007864 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 xsmp_close();
7866 return FAIL;
7867 }
7868 else
7869 return OK;
7870}
7871
7872static int dummy;
7873
7874/* Set up X Session Management Protocol */
7875 void
7876xsmp_init(void)
7877{
7878 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 SmcCallbacks smcallbacks;
7880#if 0
7881 SmPropValue smname;
7882 SmProp smnameprop;
7883 SmProp *smprops[1];
7884#endif
7885
7886 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007887 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888
7889 xsmp.save_yourself = xsmp.shutdown = False;
7890
7891 /* Set up SM callbacks - must have all, even if they're not used */
7892 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7893 smcallbacks.save_yourself.client_data = NULL;
7894 smcallbacks.die.callback = xsmp_die;
7895 smcallbacks.die.client_data = NULL;
7896 smcallbacks.save_complete.callback = xsmp_save_complete;
7897 smcallbacks.save_complete.client_data = NULL;
7898 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7899 smcallbacks.shutdown_cancelled.client_data = NULL;
7900
7901 /* Set up a watch on ICE connection creations. The "dummy" argument is
7902 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7903 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7904 {
7905 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007906 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 return;
7908 }
7909
7910 /* Create an SM connection */
7911 xsmp.smcconn = SmcOpenConnection(
7912 NULL,
7913 NULL,
7914 SmProtoMajor,
7915 SmProtoMinor,
7916 SmcSaveYourselfProcMask | SmcDieProcMask
7917 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
7918 &smcallbacks,
7919 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00007920 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 sizeof(errorstring),
7922 errorstring);
7923 if (xsmp.smcconn == NULL)
7924 {
7925 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00007926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007928 {
7929 vim_snprintf(errorreport, sizeof(errorreport),
7930 _("XSMP SmcOpenConnection failed: %s"), errorstring);
7931 verb_msg((char_u *)errorreport);
7932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 return;
7934 }
7935 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
7936
7937#if 0
7938 /* ID ourselves */
7939 smname.value = "vim";
7940 smname.length = 3;
7941 smnameprop.name = "SmProgram";
7942 smnameprop.type = "SmARRAY8";
7943 smnameprop.num_vals = 1;
7944 smnameprop.vals = &smname;
7945
7946 smprops[0] = &smnameprop;
7947 SmcSetProperties(xsmp.smcconn, 1, smprops);
7948#endif
7949}
7950
7951
7952/* Shut down XSMP comms. */
7953 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007954xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 if (xsmp_icefd != -1)
7957 {
7958 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00007959 if (xsmp.clientid != NULL)
7960 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00007961 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 xsmp_icefd = -1;
7963 }
7964}
7965#endif /* USE_XSMP */
7966
7967
7968#ifdef EBCDIC
7969/* Translate character to its CTRL- value */
7970char CtrlTable[] =
7971{
7972/* 00 - 5E */
7973 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7974 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7975 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7976 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7977 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7978 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7979/* ^ */ 0x1E,
7980/* - */ 0x1F,
7981/* 61 - 6C */
7982 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7983/* _ */ 0x1F,
7984/* 6E - 80 */
7985 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7986/* a */ 0x01,
7987/* b */ 0x02,
7988/* c */ 0x03,
7989/* d */ 0x37,
7990/* e */ 0x2D,
7991/* f */ 0x2E,
7992/* g */ 0x2F,
7993/* h */ 0x16,
7994/* i */ 0x05,
7995/* 8A - 90 */
7996 0, 0, 0, 0, 0, 0, 0,
7997/* j */ 0x15,
7998/* k */ 0x0B,
7999/* l */ 0x0C,
8000/* m */ 0x0D,
8001/* n */ 0x0E,
8002/* o */ 0x0F,
8003/* p */ 0x10,
8004/* q */ 0x11,
8005/* r */ 0x12,
8006/* 9A - A1 */
8007 0, 0, 0, 0, 0, 0, 0, 0,
8008/* s */ 0x13,
8009/* t */ 0x3C,
8010/* u */ 0x3D,
8011/* v */ 0x32,
8012/* w */ 0x26,
8013/* x */ 0x18,
8014/* y */ 0x19,
8015/* z */ 0x3F,
8016/* AA - AC */
8017 0, 0, 0,
8018/* [ */ 0x27,
8019/* AE - BC */
8020 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8021/* ] */ 0x1D,
8022/* BE - C0 */ 0, 0, 0,
8023/* A */ 0x01,
8024/* B */ 0x02,
8025/* C */ 0x03,
8026/* D */ 0x37,
8027/* E */ 0x2D,
8028/* F */ 0x2E,
8029/* G */ 0x2F,
8030/* H */ 0x16,
8031/* I */ 0x05,
8032/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8033/* J */ 0x15,
8034/* K */ 0x0B,
8035/* L */ 0x0C,
8036/* M */ 0x0D,
8037/* N */ 0x0E,
8038/* O */ 0x0F,
8039/* P */ 0x10,
8040/* Q */ 0x11,
8041/* R */ 0x12,
8042/* DA - DF */ 0, 0, 0, 0, 0, 0,
8043/* \ */ 0x1C,
8044/* E1 */ 0,
8045/* S */ 0x13,
8046/* T */ 0x3C,
8047/* U */ 0x3D,
8048/* V */ 0x32,
8049/* W */ 0x26,
8050/* X */ 0x18,
8051/* Y */ 0x19,
8052/* Z */ 0x3F,
8053/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8054 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8055};
8056
8057char MetaCharTable[]=
8058{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8059 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8060 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8061 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8062 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8063};
8064
8065
8066/* TODO: Use characters NOT numbers!!! */
8067char CtrlCharTable[]=
8068{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8069 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8070 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8071 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8072 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8073};
8074
8075
8076#endif