blob: ec12f3ea7ba8fbe78b8869c90f50383cdc519aef [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#include "os_unixx.h" /* unix includes for os_unix.c only */
28
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaar643b6142018-09-12 20:29:09 +020046#ifdef __BEOS__
Bram Moolenaar311d9822007-02-27 15:48:28 +000047# undef select
Bram Moolenaar643b6142018-09-12 20:29:09 +020048# define select beos_select
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
Bram Moolenaara2442432007-04-26 14:26:37 +000051#ifdef __CYGWIN__
52# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000053# include <cygwin/version.h>
54# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
55 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010056# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
57# define WIN32_LEAN_AND_MEAN
58# include <windows.h>
59# include "winclip.pro"
60# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000061# endif
62#endif
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_MOUSE_GPM
65# include <gpm.h>
66/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
67 * I just copied relevant defines here. A cleaner solution would be to put gpm
68 * code into separate file and include there linux/keyboard.h
69 */
70/* #include <linux/keyboard.h> */
71# define KG_SHIFT 0
72# define KG_CTRL 2
73# define KG_ALT 3
74# define KG_ALTGR 1
75# define KG_SHIFTL 4
76# define KG_SHIFTR 5
77# define KG_CTRLL 6
78# define KG_CTRLR 7
79# define KG_CAPSSHIFT 8
80
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static void gpm_close(void);
82static int gpm_open(void);
83static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
85
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000086#ifdef FEAT_SYSMOUSE
87# include <sys/consio.h>
88# include <sys/fbio.h>
89
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int sysmouse_open(void);
91static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010092static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000093#endif
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * end of autoconf section. To be extended...
97 */
98
99/* Are the following #ifdefs still required? And why? Is that for X11? */
100
101#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
102# ifdef SIGWINCH
103# undef SIGWINCH
104# endif
105# ifdef TIOCGWINSZ
106# undef TIOCGWINSZ
107# endif
108#endif
109
110#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
111# define SIGWINCH SIGWINDOW
112#endif
113
114#ifdef FEAT_X11
115# include <X11/Xlib.h>
116# include <X11/Xutil.h>
117# include <X11/Xatom.h>
118# ifdef FEAT_XCLIPBOARD
119# include <X11/Intrinsic.h>
120# include <X11/Shell.h>
121# include <X11/StringDefs.h>
122static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void clip_update(void);
124static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# endif
126
127# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
128Window x11_window = 0;
129# endif
130Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
133#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200137static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static int did_set_title = FALSE;
139static char_u *oldicon = NULL;
140static int did_set_icon = FALSE;
141#endif
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar205b8862011-09-07 15:04:31 +0200145#ifdef HAVE_UNION_WAIT
146typedef union wait waitstatus;
147#else
148typedef int waitstatus;
149#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200150static int WaitForChar(long msec, int *interrupted, int ignore_input);
151static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100152#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200153int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200155static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157
158#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000160# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#endif
162
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
165#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100166static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100169static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100172static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174#if defined(SIGALRM) && defined(FEAT_X11) \
175 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
176# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100177static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000178/* volatile because it is used in signal handler sig_alarm(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200179static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100181static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static void catch_int_signal(void);
184static void set_signals(void);
185static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200186#ifdef HAVE_SIGPROCMASK
187# define SIGSET_DECL(set) sigset_t set;
188# define BLOCK_SIGNALS(set) block_signals(set)
189# define UNBLOCK_SIGNALS(set) unblock_signals(set)
190#else
191# define SIGSET_DECL(set)
192# define BLOCK_SIGNALS(set) do { /**/ } while (0)
193# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
194#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100195static int have_wildcard(int, char_u **);
196static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100198static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000201# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000204/* volatile because it is used in signal handler sig_winch(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200205static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206static char_u *extra_shell_arg = NULL;
207static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000208/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200209static volatile sig_atomic_t deadly_signal = 0; /* The signal we caught */
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000210/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200211static volatile sig_atomic_t in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100213#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
214static int dont_check_job_ended = 0;
215#endif
216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
218
219#ifdef USE_XSMP
220typedef struct
221{
222 SmcConn smcconn; /* The SM connection ID */
223 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200224 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 Bool save_yourself; /* If we're in the middle of a save_yourself */
226 Bool shutdown; /* If we're in shutdown mode */
227} xsmp_config_T;
228
229static xsmp_config_T xsmp;
230#endif
231
232#ifdef SYS_SIGLIST_DECLARED
233/*
234 * I have seen
235 * extern char *_sys_siglist[NSIG];
236 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
237 * that describe the signals. That is nearly what we want here. But
238 * autoconf does only check for sys_siglist (without the underscore), I
239 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100240 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242#endif
243
244static struct signalinfo
245{
246 int sig; /* Signal number, eg. SIGSEGV etc */
247 char *name; /* Signal name (not char_u!). */
248 char deadly; /* Catch as a deadly signal? */
249} signal_info[] =
250{
251#ifdef SIGHUP
252 {SIGHUP, "HUP", TRUE},
253#endif
254#ifdef SIGQUIT
255 {SIGQUIT, "QUIT", TRUE},
256#endif
257#ifdef SIGILL
258 {SIGILL, "ILL", TRUE},
259#endif
260#ifdef SIGTRAP
261 {SIGTRAP, "TRAP", TRUE},
262#endif
263#ifdef SIGABRT
264 {SIGABRT, "ABRT", TRUE},
265#endif
266#ifdef SIGEMT
267 {SIGEMT, "EMT", TRUE},
268#endif
269#ifdef SIGFPE
270 {SIGFPE, "FPE", TRUE},
271#endif
272#ifdef SIGBUS
273 {SIGBUS, "BUS", TRUE},
274#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100275#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
276 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {SIGSEGV, "SEGV", TRUE},
278#endif
279#ifdef SIGSYS
280 {SIGSYS, "SYS", TRUE},
281#endif
282#ifdef SIGALRM
283 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
284#endif
285#ifdef SIGTERM
286 {SIGTERM, "TERM", TRUE},
287#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100288#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGVTALRM, "VTALRM", TRUE},
290#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000291#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
292 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
293 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {SIGPROF, "PROF", TRUE},
295#endif
296#ifdef SIGXCPU
297 {SIGXCPU, "XCPU", TRUE},
298#endif
299#ifdef SIGXFSZ
300 {SIGXFSZ, "XFSZ", TRUE},
301#endif
302#ifdef SIGUSR1
303 {SIGUSR1, "USR1", TRUE},
304#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000305#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
306 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGUSR2, "USR2", TRUE},
308#endif
309#ifdef SIGINT
310 {SIGINT, "INT", FALSE},
311#endif
312#ifdef SIGWINCH
313 {SIGWINCH, "WINCH", FALSE},
314#endif
315#ifdef SIGTSTP
316 {SIGTSTP, "TSTP", FALSE},
317#endif
318#ifdef SIGPIPE
319 {SIGPIPE, "PIPE", FALSE},
320#endif
321 {-1, "Unknown!", FALSE}
322};
323
Bram Moolenaar25724922009-07-14 15:38:41 +0000324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000326{
327 if (p_verbose >= 5)
328 {
329 verbose_enter();
330 smsg((char_u *)"chdir(%s)", path);
331 verbose_leave();
332 }
333# ifdef VMS
334 return chdir(vms_fixfilename(path));
335# else
336 return chdir(path);
337# endif
338}
339
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200340/* Why is NeXT excluded here (and not in os_unixx.h)? */
341#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
342# define NEW_TTY_SYSTEM
343#endif
344
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000345/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100346 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100349mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200351 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100353 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000357 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 * Get a characters from the keyboard.
359 * Return the number of characters that are available.
360 * If wtime == 0 do not wait for characters.
361 * If wtime == n wait a short time for characters.
362 * If wtime == -1 wait forever for characters.
363 */
364 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100365mch_inchar(
366 char_u *buf,
367 int maxlen,
368 long wtime, /* don't use "time", MIPS cannot handle it */
369 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
371 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200372 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200373 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200374 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200375 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100376#ifdef ELAPSED_FUNC
377 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200378
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100379 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200382 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200383 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200385 /* Check if window changed size while we were busy, perhaps the ":set
386 * columns=99" command was used. */
387 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200389
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200390#ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +0200391 // Only process messages when waiting.
392 if (wtime != 0)
393 {
394 parse_queued_messages();
395 // If input was put directly in typeahead buffer bail out here.
396 if (typebuf_changed(tb_change_cnt))
397 return 0;
398 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200399#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200400 if (wtime < 0 && did_start_blocking)
401 /* blocking and already waited for p_ut */
402 wait_time = -1;
403 else
404 {
405 if (wtime >= 0)
406 wait_time = wtime;
407 else
408 /* going to block after p_ut */
409 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100410#ifdef ELAPSED_FUNC
411 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200412#endif
413 wait_time -= elapsed_time;
414 if (wait_time < 0)
415 {
416 if (wtime >= 0)
417 /* no character available within "wtime" */
418 return 0;
419
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200420 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200421 {
422 /* no character available within 'updatetime' */
423 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200424 if (trigger_cursorhold() && maxlen >= 3
425 && !typebuf_changed(tb_change_cnt))
426 {
427 buf[0] = K_SPECIAL;
428 buf[1] = KS_EXTRA;
429 buf[2] = (int)KE_CURSORHOLD;
430 return 3;
431 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200432 /*
433 * If there is no character available within 'updatetime'
434 * seconds flush all the swap files to disk.
435 * Also done when interrupted by SIGWINCH.
436 */
437 before_blocking();
438 continue;
439 }
440 }
441 }
442
443#ifdef FEAT_JOB_CHANNEL
444 /* Checking if a job ended requires polling. Do this every 100 msec. */
445 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
446 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100447 /* If there is readahead then parse_queued_messages() timed out and we
448 * should call it again soon. */
449 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
450 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200451#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100452#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100453 if (p_beval && wait_time > 100L)
454 /* The 'balloonexpr' may indirectly invoke a callback while waiting
455 * for a character, need to check often. */
456 wait_time = 100L;
457#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200460 * We want to be interrupted by the winch signal
461 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200463 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200464 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200465 /* If input was put directly in typeahead buffer bail out here. */
466 if (typebuf_changed(tb_change_cnt))
467 return 0;
468
469 /*
470 * For some terminals we only get one character at a time.
471 * We want the get all available characters, so we could keep on
472 * trying until none is available
473 * For some other terminals this is quite slow, that's why we don't
474 * do it.
475 */
476 len = read_from_input_buf(buf, (long)maxlen);
477 if (len > 0)
478 return len;
479 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200482 /* no character available */
483#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
484 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100485 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200488 if (do_resize /* interrupted by SIGWINCH signal */
489#ifdef FEAT_CLIENTSERVER
490 || server_waiting()
491#endif
492#ifdef MESSAGE_QUEUE
493 || interrupted
494#endif
495 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100496 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200497 continue;
498
499 /* no character available or interrupted */
500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200502 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
505 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100506handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 do_resize = FALSE;
509 shell_resized();
510}
511
512/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200513 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
515 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100516mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200518 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200521#if defined(FEAT_TERMINAL) || defined(PROTO)
522/*
523 * Check for any pending input or messages.
524 */
525 int
526mch_check_messages(void)
527{
528 return WaitForChar(0L, NULL, TRUE);
529}
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
533# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000534# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535# endif
536# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
537# include <sys/sysctl.h>
538# endif
539# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
540# include <sys/sysinfo.h>
541# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100542# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100543# include <mach/mach_host.h>
544# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
547/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000548 * Return total amount of memory available in Kbyte.
549 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100552mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000555 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar362dc332018-03-05 21:59:37 +0100557# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100558 {
559 /* Mac (Darwin) way of getting the amount of RAM available */
560 mach_port_t host = mach_host_self();
561 kern_return_t kret;
562# ifdef HOST_VM_INFO64
563 struct vm_statistics64 vm_stat;
564 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar988615f2018-02-27 17:58:20 +0100566 kret = host_statistics64(host, HOST_VM_INFO64,
567 (host_info64_t)&vm_stat, &count);
568# else
569 struct vm_statistics vm_stat;
570 natural_t count = HOST_VM_INFO_COUNT;
571
572 kret = host_statistics(host, HOST_VM_INFO,
573 (host_info_t)&vm_stat, &count);
574# endif
575 if (kret == KERN_SUCCESS)
576 /* get the amount of user memory by summing each usage */
577 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
578 + vm_stat.inactive_count
579# ifdef MAC_OS_X_VERSION_10_9
580 + vm_stat.compressor_page_count
581# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100582 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100583 mach_port_deallocate(mach_task_self(), host);
584 }
585# endif
586
587# ifdef HAVE_SYSCTL
588 if (mem == 0)
589 {
590 /* BSD way of getting the amount of RAM available. */
591 int mib[2];
592 size_t len = sizeof(long_u);
593# ifdef HW_USERMEM64
594 long_u physmem;
595# else
596 /* sysctl() may return 32 bit or 64 bit, accept both */
597 union {
598 int_u u32;
599 long_u u64;
600 } physmem;
601# endif
602
603 mib[0] = CTL_HW;
604# ifdef HW_USERMEM64
605 mib[1] = HW_USERMEM64;
606# else
607 mib[1] = HW_USERMEM;
608# endif
609 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
610 {
611# ifdef HW_USERMEM64
612 mem = (long_u)physmem;
613# else
614 if (len == sizeof(physmem.u64))
615 mem = (long_u)physmem.u64;
616 else
617 mem = (long_u)physmem.u32;
618# endif
619 }
620 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200623# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (mem == 0)
625 {
626 struct sysinfo sinfo;
627
628 /* Linux way of getting amount of RAM available */
629 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000630 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200631# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000632 /* avoid overflow as much as possible */
633 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
634 {
635 sinfo.mem_unit = sinfo.mem_unit >> 1;
636 --shiftright;
637 }
638 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200639# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200641# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if (mem == 0)
648 {
649 long pagesize, pagecount;
650
651 /* Solaris way of getting amount of RAM available */
652 pagesize = sysconf(_SC_PAGESIZE);
653 pagecount = sysconf(_SC_PHYS_PAGES);
654 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
656 /* avoid overflow as much as possible */
657 while (shiftright > 0 && (pagesize & 1) == 0)
658 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000659 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000660 --shiftright;
661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667 /* Return the minimum of the physical memory and the user limit, because
668 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200669# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
671 struct rlimit rlp;
672
673 if (getrlimit(RLIMIT_DATA, &rlp) == 0
674 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200675# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200677# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000678 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 {
681 mem = (long_u)rlp.rlim_cur;
682 shiftright = 10;
683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
687 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000688 return mem >> shiftright;
689 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691#endif
692
693 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100694mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000697#ifdef FEAT_MZSCHEME
698 long total = msec; /* remember original value */
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701 if (ignoreinput)
702 {
703 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000704 * here. But we don't want QUIT to kill us (CTRL-\ used in a
705 * shell may produce SIGQUIT). */
706 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 old_tmode = curr_tmode;
708 if (curr_tmode == TMODE_RAW)
709 settmode(TMODE_SLEEP);
710
711 /*
712 * Everybody sleeps in a different way...
713 * Prefer nanosleep(), some versions of usleep() can only sleep up to
714 * one second.
715 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000716#ifdef FEAT_MZSCHEME
717 do
718 {
719 /* if total is large enough, wait by portions in p_mzq */
720 if (total > p_mzq)
721 msec = p_mzq;
722 else
723 msec = total;
724 total -= msec;
725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#ifdef HAVE_NANOSLEEP
727 {
728 struct timespec ts;
729
730 ts.tv_sec = msec / 1000;
731 ts.tv_nsec = (msec % 1000) * 1000000;
732 (void)nanosleep(&ts, NULL);
733 }
734#else
735# ifdef HAVE_USLEEP
736 while (msec >= 1000)
737 {
738 usleep((unsigned int)(999 * 1000));
739 msec -= 999;
740 }
741 usleep((unsigned int)(msec * 1000));
742# else
743# ifndef HAVE_SELECT
744 poll(NULL, 0, (int)msec);
745# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 struct timeval tv;
748
749 tv.tv_sec = msec / 1000;
750 tv.tv_usec = (msec % 1000) * 1000;
751 /*
752 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
753 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
754 */
755 select(0, NULL, NULL, NULL, &tv);
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757# endif /* HAVE_SELECT */
758# endif /* HAVE_NANOSLEEP */
759#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000760#ifdef FEAT_MZSCHEME
761 }
762 while (total > 0);
763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000766 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200769 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000772#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
774# define HAVE_CHECK_STACK_GROWTH
775/*
776 * Support for checking for an almost-out-of-stack-space situation.
777 */
778
779/*
780 * Return a pointer to an item on the stack. Used to find out if the stack
781 * grows up or down.
782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783static int stack_grows_downwards;
784
785/*
786 * Find out if the stack grows upwards or downwards.
787 * "p" points to a variable on the stack of the caller.
788 */
789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100790check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 int i;
793
794 stack_grows_downwards = (p > (char *)&i);
795}
796#endif
797
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000798#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799static char *stack_limit = NULL;
800
801#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
802# include <pthread.h>
803# include <pthread_np.h>
804#endif
805
806/*
807 * Find out until how var the stack can grow without getting into trouble.
808 * Called when starting up and when switching to the signal stack in
809 * deathtrap().
810 */
811 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100812get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 struct rlimit rlp;
815 int i;
816 long lim;
817
818 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
819 * limit doesn't fit in a long (rlim_cur might be "long long"). */
820 if (getrlimit(RLIMIT_STACK, &rlp) == 0
821 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
822# ifdef RLIM_INFINITY
823 && rlp.rlim_cur != RLIM_INFINITY
824# endif
825 )
826 {
827 lim = (long)rlp.rlim_cur;
828#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
829 {
830 pthread_attr_t attr;
831 size_t size;
832
833 /* On FreeBSD the initial thread always has a fixed stack size, no
834 * matter what the limits are set to. Normally it's 1 Mbyte. */
835 pthread_attr_init(&attr);
836 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
837 {
838 pthread_attr_getstacksize(&attr, &size);
839 if (lim > (long)size)
840 lim = (long)size;
841 }
842 pthread_attr_destroy(&attr);
843 }
844#endif
845 if (stack_grows_downwards)
846 {
847 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
848 if (stack_limit >= (char *)&i)
849 /* overflow, set to 1/16 of current stack position */
850 stack_limit = (char *)((long)&i / 16L);
851 }
852 else
853 {
854 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
855 if (stack_limit <= (char *)&i)
856 stack_limit = NULL; /* overflow */
857 }
858 }
859}
860
861/*
862 * Return FAIL when running out of stack space.
863 * "p" must point to any variable local to the caller that's on the stack.
864 */
865 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100866mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 if (stack_limit != NULL)
869 {
870 if (stack_grows_downwards)
871 {
872 if (p < stack_limit)
873 return FAIL;
874 }
875 else if (p > stack_limit)
876 return FAIL;
877 }
878 return OK;
879}
880#endif
881
882#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
883/*
884 * Support for using the signal stack.
885 * This helps when we run out of stack space, which causes a SIGSEGV. The
886 * signal handler then must run on another stack, since the normal stack is
887 * completely full.
888 */
889
890#ifndef SIGSTKSZ
891# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
892#endif
893
894# ifdef HAVE_SIGALTSTACK
895static stack_t sigstk; /* for sigaltstack() */
896# else
897static struct sigstack sigstk; /* for sigstack() */
898# endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900static char *signal_stack;
901
902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100903init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 if (signal_stack != NULL)
906 {
907# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908# ifdef HAVE_SS_BASE
909 sigstk.ss_base = signal_stack;
910# else
911 sigstk.ss_sp = signal_stack;
912# endif
913 sigstk.ss_size = SIGSTKSZ;
914 sigstk.ss_flags = 0;
915 (void)sigaltstack(&sigstk, NULL);
916# else
917 sigstk.ss_sp = signal_stack;
918 if (stack_grows_downwards)
919 sigstk.ss_sp += SIGSTKSZ - 1;
920 sigstk.ss_onstack = 0;
921 (void)sigstack(&sigstk, NULL);
922# endif
923 }
924}
925#endif
926
927/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000928 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * will barf when the second argument to signal() is ``wrong''.
930 * Let me try it with a few tricky defines from my own osdef.h (jw).
931 */
932#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 static RETSIGTYPE
934sig_winch SIGDEFARG(sigarg)
935{
936 /* this is not required on all systems, but it doesn't hurt anybody */
937 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
938 do_resize = TRUE;
939 SIGRETURN;
940}
941#endif
942
943#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 static RETSIGTYPE
945catch_sigint SIGDEFARG(sigarg)
946{
947 /* this is not required on all systems, but it doesn't hurt anybody */
948 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
949 got_int = TRUE;
950 SIGRETURN;
951}
952#endif
953
954#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 static RETSIGTYPE
956catch_sigpwr SIGDEFARG(sigarg)
957{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000958 /* this is not required on all systems, but it doesn't hurt anybody */
959 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /*
961 * I'm not sure we get the SIGPWR signal when the system is really going
962 * down or when the batteries are almost empty. Just preserve the swap
963 * files and don't exit, that can't do any harm.
964 */
965 ml_sync_all(FALSE, FALSE);
966 SIGRETURN;
967}
968#endif
969
970#ifdef SET_SIG_ALARM
971/*
972 * signal function for alarm().
973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 static RETSIGTYPE
975sig_alarm SIGDEFARG(sigarg)
976{
977 /* doesn't do anything, just to break a system call */
978 sig_alarm_called = TRUE;
979 SIGRETURN;
980}
981#endif
982
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100983#if defined(HAVE_SETJMP_H) || defined(PROTO)
984// argument to SETJMP()
985static JMP_BUF lc_jump_env;
986
987# ifdef SIGHASARG
988// Caught signal number, 0 when no was signal caught; used for mch_libcall().
989// Volatile because it is used in signal handlers.
990static volatile sig_atomic_t lc_signal;
991# endif
992
993// TRUE when lc_jump_env is valid.
994// Volatile because it is used in signal handler deathtrap().
995static volatile sig_atomic_t lc_active INIT(= FALSE);
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/*
998 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100999 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001001 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Usage:
1003 * mch_startjmp();
1004 * if (SETJMP(lc_jump_env) != 0)
1005 * {
1006 * mch_didjmp();
1007 * EMSG("crash!");
1008 * }
1009 * else
1010 * {
1011 * do_the_work;
1012 * mch_endjmp();
1013 * }
1014 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1015 * not return before the saved environment is used.
1016 * Returns OK for normal return, FAIL when the protected code caused a
1017 * problem and LONGJMP() was used.
1018 */
1019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001020mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022#ifdef SIGHASARG
1023 lc_signal = 0;
1024#endif
1025 lc_active = TRUE;
1026}
1027
1028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001029mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 lc_active = FALSE;
1032}
1033
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001038 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1039 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 init_signal_stack();
1041# endif
1042}
1043#endif
1044
1045/*
1046 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001047 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001049 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1050 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
1052 static RETSIGTYPE
1053deathtrap SIGDEFARG(sigarg)
1054{
1055 static int entered = 0; /* count the number of times we got here.
1056 Note: when memory has been corrupted
1057 this may get an arbitrary value! */
1058#ifdef SIGHASARG
1059 int i;
1060#endif
1061
1062#if defined(HAVE_SETJMP_H)
1063 /*
1064 * Catch a crash in protected code.
1065 * Restores the environment saved in lc_jump_env, which looks like
1066 * SETJMP() returns 1.
1067 */
1068 if (lc_active)
1069 {
1070# if defined(SIGHASARG)
1071 lc_signal = sigarg;
1072# endif
1073 lc_active = FALSE; /* don't jump again */
1074 LONGJMP(lc_jump_env, 1);
1075 /* NOTREACHED */
1076 }
1077#endif
1078
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001079#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001080# ifdef SIGQUIT
1081 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1082 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1083 * pressing CTRL-\, but we don't want Vim to exit then. */
1084 if (in_mch_delay && sigarg == SIGQUIT)
1085 SIGRETURN;
1086# endif
1087
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001088 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1089 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1090 * free(). Calling free() again may then cause a crash. */
1091 if (entered == 0
1092 && (0
1093# ifdef SIGHUP
1094 || sigarg == SIGHUP
1095# endif
1096# ifdef SIGQUIT
1097 || sigarg == SIGQUIT
1098# endif
1099# ifdef SIGTERM
1100 || sigarg == SIGTERM
1101# endif
1102# ifdef SIGPWR
1103 || sigarg == SIGPWR
1104# endif
1105# ifdef SIGUSR1
1106 || sigarg == SIGUSR1
1107# endif
1108# ifdef SIGUSR2
1109 || sigarg == SIGUSR2
1110# endif
1111 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001112 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001113 SIGRETURN;
1114#endif
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /* Remember how often we have been called. */
1117 ++entered;
1118
Bram Moolenaare429e702016-06-10 19:49:14 +02001119 /* Executing autocommands is likely to use more stack space than we have
1120 * available in the signal stack. */
1121 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#ifdef FEAT_EVAL
1124 /* Set the v:dying variable. */
1125 set_vim_var_nr(VV_DYING, (long)entered);
1126#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001127 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /* Since we are now using the signal stack, need to reset the stack
1131 * limit. Otherwise using a regexp will fail. */
1132 get_stack_limit();
1133#endif
1134
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001135#if 0
1136 /* This is for opening gdb the moment Vim crashes.
1137 * You need to manually adjust the file name and Vim executable name.
1138 * Suggested by SungHyun Nam. */
1139 {
1140# define VI_GDB_FILE "/tmp/vimgdb"
1141# define VIM_NAME "/usr/bin/vim"
1142 FILE *fp = fopen(VI_GDB_FILE, "w");
1143 if (fp)
1144 {
1145 fprintf(fp,
1146 "file %s\n"
1147 "attach %d\n"
1148 "set height 1000\n"
1149 "bt full\n"
1150 , VIM_NAME, getpid());
1151 fclose(fp);
1152 system("xterm -e gdb -x "VI_GDB_FILE);
1153 unlink(VI_GDB_FILE);
1154 }
1155 }
1156#endif
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef SIGHASARG
1159 /* try to find the name of this signal */
1160 for (i = 0; signal_info[i].sig != -1; i++)
1161 if (sigarg == signal_info[i].sig)
1162 break;
1163 deadly_signal = sigarg;
1164#endif
1165
1166 full_screen = FALSE; /* don't write message to the GUI, it might be
1167 * part of the problem... */
1168 /*
1169 * If something goes wrong after entering here, we may get here again.
1170 * When this happens, give a message and try to exit nicely (resetting the
1171 * terminal mode, etc.)
1172 * When this happens twice, just exit, don't even try to give a message,
1173 * stack may be corrupt or something weird.
1174 * When this still happens again (or memory was corrupted in such a way
1175 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1176 */
1177 if (entered >= 3)
1178 {
1179 reset_signals(); /* don't catch any signals anymore */
1180 may_core_dump();
1181 if (entered >= 4)
1182 _exit(8);
1183 exit(7);
1184 }
1185 if (entered == 2)
1186 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001187 /* No translation, it may call malloc(). */
1188 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 out_flush();
1190 getout(1);
1191 }
1192
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001193 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001195 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 signal_info[i].name);
1197#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001198 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001200
1201 /* Preserve files and exit. This sets the really_exiting flag to prevent
1202 * calling free(). */
1203 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
Bram Moolenaare429e702016-06-10 19:49:14 +02001205 /* NOTREACHED */
1206
Bram Moolenaar009b2592004-10-24 19:18:58 +00001207#ifdef NBDEBUG
1208 reset_signals();
1209 may_core_dump();
1210 abort();
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 SIGRETURN;
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001217 * Invoked after receiving SIGCONT. We don't know what happened while
1218 * sleeping, deal with part of that.
1219 */
1220 static void
1221after_sigcont(void)
1222{
1223# ifdef FEAT_TITLE
1224 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1225 // again later.
1226 oldtitle_outdated = TRUE;
1227# endif
1228 settmode(TMODE_RAW);
1229 need_check_timestamps = TRUE;
1230 did_check_timestamps = FALSE;
1231}
1232
1233#if defined(SIGCONT)
1234static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001235static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001236
1237/*
1238 * With multi-threading, suspending might not work immediately. Catch the
1239 * SIGCONT signal, which will be used as an indication whether the suspending
1240 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001241 *
1242 * On Linux, signal is not always handled immediately either.
1243 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001244 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001245 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001246 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001248static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001249static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251/*
1252 * signal handler for SIGCONT
1253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 static RETSIGTYPE
1255sigcont_handler SIGDEFARG(sigarg)
1256{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001257 if (in_mch_suspend)
1258 {
1259 sigcont_received = TRUE;
1260 }
1261 else
1262 {
1263 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1264 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1265 // back to a sane mode. We should redraw, but we can't really do that
1266 // in a signal handler, do a redraw later.
1267 after_sigcont();
1268 redraw_later(CLEAR);
1269 cursor_on_force();
1270 out_flush();
1271 }
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 SIGRETURN;
1274}
1275#endif
1276
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001277#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001278# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001279static void *clip_star_save = NULL;
1280static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001281# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001282
1283/*
1284 * Called when Vim is going to sleep or execute a shell command.
1285 * We can't respond to requests for the X selections. Lose them, otherwise
1286 * other applications will hang. But first copy the text to cut buffer 0.
1287 */
1288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001290{
1291 if (clip_star.owned || clip_plus.owned)
1292 {
1293 x11_export_final_selection();
1294 if (clip_star.owned)
1295 clip_lose_selection(&clip_star);
1296 if (clip_plus.owned)
1297 clip_lose_selection(&clip_plus);
1298 if (x11_display != NULL)
1299 XFlush(x11_display);
1300 }
1301}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001302
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001303# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001304/*
1305 * Save clipboard text to restore later.
1306 */
1307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309{
1310 if (clip_star.owned)
1311 clip_star_save = get_register('*', TRUE);
1312 if (clip_plus.owned)
1313 clip_plus_save = get_register('+', TRUE);
1314}
1315
1316/*
1317 * Restore clipboard text if no one own the X selection.
1318 */
1319 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001320restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001321{
1322 if (clip_star_save != NULL)
1323 {
1324 if (!clip_gen_owner_exists(&clip_star))
1325 put_register('*', clip_star_save);
1326 else
1327 free_register(clip_star_save);
1328 clip_star_save = NULL;
1329 }
1330 if (clip_plus_save != NULL)
1331 {
1332 if (!clip_gen_owner_exists(&clip_plus))
1333 put_register('+', clip_plus_save);
1334 else
1335 free_register(clip_plus_save);
1336 clip_plus_save = NULL;
1337 }
1338}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001339# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001340#endif
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342/*
1343 * If the machine has job control, use it to suspend the program,
1344 * otherwise fake it by starting a new shell.
1345 */
1346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001347mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 /* BeOS does have SIGTSTP, but it doesn't work. */
1350#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001351 in_mch_suspend = TRUE;
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 out_flush(); /* needed to make cursor visible on some systems */
1354 settmode(TMODE_COOK);
1355 out_flush(); /* needed to disable mouse on some systems */
1356
1357# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001358 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001360# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 sigcont_received = FALSE;
1362# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001365
1366# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001367 /*
1368 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001369 * immediately, but somehow not all the time, probably because it's handled
1370 * in another thread. Do not call pause() because there would be race
1371 * condition which would hang Vim if signal happened in between the test of
1372 * sigcont_received and the call to pause(). If signal is not yet received,
1373 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1374 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001375 */
1376 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001377 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001378
Bram Moolenaar262735e2009-07-14 10:20:22 +00001379 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001380 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
Bram Moolenaar2e310482018-08-21 13:09:10 +02001385 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#else
1387 suspend_shell();
1388#endif
1389}
1390
1391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001392mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 Columns = 80;
1395 Rows = 24;
1396
1397 out_flush();
1398 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001399
Bram Moolenaar56718732006-03-15 22:53:57 +00001400#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001401 mac_conv_init();
1402#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001403#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1404 win_clip_init();
1405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001409set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411#if defined(SIGWINCH)
1412 /*
1413 * WINDOW CHANGE signal is handled with sig_winch().
1414 */
1415 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1416#endif
1417
1418 /*
1419 * We want the STOP signal to work, to make mch_suspend() work.
1420 * For "rvim" the STOP signal is ignored.
1421 */
1422#ifdef SIGTSTP
1423 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1424#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001425#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 signal(SIGCONT, sigcont_handler);
1427#endif
1428
1429 /*
1430 * We want to ignore breaking of PIPEs.
1431 */
1432#ifdef SIGPIPE
1433 signal(SIGPIPE, SIG_IGN);
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001437 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438#endif
1439
1440 /*
1441 * Ignore alarm signals (Perl's alarm() generates it).
1442 */
1443#ifdef SIGALRM
1444 signal(SIGALRM, SIG_IGN);
1445#endif
1446
1447 /*
1448 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1449 * work will be lost.
1450 */
1451#ifdef SIGPWR
1452 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1453#endif
1454
1455 /*
1456 * Arrange for other signals to gracefully shutdown Vim.
1457 */
1458 catch_signals(deathtrap, SIG_ERR);
1459
1460#if defined(FEAT_GUI) && defined(SIGHUP)
1461 /*
1462 * When the GUI is running, ignore the hangup signal.
1463 */
1464 if (gui.in_use)
1465 signal(SIGHUP, SIG_IGN);
1466#endif
1467}
1468
Bram Moolenaardf177f62005-02-22 08:39:57 +00001469#if defined(SIGINT) || defined(PROTO)
1470/*
1471 * Catch CTRL-C (only works while in Cooked mode).
1472 */
1473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001474catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001475{
1476 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1477}
1478#endif
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001481reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001484#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 /* SIGCONT isn't in the list, because its default action is ignore */
1486 signal(SIGCONT, SIG_DFL);
1487#endif
1488}
1489
1490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001491catch_signals(
1492 RETSIGTYPE (*func_deadly)(),
1493 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 int i;
1496
1497 for (i = 0; signal_info[i].sig != -1; i++)
1498 if (signal_info[i].deadly)
1499 {
1500#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1501 struct sigaction sa;
1502
1503 /* Setup to use the alternate stack for the signal function. */
1504 sa.sa_handler = func_deadly;
1505 sigemptyset(&sa.sa_mask);
1506# if defined(__linux__) && defined(_REENTRANT)
1507 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1508 * thread handling in combination with using the alternate stack:
1509 * pthread library functions try to use the stack pointer to
1510 * identify the current thread, causing a SEGV signal, which
1511 * recursively calls deathtrap() and hangs. */
1512 sa.sa_flags = 0;
1513# else
1514 sa.sa_flags = SA_ONSTACK;
1515# endif
1516 sigaction(signal_info[i].sig, &sa, NULL);
1517#else
1518# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1519 struct sigvec sv;
1520
1521 /* Setup to use the alternate stack for the signal function. */
1522 sv.sv_handler = func_deadly;
1523 sv.sv_mask = 0;
1524 sv.sv_flags = SV_ONSTACK;
1525 sigvec(signal_info[i].sig, &sv, NULL);
1526# else
1527 signal(signal_info[i].sig, func_deadly);
1528# endif
1529#endif
1530 }
1531 else if (func_other != SIG_ERR)
1532 signal(signal_info[i].sig, func_other);
1533}
1534
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001535#ifdef HAVE_SIGPROCMASK
1536 static void
1537block_signals(sigset_t *set)
1538{
1539 sigset_t newset;
1540 int i;
1541
1542 sigemptyset(&newset);
1543
1544 for (i = 0; signal_info[i].sig != -1; i++)
1545 sigaddset(&newset, signal_info[i].sig);
1546
Bram Moolenaar2e310482018-08-21 13:09:10 +02001547# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001548 /* SIGCONT isn't in the list, because its default action is ignore */
1549 sigaddset(&newset, SIGCONT);
1550# endif
1551
1552 sigprocmask(SIG_BLOCK, &newset, set);
1553}
1554
1555 static void
1556unblock_signals(sigset_t *set)
1557{
1558 sigprocmask(SIG_SETMASK, set, NULL);
1559}
1560#endif
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001564 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1565 * return TRUE
1566 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1567 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001568 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 * Returns TRUE when Vim should exit.
1570 */
1571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001572vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001574 static int got_signal = 0;
1575 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001576
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001577 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001579 case SIGNAL_BLOCK: blocked = TRUE;
1580 break;
1581
1582 case SIGNAL_UNBLOCK: blocked = FALSE;
1583 if (got_signal != 0)
1584 {
1585 kill(getpid(), got_signal);
1586 got_signal = 0;
1587 }
1588 break;
1589
1590 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001591 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001592 got_signal = sig;
1593#ifdef SIGPWR
1594 if (sig != SIGPWR)
1595#endif
1596 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001597 break;
1598 }
1599 return FALSE;
1600}
1601
1602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * Check_win checks whether we have an interactive stdout.
1604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001606mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (isatty(1))
1609 return OK;
1610 return FAIL;
1611}
1612
1613/*
1614 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1615 */
1616 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001617mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 if (isatty(read_cmd_fd))
1620 return TRUE;
1621 return FALSE;
1622}
1623
1624#ifdef FEAT_X11
1625
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001626# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629/*
1630 * Give a message about the elapsed time for opening the X window.
1631 */
1632 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001633xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001635 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
1637# endif
1638#endif
1639
1640#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1641/*
1642 * A few functions shared by X11 title and clipboard code.
1643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645static int got_x_error = FALSE;
1646
1647/*
1648 * X Error handler, otherwise X just exits! (very rude) -- webb
1649 */
1650 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001651x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001653 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 STRCAT(IObuff, _("\nVim: Got X error\n"));
1655
1656 /* We cannot print a message and continue, because no X calls are allowed
1657 * here (causes my system to hang). Silently continuing might be an
1658 * alternative... */
1659 preserve_exit(); /* preserve files and exit */
1660
1661 return 0; /* NOTREACHED */
1662}
1663
1664/*
1665 * Another X Error handler, just used to check for errors.
1666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001668x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 got_x_error = TRUE;
1671 return 0;
1672}
1673
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001674/*
1675 * Return TRUE when connection to the X server is desired.
1676 */
1677 static int
1678x_connect_to_server(void)
1679{
1680 // No point in connecting if we are exiting or dying.
1681 if (exiting || v_dying)
1682 return FALSE;
1683
1684#if defined(FEAT_CLIENTSERVER)
1685 if (x_force_connect)
1686 return TRUE;
1687#endif
1688 if (x_no_connect)
1689 return FALSE;
1690
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001691 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001692 if (clip_exclude_prog != NULL)
1693 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001694 // Just in case we get called recursively, return FALSE. This could
1695 // happen if vpeekc() is used while executing the prog and it causes a
1696 // related callback to be invoked.
1697 if (regprog_in_use(clip_exclude_prog))
1698 return FALSE;
1699
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001700 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1701 return FALSE;
1702 }
1703 return TRUE;
1704}
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1707# if defined(HAVE_SETJMP_H)
1708/*
1709 * An X IO Error handler, used to catch error while opening the display.
1710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001712x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 /* This function should not return, it causes exit(). Longjump instead. */
1715 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001716# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001717 return 0; /* avoid the compiler complains about missing return value */
1718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719}
1720# endif
1721
1722/*
1723 * An X IO Error handler, used to catch terminal errors.
1724 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001725static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001728x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001731 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 x11_window = 0;
1733 x11_display = NULL;
1734 xterm_Shell = (Widget)0;
1735
1736 /* This function should not return, it causes exit(). Longjump instead. */
1737 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001738# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001739 return 0; /* avoid the compiler complains about missing return value */
1740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001742
1743/*
1744 * If the X11 connection was lost try to restore it.
1745 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001746 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001747 */
1748 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001749may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001750{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001751 // No point in restoring the connecting if we are exiting or dying.
1752 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001754 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001755
1756# ifndef LESSTIF_VERSION
1757 /* This has been reported to avoid Vim getting stuck. */
1758 if (app_context != (XtAppContext)NULL)
1759 {
1760 XtDestroyApplicationContext(app_context);
1761 app_context = (XtAppContext)NULL;
1762 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1763 }
1764# endif
1765
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001766 setup_term_clip();
1767 get_x11_title(FALSE);
1768 }
1769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#endif
1771
1772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 * Test if "dpy" and x11_window are valid by getting the window title.
1774 * I don't actually want it yet, so there may be a simpler call to use, but
1775 * this will cause the error handler x_error_check() to be called if anything
1776 * is wrong, such as the window pointer being invalid (as can happen when the
1777 * user changes his DISPLAY, but not his WINDOWID) -- webb
1778 */
1779 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001780test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int (*old_handler)();
1783 XTextProperty text_prop;
1784
1785 old_handler = XSetErrorHandler(x_error_check);
1786 got_x_error = FALSE;
1787 if (XGetWMName(dpy, x11_window, &text_prop))
1788 XFree((void *)text_prop.value);
1789 XSync(dpy, False);
1790 (void)XSetErrorHandler(old_handler);
1791
1792 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001793 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 return (got_x_error ? FAIL : OK);
1796}
1797#endif
1798
1799#ifdef FEAT_TITLE
1800
1801#ifdef FEAT_X11
1802
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001803static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
1805/*
1806 * try to get x11 window and display
1807 *
1808 * return FAIL for failure, OK otherwise
1809 */
1810 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001811get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
1813 char *winid;
1814 static int result = -1;
1815#define XD_NONE 0 /* x11_display not set here */
1816#define XD_HERE 1 /* x11_display opened here */
1817#define XD_GUI 2 /* x11_display used from gui.dpy */
1818#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1819 static int x11_display_from = XD_NONE;
1820 static int did_set_error_handler = FALSE;
1821
1822 if (!did_set_error_handler)
1823 {
1824 /* X just exits if it finds an error otherwise! */
1825 (void)XSetErrorHandler(x_error_handler);
1826 did_set_error_handler = TRUE;
1827 }
1828
Bram Moolenaar9372a112005-12-06 19:59:18 +00001829#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (gui.in_use)
1831 {
1832 /*
1833 * If the X11 display was opened here before, for the window where Vim
1834 * was started, close that one now to avoid a memory leak.
1835 */
1836 if (x11_display_from == XD_HERE && x11_display != NULL)
1837 {
1838 XCloseDisplay(x11_display);
1839 x11_display_from = XD_NONE;
1840 }
1841 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1842 {
1843 x11_display_from = XD_GUI;
1844 return OK;
1845 }
1846 x11_display = NULL;
1847 return FAIL;
1848 }
1849 else if (x11_display_from == XD_GUI)
1850 {
1851 /* GUI must have stopped somehow, clear x11_display */
1852 x11_window = 0;
1853 x11_display = NULL;
1854 x11_display_from = XD_NONE;
1855 }
1856#endif
1857
1858 /* When started with the "-X" argument, don't try connecting. */
1859 if (!x_connect_to_server())
1860 return FAIL;
1861
1862 /*
1863 * If WINDOWID not set, should try another method to find out
1864 * what the current window number is. The only code I know for
1865 * this is very complicated.
1866 * We assume that zero is invalid for WINDOWID.
1867 */
1868 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1869 x11_window = (Window)atol(winid);
1870
1871#ifdef FEAT_XCLIPBOARD
1872 if (xterm_dpy != NULL && x11_window != 0)
1873 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001874 /* We may have checked it already, but Gnome terminal can move us to
1875 * another window, so we need to check every time. */
1876 if (x11_display_from != XD_XTERM)
1877 {
1878 /*
1879 * If the X11 display was opened here before, for the window where
1880 * Vim was started, close that one now to avoid a memory leak.
1881 */
1882 if (x11_display_from == XD_HERE && x11_display != NULL)
1883 XCloseDisplay(x11_display);
1884 x11_display = xterm_dpy;
1885 x11_display_from = XD_XTERM;
1886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (test_x11_window(x11_display) == FAIL)
1888 {
1889 /* probably bad $WINDOWID */
1890 x11_window = 0;
1891 x11_display = NULL;
1892 x11_display_from = XD_NONE;
1893 return FAIL;
1894 }
1895 return OK;
1896 }
1897#endif
1898
1899 if (x11_window == 0 || x11_display == NULL)
1900 result = -1;
1901
1902 if (result != -1) /* Have already been here and set this */
1903 return result; /* Don't do all these X calls again */
1904
1905 if (x11_window != 0 && x11_display == NULL)
1906 {
1907#ifdef SET_SIG_ALARM
1908 RETSIGTYPE (*sig_save)();
1909#endif
1910#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1911 struct timeval start_tv;
1912
1913 if (p_verbose > 0)
1914 gettimeofday(&start_tv, NULL);
1915#endif
1916
1917#ifdef SET_SIG_ALARM
1918 /*
1919 * Opening the Display may hang if the DISPLAY setting is wrong, or
1920 * the network connection is bad. Set an alarm timer to get out.
1921 */
1922 sig_alarm_called = FALSE;
1923 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1924 (RETSIGTYPE (*)())sig_alarm);
1925 alarm(2);
1926#endif
1927 x11_display = XOpenDisplay(NULL);
1928
1929#ifdef SET_SIG_ALARM
1930 alarm(0);
1931 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1932 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001933 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934#endif
1935 if (x11_display != NULL)
1936 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001937# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001939 {
1940 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001941 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001942 verbose_leave();
1943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944# endif
1945 if (test_x11_window(x11_display) == FAIL)
1946 {
1947 /* Maybe window id is bad */
1948 x11_window = 0;
1949 XCloseDisplay(x11_display);
1950 x11_display = NULL;
1951 }
1952 else
1953 x11_display_from = XD_HERE;
1954 }
1955 }
1956 if (x11_window == 0 || x11_display == NULL)
1957 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001958
1959# ifdef FEAT_EVAL
1960 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1961# endif
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 return (result = OK);
1964}
1965
1966/*
1967 * Determine original x11 Window Title
1968 */
1969 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001970get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001972 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
1975/*
1976 * Determine original x11 Window icon
1977 */
1978 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001979get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 int retval = FALSE;
1982
1983 retval = get_x11_thing(FALSE, test_only);
1984
1985 /* could not get old icon, use terminal name */
1986 if (oldicon == NULL && !test_only)
1987 {
1988 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001989 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001991 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
1994 return retval;
1995}
1996
1997 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001998get_x11_thing(
1999 int get_title, /* get title string */
2000 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 XTextProperty text_prop;
2003 int retval = FALSE;
2004 Status status;
2005
2006 if (get_x11_windis() == OK)
2007 {
2008 /* Get window/icon name if any */
2009 if (get_title)
2010 status = XGetWMName(x11_display, x11_window, &text_prop);
2011 else
2012 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2013
2014 /*
2015 * If terminal is xterm, then x11_window may be a child window of the
2016 * outer xterm window that actually contains the window/icon name, so
2017 * keep traversing up the tree until a window with a title/icon is
2018 * found.
2019 */
2020 /* Previously this was only done for xterm and alikes. I don't see a
2021 * reason why it would fail for other terminal emulators.
2022 * if (term_is_xterm) */
2023 {
2024 Window root;
2025 Window parent;
2026 Window win = x11_window;
2027 Window *children;
2028 unsigned int num_children;
2029
2030 while (!status || text_prop.value == NULL)
2031 {
2032 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2033 &num_children))
2034 break;
2035 if (children)
2036 XFree((void *)children);
2037 if (parent == root || parent == 0)
2038 break;
2039
2040 win = parent;
2041 if (get_title)
2042 status = XGetWMName(x11_display, win, &text_prop);
2043 else
2044 status = XGetWMIconName(x11_display, win, &text_prop);
2045 }
2046 }
2047 if (status && text_prop.value != NULL)
2048 {
2049 retval = TRUE;
2050 if (!test_only)
2051 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002052#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2053 if (text_prop.encoding == XA_STRING
2054# ifdef FEAT_MBYTE
2055 && !has_mbyte
2056# endif
2057 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059#endif
2060 if (get_title)
2061 oldtitle = vim_strsave((char_u *)text_prop.value);
2062 else
2063 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002064#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066 else
2067 {
2068 char **cl;
2069 Status transform_status;
2070 int n = 0;
2071
2072 transform_status = XmbTextPropertyToTextList(x11_display,
2073 &text_prop,
2074 &cl, &n);
2075 if (transform_status >= Success && n > 0 && cl[0])
2076 {
2077 if (get_title)
2078 oldtitle = vim_strsave((char_u *) cl[0]);
2079 else
2080 oldicon = vim_strsave((char_u *) cl[0]);
2081 XFreeStringList(cl);
2082 }
2083 else
2084 {
2085 if (get_title)
2086 oldtitle = vim_strsave((char_u *)text_prop.value);
2087 else
2088 oldicon = vim_strsave((char_u *)text_prop.value);
2089 }
2090 }
2091#endif
2092 }
2093 XFree((void *)text_prop.value);
2094 }
2095 }
2096 return retval;
2097}
2098
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002099/* Xutf8 functions are not avaialble on older systems. Note that on some
2100 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2101 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2102 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002104# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105# define USE_UTF8_STRING
2106# endif
2107#endif
2108
2109/*
2110 * Set x11 Window Title
2111 *
2112 * get_x11_windis() must be called before this and have returned OK
2113 */
2114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002115set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2118 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2119 * supported everywhere and STRING doesn't work for multi-byte titles.
2120 */
2121#ifdef USE_UTF8_STRING
2122 if (enc_utf8)
2123 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2124 NULL, NULL, 0, NULL, NULL, NULL);
2125 else
2126#endif
2127 {
2128#if XtSpecificationRelease >= 4
2129# ifdef FEAT_XFONTSET
2130 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2131 NULL, NULL, 0, NULL, NULL, NULL);
2132# else
2133 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002134 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002137 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 XSetWMProperties(x11_display, x11_window, &text_prop,
2139 NULL, NULL, 0, NULL, NULL, NULL);
2140# endif
2141#else
2142 XStoreName(x11_display, x11_window, (char *)title);
2143#endif
2144 }
2145 XFlush(x11_display);
2146}
2147
2148/*
2149 * Set x11 Window icon
2150 *
2151 * get_x11_windis() must be called before this and have returned OK
2152 */
2153 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002154set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
2156 /* See above for comments about using X*SetWMProperties(). */
2157#ifdef USE_UTF8_STRING
2158 if (enc_utf8)
2159 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2160 NULL, 0, NULL, NULL, NULL);
2161 else
2162#endif
2163 {
2164#if XtSpecificationRelease >= 4
2165# ifdef FEAT_XFONTSET
2166 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2167 NULL, 0, NULL, NULL, NULL);
2168# else
2169 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002170 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002172 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2174 NULL, 0, NULL, NULL, NULL);
2175# endif
2176#else
2177 XSetIconName(x11_display, x11_window, (char *)icon);
2178#endif
2179 }
2180 XFlush(x11_display);
2181}
2182
2183#else /* FEAT_X11 */
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002186get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188 return FALSE;
2189}
2190
2191 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002192get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
2194 if (!test_only)
2195 {
2196 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002197 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002199 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201 return FALSE;
2202}
2203
2204#endif /* FEAT_X11 */
2205
2206 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002207mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 return get_x11_title(TRUE);
2210}
2211
2212 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002213mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 return get_x11_icon(TRUE);
2216}
2217
2218/*
2219 * Set the window title and icon.
2220 */
2221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002222mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 int type = 0;
2225 static int recursive = 0;
2226
2227 if (T_NAME == NULL) /* no terminal name (yet) */
2228 return;
2229 if (title == NULL && icon == NULL) /* nothing to do */
2230 return;
2231
2232 /* When one of the X11 functions causes a deadly signal, we get here again
2233 * recursively. Avoid hanging then (something is probably locked). */
2234 if (recursive)
2235 return;
2236 ++recursive;
2237
2238 /*
2239 * if the window ID and the display is known, we may use X11 calls
2240 */
2241#ifdef FEAT_X11
2242 if (get_x11_windis() == OK)
2243 type = 1;
2244#else
2245# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2246 if (gui.in_use)
2247 type = 1;
2248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
2250
2251 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002252 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 * than x11 calls, because the x11 calls don't always work
2254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if ((type || *T_TS != NUL) && title != NULL)
2256 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002257 if (oldtitle_outdated)
2258 {
2259 oldtitle_outdated = FALSE;
2260 VIM_CLEAR(oldtitle);
2261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (oldtitle == NULL
2263#ifdef FEAT_GUI
2264 && !gui.in_use
2265#endif
2266 ) /* first call but not in GUI, save title */
2267 (void)get_x11_title(FALSE);
2268
2269 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2270 term_settitle(title);
2271#ifdef FEAT_X11
2272 else
2273# ifdef FEAT_GUI_GTK
2274 if (!gui.in_use) /* don't do this if GTK+ is running */
2275# endif
2276 set_x11_title(title); /* x11 */
2277#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002278#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2280 else
2281 gui_mch_settitle(title, icon);
2282#endif
2283 did_set_title = TRUE;
2284 }
2285
2286 if ((type || *T_CIS != NUL) && icon != NULL)
2287 {
2288 if (oldicon == NULL
2289#ifdef FEAT_GUI
2290 && !gui.in_use
2291#endif
2292 ) /* first call, save icon */
2293 get_x11_icon(FALSE);
2294
2295 if (*T_CIS != NUL)
2296 {
2297 out_str(T_CIS); /* set icon start */
2298 out_str_nf(icon);
2299 out_str(T_CIE); /* set icon end */
2300 out_flush();
2301 }
2302#ifdef FEAT_X11
2303 else
2304# ifdef FEAT_GUI_GTK
2305 if (!gui.in_use) /* don't do this if GTK+ is running */
2306# endif
2307 set_x11_icon(icon); /* x11 */
2308#endif
2309 did_set_icon = TRUE;
2310 }
2311 --recursive;
2312}
2313
2314/*
2315 * Restore the window/icon title.
2316 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002317 * SAVE_RESTORE_TITLE only restore title
2318 * SAVE_RESTORE_ICON only restore icon
2319 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 */
2321 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002322mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
2324 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002325 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002327 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2328
2329 // pop and push from/to the stack
2330 term_pop_title(which);
2331 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332}
2333
2334#endif /* FEAT_TITLE */
2335
2336/*
2337 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002338 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 */
2340 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002341vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 if (name == NULL)
2344 return FALSE;
2345 return (STRNICMP(name, "xterm", 5) == 0
2346 || STRNICMP(name, "nxterm", 6) == 0
2347 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002348 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002350 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 || STRCMP(name, "builtin_xterm") == 0);
2352}
2353
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002354#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2355/*
2356 * Return TRUE if "name" appears to be that of a terminal
2357 * known to support the xterm-style mouse protocol.
2358 * Relies on term_is_xterm having been set to its correct value.
2359 */
2360 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002361use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002362{
2363 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002364 && (term_is_xterm
2365 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002366 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002367 || STRICMP(name, "st") == 0
2368 || STRNICMP(name, "st-", 3) == 0
2369 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002370}
2371#endif
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2374/*
2375 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2376 * Return 1 for "xterm".
2377 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002378 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002379 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 */
2381 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002382use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002384 if (ttym_flags == TTYM_SGR)
2385 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002386 if (ttym_flags == TTYM_URXVT)
2387 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (ttym_flags == TTYM_XTERM2)
2389 return 2;
2390 if (ttym_flags == TTYM_XTERM)
2391 return 1;
2392 return 0;
2393}
2394#endif
2395
2396 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002397vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399 if (name == NULL)
2400 return FALSE;
2401 return (STRNICMP(name, "iris-ansi", 9) == 0
2402 || STRCMP(name, "builtin_iris-ansi") == 0);
2403}
2404
2405 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002406vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 if (name == NULL)
2409 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002410 /* catch VT100 - VT5xx */
2411 return ((STRNICMP(name, "vt", 2) == 0
2412 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 || STRCMP(name, "builtin_vt320") == 0);
2414}
2415
2416/*
2417 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2418 * This should include all windowed terminal emulators.
2419 */
2420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002421vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 if (name == NULL)
2424 return FALSE;
2425 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2426 return TRUE;
2427 return ( STRNICMP(name, "hpterm", 6) == 0
2428 || STRNICMP(name, "sun-cmd", 7) == 0
2429 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002430 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 || STRNICMP(name, "dtterm", 6) == 0);
2432}
2433
2434/*
2435 * Insert user name in s[len].
2436 * Return OK if a name found.
2437 */
2438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002439mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002442 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return OK;
2444#else
2445 return mch_get_uname(getuid(), s, len);
2446#endif
2447}
2448
2449/*
2450 * Insert user name for "uid" in s[len].
2451 * Return OK if a name found.
2452 */
2453 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002454mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2457 struct passwd *pw;
2458
2459 if ((pw = getpwuid(uid)) != NULL
2460 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2461 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002462 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 return OK;
2464 }
2465#endif
2466 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2467 return FAIL; /* a number is not a name */
2468}
2469
2470/*
2471 * Insert host name is s[len].
2472 */
2473
2474#ifdef HAVE_SYS_UTSNAME_H
2475 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002476mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 struct utsname vutsname;
2479
2480 if (uname(&vutsname) < 0)
2481 *s = NUL;
2482 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002483 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485#else /* HAVE_SYS_UTSNAME_H */
2486
2487# ifdef HAVE_SYS_SYSTEMINFO_H
2488# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2489# endif
2490
2491 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002492mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494# ifdef VAXC
2495 vaxc$gethostname((char *)s, len);
2496# else
2497 gethostname((char *)s, len);
2498# endif
2499 s[len - 1] = NUL; /* make sure it's terminated */
2500}
2501#endif /* HAVE_SYS_UTSNAME_H */
2502
2503/*
2504 * return process ID
2505 */
2506 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002507mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 return (long)getpid();
2510}
2511
2512#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002514strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 extern int sys_nerr;
2517 extern char *sys_errlist[];
2518 static char er[20];
2519
2520 if (err > 0 && err < sys_nerr)
2521 return (sys_errlist[err]);
2522 sprintf(er, "Error %d", err);
2523 return er;
2524}
2525#endif
2526
2527/*
2528 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2529 * Return OK for success, FAIL for failure.
2530 */
2531 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002532mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534#if defined(USE_GETCWD)
2535 if (getcwd((char *)buf, len) == NULL)
2536 {
2537 STRCPY(buf, strerror(errno));
2538 return FAIL;
2539 }
2540 return OK;
2541#else
2542 return (getwd((char *)buf) != NULL ? OK : FAIL);
2543#endif
2544}
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002547 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 *
2549 * return FAIL for failure, OK for success
2550 */
2551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002552mch_FullName(
2553 char_u *fname,
2554 char_u *buf,
2555 int len,
2556 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002559#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 int fd = -1;
2561 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 char_u olddir[MAXPATHL];
2564 char_u *p;
2565 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002566#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002567 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2568 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002569#endif
2570
Bram Moolenaar38323e42007-03-06 19:22:53 +00002571#ifdef VMS
2572 fname = vms_fixfilename(fname);
2573#endif
2574
Bram Moolenaara2442432007-04-26 14:26:37 +00002575#ifdef __CYGWIN__
2576 /*
2577 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2578 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002579# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002580 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2581 * a forward slash. */
2582 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2583 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002584# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002585 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002586# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002587 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002588#endif
2589
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002590 /* Expand it if forced or not an absolute path.
2591 * Do not do it for "/file", the result is always "/". */
2592 if ((force || !mch_isFullName(fname))
2593 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 /*
2596 * If the file name has a path, change to that directory for a moment,
2597 * and then do the getwd() (and get back to where we were).
2598 * This will get the correct path name with "../" things.
2599 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002600 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002602#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 /*
2604 * Use fchdir() if possible, it's said to be faster and more
2605 * reliable. But on SunOS 4 it might not work. Check this by
2606 * doing a fchdir() right now.
2607 */
2608 if (!dont_fchdir)
2609 {
2610 fd = open(".", O_RDONLY | O_EXTRA, 0);
2611 if (fd >= 0 && fchdir(fd) < 0)
2612 {
2613 close(fd);
2614 fd = -1;
2615 dont_fchdir = TRUE; /* don't try again */
2616 }
2617 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 /* Only change directory when we are sure we can return to where
2621 * we are now. After doing "su" chdir(".") might not work. */
2622 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002623#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 (mch_dirname(olddir, MAXPATHL) == FAIL
2627 || mch_chdir((char *)olddir) != 0))
2628 {
2629 p = NULL; /* can't get current dir: don't chdir */
2630 retval = FAIL;
2631 }
2632 else
2633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 /* The directory is copied into buf[], to be able to remove
2635 * the file name without changing it (could be a string in
2636 * read-only memory) */
2637 if (p - fname >= len)
2638 retval = FAIL;
2639 else
2640 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002641 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (mch_chdir((char *)buf))
2643 retval = FAIL;
2644 else
2645 fname = p + 1;
2646 *buf = NUL;
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 }
2650 if (mch_dirname(buf, len) == FAIL)
2651 {
2652 retval = FAIL;
2653 *buf = NUL;
2654 }
2655 if (p != NULL)
2656 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002657#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (fd >= 0)
2659 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002660 if (p_verbose >= 5)
2661 {
2662 verbose_enter();
2663 MSG("fchdir() to previous dir");
2664 verbose_leave();
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 l = fchdir(fd);
2667 close(fd);
2668 }
2669 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 l = mch_chdir((char *)olddir);
2672 if (l != 0)
2673 EMSG(_(e_prev_dir));
2674 }
2675
2676 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002677 if (l >= len - 1)
2678 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002679#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002680 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002682 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002687 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 return FAIL;
2689
2690 /* Do not append ".", "/dir/." is equal to "/dir". */
2691 if (STRCMP(fname, ".") != 0)
2692 STRCAT(buf, fname);
2693
2694 return OK;
2695}
2696
2697/*
2698 * Return TRUE if "fname" does not depend on the current directory.
2699 */
2700 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002701mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002703#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return ( fname[0] == '/' || fname[0] == '.' ||
2705 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2706 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2707 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002708#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#endif
2711}
2712
Bram Moolenaar24552be2005-12-10 20:17:30 +00002713#if defined(USE_FNAME_CASE) || defined(PROTO)
2714/*
2715 * Set the case of the file name, if it already exists. This will cause the
2716 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002717 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002718 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002719 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002720fname_case(
2721 char_u *name,
2722 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002723{
2724 struct stat st;
2725 char_u *slash, *tail;
2726 DIR *dirp;
2727 struct dirent *dp;
2728
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002729 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002730 {
2731 /* Open the directory where the file is located. */
2732 slash = vim_strrchr(name, '/');
2733 if (slash == NULL)
2734 {
2735 dirp = opendir(".");
2736 tail = name;
2737 }
2738 else
2739 {
2740 *slash = NUL;
2741 dirp = opendir((char *)name);
2742 *slash = '/';
2743 tail = slash + 1;
2744 }
2745
2746 if (dirp != NULL)
2747 {
2748 while ((dp = readdir(dirp)) != NULL)
2749 {
2750 /* Only accept names that differ in case and are the same byte
2751 * length. TODO: accept different length name. */
2752 if (STRICMP(tail, dp->d_name) == 0
2753 && STRLEN(tail) == STRLEN(dp->d_name))
2754 {
2755 char_u newname[MAXPATHL + 1];
2756 struct stat st2;
2757
2758 /* Verify the inode is equal. */
2759 vim_strncpy(newname, name, MAXPATHL);
2760 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2761 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002762 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002763 && st.st_ino == st2.st_ino
2764 && st.st_dev == st2.st_dev)
2765 {
2766 STRCPY(tail, dp->d_name);
2767 break;
2768 }
2769 }
2770 }
2771
2772 closedir(dirp);
2773 }
2774 }
2775}
2776#endif
2777
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778/*
2779 * Get file permissions for 'name'.
2780 * Returns -1 when it doesn't exist.
2781 */
2782 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002783mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 struct stat statb;
2786
2787 /* Keep the #ifdef outside of stat(), it may be a macro. */
2788#ifdef VMS
2789 if (stat((char *)vms_fixfilename(name), &statb))
2790#else
2791 if (stat((char *)name, &statb))
2792#endif
2793 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002794#ifdef __INTERIX
2795 /* The top bit makes the value negative, which means the file doesn't
2796 * exist. Remove the bit, we don't use it. */
2797 return statb.st_mode & ~S_ADDACE;
2798#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801}
2802
2803/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002804 * Set file permission for "name" to "perm".
2805 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
2807 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002808mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 return (chmod((char *)
2811#ifdef VMS
2812 vms_fixfilename(name),
2813#else
2814 name,
2815#endif
2816 (mode_t)perm) == 0 ? OK : FAIL);
2817}
2818
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002819#if defined(HAVE_FCHMOD) || defined(PROTO)
2820/*
2821 * Set file permission for open file "fd" to "perm".
2822 * Return FAIL for failure, OK otherwise.
2823 */
2824 int
2825mch_fsetperm(int fd, long perm)
2826{
2827 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2828}
2829#endif
2830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#if defined(HAVE_ACL) || defined(PROTO)
2832# ifdef HAVE_SYS_ACL_H
2833# include <sys/acl.h>
2834# endif
2835# ifdef HAVE_SYS_ACCESS_H
2836# include <sys/access.h>
2837# endif
2838
2839# ifdef HAVE_SOLARIS_ACL
2840typedef struct vim_acl_solaris_T {
2841 int acl_cnt;
2842 aclent_t *acl_entry;
2843} vim_acl_solaris_T;
2844# endif
2845
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002846#if defined(HAVE_SELINUX) || defined(PROTO)
2847/*
2848 * Copy security info from "from_file" to "to_file".
2849 */
2850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002851mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002852{
2853 if (from_file == NULL)
2854 return;
2855
2856 if (selinux_enabled == -1)
2857 selinux_enabled = is_selinux_enabled();
2858
2859 if (selinux_enabled > 0)
2860 {
2861 security_context_t from_context = NULL;
2862 security_context_t to_context = NULL;
2863
2864 if (getfilecon((char *)from_file, &from_context) < 0)
2865 {
2866 /* If the filesystem doesn't support extended attributes,
2867 the original had no special security context and the
2868 target cannot have one either. */
2869 if (errno == EOPNOTSUPP)
2870 return;
2871
2872 MSG_PUTS(_("\nCould not get security context for "));
2873 msg_outtrans(from_file);
2874 msg_putchar('\n');
2875 return;
2876 }
2877 if (getfilecon((char *)to_file, &to_context) < 0)
2878 {
2879 MSG_PUTS(_("\nCould not get security context for "));
2880 msg_outtrans(to_file);
2881 msg_putchar('\n');
2882 freecon (from_context);
2883 return ;
2884 }
2885 if (strcmp(from_context, to_context) != 0)
2886 {
2887 if (setfilecon((char *)to_file, from_context) < 0)
2888 {
2889 MSG_PUTS(_("\nCould not set security context for "));
2890 msg_outtrans(to_file);
2891 msg_putchar('\n');
2892 }
2893 }
2894 freecon(to_context);
2895 freecon(from_context);
2896 }
2897}
2898#endif /* HAVE_SELINUX */
2899
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002900#if defined(HAVE_SMACK) && !defined(PROTO)
2901/*
2902 * Copy security info from "from_file" to "to_file".
2903 */
2904 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002905mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002906{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002907 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002908 {
2909 XATTR_NAME_SMACK,
2910 XATTR_NAME_SMACKEXEC,
2911 XATTR_NAME_SMACKMMAP
2912 };
2913
2914 char buffer[SMACK_LABEL_LEN];
2915 const char *name;
2916 int index;
2917 int ret;
2918 ssize_t size;
2919
2920 if (from_file == NULL)
2921 return;
2922
2923 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2924 / sizeof(smack_copied_attributes)[0]) ; index++)
2925 {
2926 /* get the name of the attribute to copy */
2927 name = smack_copied_attributes[index];
2928
2929 /* get the value of the attribute in buffer */
2930 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2931 if (size >= 0)
2932 {
2933 /* copy the attribute value of buffer */
2934 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2935 if (ret < 0)
2936 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002937 vim_snprintf((char *)IObuff, IOSIZE,
2938 _("Could not set security context %s for %s"),
2939 name, to_file);
2940 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002941 msg_putchar('\n');
2942 }
2943 }
2944 else
2945 {
2946 /* what reason of not having the attribute value? */
2947 switch (errno)
2948 {
2949 case ENOTSUP:
2950 /* extended attributes aren't supported or enabled */
2951 /* should a message be echoed? not sure... */
2952 return; /* leave because it isn't usefull to continue */
2953
2954 case ERANGE:
2955 default:
2956 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002957 vim_snprintf((char *)IObuff, IOSIZE,
2958 _("Could not get security context %s for %s. Removing it!"),
2959 name, from_file);
2960 msg_puts(IObuff);
2961 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002962 /* FALLTHROUGH to remove the attribute */
2963
2964 case ENODATA:
2965 /* no attribute of this name */
2966 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002967 /* Silently ignore errors, apparently this happens when
2968 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002969 break;
2970 }
2971 }
2972 }
2973}
2974#endif /* HAVE_SMACK */
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Return a pointer to the ACL of file "fname" in allocated memory.
2978 * Return NULL if the ACL is not available for whatever reason.
2979 */
2980 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002981mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 vim_acl_T ret = NULL;
2984#ifdef HAVE_POSIX_ACL
2985 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2986#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002987#ifdef HAVE_SOLARIS_ZFS_ACL
2988 acl_t *aclent;
2989
2990 if (acl_get((char *)fname, 0, &aclent) < 0)
2991 return NULL;
2992 ret = (vim_acl_T)aclent;
2993#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#ifdef HAVE_SOLARIS_ACL
2995 vim_acl_solaris_T *aclent;
2996
2997 aclent = malloc(sizeof(vim_acl_solaris_T));
2998 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2999 {
3000 free(aclent);
3001 return NULL;
3002 }
3003 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3004 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3005 {
3006 free(aclent->acl_entry);
3007 free(aclent);
3008 return NULL;
3009 }
3010 ret = (vim_acl_T)aclent;
3011#else
3012#if defined(HAVE_AIX_ACL)
3013 int aclsize;
3014 struct acl *aclent;
3015
3016 aclsize = sizeof(struct acl);
3017 aclent = malloc(aclsize);
3018 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3019 {
3020 if (errno == ENOSPC)
3021 {
3022 aclsize = aclent->acl_len;
3023 aclent = realloc(aclent, aclsize);
3024 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3025 {
3026 free(aclent);
3027 return NULL;
3028 }
3029 }
3030 else
3031 {
3032 free(aclent);
3033 return NULL;
3034 }
3035 }
3036 ret = (vim_acl_T)aclent;
3037#endif /* HAVE_AIX_ACL */
3038#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003039#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#endif /* HAVE_POSIX_ACL */
3041 return ret;
3042}
3043
3044/*
3045 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3046 */
3047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003048mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 if (aclent == NULL)
3051 return;
3052#ifdef HAVE_POSIX_ACL
3053 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3054#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003055#ifdef HAVE_SOLARIS_ZFS_ACL
3056 acl_set((char *)fname, (acl_t *)aclent);
3057#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef HAVE_SOLARIS_ACL
3059 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3060 ((vim_acl_solaris_T *)aclent)->acl_entry);
3061#else
3062#ifdef HAVE_AIX_ACL
3063 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3064#endif /* HAVE_AIX_ACL */
3065#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003066#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif /* HAVE_POSIX_ACL */
3068}
3069
3070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003071mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 if (aclent == NULL)
3074 return;
3075#ifdef HAVE_POSIX_ACL
3076 acl_free((acl_t)aclent);
3077#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003078#ifdef HAVE_SOLARIS_ZFS_ACL
3079 acl_free((acl_t *)aclent);
3080#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081#ifdef HAVE_SOLARIS_ACL
3082 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3083 free(aclent);
3084#else
3085#ifdef HAVE_AIX_ACL
3086 free(aclent);
3087#endif /* HAVE_AIX_ACL */
3088#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003089#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif /* HAVE_POSIX_ACL */
3091}
3092#endif
3093
3094/*
3095 * Set hidden flag for "name".
3096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003098mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 /* can't hide a file */
3101}
3102
3103/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003104 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * return FALSE if "name" is not a directory
3106 * return FALSE for error
3107 */
3108 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003109mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 struct stat statb;
3112
3113 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3114 return FALSE;
3115 if (stat((char *)name, &statb))
3116 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118}
3119
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003120/*
3121 * return TRUE if "name" is a directory, NOT a symlink to a directory
3122 * return FALSE if "name" is not a directory
3123 * return FALSE for error
3124 */
3125 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003126mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003127{
3128 struct stat statb;
3129
3130 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3131 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003132 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003133 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003134 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003135}
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3139 */
3140 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003141executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 struct stat st;
3144
3145 if (stat((char *)name, &st))
3146 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003147#ifdef VMS
3148 /* Like on Unix system file can have executable rights but not necessarily
3149 * be an executable, but on Unix is not a default for an ordianry file to
3150 * have an executable flag - on VMS it is in most cases.
3151 * Therefore, this check does not have any sense - let keep us to the
3152 * conventions instead:
3153 * *.COM and *.EXE files are the executables - the rest are not. This is
3154 * not ideal but better then it was.
3155 */
3156 int vms_executable = 0;
3157 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3158 {
3159 if (strstr(vms_tolower((char*)name),".exe") != NULL
3160 || strstr(vms_tolower((char*)name),".com")!= NULL)
3161 vms_executable = 1;
3162 }
3163 return vms_executable;
3164#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167}
3168
3169/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003170 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003171 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 * Return -1 if unknown.
3173 */
3174 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003175mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 char_u *buf;
3178 char_u *p, *e;
3179 int retval;
3180
Bram Moolenaarb5971142015-03-21 17:32:19 +01003181 /* When "use_path" is false and if it's an absolute or relative path don't
3182 * need to use $PATH. */
3183 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3184 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003185 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003186 /* There must be a path separator, files in the current directory
3187 * can't be executed. */
3188 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003189 {
3190 if (path != NULL)
3191 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003192 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003193 *path = FullName_save(name, TRUE);
3194 else
3195 *path = vim_strsave(name);
3196 }
3197 return TRUE;
3198 }
3199 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 p = (char_u *)getenv("PATH");
3203 if (p == NULL || *p == NUL)
3204 return -1;
3205 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3206 if (buf == NULL)
3207 return -1;
3208
3209 /*
3210 * Walk through all entries in $PATH to check if "name" exists there and
3211 * is an executable file.
3212 */
3213 for (;;)
3214 {
3215 e = (char_u *)strchr((char *)p, ':');
3216 if (e == NULL)
3217 e = p + STRLEN(p);
3218 if (e - p <= 1) /* empty entry means current dir */
3219 STRCPY(buf, "./");
3220 else
3221 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003222 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 add_pathsep(buf);
3224 }
3225 STRCAT(buf, name);
3226 retval = executable_file(buf);
3227 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003228 {
3229 if (path != NULL)
3230 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003231 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003232 *path = FullName_save(buf, TRUE);
3233 else
3234 *path = vim_strsave(buf);
3235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
3239 if (*e != ':')
3240 break;
3241 p = e + 1;
3242 }
3243
3244 vim_free(buf);
3245 return retval;
3246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
3248/*
3249 * Check what "name" is:
3250 * NODE_NORMAL: file or directory (or doesn't exist)
3251 * NODE_WRITABLE: writable device, socket, fifo, etc.
3252 * NODE_OTHER: non-writable things
3253 */
3254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003255mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 struct stat st;
3258
3259 if (stat((char *)name, &st))
3260 return NODE_NORMAL;
3261 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3262 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3264 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /* Everything else is writable? */
3266 return NODE_WRITABLE;
3267}
3268
3269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003270mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272#ifdef HAVE_CHECK_STACK_GROWTH
3273 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 check_stack_growth((char *)&i);
3276
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003277# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 get_stack_limit();
3279# endif
3280
3281#endif
3282
3283 /*
3284 * Setup an alternative stack for signals. Helps to catch signals when
3285 * running out of stack space.
3286 * Use of sigaltstack() is preferred, it's more portable.
3287 * Ignore any errors.
3288 */
3289#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003290 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 init_signal_stack();
3292#endif
3293}
3294
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003295#if defined(EXITFREE) || defined(PROTO)
3296 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003297mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003298{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003299# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3300 if (clip_star.owned)
3301 clip_lose_selection(&clip_star);
3302 if (clip_plus.owned)
3303 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003304# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003305# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003306 if (xterm_Shell != (Widget)0)
3307 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003308# ifndef LESSTIF_VERSION
3309 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003310 if (xterm_dpy != NULL)
3311 XtCloseDisplay(xterm_dpy);
3312 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003313 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003314 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003315# ifdef FEAT_X11
3316 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3317# endif
3318 }
3319# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003320# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003321# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003322 if (x11_display != NULL
3323# ifdef FEAT_XCLIPBOARD
3324 && x11_display != xterm_dpy
3325# endif
3326 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 XCloseDisplay(x11_display);
3328# endif
3329# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003330 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331# endif
3332# ifdef FEAT_TITLE
3333 vim_free(oldtitle);
3334 vim_free(oldicon);
3335# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003336}
3337#endif
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339/*
3340 * Output a newline when exiting.
3341 * Make sure the newline goes to the same stream as the text.
3342 */
3343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003344exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 if (silent_mode)
3347 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (newline_on_exit || msg_didout)
3349 {
3350 if (msg_use_printf())
3351 {
3352 if (info_message)
3353 mch_msg("\n");
3354 else
3355 mch_errmsg("\r\n");
3356 }
3357 else
3358 out_char('\n');
3359 }
3360 else
3361 {
3362 restore_cterm_colors(); /* get original colors back */
3363 msg_clr_eos_force(); /* clear the rest of the display */
3364 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3365 }
3366}
3367
3368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003369mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 exiting = TRUE;
3372
3373#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3374 x11_export_final_selection();
3375#endif
3376
3377#ifdef FEAT_GUI
3378 if (!gui.in_use)
3379#endif
3380 {
3381 settmode(TMODE_COOK);
3382#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003383 // restore xterm title and icon name
3384 mch_restore_title(SAVE_RESTORE_BOTH);
3385 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
3387 /*
3388 * When t_ti is not empty but it doesn't cause swapping terminal
3389 * pages, need to output a newline when msg_didout is set. But when
3390 * t_ti does swap pages it should not go to the shell page. Do this
3391 * before stoptermcap().
3392 */
3393 if (swapping_screen() && !newline_on_exit)
3394 exit_scroll();
3395
3396 /* Stop termcap: May need to check for T_CRV response, which
3397 * requires RAW mode. */
3398 stoptermcap();
3399
3400 /*
3401 * A newline is only required after a message in the alternate screen.
3402 * This is set to TRUE by wait_return().
3403 */
3404 if (!swapping_screen() || newline_on_exit)
3405 exit_scroll();
3406
3407 /* Cursor may have been switched off without calling starttermcap()
3408 * when doing "vim -u vimrc" and vimrc contains ":q". */
3409 if (full_screen)
3410 cursor_on();
3411 }
3412 out_flush();
3413 ml_close_all(TRUE); /* remove all memfiles */
3414 may_core_dump();
3415#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 gui_exit(r);
3418#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003419
Bram Moolenaar56718732006-03-15 22:53:57 +00003420#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003421 mac_conv_cleanup();
3422#endif
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424#ifdef __QNX__
3425 /* A core dump won't be created if the signal handler
3426 * doesn't return, so we can't call exit() */
3427 if (deadly_signal != 0)
3428 return;
3429#endif
3430
Bram Moolenaar009b2592004-10-24 19:18:58 +00003431#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003432 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003433#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003434
3435#ifdef EXITFREE
3436 free_all_mem();
3437#endif
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 exit(r);
3440}
3441
3442 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003443may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 if (deadly_signal != 0)
3446 {
3447 signal(deadly_signal, SIG_DFL);
3448 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3449 }
3450}
3451
3452#ifndef VMS
3453
3454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003455mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 static int first = TRUE;
3458
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003459#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460# ifdef HAVE_TERMIOS_H
3461 static struct termios told;
3462 struct termios tnew;
3463# else
3464 static struct termio told;
3465 struct termio tnew;
3466# endif
3467
3468 if (first)
3469 {
3470 first = FALSE;
3471# if defined(HAVE_TERMIOS_H)
3472 tcgetattr(read_cmd_fd, &told);
3473# else
3474 ioctl(read_cmd_fd, TCGETA, &told);
3475# endif
3476 }
3477
3478 tnew = told;
3479 if (tmode == TMODE_RAW)
3480 {
3481 /*
3482 * ~ICRNL enables typing ^V^M
3483 */
3484 tnew.c_iflag &= ~ICRNL;
3485 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3486# if defined(IEXTEN) && !defined(__MINT__)
3487 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3488 /* but it breaks function keys on MINT */
3489# endif
3490 );
3491# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3492 tnew.c_oflag &= ~ONLCR;
3493# endif
3494 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3495 tnew.c_cc[VTIME] = 0; /* don't wait */
3496 }
3497 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003498 {
3499 /* Also reset ICANON here, otherwise on Solaris select() won't see
3500 * typeahead characters. */
3501 tnew.c_lflag &= ~(ICANON | ECHO);
3502 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3503 tnew.c_cc[VTIME] = 0; /* don't wait */
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506# if defined(HAVE_TERMIOS_H)
3507 {
3508 int n = 10;
3509
3510 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3511 * few times. */
3512 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3513 && errno == EINTR && n > 0)
3514 --n;
3515 }
3516# else
3517 ioctl(read_cmd_fd, TCSETA, &tnew);
3518# endif
3519
3520#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 /*
3522 * for "old" tty systems
3523 */
3524# ifndef TIOCSETN
3525# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3526# endif
3527 static struct sgttyb ttybold;
3528 struct sgttyb ttybnew;
3529
3530 if (first)
3531 {
3532 first = FALSE;
3533 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3534 }
3535
3536 ttybnew = ttybold;
3537 if (tmode == TMODE_RAW)
3538 {
3539 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3540 ttybnew.sg_flags |= RAW;
3541 }
3542 else if (tmode == TMODE_SLEEP)
3543 ttybnew.sg_flags &= ~(ECHO);
3544 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3545#endif
3546 curr_tmode = tmode;
3547}
3548
3549/*
3550 * Try to get the code for "t_kb" from the stty setting
3551 *
3552 * Even if termcap claims a backspace key, the user's setting *should*
3553 * prevail. stty knows more about reality than termcap does, and if
3554 * somebody's usual erase key is DEL (which, for most BSD users, it will
3555 * be), they're going to get really annoyed if their erase key starts
3556 * doing forward deletes for no reason. (Eric Fischer)
3557 */
3558 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003559get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003561 ttyinfo_T info;
3562 char_u buf[2];
3563 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003565 if (get_tty_info(read_cmd_fd, &info) == OK)
3566 {
3567 intr_char = info.interrupt;
3568 buf[0] = info.backspace;
3569 buf[1] = NUL;
3570 add_termcode((char_u *)"kb", buf, FALSE);
3571
3572 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3573 p = find_termcode((char_u *)"kD");
3574 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3575 do_fixdel(NULL);
3576 }
3577}
3578
3579/*
3580 * Obtain the characters that Backspace and Enter produce on "fd".
3581 * Returns OK or FAIL.
3582 */
3583 int
3584get_tty_info(int fd, ttyinfo_T *info)
3585{
3586#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587# ifdef HAVE_TERMIOS_H
3588 struct termios keys;
3589# else
3590 struct termio keys;
3591# endif
3592
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003593 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003595 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003597 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003599 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003601 info->backspace = keys.c_cc[VERASE];
3602 info->interrupt = keys.c_cc[VINTR];
3603 if (keys.c_iflag & ICRNL)
3604 info->enter = NL;
3605 else
3606 info->enter = CAR;
3607 if (keys.c_oflag & ONLCR)
3608 info->nl_does_cr = TRUE;
3609 else
3610 info->nl_does_cr = FALSE;
3611 return OK;
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#else
3614 /* for "old" tty systems */
3615 struct sgttyb keys;
3616
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003617 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003619 info->backspace = keys.sg_erase;
3620 info->interrupt = keys.sg_kill;
3621 info->enter = CAR;
3622 info->nl_does_cr = TRUE;
3623 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003626 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627}
3628
3629#endif /* VMS */
3630
3631#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003632static int mouse_ison = FALSE;
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634/*
3635 * Set mouse clicks on or off.
3636 */
3637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003638mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003640# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003641 static int bevalterm_ison = FALSE;
3642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 int xterm_mouse_vers;
3644
Bram Moolenaara06afc72018-08-27 23:24:16 +02003645# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3646 if (!on)
3647 // Make sure not tracing mouse movements. Important when a button-down
3648 // was received but no release yet.
3649 stop_xterm_trace();
3650# endif
3651
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003652 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003653# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003654 && p_bevalterm == bevalterm_ison
3655# endif
3656 )
3657 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return;
3659
3660 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003661
3662# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003663 if (ttym_flags == TTYM_URXVT)
3664 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003665 out_str_nf((char_u *)
3666 (on
3667 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3668 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003669 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003670 }
3671# endif
3672
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003673# ifdef FEAT_MOUSE_SGR
3674 if (ttym_flags == TTYM_SGR)
3675 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003676 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003677 out_str_nf((char_u *)
3678 (on
3679 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3680 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003681 mouse_ison = on;
3682 }
3683# endif
3684
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003685# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686 if (bevalterm_ison != (p_bevalterm && on))
3687 {
3688 bevalterm_ison = (p_bevalterm && on);
3689 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3690 /* disable mouse movement events, enabling is below */
3691 out_str_nf((char_u *)
3692 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003693 }
3694# endif
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (xterm_mouse_vers > 0)
3697 {
3698 if (on) /* enable mouse events, use mouse tracking if available */
3699 out_str_nf((char_u *)
3700 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003701 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003702# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003703 bevalterm_ison
3704 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3705# endif
3706 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3708 else /* disable mouse events, could probably always send the same */
3709 out_str_nf((char_u *)
3710 (xterm_mouse_vers > 1
3711 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3712 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003713 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715
3716# ifdef FEAT_MOUSE_DEC
3717 else if (ttym_flags == TTYM_DEC)
3718 {
3719 if (on) /* enable mouse events */
3720 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3721 else /* disable mouse events */
3722 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003723 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725# endif
3726
3727# ifdef FEAT_MOUSE_GPM
3728 else
3729 {
3730 if (on)
3731 {
3732 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003733 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735 else
3736 {
3737 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003738 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 }
3741# endif
3742
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003743# ifdef FEAT_SYSMOUSE
3744 else
3745 {
3746 if (on)
3747 {
3748 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003749 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003750 }
3751 else
3752 {
3753 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003754 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003755 }
3756 }
3757# endif
3758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759# ifdef FEAT_MOUSE_JSB
3760 else
3761 {
3762 if (on)
3763 {
3764 /* D - Enable Mouse up/down messages
3765 * L - Enable Left Button Reporting
3766 * M - Enable Middle Button Reporting
3767 * R - Enable Right Button Reporting
3768 * K - Enable SHIFT and CTRL key Reporting
3769 * + - Enable Advanced messaging of mouse moves and up/down messages
3770 * Q - Quiet No Ack
3771 * # - Numeric value of mouse pointer required
3772 * 0 = Multiview 2000 cursor, used as standard
3773 * 1 = Windows Arrow
3774 * 2 = Windows I Beam
3775 * 3 = Windows Hour Glass
3776 * 4 = Windows Cross Hair
3777 * 5 = Windows UP Arrow
3778 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003779# ifdef JSBTERM_MOUSE_NONADVANCED
3780 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3782 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003783# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3785 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003786# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003787 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 else
3790 {
3791 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3792 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003793 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 }
3796# endif
3797# ifdef FEAT_MOUSE_PTERM
3798 else
3799 {
3800 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3801 if (on)
3802 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3803 else
3804 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003805 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807# endif
3808}
3809
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003810#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003811/*
3812 * Called when 'balloonevalterm' changed.
3813 */
3814 void
3815mch_bevalterm_changed(void)
3816{
3817 mch_setmouse(mouse_ison);
3818}
3819#endif
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821/*
3822 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3823 */
3824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003825check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826{
3827# ifdef FEAT_MOUSE_XTERM
3828 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003829# ifdef FEAT_MOUSE_URXVT
3830 && use_xterm_mouse() != 3
3831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832# ifdef FEAT_GUI
3833 && !gui.in_use
3834# endif
3835 )
3836 {
3837 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003838 ? IF_EB("\233M", CSI_STR "M")
3839 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (*p_mouse != NUL)
3841 {
3842 /* force mouse off and maybe on to send possibly new mouse
3843 * activation sequence to the xterm, with(out) drag tracing. */
3844 mch_setmouse(FALSE);
3845 setmouse();
3846 }
3847 }
3848 else
3849 del_mouse_termcode(KS_MOUSE);
3850# endif
3851
3852# ifdef FEAT_MOUSE_GPM
3853 if (!use_xterm_mouse()
3854# ifdef FEAT_GUI
3855 && !gui.in_use
3856# endif
3857 )
3858 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3859# endif
3860
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003861# ifdef FEAT_SYSMOUSE
3862 if (!use_xterm_mouse()
3863# ifdef FEAT_GUI
3864 && !gui.in_use
3865# endif
3866 )
3867 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3868# endif
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003871 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (!use_xterm_mouse()
3873# ifdef FEAT_GUI
3874 && !gui.in_use
3875# endif
3876 )
3877 set_mouse_termcode(KS_JSBTERM_MOUSE,
3878 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3879 else
3880 del_mouse_termcode(KS_JSBTERM_MOUSE);
3881# endif
3882
3883# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003884 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * define it in the GUI or when using an xterm. */
3886 if (!use_xterm_mouse()
3887# ifdef FEAT_GUI
3888 && !gui.in_use
3889# endif
3890 )
3891 set_mouse_termcode(KS_NETTERM_MOUSE,
3892 (char_u *)IF_EB("\033}", ESC_STR "}"));
3893 else
3894 del_mouse_termcode(KS_NETTERM_MOUSE);
3895# endif
3896
3897# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003898 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003899 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900# ifdef FEAT_GUI
3901 && !gui.in_use
3902# endif
3903 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003904 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3905 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 else
3907 del_mouse_termcode(KS_DEC_MOUSE);
3908# endif
3909# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003910 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003911 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912# ifdef FEAT_GUI
3913 && !gui.in_use
3914# endif
3915 )
3916 set_mouse_termcode(KS_PTERM_MOUSE,
3917 (char_u *) IF_EB("\033[", ESC_STR "["));
3918 else
3919 del_mouse_termcode(KS_PTERM_MOUSE);
3920# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003921# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003922 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003923# ifdef FEAT_GUI
3924 && !gui.in_use
3925# endif
3926 )
3927 {
3928 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003929 ? IF_EB("\233*M", CSI_STR "*M")
3930 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003931
3932 if (*p_mouse != NUL)
3933 {
3934 mch_setmouse(FALSE);
3935 setmouse();
3936 }
3937 }
3938 else
3939 del_mouse_termcode(KS_URXVT_MOUSE);
3940# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003941# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003942 if (use_xterm_mouse() == 4
3943# ifdef FEAT_GUI
3944 && !gui.in_use
3945# endif
3946 )
3947 {
3948 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003949 ? IF_EB("\233<*M", CSI_STR "<*M")
3950 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3951
3952 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3953 ? IF_EB("\233<*m", CSI_STR "<*m")
3954 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003955
3956 if (*p_mouse != NUL)
3957 {
3958 mch_setmouse(FALSE);
3959 setmouse();
3960 }
3961 }
3962 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003963 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003964 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003965 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3966 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969#endif
3970
3971/*
3972 * set screen mode, always fails.
3973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003975mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977 EMSG(_(e_screenmode));
3978 return FAIL;
3979}
3980
3981#ifndef VMS
3982
3983/*
3984 * Try to get the current window size:
3985 * 1. with an ioctl(), most accurate method
3986 * 2. from the environment variables LINES and COLUMNS
3987 * 3. from the termcap
3988 * 4. keep using the old values
3989 * Return OK when size could be determined, FAIL otherwise.
3990 */
3991 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003992mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
3994 long rows = 0;
3995 long columns = 0;
3996 char_u *p;
3997
3998 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * 1. try using an ioctl. It is the most accurate method.
4000 *
4001 * Try using TIOCGWINSZ first, some systems that have it also define
4002 * TIOCGSIZE but don't have a struct ttysize.
4003 */
4004# ifdef TIOCGWINSZ
4005 {
4006 struct winsize ws;
4007 int fd = 1;
4008
4009 /* When stdout is not a tty, use stdin for the ioctl(). */
4010 if (!isatty(fd) && isatty(read_cmd_fd))
4011 fd = read_cmd_fd;
4012 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4013 {
4014 columns = ws.ws_col;
4015 rows = ws.ws_row;
4016 }
4017 }
4018# else /* TIOCGWINSZ */
4019# ifdef TIOCGSIZE
4020 {
4021 struct ttysize ts;
4022 int fd = 1;
4023
4024 /* When stdout is not a tty, use stdin for the ioctl(). */
4025 if (!isatty(fd) && isatty(read_cmd_fd))
4026 fd = read_cmd_fd;
4027 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4028 {
4029 columns = ts.ts_cols;
4030 rows = ts.ts_lines;
4031 }
4032 }
4033# endif /* TIOCGSIZE */
4034# endif /* TIOCGWINSZ */
4035
4036 /*
4037 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004038 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4039 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004041 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 if ((p = (char_u *)getenv("LINES")))
4044 rows = atoi((char *)p);
4045 if ((p = (char_u *)getenv("COLUMNS")))
4046 columns = atoi((char *)p);
4047 }
4048
4049#ifdef HAVE_TGETENT
4050 /*
4051 * 3. try reading "co" and "li" entries from termcap
4052 */
4053 if (columns == 0 || rows == 0)
4054 getlinecol(&columns, &rows);
4055#endif
4056
4057 /*
4058 * 4. If everything fails, use the old values
4059 */
4060 if (columns <= 0 || rows <= 0)
4061 return FAIL;
4062
4063 Rows = rows;
4064 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004065 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return OK;
4067}
4068
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004069#if defined(FEAT_TERMINAL) || defined(PROTO)
4070/*
4071 * Report the windows size "rows" and "cols" to tty "fd".
4072 */
4073 int
4074mch_report_winsize(int fd, int rows, int cols)
4075{
4076# ifdef TIOCSWINSZ
4077 struct winsize ws;
4078
4079 ws.ws_col = cols;
4080 ws.ws_row = rows;
4081 ws.ws_xpixel = cols * 5;
4082 ws.ws_ypixel = rows * 10;
4083 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4084 {
4085 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4086 return OK;
4087 }
4088 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4089# else
4090# ifdef TIOCSSIZE
4091 struct ttysize ts;
4092
4093 ts.ts_cols = cols;
4094 ts.ts_lines = rows;
4095 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4096 {
4097 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4098 return OK;
4099 }
4100 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4101# endif
4102# endif
4103 return FAIL;
4104}
4105#endif
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107/*
4108 * Try to set the window size to Rows and Columns.
4109 */
4110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004111mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 if (*T_CWS)
4114 {
4115 /*
4116 * NOTE: if you get an error here that term_set_winsize() is
4117 * undefined, check the output of configure. It could probably not
4118 * find a ncurses, termcap or termlib library.
4119 */
4120 term_set_winsize((int)Rows, (int)Columns);
4121 out_flush();
4122 screen_start(); /* don't know where cursor is now */
4123 }
4124}
4125
4126#endif /* VMS */
4127
4128/*
4129 * Rows and/or Columns has changed.
4130 */
4131 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004132mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 /* Nothing to do. */
4135}
4136
Bram Moolenaar205b8862011-09-07 15:04:31 +02004137/*
4138 * Wait for process "child" to end.
4139 * Return "child" if it exited properly, <= 0 on error.
4140 */
4141 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004142wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004143{
4144 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004145 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004146
4147 while (wait_pid != child)
4148 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004149 /* When compiled with Python threads are probably used, in which case
4150 * wait() sometimes hangs for no obvious reason. Use waitpid()
4151 * instead and loop (like the GUI). Also needed for other interfaces,
4152 * they might call system(). */
4153# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004154 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004155# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004156 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004157# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158 if (wait_pid == 0)
4159 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004160 /* Wait for 1 to 10 msec before trying again. */
4161 mch_delay(delay_msec, TRUE);
4162 if (++delay_msec > 10)
4163 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004164 continue;
4165 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004166 if (wait_pid <= 0
4167# ifdef ECHILD
4168 && errno == ECHILD
4169# endif
4170 )
4171 break;
4172 }
4173 return wait_pid;
4174}
4175
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004176#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004177/*
4178 * Set the environment for a child process.
4179 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004180 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004181set_child_environment(
4182 long rows,
4183 long columns,
4184 char *term,
4185 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004186{
4187# ifdef HAVE_SETENV
4188 char envbuf[50];
4189# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004190 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004191 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004192 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004193 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004194 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004195# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004196 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004197# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004198# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004199 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004200# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004201# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004202 long colors =
4203# ifdef FEAT_GUI
4204 gui.in_use ? 256*256*256 :
4205# endif
4206 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004207
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004208# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004209 setenv("TERM", term, 1);
4210 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004211 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004212 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004213 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004214 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004215 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004216 sprintf((char *)envbuf, "%ld", colors);
4217 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004218# ifdef FEAT_TERMINAL
4219 if (is_terminal)
4220 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004221 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004222 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4223 }
4224# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004225# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004226 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004227# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004228# else
4229 /*
4230 * Putenv does not copy the string, it has to remain valid.
4231 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004232 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004234 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4235 putenv(envbuf_Term);
4236 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004237 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004238 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4239 putenv(envbuf_Lines);
4240 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4241 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004242 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004243 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4244 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004245# ifdef FEAT_TERMINAL
4246 if (is_terminal)
4247 {
4248 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004249 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004250 putenv(envbuf_Version);
4251 }
4252# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004253# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004254 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4255 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4256 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004257# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004258# endif
4259}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004260
4261 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004262set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004263{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004264 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004265}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004266#endif
4267
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004268#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004269/*
4270 * Open a PTY, with FD for the master and slave side.
4271 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4272 * When successful both file descriptors are stored.
4273 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004274 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004275open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004276{
4277 char *tty_name;
4278
4279 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4280 if (*pty_master_fd >= 0)
4281 {
4282 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4283 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4284 * adding O_NOCTTY always works when defined. */
4285#ifdef O_NOCTTY
4286 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4287#else
4288 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4289#endif
4290 if (*pty_slave_fd < 0)
4291 {
4292 close(*pty_master_fd);
4293 *pty_master_fd = -1;
4294 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004295 else if (namep != NULL)
4296 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004297 }
4298}
4299#endif
4300
Bram Moolenaarfae42832017-08-01 22:24:26 +02004301/*
4302 * Send SIGINT to a child process if "c" is an interrupt character.
4303 */
4304 void
4305may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4306{
4307# ifdef SIGINT
4308 if (c == Ctrl_C || c == intr_char)
4309 {
4310# ifdef HAVE_SETSID
4311 kill(-pid, SIGINT);
4312# else
4313 kill(0, SIGINT);
4314# endif
4315 if (wpid > 0)
4316 kill(wpid, SIGINT);
4317 }
4318# endif
4319}
4320
Bram Moolenaar13568252018-03-16 20:46:58 +01004321#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4322
4323 static int
4324build_argv(
4325 char_u *cmd,
4326 char ***argvp,
4327 char_u **sh_tofree,
4328 char_u **shcf_tofree)
4329{
4330 char **argv = NULL;
4331 int argc;
4332
4333 *sh_tofree = vim_strsave(p_sh);
4334 if (*sh_tofree == NULL) /* out of memory */
4335 return FAIL;
4336
4337 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4338 return FAIL;
4339 *argvp = argv;
4340
4341 if (cmd != NULL)
4342 {
4343 char_u *s;
4344 char_u *p;
4345
4346 if (extra_shell_arg != NULL)
4347 argv[argc++] = (char *)extra_shell_arg;
4348
4349 /* Break 'shellcmdflag' into white separated parts. This doesn't
4350 * handle quoted strings, they are very unlikely to appear. */
4351 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4352 if (*shcf_tofree == NULL) /* out of memory */
4353 return FAIL;
4354 s = *shcf_tofree;
4355 p = p_shcf;
4356 while (*p != NUL)
4357 {
4358 argv[argc++] = (char *)s;
4359 while (*p && *p != ' ' && *p != TAB)
4360 *s++ = *p++;
4361 *s++ = NUL;
4362 p = skipwhite(p);
4363 }
4364
4365 argv[argc++] = (char *)cmd;
4366 }
4367 argv[argc] = NULL;
4368 return OK;
4369}
4370#endif
4371
4372#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4373/*
4374 * Use a terminal window to run a shell command in.
4375 */
4376 static int
4377mch_call_shell_terminal(
4378 char_u *cmd,
4379 int options UNUSED) /* SHELL_*, see vim.h */
4380{
4381 jobopt_T opt;
4382 char **argv = NULL;
4383 char_u *tofree1 = NULL;
4384 char_u *tofree2 = NULL;
4385 int retval = -1;
4386 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004387 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004388 aco_save_T aco;
4389 oparg_T oa; /* operator arguments */
4390
4391 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4392 goto theend;
4393
4394 init_job_options(&opt);
4395 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4396 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004397 if (buf == NULL)
4398 goto theend;
4399
4400 job = term_getjob(buf->b_term);
4401 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004402
4403 /* Find a window to make "buf" curbuf. */
4404 aucmd_prepbuf(&aco, buf);
4405
4406 clear_oparg(&oa);
4407 while (term_use_loop())
4408 {
4409 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4410 {
4411 /* If terminal_loop() returns OK we got a key that is handled
4412 * in Normal model. We don't do redrawing anyway. */
4413 if (terminal_loop(TRUE) == OK)
4414 normal_cmd(&oa, TRUE);
4415 }
4416 else
4417 normal_cmd(&oa, TRUE);
4418 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004419 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004420 ch_log(NULL, "system command finished");
4421
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004422 job_unref(job);
4423
Bram Moolenaar13568252018-03-16 20:46:58 +01004424 /* restore curwin/curbuf and a few other things */
4425 aucmd_restbuf(&aco);
4426
4427 wait_return(TRUE);
4428 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4429
4430theend:
4431 vim_free(argv);
4432 vim_free(tofree1);
4433 vim_free(tofree2);
4434 return retval;
4435}
4436#endif
4437
4438#ifdef USE_SYSTEM
4439/*
4440 * Use system() to start the shell: simple but slow.
4441 */
4442 static int
4443mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004444 char_u *cmd,
4445 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447#ifdef VMS
4448 char *ifn = NULL;
4449 char *ofn = NULL;
4450#endif
4451 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004452 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004453 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455 out_flush();
4456
4457 if (options & SHELL_COOKED)
4458 settmode(TMODE_COOK); /* set to normal mode */
4459
Bram Moolenaar62b42182010-09-21 22:09:37 +02004460# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004461 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004462 loose_clipboard();
4463# endif
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (cmd == NULL)
4466 x = system((char *)p_sh);
4467 else
4468 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004469# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (ofn = strchr((char *)cmd, '>'))
4471 *ofn++ = '\0';
4472 if (ifn = strchr((char *)cmd, '<'))
4473 {
4474 char *p;
4475
4476 *ifn++ = '\0';
4477 p = strchr(ifn,' '); /* chop off any trailing spaces */
4478 if (p)
4479 *p = '\0';
4480 }
4481 if (ofn)
4482 x = vms_sys((char *)cmd, ofn, ifn);
4483 else
4484 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004485# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 newcmd = lalloc(STRLEN(p_sh)
4487 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4488 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4489 if (newcmd == NULL)
4490 x = 0;
4491 else
4492 {
4493 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4494 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4495 (char *)p_shcf,
4496 (char *)cmd);
4497 x = system((char *)newcmd);
4498 vim_free(newcmd);
4499 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502# ifdef VMS
4503 x = vms_sys_status(x);
4504# endif
4505 if (emsg_silent)
4506 ;
4507 else if (x == 127)
4508 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else if (x && !(options & SHELL_SILENT))
4510 {
4511 MSG_PUTS(_("\nshell returned "));
4512 msg_outnum((long)x);
4513 msg_putchar('\n');
4514 }
4515
4516 if (tmode == TMODE_RAW)
4517 settmode(TMODE_RAW); /* set to raw mode */
4518# ifdef FEAT_TITLE
4519 resettitle();
4520# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004521# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4522 restore_clipboard();
4523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaar13568252018-03-16 20:46:58 +01004527#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaardf177f62005-02-22 08:39:57 +00004529# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4530 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004531# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaar13568252018-03-16 20:46:58 +01004533/*
4534 * Don't use system(), use fork()/exec().
4535 */
4536 static int
4537mch_call_shell_fork(
4538 char_u *cmd,
4539 int options) /* SHELL_*, see vim.h */
4540{
4541 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004543 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 pid_t wait_pid = 0;
4545# ifdef HAVE_UNION_WAIT
4546 union wait status;
4547# else
4548 int status = -1;
4549# endif
4550 int retval = -1;
4551 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004552 char_u *tofree1 = NULL;
4553 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004556# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004558# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004559 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 int fd_fromshell[2];
4561 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004562 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 out_flush();
4565 if (options & SHELL_COOKED)
4566 settmode(TMODE_COOK); /* set to normal mode */
4567
Bram Moolenaar13568252018-03-16 20:46:58 +01004568 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004569 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004572 * For the GUI, when writing the output into the buffer and when reading
4573 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4574 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004576 if ((options & (SHELL_READ|SHELL_WRITE))
4577# ifdef FEAT_GUI
4578 || (gui.in_use && show_shell_mess)
4579# endif
4580 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004582# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /*
4584 * Try to open a master pty.
4585 * If this works, open the slave pty.
4586 * If the slave can't be opened, close the master pty.
4587 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004588 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004589 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 /*
4591 * If not opening a pty or it didn't work, try using pipes.
4592 */
4593 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 pipe_error = (pipe(fd_toshell) < 0);
4597 if (!pipe_error) /* pipe create OK */
4598 {
4599 pipe_error = (pipe(fd_fromshell) < 0);
4600 if (pipe_error) /* pipe create failed */
4601 {
4602 close(fd_toshell[0]);
4603 close(fd_toshell[1]);
4604 }
4605 }
4606 if (pipe_error)
4607 {
4608 MSG_PUTS(_("\nCannot create pipes\n"));
4609 out_flush();
4610 }
4611 }
4612 }
4613
4614 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004616 SIGSET_DECL(curset)
4617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618# ifdef __BEOS__
4619 beos_cleanup_read_thread();
4620# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004621
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004622 BLOCK_SIGNALS(&curset);
4623 pid = fork(); /* maybe we should use vfork() */
4624 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004626 UNBLOCK_SIGNALS(&curset);
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004629 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004631 || (gui.in_use && show_shell_mess)
4632# endif
4633 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004635# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (pty_master_fd >= 0) /* close the pseudo tty */
4637 {
4638 close(pty_master_fd);
4639 close(pty_slave_fd);
4640 }
4641 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 close(fd_toshell[0]);
4645 close(fd_toshell[1]);
4646 close(fd_fromshell[0]);
4647 close(fd_fromshell[1]);
4648 }
4649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 else if (pid == 0) /* child */
4652 {
4653 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004654 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Bram Moolenaar819524702018-02-27 19:10:00 +01004656# ifdef FEAT_JOB_CHANNEL
4657 if (ch_log_active())
4658 /* close the log file in the child */
4659 ch_logfile((char_u *)"", (char_u *)"");
4660# endif
4661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (!show_shell_mess || (options & SHELL_EXPAND))
4663 {
4664 int fd;
4665
4666 /*
4667 * Don't want to show any message from the shell. Can't just
4668 * close stdout and stderr though, because some systems will
4669 * break if you try to write to them after that, so we must
4670 * use dup() to replace them with something else -- webb
4671 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4672 * waiting for input.
4673 */
4674 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4675 fclose(stdin);
4676 fclose(stdout);
4677 fclose(stderr);
4678
4679 /*
4680 * If any of these open()'s and dup()'s fail, we just continue
4681 * anyway. It's not fatal, and on most systems it will make
4682 * no difference at all. On a few it will cause the execvp()
4683 * to exit with a non-zero status even when the completion
4684 * could be done, which is nothing too serious. If the open()
4685 * or dup() failed we'd just do the same thing ourselves
4686 * anyway -- webb
4687 */
4688 if (fd >= 0)
4689 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004690 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4691 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4692 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
4694 /* Don't need this now that we've duplicated it */
4695 close(fd);
4696 }
4697 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004698 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004700 || gui.in_use
4701# endif
4702 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004706 /* Create our own process group, so that the child and all its
4707 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004708 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004709 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004710 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004711 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004712# if defined(SIGHUP)
4713 /* When doing "!xterm&" and 'shell' is bash: the shell
4714 * will exit and send SIGHUP to all processes in its
4715 * group, killing the just started process. Ignore SIGHUP
4716 * to avoid that. (suggested by Simon Schubert)
4717 */
4718 signal(SIGHUP, SIG_IGN);
4719# endif
4720 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004721# endif
4722# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004723 if (pty_slave_fd >= 0)
4724 {
4725 /* push stream discipline modules */
4726 if (options & SHELL_COOKED)
4727 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004729 /* Try to become controlling tty (probably doesn't work,
4730 * unless run by root) */
4731 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004733 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004734# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004735 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaara5792f52005-11-23 21:25:05 +00004737 /*
4738 * stderr is only redirected when using the GUI, so that a
4739 * program like gpg can still access the terminal to get a
4740 * passphrase using stderr.
4741 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004742# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (pty_master_fd >= 0)
4744 {
4745 close(pty_master_fd); /* close master side of pty */
4746
4747 /* set up stdin/stdout/stderr for the child */
4748 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004749 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004751 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004752 if (gui.in_use)
4753 {
4754 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004755 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
4758 close(pty_slave_fd); /* has been dupped, close it now */
4759 }
4760 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 /* set up stdin for the child */
4764 close(fd_toshell[1]);
4765 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004766 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 close(fd_toshell[0]);
4768
4769 /* set up stdout for the child */
4770 close(fd_fromshell[0]);
4771 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004772 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 close(fd_fromshell[1]);
4774
Bram Moolenaara5792f52005-11-23 21:25:05 +00004775# ifdef FEAT_GUI
4776 if (gui.in_use)
4777 {
4778 /* set up stderr for the child */
4779 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004780 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004781 }
4782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /*
4787 * There is no type cast for the argv, because the type may be
4788 * different on different machines. This may cause a warning
4789 * message with strict compilers, don't worry about it.
4790 * Call _exit() instead of exit() to avoid closing the connection
4791 * to the X server (esp. with GTK, which uses atexit()).
4792 */
4793 execvp(argv[0], argv);
4794 _exit(EXEC_FAILED); /* exec failed, return failure code */
4795 }
4796 else /* parent */
4797 {
4798 /*
4799 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
4802 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004803 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004804 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004805# ifdef FEAT_JOB_CHANNEL
4806 ++dont_check_job_ended;
4807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /*
4809 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004810 * This is also used to pipe stdin/stdout to/from the external
4811 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813 if ((options & (SHELL_READ|SHELL_WRITE))
4814# ifdef FEAT_GUI
4815 || (gui.in_use && show_shell_mess)
4816# endif
4817 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004819# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004821# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4825 int ta_len = 0; /* valid bytes in ta_buf[] */
4826 int len;
4827 int p_more_save;
4828 int old_State;
4829 int c;
4830 int toshell_fd;
4831 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832 garray_T ga;
4833 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004834# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4835 struct timeval start_tv;
4836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (pty_master_fd >= 0)
4840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 fromshell_fd = pty_master_fd;
4842 toshell_fd = dup(pty_master_fd);
4843 }
4844 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
4847 close(fd_toshell[0]);
4848 close(fd_fromshell[1]);
4849 toshell_fd = fd_toshell[1];
4850 fromshell_fd = fd_fromshell[0];
4851 }
4852
4853 /*
4854 * Write to the child if there are typed characters.
4855 * Read from the child if there are characters available.
4856 * Repeat the reading a few times if more characters are
4857 * available. Need to check for typed keys now and then, but
4858 * not too often (delays when no chars are available).
4859 * This loop is quit if no characters can be read from the pty
4860 * (WaitForChar detected special condition), or there are no
4861 * characters available and the child has exited.
4862 * Only check if the child has exited when there is no more
4863 * output. The child may exit before all the output has
4864 * been printed.
4865 *
4866 * Currently this busy loops!
4867 * This can probably dead-lock when the write blocks!
4868 */
4869 p_more_save = p_more;
4870 p_more = FALSE;
4871 old_State = State;
4872 State = EXTERNCMD; /* don't redraw at window resize */
4873
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004874 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004875 {
4876 /* Fork a process that will write the lines to the
4877 * external program. */
4878 if ((wpid = fork()) == -1)
4879 {
4880 MSG_PUTS(_("\nCannot fork\n"));
4881 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004882 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004883 {
4884 linenr_T lnum = curbuf->b_op_start.lnum;
4885 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004886 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 size_t l;
4888
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004889 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890 for (;;)
4891 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004892 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004893 if (l == 0)
4894 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004895 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004896 /* NL -> NUL translation */
4897 len = write(toshell_fd, "", (size_t)1);
4898 else
4899 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004900 char_u *s = vim_strchr(lp + written, NL);
4901
Bram Moolenaar89d40322006-08-29 15:30:07 +00004902 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004903 s == NULL ? l
4904 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004906 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004907 {
4908 /* Finished a line, add a NL, unless this line
4909 * should not have one. */
4910 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004911 || (!curbuf->b_p_bin
4912 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004913 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 && (lnum !=
4915 curbuf->b_ml.ml_line_count
4916 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004917 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004918 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 ++lnum;
4920 if (lnum > curbuf->b_op_end.lnum)
4921 {
4922 /* finished all the lines, close pipe */
4923 close(toshell_fd);
4924 toshell_fd = -1;
4925 break;
4926 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004927 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004928 written = 0;
4929 }
4930 else if (len > 0)
4931 written += len;
4932 }
4933 _exit(0);
4934 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004935 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004936 {
4937 close(toshell_fd);
4938 toshell_fd = -1;
4939 }
4940 }
4941
4942 if (options & SHELL_READ)
4943 ga_init2(&ga, 1, BUFLEN);
4944
4945 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004946# ifdef ELAPSED_FUNC
4947 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 for (;;)
4950 {
4951 /*
4952 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004953 * if there are any.
4954 * Don't do this if we are expanding wild cards (would eat
4955 * typeahead).
4956 * Don't do this when filtering and terminal is in cooked
4957 * mode, the shell command will handle the I/O. Avoids
4958 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004959 * Don't get characters when the child has already
4960 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004961 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004962 * while (noread_cnt > 4), avoids that ":r !ls" eats
4963 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 */
4965 len = 0;
4966 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004967 && ((options &
4968 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4969 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004970# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004971 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004972# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004973 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004974 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004975 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004977 if (ta_len == 0)
4978 {
4979 /* Get extra characters when we don't have any.
4980 * Reset the counter and timer. */
4981 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004982# ifdef ELAPSED_FUNC
4983 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004984# endif
4985 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4986 }
4987 if (ta_len > 0 || len > 0)
4988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /*
4990 * For pipes:
4991 * Check for CTRL-C: send interrupt signal to child.
4992 * Check for CTRL-D: EOF, close pipe to child.
4993 */
4994 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /*
4997 * Send SIGINT to the child's group or all
4998 * processes in our group.
4999 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005000 may_send_sigint(ta_buf[ta_len], pid, wpid);
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (pty_master_fd < 0 && toshell_fd >= 0
5003 && ta_buf[ta_len] == Ctrl_D)
5004 {
5005 close(toshell_fd);
5006 toshell_fd = -1;
5007 }
5008 }
5009
5010 /* replace K_BS by <BS> and K_DEL by <DEL> */
5011 for (i = ta_len; i < ta_len + len; ++i)
5012 {
5013 if (ta_buf[i] == CSI && len - i > 2)
5014 {
5015 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5016 if (c == K_DEL || c == K_KDEL || c == K_BS)
5017 {
5018 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5019 (size_t)(len - i - 2));
5020 if (c == K_DEL || c == K_KDEL)
5021 ta_buf[i] = DEL;
5022 else
5023 ta_buf[i] = Ctrl_H;
5024 len -= 2;
5025 }
5026 }
5027 else if (ta_buf[i] == '\r')
5028 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005029# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005031 i += (*mb_ptr2len_len)(ta_buf + i,
5032 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035
5036 /*
5037 * For pipes: echo the typed characters.
5038 * For a pty this does not seem to work.
5039 */
5040 if (pty_master_fd < 0)
5041 {
5042 for (i = ta_len; i < ta_len + len; ++i)
5043 {
5044 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5045 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005046# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 else if (has_mbyte)
5048 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005049 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 msg_outtrans_len(ta_buf + i, l);
5052 i += l - 1;
5053 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 else
5056 msg_outtrans_len(ta_buf + i, 1);
5057 }
5058 windgoto(msg_row, msg_col);
5059 out_flush();
5060 }
5061
5062 ta_len += len;
5063
5064 /*
5065 * Write the characters to the child, unless EOF has
5066 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005067 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005068 * When writing buffer lines, drop the typed
5069 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005071 if (options & SHELL_WRITE)
5072 ta_len = 0;
5073 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5076 if (len > 0)
5077 {
5078 ta_len -= len;
5079 mch_memmove(ta_buf, ta_buf + len, ta_len);
5080 }
5081 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084
Bram Moolenaardf177f62005-02-22 08:39:57 +00005085 if (got_int)
5086 {
5087 /* CTRL-C sends a signal to the child, we ignore it
5088 * ourselves */
5089# ifdef HAVE_SETSID
5090 kill(-pid, SIGINT);
5091# else
5092 kill(0, SIGINT);
5093# endif
5094 if (wpid > 0)
5095 kill(wpid, SIGINT);
5096 got_int = FALSE;
5097 }
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
5100 * Check if the child has any characters to be printed.
5101 * Read them and write them to our window. Repeat this as
5102 * long as there is something to do, avoid the 10ms wait
5103 * for mch_inchar(), or sending typeahead characters to
5104 * the external process.
5105 * TODO: This should handle escape sequences, compatible
5106 * to some terminal (vt52?).
5107 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005108 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005109 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005111 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005112# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005114# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 );
5118 if (len <= 0) /* end of file or error */
5119 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005120
5121 noread_cnt = 0;
5122 if (options & SHELL_READ)
5123 {
5124 /* Do NUL -> NL translation, append NL separated
5125 * lines to the current buffer. */
5126 for (i = 0; i < len; ++i)
5127 {
5128 if (buffer[i] == NL)
5129 append_ga_line(&ga);
5130 else if (buffer[i] == NUL)
5131 ga_append(&ga, NL);
5132 else
5133 ga_append(&ga, buffer[i]);
5134 }
5135 }
5136# ifdef FEAT_MBYTE
5137 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
5139 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005140 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
Bram Moolenaardf177f62005-02-22 08:39:57 +00005142 len += buffer_off;
5143 buffer[len] = NUL;
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /* Check if the last character in buffer[] is
5146 * incomplete, keep these bytes for the next
5147 * round. */
5148 for (p = buffer; p < buffer + len; p += l)
5149 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005150 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (l == 0)
5152 l = 1; /* NUL byte? */
5153 else if (MB_BYTE2LEN(*p) != l)
5154 break;
5155 }
5156 if (p == buffer) /* no complete character */
5157 {
5158 /* avoid getting stuck at an illegal byte */
5159 if (len >= 12)
5160 ++p;
5161 else
5162 {
5163 buffer_off = len;
5164 continue;
5165 }
5166 }
5167 c = *p;
5168 *p = NUL;
5169 msg_puts(buffer);
5170 if (p < buffer + len)
5171 {
5172 *p = c;
5173 buffer_off = (buffer + len) - p;
5174 mch_memmove(buffer, p, buffer_off);
5175 continue;
5176 }
5177 buffer_off = 0;
5178 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005179# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 buffer[len] = NUL;
5183 msg_puts(buffer);
5184 }
5185
5186 windgoto(msg_row, msg_col);
5187 cursor_on();
5188 out_flush();
5189 if (got_int)
5190 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005191
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005192# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005193 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005194 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005195 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005196
5197 /* Avoid that we keep looping here without
5198 * checking for a CTRL-C for a long time. Don't
5199 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005200 if (msec > 2000)
5201 {
5202 noread_cnt = 5;
5203 break;
5204 }
5205 }
5206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005209 /* If we already detected the child has finished, continue
5210 * reading output for a short while. Some text may be
5211 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005212 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005213 {
5214 if (noread_cnt < 5)
5215 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005216 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005217 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 /*
5220 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005221 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005223# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005224 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005225# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5229 || (wait_pid == pid && WIFEXITED(status)))
5230 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005231 /* Don't break the loop yet, try reading more
5232 * characters from "fromshell_fd" first. When using
5233 * pipes there might still be something to read and
5234 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005237 else
5238 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005239
Bram Moolenaar95a51352013-03-21 22:53:50 +01005240# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005241 /* Handle any X events, e.g. serving the clipboard. */
5242 clip_update();
5243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245finished:
5246 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005247 if (options & SHELL_READ)
5248 {
5249 if (ga.ga_len > 0)
5250 {
5251 append_ga_line(&ga);
5252 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005253 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005254 }
5255 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005256 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005257 ga_clear(&ga);
5258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 /*
5261 * Give all typeahead that wasn't used back to ui_inchar().
5262 */
5263 if (ta_len)
5264 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 State = old_State;
5266 if (toshell_fd >= 0)
5267 close(toshell_fd);
5268 close(fromshell_fd);
5269 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005270# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005271 else
5272 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005273 long delay_msec = 1;
5274
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005275 /*
5276 * Similar to the loop above, but only handle X events, no
5277 * I/O.
5278 */
5279 for (;;)
5280 {
5281 if (got_int)
5282 {
5283 /* CTRL-C sends a signal to the child, we ignore it
5284 * ourselves */
5285# ifdef HAVE_SETSID
5286 kill(-pid, SIGINT);
5287# else
5288 kill(0, SIGINT);
5289# endif
5290 got_int = FALSE;
5291 }
5292# ifdef __NeXT__
5293 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5294# else
5295 wait_pid = waitpid(pid, &status, WNOHANG);
5296# endif
5297 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5298 || (wait_pid == pid && WIFEXITED(status)))
5299 {
5300 wait_pid = pid;
5301 break;
5302 }
5303
5304 /* Handle any X events, e.g. serving the clipboard. */
5305 clip_update();
5306
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005307 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5308 * less time. */
5309 mch_delay(delay_msec, TRUE);
5310 if (++delay_msec > 10)
5311 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005312 }
5313 }
5314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 /*
5317 * Wait until our child has exited.
5318 * Ignore wait() returning pids of other children and returning
5319 * because of some signal like SIGWINCH.
5320 * Don't wait if wait_pid was already set above, indicating the
5321 * child already exited.
5322 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005323 if (wait_pid != pid)
5324 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
Bram Moolenaar624891f2010-10-13 16:22:09 +02005326# ifdef FEAT_GUI
5327 /* Close slave side of pty. Only do this after the child has
5328 * exited, otherwise the child may hang when it tries to write on
5329 * the pty. */
5330 if (pty_master_fd >= 0)
5331 close(pty_slave_fd);
5332# endif
5333
Bram Moolenaardf177f62005-02-22 08:39:57 +00005334 /* Make sure the child that writes to the external program is
5335 * dead. */
5336 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005337 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005338 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005339 wait4pid(wpid, NULL);
5340 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005341
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005342# ifdef FEAT_JOB_CHANNEL
5343 --dont_check_job_ended;
5344# endif
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 /*
5347 * Set to raw mode right now, otherwise a CTRL-C after
5348 * catch_signals() will kill Vim.
5349 */
5350 if (tmode == TMODE_RAW)
5351 settmode(TMODE_RAW);
5352 did_settmode = TRUE;
5353 set_signals();
5354
5355 if (WIFEXITED(status))
5356 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005357 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005359 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
5361 if (retval == EXEC_FAILED)
5362 {
5363 MSG_PUTS(_("\nCannot execute shell "));
5364 msg_outtrans(p_sh);
5365 msg_putchar('\n');
5366 }
5367 else if (!(options & SHELL_SILENT))
5368 {
5369 MSG_PUTS(_("\nshell returned "));
5370 msg_outnum((long)retval);
5371 msg_putchar('\n');
5372 }
5373 }
5374 }
5375 else
5376 MSG_PUTS(_("\nCommand terminated\n"));
5377 }
5378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380error:
5381 if (!did_settmode)
5382 if (tmode == TMODE_RAW)
5383 settmode(TMODE_RAW); /* set to raw mode */
5384# ifdef FEAT_TITLE
5385 resettitle();
5386# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005387 vim_free(argv);
5388 vim_free(tofree1);
5389 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005394
5395 int
5396mch_call_shell(
5397 char_u *cmd,
5398 int options) /* SHELL_*, see vim.h */
5399{
5400#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5401 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5402 return mch_call_shell_terminal(cmd, options);
5403#endif
5404#ifdef USE_SYSTEM
5405 return mch_call_shell_system(cmd, options);
5406#else
5407 return mch_call_shell_fork(cmd, options);
5408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409}
5410
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005411#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005412 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005413mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005414{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005415 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005416 int fd_in[2] = {-1, -1}; /* for stdin */
5417 int fd_out[2] = {-1, -1}; /* for stdout */
5418 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005419 int pty_master_fd = -1;
5420 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005421 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005422 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5423 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5424 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005425 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005426 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5427 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005428 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005429 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005430 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005431
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005432 if (use_out_for_err && use_null_for_out)
5433 use_null_for_err = TRUE;
5434
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005435 /* default is to fail */
5436 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005437
Bram Moolenaarb2412082017-08-20 18:09:14 +02005438 if (options->jo_pty
5439 && (!(use_file_for_in || use_null_for_in)
5440 || !(use_file_for_in || use_null_for_out)
5441 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005442 {
5443 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5444 if (job->jv_tty_out != NULL)
5445 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5446 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005447
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005448 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005449 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005450 if (use_file_for_in)
5451 {
5452 char_u *fname = options->jo_io_name[PART_IN];
5453
5454 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5455 if (fd_in[0] < 0)
5456 {
5457 EMSG2(_(e_notopen), fname);
5458 goto failed;
5459 }
5460 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005461 else
5462 /* When writing buffer lines to the input don't use the pty, so that
5463 * the pipe can be closed when all lines were written. */
5464 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5465 && pipe(fd_in) < 0)
5466 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005467
5468 if (use_file_for_out)
5469 {
5470 char_u *fname = options->jo_io_name[PART_OUT];
5471
5472 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5473 if (fd_out[1] < 0)
5474 {
5475 EMSG2(_(e_notopen), fname);
5476 goto failed;
5477 }
5478 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005479 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005480 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005481
5482 if (use_file_for_err)
5483 {
5484 char_u *fname = options->jo_io_name[PART_ERR];
5485
5486 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5487 if (fd_err[1] < 0)
5488 {
5489 EMSG2(_(e_notopen), fname);
5490 goto failed;
5491 }
5492 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005493 else if (!use_out_for_err && !use_null_for_err
5494 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005495 goto failed;
5496
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005497 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5498 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005499 if (options->jo_set & JO_CHANNEL)
5500 {
5501 channel = options->jo_channel;
5502 if (channel != NULL)
5503 ++channel->ch_refcount;
5504 }
5505 else
5506 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005507 if (channel == NULL)
5508 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005509 if (job->jv_tty_out != NULL)
5510 ch_log(channel, "using pty %s on fd %d",
5511 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005512 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005513
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005514 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005515 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005516 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005518 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005519 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005520 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005522 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005523 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005524 int null_fd = -1;
5525 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005526
Bram Moolenaar835dc632016-02-07 14:27:38 +01005527 /* child */
5528 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005529 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005530
Bram Moolenaar819524702018-02-27 19:10:00 +01005531# ifdef FEAT_JOB_CHANNEL
5532 if (ch_log_active())
5533 /* close the log file in the child */
5534 ch_logfile((char_u *)"", (char_u *)"");
5535# endif
5536
Bram Moolenaar835dc632016-02-07 14:27:38 +01005537# ifdef HAVE_SETSID
5538 /* Create our own process group, so that the child and all its
5539 * children can be kill()ed. Don't do this when using pipes,
5540 * because stdin is not a tty, we would lose /dev/tty. */
5541 (void)setsid();
5542# endif
5543
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005544# ifdef FEAT_TERMINAL
5545 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005546 {
5547 char *term = (char *)T_NAME;
5548
5549#ifdef FEAT_GUI
5550 if (term_is_gui(T_NAME))
5551 /* In the GUI 'term' is not what we want, use $TERM. */
5552 term = getenv("TERM");
5553#endif
5554 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5555 * back to "xterm". */
5556 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5557 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005558 set_child_environment(
5559 (long)options->jo_term_rows,
5560 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005561 term,
5562 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005563 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005564 else
5565# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005566 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005567
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005568 if (options->jo_env != NULL)
5569 {
5570 dict_T *dict = options->jo_env;
5571 hashitem_T *hi;
5572 int todo = (int)dict->dv_hashtab.ht_used;
5573
5574 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5575 if (!HASHITEM_EMPTY(hi))
5576 {
5577 typval_T *item = &dict_lookup(hi)->di_tv;
5578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005579 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005580 --todo;
5581 }
5582 }
5583
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005584 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005585 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005586 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005587 if (null_fd < 0)
5588 {
5589 perror("opening /dev/null failed");
5590 _exit(OPEN_NULL_FAILED);
5591 }
5592 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005593
Bram Moolenaar223896d2017-08-02 22:33:28 +02005594 if (pty_slave_fd >= 0)
5595 {
5596 /* push stream discipline modules */
5597 SetupSlavePTY(pty_slave_fd);
5598# ifdef TIOCSCTTY
5599 /* Try to become controlling tty (probably doesn't work,
5600 * unless run by root) */
5601 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5602# endif
5603 }
5604
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005605 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005606 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005607 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005608 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005609 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005610 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005611 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005612 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005613
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005614 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005615 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005616 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005617 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005618 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005619 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005620 }
5621 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005622 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005623 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005624 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005625 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005626 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005627
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005628 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005629 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005630 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005631 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005632 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005633 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005634 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005635 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005636
5637 if (fd_in[0] >= 0)
5638 close(fd_in[0]);
5639 if (fd_in[1] >= 0)
5640 close(fd_in[1]);
5641 if (fd_out[0] >= 0)
5642 close(fd_out[0]);
5643 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005644 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005645 if (fd_err[0] >= 0)
5646 close(fd_err[0]);
5647 if (fd_err[1] >= 0)
5648 close(fd_err[1]);
5649 if (pty_master_fd >= 0)
5650 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005651 close(pty_master_fd); /* not used in the child */
5652 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005653 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005654
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005655 if (null_fd >= 0)
5656 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005657
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005658 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5659 _exit(EXEC_FAILED);
5660
Bram Moolenaar835dc632016-02-07 14:27:38 +01005661 /* See above for type of argv. */
5662 execvp(argv[0], argv);
5663
Bram Moolenaar4694a172016-04-21 14:05:23 +02005664 if (stderr_works)
5665 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005666# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005667 /* calling free_all_mem() here causes problems. Ignore valgrind
5668 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005669# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005670 _exit(EXEC_FAILED); /* exec failed, return failure code */
5671 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005672
5673 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005674 UNBLOCK_SIGNALS(&curset);
5675
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005676 job->jv_pid = pid;
5677 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005678 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005679
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005680 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005681 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005682 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005683 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005684 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005685 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005686 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005687 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005688 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005689 if (channel != NULL)
5690 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005691 int in_fd = use_file_for_in || use_null_for_in
5692 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5693 int out_fd = use_file_for_out || use_null_for_out
5694 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005695 /* When using pty_master_fd only set it for stdout, do not duplicate it
5696 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005697 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005698 ? INVALID_FD
5699 : fd_err[0] >= 0
5700 ? fd_err[0]
5701 : (out_fd == pty_master_fd
5702 ? INVALID_FD
5703 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005704
5705 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005706 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005707 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005708 else
5709 {
5710 if (fd_in[1] >= 0)
5711 close(fd_in[1]);
5712 if (fd_out[0] >= 0)
5713 close(fd_out[0]);
5714 if (fd_err[0] >= 0)
5715 close(fd_err[0]);
5716 if (pty_master_fd >= 0)
5717 close(pty_master_fd);
5718 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005720 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721 return;
5722
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005723failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005724 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005725 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005727 if (fd_in[1] >= 0)
5728 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005729 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005730 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005731 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005732 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005733 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005735 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005736 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005737 if (pty_master_fd >= 0)
5738 close(pty_master_fd);
5739 if (pty_slave_fd >= 0)
5740 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005741}
5742
5743 char *
5744mch_job_status(job_T *job)
5745{
5746# ifdef HAVE_UNION_WAIT
5747 union wait status;
5748# else
5749 int status = -1;
5750# endif
5751 pid_t wait_pid = 0;
5752
5753# ifdef __NeXT__
5754 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5755# else
5756 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5757# endif
5758 if (wait_pid == -1)
5759 {
5760 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005761 if (job->jv_status < JOB_ENDED)
5762 ch_log(job->jv_channel, "Job no longer exists: %s",
5763 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005764 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005765 }
5766 if (wait_pid == 0)
5767 return "run";
5768 if (WIFEXITED(status))
5769 {
5770 /* LINTED avoid "bitwise operation on signed value" */
5771 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005772 if (job->jv_status < JOB_ENDED)
5773 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005774 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005775 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005776 if (WIFSIGNALED(status))
5777 {
5778 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005779 if (job->jv_status < JOB_ENDED)
5780 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005781 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005782 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005783 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005784
5785return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005786 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005787 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005788 return "dead";
5789}
5790
5791 job_T *
5792mch_detect_ended_job(job_T *job_list)
5793{
5794# ifdef HAVE_UNION_WAIT
5795 union wait status;
5796# else
5797 int status = -1;
5798# endif
5799 pid_t wait_pid = 0;
5800 job_T *job;
5801
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005802# ifndef USE_SYSTEM
5803 /* Do not do this when waiting for a shell command to finish, we would get
5804 * the exit value here (and discard it), the exit value obtained there
5805 * would then be wrong. */
5806 if (dont_check_job_ended > 0)
5807 return NULL;
5808# endif
5809
Bram Moolenaar97792de2016-10-15 18:36:49 +02005810# ifdef __NeXT__
5811 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5812# else
5813 wait_pid = waitpid(-1, &status, WNOHANG);
5814# endif
5815 if (wait_pid <= 0)
5816 /* no process ended */
5817 return NULL;
5818 for (job = job_list; job != NULL; job = job->jv_next)
5819 {
5820 if (job->jv_pid == wait_pid)
5821 {
5822 if (WIFEXITED(status))
5823 /* LINTED avoid "bitwise operation on signed value" */
5824 job->jv_exitval = WEXITSTATUS(status);
5825 else if (WIFSIGNALED(status))
5826 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005827 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005828 {
5829 ch_log(job->jv_channel, "Job ended");
5830 job->jv_status = JOB_ENDED;
5831 }
5832 return job;
5833 }
5834 }
5835 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005836}
5837
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005838/*
5839 * Send a (deadly) signal to "job".
5840 * Return FAIL if "how" is not a valid name.
5841 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005842 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005843mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005844{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005845 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846
Bram Moolenaar923d9262016-02-25 20:56:01 +01005847 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005848 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005849 else if (STRCMP(how, "hup") == 0)
5850 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851 else if (STRCMP(how, "quit") == 0)
5852 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005853 else if (STRCMP(how, "int") == 0)
5854 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 else if (STRCMP(how, "kill") == 0)
5856 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005857#ifdef SIGWINCH
5858 else if (STRCMP(how, "winch") == 0)
5859 sig = SIGWINCH;
5860#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005861 else if (isdigit(*how))
5862 sig = atoi((char *)how);
5863 else
5864 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005865
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005866 // Never kill ourselves!
5867 if (job->jv_pid != 0)
5868 {
5869 // TODO: have an option to only kill the process, not the group?
5870 kill(-job->jv_pid, sig);
5871 kill(job->jv_pid, sig);
5872 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005873
Bram Moolenaar835dc632016-02-07 14:27:38 +01005874 return OK;
5875}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005876
5877/*
5878 * Clear the data related to "job".
5879 */
5880 void
5881mch_clear_job(job_T *job)
5882{
5883 /* call waitpid because child process may become zombie */
5884# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005885 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005886# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005887 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888# endif
5889}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005890#endif
5891
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005892#if defined(FEAT_TERMINAL) || defined(PROTO)
5893 int
5894mch_create_pty_channel(job_T *job, jobopt_T *options)
5895{
5896 int pty_master_fd = -1;
5897 int pty_slave_fd = -1;
5898 channel_T *channel;
5899
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005900 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5901 if (job->jv_tty_out != NULL)
5902 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005903 close(pty_slave_fd);
5904
5905 channel = add_channel();
5906 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005907 {
5908 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005909 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005910 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005911 if (job->jv_tty_out != NULL)
5912 ch_log(channel, "using pty %s on fd %d",
5913 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005914 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5915 channel->ch_keep_open = TRUE;
5916
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005917 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5918 * it only needs to be read once. */
5919 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005920 channel_set_job(channel, job, options);
5921 return OK;
5922}
5923#endif
5924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925/*
5926 * Check for CTRL-C typed by reading all available characters.
5927 * In cooked mode we should get SIGINT, no need to check.
5928 */
5929 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005930mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005932 if ((curr_tmode == TMODE_RAW || force)
5933 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 fill_input_buf(FALSE);
5935}
5936
5937/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005938 * Wait "msec" msec until a character is available from the mouse, keyboard,
5939 * from inbuf[].
5940 * "msec" == -1 will block forever.
5941 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005942 * When "ignore_input" is TRUE even check for pending input when input is
5943 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005944 * "interrupted" (if not NULL) is set to TRUE when no character is available
5945 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005946 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005947 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 */
5949 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005950WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005952#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005953 return ui_wait_for_chars_or_timer(
5954 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005955#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005956 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005957#endif
5958}
5959
5960/*
5961 * Wait "msec" msec until a character is available from the mouse or keyboard
5962 * or from inbuf[].
5963 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005964 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005965 * "interrupted" (if not NULL) is set to TRUE when no character is available
5966 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005967 * When a GUI is being used, this will never get called -- webb
5968 */
5969 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005970WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972#ifdef FEAT_MOUSE_GPM
5973 int gpm_process_wanted;
5974#endif
5975#ifdef FEAT_XCLIPBOARD
5976 int rest;
5977#endif
5978 int avail;
5979
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005980 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 return 1;
5982
5983#if defined(FEAT_MOUSE_DEC)
5984 /* May need to query the mouse position. */
5985 if (WantQueryMouse)
5986 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005987 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5989 }
5990#endif
5991
5992 /*
5993 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5994 * events. This is a bit complicated, because they might both be defined.
5995 */
5996#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5997# ifdef FEAT_XCLIPBOARD
5998 rest = 0;
5999 if (do_xterm_trace())
6000 rest = msec;
6001# endif
6002 do
6003 {
6004# ifdef FEAT_XCLIPBOARD
6005 if (rest != 0)
6006 {
6007 msec = XT_TRACE_DELAY;
6008 if (rest >= 0 && rest < XT_TRACE_DELAY)
6009 msec = rest;
6010 if (rest >= 0)
6011 rest -= msec;
6012 }
6013# endif
6014# ifdef FEAT_MOUSE_GPM
6015 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006016 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006017 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006019 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020# endif
6021 if (!avail)
6022 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006023 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 return 1;
6025# ifdef FEAT_XCLIPBOARD
6026 if (rest == 0 || !do_xterm_trace())
6027# endif
6028 break;
6029 }
6030 }
6031 while (FALSE
6032# ifdef FEAT_MOUSE_GPM
6033 || (gpm_process_wanted && mch_gpm_process() == 0)
6034# endif
6035# ifdef FEAT_XCLIPBOARD
6036 || (!avail && rest != 0)
6037# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006038 )
6039 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040
6041#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006042 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#endif
6044 return avail;
6045}
6046
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006047#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048/*
6049 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006050 * "msec" == 0 will check for characters once.
6051 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006054 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006055 * "interrupted" (if not NULL) is set to TRUE when no character is available
6056 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#if defined(__BEOS__)
6059 int
6060#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006061 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006063RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006066 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006067#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 static int busy = FALSE;
6069
6070 /* May retry getting characters after an event was handled. */
6071# define MAY_LOOP
6072
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006073# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 /* Remember at what time we started, so that we know how much longer we
6075 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006076 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006077 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006079 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006080 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# endif
6082
6083 /* Handle being called recursively. This may happen for the session
6084 * manager stuff, it may save the file, which does a breakcheck. */
6085 if (busy)
6086 return 0;
6087#endif
6088
6089#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006090 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091#endif
6092 {
6093#ifdef MAY_LOOP
6094 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006095# ifdef FEAT_MZSCHEME
6096 int mzquantum_used = FALSE;
6097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#endif
6099#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006100 /* each channel may use in, out and err */
6101 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 int nfd;
6103# ifdef FEAT_XCLIPBOARD
6104 int xterm_idx = -1;
6105# endif
6106# ifdef FEAT_MOUSE_GPM
6107 int gpm_idx = -1;
6108# endif
6109# ifdef USE_XSMP
6110 int xsmp_idx = -1;
6111# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006112 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006114# ifdef FEAT_MZSCHEME
6115 mzvim_check_threads();
6116 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6117 {
6118 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6119 mzquantum_used = TRUE;
6120 }
6121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 fds[0].fd = fd;
6123 fds[0].events = POLLIN;
6124 nfd = 1;
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006127 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (xterm_Shell != (Widget)0)
6129 {
6130 xterm_idx = nfd;
6131 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6132 fds[nfd].events = POLLIN;
6133 nfd++;
6134 }
6135# endif
6136# ifdef FEAT_MOUSE_GPM
6137 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6138 {
6139 gpm_idx = nfd;
6140 fds[nfd].fd = gpm_fd;
6141 fds[nfd].events = POLLIN;
6142 nfd++;
6143 }
6144# endif
6145# ifdef USE_XSMP
6146 if (xsmp_icefd != -1)
6147 {
6148 xsmp_idx = nfd;
6149 fds[nfd].fd = xsmp_icefd;
6150 fds[nfd].events = POLLIN;
6151 nfd++;
6152 }
6153# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006154#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006155 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006156#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 if (interrupted != NULL)
6158 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006160 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006161
6162 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006163 if (result == 0 && interrupted != NULL && ret > 0)
6164 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006165
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006166# ifdef FEAT_MZSCHEME
6167 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006168 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006169 finished = FALSE;
6170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172# ifdef FEAT_XCLIPBOARD
6173 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6174 {
6175 xterm_update(); /* Maybe we should hand out clipboard */
6176 if (--ret == 0 && !input_available())
6177 /* Try again */
6178 finished = FALSE;
6179 }
6180# endif
6181# ifdef FEAT_MOUSE_GPM
6182 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6183 {
6184 *check_for_gpm = 1;
6185 }
6186# endif
6187# ifdef USE_XSMP
6188 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6189 {
6190 if (fds[xsmp_idx].revents & POLLIN)
6191 {
6192 busy = TRUE;
6193 xsmp_handle_requests();
6194 busy = FALSE;
6195 }
6196 else if (fds[xsmp_idx].revents & POLLHUP)
6197 {
6198 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006199 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 xsmp_close();
6201 }
6202 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006203 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 }
6205# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006206#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006207 /* also call when ret == 0, we may be polling a keep-open channel */
6208 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006209 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#else /* HAVE_SELECT */
6213
6214 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006215 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006216 // These are static because they can take 8 Kbyte each and cause the
6217 // signal stack to run out with -O3.
6218 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006220 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006222# ifdef FEAT_MZSCHEME
6223 mzvim_check_threads();
6224 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6225 {
6226 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6227 mzquantum_used = TRUE;
6228 }
6229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006233 tv.tv_sec = towait / 1000;
6234 tv.tv_usec = (towait % 1000) * (1000000/1000);
6235 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237 else
6238 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
6240 /*
6241 * Select on ready for reading and exceptional condition (end of file).
6242 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006243select_eintr:
6244 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006245 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 FD_ZERO(&efds);
6247 FD_SET(fd, &rfds);
6248# if !defined(__QNX__) && !defined(__CYGWIN32__)
6249 /* For QNX select() always returns 1 if this is set. Why? */
6250 FD_SET(fd, &efds);
6251# endif
6252 maxfd = fd;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006255 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (xterm_Shell != (Widget)0)
6257 {
6258 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6259 if (maxfd < ConnectionNumber(xterm_dpy))
6260 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006261
6262 /* An event may have already been read but not handled. In
6263 * particulary, XFlush may cause this. */
6264 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
6266# endif
6267# ifdef FEAT_MOUSE_GPM
6268 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6269 {
6270 FD_SET(gpm_fd, &rfds);
6271 FD_SET(gpm_fd, &efds);
6272 if (maxfd < gpm_fd)
6273 maxfd = gpm_fd;
6274 }
6275# endif
6276# ifdef USE_XSMP
6277 if (xsmp_icefd != -1)
6278 {
6279 FD_SET(xsmp_icefd, &rfds);
6280 FD_SET(xsmp_icefd, &efds);
6281 if (maxfd < xsmp_icefd)
6282 maxfd = xsmp_icefd;
6283 }
6284# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006285# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006286 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006287# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006288 if (interrupted != NULL)
6289 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
Bram Moolenaar643b6142018-09-12 20:29:09 +02006291 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6292 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006293 result = ret > 0 && FD_ISSET(fd, &rfds);
6294 if (result)
6295 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006296 else if (interrupted != NULL && ret > 0)
6297 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006298
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006299# ifdef EINTR
6300 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006301 {
6302 /* Check whether window has been resized, EINTR may be caused by
6303 * SIGWINCH. */
6304 if (do_resize)
6305 handle_resize();
6306
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006307 /* Interrupted by a signal, need to try again. We ignore msec
6308 * here, because we do want to check even after a timeout if
6309 * characters are available. Needed for reading output of an
6310 * external command after the process has finished. */
6311 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006312 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006313# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006314# ifdef __TANDEM
6315 if (ret == -1 && errno == ENOTSUP)
6316 {
6317 FD_ZERO(&rfds);
6318 FD_ZERO(&efds);
6319 ret = 0;
6320 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006321# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006322# ifdef FEAT_MZSCHEME
6323 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006324 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006325 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326# endif
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# ifdef FEAT_XCLIPBOARD
6329 if (ret > 0 && xterm_Shell != (Widget)0
6330 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6331 {
6332 xterm_update(); /* Maybe we should hand out clipboard */
6333 /* continue looping when we only got the X event and the input
6334 * buffer is empty */
6335 if (--ret == 0 && !input_available())
6336 {
6337 /* Try again */
6338 finished = FALSE;
6339 }
6340 }
6341# endif
6342# ifdef FEAT_MOUSE_GPM
6343 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6344 {
6345 if (FD_ISSET(gpm_fd, &efds))
6346 gpm_close();
6347 else if (FD_ISSET(gpm_fd, &rfds))
6348 *check_for_gpm = 1;
6349 }
6350# endif
6351# ifdef USE_XSMP
6352 if (ret > 0 && xsmp_icefd != -1)
6353 {
6354 if (FD_ISSET(xsmp_icefd, &efds))
6355 {
6356 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006357 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 xsmp_close();
6359 if (--ret == 0)
6360 finished = FALSE; /* keep going if event was only one */
6361 }
6362 else if (FD_ISSET(xsmp_icefd, &rfds))
6363 {
6364 busy = TRUE;
6365 xsmp_handle_requests();
6366 busy = FALSE;
6367 if (--ret == 0)
6368 finished = FALSE; /* keep going if event was only one */
6369 }
6370 }
6371# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006372#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006373 /* also call when ret == 0, we may be polling a keep-open channel */
6374 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006375 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378#endif /* HAVE_SELECT */
6379
6380#ifdef MAY_LOOP
6381 if (finished || msec == 0)
6382 break;
6383
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006384# ifdef FEAT_CLIENTSERVER
6385 if (server_waiting())
6386 break;
6387# endif
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 /* We're going to loop around again, find out for how long */
6390 if (msec > 0)
6391 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006392# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006394 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395# else
6396 /* Guess we got interrupted halfway. */
6397 msec = msec / 2;
6398# endif
6399 if (msec <= 0)
6400 break; /* waited long enough */
6401 }
6402#endif
6403 }
6404
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006405 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406}
6407
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006410 * Expand a path into all matching files and/or directories. Handles "*",
6411 * "?", "[a-z]", "**", etc.
6412 * "path" has backslashes before chars that are not to be expanded.
6413 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 */
6415 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006416mch_expandpath(
6417 garray_T *gap,
6418 char_u *path,
6419 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
Bram Moolenaar02743632005-07-25 20:42:36 +00006421 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422}
6423#endif
6424
6425/*
6426 * mch_expand_wildcards() - this code does wild-card pattern matching using
6427 * the shell
6428 *
6429 * return OK for success, FAIL for error (you may lose some memory) and put
6430 * an error message in *file.
6431 *
6432 * num_pat is number of input patterns
6433 * pat is array of pointers to input patterns
6434 * num_file is pointer to number of matched file names
6435 * file is pointer to array of pointers to matched file names
6436 */
6437
6438#ifndef SEEK_SET
6439# define SEEK_SET 0
6440#endif
6441#ifndef SEEK_END
6442# define SEEK_END 2
6443#endif
6444
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006445#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006448mch_expand_wildcards(
6449 int num_pat,
6450 char_u **pat,
6451 int *num_file,
6452 char_u ***file,
6453 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int i;
6456 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006457 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 char_u *p;
6459 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
Bram Moolenaarc7247912008-01-13 12:54:11 +00006461 /*
6462 * This is the non-OS/2 implementation (really Unix).
6463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 int j;
6465 char_u *tempname;
6466 char_u *command;
6467 FILE *fd;
6468 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006469#define STYLE_ECHO 0 /* use "echo", the default */
6470#define STYLE_GLOB 1 /* use "glob", for csh */
6471#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6472#define STYLE_PRINT 3 /* use "print -N", for zsh */
6473#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6474 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 int shell_style = STYLE_ECHO;
6476 int check_spaces;
6477 static int did_find_nul = FALSE;
6478 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006479 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006480 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
6482 *num_file = 0; /* default: no files found */
6483 *file = NULL;
6484
6485 /*
6486 * If there are no wildcards, just copy the names to allocated memory.
6487 * Saves a lot of time, because we don't have to start a new shell.
6488 */
6489 if (!have_wildcard(num_pat, pat))
6490 return save_patterns(num_pat, pat, num_file, file);
6491
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006492# ifdef HAVE_SANDBOX
6493 /* Don't allow any shell command in the sandbox. */
6494 if (sandbox != 0 && check_secure())
6495 return FAIL;
6496# endif
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 /*
6499 * Don't allow the use of backticks in secure and restricted mode.
6500 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006501 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 for (i = 0; i < num_pat; ++i)
6503 if (vim_strchr(pat[i], '`') != NULL
6504 && (check_restricted() || check_secure()))
6505 return FAIL;
6506
6507 /*
6508 * get a name for the temp file
6509 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006510 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
6512 EMSG(_(e_notmp));
6513 return FAIL;
6514 }
6515
6516 /*
6517 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006518 * file.
6519 * STYLE_BT: NL separated
6520 * If expanding `cmd` execute it directly.
6521 * STYLE_GLOB: NUL separated
6522 * If we use *csh, "glob" will work better than "echo".
6523 * STYLE_PRINT: NL or NUL separated
6524 * If we use *zsh, "print -N" will work better than "glob".
6525 * STYLE_VIMGLOB: NL separated
6526 * If we use *sh*, we define "vimglob()".
6527 * STYLE_ECHO: space separated.
6528 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 */
6530 if (num_pat == 1 && *pat[0] == '`'
6531 && (len = STRLEN(pat[0])) > 2
6532 && *(pat[0] + len - 1) == '`')
6533 shell_style = STYLE_BT;
6534 else if ((len = STRLEN(p_sh)) >= 3)
6535 {
6536 if (STRCMP(p_sh + len - 3, "csh") == 0)
6537 shell_style = STYLE_GLOB;
6538 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6539 shell_style = STYLE_PRINT;
6540 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006541 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6542 "sh") != NULL)
6543 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaarc7247912008-01-13 12:54:11 +00006545 /* Compute the length of the command. We need 2 extra bytes: for the
6546 * optional '&' and for the NUL.
6547 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006549 if (shell_style == STYLE_VIMGLOB)
6550 len += STRLEN(sh_vimglob_func);
6551
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006552 for (i = 0; i < num_pat; ++i)
6553 {
6554 /* Count the length of the patterns in the same way as they are put in
6555 * "command" below. */
6556#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006558#else
6559 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006560 for (j = 0; pat[i][j] != NUL; ++j)
6561 {
6562 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6563 ++len; /* may add a backslash */
6564 ++len;
6565 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006566#endif
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 command = alloc(len);
6569 if (command == NULL)
6570 {
6571 /* out of memory */
6572 vim_free(tempname);
6573 return FAIL;
6574 }
6575
6576 /*
6577 * Build the shell command:
6578 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6579 * recognizes this).
6580 * - Add the shell command to print the expanded names.
6581 * - Add the temp file name.
6582 * - Add the file name patterns.
6583 */
6584 if (shell_style == STYLE_BT)
6585 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006586 /* change `command; command& ` to (command; command ) */
6587 STRCPY(command, "(");
6588 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006590 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006591 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 --p;
6593 if (*p == '&') /* remove trailing '&' */
6594 {
6595 ampersent = TRUE;
6596 *p = ' ';
6597 }
6598 STRCAT(command, ">");
6599 }
6600 else
6601 {
6602 if (flags & EW_NOTFOUND)
6603 STRCPY(command, "set nonomatch; ");
6604 else
6605 STRCPY(command, "unset nonomatch; ");
6606 if (shell_style == STYLE_GLOB)
6607 STRCAT(command, "glob >");
6608 else if (shell_style == STYLE_PRINT)
6609 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006610 else if (shell_style == STYLE_VIMGLOB)
6611 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 else
6613 STRCAT(command, "echo >");
6614 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 if (shell_style != STYLE_BT)
6619 for (i = 0; i < num_pat; ++i)
6620 {
6621 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006622 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006623 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624#ifdef USE_SYSTEM
6625 STRCAT(command, " \"");
6626 STRCAT(command, pat[i]);
6627 STRCAT(command, "\"");
6628#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006629 int intick = FALSE;
6630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 p = command + STRLEN(command);
6632 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006633 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006634 {
6635 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006636 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006637 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6638 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006639 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006640 * backslash inside backticks, before a special character
6641 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006642 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006643 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6644 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006645 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006646 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006647 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006648 else if (!intick
6649 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6650 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006651 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006652 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6653 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006654 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006655
6656 /* Copy one character. */
6657 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 *p = NUL;
6660#endif
6661 }
6662 if (flags & EW_SILENT)
6663 show_shell_mess = FALSE;
6664 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006665 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /*
6668 * Using zsh -G: If a pattern has no matches, it is just deleted from
6669 * the argument list, otherwise zsh gives an error message and doesn't
6670 * expand any other pattern.
6671 */
6672 if (shell_style == STYLE_PRINT)
6673 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6674
6675 /*
6676 * If we use -f then shell variables set in .cshrc won't get expanded.
6677 * vi can do it, so we will too, but it is only necessary if there is a "$"
6678 * in one of the patterns, otherwise we can still use the fast option.
6679 */
6680 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6681 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6682
6683 /*
6684 * execute the shell command
6685 */
6686 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6687
6688 /* When running in the background, give it some time to create the temp
6689 * file, but don't wait for it to finish. */
6690 if (ampersent)
6691 mch_delay(10L, TRUE);
6692
6693 extra_shell_arg = NULL; /* cleanup */
6694 show_shell_mess = TRUE;
6695 vim_free(command);
6696
Bram Moolenaarc7247912008-01-13 12:54:11 +00006697 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
6699 mch_remove(tempname);
6700 vim_free(tempname);
6701 /*
6702 * With interactive completion, the error message is not printed.
6703 * However with USE_SYSTEM, I don't know how to turn off error messages
6704 * from the shell, so screen may still get messed up -- webb.
6705 */
6706#ifndef USE_SYSTEM
6707 if (!(flags & EW_SILENT))
6708#endif
6709 {
6710 redraw_later_clear(); /* probably messed up screen */
6711 msg_putchar('\n'); /* clear bottom line quickly */
6712 cmdline_row = Rows - 1; /* continue on last line */
6713#ifdef USE_SYSTEM
6714 if (!(flags & EW_SILENT))
6715#endif
6716 {
6717 MSG(_(e_wildexpand));
6718 msg_start(); /* don't overwrite this message */
6719 }
6720 }
6721 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6722 * EW_NOTFOUND is given */
6723 if (shell_style == STYLE_BT)
6724 return FAIL;
6725 goto notfound;
6726 }
6727
6728 /*
6729 * read the names from the file into memory
6730 */
6731 fd = fopen((char *)tempname, READBIN);
6732 if (fd == NULL)
6733 {
6734 /* Something went wrong, perhaps a file name with a special char. */
6735 if (!(flags & EW_SILENT))
6736 {
6737 MSG(_(e_wildexpand));
6738 msg_start(); /* don't overwrite this message */
6739 }
6740 vim_free(tempname);
6741 goto notfound;
6742 }
6743 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006744 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006746 if (llen < 0)
6747 /* just in case ftell() would fail */
6748 buffer = NULL;
6749 else
6750 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (buffer == NULL)
6752 {
6753 /* out of memory */
6754 mch_remove(tempname);
6755 vim_free(tempname);
6756 fclose(fd);
6757 return FAIL;
6758 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006759 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 i = fread((char *)buffer, 1, len, fd);
6761 fclose(fd);
6762 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006763 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
6765 /* unexpected read error */
6766 EMSG2(_(e_notread), tempname);
6767 vim_free(tempname);
6768 vim_free(buffer);
6769 return FAIL;
6770 }
6771 vim_free(tempname);
6772
Bram Moolenaarc7247912008-01-13 12:54:11 +00006773# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6775 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006776 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6778 *p++ = buffer[i];
6779 len = p - buffer;
6780# endif
6781
6782
6783 /* file names are separated with Space */
6784 if (shell_style == STYLE_ECHO)
6785 {
6786 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6787 p = buffer;
6788 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6789 {
6790 while (*p != ' ' && *p != '\n')
6791 ++p;
6792 p = skipwhite(p); /* skip to next entry */
6793 }
6794 }
6795 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006796 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
6798 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6799 p = buffer;
6800 for (i = 0; *p != NUL; ++i) /* count number of entries */
6801 {
6802 while (*p != '\n' && *p != NUL)
6803 ++p;
6804 if (*p != NUL)
6805 ++p;
6806 p = skipwhite(p); /* skip leading white space */
6807 }
6808 }
6809 /* file names are separated with NUL */
6810 else
6811 {
6812 /*
6813 * Some versions of zsh use spaces instead of NULs to separate
6814 * results. Only do this when there is no NUL before the end of the
6815 * buffer, otherwise we would never be able to use file names with
6816 * embedded spaces when zsh does use NULs.
6817 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6818 * don't check for spaces again.
6819 */
6820 check_spaces = FALSE;
6821 if (shell_style == STYLE_PRINT && !did_find_nul)
6822 {
6823 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006824 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006825 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 did_find_nul = TRUE;
6827 else
6828 check_spaces = TRUE;
6829 }
6830
6831 /*
6832 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6833 * already is one, for STYLE_GLOB it needs to be added.
6834 */
6835 if (len && buffer[len - 1] == NUL)
6836 --len;
6837 else
6838 buffer[len] = NUL;
6839 i = 0;
6840 for (p = buffer; p < buffer + len; ++p)
6841 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6842 {
6843 ++i;
6844 *p = NUL;
6845 }
6846 if (len)
6847 ++i; /* count last entry */
6848 }
6849 if (i == 0)
6850 {
6851 /*
6852 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6853 * /bin/sh will happily expand it to nothing rather than returning an
6854 * error; and hey, it's good to check anyway -- webb.
6855 */
6856 vim_free(buffer);
6857 goto notfound;
6858 }
6859 *num_file = i;
6860 *file = (char_u **)alloc(sizeof(char_u *) * i);
6861 if (*file == NULL)
6862 {
6863 /* out of memory */
6864 vim_free(buffer);
6865 return FAIL;
6866 }
6867
6868 /*
6869 * Isolate the individual file names.
6870 */
6871 p = buffer;
6872 for (i = 0; i < *num_file; ++i)
6873 {
6874 (*file)[i] = p;
6875 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006876 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6877 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006879 while (!(shell_style == STYLE_ECHO && *p == ' ')
6880 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 ++p;
6882 if (p == buffer + len) /* last entry */
6883 *p = NUL;
6884 else
6885 {
6886 *p++ = NUL;
6887 p = skipwhite(p); /* skip to next entry */
6888 }
6889 }
6890 else /* NUL separates */
6891 {
6892 while (*p && p < buffer + len) /* skip entry */
6893 ++p;
6894 ++p; /* skip NUL */
6895 }
6896 }
6897
6898 /*
6899 * Move the file names to allocated memory.
6900 */
6901 for (j = 0, i = 0; i < *num_file; ++i)
6902 {
6903 /* Require the files to exist. Helps when using /bin/sh */
6904 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6905 continue;
6906
6907 /* check if this entry should be included */
6908 dir = (mch_isdir((*file)[i]));
6909 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6910 continue;
6911
Bram Moolenaara2031822006-03-07 22:29:51 +00006912 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006913 if (!dir && (flags & EW_EXEC)
6914 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006915 continue;
6916
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6918 if (p)
6919 {
6920 STRCPY(p, (*file)[i]);
6921 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006922 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 (*file)[j++] = p;
6924 }
6925 }
6926 vim_free(buffer);
6927 *num_file = j;
6928
6929 if (*num_file == 0) /* rejected all entries */
6930 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006931 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 goto notfound;
6933 }
6934
6935 return OK;
6936
6937notfound:
6938 if (flags & EW_NOTFOUND)
6939 return save_patterns(num_pat, pat, num_file, file);
6940 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941}
6942
6943#endif /* VMS */
6944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006946save_patterns(
6947 int num_pat,
6948 char_u **pat,
6949 int *num_file,
6950 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
6952 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006953 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6956 if (*file == NULL)
6957 return FAIL;
6958 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006959 {
6960 s = vim_strsave(pat[i]);
6961 if (s != NULL)
6962 /* Be compatible with expand_filename(): halve the number of
6963 * backslashes. */
6964 backslash_halve(s);
6965 (*file)[i] = s;
6966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 *num_file = num_pat;
6968 return OK;
6969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971/*
6972 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6973 * expand.
6974 */
6975 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006976mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006978 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (*p == '\\' && p[1] != NUL)
6981 ++p;
6982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (vim_strchr((char_u *)
6984#ifdef VMS
6985 "*?%"
6986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988#endif
6989 , *p) != NULL)
6990 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
6992 return FALSE;
6993}
6994
6995/*
6996 * Return TRUE if the string "p" contains a wildcard.
6997 * Don't recognize '~' at the end as a wildcard.
6998 */
6999 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007000mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007002 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (*p == '\\' && p[1] != NUL)
7005 ++p;
7006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (vim_strchr((char_u *)
7008#ifdef VMS
7009 "*?%$"
7010#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012#endif
7013 , *p) != NULL
7014 || (*p == '~' && p[1] != NUL))
7015 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017 return FALSE;
7018}
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007021have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 int i;
7024
7025 for (i = 0; i < num; i++)
7026 if (mch_has_wildcard(file[i]))
7027 return 1;
7028 return 0;
7029}
7030
7031 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007032have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 int i;
7035
7036 for (i = 0; i < num; i++)
7037 if (vim_strchr(file[i], '$') != NULL)
7038 return TRUE;
7039 return FALSE;
7040}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007042#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043/*
7044 * Scaled-down version of rename(), which is missing in Xenix.
7045 * This version can only move regular files and will fail if the
7046 * destination exists.
7047 */
7048 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007049mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
7051 struct stat st;
7052
7053 if (stat(dest, &st) >= 0) /* fail if destination exists */
7054 return -1;
7055 if (link(src, dest) != 0) /* link file to new name */
7056 return -1;
7057 if (mch_remove(src) == 0) /* delete link to old name */
7058 return 0;
7059 return -1;
7060}
7061#endif /* !HAVE_RENAME */
7062
7063#ifdef FEAT_MOUSE_GPM
7064/*
7065 * Initializes connection with gpm (if it isn't already opened)
7066 * Return 1 if succeeded (or connection already opened), 0 if failed
7067 */
7068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007069gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7072
7073 if (!gpm_flag)
7074 {
7075 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7076 gpm_connect.defaultMask = ~GPM_HARD;
7077 /* Default handling for mouse move*/
7078 gpm_connect.minMod = 0; /* Handle any modifier keys */
7079 gpm_connect.maxMod = 0xffff;
7080 if (Gpm_Open(&gpm_connect, 0) > 0)
7081 {
7082 /* gpm library tries to handling TSTP causes
7083 * problems. Anyways, we close connection to Gpm whenever
7084 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007085 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007087# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 return 1; /* succeed */
7091 }
7092 if (gpm_fd == -2)
7093 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7094 return 0;
7095 }
7096 return 1; /* already open */
7097}
7098
7099/*
7100 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 */
7102 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007103gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 if (gpm_flag && gpm_fd >= 0) /* if Open */
7106 Gpm_Close();
7107}
7108
7109/* Reads gpm event and adds special keys to input buf. Returns length of
7110 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007111 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 */
7113 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007114mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 int button;
7117 static Gpm_Event gpm_event;
7118 char_u string[6];
7119 int_u vim_modifiers;
7120 int row,col;
7121 unsigned char buttons_mask;
7122 unsigned char gpm_modifiers;
7123 static unsigned char old_buttons = 0;
7124
7125 Gpm_GetEvent(&gpm_event);
7126
7127#ifdef FEAT_GUI
7128 /* Don't put events in the input queue now. */
7129 if (hold_gui_events)
7130 return 0;
7131#endif
7132
7133 row = gpm_event.y - 1;
7134 col = gpm_event.x - 1;
7135
7136 string[0] = ESC; /* Our termcode */
7137 string[1] = 'M';
7138 string[2] = 'G';
7139 switch (GPM_BARE_EVENTS(gpm_event.type))
7140 {
7141 case GPM_DRAG:
7142 string[3] = MOUSE_DRAG;
7143 break;
7144 case GPM_DOWN:
7145 buttons_mask = gpm_event.buttons & ~old_buttons;
7146 old_buttons = gpm_event.buttons;
7147 switch (buttons_mask)
7148 {
7149 case GPM_B_LEFT:
7150 button = MOUSE_LEFT;
7151 break;
7152 case GPM_B_MIDDLE:
7153 button = MOUSE_MIDDLE;
7154 break;
7155 case GPM_B_RIGHT:
7156 button = MOUSE_RIGHT;
7157 break;
7158 default:
7159 return 0;
7160 /*Don't know what to do. Can more than one button be
7161 * reported in one event? */
7162 }
7163 string[3] = (char_u)(button | 0x20);
7164 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7165 break;
7166 case GPM_UP:
7167 string[3] = MOUSE_RELEASE;
7168 old_buttons &= ~gpm_event.buttons;
7169 break;
7170 default:
7171 return 0;
7172 }
7173 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7174 gpm_modifiers = gpm_event.modifiers;
7175 vim_modifiers = 0x0;
7176 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7177 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7178 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7179 */
7180 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7181 vim_modifiers |= MOUSE_SHIFT;
7182
7183 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7184 vim_modifiers |= MOUSE_CTRL;
7185 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7186 vim_modifiers |= MOUSE_ALT;
7187 string[3] |= vim_modifiers;
7188 string[4] = (char_u)(col + ' ' + 1);
7189 string[5] = (char_u)(row + ' ' + 1);
7190 add_to_input_buf(string, 6);
7191 return 6;
7192}
7193#endif /* FEAT_MOUSE_GPM */
7194
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007195#ifdef FEAT_SYSMOUSE
7196/*
7197 * Initialize connection with sysmouse.
7198 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7199 * output, any sysmouse output than will be processed via sig_sysmouse().
7200 * Return OK if succeeded, FAIL if failed.
7201 */
7202 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007203sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007204{
7205 struct mouse_info mouse;
7206
7207 mouse.operation = MOUSE_MODE;
7208 mouse.u.mode.mode = 0;
7209 mouse.u.mode.signal = SIGUSR2;
7210 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7211 {
7212 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7213 mouse.operation = MOUSE_SHOW;
7214 ioctl(1, CONS_MOUSECTL, &mouse);
7215 return OK;
7216 }
7217 return FAIL;
7218}
7219
7220/*
7221 * Stop processing SIGUSR2 signals, and also make sure that
7222 * virtual console do not send us any sysmouse related signal.
7223 */
7224 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007225sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007226{
7227 struct mouse_info mouse;
7228
7229 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7230 mouse.operation = MOUSE_MODE;
7231 mouse.u.mode.mode = 0;
7232 mouse.u.mode.signal = 0;
7233 ioctl(1, CONS_MOUSECTL, &mouse);
7234}
7235
7236/*
7237 * Gets info from sysmouse and adds special keys to input buf.
7238 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007239 static RETSIGTYPE
7240sig_sysmouse SIGDEFARG(sigarg)
7241{
7242 struct mouse_info mouse;
7243 struct video_info video;
7244 char_u string[6];
7245 int row, col;
7246 int button;
7247 int buttons;
7248 static int oldbuttons = 0;
7249
7250#ifdef FEAT_GUI
7251 /* Don't put events in the input queue now. */
7252 if (hold_gui_events)
7253 return;
7254#endif
7255
7256 mouse.operation = MOUSE_GETINFO;
7257 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7258 && ioctl(1, FBIO_MODEINFO, &video) != -1
7259 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7260 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7261 {
7262 row = mouse.u.data.y / video.vi_cheight;
7263 col = mouse.u.data.x / video.vi_cwidth;
7264 buttons = mouse.u.data.buttons;
7265 string[0] = ESC; /* Our termcode */
7266 string[1] = 'M';
7267 string[2] = 'S';
7268 if (oldbuttons == buttons && buttons != 0)
7269 {
7270 button = MOUSE_DRAG;
7271 }
7272 else
7273 {
7274 switch (buttons)
7275 {
7276 case 0:
7277 button = MOUSE_RELEASE;
7278 break;
7279 case 1:
7280 button = MOUSE_LEFT;
7281 break;
7282 case 2:
7283 button = MOUSE_MIDDLE;
7284 break;
7285 case 4:
7286 button = MOUSE_RIGHT;
7287 break;
7288 default:
7289 return;
7290 }
7291 oldbuttons = buttons;
7292 }
7293 string[3] = (char_u)(button);
7294 string[4] = (char_u)(col + ' ' + 1);
7295 string[5] = (char_u)(row + ' ' + 1);
7296 add_to_input_buf(string, 6);
7297 }
7298 return;
7299}
7300#endif /* FEAT_SYSMOUSE */
7301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007303typedef char_u * (*STRPROCSTR)(char_u *);
7304typedef char_u * (*INTPROCSTR)(int);
7305typedef int (*STRPROCINT)(char_u *);
7306typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307
7308/*
7309 * Call a DLL routine which takes either a string or int param
7310 * and returns an allocated string.
7311 */
7312 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007313mch_libcall(
7314 char_u *libname,
7315 char_u *funcname,
7316 char_u *argstring, /* NULL when using a argint */
7317 int argint,
7318 char_u **string_result,/* NULL when using number_result */
7319 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321# if defined(USE_DLOPEN)
7322 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007323 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324# else
7325 shl_t hinstLib;
7326# endif
7327 STRPROCSTR ProcAdd;
7328 INTPROCSTR ProcAddI;
7329 char_u *retval_str = NULL;
7330 int retval_int = 0;
7331 int success = FALSE;
7332
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007333 /*
7334 * Get a handle to the DLL module.
7335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007337 /* First clear any error, it's not cleared by the dlopen() call. */
7338 (void)dlerror();
7339
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 hinstLib = dlopen((char *)libname, RTLD_LAZY
7341# ifdef RTLD_LOCAL
7342 | RTLD_LOCAL
7343# endif
7344 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007345 if (hinstLib == NULL)
7346 {
7347 /* "dlerr" must be used before dlclose() */
7348 dlerr = (char *)dlerror();
7349 if (dlerr != NULL)
7350 EMSG2(_("dlerror = \"%s\""), dlerr);
7351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352# else
7353 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7354# endif
7355
7356 /* If the handle is valid, try to get the function address. */
7357 if (hinstLib != NULL)
7358 {
7359# ifdef HAVE_SETJMP_H
7360 /*
7361 * Catch a crash when calling the library function. For example when
7362 * using a number where a string pointer is expected.
7363 */
7364 mch_startjmp();
7365 if (SETJMP(lc_jump_env) != 0)
7366 {
7367 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007368# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007369 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 mch_didjmp();
7372 }
7373 else
7374# endif
7375 {
7376 retval_str = NULL;
7377 retval_int = 0;
7378
7379 if (argstring != NULL)
7380 {
7381# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007382 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007383 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384# else
7385 if (shl_findsym(&hinstLib, (const char *)funcname,
7386 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7387 ProcAdd = NULL;
7388# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007389 if ((success = (ProcAdd != NULL
7390# if defined(USE_DLOPEN)
7391 && dlerr == NULL
7392# endif
7393 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 if (string_result == NULL)
7396 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7397 else
7398 retval_str = (ProcAdd)(argstring);
7399 }
7400 }
7401 else
7402 {
7403# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007404 *(void **)(&ProcAddI) = 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 *)&ProcAddI) < 0)
7409 ProcAddI = NULL;
7410# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007411 if ((success = (ProcAddI != 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 = ((INTPROCINT)ProcAddI)(argint);
7419 else
7420 retval_str = (ProcAddI)(argint);
7421 }
7422 }
7423
7424 /* Save the string before we free the library. */
7425 /* Assume that a "1" or "-1" result is an illegal pointer. */
7426 if (string_result == NULL)
7427 *number_result = retval_int;
7428 else if (retval_str != NULL
7429 && retval_str != (char_u *)1
7430 && retval_str != (char_u *)-1)
7431 *string_result = vim_strsave(retval_str);
7432 }
7433
7434# ifdef HAVE_SETJMP_H
7435 mch_endjmp();
7436# ifdef SIGHASARG
7437 if (lc_signal != 0)
7438 {
7439 int i;
7440
7441 /* try to find the name of this signal */
7442 for (i = 0; signal_info[i].sig != -1; i++)
7443 if (lc_signal == signal_info[i].sig)
7444 break;
7445 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7446 }
7447# endif
7448# endif
7449
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007451 /* "dlerr" must be used before dlclose() */
7452 if (dlerr != NULL)
7453 EMSG2(_("dlerror = \"%s\""), dlerr);
7454
7455 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 (void)dlclose(hinstLib);
7457# else
7458 (void)shl_unload(hinstLib);
7459# endif
7460 }
7461
7462 if (!success)
7463 {
7464 EMSG2(_(e_libcall), funcname);
7465 return FAIL;
7466 }
7467
7468 return OK;
7469}
7470#endif
7471
7472#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7473static int xterm_trace = -1; /* default: disabled */
7474static int xterm_button;
7475
7476/*
7477 * Setup a dummy window for X selections in a terminal.
7478 */
7479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007480setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 int z = 0;
7483 char *strp = "";
7484 Widget AppShell;
7485
7486 if (!x_connect_to_server())
7487 return;
7488
7489 open_app_context();
7490 if (app_context != NULL && xterm_Shell == (Widget)0)
7491 {
7492 int (*oldhandler)();
7493#if defined(HAVE_SETJMP_H)
7494 int (*oldIOhandler)();
7495#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007496# ifdef ELAPSED_FUNC
7497 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
7499 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007500 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501# endif
7502
7503 /* Ignore X errors while opening the display */
7504 oldhandler = XSetErrorHandler(x_error_check);
7505
7506#if defined(HAVE_SETJMP_H)
7507 /* Ignore X IO errors while opening the display */
7508 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7509 mch_startjmp();
7510 if (SETJMP(lc_jump_env) != 0)
7511 {
7512 mch_didjmp();
7513 xterm_dpy = NULL;
7514 }
7515 else
7516#endif
7517 {
7518 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7519 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007520 if (xterm_dpy != NULL)
7521 xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522#if defined(HAVE_SETJMP_H)
7523 mch_endjmp();
7524#endif
7525 }
7526
7527#if defined(HAVE_SETJMP_H)
7528 /* Now handle X IO errors normally. */
7529 (void)XSetIOErrorHandler(oldIOhandler);
7530#endif
7531 /* Now handle X errors normally. */
7532 (void)XSetErrorHandler(oldhandler);
7533
7534 if (xterm_dpy == NULL)
7535 {
7536 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007537 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 return;
7539 }
7540
7541 /* Catch terminating error of the X server connection. */
7542 (void)XSetIOErrorHandler(x_IOerror_handler);
7543
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007544# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007546 {
7547 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007548 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007549 verbose_leave();
7550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551# endif
7552
7553 /* Create a Shell to make converters work. */
7554 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7555 applicationShellWidgetClass, xterm_dpy,
7556 NULL);
7557 if (AppShell == (Widget)0)
7558 return;
7559 xterm_Shell = XtVaCreatePopupShell("VIM",
7560 topLevelShellWidgetClass, AppShell,
7561 XtNmappedWhenManaged, 0,
7562 XtNwidth, 1,
7563 XtNheight, 1,
7564 NULL);
7565 if (xterm_Shell == (Widget)0)
7566 return;
7567
7568 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007569 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 if (x11_display == NULL)
7571 x11_display = xterm_dpy;
7572
7573 XtRealizeWidget(xterm_Shell);
7574 XSync(xterm_dpy, False);
7575 xterm_update();
7576 }
7577 if (xterm_Shell != (Widget)0)
7578 {
7579 clip_init(TRUE);
7580 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7581 x11_window = (Window)atol(strp);
7582 /* Check if $WINDOWID is valid. */
7583 if (test_x11_window(xterm_dpy) == FAIL)
7584 x11_window = 0;
7585 if (x11_window != 0)
7586 xterm_trace = 0;
7587 }
7588}
7589
7590 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007591start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7594 return;
7595 xterm_trace = 1;
7596 xterm_button = button;
7597 do_xterm_trace();
7598}
7599
7600
7601 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007602stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
7604 if (xterm_trace < 0)
7605 return;
7606 xterm_trace = 0;
7607}
7608
7609/*
7610 * Query the xterm pointer and generate mouse termcodes if necessary
7611 * return TRUE if dragging is active, else FALSE
7612 */
7613 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007614do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 Window root, child;
7617 int root_x, root_y;
7618 int win_x, win_y;
7619 int row, col;
7620 int_u mask_return;
7621 char_u buf[50];
7622 char_u *strp;
7623 long got_hints;
7624 static char_u *mouse_code;
7625 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7626 static int prev_row = 0, prev_col = 0;
7627 static XSizeHints xterm_hints;
7628
7629 if (xterm_trace <= 0)
7630 return FALSE;
7631
7632 if (xterm_trace == 1)
7633 {
7634 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007635 * have changed recently. */
7636 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7637 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 || xterm_hints.width_inc <= 1
7639 || xterm_hints.height_inc <= 1)
7640 {
7641 xterm_trace = -1; /* Not enough data -- disable tracing */
7642 return FALSE;
7643 }
7644
7645 /* Rely on the same mouse code for the duration of this */
7646 mouse_code = find_termcode(mouse_name);
7647 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007648 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 xterm_trace = 2;
7650
7651 /* Find the offset of the chars, there might be a scrollbar on the
7652 * left of the window and/or a menu on the top (eterm etc.) */
7653 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7654 &win_x, &win_y, &mask_return);
7655 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7656 - (xterm_hints.height_inc / 2);
7657 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7658 xterm_hints.y = 2;
7659 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7660 - (xterm_hints.width_inc / 2);
7661 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7662 xterm_hints.x = 2;
7663 return TRUE;
7664 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007665 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
7667 xterm_trace = 0;
7668 return FALSE;
7669 }
7670
7671 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7672 &win_x, &win_y, &mask_return);
7673
7674 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7675 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7676 if (row == prev_row && col == prev_col)
7677 return TRUE;
7678
7679 STRCPY(buf, mouse_code);
7680 strp = buf + STRLEN(buf);
7681 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7682 *strp++ = (char_u)(col + ' ' + 1);
7683 *strp++ = (char_u)(row + ' ' + 1);
7684 *strp = 0;
7685 add_to_input_buf(buf, STRLEN(buf));
7686
7687 prev_row = row;
7688 prev_col = col;
7689 return TRUE;
7690}
7691
7692# if defined(FEAT_GUI) || defined(PROTO)
7693/*
7694 * Destroy the display, window and app_context. Required for GTK.
7695 */
7696 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007697clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 if (xterm_Shell != (Widget)0)
7700 {
7701 XtDestroyWidget(xterm_Shell);
7702 xterm_Shell = (Widget)0;
7703 }
7704 if (xterm_dpy != NULL)
7705 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007706# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 /* Lesstif and Solaris crash here, lose some memory */
7708 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 if (x11_display == xterm_dpy)
7711 x11_display = NULL;
7712 xterm_dpy = NULL;
7713 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007714# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (app_context != (XtAppContext)NULL)
7716 {
7717 /* Lesstif and Solaris crash here, lose some memory */
7718 XtDestroyApplicationContext(app_context);
7719 app_context = (XtAppContext)NULL;
7720 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723# endif
7724
7725/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007726 * Catch up with GUI or X events.
7727 */
7728 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007729clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007730{
7731# ifdef FEAT_GUI
7732 if (gui.in_use)
7733 gui_mch_update();
7734 else
7735# endif
7736 if (xterm_Shell != (Widget)0)
7737 xterm_update();
7738}
7739
7740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 * Catch up with any queued X events. This may put keyboard input into the
7742 * input buffer, call resize call-backs, trigger timers etc. If there is
7743 * nothing in the X event queue (& no timers pending), then we return
7744 * immediately.
7745 */
7746 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007747xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 XEvent event;
7750
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007751 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007753 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007755 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007756 break;
7757
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007758 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007759 {
7760 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007761 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007762#ifdef FEAT_CLIENTSERVER
7763 {
7764 XPropertyEvent *e = (XPropertyEvent *)&event;
7765
7766 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007768 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007771 XtDispatchEvent(&event);
7772 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007773 else
7774 {
7775 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007776 XtAppProcessEvent(app_context, mask);
7777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779}
7780
7781 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007782clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 if (xterm_Shell != (Widget)0)
7785 return clip_x11_own_selection(xterm_Shell, cbd);
7786 return FAIL;
7787}
7788
7789 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007790clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 if (xterm_Shell != (Widget)0)
7793 clip_x11_lose_selection(xterm_Shell, cbd);
7794}
7795
7796 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007797clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 if (xterm_Shell != (Widget)0)
7800 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7801}
7802
7803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007804clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 clip_x11_set_selection(cbd);
7807}
7808#endif
7809
7810
7811#if defined(USE_XSMP) || defined(PROTO)
7812/*
7813 * Code for X Session Management Protocol.
7814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
7816# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817/*
7818 * This is our chance to ask the user if they want to save,
7819 * or abort the logout
7820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007822xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 cmdmod_T save_cmdmod;
7825 int cancel_shutdown = False;
7826
7827 save_cmdmod = cmdmod;
7828 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007829 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 /* Mustn't logout */
7831 cancel_shutdown = True;
7832 cmdmod = save_cmdmod;
7833 setcursor(); /* position cursor */
7834 out_flush();
7835
7836 /* Done interaction */
7837 SmcInteractDone(smc_conn, cancel_shutdown);
7838
7839 /* Finish off
7840 * Only end save-yourself here if we're not cancelling shutdown;
7841 * we'll get a cancelled callback later in which we'll end it.
7842 * Hopefully get around glitchy SMs (like GNOME-1)
7843 */
7844 if (!cancel_shutdown)
7845 {
7846 xsmp.save_yourself = False;
7847 SmcSaveYourselfDone(smc_conn, True);
7848 }
7849}
7850# endif
7851
7852/*
7853 * Callback that starts save-yourself.
7854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007856xsmp_handle_save_yourself(
7857 SmcConn smc_conn,
7858 SmPointer client_data UNUSED,
7859 int save_type UNUSED,
7860 Bool shutdown,
7861 int interact_style UNUSED,
7862 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 /* Handle already being in saveyourself */
7865 if (xsmp.save_yourself)
7866 SmcSaveYourselfDone(smc_conn, True);
7867 xsmp.save_yourself = True;
7868 xsmp.shutdown = shutdown;
7869
7870 /* First up, preserve all files */
7871 out_flush();
7872 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7873
7874 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007875 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
7877# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7878 /* Now see if we can ask about unsaved files */
7879 if (shutdown && !fast && gui.in_use)
7880 /* Need to interact with user, but need SM's permission */
7881 SmcInteractRequest(smc_conn, SmDialogError,
7882 xsmp_handle_interaction, client_data);
7883 else
7884# endif
7885 {
7886 /* Can stop the cycle here */
7887 SmcSaveYourselfDone(smc_conn, True);
7888 xsmp.save_yourself = False;
7889 }
7890}
7891
7892
7893/*
7894 * Callback to warn us of imminent death.
7895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007897xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 xsmp_close();
7900
7901 /* quit quickly leaving swapfiles for modified buffers behind */
7902 getout_preserve_modified(0);
7903}
7904
7905
7906/*
7907 * Callback to tell us that save-yourself has completed.
7908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007910xsmp_save_complete(
7911 SmcConn smc_conn UNUSED,
7912 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 xsmp.save_yourself = False;
7915}
7916
7917
7918/*
7919 * Callback to tell us that an instigated shutdown was cancelled
7920 * (maybe even by us)
7921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007923xsmp_shutdown_cancelled(
7924 SmcConn smc_conn,
7925 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 if (xsmp.save_yourself)
7928 SmcSaveYourselfDone(smc_conn, True);
7929 xsmp.save_yourself = False;
7930 xsmp.shutdown = False;
7931}
7932
7933
7934/*
7935 * Callback to tell us that a new ICE connection has been established.
7936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007938xsmp_ice_connection(
7939 IceConn iceConn,
7940 IcePointer clientData UNUSED,
7941 Bool opening,
7942 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 /* Intercept creation of ICE connection fd */
7945 if (opening)
7946 {
7947 xsmp_icefd = IceConnectionNumber(iceConn);
7948 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7949 }
7950}
7951
7952
7953/* Handle any ICE processing that's required; return FAIL if SM lost */
7954 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007955xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 Bool rep;
7958
7959 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7960 == IceProcessMessagesIOError)
7961 {
7962 /* Lost ICE */
7963 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007964 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 xsmp_close();
7966 return FAIL;
7967 }
7968 else
7969 return OK;
7970}
7971
7972static int dummy;
7973
7974/* Set up X Session Management Protocol */
7975 void
7976xsmp_init(void)
7977{
7978 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 SmcCallbacks smcallbacks;
7980#if 0
7981 SmPropValue smname;
7982 SmProp smnameprop;
7983 SmProp *smprops[1];
7984#endif
7985
7986 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007987 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989 xsmp.save_yourself = xsmp.shutdown = False;
7990
7991 /* Set up SM callbacks - must have all, even if they're not used */
7992 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7993 smcallbacks.save_yourself.client_data = NULL;
7994 smcallbacks.die.callback = xsmp_die;
7995 smcallbacks.die.client_data = NULL;
7996 smcallbacks.save_complete.callback = xsmp_save_complete;
7997 smcallbacks.save_complete.client_data = NULL;
7998 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7999 smcallbacks.shutdown_cancelled.client_data = NULL;
8000
8001 /* Set up a watch on ICE connection creations. The "dummy" argument is
8002 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8003 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8004 {
8005 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008006 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 return;
8008 }
8009
8010 /* Create an SM connection */
8011 xsmp.smcconn = SmcOpenConnection(
8012 NULL,
8013 NULL,
8014 SmProtoMajor,
8015 SmProtoMinor,
8016 SmcSaveYourselfProcMask | SmcDieProcMask
8017 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8018 &smcallbacks,
8019 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008020 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008021 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 errorstring);
8023 if (xsmp.smcconn == NULL)
8024 {
8025 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008028 {
8029 vim_snprintf(errorreport, sizeof(errorreport),
8030 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8031 verb_msg((char_u *)errorreport);
8032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 return;
8034 }
8035 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8036
8037#if 0
8038 /* ID ourselves */
8039 smname.value = "vim";
8040 smname.length = 3;
8041 smnameprop.name = "SmProgram";
8042 smnameprop.type = "SmARRAY8";
8043 smnameprop.num_vals = 1;
8044 smnameprop.vals = &smname;
8045
8046 smprops[0] = &smnameprop;
8047 SmcSetProperties(xsmp.smcconn, 1, smprops);
8048#endif
8049}
8050
8051
8052/* Shut down XSMP comms. */
8053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008054xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
8056 if (xsmp_icefd != -1)
8057 {
8058 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008059 if (xsmp.clientid != NULL)
8060 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008061 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 xsmp_icefd = -1;
8063 }
8064}
8065#endif /* USE_XSMP */
8066
8067
8068#ifdef EBCDIC
8069/* Translate character to its CTRL- value */
8070char CtrlTable[] =
8071{
8072/* 00 - 5E */
8073 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8075 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8079/* ^ */ 0x1E,
8080/* - */ 0x1F,
8081/* 61 - 6C */
8082 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8083/* _ */ 0x1F,
8084/* 6E - 80 */
8085 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8086/* a */ 0x01,
8087/* b */ 0x02,
8088/* c */ 0x03,
8089/* d */ 0x37,
8090/* e */ 0x2D,
8091/* f */ 0x2E,
8092/* g */ 0x2F,
8093/* h */ 0x16,
8094/* i */ 0x05,
8095/* 8A - 90 */
8096 0, 0, 0, 0, 0, 0, 0,
8097/* j */ 0x15,
8098/* k */ 0x0B,
8099/* l */ 0x0C,
8100/* m */ 0x0D,
8101/* n */ 0x0E,
8102/* o */ 0x0F,
8103/* p */ 0x10,
8104/* q */ 0x11,
8105/* r */ 0x12,
8106/* 9A - A1 */
8107 0, 0, 0, 0, 0, 0, 0, 0,
8108/* s */ 0x13,
8109/* t */ 0x3C,
8110/* u */ 0x3D,
8111/* v */ 0x32,
8112/* w */ 0x26,
8113/* x */ 0x18,
8114/* y */ 0x19,
8115/* z */ 0x3F,
8116/* AA - AC */
8117 0, 0, 0,
8118/* [ */ 0x27,
8119/* AE - BC */
8120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8121/* ] */ 0x1D,
8122/* BE - C0 */ 0, 0, 0,
8123/* A */ 0x01,
8124/* B */ 0x02,
8125/* C */ 0x03,
8126/* D */ 0x37,
8127/* E */ 0x2D,
8128/* F */ 0x2E,
8129/* G */ 0x2F,
8130/* H */ 0x16,
8131/* I */ 0x05,
8132/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8133/* J */ 0x15,
8134/* K */ 0x0B,
8135/* L */ 0x0C,
8136/* M */ 0x0D,
8137/* N */ 0x0E,
8138/* O */ 0x0F,
8139/* P */ 0x10,
8140/* Q */ 0x11,
8141/* R */ 0x12,
8142/* DA - DF */ 0, 0, 0, 0, 0, 0,
8143/* \ */ 0x1C,
8144/* E1 */ 0,
8145/* S */ 0x13,
8146/* T */ 0x3C,
8147/* U */ 0x3D,
8148/* V */ 0x32,
8149/* W */ 0x26,
8150/* X */ 0x18,
8151/* Y */ 0x19,
8152/* Z */ 0x3F,
8153/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8155};
8156
8157char MetaCharTable[]=
8158{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8159 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8160 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8161 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8162 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8163};
8164
8165
8166/* TODO: Use characters NOT numbers!!! */
8167char CtrlCharTable[]=
8168{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8169 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8170 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8171 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8172 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8173};
8174
8175
8176#endif