blob: 148d8a499c49f518ec8cc65db3737b33c71698d1 [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
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200444 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200445 {
446 /* no character available within 'updatetime' */
447 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200448 if (trigger_cursorhold() && maxlen >= 3
449 && !typebuf_changed(tb_change_cnt))
450 {
451 buf[0] = K_SPECIAL;
452 buf[1] = KS_EXTRA;
453 buf[2] = (int)KE_CURSORHOLD;
454 return 3;
455 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200456 /*
457 * If there is no character available within 'updatetime'
458 * seconds flush all the swap files to disk.
459 * Also done when interrupted by SIGWINCH.
460 */
461 before_blocking();
462 continue;
463 }
464 }
465 }
466
467#ifdef FEAT_JOB_CHANNEL
468 /* Checking if a job ended requires polling. Do this every 100 msec. */
469 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
470 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100471 /* If there is readahead then parse_queued_messages() timed out and we
472 * should call it again soon. */
473 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
474 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200475#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100476#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100477 if (p_beval && wait_time > 100L)
478 /* The 'balloonexpr' may indirectly invoke a callback while waiting
479 * for a character, need to check often. */
480 wait_time = 100L;
481#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200482
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200484 * We want to be interrupted by the winch signal
485 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200487 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200488 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200489 /* If input was put directly in typeahead buffer bail out here. */
490 if (typebuf_changed(tb_change_cnt))
491 return 0;
492
493 /*
494 * For some terminals we only get one character at a time.
495 * We want the get all available characters, so we could keep on
496 * trying until none is available
497 * For some other terminals this is quite slow, that's why we don't
498 * do it.
499 */
500 len = read_from_input_buf(buf, (long)maxlen);
501 if (len > 0)
502 return len;
503 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200506 /* no character available */
507#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
508 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100509 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200512 if (do_resize /* interrupted by SIGWINCH signal */
513#ifdef FEAT_CLIENTSERVER
514 || server_waiting()
515#endif
516#ifdef MESSAGE_QUEUE
517 || interrupted
518#endif
519 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100520 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200521 continue;
522
523 /* no character available or interrupted */
524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200526 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100530handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 do_resize = FALSE;
533 shell_resized();
534}
535
536/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200537 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100540mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200542 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543}
544
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200545#if defined(FEAT_TERMINAL) || defined(PROTO)
546/*
547 * Check for any pending input or messages.
548 */
549 int
550mch_check_messages(void)
551{
552 return WaitForChar(0L, NULL, TRUE);
553}
554#endif
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
557# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000558# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559# endif
560# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
561# include <sys/sysctl.h>
562# endif
563# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
564# include <sys/sysinfo.h>
565# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100566# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100567# include <mach/mach_host.h>
568# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000572 * Return total amount of memory available in Kbyte.
573 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100576mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000579 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaar362dc332018-03-05 21:59:37 +0100581# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100582 {
583 /* Mac (Darwin) way of getting the amount of RAM available */
584 mach_port_t host = mach_host_self();
585 kern_return_t kret;
586# ifdef HOST_VM_INFO64
587 struct vm_statistics64 vm_stat;
588 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
Bram Moolenaar988615f2018-02-27 17:58:20 +0100590 kret = host_statistics64(host, HOST_VM_INFO64,
591 (host_info64_t)&vm_stat, &count);
592# else
593 struct vm_statistics vm_stat;
594 natural_t count = HOST_VM_INFO_COUNT;
595
596 kret = host_statistics(host, HOST_VM_INFO,
597 (host_info_t)&vm_stat, &count);
598# endif
599 if (kret == KERN_SUCCESS)
600 /* get the amount of user memory by summing each usage */
601 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
602 + vm_stat.inactive_count
603# ifdef MAC_OS_X_VERSION_10_9
604 + vm_stat.compressor_page_count
605# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100606 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100607 mach_port_deallocate(mach_task_self(), host);
608 }
609# endif
610
611# ifdef HAVE_SYSCTL
612 if (mem == 0)
613 {
614 /* BSD way of getting the amount of RAM available. */
615 int mib[2];
616 size_t len = sizeof(long_u);
617# ifdef HW_USERMEM64
618 long_u physmem;
619# else
620 /* sysctl() may return 32 bit or 64 bit, accept both */
621 union {
622 int_u u32;
623 long_u u64;
624 } physmem;
625# endif
626
627 mib[0] = CTL_HW;
628# ifdef HW_USERMEM64
629 mib[1] = HW_USERMEM64;
630# else
631 mib[1] = HW_USERMEM;
632# endif
633 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
634 {
635# ifdef HW_USERMEM64
636 mem = (long_u)physmem;
637# else
638 if (len == sizeof(physmem.u64))
639 mem = (long_u)physmem.u64;
640 else
641 mem = (long_u)physmem.u32;
642# endif
643 }
644 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200647# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (mem == 0)
649 {
650 struct sysinfo sinfo;
651
652 /* Linux way of getting amount of RAM available */
653 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000654 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200655# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000656 /* avoid overflow as much as possible */
657 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
658 {
659 sinfo.mem_unit = sinfo.mem_unit >> 1;
660 --shiftright;
661 }
662 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200663# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200665# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200670# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (mem == 0)
672 {
673 long pagesize, pagecount;
674
675 /* Solaris way of getting amount of RAM available */
676 pagesize = sysconf(_SC_PAGESIZE);
677 pagecount = sysconf(_SC_PHYS_PAGES);
678 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000679 {
680 /* avoid overflow as much as possible */
681 while (shiftright > 0 && (pagesize & 1) == 0)
682 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000683 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000684 --shiftright;
685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 /* Return the minimum of the physical memory and the user limit, because
692 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200693# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 struct rlimit rlp;
696
697 if (getrlimit(RLIMIT_DATA, &rlp) == 0
698 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200699# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200701# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000702 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000704 {
705 mem = (long_u)rlp.rlim_cur;
706 shiftright = 10;
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
711 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000712 return mem >> shiftright;
713 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714}
715#endif
716
717 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100718mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000721#ifdef FEAT_MZSCHEME
722 long total = msec; /* remember original value */
723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725 if (ignoreinput)
726 {
727 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000728 * here. But we don't want QUIT to kill us (CTRL-\ used in a
729 * shell may produce SIGQUIT). */
730 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 old_tmode = curr_tmode;
732 if (curr_tmode == TMODE_RAW)
733 settmode(TMODE_SLEEP);
734
735 /*
736 * Everybody sleeps in a different way...
737 * Prefer nanosleep(), some versions of usleep() can only sleep up to
738 * one second.
739 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000740#ifdef FEAT_MZSCHEME
741 do
742 {
743 /* if total is large enough, wait by portions in p_mzq */
744 if (total > p_mzq)
745 msec = p_mzq;
746 else
747 msec = total;
748 total -= msec;
749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef HAVE_NANOSLEEP
751 {
752 struct timespec ts;
753
754 ts.tv_sec = msec / 1000;
755 ts.tv_nsec = (msec % 1000) * 1000000;
756 (void)nanosleep(&ts, NULL);
757 }
758#else
759# ifdef HAVE_USLEEP
760 while (msec >= 1000)
761 {
762 usleep((unsigned int)(999 * 1000));
763 msec -= 999;
764 }
765 usleep((unsigned int)(msec * 1000));
766# else
767# ifndef HAVE_SELECT
768 poll(NULL, 0, (int)msec);
769# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
771 struct timeval tv;
772
773 tv.tv_sec = msec / 1000;
774 tv.tv_usec = (msec % 1000) * 1000;
775 /*
776 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
777 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
778 */
779 select(0, NULL, NULL, NULL, &tv);
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781# endif /* HAVE_SELECT */
782# endif /* HAVE_NANOSLEEP */
783#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000784#ifdef FEAT_MZSCHEME
785 }
786 while (total > 0);
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000790 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200793 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
795
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000796#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
798# define HAVE_CHECK_STACK_GROWTH
799/*
800 * Support for checking for an almost-out-of-stack-space situation.
801 */
802
803/*
804 * Return a pointer to an item on the stack. Used to find out if the stack
805 * grows up or down.
806 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100807static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808static int stack_grows_downwards;
809
810/*
811 * Find out if the stack grows upwards or downwards.
812 * "p" points to a variable on the stack of the caller.
813 */
814 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100815check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 int i;
818
819 stack_grows_downwards = (p > (char *)&i);
820}
821#endif
822
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000823#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824static char *stack_limit = NULL;
825
826#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
827# include <pthread.h>
828# include <pthread_np.h>
829#endif
830
831/*
832 * Find out until how var the stack can grow without getting into trouble.
833 * Called when starting up and when switching to the signal stack in
834 * deathtrap().
835 */
836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100837get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 struct rlimit rlp;
840 int i;
841 long lim;
842
843 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
844 * limit doesn't fit in a long (rlim_cur might be "long long"). */
845 if (getrlimit(RLIMIT_STACK, &rlp) == 0
846 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
847# ifdef RLIM_INFINITY
848 && rlp.rlim_cur != RLIM_INFINITY
849# endif
850 )
851 {
852 lim = (long)rlp.rlim_cur;
853#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
854 {
855 pthread_attr_t attr;
856 size_t size;
857
858 /* On FreeBSD the initial thread always has a fixed stack size, no
859 * matter what the limits are set to. Normally it's 1 Mbyte. */
860 pthread_attr_init(&attr);
861 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
862 {
863 pthread_attr_getstacksize(&attr, &size);
864 if (lim > (long)size)
865 lim = (long)size;
866 }
867 pthread_attr_destroy(&attr);
868 }
869#endif
870 if (stack_grows_downwards)
871 {
872 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
873 if (stack_limit >= (char *)&i)
874 /* overflow, set to 1/16 of current stack position */
875 stack_limit = (char *)((long)&i / 16L);
876 }
877 else
878 {
879 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
880 if (stack_limit <= (char *)&i)
881 stack_limit = NULL; /* overflow */
882 }
883 }
884}
885
886/*
887 * Return FAIL when running out of stack space.
888 * "p" must point to any variable local to the caller that's on the stack.
889 */
890 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100891mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 if (stack_limit != NULL)
894 {
895 if (stack_grows_downwards)
896 {
897 if (p < stack_limit)
898 return FAIL;
899 }
900 else if (p > stack_limit)
901 return FAIL;
902 }
903 return OK;
904}
905#endif
906
907#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
908/*
909 * Support for using the signal stack.
910 * This helps when we run out of stack space, which causes a SIGSEGV. The
911 * signal handler then must run on another stack, since the normal stack is
912 * completely full.
913 */
914
915#ifndef SIGSTKSZ
916# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
917#endif
918
919# ifdef HAVE_SIGALTSTACK
920static stack_t sigstk; /* for sigaltstack() */
921# else
922static struct sigstack sigstk; /* for sigstack() */
923# endif
924
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100925static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926static char *signal_stack;
927
928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 if (signal_stack != NULL)
932 {
933# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934# ifdef HAVE_SS_BASE
935 sigstk.ss_base = signal_stack;
936# else
937 sigstk.ss_sp = signal_stack;
938# endif
939 sigstk.ss_size = SIGSTKSZ;
940 sigstk.ss_flags = 0;
941 (void)sigaltstack(&sigstk, NULL);
942# else
943 sigstk.ss_sp = signal_stack;
944 if (stack_grows_downwards)
945 sigstk.ss_sp += SIGSTKSZ - 1;
946 sigstk.ss_onstack = 0;
947 (void)sigstack(&sigstk, NULL);
948# endif
949 }
950}
951#endif
952
953/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000954 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * will barf when the second argument to signal() is ``wrong''.
956 * Let me try it with a few tricky defines from my own osdef.h (jw).
957 */
958#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 static RETSIGTYPE
960sig_winch SIGDEFARG(sigarg)
961{
962 /* this is not required on all systems, but it doesn't hurt anybody */
963 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
964 do_resize = TRUE;
965 SIGRETURN;
966}
967#endif
968
969#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 static RETSIGTYPE
971catch_sigint SIGDEFARG(sigarg)
972{
973 /* this is not required on all systems, but it doesn't hurt anybody */
974 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
975 got_int = TRUE;
976 SIGRETURN;
977}
978#endif
979
980#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 static RETSIGTYPE
982catch_sigpwr SIGDEFARG(sigarg)
983{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000984 /* this is not required on all systems, but it doesn't hurt anybody */
985 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 /*
987 * I'm not sure we get the SIGPWR signal when the system is really going
988 * down or when the batteries are almost empty. Just preserve the swap
989 * files and don't exit, that can't do any harm.
990 */
991 ml_sync_all(FALSE, FALSE);
992 SIGRETURN;
993}
994#endif
995
996#ifdef SET_SIG_ALARM
997/*
998 * signal function for alarm().
999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 static RETSIGTYPE
1001sig_alarm SIGDEFARG(sigarg)
1002{
1003 /* doesn't do anything, just to break a system call */
1004 sig_alarm_called = TRUE;
1005 SIGRETURN;
1006}
1007#endif
1008
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001009#if (defined(HAVE_SETJMP_H) \
1010 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1011 || defined(FEAT_LIBCALL))) \
1012 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013/*
1014 * A simplistic version of setjmp() that only allows one level of using.
1015 * Don't call twice before calling mch_endjmp()!.
1016 * Usage:
1017 * mch_startjmp();
1018 * if (SETJMP(lc_jump_env) != 0)
1019 * {
1020 * mch_didjmp();
1021 * EMSG("crash!");
1022 * }
1023 * else
1024 * {
1025 * do_the_work;
1026 * mch_endjmp();
1027 * }
1028 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1029 * not return before the saved environment is used.
1030 * Returns OK for normal return, FAIL when the protected code caused a
1031 * problem and LONGJMP() was used.
1032 */
1033 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001034mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036#ifdef SIGHASARG
1037 lc_signal = 0;
1038#endif
1039 lc_active = TRUE;
1040}
1041
1042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001043mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
1045 lc_active = FALSE;
1046}
1047
1048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1052 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1053 * otherwise catching the signal only works once. */
1054 init_signal_stack();
1055# endif
1056}
1057#endif
1058
1059/*
1060 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001061 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001063 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1064 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
1066 static RETSIGTYPE
1067deathtrap SIGDEFARG(sigarg)
1068{
1069 static int entered = 0; /* count the number of times we got here.
1070 Note: when memory has been corrupted
1071 this may get an arbitrary value! */
1072#ifdef SIGHASARG
1073 int i;
1074#endif
1075
1076#if defined(HAVE_SETJMP_H)
1077 /*
1078 * Catch a crash in protected code.
1079 * Restores the environment saved in lc_jump_env, which looks like
1080 * SETJMP() returns 1.
1081 */
1082 if (lc_active)
1083 {
1084# if defined(SIGHASARG)
1085 lc_signal = sigarg;
1086# endif
1087 lc_active = FALSE; /* don't jump again */
1088 LONGJMP(lc_jump_env, 1);
1089 /* NOTREACHED */
1090 }
1091#endif
1092
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001093#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001094# ifdef SIGQUIT
1095 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1096 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1097 * pressing CTRL-\, but we don't want Vim to exit then. */
1098 if (in_mch_delay && sigarg == SIGQUIT)
1099 SIGRETURN;
1100# endif
1101
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001102 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1103 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1104 * free(). Calling free() again may then cause a crash. */
1105 if (entered == 0
1106 && (0
1107# ifdef SIGHUP
1108 || sigarg == SIGHUP
1109# endif
1110# ifdef SIGQUIT
1111 || sigarg == SIGQUIT
1112# endif
1113# ifdef SIGTERM
1114 || sigarg == SIGTERM
1115# endif
1116# ifdef SIGPWR
1117 || sigarg == SIGPWR
1118# endif
1119# ifdef SIGUSR1
1120 || sigarg == SIGUSR1
1121# endif
1122# ifdef SIGUSR2
1123 || sigarg == SIGUSR2
1124# endif
1125 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001126 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001127 SIGRETURN;
1128#endif
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /* Remember how often we have been called. */
1131 ++entered;
1132
Bram Moolenaare429e702016-06-10 19:49:14 +02001133 /* Executing autocommands is likely to use more stack space than we have
1134 * available in the signal stack. */
1135 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_EVAL
1138 /* Set the v:dying variable. */
1139 set_vim_var_nr(VV_DYING, (long)entered);
1140#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001141 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001143#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /* Since we are now using the signal stack, need to reset the stack
1145 * limit. Otherwise using a regexp will fail. */
1146 get_stack_limit();
1147#endif
1148
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001149#if 0
1150 /* This is for opening gdb the moment Vim crashes.
1151 * You need to manually adjust the file name and Vim executable name.
1152 * Suggested by SungHyun Nam. */
1153 {
1154# define VI_GDB_FILE "/tmp/vimgdb"
1155# define VIM_NAME "/usr/bin/vim"
1156 FILE *fp = fopen(VI_GDB_FILE, "w");
1157 if (fp)
1158 {
1159 fprintf(fp,
1160 "file %s\n"
1161 "attach %d\n"
1162 "set height 1000\n"
1163 "bt full\n"
1164 , VIM_NAME, getpid());
1165 fclose(fp);
1166 system("xterm -e gdb -x "VI_GDB_FILE);
1167 unlink(VI_GDB_FILE);
1168 }
1169 }
1170#endif
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#ifdef SIGHASARG
1173 /* try to find the name of this signal */
1174 for (i = 0; signal_info[i].sig != -1; i++)
1175 if (sigarg == signal_info[i].sig)
1176 break;
1177 deadly_signal = sigarg;
1178#endif
1179
1180 full_screen = FALSE; /* don't write message to the GUI, it might be
1181 * part of the problem... */
1182 /*
1183 * If something goes wrong after entering here, we may get here again.
1184 * When this happens, give a message and try to exit nicely (resetting the
1185 * terminal mode, etc.)
1186 * When this happens twice, just exit, don't even try to give a message,
1187 * stack may be corrupt or something weird.
1188 * When this still happens again (or memory was corrupted in such a way
1189 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1190 */
1191 if (entered >= 3)
1192 {
1193 reset_signals(); /* don't catch any signals anymore */
1194 may_core_dump();
1195 if (entered >= 4)
1196 _exit(8);
1197 exit(7);
1198 }
1199 if (entered == 2)
1200 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001201 /* No translation, it may call malloc(). */
1202 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 out_flush();
1204 getout(1);
1205 }
1206
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001207 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001209 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 signal_info[i].name);
1211#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001212 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001214
1215 /* Preserve files and exit. This sets the really_exiting flag to prevent
1216 * calling free(). */
1217 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaare429e702016-06-10 19:49:14 +02001219 /* NOTREACHED */
1220
Bram Moolenaar009b2592004-10-24 19:18:58 +00001221#ifdef NBDEBUG
1222 reset_signals();
1223 may_core_dump();
1224 abort();
1225#endif
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 SIGRETURN;
1228}
1229
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001230 static void
1231after_sigcont(void)
1232{
1233# ifdef FEAT_TITLE
1234 // Set oldtitle to NULL, so the current title is obtained again.
1235 VIM_CLEAR(oldtitle);
1236# endif
1237 settmode(TMODE_RAW);
1238 need_check_timestamps = TRUE;
1239 did_check_timestamps = FALSE;
1240}
1241
1242#if defined(SIGCONT)
1243static RETSIGTYPE sigcont_handler SIGPROTOARG;
1244static int in_mch_suspend = FALSE;
1245
1246# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247/*
1248 * On Solaris with multi-threading, suspending might not work immediately.
1249 * Catch the SIGCONT signal, which will be used as an indication whether the
1250 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001251 *
1252 * On Linux, signal is not always handled immediately either.
1253 * See https://bugs.launchpad.net/bugs/291373
1254 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001255 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001257static volatile int sigcont_received;
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
1260/*
1261 * signal handler for SIGCONT
1262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 static RETSIGTYPE
1264sigcont_handler SIGDEFARG(sigarg)
1265{
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001266 if (in_mch_suspend)
1267 {
1268# if defined(_REENTRANT) && defined(SIGCONT)
1269 sigcont_received = TRUE;
1270# endif
1271 }
1272 else
1273 {
1274 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1275 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1276 // back to a sane mode and redraw.
1277 after_sigcont();
1278
1279 update_screen(CLEAR);
1280 if (State & CMDLINE)
1281 redrawcmdline();
1282 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
1283 || State == EXTERNCMD || State == CONFIRM || exmode_active)
1284 repeat_message();
1285 else if (redrawing())
1286 setcursor();
1287#if defined(FEAT_INS_EXPAND)
1288 if (pum_visible())
1289 {
1290 redraw_later(NOT_VALID);
1291 ins_compl_show_pum();
1292 }
1293#endif
1294 cursor_on_force();
1295 out_flush();
1296 }
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 SIGRETURN;
1299}
1300#endif
1301
Bram Moolenaar62b42182010-09-21 22:09:37 +02001302# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001303static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001304# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001305static void save_clipboard(void);
1306static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001307
1308static void *clip_star_save = NULL;
1309static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001310# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001311
1312/*
1313 * Called when Vim is going to sleep or execute a shell command.
1314 * We can't respond to requests for the X selections. Lose them, otherwise
1315 * other applications will hang. But first copy the text to cut buffer 0.
1316 */
1317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001318loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001319{
1320 if (clip_star.owned || clip_plus.owned)
1321 {
1322 x11_export_final_selection();
1323 if (clip_star.owned)
1324 clip_lose_selection(&clip_star);
1325 if (clip_plus.owned)
1326 clip_lose_selection(&clip_plus);
1327 if (x11_display != NULL)
1328 XFlush(x11_display);
1329 }
1330}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001331
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001332# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001333/*
1334 * Save clipboard text to restore later.
1335 */
1336 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001337save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001338{
1339 if (clip_star.owned)
1340 clip_star_save = get_register('*', TRUE);
1341 if (clip_plus.owned)
1342 clip_plus_save = get_register('+', TRUE);
1343}
1344
1345/*
1346 * Restore clipboard text if no one own the X selection.
1347 */
1348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001349restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001350{
1351 if (clip_star_save != NULL)
1352 {
1353 if (!clip_gen_owner_exists(&clip_star))
1354 put_register('*', clip_star_save);
1355 else
1356 free_register(clip_star_save);
1357 clip_star_save = NULL;
1358 }
1359 if (clip_plus_save != NULL)
1360 {
1361 if (!clip_gen_owner_exists(&clip_plus))
1362 put_register('+', clip_plus_save);
1363 else
1364 free_register(clip_plus_save);
1365 clip_plus_save = NULL;
1366 }
1367}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001368# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * If the machine has job control, use it to suspend the program,
1373 * otherwise fake it by starting a new shell.
1374 */
1375 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001376mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 /* BeOS does have SIGTSTP, but it doesn't work. */
1379#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001380 in_mch_suspend = TRUE;
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 out_flush(); /* needed to make cursor visible on some systems */
1383 settmode(TMODE_COOK);
1384 out_flush(); /* needed to disable mouse on some systems */
1385
1386# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001387 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388# endif
1389
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001390# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 sigcont_received = FALSE;
1392# endif
1393 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001394# if defined(_REENTRANT) && defined(SIGCONT)
1395 /*
1396 * Wait for the SIGCONT signal to be handled. It generally happens
1397 * immediately, but somehow not all the time. Do not call pause()
1398 * because there would be race condition which would hang Vim if
1399 * signal happened in between the test of sigcont_received and the
1400 * call to pause(). If signal is not yet received, call sleep(0)
1401 * to just yield CPU. Signal should then be received. If somehow
1402 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1403 * further if signal is not received after 1+2+3+4 ms (not expected
1404 * to happen).
1405 */
1406 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001407 long wait_time;
1408 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001409 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001410 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412# endif
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001413 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001415 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#else
1417 suspend_shell();
1418#endif
1419}
1420
1421 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001422mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 Columns = 80;
1425 Rows = 24;
1426
1427 out_flush();
1428 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001429
Bram Moolenaar56718732006-03-15 22:53:57 +00001430#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001431 mac_conv_init();
1432#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001433#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1434 win_clip_init();
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436}
1437
1438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001439set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441#if defined(SIGWINCH)
1442 /*
1443 * WINDOW CHANGE signal is handled with sig_winch().
1444 */
1445 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1446#endif
1447
1448 /*
1449 * We want the STOP signal to work, to make mch_suspend() work.
1450 * For "rvim" the STOP signal is ignored.
1451 */
1452#ifdef SIGTSTP
1453 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1454#endif
Bram Moolenaar917e32b2018-08-07 17:38:41 +02001455#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 signal(SIGCONT, sigcont_handler);
1457#endif
1458
1459 /*
1460 * We want to ignore breaking of PIPEs.
1461 */
1462#ifdef SIGPIPE
1463 signal(SIGPIPE, SIG_IGN);
1464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001467 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#endif
1469
1470 /*
1471 * Ignore alarm signals (Perl's alarm() generates it).
1472 */
1473#ifdef SIGALRM
1474 signal(SIGALRM, SIG_IGN);
1475#endif
1476
1477 /*
1478 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1479 * work will be lost.
1480 */
1481#ifdef SIGPWR
1482 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1483#endif
1484
1485 /*
1486 * Arrange for other signals to gracefully shutdown Vim.
1487 */
1488 catch_signals(deathtrap, SIG_ERR);
1489
1490#if defined(FEAT_GUI) && defined(SIGHUP)
1491 /*
1492 * When the GUI is running, ignore the hangup signal.
1493 */
1494 if (gui.in_use)
1495 signal(SIGHUP, SIG_IGN);
1496#endif
1497}
1498
Bram Moolenaardf177f62005-02-22 08:39:57 +00001499#if defined(SIGINT) || defined(PROTO)
1500/*
1501 * Catch CTRL-C (only works while in Cooked mode).
1502 */
1503 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001504catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001505{
1506 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1507}
1508#endif
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001511reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001514#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 /* SIGCONT isn't in the list, because its default action is ignore */
1516 signal(SIGCONT, SIG_DFL);
1517#endif
1518}
1519
1520 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001521catch_signals(
1522 RETSIGTYPE (*func_deadly)(),
1523 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 int i;
1526
1527 for (i = 0; signal_info[i].sig != -1; i++)
1528 if (signal_info[i].deadly)
1529 {
1530#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1531 struct sigaction sa;
1532
1533 /* Setup to use the alternate stack for the signal function. */
1534 sa.sa_handler = func_deadly;
1535 sigemptyset(&sa.sa_mask);
1536# if defined(__linux__) && defined(_REENTRANT)
1537 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1538 * thread handling in combination with using the alternate stack:
1539 * pthread library functions try to use the stack pointer to
1540 * identify the current thread, causing a SEGV signal, which
1541 * recursively calls deathtrap() and hangs. */
1542 sa.sa_flags = 0;
1543# else
1544 sa.sa_flags = SA_ONSTACK;
1545# endif
1546 sigaction(signal_info[i].sig, &sa, NULL);
1547#else
1548# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1549 struct sigvec sv;
1550
1551 /* Setup to use the alternate stack for the signal function. */
1552 sv.sv_handler = func_deadly;
1553 sv.sv_mask = 0;
1554 sv.sv_flags = SV_ONSTACK;
1555 sigvec(signal_info[i].sig, &sv, NULL);
1556# else
1557 signal(signal_info[i].sig, func_deadly);
1558# endif
1559#endif
1560 }
1561 else if (func_other != SIG_ERR)
1562 signal(signal_info[i].sig, func_other);
1563}
1564
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001565#ifdef HAVE_SIGPROCMASK
1566 static void
1567block_signals(sigset_t *set)
1568{
1569 sigset_t newset;
1570 int i;
1571
1572 sigemptyset(&newset);
1573
1574 for (i = 0; signal_info[i].sig != -1; i++)
1575 sigaddset(&newset, signal_info[i].sig);
1576
1577# if defined(_REENTRANT) && defined(SIGCONT)
1578 /* SIGCONT isn't in the list, because its default action is ignore */
1579 sigaddset(&newset, SIGCONT);
1580# endif
1581
1582 sigprocmask(SIG_BLOCK, &newset, set);
1583}
1584
1585 static void
1586unblock_signals(sigset_t *set)
1587{
1588 sigprocmask(SIG_SETMASK, set, NULL);
1589}
1590#endif
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001593 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001594 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1595 * return TRUE
1596 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1597 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001598 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001599 * Returns TRUE when Vim should exit.
1600 */
1601 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001602vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001603{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001604 static int got_signal = 0;
1605 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001606
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001607 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001608 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001609 case SIGNAL_BLOCK: blocked = TRUE;
1610 break;
1611
1612 case SIGNAL_UNBLOCK: blocked = FALSE;
1613 if (got_signal != 0)
1614 {
1615 kill(getpid(), got_signal);
1616 got_signal = 0;
1617 }
1618 break;
1619
1620 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001621 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001622 got_signal = sig;
1623#ifdef SIGPWR
1624 if (sig != SIGPWR)
1625#endif
1626 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001627 break;
1628 }
1629 return FALSE;
1630}
1631
1632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 * Check_win checks whether we have an interactive stdout.
1634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001636mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (isatty(1))
1639 return OK;
1640 return FAIL;
1641}
1642
1643/*
1644 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1645 */
1646 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001647mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 if (isatty(read_cmd_fd))
1650 return TRUE;
1651 return FALSE;
1652}
1653
1654#ifdef FEAT_X11
1655
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001656# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659/*
1660 * Give a message about the elapsed time for opening the X window.
1661 */
1662 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001663xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001665 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666}
1667# endif
1668#endif
1669
1670#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1671/*
1672 * A few functions shared by X11 title and clipboard code.
1673 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001674static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1675static int x_error_check(Display *dpy, XErrorEvent *error_event);
1676static int x_connect_to_server(void);
1677static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679static int got_x_error = FALSE;
1680
1681/*
1682 * X Error handler, otherwise X just exits! (very rude) -- webb
1683 */
1684 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001685x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001687 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 STRCAT(IObuff, _("\nVim: Got X error\n"));
1689
1690 /* We cannot print a message and continue, because no X calls are allowed
1691 * here (causes my system to hang). Silently continuing might be an
1692 * alternative... */
1693 preserve_exit(); /* preserve files and exit */
1694
1695 return 0; /* NOTREACHED */
1696}
1697
1698/*
1699 * Another X Error handler, just used to check for errors.
1700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001702x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 got_x_error = TRUE;
1705 return 0;
1706}
1707
1708#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1709# if defined(HAVE_SETJMP_H)
1710/*
1711 * An X IO Error handler, used to catch error while opening the display.
1712 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001713static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001716x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 /* This function should not return, it causes exit(). Longjump instead. */
1719 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001720# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001721 return 0; /* avoid the compiler complains about missing return value */
1722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724# endif
1725
1726/*
1727 * An X IO Error handler, used to catch terminal errors.
1728 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001729static int x_IOerror_handler(Display *dpy);
1730static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001731static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001734x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
1736 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001737 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 x11_window = 0;
1739 x11_display = NULL;
1740 xterm_Shell = (Widget)0;
1741
1742 /* This function should not return, it causes exit(). Longjump instead. */
1743 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001744# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001745 return 0; /* avoid the compiler complains about missing return value */
1746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001748
1749/*
1750 * If the X11 connection was lost try to restore it.
1751 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001752 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001753 */
1754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001755may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001756{
1757 if (xterm_dpy_was_reset)
1758 {
1759 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001760
1761# ifndef LESSTIF_VERSION
1762 /* This has been reported to avoid Vim getting stuck. */
1763 if (app_context != (XtAppContext)NULL)
1764 {
1765 XtDestroyApplicationContext(app_context);
1766 app_context = (XtAppContext)NULL;
1767 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1768 }
1769# endif
1770
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001771 setup_term_clip();
1772 get_x11_title(FALSE);
1773 }
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
1778 * Return TRUE when connection to the X server is desired.
1779 */
1780 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001781x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783#if defined(FEAT_CLIENTSERVER)
1784 if (x_force_connect)
1785 return TRUE;
1786#endif
1787 if (x_no_connect)
1788 return FALSE;
1789
1790 /* Check for a match with "exclude:" from 'clipboard'. */
1791 if (clip_exclude_prog != NULL)
1792 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001793 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return FALSE;
1795 }
1796 return TRUE;
1797}
1798
1799/*
1800 * Test if "dpy" and x11_window are valid by getting the window title.
1801 * I don't actually want it yet, so there may be a simpler call to use, but
1802 * this will cause the error handler x_error_check() to be called if anything
1803 * is wrong, such as the window pointer being invalid (as can happen when the
1804 * user changes his DISPLAY, but not his WINDOWID) -- webb
1805 */
1806 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001807test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
1809 int (*old_handler)();
1810 XTextProperty text_prop;
1811
1812 old_handler = XSetErrorHandler(x_error_check);
1813 got_x_error = FALSE;
1814 if (XGetWMName(dpy, x11_window, &text_prop))
1815 XFree((void *)text_prop.value);
1816 XSync(dpy, False);
1817 (void)XSetErrorHandler(old_handler);
1818
1819 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001820 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 return (got_x_error ? FAIL : OK);
1823}
1824#endif
1825
1826#ifdef FEAT_TITLE
1827
1828#ifdef FEAT_X11
1829
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001830static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832/*
1833 * try to get x11 window and display
1834 *
1835 * return FAIL for failure, OK otherwise
1836 */
1837 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001838get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 char *winid;
1841 static int result = -1;
1842#define XD_NONE 0 /* x11_display not set here */
1843#define XD_HERE 1 /* x11_display opened here */
1844#define XD_GUI 2 /* x11_display used from gui.dpy */
1845#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1846 static int x11_display_from = XD_NONE;
1847 static int did_set_error_handler = FALSE;
1848
1849 if (!did_set_error_handler)
1850 {
1851 /* X just exits if it finds an error otherwise! */
1852 (void)XSetErrorHandler(x_error_handler);
1853 did_set_error_handler = TRUE;
1854 }
1855
Bram Moolenaar9372a112005-12-06 19:59:18 +00001856#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (gui.in_use)
1858 {
1859 /*
1860 * If the X11 display was opened here before, for the window where Vim
1861 * was started, close that one now to avoid a memory leak.
1862 */
1863 if (x11_display_from == XD_HERE && x11_display != NULL)
1864 {
1865 XCloseDisplay(x11_display);
1866 x11_display_from = XD_NONE;
1867 }
1868 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1869 {
1870 x11_display_from = XD_GUI;
1871 return OK;
1872 }
1873 x11_display = NULL;
1874 return FAIL;
1875 }
1876 else if (x11_display_from == XD_GUI)
1877 {
1878 /* GUI must have stopped somehow, clear x11_display */
1879 x11_window = 0;
1880 x11_display = NULL;
1881 x11_display_from = XD_NONE;
1882 }
1883#endif
1884
1885 /* When started with the "-X" argument, don't try connecting. */
1886 if (!x_connect_to_server())
1887 return FAIL;
1888
1889 /*
1890 * If WINDOWID not set, should try another method to find out
1891 * what the current window number is. The only code I know for
1892 * this is very complicated.
1893 * We assume that zero is invalid for WINDOWID.
1894 */
1895 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1896 x11_window = (Window)atol(winid);
1897
1898#ifdef FEAT_XCLIPBOARD
1899 if (xterm_dpy != NULL && x11_window != 0)
1900 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001901 /* We may have checked it already, but Gnome terminal can move us to
1902 * another window, so we need to check every time. */
1903 if (x11_display_from != XD_XTERM)
1904 {
1905 /*
1906 * If the X11 display was opened here before, for the window where
1907 * Vim was started, close that one now to avoid a memory leak.
1908 */
1909 if (x11_display_from == XD_HERE && x11_display != NULL)
1910 XCloseDisplay(x11_display);
1911 x11_display = xterm_dpy;
1912 x11_display_from = XD_XTERM;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (test_x11_window(x11_display) == FAIL)
1915 {
1916 /* probably bad $WINDOWID */
1917 x11_window = 0;
1918 x11_display = NULL;
1919 x11_display_from = XD_NONE;
1920 return FAIL;
1921 }
1922 return OK;
1923 }
1924#endif
1925
1926 if (x11_window == 0 || x11_display == NULL)
1927 result = -1;
1928
1929 if (result != -1) /* Have already been here and set this */
1930 return result; /* Don't do all these X calls again */
1931
1932 if (x11_window != 0 && x11_display == NULL)
1933 {
1934#ifdef SET_SIG_ALARM
1935 RETSIGTYPE (*sig_save)();
1936#endif
1937#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1938 struct timeval start_tv;
1939
1940 if (p_verbose > 0)
1941 gettimeofday(&start_tv, NULL);
1942#endif
1943
1944#ifdef SET_SIG_ALARM
1945 /*
1946 * Opening the Display may hang if the DISPLAY setting is wrong, or
1947 * the network connection is bad. Set an alarm timer to get out.
1948 */
1949 sig_alarm_called = FALSE;
1950 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1951 (RETSIGTYPE (*)())sig_alarm);
1952 alarm(2);
1953#endif
1954 x11_display = XOpenDisplay(NULL);
1955
1956#ifdef SET_SIG_ALARM
1957 alarm(0);
1958 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1959 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001960 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#endif
1962 if (x11_display != NULL)
1963 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001964# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001966 {
1967 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001968 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001969 verbose_leave();
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971# endif
1972 if (test_x11_window(x11_display) == FAIL)
1973 {
1974 /* Maybe window id is bad */
1975 x11_window = 0;
1976 XCloseDisplay(x11_display);
1977 x11_display = NULL;
1978 }
1979 else
1980 x11_display_from = XD_HERE;
1981 }
1982 }
1983 if (x11_window == 0 || x11_display == NULL)
1984 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001985
1986# ifdef FEAT_EVAL
1987 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1988# endif
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 return (result = OK);
1991}
1992
1993/*
1994 * Determine original x11 Window Title
1995 */
1996 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001997get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001999 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000}
2001
2002/*
2003 * Determine original x11 Window icon
2004 */
2005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002006get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int retval = FALSE;
2009
2010 retval = get_x11_thing(FALSE, test_only);
2011
2012 /* could not get old icon, use terminal name */
2013 if (oldicon == NULL && !test_only)
2014 {
2015 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002016 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002018 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 return retval;
2022}
2023
2024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002025get_x11_thing(
2026 int get_title, /* get title string */
2027 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 XTextProperty text_prop;
2030 int retval = FALSE;
2031 Status status;
2032
2033 if (get_x11_windis() == OK)
2034 {
2035 /* Get window/icon name if any */
2036 if (get_title)
2037 status = XGetWMName(x11_display, x11_window, &text_prop);
2038 else
2039 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2040
2041 /*
2042 * If terminal is xterm, then x11_window may be a child window of the
2043 * outer xterm window that actually contains the window/icon name, so
2044 * keep traversing up the tree until a window with a title/icon is
2045 * found.
2046 */
2047 /* Previously this was only done for xterm and alikes. I don't see a
2048 * reason why it would fail for other terminal emulators.
2049 * if (term_is_xterm) */
2050 {
2051 Window root;
2052 Window parent;
2053 Window win = x11_window;
2054 Window *children;
2055 unsigned int num_children;
2056
2057 while (!status || text_prop.value == NULL)
2058 {
2059 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2060 &num_children))
2061 break;
2062 if (children)
2063 XFree((void *)children);
2064 if (parent == root || parent == 0)
2065 break;
2066
2067 win = parent;
2068 if (get_title)
2069 status = XGetWMName(x11_display, win, &text_prop);
2070 else
2071 status = XGetWMIconName(x11_display, win, &text_prop);
2072 }
2073 }
2074 if (status && text_prop.value != NULL)
2075 {
2076 retval = TRUE;
2077 if (!test_only)
2078 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002079#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2080 if (text_prop.encoding == XA_STRING
2081# ifdef FEAT_MBYTE
2082 && !has_mbyte
2083# endif
2084 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086#endif
2087 if (get_title)
2088 oldtitle = vim_strsave((char_u *)text_prop.value);
2089 else
2090 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002091#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 else
2094 {
2095 char **cl;
2096 Status transform_status;
2097 int n = 0;
2098
2099 transform_status = XmbTextPropertyToTextList(x11_display,
2100 &text_prop,
2101 &cl, &n);
2102 if (transform_status >= Success && n > 0 && cl[0])
2103 {
2104 if (get_title)
2105 oldtitle = vim_strsave((char_u *) cl[0]);
2106 else
2107 oldicon = vim_strsave((char_u *) cl[0]);
2108 XFreeStringList(cl);
2109 }
2110 else
2111 {
2112 if (get_title)
2113 oldtitle = vim_strsave((char_u *)text_prop.value);
2114 else
2115 oldicon = vim_strsave((char_u *)text_prop.value);
2116 }
2117 }
2118#endif
2119 }
2120 XFree((void *)text_prop.value);
2121 }
2122 }
2123 return retval;
2124}
2125
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002126/* Xutf8 functions are not avaialble on older systems. Note that on some
2127 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2128 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2129 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002131# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132# define USE_UTF8_STRING
2133# endif
2134#endif
2135
2136/*
2137 * Set x11 Window Title
2138 *
2139 * get_x11_windis() must be called before this and have returned OK
2140 */
2141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002142set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2145 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2146 * supported everywhere and STRING doesn't work for multi-byte titles.
2147 */
2148#ifdef USE_UTF8_STRING
2149 if (enc_utf8)
2150 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2151 NULL, NULL, 0, NULL, NULL, NULL);
2152 else
2153#endif
2154 {
2155#if XtSpecificationRelease >= 4
2156# ifdef FEAT_XFONTSET
2157 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2158 NULL, NULL, 0, NULL, NULL, NULL);
2159# else
2160 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002161 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002164 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 XSetWMProperties(x11_display, x11_window, &text_prop,
2166 NULL, NULL, 0, NULL, NULL, NULL);
2167# endif
2168#else
2169 XStoreName(x11_display, x11_window, (char *)title);
2170#endif
2171 }
2172 XFlush(x11_display);
2173}
2174
2175/*
2176 * Set x11 Window icon
2177 *
2178 * get_x11_windis() must be called before this and have returned OK
2179 */
2180 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002181set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 /* See above for comments about using X*SetWMProperties(). */
2184#ifdef USE_UTF8_STRING
2185 if (enc_utf8)
2186 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2187 NULL, 0, NULL, NULL, NULL);
2188 else
2189#endif
2190 {
2191#if XtSpecificationRelease >= 4
2192# ifdef FEAT_XFONTSET
2193 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2194 NULL, 0, NULL, NULL, NULL);
2195# else
2196 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002197 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002199 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2201 NULL, 0, NULL, NULL, NULL);
2202# endif
2203#else
2204 XSetIconName(x11_display, x11_window, (char *)icon);
2205#endif
2206 }
2207 XFlush(x11_display);
2208}
2209
2210#else /* FEAT_X11 */
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002213get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 return FALSE;
2216}
2217
2218 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002219get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 if (!test_only)
2222 {
2223 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002224 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002226 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228 return FALSE;
2229}
2230
2231#endif /* FEAT_X11 */
2232
2233 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002234mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 return get_x11_title(TRUE);
2237}
2238
2239 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002240mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 return get_x11_icon(TRUE);
2243}
2244
2245/*
2246 * Set the window title and icon.
2247 */
2248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002249mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 int type = 0;
2252 static int recursive = 0;
2253
2254 if (T_NAME == NULL) /* no terminal name (yet) */
2255 return;
2256 if (title == NULL && icon == NULL) /* nothing to do */
2257 return;
2258
2259 /* When one of the X11 functions causes a deadly signal, we get here again
2260 * recursively. Avoid hanging then (something is probably locked). */
2261 if (recursive)
2262 return;
2263 ++recursive;
2264
2265 /*
2266 * if the window ID and the display is known, we may use X11 calls
2267 */
2268#ifdef FEAT_X11
2269 if (get_x11_windis() == OK)
2270 type = 1;
2271#else
2272# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2273 if (gui.in_use)
2274 type = 1;
2275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#endif
2277
2278 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002279 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 * than x11 calls, because the x11 calls don't always work
2281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if ((type || *T_TS != NUL) && title != NULL)
2283 {
2284 if (oldtitle == NULL
2285#ifdef FEAT_GUI
2286 && !gui.in_use
2287#endif
2288 ) /* first call but not in GUI, save title */
2289 (void)get_x11_title(FALSE);
2290
2291 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2292 term_settitle(title);
2293#ifdef FEAT_X11
2294 else
2295# ifdef FEAT_GUI_GTK
2296 if (!gui.in_use) /* don't do this if GTK+ is running */
2297# endif
2298 set_x11_title(title); /* x11 */
2299#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002300#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2302 else
2303 gui_mch_settitle(title, icon);
2304#endif
2305 did_set_title = TRUE;
2306 }
2307
2308 if ((type || *T_CIS != NUL) && icon != NULL)
2309 {
2310 if (oldicon == NULL
2311#ifdef FEAT_GUI
2312 && !gui.in_use
2313#endif
2314 ) /* first call, save icon */
2315 get_x11_icon(FALSE);
2316
2317 if (*T_CIS != NUL)
2318 {
2319 out_str(T_CIS); /* set icon start */
2320 out_str_nf(icon);
2321 out_str(T_CIE); /* set icon end */
2322 out_flush();
2323 }
2324#ifdef FEAT_X11
2325 else
2326# ifdef FEAT_GUI_GTK
2327 if (!gui.in_use) /* don't do this if GTK+ is running */
2328# endif
2329 set_x11_icon(icon); /* x11 */
2330#endif
2331 did_set_icon = TRUE;
2332 }
2333 --recursive;
2334}
2335
2336/*
2337 * Restore the window/icon title.
2338 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002339 * SAVE_RESTORE_TITLE only restore title
2340 * SAVE_RESTORE_ICON only restore icon
2341 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 */
2343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002344mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002347 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002349 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2350
2351 // pop and push from/to the stack
2352 term_pop_title(which);
2353 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354}
2355
2356#endif /* FEAT_TITLE */
2357
2358/*
2359 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002360 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 */
2362 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002363vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
2365 if (name == NULL)
2366 return FALSE;
2367 return (STRNICMP(name, "xterm", 5) == 0
2368 || STRNICMP(name, "nxterm", 6) == 0
2369 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002370 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002372 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 || STRCMP(name, "builtin_xterm") == 0);
2374}
2375
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002376#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2377/*
2378 * Return TRUE if "name" appears to be that of a terminal
2379 * known to support the xterm-style mouse protocol.
2380 * Relies on term_is_xterm having been set to its correct value.
2381 */
2382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002383use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002384{
2385 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002386 && (term_is_xterm
2387 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002388 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002389 || STRICMP(name, "st") == 0
2390 || STRNICMP(name, "st-", 3) == 0
2391 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002392}
2393#endif
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2396/*
2397 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2398 * Return 1 for "xterm".
2399 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002400 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002401 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
2403 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002404use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002406 if (ttym_flags == TTYM_SGR)
2407 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002408 if (ttym_flags == TTYM_URXVT)
2409 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (ttym_flags == TTYM_XTERM2)
2411 return 2;
2412 if (ttym_flags == TTYM_XTERM)
2413 return 1;
2414 return 0;
2415}
2416#endif
2417
2418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002419vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 if (name == NULL)
2422 return FALSE;
2423 return (STRNICMP(name, "iris-ansi", 9) == 0
2424 || STRCMP(name, "builtin_iris-ansi") == 0);
2425}
2426
2427 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 if (name == NULL)
2431 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002432 /* catch VT100 - VT5xx */
2433 return ((STRNICMP(name, "vt", 2) == 0
2434 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 || STRCMP(name, "builtin_vt320") == 0);
2436}
2437
2438/*
2439 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2440 * This should include all windowed terminal emulators.
2441 */
2442 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002443vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 if (name == NULL)
2446 return FALSE;
2447 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2448 return TRUE;
2449 return ( STRNICMP(name, "hpterm", 6) == 0
2450 || STRNICMP(name, "sun-cmd", 7) == 0
2451 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002452 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 || STRNICMP(name, "dtterm", 6) == 0);
2454}
2455
2456/*
2457 * Insert user name in s[len].
2458 * Return OK if a name found.
2459 */
2460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002461mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002464 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 return OK;
2466#else
2467 return mch_get_uname(getuid(), s, len);
2468#endif
2469}
2470
2471/*
2472 * Insert user name for "uid" in s[len].
2473 * Return OK if a name found.
2474 */
2475 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002476mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2479 struct passwd *pw;
2480
2481 if ((pw = getpwuid(uid)) != NULL
2482 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2483 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002484 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return OK;
2486 }
2487#endif
2488 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2489 return FAIL; /* a number is not a name */
2490}
2491
2492/*
2493 * Insert host name is s[len].
2494 */
2495
2496#ifdef HAVE_SYS_UTSNAME_H
2497 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002498mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 struct utsname vutsname;
2501
2502 if (uname(&vutsname) < 0)
2503 *s = NUL;
2504 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002505 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506}
2507#else /* HAVE_SYS_UTSNAME_H */
2508
2509# ifdef HAVE_SYS_SYSTEMINFO_H
2510# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2511# endif
2512
2513 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002514mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516# ifdef VAXC
2517 vaxc$gethostname((char *)s, len);
2518# else
2519 gethostname((char *)s, len);
2520# endif
2521 s[len - 1] = NUL; /* make sure it's terminated */
2522}
2523#endif /* HAVE_SYS_UTSNAME_H */
2524
2525/*
2526 * return process ID
2527 */
2528 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002529mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531 return (long)getpid();
2532}
2533
2534#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002535static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002538strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 extern int sys_nerr;
2541 extern char *sys_errlist[];
2542 static char er[20];
2543
2544 if (err > 0 && err < sys_nerr)
2545 return (sys_errlist[err]);
2546 sprintf(er, "Error %d", err);
2547 return er;
2548}
2549#endif
2550
2551/*
2552 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2553 * Return OK for success, FAIL for failure.
2554 */
2555 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002556mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558#if defined(USE_GETCWD)
2559 if (getcwd((char *)buf, len) == NULL)
2560 {
2561 STRCPY(buf, strerror(errno));
2562 return FAIL;
2563 }
2564 return OK;
2565#else
2566 return (getwd((char *)buf) != NULL ? OK : FAIL);
2567#endif
2568}
2569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002571 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 *
2573 * return FAIL for failure, OK for success
2574 */
2575 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002576mch_FullName(
2577 char_u *fname,
2578 char_u *buf,
2579 int len,
2580 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002583#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 int fd = -1;
2585 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 char_u olddir[MAXPATHL];
2588 char_u *p;
2589 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002590#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002591 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2592 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002593#endif
2594
Bram Moolenaar38323e42007-03-06 19:22:53 +00002595#ifdef VMS
2596 fname = vms_fixfilename(fname);
2597#endif
2598
Bram Moolenaara2442432007-04-26 14:26:37 +00002599#ifdef __CYGWIN__
2600 /*
2601 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2602 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002603# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002604 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2605 * a forward slash. */
2606 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2607 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002608# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002609 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002610# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002611 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002612#endif
2613
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002614 /* Expand it if forced or not an absolute path.
2615 * Do not do it for "/file", the result is always "/". */
2616 if ((force || !mch_isFullName(fname))
2617 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
2619 /*
2620 * If the file name has a path, change to that directory for a moment,
2621 * and then do the getwd() (and get back to where we were).
2622 * This will get the correct path name with "../" things.
2623 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002624 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002626#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * Use fchdir() if possible, it's said to be faster and more
2629 * reliable. But on SunOS 4 it might not work. Check this by
2630 * doing a fchdir() right now.
2631 */
2632 if (!dont_fchdir)
2633 {
2634 fd = open(".", O_RDONLY | O_EXTRA, 0);
2635 if (fd >= 0 && fchdir(fd) < 0)
2636 {
2637 close(fd);
2638 fd = -1;
2639 dont_fchdir = TRUE; /* don't try again */
2640 }
2641 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 /* Only change directory when we are sure we can return to where
2645 * we are now. After doing "su" chdir(".") might not work. */
2646 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002647#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 (mch_dirname(olddir, MAXPATHL) == FAIL
2651 || mch_chdir((char *)olddir) != 0))
2652 {
2653 p = NULL; /* can't get current dir: don't chdir */
2654 retval = FAIL;
2655 }
2656 else
2657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 /* The directory is copied into buf[], to be able to remove
2659 * the file name without changing it (could be a string in
2660 * read-only memory) */
2661 if (p - fname >= len)
2662 retval = FAIL;
2663 else
2664 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002665 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (mch_chdir((char *)buf))
2667 retval = FAIL;
2668 else
2669 fname = p + 1;
2670 *buf = NUL;
2671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 }
2673 }
2674 if (mch_dirname(buf, len) == FAIL)
2675 {
2676 retval = FAIL;
2677 *buf = NUL;
2678 }
2679 if (p != NULL)
2680 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002681#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (fd >= 0)
2683 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002684 if (p_verbose >= 5)
2685 {
2686 verbose_enter();
2687 MSG("fchdir() to previous dir");
2688 verbose_leave();
2689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 l = fchdir(fd);
2691 close(fd);
2692 }
2693 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 l = mch_chdir((char *)olddir);
2696 if (l != 0)
2697 EMSG(_(e_prev_dir));
2698 }
2699
2700 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002701 if (l >= len - 1)
2702 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002703#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002704 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002706 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002711 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 return FAIL;
2713
2714 /* Do not append ".", "/dir/." is equal to "/dir". */
2715 if (STRCMP(fname, ".") != 0)
2716 STRCAT(buf, fname);
2717
2718 return OK;
2719}
2720
2721/*
2722 * Return TRUE if "fname" does not depend on the current directory.
2723 */
2724 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002725mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002727#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return ( fname[0] == '/' || fname[0] == '.' ||
2729 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2730 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2731 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002732#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#endif
2735}
2736
Bram Moolenaar24552be2005-12-10 20:17:30 +00002737#if defined(USE_FNAME_CASE) || defined(PROTO)
2738/*
2739 * Set the case of the file name, if it already exists. This will cause the
2740 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002741 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002742 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002743 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002744fname_case(
2745 char_u *name,
2746 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002747{
2748 struct stat st;
2749 char_u *slash, *tail;
2750 DIR *dirp;
2751 struct dirent *dp;
2752
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002753 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002754 {
2755 /* Open the directory where the file is located. */
2756 slash = vim_strrchr(name, '/');
2757 if (slash == NULL)
2758 {
2759 dirp = opendir(".");
2760 tail = name;
2761 }
2762 else
2763 {
2764 *slash = NUL;
2765 dirp = opendir((char *)name);
2766 *slash = '/';
2767 tail = slash + 1;
2768 }
2769
2770 if (dirp != NULL)
2771 {
2772 while ((dp = readdir(dirp)) != NULL)
2773 {
2774 /* Only accept names that differ in case and are the same byte
2775 * length. TODO: accept different length name. */
2776 if (STRICMP(tail, dp->d_name) == 0
2777 && STRLEN(tail) == STRLEN(dp->d_name))
2778 {
2779 char_u newname[MAXPATHL + 1];
2780 struct stat st2;
2781
2782 /* Verify the inode is equal. */
2783 vim_strncpy(newname, name, MAXPATHL);
2784 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2785 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002786 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002787 && st.st_ino == st2.st_ino
2788 && st.st_dev == st2.st_dev)
2789 {
2790 STRCPY(tail, dp->d_name);
2791 break;
2792 }
2793 }
2794 }
2795
2796 closedir(dirp);
2797 }
2798 }
2799}
2800#endif
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802/*
2803 * Get file permissions for 'name'.
2804 * Returns -1 when it doesn't exist.
2805 */
2806 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002807mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 struct stat statb;
2810
2811 /* Keep the #ifdef outside of stat(), it may be a macro. */
2812#ifdef VMS
2813 if (stat((char *)vms_fixfilename(name), &statb))
2814#else
2815 if (stat((char *)name, &statb))
2816#endif
2817 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002818#ifdef __INTERIX
2819 /* The top bit makes the value negative, which means the file doesn't
2820 * exist. Remove the bit, we don't use it. */
2821 return statb.st_mode & ~S_ADDACE;
2822#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825}
2826
2827/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002828 * Set file permission for "name" to "perm".
2829 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 */
2831 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002832mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
2834 return (chmod((char *)
2835#ifdef VMS
2836 vms_fixfilename(name),
2837#else
2838 name,
2839#endif
2840 (mode_t)perm) == 0 ? OK : FAIL);
2841}
2842
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002843#if defined(HAVE_FCHMOD) || defined(PROTO)
2844/*
2845 * Set file permission for open file "fd" to "perm".
2846 * Return FAIL for failure, OK otherwise.
2847 */
2848 int
2849mch_fsetperm(int fd, long perm)
2850{
2851 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2852}
2853#endif
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855#if defined(HAVE_ACL) || defined(PROTO)
2856# ifdef HAVE_SYS_ACL_H
2857# include <sys/acl.h>
2858# endif
2859# ifdef HAVE_SYS_ACCESS_H
2860# include <sys/access.h>
2861# endif
2862
2863# ifdef HAVE_SOLARIS_ACL
2864typedef struct vim_acl_solaris_T {
2865 int acl_cnt;
2866 aclent_t *acl_entry;
2867} vim_acl_solaris_T;
2868# endif
2869
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002870#if defined(HAVE_SELINUX) || defined(PROTO)
2871/*
2872 * Copy security info from "from_file" to "to_file".
2873 */
2874 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002875mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002876{
2877 if (from_file == NULL)
2878 return;
2879
2880 if (selinux_enabled == -1)
2881 selinux_enabled = is_selinux_enabled();
2882
2883 if (selinux_enabled > 0)
2884 {
2885 security_context_t from_context = NULL;
2886 security_context_t to_context = NULL;
2887
2888 if (getfilecon((char *)from_file, &from_context) < 0)
2889 {
2890 /* If the filesystem doesn't support extended attributes,
2891 the original had no special security context and the
2892 target cannot have one either. */
2893 if (errno == EOPNOTSUPP)
2894 return;
2895
2896 MSG_PUTS(_("\nCould not get security context for "));
2897 msg_outtrans(from_file);
2898 msg_putchar('\n');
2899 return;
2900 }
2901 if (getfilecon((char *)to_file, &to_context) < 0)
2902 {
2903 MSG_PUTS(_("\nCould not get security context for "));
2904 msg_outtrans(to_file);
2905 msg_putchar('\n');
2906 freecon (from_context);
2907 return ;
2908 }
2909 if (strcmp(from_context, to_context) != 0)
2910 {
2911 if (setfilecon((char *)to_file, from_context) < 0)
2912 {
2913 MSG_PUTS(_("\nCould not set security context for "));
2914 msg_outtrans(to_file);
2915 msg_putchar('\n');
2916 }
2917 }
2918 freecon(to_context);
2919 freecon(from_context);
2920 }
2921}
2922#endif /* HAVE_SELINUX */
2923
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002924#if defined(HAVE_SMACK) && !defined(PROTO)
2925/*
2926 * Copy security info from "from_file" to "to_file".
2927 */
2928 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002929mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002930{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002931 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002932 {
2933 XATTR_NAME_SMACK,
2934 XATTR_NAME_SMACKEXEC,
2935 XATTR_NAME_SMACKMMAP
2936 };
2937
2938 char buffer[SMACK_LABEL_LEN];
2939 const char *name;
2940 int index;
2941 int ret;
2942 ssize_t size;
2943
2944 if (from_file == NULL)
2945 return;
2946
2947 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2948 / sizeof(smack_copied_attributes)[0]) ; index++)
2949 {
2950 /* get the name of the attribute to copy */
2951 name = smack_copied_attributes[index];
2952
2953 /* get the value of the attribute in buffer */
2954 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2955 if (size >= 0)
2956 {
2957 /* copy the attribute value of buffer */
2958 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2959 if (ret < 0)
2960 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002961 vim_snprintf((char *)IObuff, IOSIZE,
2962 _("Could not set security context %s for %s"),
2963 name, to_file);
2964 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002965 msg_putchar('\n');
2966 }
2967 }
2968 else
2969 {
2970 /* what reason of not having the attribute value? */
2971 switch (errno)
2972 {
2973 case ENOTSUP:
2974 /* extended attributes aren't supported or enabled */
2975 /* should a message be echoed? not sure... */
2976 return; /* leave because it isn't usefull to continue */
2977
2978 case ERANGE:
2979 default:
2980 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002981 vim_snprintf((char *)IObuff, IOSIZE,
2982 _("Could not get security context %s for %s. Removing it!"),
2983 name, from_file);
2984 msg_puts(IObuff);
2985 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002986 /* FALLTHROUGH to remove the attribute */
2987
2988 case ENODATA:
2989 /* no attribute of this name */
2990 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002991 /* Silently ignore errors, apparently this happens when
2992 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002993 break;
2994 }
2995 }
2996 }
2997}
2998#endif /* HAVE_SMACK */
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000/*
3001 * Return a pointer to the ACL of file "fname" in allocated memory.
3002 * Return NULL if the ACL is not available for whatever reason.
3003 */
3004 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003005mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 vim_acl_T ret = NULL;
3008#ifdef HAVE_POSIX_ACL
3009 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3010#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003011#ifdef HAVE_SOLARIS_ZFS_ACL
3012 acl_t *aclent;
3013
3014 if (acl_get((char *)fname, 0, &aclent) < 0)
3015 return NULL;
3016 ret = (vim_acl_T)aclent;
3017#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018#ifdef HAVE_SOLARIS_ACL
3019 vim_acl_solaris_T *aclent;
3020
3021 aclent = malloc(sizeof(vim_acl_solaris_T));
3022 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3023 {
3024 free(aclent);
3025 return NULL;
3026 }
3027 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3028 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3029 {
3030 free(aclent->acl_entry);
3031 free(aclent);
3032 return NULL;
3033 }
3034 ret = (vim_acl_T)aclent;
3035#else
3036#if defined(HAVE_AIX_ACL)
3037 int aclsize;
3038 struct acl *aclent;
3039
3040 aclsize = sizeof(struct acl);
3041 aclent = malloc(aclsize);
3042 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3043 {
3044 if (errno == ENOSPC)
3045 {
3046 aclsize = aclent->acl_len;
3047 aclent = realloc(aclent, aclsize);
3048 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3049 {
3050 free(aclent);
3051 return NULL;
3052 }
3053 }
3054 else
3055 {
3056 free(aclent);
3057 return NULL;
3058 }
3059 }
3060 ret = (vim_acl_T)aclent;
3061#endif /* HAVE_AIX_ACL */
3062#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003063#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif /* HAVE_POSIX_ACL */
3065 return ret;
3066}
3067
3068/*
3069 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3070 */
3071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003072mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 if (aclent == NULL)
3075 return;
3076#ifdef HAVE_POSIX_ACL
3077 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3078#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003079#ifdef HAVE_SOLARIS_ZFS_ACL
3080 acl_set((char *)fname, (acl_t *)aclent);
3081#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#ifdef HAVE_SOLARIS_ACL
3083 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3084 ((vim_acl_solaris_T *)aclent)->acl_entry);
3085#else
3086#ifdef HAVE_AIX_ACL
3087 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3088#endif /* HAVE_AIX_ACL */
3089#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003090#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#endif /* HAVE_POSIX_ACL */
3092}
3093
3094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003095mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 if (aclent == NULL)
3098 return;
3099#ifdef HAVE_POSIX_ACL
3100 acl_free((acl_t)aclent);
3101#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003102#ifdef HAVE_SOLARIS_ZFS_ACL
3103 acl_free((acl_t *)aclent);
3104#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105#ifdef HAVE_SOLARIS_ACL
3106 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3107 free(aclent);
3108#else
3109#ifdef HAVE_AIX_ACL
3110 free(aclent);
3111#endif /* HAVE_AIX_ACL */
3112#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003113#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif /* HAVE_POSIX_ACL */
3115}
3116#endif
3117
3118/*
3119 * Set hidden flag for "name".
3120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003122mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 /* can't hide a file */
3125}
3126
3127/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003128 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * return FALSE if "name" is not a directory
3130 * return FALSE for error
3131 */
3132 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003133mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 struct stat statb;
3136
3137 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3138 return FALSE;
3139 if (stat((char *)name, &statb))
3140 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003144/*
3145 * return TRUE if "name" is a directory, NOT a symlink to a directory
3146 * return FALSE if "name" is not a directory
3147 * return FALSE for error
3148 */
3149 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003150mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003151{
3152 struct stat statb;
3153
3154 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3155 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003156 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003157 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003158 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003159}
3160
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003161static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163/*
3164 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3165 */
3166 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003167executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 struct stat st;
3170
3171 if (stat((char *)name, &st))
3172 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003173#ifdef VMS
3174 /* Like on Unix system file can have executable rights but not necessarily
3175 * be an executable, but on Unix is not a default for an ordianry file to
3176 * have an executable flag - on VMS it is in most cases.
3177 * Therefore, this check does not have any sense - let keep us to the
3178 * conventions instead:
3179 * *.COM and *.EXE files are the executables - the rest are not. This is
3180 * not ideal but better then it was.
3181 */
3182 int vms_executable = 0;
3183 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3184 {
3185 if (strstr(vms_tolower((char*)name),".exe") != NULL
3186 || strstr(vms_tolower((char*)name),".com")!= NULL)
3187 vms_executable = 1;
3188 }
3189 return vms_executable;
3190#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193}
3194
3195/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003196 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003197 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * Return -1 if unknown.
3199 */
3200 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003201mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
3203 char_u *buf;
3204 char_u *p, *e;
3205 int retval;
3206
Bram Moolenaarb5971142015-03-21 17:32:19 +01003207 /* When "use_path" is false and if it's an absolute or relative path don't
3208 * need to use $PATH. */
3209 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3210 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003211 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003212 /* There must be a path separator, files in the current directory
3213 * can't be executed. */
3214 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003215 {
3216 if (path != NULL)
3217 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003218 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003219 *path = FullName_save(name, TRUE);
3220 else
3221 *path = vim_strsave(name);
3222 }
3223 return TRUE;
3224 }
3225 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 p = (char_u *)getenv("PATH");
3229 if (p == NULL || *p == NUL)
3230 return -1;
3231 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3232 if (buf == NULL)
3233 return -1;
3234
3235 /*
3236 * Walk through all entries in $PATH to check if "name" exists there and
3237 * is an executable file.
3238 */
3239 for (;;)
3240 {
3241 e = (char_u *)strchr((char *)p, ':');
3242 if (e == NULL)
3243 e = p + STRLEN(p);
3244 if (e - p <= 1) /* empty entry means current dir */
3245 STRCPY(buf, "./");
3246 else
3247 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003248 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 add_pathsep(buf);
3250 }
3251 STRCAT(buf, name);
3252 retval = executable_file(buf);
3253 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003254 {
3255 if (path != NULL)
3256 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003257 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003258 *path = FullName_save(buf, TRUE);
3259 else
3260 *path = vim_strsave(buf);
3261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265 if (*e != ':')
3266 break;
3267 p = e + 1;
3268 }
3269
3270 vim_free(buf);
3271 return retval;
3272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274/*
3275 * Check what "name" is:
3276 * NODE_NORMAL: file or directory (or doesn't exist)
3277 * NODE_WRITABLE: writable device, socket, fifo, etc.
3278 * NODE_OTHER: non-writable things
3279 */
3280 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003281mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 struct stat st;
3284
3285 if (stat((char *)name, &st))
3286 return NODE_NORMAL;
3287 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3288 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3290 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 /* Everything else is writable? */
3292 return NODE_WRITABLE;
3293}
3294
3295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003296mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298#ifdef HAVE_CHECK_STACK_GROWTH
3299 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 check_stack_growth((char *)&i);
3302
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003303# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 get_stack_limit();
3305# endif
3306
3307#endif
3308
3309 /*
3310 * Setup an alternative stack for signals. Helps to catch signals when
3311 * running out of stack space.
3312 * Use of sigaltstack() is preferred, it's more portable.
3313 * Ignore any errors.
3314 */
3315#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003316 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 init_signal_stack();
3318#endif
3319}
3320
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003321#if defined(EXITFREE) || defined(PROTO)
3322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003323mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003324{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3326 if (clip_star.owned)
3327 clip_lose_selection(&clip_star);
3328 if (clip_plus.owned)
3329 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003330# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003331# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003332 if (xterm_Shell != (Widget)0)
3333 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003334# ifndef LESSTIF_VERSION
3335 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003336 if (xterm_dpy != NULL)
3337 XtCloseDisplay(xterm_dpy);
3338 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003339 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003340 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003341# ifdef FEAT_X11
3342 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3343# endif
3344 }
3345# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003346# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003347# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003348 if (x11_display != NULL
3349# ifdef FEAT_XCLIPBOARD
3350 && x11_display != xterm_dpy
3351# endif
3352 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003353 XCloseDisplay(x11_display);
3354# endif
3355# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003356 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003357# endif
3358# ifdef FEAT_TITLE
3359 vim_free(oldtitle);
3360 vim_free(oldicon);
3361# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003362}
3363#endif
3364
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003365static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367/*
3368 * Output a newline when exiting.
3369 * Make sure the newline goes to the same stream as the text.
3370 */
3371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003372exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003374 if (silent_mode)
3375 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (newline_on_exit || msg_didout)
3377 {
3378 if (msg_use_printf())
3379 {
3380 if (info_message)
3381 mch_msg("\n");
3382 else
3383 mch_errmsg("\r\n");
3384 }
3385 else
3386 out_char('\n');
3387 }
3388 else
3389 {
3390 restore_cterm_colors(); /* get original colors back */
3391 msg_clr_eos_force(); /* clear the rest of the display */
3392 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3393 }
3394}
3395
3396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003397mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 exiting = TRUE;
3400
3401#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3402 x11_export_final_selection();
3403#endif
3404
3405#ifdef FEAT_GUI
3406 if (!gui.in_use)
3407#endif
3408 {
3409 settmode(TMODE_COOK);
3410#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003411 // restore xterm title and icon name
3412 mch_restore_title(SAVE_RESTORE_BOTH);
3413 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#endif
3415 /*
3416 * When t_ti is not empty but it doesn't cause swapping terminal
3417 * pages, need to output a newline when msg_didout is set. But when
3418 * t_ti does swap pages it should not go to the shell page. Do this
3419 * before stoptermcap().
3420 */
3421 if (swapping_screen() && !newline_on_exit)
3422 exit_scroll();
3423
3424 /* Stop termcap: May need to check for T_CRV response, which
3425 * requires RAW mode. */
3426 stoptermcap();
3427
3428 /*
3429 * A newline is only required after a message in the alternate screen.
3430 * This is set to TRUE by wait_return().
3431 */
3432 if (!swapping_screen() || newline_on_exit)
3433 exit_scroll();
3434
3435 /* Cursor may have been switched off without calling starttermcap()
3436 * when doing "vim -u vimrc" and vimrc contains ":q". */
3437 if (full_screen)
3438 cursor_on();
3439 }
3440 out_flush();
3441 ml_close_all(TRUE); /* remove all memfiles */
3442 may_core_dump();
3443#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 gui_exit(r);
3446#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003447
Bram Moolenaar56718732006-03-15 22:53:57 +00003448#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003449 mac_conv_cleanup();
3450#endif
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#ifdef __QNX__
3453 /* A core dump won't be created if the signal handler
3454 * doesn't return, so we can't call exit() */
3455 if (deadly_signal != 0)
3456 return;
3457#endif
3458
Bram Moolenaar009b2592004-10-24 19:18:58 +00003459#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003460 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003461#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003462
3463#ifdef EXITFREE
3464 free_all_mem();
3465#endif
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 exit(r);
3468}
3469
3470 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003471may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 if (deadly_signal != 0)
3474 {
3475 signal(deadly_signal, SIG_DFL);
3476 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3477 }
3478}
3479
3480#ifndef VMS
3481
3482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003483mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
3485 static int first = TRUE;
3486
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003487#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488# ifdef HAVE_TERMIOS_H
3489 static struct termios told;
3490 struct termios tnew;
3491# else
3492 static struct termio told;
3493 struct termio tnew;
3494# endif
3495
3496 if (first)
3497 {
3498 first = FALSE;
3499# if defined(HAVE_TERMIOS_H)
3500 tcgetattr(read_cmd_fd, &told);
3501# else
3502 ioctl(read_cmd_fd, TCGETA, &told);
3503# endif
3504 }
3505
3506 tnew = told;
3507 if (tmode == TMODE_RAW)
3508 {
3509 /*
3510 * ~ICRNL enables typing ^V^M
3511 */
3512 tnew.c_iflag &= ~ICRNL;
3513 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3514# if defined(IEXTEN) && !defined(__MINT__)
3515 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3516 /* but it breaks function keys on MINT */
3517# endif
3518 );
3519# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3520 tnew.c_oflag &= ~ONLCR;
3521# endif
3522 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3523 tnew.c_cc[VTIME] = 0; /* don't wait */
3524 }
3525 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003526 {
3527 /* Also reset ICANON here, otherwise on Solaris select() won't see
3528 * typeahead characters. */
3529 tnew.c_lflag &= ~(ICANON | ECHO);
3530 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3531 tnew.c_cc[VTIME] = 0; /* don't wait */
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
3534# if defined(HAVE_TERMIOS_H)
3535 {
3536 int n = 10;
3537
3538 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3539 * few times. */
3540 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3541 && errno == EINTR && n > 0)
3542 --n;
3543 }
3544# else
3545 ioctl(read_cmd_fd, TCSETA, &tnew);
3546# endif
3547
3548#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 /*
3550 * for "old" tty systems
3551 */
3552# ifndef TIOCSETN
3553# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3554# endif
3555 static struct sgttyb ttybold;
3556 struct sgttyb ttybnew;
3557
3558 if (first)
3559 {
3560 first = FALSE;
3561 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3562 }
3563
3564 ttybnew = ttybold;
3565 if (tmode == TMODE_RAW)
3566 {
3567 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3568 ttybnew.sg_flags |= RAW;
3569 }
3570 else if (tmode == TMODE_SLEEP)
3571 ttybnew.sg_flags &= ~(ECHO);
3572 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3573#endif
3574 curr_tmode = tmode;
3575}
3576
3577/*
3578 * Try to get the code for "t_kb" from the stty setting
3579 *
3580 * Even if termcap claims a backspace key, the user's setting *should*
3581 * prevail. stty knows more about reality than termcap does, and if
3582 * somebody's usual erase key is DEL (which, for most BSD users, it will
3583 * be), they're going to get really annoyed if their erase key starts
3584 * doing forward deletes for no reason. (Eric Fischer)
3585 */
3586 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003587get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003589 ttyinfo_T info;
3590 char_u buf[2];
3591 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003593 if (get_tty_info(read_cmd_fd, &info) == OK)
3594 {
3595 intr_char = info.interrupt;
3596 buf[0] = info.backspace;
3597 buf[1] = NUL;
3598 add_termcode((char_u *)"kb", buf, FALSE);
3599
3600 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3601 p = find_termcode((char_u *)"kD");
3602 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3603 do_fixdel(NULL);
3604 }
3605}
3606
3607/*
3608 * Obtain the characters that Backspace and Enter produce on "fd".
3609 * Returns OK or FAIL.
3610 */
3611 int
3612get_tty_info(int fd, ttyinfo_T *info)
3613{
3614#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615# ifdef HAVE_TERMIOS_H
3616 struct termios keys;
3617# else
3618 struct termio keys;
3619# endif
3620
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003621 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003623 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003625 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003627 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003629 info->backspace = keys.c_cc[VERASE];
3630 info->interrupt = keys.c_cc[VINTR];
3631 if (keys.c_iflag & ICRNL)
3632 info->enter = NL;
3633 else
3634 info->enter = CAR;
3635 if (keys.c_oflag & ONLCR)
3636 info->nl_does_cr = TRUE;
3637 else
3638 info->nl_does_cr = FALSE;
3639 return OK;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641#else
3642 /* for "old" tty systems */
3643 struct sgttyb keys;
3644
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003645 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003647 info->backspace = keys.sg_erase;
3648 info->interrupt = keys.sg_kill;
3649 info->enter = CAR;
3650 info->nl_does_cr = TRUE;
3651 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003654 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655}
3656
3657#endif /* VMS */
3658
3659#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003660static int mouse_ison = FALSE;
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
3663 * Set mouse clicks on or off.
3664 */
3665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003666mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003668# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003669 static int bevalterm_ison = FALSE;
3670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 int xterm_mouse_vers;
3672
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003673 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003674# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003675 && p_bevalterm == bevalterm_ison
3676# endif
3677 )
3678 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return;
3680
3681 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003682
3683# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003684 if (ttym_flags == TTYM_URXVT)
3685 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003686 out_str_nf((char_u *)
3687 (on
3688 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3689 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003691 }
3692# endif
3693
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003694# ifdef FEAT_MOUSE_SGR
3695 if (ttym_flags == TTYM_SGR)
3696 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003697 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003698 out_str_nf((char_u *)
3699 (on
3700 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3701 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702 mouse_ison = on;
3703 }
3704# endif
3705
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003706# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 if (bevalterm_ison != (p_bevalterm && on))
3708 {
3709 bevalterm_ison = (p_bevalterm && on);
3710 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3711 /* disable mouse movement events, enabling is below */
3712 out_str_nf((char_u *)
3713 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003714 }
3715# endif
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (xterm_mouse_vers > 0)
3718 {
3719 if (on) /* enable mouse events, use mouse tracking if available */
3720 out_str_nf((char_u *)
3721 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003722 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003723# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003724 bevalterm_ison
3725 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3726# endif
3727 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3729 else /* disable mouse events, could probably always send the same */
3730 out_str_nf((char_u *)
3731 (xterm_mouse_vers > 1
3732 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3733 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003734 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736
3737# ifdef FEAT_MOUSE_DEC
3738 else if (ttym_flags == TTYM_DEC)
3739 {
3740 if (on) /* enable mouse events */
3741 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3742 else /* disable mouse events */
3743 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003744 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746# endif
3747
3748# ifdef FEAT_MOUSE_GPM
3749 else
3750 {
3751 if (on)
3752 {
3753 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 else
3757 {
3758 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 }
3762# endif
3763
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003764# ifdef FEAT_SYSMOUSE
3765 else
3766 {
3767 if (on)
3768 {
3769 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003770 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003771 }
3772 else
3773 {
3774 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003775 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003776 }
3777 }
3778# endif
3779
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780# ifdef FEAT_MOUSE_JSB
3781 else
3782 {
3783 if (on)
3784 {
3785 /* D - Enable Mouse up/down messages
3786 * L - Enable Left Button Reporting
3787 * M - Enable Middle Button Reporting
3788 * R - Enable Right Button Reporting
3789 * K - Enable SHIFT and CTRL key Reporting
3790 * + - Enable Advanced messaging of mouse moves and up/down messages
3791 * Q - Quiet No Ack
3792 * # - Numeric value of mouse pointer required
3793 * 0 = Multiview 2000 cursor, used as standard
3794 * 1 = Windows Arrow
3795 * 2 = Windows I Beam
3796 * 3 = Windows Hour Glass
3797 * 4 = Windows Cross Hair
3798 * 5 = Windows UP Arrow
3799 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003800# ifdef JSBTERM_MOUSE_NONADVANCED
3801 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3803 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003804# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3806 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003807# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003808 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 else
3811 {
3812 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3813 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003814 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
3817# endif
3818# ifdef FEAT_MOUSE_PTERM
3819 else
3820 {
3821 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3822 if (on)
3823 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3824 else
3825 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003826 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828# endif
3829}
3830
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003831#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003832/*
3833 * Called when 'balloonevalterm' changed.
3834 */
3835 void
3836mch_bevalterm_changed(void)
3837{
3838 mch_setmouse(mouse_ison);
3839}
3840#endif
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842/*
3843 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3844 */
3845 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003846check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848# ifdef FEAT_MOUSE_XTERM
3849 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003850# ifdef FEAT_MOUSE_URXVT
3851 && use_xterm_mouse() != 3
3852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853# ifdef FEAT_GUI
3854 && !gui.in_use
3855# endif
3856 )
3857 {
3858 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003859 ? IF_EB("\233M", CSI_STR "M")
3860 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (*p_mouse != NUL)
3862 {
3863 /* force mouse off and maybe on to send possibly new mouse
3864 * activation sequence to the xterm, with(out) drag tracing. */
3865 mch_setmouse(FALSE);
3866 setmouse();
3867 }
3868 }
3869 else
3870 del_mouse_termcode(KS_MOUSE);
3871# endif
3872
3873# ifdef FEAT_MOUSE_GPM
3874 if (!use_xterm_mouse()
3875# ifdef FEAT_GUI
3876 && !gui.in_use
3877# endif
3878 )
3879 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3880# endif
3881
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003882# ifdef FEAT_SYSMOUSE
3883 if (!use_xterm_mouse()
3884# ifdef FEAT_GUI
3885 && !gui.in_use
3886# endif
3887 )
3888 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3889# endif
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003892 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (!use_xterm_mouse()
3894# ifdef FEAT_GUI
3895 && !gui.in_use
3896# endif
3897 )
3898 set_mouse_termcode(KS_JSBTERM_MOUSE,
3899 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3900 else
3901 del_mouse_termcode(KS_JSBTERM_MOUSE);
3902# endif
3903
3904# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003905 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 * define it in the GUI or when using an xterm. */
3907 if (!use_xterm_mouse()
3908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
3912 set_mouse_termcode(KS_NETTERM_MOUSE,
3913 (char_u *)IF_EB("\033}", ESC_STR "}"));
3914 else
3915 del_mouse_termcode(KS_NETTERM_MOUSE);
3916# endif
3917
3918# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003919 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003920 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921# ifdef FEAT_GUI
3922 && !gui.in_use
3923# endif
3924 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003925 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3926 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 else
3928 del_mouse_termcode(KS_DEC_MOUSE);
3929# endif
3930# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003931 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003932 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933# ifdef FEAT_GUI
3934 && !gui.in_use
3935# endif
3936 )
3937 set_mouse_termcode(KS_PTERM_MOUSE,
3938 (char_u *) IF_EB("\033[", ESC_STR "["));
3939 else
3940 del_mouse_termcode(KS_PTERM_MOUSE);
3941# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003942# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003943 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003944# ifdef FEAT_GUI
3945 && !gui.in_use
3946# endif
3947 )
3948 {
3949 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003950 ? IF_EB("\233*M", CSI_STR "*M")
3951 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003952
3953 if (*p_mouse != NUL)
3954 {
3955 mch_setmouse(FALSE);
3956 setmouse();
3957 }
3958 }
3959 else
3960 del_mouse_termcode(KS_URXVT_MOUSE);
3961# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003962# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003963 if (use_xterm_mouse() == 4
3964# ifdef FEAT_GUI
3965 && !gui.in_use
3966# endif
3967 )
3968 {
3969 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003970 ? IF_EB("\233<*M", CSI_STR "<*M")
3971 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3972
3973 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3974 ? IF_EB("\233<*m", CSI_STR "<*m")
3975 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003976
3977 if (*p_mouse != NUL)
3978 {
3979 mch_setmouse(FALSE);
3980 setmouse();
3981 }
3982 }
3983 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003984 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003985 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003986 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3987 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990#endif
3991
3992/*
3993 * set screen mode, always fails.
3994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003996mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
3998 EMSG(_(e_screenmode));
3999 return FAIL;
4000}
4001
4002#ifndef VMS
4003
4004/*
4005 * Try to get the current window size:
4006 * 1. with an ioctl(), most accurate method
4007 * 2. from the environment variables LINES and COLUMNS
4008 * 3. from the termcap
4009 * 4. keep using the old values
4010 * Return OK when size could be determined, FAIL otherwise.
4011 */
4012 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004013mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 long rows = 0;
4016 long columns = 0;
4017 char_u *p;
4018
4019 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * 1. try using an ioctl. It is the most accurate method.
4021 *
4022 * Try using TIOCGWINSZ first, some systems that have it also define
4023 * TIOCGSIZE but don't have a struct ttysize.
4024 */
4025# ifdef TIOCGWINSZ
4026 {
4027 struct winsize ws;
4028 int fd = 1;
4029
4030 /* When stdout is not a tty, use stdin for the ioctl(). */
4031 if (!isatty(fd) && isatty(read_cmd_fd))
4032 fd = read_cmd_fd;
4033 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4034 {
4035 columns = ws.ws_col;
4036 rows = ws.ws_row;
4037 }
4038 }
4039# else /* TIOCGWINSZ */
4040# ifdef TIOCGSIZE
4041 {
4042 struct ttysize ts;
4043 int fd = 1;
4044
4045 /* When stdout is not a tty, use stdin for the ioctl(). */
4046 if (!isatty(fd) && isatty(read_cmd_fd))
4047 fd = read_cmd_fd;
4048 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4049 {
4050 columns = ts.ts_cols;
4051 rows = ts.ts_lines;
4052 }
4053 }
4054# endif /* TIOCGSIZE */
4055# endif /* TIOCGWINSZ */
4056
4057 /*
4058 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004059 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4060 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004062 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 if ((p = (char_u *)getenv("LINES")))
4065 rows = atoi((char *)p);
4066 if ((p = (char_u *)getenv("COLUMNS")))
4067 columns = atoi((char *)p);
4068 }
4069
4070#ifdef HAVE_TGETENT
4071 /*
4072 * 3. try reading "co" and "li" entries from termcap
4073 */
4074 if (columns == 0 || rows == 0)
4075 getlinecol(&columns, &rows);
4076#endif
4077
4078 /*
4079 * 4. If everything fails, use the old values
4080 */
4081 if (columns <= 0 || rows <= 0)
4082 return FAIL;
4083
4084 Rows = rows;
4085 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004086 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return OK;
4088}
4089
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004090#if defined(FEAT_TERMINAL) || defined(PROTO)
4091/*
4092 * Report the windows size "rows" and "cols" to tty "fd".
4093 */
4094 int
4095mch_report_winsize(int fd, int rows, int cols)
4096{
4097# ifdef TIOCSWINSZ
4098 struct winsize ws;
4099
4100 ws.ws_col = cols;
4101 ws.ws_row = rows;
4102 ws.ws_xpixel = cols * 5;
4103 ws.ws_ypixel = rows * 10;
4104 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4105 {
4106 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4107 return OK;
4108 }
4109 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4110# else
4111# ifdef TIOCSSIZE
4112 struct ttysize ts;
4113
4114 ts.ts_cols = cols;
4115 ts.ts_lines = rows;
4116 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4117 {
4118 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4119 return OK;
4120 }
4121 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4122# endif
4123# endif
4124 return FAIL;
4125}
4126#endif
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128/*
4129 * Try to set the window size to Rows and Columns.
4130 */
4131 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004132mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 if (*T_CWS)
4135 {
4136 /*
4137 * NOTE: if you get an error here that term_set_winsize() is
4138 * undefined, check the output of configure. It could probably not
4139 * find a ncurses, termcap or termlib library.
4140 */
4141 term_set_winsize((int)Rows, (int)Columns);
4142 out_flush();
4143 screen_start(); /* don't know where cursor is now */
4144 }
4145}
4146
4147#endif /* VMS */
4148
4149/*
4150 * Rows and/or Columns has changed.
4151 */
4152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004153mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155 /* Nothing to do. */
4156}
4157
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158/*
4159 * Wait for process "child" to end.
4160 * Return "child" if it exited properly, <= 0 on error.
4161 */
4162 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004163wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004164{
4165 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004166 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004167
4168 while (wait_pid != child)
4169 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004170 /* When compiled with Python threads are probably used, in which case
4171 * wait() sometimes hangs for no obvious reason. Use waitpid()
4172 * instead and loop (like the GUI). Also needed for other interfaces,
4173 * they might call system(). */
4174# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004175 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004176# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004177 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004178# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004179 if (wait_pid == 0)
4180 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004181 /* Wait for 1 to 10 msec before trying again. */
4182 mch_delay(delay_msec, TRUE);
4183 if (++delay_msec > 10)
4184 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004185 continue;
4186 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004187 if (wait_pid <= 0
4188# ifdef ECHILD
4189 && errno == ECHILD
4190# endif
4191 )
4192 break;
4193 }
4194 return wait_pid;
4195}
4196
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004197#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004198/*
4199 * Set the environment for a child process.
4200 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004201 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004202set_child_environment(
4203 long rows,
4204 long columns,
4205 char *term,
4206 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004207{
4208# ifdef HAVE_SETENV
4209 char envbuf[50];
4210# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004211 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004212 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004213 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004214 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004215 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004216# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004217 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004218# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004219# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004220 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004221# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004223 long colors =
4224# ifdef FEAT_GUI
4225 gui.in_use ? 256*256*256 :
4226# endif
4227 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004228
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004229# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004230 setenv("TERM", term, 1);
4231 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004232 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004233 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004234 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004235 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004236 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004237 sprintf((char *)envbuf, "%ld", colors);
4238 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004239# ifdef FEAT_TERMINAL
4240 if (is_terminal)
4241 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004242 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004243 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4244 }
4245# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004246# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004247 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004248# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249# else
4250 /*
4251 * Putenv does not copy the string, it has to remain valid.
4252 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004253 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004254 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004255 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4256 putenv(envbuf_Term);
4257 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004259 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4260 putenv(envbuf_Lines);
4261 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4262 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004263 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004264 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4265 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004266# ifdef FEAT_TERMINAL
4267 if (is_terminal)
4268 {
4269 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004270 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004271 putenv(envbuf_Version);
4272 }
4273# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004274# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004275 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4276 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4277 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004278# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004279# endif
4280}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004281
4282 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004283set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004284{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004285 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004286}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004287#endif
4288
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004289#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004290/*
4291 * Open a PTY, with FD for the master and slave side.
4292 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4293 * When successful both file descriptors are stored.
4294 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004295 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004296open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004297{
4298 char *tty_name;
4299
4300 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4301 if (*pty_master_fd >= 0)
4302 {
4303 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4304 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4305 * adding O_NOCTTY always works when defined. */
4306#ifdef O_NOCTTY
4307 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4308#else
4309 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4310#endif
4311 if (*pty_slave_fd < 0)
4312 {
4313 close(*pty_master_fd);
4314 *pty_master_fd = -1;
4315 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004316 else if (namep != NULL)
4317 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004318 }
4319}
4320#endif
4321
Bram Moolenaarfae42832017-08-01 22:24:26 +02004322/*
4323 * Send SIGINT to a child process if "c" is an interrupt character.
4324 */
4325 void
4326may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4327{
4328# ifdef SIGINT
4329 if (c == Ctrl_C || c == intr_char)
4330 {
4331# ifdef HAVE_SETSID
4332 kill(-pid, SIGINT);
4333# else
4334 kill(0, SIGINT);
4335# endif
4336 if (wpid > 0)
4337 kill(wpid, SIGINT);
4338 }
4339# endif
4340}
4341
Bram Moolenaar13568252018-03-16 20:46:58 +01004342#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4343
4344 static int
4345build_argv(
4346 char_u *cmd,
4347 char ***argvp,
4348 char_u **sh_tofree,
4349 char_u **shcf_tofree)
4350{
4351 char **argv = NULL;
4352 int argc;
4353
4354 *sh_tofree = vim_strsave(p_sh);
4355 if (*sh_tofree == NULL) /* out of memory */
4356 return FAIL;
4357
4358 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4359 return FAIL;
4360 *argvp = argv;
4361
4362 if (cmd != NULL)
4363 {
4364 char_u *s;
4365 char_u *p;
4366
4367 if (extra_shell_arg != NULL)
4368 argv[argc++] = (char *)extra_shell_arg;
4369
4370 /* Break 'shellcmdflag' into white separated parts. This doesn't
4371 * handle quoted strings, they are very unlikely to appear. */
4372 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4373 if (*shcf_tofree == NULL) /* out of memory */
4374 return FAIL;
4375 s = *shcf_tofree;
4376 p = p_shcf;
4377 while (*p != NUL)
4378 {
4379 argv[argc++] = (char *)s;
4380 while (*p && *p != ' ' && *p != TAB)
4381 *s++ = *p++;
4382 *s++ = NUL;
4383 p = skipwhite(p);
4384 }
4385
4386 argv[argc++] = (char *)cmd;
4387 }
4388 argv[argc] = NULL;
4389 return OK;
4390}
4391#endif
4392
4393#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4394/*
4395 * Use a terminal window to run a shell command in.
4396 */
4397 static int
4398mch_call_shell_terminal(
4399 char_u *cmd,
4400 int options UNUSED) /* SHELL_*, see vim.h */
4401{
4402 jobopt_T opt;
4403 char **argv = NULL;
4404 char_u *tofree1 = NULL;
4405 char_u *tofree2 = NULL;
4406 int retval = -1;
4407 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004408 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004409 aco_save_T aco;
4410 oparg_T oa; /* operator arguments */
4411
4412 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4413 goto theend;
4414
4415 init_job_options(&opt);
4416 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4417 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004418 if (buf == NULL)
4419 goto theend;
4420
4421 job = term_getjob(buf->b_term);
4422 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004423
4424 /* Find a window to make "buf" curbuf. */
4425 aucmd_prepbuf(&aco, buf);
4426
4427 clear_oparg(&oa);
4428 while (term_use_loop())
4429 {
4430 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4431 {
4432 /* If terminal_loop() returns OK we got a key that is handled
4433 * in Normal model. We don't do redrawing anyway. */
4434 if (terminal_loop(TRUE) == OK)
4435 normal_cmd(&oa, TRUE);
4436 }
4437 else
4438 normal_cmd(&oa, TRUE);
4439 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004440 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004441 ch_log(NULL, "system command finished");
4442
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004443 job_unref(job);
4444
Bram Moolenaar13568252018-03-16 20:46:58 +01004445 /* restore curwin/curbuf and a few other things */
4446 aucmd_restbuf(&aco);
4447
4448 wait_return(TRUE);
4449 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4450
4451theend:
4452 vim_free(argv);
4453 vim_free(tofree1);
4454 vim_free(tofree2);
4455 return retval;
4456}
4457#endif
4458
4459#ifdef USE_SYSTEM
4460/*
4461 * Use system() to start the shell: simple but slow.
4462 */
4463 static int
4464mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004465 char_u *cmd,
4466 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
4468#ifdef VMS
4469 char *ifn = NULL;
4470 char *ofn = NULL;
4471#endif
4472 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004473 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004474 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
4476 out_flush();
4477
4478 if (options & SHELL_COOKED)
4479 settmode(TMODE_COOK); /* set to normal mode */
4480
Bram Moolenaar62b42182010-09-21 22:09:37 +02004481# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004482 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004483 loose_clipboard();
4484# endif
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (cmd == NULL)
4487 x = system((char *)p_sh);
4488 else
4489 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004490# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (ofn = strchr((char *)cmd, '>'))
4492 *ofn++ = '\0';
4493 if (ifn = strchr((char *)cmd, '<'))
4494 {
4495 char *p;
4496
4497 *ifn++ = '\0';
4498 p = strchr(ifn,' '); /* chop off any trailing spaces */
4499 if (p)
4500 *p = '\0';
4501 }
4502 if (ofn)
4503 x = vms_sys((char *)cmd, ofn, ifn);
4504 else
4505 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004506# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 newcmd = lalloc(STRLEN(p_sh)
4508 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4509 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4510 if (newcmd == NULL)
4511 x = 0;
4512 else
4513 {
4514 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4515 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4516 (char *)p_shcf,
4517 (char *)cmd);
4518 x = system((char *)newcmd);
4519 vim_free(newcmd);
4520 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523# ifdef VMS
4524 x = vms_sys_status(x);
4525# endif
4526 if (emsg_silent)
4527 ;
4528 else if (x == 127)
4529 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else if (x && !(options & SHELL_SILENT))
4531 {
4532 MSG_PUTS(_("\nshell returned "));
4533 msg_outnum((long)x);
4534 msg_putchar('\n');
4535 }
4536
4537 if (tmode == TMODE_RAW)
4538 settmode(TMODE_RAW); /* set to raw mode */
4539# ifdef FEAT_TITLE
4540 resettitle();
4541# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004542# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4543 restore_clipboard();
4544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004546}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Bram Moolenaar13568252018-03-16 20:46:58 +01004548#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaardf177f62005-02-22 08:39:57 +00004550# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4551 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004552# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar13568252018-03-16 20:46:58 +01004554/*
4555 * Don't use system(), use fork()/exec().
4556 */
4557 static int
4558mch_call_shell_fork(
4559 char_u *cmd,
4560 int options) /* SHELL_*, see vim.h */
4561{
4562 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004564 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 pid_t wait_pid = 0;
4566# ifdef HAVE_UNION_WAIT
4567 union wait status;
4568# else
4569 int status = -1;
4570# endif
4571 int retval = -1;
4572 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004573 char_u *tofree1 = NULL;
4574 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004580 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 int fd_fromshell[2];
4582 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004583 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585 out_flush();
4586 if (options & SHELL_COOKED)
4587 settmode(TMODE_COOK); /* set to normal mode */
4588
Bram Moolenaar13568252018-03-16 20:46:58 +01004589 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004590 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004593 * For the GUI, when writing the output into the buffer and when reading
4594 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4595 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004597 if ((options & (SHELL_READ|SHELL_WRITE))
4598# ifdef FEAT_GUI
4599 || (gui.in_use && show_shell_mess)
4600# endif
4601 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * Try to open a master pty.
4606 * If this works, open the slave pty.
4607 * If the slave can't be opened, close the master pty.
4608 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004609 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004610 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 /*
4612 * If not opening a pty or it didn't work, try using pipes.
4613 */
4614 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 pipe_error = (pipe(fd_toshell) < 0);
4618 if (!pipe_error) /* pipe create OK */
4619 {
4620 pipe_error = (pipe(fd_fromshell) < 0);
4621 if (pipe_error) /* pipe create failed */
4622 {
4623 close(fd_toshell[0]);
4624 close(fd_toshell[1]);
4625 }
4626 }
4627 if (pipe_error)
4628 {
4629 MSG_PUTS(_("\nCannot create pipes\n"));
4630 out_flush();
4631 }
4632 }
4633 }
4634
4635 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004637 SIGSET_DECL(curset)
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639# ifdef __BEOS__
4640 beos_cleanup_read_thread();
4641# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004642
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004643 BLOCK_SIGNALS(&curset);
4644 pid = fork(); /* maybe we should use vfork() */
4645 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004647 UNBLOCK_SIGNALS(&curset);
4648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004650 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004652 || (gui.in_use && show_shell_mess)
4653# endif
4654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004656# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (pty_master_fd >= 0) /* close the pseudo tty */
4658 {
4659 close(pty_master_fd);
4660 close(pty_slave_fd);
4661 }
4662 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
4665 close(fd_toshell[0]);
4666 close(fd_toshell[1]);
4667 close(fd_fromshell[0]);
4668 close(fd_fromshell[1]);
4669 }
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 else if (pid == 0) /* child */
4673 {
4674 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004675 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaar819524702018-02-27 19:10:00 +01004677# ifdef FEAT_JOB_CHANNEL
4678 if (ch_log_active())
4679 /* close the log file in the child */
4680 ch_logfile((char_u *)"", (char_u *)"");
4681# endif
4682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (!show_shell_mess || (options & SHELL_EXPAND))
4684 {
4685 int fd;
4686
4687 /*
4688 * Don't want to show any message from the shell. Can't just
4689 * close stdout and stderr though, because some systems will
4690 * break if you try to write to them after that, so we must
4691 * use dup() to replace them with something else -- webb
4692 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4693 * waiting for input.
4694 */
4695 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4696 fclose(stdin);
4697 fclose(stdout);
4698 fclose(stderr);
4699
4700 /*
4701 * If any of these open()'s and dup()'s fail, we just continue
4702 * anyway. It's not fatal, and on most systems it will make
4703 * no difference at all. On a few it will cause the execvp()
4704 * to exit with a non-zero status even when the completion
4705 * could be done, which is nothing too serious. If the open()
4706 * or dup() failed we'd just do the same thing ourselves
4707 * anyway -- webb
4708 */
4709 if (fd >= 0)
4710 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004711 ignored = dup(fd); /* To replace stdin (fd 0) */
4712 ignored = dup(fd); /* To replace stdout (fd 1) */
4713 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
4715 /* Don't need this now that we've duplicated it */
4716 close(fd);
4717 }
4718 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004719 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004721 || gui.in_use
4722# endif
4723 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004727 /* Create our own process group, so that the child and all its
4728 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004729 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004730 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004731 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004732 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004733# if defined(SIGHUP)
4734 /* When doing "!xterm&" and 'shell' is bash: the shell
4735 * will exit and send SIGHUP to all processes in its
4736 * group, killing the just started process. Ignore SIGHUP
4737 * to avoid that. (suggested by Simon Schubert)
4738 */
4739 signal(SIGHUP, SIG_IGN);
4740# endif
4741 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004742# endif
4743# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004744 if (pty_slave_fd >= 0)
4745 {
4746 /* push stream discipline modules */
4747 if (options & SHELL_COOKED)
4748 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004750 /* Try to become controlling tty (probably doesn't work,
4751 * unless run by root) */
4752 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004754 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004756 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
Bram Moolenaara5792f52005-11-23 21:25:05 +00004758 /*
4759 * stderr is only redirected when using the GUI, so that a
4760 * program like gpg can still access the terminal to get a
4761 * passphrase using stderr.
4762 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004763# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (pty_master_fd >= 0)
4765 {
4766 close(pty_master_fd); /* close master side of pty */
4767
4768 /* set up stdin/stdout/stderr for the child */
4769 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004770 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004772 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004773 if (gui.in_use)
4774 {
4775 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004776 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 close(pty_slave_fd); /* has been dupped, close it now */
4780 }
4781 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 /* set up stdin for the child */
4785 close(fd_toshell[1]);
4786 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004787 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 close(fd_toshell[0]);
4789
4790 /* set up stdout for the child */
4791 close(fd_fromshell[0]);
4792 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004793 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 close(fd_fromshell[1]);
4795
Bram Moolenaara5792f52005-11-23 21:25:05 +00004796# ifdef FEAT_GUI
4797 if (gui.in_use)
4798 {
4799 /* set up stderr for the child */
4800 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004801 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004802 }
4803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 /*
4808 * There is no type cast for the argv, because the type may be
4809 * different on different machines. This may cause a warning
4810 * message with strict compilers, don't worry about it.
4811 * Call _exit() instead of exit() to avoid closing the connection
4812 * to the X server (esp. with GTK, which uses atexit()).
4813 */
4814 execvp(argv[0], argv);
4815 _exit(EXEC_FAILED); /* exec failed, return failure code */
4816 }
4817 else /* parent */
4818 {
4819 /*
4820 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004821 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 */
4823 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004824 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004825 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004826# ifdef FEAT_JOB_CHANNEL
4827 ++dont_check_job_ended;
4828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 /*
4830 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004831 * This is also used to pipe stdin/stdout to/from the external
4832 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834 if ((options & (SHELL_READ|SHELL_WRITE))
4835# ifdef FEAT_GUI
4836 || (gui.in_use && show_shell_mess)
4837# endif
4838 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004844# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4846 int ta_len = 0; /* valid bytes in ta_buf[] */
4847 int len;
4848 int p_more_save;
4849 int old_State;
4850 int c;
4851 int toshell_fd;
4852 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004853 garray_T ga;
4854 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004855# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4856 struct timeval start_tv;
4857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
Bram Moolenaardf177f62005-02-22 08:39:57 +00004859# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (pty_master_fd >= 0)
4861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 fromshell_fd = pty_master_fd;
4863 toshell_fd = dup(pty_master_fd);
4864 }
4865 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 close(fd_toshell[0]);
4869 close(fd_fromshell[1]);
4870 toshell_fd = fd_toshell[1];
4871 fromshell_fd = fd_fromshell[0];
4872 }
4873
4874 /*
4875 * Write to the child if there are typed characters.
4876 * Read from the child if there are characters available.
4877 * Repeat the reading a few times if more characters are
4878 * available. Need to check for typed keys now and then, but
4879 * not too often (delays when no chars are available).
4880 * This loop is quit if no characters can be read from the pty
4881 * (WaitForChar detected special condition), or there are no
4882 * characters available and the child has exited.
4883 * Only check if the child has exited when there is no more
4884 * output. The child may exit before all the output has
4885 * been printed.
4886 *
4887 * Currently this busy loops!
4888 * This can probably dead-lock when the write blocks!
4889 */
4890 p_more_save = p_more;
4891 p_more = FALSE;
4892 old_State = State;
4893 State = EXTERNCMD; /* don't redraw at window resize */
4894
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004895 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004896 {
4897 /* Fork a process that will write the lines to the
4898 * external program. */
4899 if ((wpid = fork()) == -1)
4900 {
4901 MSG_PUTS(_("\nCannot fork\n"));
4902 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004903 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004904 {
4905 linenr_T lnum = curbuf->b_op_start.lnum;
4906 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004907 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 size_t l;
4909
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004910 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 for (;;)
4912 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004913 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 if (l == 0)
4915 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004916 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917 /* NL -> NUL translation */
4918 len = write(toshell_fd, "", (size_t)1);
4919 else
4920 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004921 char_u *s = vim_strchr(lp + written, NL);
4922
Bram Moolenaar89d40322006-08-29 15:30:07 +00004923 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004924 s == NULL ? l
4925 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004926 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004927 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004928 {
4929 /* Finished a line, add a NL, unless this line
4930 * should not have one. */
4931 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004932 || (!curbuf->b_p_bin
4933 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004934 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004935 && (lnum !=
4936 curbuf->b_ml.ml_line_count
4937 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004938 ignored = write(toshell_fd, "\n",
4939 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 ++lnum;
4941 if (lnum > curbuf->b_op_end.lnum)
4942 {
4943 /* finished all the lines, close pipe */
4944 close(toshell_fd);
4945 toshell_fd = -1;
4946 break;
4947 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004948 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004949 written = 0;
4950 }
4951 else if (len > 0)
4952 written += len;
4953 }
4954 _exit(0);
4955 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004956 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004957 {
4958 close(toshell_fd);
4959 toshell_fd = -1;
4960 }
4961 }
4962
4963 if (options & SHELL_READ)
4964 ga_init2(&ga, 1, BUFLEN);
4965
4966 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004967# ifdef ELAPSED_FUNC
4968 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 for (;;)
4971 {
4972 /*
4973 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004974 * if there are any.
4975 * Don't do this if we are expanding wild cards (would eat
4976 * typeahead).
4977 * Don't do this when filtering and terminal is in cooked
4978 * mode, the shell command will handle the I/O. Avoids
4979 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004980 * Don't get characters when the child has already
4981 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004982 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004983 * while (noread_cnt > 4), avoids that ":r !ls" eats
4984 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
4986 len = 0;
4987 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004988 && ((options &
4989 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4990 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004991# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004992 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004993# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004994 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004995 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004996 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004998 if (ta_len == 0)
4999 {
5000 /* Get extra characters when we don't have any.
5001 * Reset the counter and timer. */
5002 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005003# ifdef ELAPSED_FUNC
5004 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005005# endif
5006 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5007 }
5008 if (ta_len > 0 || len > 0)
5009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 /*
5011 * For pipes:
5012 * Check for CTRL-C: send interrupt signal to child.
5013 * Check for CTRL-D: EOF, close pipe to child.
5014 */
5015 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 /*
5018 * Send SIGINT to the child's group or all
5019 * processes in our group.
5020 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005021 may_send_sigint(ta_buf[ta_len], pid, wpid);
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (pty_master_fd < 0 && toshell_fd >= 0
5024 && ta_buf[ta_len] == Ctrl_D)
5025 {
5026 close(toshell_fd);
5027 toshell_fd = -1;
5028 }
5029 }
5030
5031 /* replace K_BS by <BS> and K_DEL by <DEL> */
5032 for (i = ta_len; i < ta_len + len; ++i)
5033 {
5034 if (ta_buf[i] == CSI && len - i > 2)
5035 {
5036 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5037 if (c == K_DEL || c == K_KDEL || c == K_BS)
5038 {
5039 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5040 (size_t)(len - i - 2));
5041 if (c == K_DEL || c == K_KDEL)
5042 ta_buf[i] = DEL;
5043 else
5044 ta_buf[i] = Ctrl_H;
5045 len -= 2;
5046 }
5047 }
5048 else if (ta_buf[i] == '\r')
5049 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005050# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005052 i += (*mb_ptr2len_len)(ta_buf + i,
5053 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056
5057 /*
5058 * For pipes: echo the typed characters.
5059 * For a pty this does not seem to work.
5060 */
5061 if (pty_master_fd < 0)
5062 {
5063 for (i = ta_len; i < ta_len + len; ++i)
5064 {
5065 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5066 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005067# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else if (has_mbyte)
5069 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005070 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 msg_outtrans_len(ta_buf + i, l);
5073 i += l - 1;
5074 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 else
5077 msg_outtrans_len(ta_buf + i, 1);
5078 }
5079 windgoto(msg_row, msg_col);
5080 out_flush();
5081 }
5082
5083 ta_len += len;
5084
5085 /*
5086 * Write the characters to the child, unless EOF has
5087 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005088 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005089 * When writing buffer lines, drop the typed
5090 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005092 if (options & SHELL_WRITE)
5093 ta_len = 0;
5094 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5097 if (len > 0)
5098 {
5099 ta_len -= len;
5100 mch_memmove(ta_buf, ta_buf + len, ta_len);
5101 }
5102 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105
Bram Moolenaardf177f62005-02-22 08:39:57 +00005106 if (got_int)
5107 {
5108 /* CTRL-C sends a signal to the child, we ignore it
5109 * ourselves */
5110# ifdef HAVE_SETSID
5111 kill(-pid, SIGINT);
5112# else
5113 kill(0, SIGINT);
5114# endif
5115 if (wpid > 0)
5116 kill(wpid, SIGINT);
5117 got_int = FALSE;
5118 }
5119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 /*
5121 * Check if the child has any characters to be printed.
5122 * Read them and write them to our window. Repeat this as
5123 * long as there is something to do, avoid the 10ms wait
5124 * for mch_inchar(), or sending typeahead characters to
5125 * the external process.
5126 * TODO: This should handle escape sequences, compatible
5127 * to some terminal (vt52?).
5128 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005129 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005130 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005132 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005133# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005135# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 );
5139 if (len <= 0) /* end of file or error */
5140 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005141
5142 noread_cnt = 0;
5143 if (options & SHELL_READ)
5144 {
5145 /* Do NUL -> NL translation, append NL separated
5146 * lines to the current buffer. */
5147 for (i = 0; i < len; ++i)
5148 {
5149 if (buffer[i] == NL)
5150 append_ga_line(&ga);
5151 else if (buffer[i] == NUL)
5152 ga_append(&ga, NL);
5153 else
5154 ga_append(&ga, buffer[i]);
5155 }
5156 }
5157# ifdef FEAT_MBYTE
5158 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
5160 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005161 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
Bram Moolenaardf177f62005-02-22 08:39:57 +00005163 len += buffer_off;
5164 buffer[len] = NUL;
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /* Check if the last character in buffer[] is
5167 * incomplete, keep these bytes for the next
5168 * round. */
5169 for (p = buffer; p < buffer + len; p += l)
5170 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005171 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (l == 0)
5173 l = 1; /* NUL byte? */
5174 else if (MB_BYTE2LEN(*p) != l)
5175 break;
5176 }
5177 if (p == buffer) /* no complete character */
5178 {
5179 /* avoid getting stuck at an illegal byte */
5180 if (len >= 12)
5181 ++p;
5182 else
5183 {
5184 buffer_off = len;
5185 continue;
5186 }
5187 }
5188 c = *p;
5189 *p = NUL;
5190 msg_puts(buffer);
5191 if (p < buffer + len)
5192 {
5193 *p = c;
5194 buffer_off = (buffer + len) - p;
5195 mch_memmove(buffer, p, buffer_off);
5196 continue;
5197 }
5198 buffer_off = 0;
5199 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005200# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 buffer[len] = NUL;
5204 msg_puts(buffer);
5205 }
5206
5207 windgoto(msg_row, msg_col);
5208 cursor_on();
5209 out_flush();
5210 if (got_int)
5211 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005212
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005213# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005214 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005215 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005216 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005217
5218 /* Avoid that we keep looping here without
5219 * checking for a CTRL-C for a long time. Don't
5220 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005221 if (msec > 2000)
5222 {
5223 noread_cnt = 5;
5224 break;
5225 }
5226 }
5227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005230 /* If we already detected the child has finished, continue
5231 * reading output for a short while. Some text may be
5232 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005233 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005234 {
5235 if (noread_cnt < 5)
5236 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005237 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005238 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 /*
5241 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005242 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005244# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005245 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005246# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5250 || (wait_pid == pid && WIFEXITED(status)))
5251 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005252 /* Don't break the loop yet, try reading more
5253 * characters from "fromshell_fd" first. When using
5254 * pipes there might still be something to read and
5255 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005258 else
5259 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005260
Bram Moolenaar95a51352013-03-21 22:53:50 +01005261# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005262 /* Handle any X events, e.g. serving the clipboard. */
5263 clip_update();
5264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266finished:
5267 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005268 if (options & SHELL_READ)
5269 {
5270 if (ga.ga_len > 0)
5271 {
5272 append_ga_line(&ga);
5273 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005274 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005275 }
5276 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005277 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005278 ga_clear(&ga);
5279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /*
5282 * Give all typeahead that wasn't used back to ui_inchar().
5283 */
5284 if (ta_len)
5285 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 State = old_State;
5287 if (toshell_fd >= 0)
5288 close(toshell_fd);
5289 close(fromshell_fd);
5290 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005291# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005292 else
5293 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005294 long delay_msec = 1;
5295
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005296 /*
5297 * Similar to the loop above, but only handle X events, no
5298 * I/O.
5299 */
5300 for (;;)
5301 {
5302 if (got_int)
5303 {
5304 /* CTRL-C sends a signal to the child, we ignore it
5305 * ourselves */
5306# ifdef HAVE_SETSID
5307 kill(-pid, SIGINT);
5308# else
5309 kill(0, SIGINT);
5310# endif
5311 got_int = FALSE;
5312 }
5313# ifdef __NeXT__
5314 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5315# else
5316 wait_pid = waitpid(pid, &status, WNOHANG);
5317# endif
5318 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5319 || (wait_pid == pid && WIFEXITED(status)))
5320 {
5321 wait_pid = pid;
5322 break;
5323 }
5324
5325 /* Handle any X events, e.g. serving the clipboard. */
5326 clip_update();
5327
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005328 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5329 * less time. */
5330 mch_delay(delay_msec, TRUE);
5331 if (++delay_msec > 10)
5332 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005333 }
5334 }
5335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 /*
5338 * Wait until our child has exited.
5339 * Ignore wait() returning pids of other children and returning
5340 * because of some signal like SIGWINCH.
5341 * Don't wait if wait_pid was already set above, indicating the
5342 * child already exited.
5343 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005344 if (wait_pid != pid)
5345 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
Bram Moolenaar624891f2010-10-13 16:22:09 +02005347# ifdef FEAT_GUI
5348 /* Close slave side of pty. Only do this after the child has
5349 * exited, otherwise the child may hang when it tries to write on
5350 * the pty. */
5351 if (pty_master_fd >= 0)
5352 close(pty_slave_fd);
5353# endif
5354
Bram Moolenaardf177f62005-02-22 08:39:57 +00005355 /* Make sure the child that writes to the external program is
5356 * dead. */
5357 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005358 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005359 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005360 wait4pid(wpid, NULL);
5361 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005362
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005363# ifdef FEAT_JOB_CHANNEL
5364 --dont_check_job_ended;
5365# endif
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 /*
5368 * Set to raw mode right now, otherwise a CTRL-C after
5369 * catch_signals() will kill Vim.
5370 */
5371 if (tmode == TMODE_RAW)
5372 settmode(TMODE_RAW);
5373 did_settmode = TRUE;
5374 set_signals();
5375
5376 if (WIFEXITED(status))
5377 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005378 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005380 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 if (retval == EXEC_FAILED)
5383 {
5384 MSG_PUTS(_("\nCannot execute shell "));
5385 msg_outtrans(p_sh);
5386 msg_putchar('\n');
5387 }
5388 else if (!(options & SHELL_SILENT))
5389 {
5390 MSG_PUTS(_("\nshell returned "));
5391 msg_outnum((long)retval);
5392 msg_putchar('\n');
5393 }
5394 }
5395 }
5396 else
5397 MSG_PUTS(_("\nCommand terminated\n"));
5398 }
5399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401error:
5402 if (!did_settmode)
5403 if (tmode == TMODE_RAW)
5404 settmode(TMODE_RAW); /* set to raw mode */
5405# ifdef FEAT_TITLE
5406 resettitle();
5407# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005408 vim_free(argv);
5409 vim_free(tofree1);
5410 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005415
5416 int
5417mch_call_shell(
5418 char_u *cmd,
5419 int options) /* SHELL_*, see vim.h */
5420{
5421#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5422 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5423 return mch_call_shell_terminal(cmd, options);
5424#endif
5425#ifdef USE_SYSTEM
5426 return mch_call_shell_system(cmd, options);
5427#else
5428 return mch_call_shell_fork(cmd, options);
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430}
5431
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005432#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005433 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005434mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005435{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005436 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005437 int fd_in[2] = {-1, -1}; /* for stdin */
5438 int fd_out[2] = {-1, -1}; /* for stdout */
5439 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005440 int pty_master_fd = -1;
5441 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005442 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005443 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5444 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5445 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005446 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005447 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5448 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005449 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005450 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005451 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005452
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005453 if (use_out_for_err && use_null_for_out)
5454 use_null_for_err = TRUE;
5455
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005456 /* default is to fail */
5457 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005458
Bram Moolenaarb2412082017-08-20 18:09:14 +02005459 if (options->jo_pty
5460 && (!(use_file_for_in || use_null_for_in)
5461 || !(use_file_for_in || use_null_for_out)
5462 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005463 {
5464 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5465 if (job->jv_tty_out != NULL)
5466 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5467 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005468
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005469 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005470 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005471 if (use_file_for_in)
5472 {
5473 char_u *fname = options->jo_io_name[PART_IN];
5474
5475 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5476 if (fd_in[0] < 0)
5477 {
5478 EMSG2(_(e_notopen), fname);
5479 goto failed;
5480 }
5481 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005482 else
5483 /* When writing buffer lines to the input don't use the pty, so that
5484 * the pipe can be closed when all lines were written. */
5485 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5486 && pipe(fd_in) < 0)
5487 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005488
5489 if (use_file_for_out)
5490 {
5491 char_u *fname = options->jo_io_name[PART_OUT];
5492
5493 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5494 if (fd_out[1] < 0)
5495 {
5496 EMSG2(_(e_notopen), fname);
5497 goto failed;
5498 }
5499 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005500 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005501 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005502
5503 if (use_file_for_err)
5504 {
5505 char_u *fname = options->jo_io_name[PART_ERR];
5506
5507 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5508 if (fd_err[1] < 0)
5509 {
5510 EMSG2(_(e_notopen), fname);
5511 goto failed;
5512 }
5513 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005514 else if (!use_out_for_err && !use_null_for_err
5515 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005516 goto failed;
5517
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005518 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5519 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005520 if (options->jo_set & JO_CHANNEL)
5521 {
5522 channel = options->jo_channel;
5523 if (channel != NULL)
5524 ++channel->ch_refcount;
5525 }
5526 else
5527 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005528 if (channel == NULL)
5529 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005530 if (job->jv_tty_out != NULL)
5531 ch_log(channel, "using pty %s on fd %d",
5532 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005533 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005534
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005535 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005536 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005537 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005538 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005539 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005540 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005541 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005542 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005543 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005544 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005545 int null_fd = -1;
5546 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005547
Bram Moolenaar835dc632016-02-07 14:27:38 +01005548 /* child */
5549 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005550 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005551
Bram Moolenaar819524702018-02-27 19:10:00 +01005552# ifdef FEAT_JOB_CHANNEL
5553 if (ch_log_active())
5554 /* close the log file in the child */
5555 ch_logfile((char_u *)"", (char_u *)"");
5556# endif
5557
Bram Moolenaar835dc632016-02-07 14:27:38 +01005558# ifdef HAVE_SETSID
5559 /* Create our own process group, so that the child and all its
5560 * children can be kill()ed. Don't do this when using pipes,
5561 * because stdin is not a tty, we would lose /dev/tty. */
5562 (void)setsid();
5563# endif
5564
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005565# ifdef FEAT_TERMINAL
5566 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005567 {
5568 char *term = (char *)T_NAME;
5569
5570#ifdef FEAT_GUI
5571 if (term_is_gui(T_NAME))
5572 /* In the GUI 'term' is not what we want, use $TERM. */
5573 term = getenv("TERM");
5574#endif
5575 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5576 * back to "xterm". */
5577 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5578 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005579 set_child_environment(
5580 (long)options->jo_term_rows,
5581 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005582 term,
5583 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005584 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005585 else
5586# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005587 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005588
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005589 if (options->jo_env != NULL)
5590 {
5591 dict_T *dict = options->jo_env;
5592 hashitem_T *hi;
5593 int todo = (int)dict->dv_hashtab.ht_used;
5594
5595 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5596 if (!HASHITEM_EMPTY(hi))
5597 {
5598 typval_T *item = &dict_lookup(hi)->di_tv;
5599
5600 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5601 --todo;
5602 }
5603 }
5604
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005606 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005607 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005608 if (null_fd < 0)
5609 {
5610 perror("opening /dev/null failed");
5611 _exit(OPEN_NULL_FAILED);
5612 }
5613 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005614
Bram Moolenaar223896d2017-08-02 22:33:28 +02005615 if (pty_slave_fd >= 0)
5616 {
5617 /* push stream discipline modules */
5618 SetupSlavePTY(pty_slave_fd);
5619# ifdef TIOCSCTTY
5620 /* Try to become controlling tty (probably doesn't work,
5621 * unless run by root) */
5622 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5623# endif
5624 }
5625
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005626 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005627 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005628 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005629 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005630 else if (fd_in[0] < 0)
5631 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005632 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005633 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005634
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005635 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005636 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005637 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005638 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005639 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005640 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005641 }
5642 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005643 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005644 else if (fd_err[1] < 0)
5645 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005646 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005647 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005648
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005649 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005650 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005651 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005652 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005653 else if (fd_out[1] < 0)
5654 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005655 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005656 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005657
5658 if (fd_in[0] >= 0)
5659 close(fd_in[0]);
5660 if (fd_in[1] >= 0)
5661 close(fd_in[1]);
5662 if (fd_out[0] >= 0)
5663 close(fd_out[0]);
5664 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005665 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005666 if (fd_err[0] >= 0)
5667 close(fd_err[0]);
5668 if (fd_err[1] >= 0)
5669 close(fd_err[1]);
5670 if (pty_master_fd >= 0)
5671 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005672 close(pty_master_fd); /* not used in the child */
5673 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005674 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005675
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005676 if (null_fd >= 0)
5677 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005678
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005679 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5680 _exit(EXEC_FAILED);
5681
Bram Moolenaar835dc632016-02-07 14:27:38 +01005682 /* See above for type of argv. */
5683 execvp(argv[0], argv);
5684
Bram Moolenaar4694a172016-04-21 14:05:23 +02005685 if (stderr_works)
5686 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005687# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005688 /* calling free_all_mem() here causes problems. Ignore valgrind
5689 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005690# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005691 _exit(EXEC_FAILED); /* exec failed, return failure code */
5692 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005693
5694 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005695 UNBLOCK_SIGNALS(&curset);
5696
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005697 job->jv_pid = pid;
5698 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005699 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005700
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005701 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005702 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005703 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005704 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005705 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005706 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005707 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005708 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005709 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005710 if (channel != NULL)
5711 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005712 int in_fd = use_file_for_in || use_null_for_in
5713 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5714 int out_fd = use_file_for_out || use_null_for_out
5715 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005716 /* When using pty_master_fd only set it for stdout, do not duplicate it
5717 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005718 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005719 ? INVALID_FD
5720 : fd_err[0] >= 0
5721 ? fd_err[0]
5722 : (out_fd == pty_master_fd
5723 ? INVALID_FD
5724 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005725
5726 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005727 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005728 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005729 else
5730 {
5731 if (fd_in[1] >= 0)
5732 close(fd_in[1]);
5733 if (fd_out[0] >= 0)
5734 close(fd_out[0]);
5735 if (fd_err[0] >= 0)
5736 close(fd_err[0]);
5737 if (pty_master_fd >= 0)
5738 close(pty_master_fd);
5739 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005740
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005741 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005742 return;
5743
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005744failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005745 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005746 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005747 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005748 if (fd_in[1] >= 0)
5749 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005750 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005752 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005753 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005754 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005755 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005756 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005757 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005758 if (pty_master_fd >= 0)
5759 close(pty_master_fd);
5760 if (pty_slave_fd >= 0)
5761 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005762}
5763
5764 char *
5765mch_job_status(job_T *job)
5766{
5767# ifdef HAVE_UNION_WAIT
5768 union wait status;
5769# else
5770 int status = -1;
5771# endif
5772 pid_t wait_pid = 0;
5773
5774# ifdef __NeXT__
5775 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5776# else
5777 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5778# endif
5779 if (wait_pid == -1)
5780 {
5781 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005782 if (job->jv_status < JOB_ENDED)
5783 ch_log(job->jv_channel, "Job no longer exists: %s",
5784 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005785 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005786 }
5787 if (wait_pid == 0)
5788 return "run";
5789 if (WIFEXITED(status))
5790 {
5791 /* LINTED avoid "bitwise operation on signed value" */
5792 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005793 if (job->jv_status < JOB_ENDED)
5794 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005795 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005796 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005797 if (WIFSIGNALED(status))
5798 {
5799 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005800 if (job->jv_status < JOB_ENDED)
5801 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005802 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005803 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005804 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005805
5806return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005807 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005808 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005809 return "dead";
5810}
5811
5812 job_T *
5813mch_detect_ended_job(job_T *job_list)
5814{
5815# ifdef HAVE_UNION_WAIT
5816 union wait status;
5817# else
5818 int status = -1;
5819# endif
5820 pid_t wait_pid = 0;
5821 job_T *job;
5822
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005823# ifndef USE_SYSTEM
5824 /* Do not do this when waiting for a shell command to finish, we would get
5825 * the exit value here (and discard it), the exit value obtained there
5826 * would then be wrong. */
5827 if (dont_check_job_ended > 0)
5828 return NULL;
5829# endif
5830
Bram Moolenaar97792de2016-10-15 18:36:49 +02005831# ifdef __NeXT__
5832 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5833# else
5834 wait_pid = waitpid(-1, &status, WNOHANG);
5835# endif
5836 if (wait_pid <= 0)
5837 /* no process ended */
5838 return NULL;
5839 for (job = job_list; job != NULL; job = job->jv_next)
5840 {
5841 if (job->jv_pid == wait_pid)
5842 {
5843 if (WIFEXITED(status))
5844 /* LINTED avoid "bitwise operation on signed value" */
5845 job->jv_exitval = WEXITSTATUS(status);
5846 else if (WIFSIGNALED(status))
5847 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005848 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005849 {
5850 ch_log(job->jv_channel, "Job ended");
5851 job->jv_status = JOB_ENDED;
5852 }
5853 return job;
5854 }
5855 }
5856 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005857}
5858
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005859/*
5860 * Send a (deadly) signal to "job".
5861 * Return FAIL if "how" is not a valid name.
5862 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005863 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005864mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005865{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005866 int sig = -1;
5867 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868
Bram Moolenaar923d9262016-02-25 20:56:01 +01005869 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005870 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005871 else if (STRCMP(how, "hup") == 0)
5872 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005873 else if (STRCMP(how, "quit") == 0)
5874 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005875 else if (STRCMP(how, "int") == 0)
5876 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005877 else if (STRCMP(how, "kill") == 0)
5878 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005879#ifdef SIGWINCH
5880 else if (STRCMP(how, "winch") == 0)
5881 sig = SIGWINCH;
5882#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005883 else if (isdigit(*how))
5884 sig = atoi((char *)how);
5885 else
5886 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005887
Bram Moolenaar72801402016-02-09 11:37:50 +01005888 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005889 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005890#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891 if (job_pid == getpgid(job_pid))
5892 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005893#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005894
Bram Moolenaar61a66052017-07-22 18:39:00 +02005895 /* Never kill ourselves! */
5896 if (job_pid != 0)
5897 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005898
Bram Moolenaar835dc632016-02-07 14:27:38 +01005899 return OK;
5900}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005901
5902/*
5903 * Clear the data related to "job".
5904 */
5905 void
5906mch_clear_job(job_T *job)
5907{
5908 /* call waitpid because child process may become zombie */
5909# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005910 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005911# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005912 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005913# endif
5914}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005915#endif
5916
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005917#if defined(FEAT_TERMINAL) || defined(PROTO)
5918 int
5919mch_create_pty_channel(job_T *job, jobopt_T *options)
5920{
5921 int pty_master_fd = -1;
5922 int pty_slave_fd = -1;
5923 channel_T *channel;
5924
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005925 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5926 if (job->jv_tty_out != NULL)
5927 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005928 close(pty_slave_fd);
5929
5930 channel = add_channel();
5931 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005932 {
5933 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005934 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005935 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005936 if (job->jv_tty_out != NULL)
5937 ch_log(channel, "using pty %s on fd %d",
5938 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005939 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5940 channel->ch_keep_open = TRUE;
5941
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005942 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5943 * it only needs to be read once. */
5944 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005945 channel_set_job(channel, job, options);
5946 return OK;
5947}
5948#endif
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950/*
5951 * Check for CTRL-C typed by reading all available characters.
5952 * In cooked mode we should get SIGINT, no need to check.
5953 */
5954 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005955mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005957 if ((curr_tmode == TMODE_RAW || force)
5958 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 fill_input_buf(FALSE);
5960}
5961
5962/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005963 * Wait "msec" msec until a character is available from the mouse, keyboard,
5964 * from inbuf[].
5965 * "msec" == -1 will block forever.
5966 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005967 * When "ignore_input" is TRUE even check for pending input when input is
5968 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005969 * "interrupted" (if not NULL) is set to TRUE when no character is available
5970 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005971 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005972 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 */
5974 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005975WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005977#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005978 return ui_wait_for_chars_or_timer(
5979 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005980#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005981 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005982#endif
5983}
5984
5985/*
5986 * Wait "msec" msec until a character is available from the mouse or keyboard
5987 * or from inbuf[].
5988 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005989 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005990 * "interrupted" (if not NULL) is set to TRUE when no character is available
5991 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005992 * When a GUI is being used, this will never get called -- webb
5993 */
5994 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005995WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005996{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997#ifdef FEAT_MOUSE_GPM
5998 int gpm_process_wanted;
5999#endif
6000#ifdef FEAT_XCLIPBOARD
6001 int rest;
6002#endif
6003 int avail;
6004
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006005 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 return 1;
6007
6008#if defined(FEAT_MOUSE_DEC)
6009 /* May need to query the mouse position. */
6010 if (WantQueryMouse)
6011 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006012 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
6014 }
6015#endif
6016
6017 /*
6018 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6019 * events. This is a bit complicated, because they might both be defined.
6020 */
6021#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6022# ifdef FEAT_XCLIPBOARD
6023 rest = 0;
6024 if (do_xterm_trace())
6025 rest = msec;
6026# endif
6027 do
6028 {
6029# ifdef FEAT_XCLIPBOARD
6030 if (rest != 0)
6031 {
6032 msec = XT_TRACE_DELAY;
6033 if (rest >= 0 && rest < XT_TRACE_DELAY)
6034 msec = rest;
6035 if (rest >= 0)
6036 rest -= msec;
6037 }
6038# endif
6039# ifdef FEAT_MOUSE_GPM
6040 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006041 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006042 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006044 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045# endif
6046 if (!avail)
6047 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006048 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 return 1;
6050# ifdef FEAT_XCLIPBOARD
6051 if (rest == 0 || !do_xterm_trace())
6052# endif
6053 break;
6054 }
6055 }
6056 while (FALSE
6057# ifdef FEAT_MOUSE_GPM
6058 || (gpm_process_wanted && mch_gpm_process() == 0)
6059# endif
6060# ifdef FEAT_XCLIPBOARD
6061 || (!avail && rest != 0)
6062# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006063 )
6064 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
6066#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006067 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068#endif
6069 return avail;
6070}
6071
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006072#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073/*
6074 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006075 * "msec" == 0 will check for characters once.
6076 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006079 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006080 * "interrupted" (if not NULL) is set to TRUE when no character is available
6081 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#if defined(__BEOS__)
6084 int
6085#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006086 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006088RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006091 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006092#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 static int busy = FALSE;
6094
6095 /* May retry getting characters after an event was handled. */
6096# define MAY_LOOP
6097
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006098# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 /* Remember at what time we started, so that we know how much longer we
6100 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006101 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006102 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006104 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006105 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106# endif
6107
6108 /* Handle being called recursively. This may happen for the session
6109 * manager stuff, it may save the file, which does a breakcheck. */
6110 if (busy)
6111 return 0;
6112#endif
6113
6114#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006115 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116#endif
6117 {
6118#ifdef MAY_LOOP
6119 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006120# ifdef FEAT_MZSCHEME
6121 int mzquantum_used = FALSE;
6122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123#endif
6124#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006125 /* each channel may use in, out and err */
6126 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 int nfd;
6128# ifdef FEAT_XCLIPBOARD
6129 int xterm_idx = -1;
6130# endif
6131# ifdef FEAT_MOUSE_GPM
6132 int gpm_idx = -1;
6133# endif
6134# ifdef USE_XSMP
6135 int xsmp_idx = -1;
6136# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006137 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006139# ifdef FEAT_MZSCHEME
6140 mzvim_check_threads();
6141 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6142 {
6143 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6144 mzquantum_used = TRUE;
6145 }
6146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 fds[0].fd = fd;
6148 fds[0].events = POLLIN;
6149 nfd = 1;
6150
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006152 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (xterm_Shell != (Widget)0)
6154 {
6155 xterm_idx = nfd;
6156 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6157 fds[nfd].events = POLLIN;
6158 nfd++;
6159 }
6160# endif
6161# ifdef FEAT_MOUSE_GPM
6162 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6163 {
6164 gpm_idx = nfd;
6165 fds[nfd].fd = gpm_fd;
6166 fds[nfd].events = POLLIN;
6167 nfd++;
6168 }
6169# endif
6170# ifdef USE_XSMP
6171 if (xsmp_icefd != -1)
6172 {
6173 xsmp_idx = nfd;
6174 fds[nfd].fd = xsmp_icefd;
6175 fds[nfd].events = POLLIN;
6176 nfd++;
6177 }
6178# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006179#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006180 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006181#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006182 if (interrupted != NULL)
6183 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006185 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006186
6187 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006188 if (result == 0 && interrupted != NULL && ret > 0)
6189 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006190
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006191# ifdef FEAT_MZSCHEME
6192 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006193 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006194 finished = FALSE;
6195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197# ifdef FEAT_XCLIPBOARD
6198 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6199 {
6200 xterm_update(); /* Maybe we should hand out clipboard */
6201 if (--ret == 0 && !input_available())
6202 /* Try again */
6203 finished = FALSE;
6204 }
6205# endif
6206# ifdef FEAT_MOUSE_GPM
6207 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6208 {
6209 *check_for_gpm = 1;
6210 }
6211# endif
6212# ifdef USE_XSMP
6213 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6214 {
6215 if (fds[xsmp_idx].revents & POLLIN)
6216 {
6217 busy = TRUE;
6218 xsmp_handle_requests();
6219 busy = FALSE;
6220 }
6221 else if (fds[xsmp_idx].revents & POLLHUP)
6222 {
6223 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006224 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 xsmp_close();
6226 }
6227 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
6230# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006231#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006232 /* also call when ret == 0, we may be polling a keep-open channel */
6233 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006234 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#else /* HAVE_SELECT */
6238
6239 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006240 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006241 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006243 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006245# ifdef FEAT_MZSCHEME
6246 mzvim_check_threads();
6247 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6248 {
6249 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6250 mzquantum_used = TRUE;
6251 }
6252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006254 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006256 tv.tv_sec = towait / 1000;
6257 tv.tv_usec = (towait % 1000) * (1000000/1000);
6258 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006260 else
6261 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263 /*
6264 * Select on ready for reading and exceptional condition (end of file).
6265 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006266select_eintr:
6267 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006268 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 FD_ZERO(&efds);
6270 FD_SET(fd, &rfds);
6271# if !defined(__QNX__) && !defined(__CYGWIN32__)
6272 /* For QNX select() always returns 1 if this is set. Why? */
6273 FD_SET(fd, &efds);
6274# endif
6275 maxfd = fd;
6276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006278 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (xterm_Shell != (Widget)0)
6280 {
6281 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6282 if (maxfd < ConnectionNumber(xterm_dpy))
6283 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006284
6285 /* An event may have already been read but not handled. In
6286 * particulary, XFlush may cause this. */
6287 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289# endif
6290# ifdef FEAT_MOUSE_GPM
6291 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6292 {
6293 FD_SET(gpm_fd, &rfds);
6294 FD_SET(gpm_fd, &efds);
6295 if (maxfd < gpm_fd)
6296 maxfd = gpm_fd;
6297 }
6298# endif
6299# ifdef USE_XSMP
6300 if (xsmp_icefd != -1)
6301 {
6302 FD_SET(xsmp_icefd, &rfds);
6303 FD_SET(xsmp_icefd, &efds);
6304 if (maxfd < xsmp_icefd)
6305 maxfd = xsmp_icefd;
6306 }
6307# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006308# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006309 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006310# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006311 if (interrupted != NULL)
6312 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006314 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006315 result = ret > 0 && FD_ISSET(fd, &rfds);
6316 if (result)
6317 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006318 else if (interrupted != NULL && ret > 0)
6319 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006320
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006321# ifdef EINTR
6322 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006323 {
6324 /* Check whether window has been resized, EINTR may be caused by
6325 * SIGWINCH. */
6326 if (do_resize)
6327 handle_resize();
6328
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006329 /* Interrupted by a signal, need to try again. We ignore msec
6330 * here, because we do want to check even after a timeout if
6331 * characters are available. Needed for reading output of an
6332 * external command after the process has finished. */
6333 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006334 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006335# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006336# ifdef __TANDEM
6337 if (ret == -1 && errno == ENOTSUP)
6338 {
6339 FD_ZERO(&rfds);
6340 FD_ZERO(&efds);
6341 ret = 0;
6342 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006343# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006344# ifdef FEAT_MZSCHEME
6345 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006346 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006347 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348# endif
6349
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350# ifdef FEAT_XCLIPBOARD
6351 if (ret > 0 && xterm_Shell != (Widget)0
6352 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6353 {
6354 xterm_update(); /* Maybe we should hand out clipboard */
6355 /* continue looping when we only got the X event and the input
6356 * buffer is empty */
6357 if (--ret == 0 && !input_available())
6358 {
6359 /* Try again */
6360 finished = FALSE;
6361 }
6362 }
6363# endif
6364# ifdef FEAT_MOUSE_GPM
6365 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6366 {
6367 if (FD_ISSET(gpm_fd, &efds))
6368 gpm_close();
6369 else if (FD_ISSET(gpm_fd, &rfds))
6370 *check_for_gpm = 1;
6371 }
6372# endif
6373# ifdef USE_XSMP
6374 if (ret > 0 && xsmp_icefd != -1)
6375 {
6376 if (FD_ISSET(xsmp_icefd, &efds))
6377 {
6378 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006379 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 xsmp_close();
6381 if (--ret == 0)
6382 finished = FALSE; /* keep going if event was only one */
6383 }
6384 else if (FD_ISSET(xsmp_icefd, &rfds))
6385 {
6386 busy = TRUE;
6387 xsmp_handle_requests();
6388 busy = FALSE;
6389 if (--ret == 0)
6390 finished = FALSE; /* keep going if event was only one */
6391 }
6392 }
6393# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006394#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006395 /* also call when ret == 0, we may be polling a keep-open channel */
6396 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006397 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400#endif /* HAVE_SELECT */
6401
6402#ifdef MAY_LOOP
6403 if (finished || msec == 0)
6404 break;
6405
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006406# ifdef FEAT_CLIENTSERVER
6407 if (server_waiting())
6408 break;
6409# endif
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 /* We're going to loop around again, find out for how long */
6412 if (msec > 0)
6413 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006414# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006416 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417# else
6418 /* Guess we got interrupted halfway. */
6419 msec = msec / 2;
6420# endif
6421 if (msec <= 0)
6422 break; /* waited long enough */
6423 }
6424#endif
6425 }
6426
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006427 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428}
6429
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006432 * Expand a path into all matching files and/or directories. Handles "*",
6433 * "?", "[a-z]", "**", etc.
6434 * "path" has backslashes before chars that are not to be expanded.
6435 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 */
6437 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006438mch_expandpath(
6439 garray_T *gap,
6440 char_u *path,
6441 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
Bram Moolenaar02743632005-07-25 20:42:36 +00006443 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444}
6445#endif
6446
6447/*
6448 * mch_expand_wildcards() - this code does wild-card pattern matching using
6449 * the shell
6450 *
6451 * return OK for success, FAIL for error (you may lose some memory) and put
6452 * an error message in *file.
6453 *
6454 * num_pat is number of input patterns
6455 * pat is array of pointers to input patterns
6456 * num_file is pointer to number of matched file names
6457 * file is pointer to array of pointers to matched file names
6458 */
6459
6460#ifndef SEEK_SET
6461# define SEEK_SET 0
6462#endif
6463#ifndef SEEK_END
6464# define SEEK_END 2
6465#endif
6466
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006467#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006468
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006470mch_expand_wildcards(
6471 int num_pat,
6472 char_u **pat,
6473 int *num_file,
6474 char_u ***file,
6475 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 int i;
6478 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006479 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 char_u *p;
6481 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaarc7247912008-01-13 12:54:11 +00006483 /*
6484 * This is the non-OS/2 implementation (really Unix).
6485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 int j;
6487 char_u *tempname;
6488 char_u *command;
6489 FILE *fd;
6490 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006491#define STYLE_ECHO 0 /* use "echo", the default */
6492#define STYLE_GLOB 1 /* use "glob", for csh */
6493#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6494#define STYLE_PRINT 3 /* use "print -N", for zsh */
6495#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6496 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 int shell_style = STYLE_ECHO;
6498 int check_spaces;
6499 static int did_find_nul = FALSE;
6500 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006501 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006502 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504 *num_file = 0; /* default: no files found */
6505 *file = NULL;
6506
6507 /*
6508 * If there are no wildcards, just copy the names to allocated memory.
6509 * Saves a lot of time, because we don't have to start a new shell.
6510 */
6511 if (!have_wildcard(num_pat, pat))
6512 return save_patterns(num_pat, pat, num_file, file);
6513
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006514# ifdef HAVE_SANDBOX
6515 /* Don't allow any shell command in the sandbox. */
6516 if (sandbox != 0 && check_secure())
6517 return FAIL;
6518# endif
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 /*
6521 * Don't allow the use of backticks in secure and restricted mode.
6522 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006523 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 for (i = 0; i < num_pat; ++i)
6525 if (vim_strchr(pat[i], '`') != NULL
6526 && (check_restricted() || check_secure()))
6527 return FAIL;
6528
6529 /*
6530 * get a name for the temp file
6531 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006532 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 EMSG(_(e_notmp));
6535 return FAIL;
6536 }
6537
6538 /*
6539 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006540 * file.
6541 * STYLE_BT: NL separated
6542 * If expanding `cmd` execute it directly.
6543 * STYLE_GLOB: NUL separated
6544 * If we use *csh, "glob" will work better than "echo".
6545 * STYLE_PRINT: NL or NUL separated
6546 * If we use *zsh, "print -N" will work better than "glob".
6547 * STYLE_VIMGLOB: NL separated
6548 * If we use *sh*, we define "vimglob()".
6549 * STYLE_ECHO: space separated.
6550 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 */
6552 if (num_pat == 1 && *pat[0] == '`'
6553 && (len = STRLEN(pat[0])) > 2
6554 && *(pat[0] + len - 1) == '`')
6555 shell_style = STYLE_BT;
6556 else if ((len = STRLEN(p_sh)) >= 3)
6557 {
6558 if (STRCMP(p_sh + len - 3, "csh") == 0)
6559 shell_style = STYLE_GLOB;
6560 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6561 shell_style = STYLE_PRINT;
6562 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006563 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6564 "sh") != NULL)
6565 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaarc7247912008-01-13 12:54:11 +00006567 /* Compute the length of the command. We need 2 extra bytes: for the
6568 * optional '&' and for the NUL.
6569 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006571 if (shell_style == STYLE_VIMGLOB)
6572 len += STRLEN(sh_vimglob_func);
6573
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006574 for (i = 0; i < num_pat; ++i)
6575 {
6576 /* Count the length of the patterns in the same way as they are put in
6577 * "command" below. */
6578#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006580#else
6581 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006582 for (j = 0; pat[i][j] != NUL; ++j)
6583 {
6584 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6585 ++len; /* may add a backslash */
6586 ++len;
6587 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006588#endif
6589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 command = alloc(len);
6591 if (command == NULL)
6592 {
6593 /* out of memory */
6594 vim_free(tempname);
6595 return FAIL;
6596 }
6597
6598 /*
6599 * Build the shell command:
6600 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6601 * recognizes this).
6602 * - Add the shell command to print the expanded names.
6603 * - Add the temp file name.
6604 * - Add the file name patterns.
6605 */
6606 if (shell_style == STYLE_BT)
6607 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006608 /* change `command; command& ` to (command; command ) */
6609 STRCPY(command, "(");
6610 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006612 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006613 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 --p;
6615 if (*p == '&') /* remove trailing '&' */
6616 {
6617 ampersent = TRUE;
6618 *p = ' ';
6619 }
6620 STRCAT(command, ">");
6621 }
6622 else
6623 {
6624 if (flags & EW_NOTFOUND)
6625 STRCPY(command, "set nonomatch; ");
6626 else
6627 STRCPY(command, "unset nonomatch; ");
6628 if (shell_style == STYLE_GLOB)
6629 STRCAT(command, "glob >");
6630 else if (shell_style == STYLE_PRINT)
6631 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006632 else if (shell_style == STYLE_VIMGLOB)
6633 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 else
6635 STRCAT(command, "echo >");
6636 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006637
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006639
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 if (shell_style != STYLE_BT)
6641 for (i = 0; i < num_pat; ++i)
6642 {
6643 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006644 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006645 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646#ifdef USE_SYSTEM
6647 STRCAT(command, " \"");
6648 STRCAT(command, pat[i]);
6649 STRCAT(command, "\"");
6650#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006651 int intick = FALSE;
6652
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 p = command + STRLEN(command);
6654 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006655 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006656 {
6657 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006658 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006659 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6660 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006661 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006662 * backslash inside backticks, before a special character
6663 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006664 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006665 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6666 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006667 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006668 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006669 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006670 else if (!intick
6671 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6672 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006673 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006674 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6675 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006676 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006677
6678 /* Copy one character. */
6679 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 *p = NUL;
6682#endif
6683 }
6684 if (flags & EW_SILENT)
6685 show_shell_mess = FALSE;
6686 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006687 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 /*
6690 * Using zsh -G: If a pattern has no matches, it is just deleted from
6691 * the argument list, otherwise zsh gives an error message and doesn't
6692 * expand any other pattern.
6693 */
6694 if (shell_style == STYLE_PRINT)
6695 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6696
6697 /*
6698 * If we use -f then shell variables set in .cshrc won't get expanded.
6699 * vi can do it, so we will too, but it is only necessary if there is a "$"
6700 * in one of the patterns, otherwise we can still use the fast option.
6701 */
6702 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6703 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6704
6705 /*
6706 * execute the shell command
6707 */
6708 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6709
6710 /* When running in the background, give it some time to create the temp
6711 * file, but don't wait for it to finish. */
6712 if (ampersent)
6713 mch_delay(10L, TRUE);
6714
6715 extra_shell_arg = NULL; /* cleanup */
6716 show_shell_mess = TRUE;
6717 vim_free(command);
6718
Bram Moolenaarc7247912008-01-13 12:54:11 +00006719 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 mch_remove(tempname);
6722 vim_free(tempname);
6723 /*
6724 * With interactive completion, the error message is not printed.
6725 * However with USE_SYSTEM, I don't know how to turn off error messages
6726 * from the shell, so screen may still get messed up -- webb.
6727 */
6728#ifndef USE_SYSTEM
6729 if (!(flags & EW_SILENT))
6730#endif
6731 {
6732 redraw_later_clear(); /* probably messed up screen */
6733 msg_putchar('\n'); /* clear bottom line quickly */
6734 cmdline_row = Rows - 1; /* continue on last line */
6735#ifdef USE_SYSTEM
6736 if (!(flags & EW_SILENT))
6737#endif
6738 {
6739 MSG(_(e_wildexpand));
6740 msg_start(); /* don't overwrite this message */
6741 }
6742 }
6743 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6744 * EW_NOTFOUND is given */
6745 if (shell_style == STYLE_BT)
6746 return FAIL;
6747 goto notfound;
6748 }
6749
6750 /*
6751 * read the names from the file into memory
6752 */
6753 fd = fopen((char *)tempname, READBIN);
6754 if (fd == NULL)
6755 {
6756 /* Something went wrong, perhaps a file name with a special char. */
6757 if (!(flags & EW_SILENT))
6758 {
6759 MSG(_(e_wildexpand));
6760 msg_start(); /* don't overwrite this message */
6761 }
6762 vim_free(tempname);
6763 goto notfound;
6764 }
6765 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006766 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006768 if (llen < 0)
6769 /* just in case ftell() would fail */
6770 buffer = NULL;
6771 else
6772 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (buffer == NULL)
6774 {
6775 /* out of memory */
6776 mch_remove(tempname);
6777 vim_free(tempname);
6778 fclose(fd);
6779 return FAIL;
6780 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006781 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 i = fread((char *)buffer, 1, len, fd);
6783 fclose(fd);
6784 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006785 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
6787 /* unexpected read error */
6788 EMSG2(_(e_notread), tempname);
6789 vim_free(tempname);
6790 vim_free(buffer);
6791 return FAIL;
6792 }
6793 vim_free(tempname);
6794
Bram Moolenaarc7247912008-01-13 12:54:11 +00006795# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6797 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006798 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6800 *p++ = buffer[i];
6801 len = p - buffer;
6802# endif
6803
6804
6805 /* file names are separated with Space */
6806 if (shell_style == STYLE_ECHO)
6807 {
6808 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6809 p = buffer;
6810 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6811 {
6812 while (*p != ' ' && *p != '\n')
6813 ++p;
6814 p = skipwhite(p); /* skip to next entry */
6815 }
6816 }
6817 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006818 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
6820 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6821 p = buffer;
6822 for (i = 0; *p != NUL; ++i) /* count number of entries */
6823 {
6824 while (*p != '\n' && *p != NUL)
6825 ++p;
6826 if (*p != NUL)
6827 ++p;
6828 p = skipwhite(p); /* skip leading white space */
6829 }
6830 }
6831 /* file names are separated with NUL */
6832 else
6833 {
6834 /*
6835 * Some versions of zsh use spaces instead of NULs to separate
6836 * results. Only do this when there is no NUL before the end of the
6837 * buffer, otherwise we would never be able to use file names with
6838 * embedded spaces when zsh does use NULs.
6839 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6840 * don't check for spaces again.
6841 */
6842 check_spaces = FALSE;
6843 if (shell_style == STYLE_PRINT && !did_find_nul)
6844 {
6845 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006846 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006847 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 did_find_nul = TRUE;
6849 else
6850 check_spaces = TRUE;
6851 }
6852
6853 /*
6854 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6855 * already is one, for STYLE_GLOB it needs to be added.
6856 */
6857 if (len && buffer[len - 1] == NUL)
6858 --len;
6859 else
6860 buffer[len] = NUL;
6861 i = 0;
6862 for (p = buffer; p < buffer + len; ++p)
6863 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6864 {
6865 ++i;
6866 *p = NUL;
6867 }
6868 if (len)
6869 ++i; /* count last entry */
6870 }
6871 if (i == 0)
6872 {
6873 /*
6874 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6875 * /bin/sh will happily expand it to nothing rather than returning an
6876 * error; and hey, it's good to check anyway -- webb.
6877 */
6878 vim_free(buffer);
6879 goto notfound;
6880 }
6881 *num_file = i;
6882 *file = (char_u **)alloc(sizeof(char_u *) * i);
6883 if (*file == NULL)
6884 {
6885 /* out of memory */
6886 vim_free(buffer);
6887 return FAIL;
6888 }
6889
6890 /*
6891 * Isolate the individual file names.
6892 */
6893 p = buffer;
6894 for (i = 0; i < *num_file; ++i)
6895 {
6896 (*file)[i] = p;
6897 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006898 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6899 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006901 while (!(shell_style == STYLE_ECHO && *p == ' ')
6902 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 ++p;
6904 if (p == buffer + len) /* last entry */
6905 *p = NUL;
6906 else
6907 {
6908 *p++ = NUL;
6909 p = skipwhite(p); /* skip to next entry */
6910 }
6911 }
6912 else /* NUL separates */
6913 {
6914 while (*p && p < buffer + len) /* skip entry */
6915 ++p;
6916 ++p; /* skip NUL */
6917 }
6918 }
6919
6920 /*
6921 * Move the file names to allocated memory.
6922 */
6923 for (j = 0, i = 0; i < *num_file; ++i)
6924 {
6925 /* Require the files to exist. Helps when using /bin/sh */
6926 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6927 continue;
6928
6929 /* check if this entry should be included */
6930 dir = (mch_isdir((*file)[i]));
6931 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6932 continue;
6933
Bram Moolenaara2031822006-03-07 22:29:51 +00006934 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006935 if (!dir && (flags & EW_EXEC)
6936 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006937 continue;
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6940 if (p)
6941 {
6942 STRCPY(p, (*file)[i]);
6943 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006944 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 (*file)[j++] = p;
6946 }
6947 }
6948 vim_free(buffer);
6949 *num_file = j;
6950
6951 if (*num_file == 0) /* rejected all entries */
6952 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006953 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 goto notfound;
6955 }
6956
6957 return OK;
6958
6959notfound:
6960 if (flags & EW_NOTFOUND)
6961 return save_patterns(num_pat, pat, num_file, file);
6962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963}
6964
6965#endif /* VMS */
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006968save_patterns(
6969 int num_pat,
6970 char_u **pat,
6971 int *num_file,
6972 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006975 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6978 if (*file == NULL)
6979 return FAIL;
6980 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006981 {
6982 s = vim_strsave(pat[i]);
6983 if (s != NULL)
6984 /* Be compatible with expand_filename(): halve the number of
6985 * backslashes. */
6986 backslash_halve(s);
6987 (*file)[i] = s;
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 *num_file = num_pat;
6990 return OK;
6991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993/*
6994 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6995 * expand.
6996 */
6997 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006998mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007000 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (*p == '\\' && p[1] != NUL)
7003 ++p;
7004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (vim_strchr((char_u *)
7006#ifdef VMS
7007 "*?%"
7008#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010#endif
7011 , *p) != NULL)
7012 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014 return FALSE;
7015}
7016
7017/*
7018 * Return TRUE if the string "p" contains a wildcard.
7019 * Don't recognize '~' at the end as a wildcard.
7020 */
7021 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007022mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007024 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (*p == '\\' && p[1] != NUL)
7027 ++p;
7028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (vim_strchr((char_u *)
7030#ifdef VMS
7031 "*?%$"
7032#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#endif
7035 , *p) != NULL
7036 || (*p == '~' && p[1] != NUL))
7037 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039 return FALSE;
7040}
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007043have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 int i;
7046
7047 for (i = 0; i < num; i++)
7048 if (mch_has_wildcard(file[i]))
7049 return 1;
7050 return 0;
7051}
7052
7053 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007054have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 int i;
7057
7058 for (i = 0; i < num; i++)
7059 if (vim_strchr(file[i], '$') != NULL)
7060 return TRUE;
7061 return FALSE;
7062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007064#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065/*
7066 * Scaled-down version of rename(), which is missing in Xenix.
7067 * This version can only move regular files and will fail if the
7068 * destination exists.
7069 */
7070 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007071mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 struct stat st;
7074
7075 if (stat(dest, &st) >= 0) /* fail if destination exists */
7076 return -1;
7077 if (link(src, dest) != 0) /* link file to new name */
7078 return -1;
7079 if (mch_remove(src) == 0) /* delete link to old name */
7080 return 0;
7081 return -1;
7082}
7083#endif /* !HAVE_RENAME */
7084
7085#ifdef FEAT_MOUSE_GPM
7086/*
7087 * Initializes connection with gpm (if it isn't already opened)
7088 * Return 1 if succeeded (or connection already opened), 0 if failed
7089 */
7090 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007091gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7094
7095 if (!gpm_flag)
7096 {
7097 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7098 gpm_connect.defaultMask = ~GPM_HARD;
7099 /* Default handling for mouse move*/
7100 gpm_connect.minMod = 0; /* Handle any modifier keys */
7101 gpm_connect.maxMod = 0xffff;
7102 if (Gpm_Open(&gpm_connect, 0) > 0)
7103 {
7104 /* gpm library tries to handling TSTP causes
7105 * problems. Anyways, we close connection to Gpm whenever
7106 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007107 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007109# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 return 1; /* succeed */
7113 }
7114 if (gpm_fd == -2)
7115 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7116 return 0;
7117 }
7118 return 1; /* already open */
7119}
7120
7121/*
7122 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 */
7124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007125gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 if (gpm_flag && gpm_fd >= 0) /* if Open */
7128 Gpm_Close();
7129}
7130
7131/* Reads gpm event and adds special keys to input buf. Returns length of
7132 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007133 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 */
7135 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007136mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
7138 int button;
7139 static Gpm_Event gpm_event;
7140 char_u string[6];
7141 int_u vim_modifiers;
7142 int row,col;
7143 unsigned char buttons_mask;
7144 unsigned char gpm_modifiers;
7145 static unsigned char old_buttons = 0;
7146
7147 Gpm_GetEvent(&gpm_event);
7148
7149#ifdef FEAT_GUI
7150 /* Don't put events in the input queue now. */
7151 if (hold_gui_events)
7152 return 0;
7153#endif
7154
7155 row = gpm_event.y - 1;
7156 col = gpm_event.x - 1;
7157
7158 string[0] = ESC; /* Our termcode */
7159 string[1] = 'M';
7160 string[2] = 'G';
7161 switch (GPM_BARE_EVENTS(gpm_event.type))
7162 {
7163 case GPM_DRAG:
7164 string[3] = MOUSE_DRAG;
7165 break;
7166 case GPM_DOWN:
7167 buttons_mask = gpm_event.buttons & ~old_buttons;
7168 old_buttons = gpm_event.buttons;
7169 switch (buttons_mask)
7170 {
7171 case GPM_B_LEFT:
7172 button = MOUSE_LEFT;
7173 break;
7174 case GPM_B_MIDDLE:
7175 button = MOUSE_MIDDLE;
7176 break;
7177 case GPM_B_RIGHT:
7178 button = MOUSE_RIGHT;
7179 break;
7180 default:
7181 return 0;
7182 /*Don't know what to do. Can more than one button be
7183 * reported in one event? */
7184 }
7185 string[3] = (char_u)(button | 0x20);
7186 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7187 break;
7188 case GPM_UP:
7189 string[3] = MOUSE_RELEASE;
7190 old_buttons &= ~gpm_event.buttons;
7191 break;
7192 default:
7193 return 0;
7194 }
7195 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7196 gpm_modifiers = gpm_event.modifiers;
7197 vim_modifiers = 0x0;
7198 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7199 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7200 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7201 */
7202 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7203 vim_modifiers |= MOUSE_SHIFT;
7204
7205 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7206 vim_modifiers |= MOUSE_CTRL;
7207 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7208 vim_modifiers |= MOUSE_ALT;
7209 string[3] |= vim_modifiers;
7210 string[4] = (char_u)(col + ' ' + 1);
7211 string[5] = (char_u)(row + ' ' + 1);
7212 add_to_input_buf(string, 6);
7213 return 6;
7214}
7215#endif /* FEAT_MOUSE_GPM */
7216
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007217#ifdef FEAT_SYSMOUSE
7218/*
7219 * Initialize connection with sysmouse.
7220 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7221 * output, any sysmouse output than will be processed via sig_sysmouse().
7222 * Return OK if succeeded, FAIL if failed.
7223 */
7224 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007225sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007226{
7227 struct mouse_info mouse;
7228
7229 mouse.operation = MOUSE_MODE;
7230 mouse.u.mode.mode = 0;
7231 mouse.u.mode.signal = SIGUSR2;
7232 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7233 {
7234 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7235 mouse.operation = MOUSE_SHOW;
7236 ioctl(1, CONS_MOUSECTL, &mouse);
7237 return OK;
7238 }
7239 return FAIL;
7240}
7241
7242/*
7243 * Stop processing SIGUSR2 signals, and also make sure that
7244 * virtual console do not send us any sysmouse related signal.
7245 */
7246 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007247sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007248{
7249 struct mouse_info mouse;
7250
7251 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7252 mouse.operation = MOUSE_MODE;
7253 mouse.u.mode.mode = 0;
7254 mouse.u.mode.signal = 0;
7255 ioctl(1, CONS_MOUSECTL, &mouse);
7256}
7257
7258/*
7259 * Gets info from sysmouse and adds special keys to input buf.
7260 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007261 static RETSIGTYPE
7262sig_sysmouse SIGDEFARG(sigarg)
7263{
7264 struct mouse_info mouse;
7265 struct video_info video;
7266 char_u string[6];
7267 int row, col;
7268 int button;
7269 int buttons;
7270 static int oldbuttons = 0;
7271
7272#ifdef FEAT_GUI
7273 /* Don't put events in the input queue now. */
7274 if (hold_gui_events)
7275 return;
7276#endif
7277
7278 mouse.operation = MOUSE_GETINFO;
7279 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7280 && ioctl(1, FBIO_MODEINFO, &video) != -1
7281 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7282 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7283 {
7284 row = mouse.u.data.y / video.vi_cheight;
7285 col = mouse.u.data.x / video.vi_cwidth;
7286 buttons = mouse.u.data.buttons;
7287 string[0] = ESC; /* Our termcode */
7288 string[1] = 'M';
7289 string[2] = 'S';
7290 if (oldbuttons == buttons && buttons != 0)
7291 {
7292 button = MOUSE_DRAG;
7293 }
7294 else
7295 {
7296 switch (buttons)
7297 {
7298 case 0:
7299 button = MOUSE_RELEASE;
7300 break;
7301 case 1:
7302 button = MOUSE_LEFT;
7303 break;
7304 case 2:
7305 button = MOUSE_MIDDLE;
7306 break;
7307 case 4:
7308 button = MOUSE_RIGHT;
7309 break;
7310 default:
7311 return;
7312 }
7313 oldbuttons = buttons;
7314 }
7315 string[3] = (char_u)(button);
7316 string[4] = (char_u)(col + ' ' + 1);
7317 string[5] = (char_u)(row + ' ' + 1);
7318 add_to_input_buf(string, 6);
7319 }
7320 return;
7321}
7322#endif /* FEAT_SYSMOUSE */
7323
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007325typedef char_u * (*STRPROCSTR)(char_u *);
7326typedef char_u * (*INTPROCSTR)(int);
7327typedef int (*STRPROCINT)(char_u *);
7328typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
7330/*
7331 * Call a DLL routine which takes either a string or int param
7332 * and returns an allocated string.
7333 */
7334 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007335mch_libcall(
7336 char_u *libname,
7337 char_u *funcname,
7338 char_u *argstring, /* NULL when using a argint */
7339 int argint,
7340 char_u **string_result,/* NULL when using number_result */
7341 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343# if defined(USE_DLOPEN)
7344 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007345 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# else
7347 shl_t hinstLib;
7348# endif
7349 STRPROCSTR ProcAdd;
7350 INTPROCSTR ProcAddI;
7351 char_u *retval_str = NULL;
7352 int retval_int = 0;
7353 int success = FALSE;
7354
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007355 /*
7356 * Get a handle to the DLL module.
7357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007359 /* First clear any error, it's not cleared by the dlopen() call. */
7360 (void)dlerror();
7361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 hinstLib = dlopen((char *)libname, RTLD_LAZY
7363# ifdef RTLD_LOCAL
7364 | RTLD_LOCAL
7365# endif
7366 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007367 if (hinstLib == NULL)
7368 {
7369 /* "dlerr" must be used before dlclose() */
7370 dlerr = (char *)dlerror();
7371 if (dlerr != NULL)
7372 EMSG2(_("dlerror = \"%s\""), dlerr);
7373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374# else
7375 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7376# endif
7377
7378 /* If the handle is valid, try to get the function address. */
7379 if (hinstLib != NULL)
7380 {
7381# ifdef HAVE_SETJMP_H
7382 /*
7383 * Catch a crash when calling the library function. For example when
7384 * using a number where a string pointer is expected.
7385 */
7386 mch_startjmp();
7387 if (SETJMP(lc_jump_env) != 0)
7388 {
7389 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007390# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007391 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 mch_didjmp();
7394 }
7395 else
7396# endif
7397 {
7398 retval_str = NULL;
7399 retval_int = 0;
7400
7401 if (argstring != NULL)
7402 {
7403# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007404 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007405 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406# else
7407 if (shl_findsym(&hinstLib, (const char *)funcname,
7408 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7409 ProcAdd = NULL;
7410# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007411 if ((success = (ProcAdd != NULL
7412# if defined(USE_DLOPEN)
7413 && dlerr == NULL
7414# endif
7415 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 {
7417 if (string_result == NULL)
7418 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7419 else
7420 retval_str = (ProcAdd)(argstring);
7421 }
7422 }
7423 else
7424 {
7425# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007426 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007427 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428# else
7429 if (shl_findsym(&hinstLib, (const char *)funcname,
7430 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7431 ProcAddI = NULL;
7432# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007433 if ((success = (ProcAddI != NULL
7434# if defined(USE_DLOPEN)
7435 && dlerr == NULL
7436# endif
7437 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 {
7439 if (string_result == NULL)
7440 retval_int = ((INTPROCINT)ProcAddI)(argint);
7441 else
7442 retval_str = (ProcAddI)(argint);
7443 }
7444 }
7445
7446 /* Save the string before we free the library. */
7447 /* Assume that a "1" or "-1" result is an illegal pointer. */
7448 if (string_result == NULL)
7449 *number_result = retval_int;
7450 else if (retval_str != NULL
7451 && retval_str != (char_u *)1
7452 && retval_str != (char_u *)-1)
7453 *string_result = vim_strsave(retval_str);
7454 }
7455
7456# ifdef HAVE_SETJMP_H
7457 mch_endjmp();
7458# ifdef SIGHASARG
7459 if (lc_signal != 0)
7460 {
7461 int i;
7462
7463 /* try to find the name of this signal */
7464 for (i = 0; signal_info[i].sig != -1; i++)
7465 if (lc_signal == signal_info[i].sig)
7466 break;
7467 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7468 }
7469# endif
7470# endif
7471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007473 /* "dlerr" must be used before dlclose() */
7474 if (dlerr != NULL)
7475 EMSG2(_("dlerror = \"%s\""), dlerr);
7476
7477 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 (void)dlclose(hinstLib);
7479# else
7480 (void)shl_unload(hinstLib);
7481# endif
7482 }
7483
7484 if (!success)
7485 {
7486 EMSG2(_(e_libcall), funcname);
7487 return FAIL;
7488 }
7489
7490 return OK;
7491}
7492#endif
7493
7494#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7495static int xterm_trace = -1; /* default: disabled */
7496static int xterm_button;
7497
7498/*
7499 * Setup a dummy window for X selections in a terminal.
7500 */
7501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007502setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
7504 int z = 0;
7505 char *strp = "";
7506 Widget AppShell;
7507
7508 if (!x_connect_to_server())
7509 return;
7510
7511 open_app_context();
7512 if (app_context != NULL && xterm_Shell == (Widget)0)
7513 {
7514 int (*oldhandler)();
7515#if defined(HAVE_SETJMP_H)
7516 int (*oldIOhandler)();
7517#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007518# ifdef ELAPSED_FUNC
7519 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007522 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523# endif
7524
7525 /* Ignore X errors while opening the display */
7526 oldhandler = XSetErrorHandler(x_error_check);
7527
7528#if defined(HAVE_SETJMP_H)
7529 /* Ignore X IO errors while opening the display */
7530 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7531 mch_startjmp();
7532 if (SETJMP(lc_jump_env) != 0)
7533 {
7534 mch_didjmp();
7535 xterm_dpy = NULL;
7536 }
7537 else
7538#endif
7539 {
7540 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7541 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7542#if defined(HAVE_SETJMP_H)
7543 mch_endjmp();
7544#endif
7545 }
7546
7547#if defined(HAVE_SETJMP_H)
7548 /* Now handle X IO errors normally. */
7549 (void)XSetIOErrorHandler(oldIOhandler);
7550#endif
7551 /* Now handle X errors normally. */
7552 (void)XSetErrorHandler(oldhandler);
7553
7554 if (xterm_dpy == NULL)
7555 {
7556 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007557 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 return;
7559 }
7560
7561 /* Catch terminating error of the X server connection. */
7562 (void)XSetIOErrorHandler(x_IOerror_handler);
7563
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007564# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007566 {
7567 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007568 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007569 verbose_leave();
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571# endif
7572
7573 /* Create a Shell to make converters work. */
7574 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7575 applicationShellWidgetClass, xterm_dpy,
7576 NULL);
7577 if (AppShell == (Widget)0)
7578 return;
7579 xterm_Shell = XtVaCreatePopupShell("VIM",
7580 topLevelShellWidgetClass, AppShell,
7581 XtNmappedWhenManaged, 0,
7582 XtNwidth, 1,
7583 XtNheight, 1,
7584 NULL);
7585 if (xterm_Shell == (Widget)0)
7586 return;
7587
7588 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007589 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 if (x11_display == NULL)
7591 x11_display = xterm_dpy;
7592
7593 XtRealizeWidget(xterm_Shell);
7594 XSync(xterm_dpy, False);
7595 xterm_update();
7596 }
7597 if (xterm_Shell != (Widget)0)
7598 {
7599 clip_init(TRUE);
7600 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7601 x11_window = (Window)atol(strp);
7602 /* Check if $WINDOWID is valid. */
7603 if (test_x11_window(xterm_dpy) == FAIL)
7604 x11_window = 0;
7605 if (x11_window != 0)
7606 xterm_trace = 0;
7607 }
7608}
7609
7610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007611start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7614 return;
7615 xterm_trace = 1;
7616 xterm_button = button;
7617 do_xterm_trace();
7618}
7619
7620
7621 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007622stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 if (xterm_trace < 0)
7625 return;
7626 xterm_trace = 0;
7627}
7628
7629/*
7630 * Query the xterm pointer and generate mouse termcodes if necessary
7631 * return TRUE if dragging is active, else FALSE
7632 */
7633 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007634do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 Window root, child;
7637 int root_x, root_y;
7638 int win_x, win_y;
7639 int row, col;
7640 int_u mask_return;
7641 char_u buf[50];
7642 char_u *strp;
7643 long got_hints;
7644 static char_u *mouse_code;
7645 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7646 static int prev_row = 0, prev_col = 0;
7647 static XSizeHints xterm_hints;
7648
7649 if (xterm_trace <= 0)
7650 return FALSE;
7651
7652 if (xterm_trace == 1)
7653 {
7654 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007655 * have changed recently. */
7656 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7657 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 || xterm_hints.width_inc <= 1
7659 || xterm_hints.height_inc <= 1)
7660 {
7661 xterm_trace = -1; /* Not enough data -- disable tracing */
7662 return FALSE;
7663 }
7664
7665 /* Rely on the same mouse code for the duration of this */
7666 mouse_code = find_termcode(mouse_name);
7667 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007668 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 xterm_trace = 2;
7670
7671 /* Find the offset of the chars, there might be a scrollbar on the
7672 * left of the window and/or a menu on the top (eterm etc.) */
7673 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7674 &win_x, &win_y, &mask_return);
7675 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7676 - (xterm_hints.height_inc / 2);
7677 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7678 xterm_hints.y = 2;
7679 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7680 - (xterm_hints.width_inc / 2);
7681 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7682 xterm_hints.x = 2;
7683 return TRUE;
7684 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007685 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
7687 xterm_trace = 0;
7688 return FALSE;
7689 }
7690
7691 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7692 &win_x, &win_y, &mask_return);
7693
7694 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7695 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7696 if (row == prev_row && col == prev_col)
7697 return TRUE;
7698
7699 STRCPY(buf, mouse_code);
7700 strp = buf + STRLEN(buf);
7701 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7702 *strp++ = (char_u)(col + ' ' + 1);
7703 *strp++ = (char_u)(row + ' ' + 1);
7704 *strp = 0;
7705 add_to_input_buf(buf, STRLEN(buf));
7706
7707 prev_row = row;
7708 prev_col = col;
7709 return TRUE;
7710}
7711
7712# if defined(FEAT_GUI) || defined(PROTO)
7713/*
7714 * Destroy the display, window and app_context. Required for GTK.
7715 */
7716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007717clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 if (xterm_Shell != (Widget)0)
7720 {
7721 XtDestroyWidget(xterm_Shell);
7722 xterm_Shell = (Widget)0;
7723 }
7724 if (xterm_dpy != NULL)
7725 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007726# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 /* Lesstif and Solaris crash here, lose some memory */
7728 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 if (x11_display == xterm_dpy)
7731 x11_display = NULL;
7732 xterm_dpy = NULL;
7733 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007734# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (app_context != (XtAppContext)NULL)
7736 {
7737 /* Lesstif and Solaris crash here, lose some memory */
7738 XtDestroyApplicationContext(app_context);
7739 app_context = (XtAppContext)NULL;
7740 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743# endif
7744
7745/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007746 * Catch up with GUI or X events.
7747 */
7748 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007749clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007750{
7751# ifdef FEAT_GUI
7752 if (gui.in_use)
7753 gui_mch_update();
7754 else
7755# endif
7756 if (xterm_Shell != (Widget)0)
7757 xterm_update();
7758}
7759
7760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 * Catch up with any queued X events. This may put keyboard input into the
7762 * input buffer, call resize call-backs, trigger timers etc. If there is
7763 * nothing in the X event queue (& no timers pending), then we return
7764 * immediately.
7765 */
7766 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007767xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 XEvent event;
7770
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007771 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007773 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007775 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007776 break;
7777
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007778 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007779 {
7780 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007781 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007782#ifdef FEAT_CLIENTSERVER
7783 {
7784 XPropertyEvent *e = (XPropertyEvent *)&event;
7785
7786 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007788 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007791 XtDispatchEvent(&event);
7792 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007793 else
7794 {
7795 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007796 XtAppProcessEvent(app_context, mask);
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799}
7800
7801 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007802clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 if (xterm_Shell != (Widget)0)
7805 return clip_x11_own_selection(xterm_Shell, cbd);
7806 return FAIL;
7807}
7808
7809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 if (xterm_Shell != (Widget)0)
7813 clip_x11_lose_selection(xterm_Shell, cbd);
7814}
7815
7816 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007817clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 if (xterm_Shell != (Widget)0)
7820 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7821}
7822
7823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007824clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 clip_x11_set_selection(cbd);
7827}
7828#endif
7829
7830
7831#if defined(USE_XSMP) || defined(PROTO)
7832/*
7833 * Code for X Session Management Protocol.
7834 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007835static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7836static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7837static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7838static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7839static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
7841
7842# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007843static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845/*
7846 * This is our chance to ask the user if they want to save,
7847 * or abort the logout
7848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007850xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 cmdmod_T save_cmdmod;
7853 int cancel_shutdown = False;
7854
7855 save_cmdmod = cmdmod;
7856 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007857 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 /* Mustn't logout */
7859 cancel_shutdown = True;
7860 cmdmod = save_cmdmod;
7861 setcursor(); /* position cursor */
7862 out_flush();
7863
7864 /* Done interaction */
7865 SmcInteractDone(smc_conn, cancel_shutdown);
7866
7867 /* Finish off
7868 * Only end save-yourself here if we're not cancelling shutdown;
7869 * we'll get a cancelled callback later in which we'll end it.
7870 * Hopefully get around glitchy SMs (like GNOME-1)
7871 */
7872 if (!cancel_shutdown)
7873 {
7874 xsmp.save_yourself = False;
7875 SmcSaveYourselfDone(smc_conn, True);
7876 }
7877}
7878# endif
7879
7880/*
7881 * Callback that starts save-yourself.
7882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007884xsmp_handle_save_yourself(
7885 SmcConn smc_conn,
7886 SmPointer client_data UNUSED,
7887 int save_type UNUSED,
7888 Bool shutdown,
7889 int interact_style UNUSED,
7890 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 /* Handle already being in saveyourself */
7893 if (xsmp.save_yourself)
7894 SmcSaveYourselfDone(smc_conn, True);
7895 xsmp.save_yourself = True;
7896 xsmp.shutdown = shutdown;
7897
7898 /* First up, preserve all files */
7899 out_flush();
7900 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7901
7902 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007903 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
7905# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7906 /* Now see if we can ask about unsaved files */
7907 if (shutdown && !fast && gui.in_use)
7908 /* Need to interact with user, but need SM's permission */
7909 SmcInteractRequest(smc_conn, SmDialogError,
7910 xsmp_handle_interaction, client_data);
7911 else
7912# endif
7913 {
7914 /* Can stop the cycle here */
7915 SmcSaveYourselfDone(smc_conn, True);
7916 xsmp.save_yourself = False;
7917 }
7918}
7919
7920
7921/*
7922 * Callback to warn us of imminent death.
7923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007925xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 xsmp_close();
7928
7929 /* quit quickly leaving swapfiles for modified buffers behind */
7930 getout_preserve_modified(0);
7931}
7932
7933
7934/*
7935 * Callback to tell us that save-yourself has completed.
7936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007938xsmp_save_complete(
7939 SmcConn smc_conn UNUSED,
7940 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
7942 xsmp.save_yourself = False;
7943}
7944
7945
7946/*
7947 * Callback to tell us that an instigated shutdown was cancelled
7948 * (maybe even by us)
7949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007951xsmp_shutdown_cancelled(
7952 SmcConn smc_conn,
7953 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 if (xsmp.save_yourself)
7956 SmcSaveYourselfDone(smc_conn, True);
7957 xsmp.save_yourself = False;
7958 xsmp.shutdown = False;
7959}
7960
7961
7962/*
7963 * Callback to tell us that a new ICE connection has been established.
7964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007966xsmp_ice_connection(
7967 IceConn iceConn,
7968 IcePointer clientData UNUSED,
7969 Bool opening,
7970 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971{
7972 /* Intercept creation of ICE connection fd */
7973 if (opening)
7974 {
7975 xsmp_icefd = IceConnectionNumber(iceConn);
7976 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7977 }
7978}
7979
7980
7981/* Handle any ICE processing that's required; return FAIL if SM lost */
7982 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007983xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985 Bool rep;
7986
7987 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7988 == IceProcessMessagesIOError)
7989 {
7990 /* Lost ICE */
7991 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007992 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 xsmp_close();
7994 return FAIL;
7995 }
7996 else
7997 return OK;
7998}
7999
8000static int dummy;
8001
8002/* Set up X Session Management Protocol */
8003 void
8004xsmp_init(void)
8005{
8006 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 SmcCallbacks smcallbacks;
8008#if 0
8009 SmPropValue smname;
8010 SmProp smnameprop;
8011 SmProp *smprops[1];
8012#endif
8013
8014 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008015 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 xsmp.save_yourself = xsmp.shutdown = False;
8018
8019 /* Set up SM callbacks - must have all, even if they're not used */
8020 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8021 smcallbacks.save_yourself.client_data = NULL;
8022 smcallbacks.die.callback = xsmp_die;
8023 smcallbacks.die.client_data = NULL;
8024 smcallbacks.save_complete.callback = xsmp_save_complete;
8025 smcallbacks.save_complete.client_data = NULL;
8026 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8027 smcallbacks.shutdown_cancelled.client_data = NULL;
8028
8029 /* Set up a watch on ICE connection creations. The "dummy" argument is
8030 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8031 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8032 {
8033 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008034 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 return;
8036 }
8037
8038 /* Create an SM connection */
8039 xsmp.smcconn = SmcOpenConnection(
8040 NULL,
8041 NULL,
8042 SmProtoMajor,
8043 SmProtoMinor,
8044 SmcSaveYourselfProcMask | SmcDieProcMask
8045 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8046 &smcallbacks,
8047 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008048 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 sizeof(errorstring),
8050 errorstring);
8051 if (xsmp.smcconn == NULL)
8052 {
8053 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008054
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008056 {
8057 vim_snprintf(errorreport, sizeof(errorreport),
8058 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8059 verb_msg((char_u *)errorreport);
8060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 return;
8062 }
8063 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8064
8065#if 0
8066 /* ID ourselves */
8067 smname.value = "vim";
8068 smname.length = 3;
8069 smnameprop.name = "SmProgram";
8070 smnameprop.type = "SmARRAY8";
8071 smnameprop.num_vals = 1;
8072 smnameprop.vals = &smname;
8073
8074 smprops[0] = &smnameprop;
8075 SmcSetProperties(xsmp.smcconn, 1, smprops);
8076#endif
8077}
8078
8079
8080/* Shut down XSMP comms. */
8081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008082xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 if (xsmp_icefd != -1)
8085 {
8086 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008087 if (xsmp.clientid != NULL)
8088 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008089 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 xsmp_icefd = -1;
8091 }
8092}
8093#endif /* USE_XSMP */
8094
8095
8096#ifdef EBCDIC
8097/* Translate character to its CTRL- value */
8098char CtrlTable[] =
8099{
8100/* 00 - 5E */
8101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8107/* ^ */ 0x1E,
8108/* - */ 0x1F,
8109/* 61 - 6C */
8110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8111/* _ */ 0x1F,
8112/* 6E - 80 */
8113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8114/* a */ 0x01,
8115/* b */ 0x02,
8116/* c */ 0x03,
8117/* d */ 0x37,
8118/* e */ 0x2D,
8119/* f */ 0x2E,
8120/* g */ 0x2F,
8121/* h */ 0x16,
8122/* i */ 0x05,
8123/* 8A - 90 */
8124 0, 0, 0, 0, 0, 0, 0,
8125/* j */ 0x15,
8126/* k */ 0x0B,
8127/* l */ 0x0C,
8128/* m */ 0x0D,
8129/* n */ 0x0E,
8130/* o */ 0x0F,
8131/* p */ 0x10,
8132/* q */ 0x11,
8133/* r */ 0x12,
8134/* 9A - A1 */
8135 0, 0, 0, 0, 0, 0, 0, 0,
8136/* s */ 0x13,
8137/* t */ 0x3C,
8138/* u */ 0x3D,
8139/* v */ 0x32,
8140/* w */ 0x26,
8141/* x */ 0x18,
8142/* y */ 0x19,
8143/* z */ 0x3F,
8144/* AA - AC */
8145 0, 0, 0,
8146/* [ */ 0x27,
8147/* AE - BC */
8148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8149/* ] */ 0x1D,
8150/* BE - C0 */ 0, 0, 0,
8151/* A */ 0x01,
8152/* B */ 0x02,
8153/* C */ 0x03,
8154/* D */ 0x37,
8155/* E */ 0x2D,
8156/* F */ 0x2E,
8157/* G */ 0x2F,
8158/* H */ 0x16,
8159/* I */ 0x05,
8160/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8161/* J */ 0x15,
8162/* K */ 0x0B,
8163/* L */ 0x0C,
8164/* M */ 0x0D,
8165/* N */ 0x0E,
8166/* O */ 0x0F,
8167/* P */ 0x10,
8168/* Q */ 0x11,
8169/* R */ 0x12,
8170/* DA - DF */ 0, 0, 0, 0, 0, 0,
8171/* \ */ 0x1C,
8172/* E1 */ 0,
8173/* S */ 0x13,
8174/* T */ 0x3C,
8175/* U */ 0x3D,
8176/* V */ 0x32,
8177/* W */ 0x26,
8178/* X */ 0x18,
8179/* Y */ 0x19,
8180/* Z */ 0x3F,
8181/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8182 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8183};
8184
8185char MetaCharTable[]=
8186{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8187 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8188 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8189 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8190 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8191};
8192
8193
8194/* TODO: Use characters NOT numbers!!! */
8195char CtrlCharTable[]=
8196{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8197 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8198 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8199 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8200 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8201};
8202
8203
8204#endif