blob: 5ab3bd3cf3f94144def567ab994fd992281942b6 [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();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100330 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000331 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
Bram Moolenaar1ac56c22019-01-17 22:28:22 +0100377 elapsed_T 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 */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +0100483#ifndef ELAPSED_FUNC
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200484 /* 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 Moolenaaredce7422019-01-20 18:39:30 +0100983#if (defined(HAVE_SETJMP_H) \
984 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
985 || defined(FEAT_LIBCALL))) \
986 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100987# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100988
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100989// argument to SETJMP()
990static JMP_BUF lc_jump_env;
991
992# ifdef SIGHASARG
993// Caught signal number, 0 when no was signal caught; used for mch_libcall().
994// Volatile because it is used in signal handlers.
995static volatile sig_atomic_t lc_signal;
996# endif
997
998// TRUE when lc_jump_env is valid.
999// Volatile because it is used in signal handler deathtrap().
1000static volatile sig_atomic_t lc_active INIT(= FALSE);
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001004 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001006 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Usage:
1008 * mch_startjmp();
1009 * if (SETJMP(lc_jump_env) != 0)
1010 * {
1011 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001012 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * }
1014 * else
1015 * {
1016 * do_the_work;
1017 * mch_endjmp();
1018 * }
1019 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1020 * not return before the saved environment is used.
1021 * Returns OK for normal return, FAIL when the protected code caused a
1022 * problem and LONGJMP() was used.
1023 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001024 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001025mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001027# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 lc_active = TRUE;
1031}
1032
Bram Moolenaar113e1072019-01-20 15:30:40 +01001033 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001034mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036 lc_active = FALSE;
1037}
1038
Bram Moolenaar113e1072019-01-20 15:30:40 +01001039 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001040mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001043 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1044 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 init_signal_stack();
1046# endif
1047}
1048#endif
1049
1050/*
1051 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001052 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001054 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1055 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
1057 static RETSIGTYPE
1058deathtrap SIGDEFARG(sigarg)
1059{
1060 static int entered = 0; /* count the number of times we got here.
1061 Note: when memory has been corrupted
1062 this may get an arbitrary value! */
1063#ifdef SIGHASARG
1064 int i;
1065#endif
1066
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001067#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 /*
1069 * Catch a crash in protected code.
1070 * Restores the environment saved in lc_jump_env, which looks like
1071 * SETJMP() returns 1.
1072 */
1073 if (lc_active)
1074 {
1075# if defined(SIGHASARG)
1076 lc_signal = sigarg;
1077# endif
1078 lc_active = FALSE; /* don't jump again */
1079 LONGJMP(lc_jump_env, 1);
1080 /* NOTREACHED */
1081 }
1082#endif
1083
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001084#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001085# ifdef SIGQUIT
1086 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1087 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1088 * pressing CTRL-\, but we don't want Vim to exit then. */
1089 if (in_mch_delay && sigarg == SIGQUIT)
1090 SIGRETURN;
1091# endif
1092
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001093 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1094 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1095 * free(). Calling free() again may then cause a crash. */
1096 if (entered == 0
1097 && (0
1098# ifdef SIGHUP
1099 || sigarg == SIGHUP
1100# endif
1101# ifdef SIGQUIT
1102 || sigarg == SIGQUIT
1103# endif
1104# ifdef SIGTERM
1105 || sigarg == SIGTERM
1106# endif
1107# ifdef SIGPWR
1108 || sigarg == SIGPWR
1109# endif
1110# ifdef SIGUSR1
1111 || sigarg == SIGUSR1
1112# endif
1113# ifdef SIGUSR2
1114 || sigarg == SIGUSR2
1115# endif
1116 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001117 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001118 SIGRETURN;
1119#endif
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 /* Remember how often we have been called. */
1122 ++entered;
1123
Bram Moolenaare429e702016-06-10 19:49:14 +02001124 /* Executing autocommands is likely to use more stack space than we have
1125 * available in the signal stack. */
1126 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128#ifdef FEAT_EVAL
1129 /* Set the v:dying variable. */
1130 set_vim_var_nr(VV_DYING, (long)entered);
1131#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001132 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001134#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /* Since we are now using the signal stack, need to reset the stack
1136 * limit. Otherwise using a regexp will fail. */
1137 get_stack_limit();
1138#endif
1139
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001140#if 0
1141 /* This is for opening gdb the moment Vim crashes.
1142 * You need to manually adjust the file name and Vim executable name.
1143 * Suggested by SungHyun Nam. */
1144 {
1145# define VI_GDB_FILE "/tmp/vimgdb"
1146# define VIM_NAME "/usr/bin/vim"
1147 FILE *fp = fopen(VI_GDB_FILE, "w");
1148 if (fp)
1149 {
1150 fprintf(fp,
1151 "file %s\n"
1152 "attach %d\n"
1153 "set height 1000\n"
1154 "bt full\n"
1155 , VIM_NAME, getpid());
1156 fclose(fp);
1157 system("xterm -e gdb -x "VI_GDB_FILE);
1158 unlink(VI_GDB_FILE);
1159 }
1160 }
1161#endif
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163#ifdef SIGHASARG
1164 /* try to find the name of this signal */
1165 for (i = 0; signal_info[i].sig != -1; i++)
1166 if (sigarg == signal_info[i].sig)
1167 break;
1168 deadly_signal = sigarg;
1169#endif
1170
1171 full_screen = FALSE; /* don't write message to the GUI, it might be
1172 * part of the problem... */
1173 /*
1174 * If something goes wrong after entering here, we may get here again.
1175 * When this happens, give a message and try to exit nicely (resetting the
1176 * terminal mode, etc.)
1177 * When this happens twice, just exit, don't even try to give a message,
1178 * stack may be corrupt or something weird.
1179 * When this still happens again (or memory was corrupted in such a way
1180 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1181 */
1182 if (entered >= 3)
1183 {
1184 reset_signals(); /* don't catch any signals anymore */
1185 may_core_dump();
1186 if (entered >= 4)
1187 _exit(8);
1188 exit(7);
1189 }
1190 if (entered == 2)
1191 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001192 /* No translation, it may call malloc(). */
1193 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 out_flush();
1195 getout(1);
1196 }
1197
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001198 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001200 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 signal_info[i].name);
1202#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001203 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001205
1206 /* Preserve files and exit. This sets the really_exiting flag to prevent
1207 * calling free(). */
1208 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaare429e702016-06-10 19:49:14 +02001210 /* NOTREACHED */
1211
Bram Moolenaar009b2592004-10-24 19:18:58 +00001212#ifdef NBDEBUG
1213 reset_signals();
1214 may_core_dump();
1215 abort();
1216#endif
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 SIGRETURN;
1219}
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001222 * Invoked after receiving SIGCONT. We don't know what happened while
1223 * sleeping, deal with part of that.
1224 */
1225 static void
1226after_sigcont(void)
1227{
1228# ifdef FEAT_TITLE
1229 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1230 // again later.
1231 oldtitle_outdated = TRUE;
1232# endif
1233 settmode(TMODE_RAW);
1234 need_check_timestamps = TRUE;
1235 did_check_timestamps = FALSE;
1236}
1237
1238#if defined(SIGCONT)
1239static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001240static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001241
1242/*
1243 * With multi-threading, suspending might not work immediately. Catch the
1244 * SIGCONT signal, which will be used as an indication whether the suspending
1245 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001246 *
1247 * On Linux, signal is not always handled immediately either.
1248 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001249 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001250 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001251 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001253static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001254static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256/*
1257 * signal handler for SIGCONT
1258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 static RETSIGTYPE
1260sigcont_handler SIGDEFARG(sigarg)
1261{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001262 if (in_mch_suspend)
1263 {
1264 sigcont_received = TRUE;
1265 }
1266 else
1267 {
1268 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1269 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1270 // back to a sane mode. We should redraw, but we can't really do that
1271 // in a signal handler, do a redraw later.
1272 after_sigcont();
1273 redraw_later(CLEAR);
1274 cursor_on_force();
1275 out_flush();
1276 }
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 SIGRETURN;
1279}
1280#endif
1281
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001282#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001283# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001284static void *clip_star_save = NULL;
1285static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001286# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001287
1288/*
1289 * Called when Vim is going to sleep or execute a shell command.
1290 * We can't respond to requests for the X selections. Lose them, otherwise
1291 * other applications will hang. But first copy the text to cut buffer 0.
1292 */
1293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001294loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001295{
1296 if (clip_star.owned || clip_plus.owned)
1297 {
1298 x11_export_final_selection();
1299 if (clip_star.owned)
1300 clip_lose_selection(&clip_star);
1301 if (clip_plus.owned)
1302 clip_lose_selection(&clip_plus);
1303 if (x11_display != NULL)
1304 XFlush(x11_display);
1305 }
1306}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001307
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001308# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309/*
1310 * Save clipboard text to restore later.
1311 */
1312 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001313save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001314{
1315 if (clip_star.owned)
1316 clip_star_save = get_register('*', TRUE);
1317 if (clip_plus.owned)
1318 clip_plus_save = get_register('+', TRUE);
1319}
1320
1321/*
1322 * Restore clipboard text if no one own the X selection.
1323 */
1324 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001325restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001326{
1327 if (clip_star_save != NULL)
1328 {
1329 if (!clip_gen_owner_exists(&clip_star))
1330 put_register('*', clip_star_save);
1331 else
1332 free_register(clip_star_save);
1333 clip_star_save = NULL;
1334 }
1335 if (clip_plus_save != NULL)
1336 {
1337 if (!clip_gen_owner_exists(&clip_plus))
1338 put_register('+', clip_plus_save);
1339 else
1340 free_register(clip_plus_save);
1341 clip_plus_save = NULL;
1342 }
1343}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001344# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001345#endif
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347/*
1348 * If the machine has job control, use it to suspend the program,
1349 * otherwise fake it by starting a new shell.
1350 */
1351 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001352mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 /* BeOS does have SIGTSTP, but it doesn't work. */
1355#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001356 in_mch_suspend = TRUE;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 out_flush(); /* needed to make cursor visible on some systems */
1359 settmode(TMODE_COOK);
1360 out_flush(); /* needed to disable mouse on some systems */
1361
1362# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001363 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001365# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 sigcont_received = FALSE;
1367# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001370
1371# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001372 /*
1373 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001374 * immediately, but somehow not all the time, probably because it's handled
1375 * in another thread. Do not call pause() because there would be race
1376 * condition which would hang Vim if signal happened in between the test of
1377 * sigcont_received and the call to pause(). If signal is not yet received,
1378 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1379 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001380 */
1381 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001382 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383
Bram Moolenaar262735e2009-07-14 10:20:22 +00001384 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001385 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar2e310482018-08-21 13:09:10 +02001390 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#else
1392 suspend_shell();
1393#endif
1394}
1395
1396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 Columns = 80;
1400 Rows = 24;
1401
1402 out_flush();
1403 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001404
Bram Moolenaar56718732006-03-15 22:53:57 +00001405#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001406 mac_conv_init();
1407#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001408#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1409 win_clip_init();
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411}
1412
1413 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001414set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416#if defined(SIGWINCH)
1417 /*
1418 * WINDOW CHANGE signal is handled with sig_winch().
1419 */
1420 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1421#endif
1422
1423 /*
1424 * We want the STOP signal to work, to make mch_suspend() work.
1425 * For "rvim" the STOP signal is ignored.
1426 */
1427#ifdef SIGTSTP
1428 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1429#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001430#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 signal(SIGCONT, sigcont_handler);
1432#endif
1433
1434 /*
1435 * We want to ignore breaking of PIPEs.
1436 */
1437#ifdef SIGPIPE
1438 signal(SIGPIPE, SIG_IGN);
1439#endif
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001442 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#endif
1444
1445 /*
1446 * Ignore alarm signals (Perl's alarm() generates it).
1447 */
1448#ifdef SIGALRM
1449 signal(SIGALRM, SIG_IGN);
1450#endif
1451
1452 /*
1453 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1454 * work will be lost.
1455 */
1456#ifdef SIGPWR
1457 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1458#endif
1459
1460 /*
1461 * Arrange for other signals to gracefully shutdown Vim.
1462 */
1463 catch_signals(deathtrap, SIG_ERR);
1464
1465#if defined(FEAT_GUI) && defined(SIGHUP)
1466 /*
1467 * When the GUI is running, ignore the hangup signal.
1468 */
1469 if (gui.in_use)
1470 signal(SIGHUP, SIG_IGN);
1471#endif
1472}
1473
Bram Moolenaardf177f62005-02-22 08:39:57 +00001474#if defined(SIGINT) || defined(PROTO)
1475/*
1476 * Catch CTRL-C (only works while in Cooked mode).
1477 */
1478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001479catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001480{
1481 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1482}
1483#endif
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001486reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001489#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 /* SIGCONT isn't in the list, because its default action is ignore */
1491 signal(SIGCONT, SIG_DFL);
1492#endif
1493}
1494
1495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001496catch_signals(
1497 RETSIGTYPE (*func_deadly)(),
1498 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 int i;
1501
1502 for (i = 0; signal_info[i].sig != -1; i++)
1503 if (signal_info[i].deadly)
1504 {
1505#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1506 struct sigaction sa;
1507
1508 /* Setup to use the alternate stack for the signal function. */
1509 sa.sa_handler = func_deadly;
1510 sigemptyset(&sa.sa_mask);
1511# if defined(__linux__) && defined(_REENTRANT)
1512 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1513 * thread handling in combination with using the alternate stack:
1514 * pthread library functions try to use the stack pointer to
1515 * identify the current thread, causing a SEGV signal, which
1516 * recursively calls deathtrap() and hangs. */
1517 sa.sa_flags = 0;
1518# else
1519 sa.sa_flags = SA_ONSTACK;
1520# endif
1521 sigaction(signal_info[i].sig, &sa, NULL);
1522#else
1523# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1524 struct sigvec sv;
1525
1526 /* Setup to use the alternate stack for the signal function. */
1527 sv.sv_handler = func_deadly;
1528 sv.sv_mask = 0;
1529 sv.sv_flags = SV_ONSTACK;
1530 sigvec(signal_info[i].sig, &sv, NULL);
1531# else
1532 signal(signal_info[i].sig, func_deadly);
1533# endif
1534#endif
1535 }
1536 else if (func_other != SIG_ERR)
1537 signal(signal_info[i].sig, func_other);
1538}
1539
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001540#ifdef HAVE_SIGPROCMASK
1541 static void
1542block_signals(sigset_t *set)
1543{
1544 sigset_t newset;
1545 int i;
1546
1547 sigemptyset(&newset);
1548
1549 for (i = 0; signal_info[i].sig != -1; i++)
1550 sigaddset(&newset, signal_info[i].sig);
1551
Bram Moolenaar2e310482018-08-21 13:09:10 +02001552# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001553 /* SIGCONT isn't in the list, because its default action is ignore */
1554 sigaddset(&newset, SIGCONT);
1555# endif
1556
1557 sigprocmask(SIG_BLOCK, &newset, set);
1558}
1559
1560 static void
1561unblock_signals(sigset_t *set)
1562{
1563 sigprocmask(SIG_SETMASK, set, NULL);
1564}
1565#endif
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001568 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001569 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1570 * return TRUE
1571 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1572 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001573 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001574 * Returns TRUE when Vim should exit.
1575 */
1576 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001577vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001579 static int got_signal = 0;
1580 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001581
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001582 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001583 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001584 case SIGNAL_BLOCK: blocked = TRUE;
1585 break;
1586
1587 case SIGNAL_UNBLOCK: blocked = FALSE;
1588 if (got_signal != 0)
1589 {
1590 kill(getpid(), got_signal);
1591 got_signal = 0;
1592 }
1593 break;
1594
1595 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001596 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001597 got_signal = sig;
1598#ifdef SIGPWR
1599 if (sig != SIGPWR)
1600#endif
1601 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001602 break;
1603 }
1604 return FALSE;
1605}
1606
1607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 * Check_win checks whether we have an interactive stdout.
1609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001611mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (isatty(1))
1614 return OK;
1615 return FAIL;
1616}
1617
1618/*
1619 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1620 */
1621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001622mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 if (isatty(read_cmd_fd))
1625 return TRUE;
1626 return FALSE;
1627}
1628
1629#ifdef FEAT_X11
1630
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001631# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634/*
1635 * Give a message about the elapsed time for opening the X window.
1636 */
1637 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001638xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001640 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641}
1642# endif
1643#endif
1644
1645#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1646/*
1647 * A few functions shared by X11 title and clipboard code.
1648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650static int got_x_error = FALSE;
1651
1652/*
1653 * X Error handler, otherwise X just exits! (very rude) -- webb
1654 */
1655 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001656x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001658 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 STRCAT(IObuff, _("\nVim: Got X error\n"));
1660
1661 /* We cannot print a message and continue, because no X calls are allowed
1662 * here (causes my system to hang). Silently continuing might be an
1663 * alternative... */
1664 preserve_exit(); /* preserve files and exit */
1665
1666 return 0; /* NOTREACHED */
1667}
1668
1669/*
1670 * Another X Error handler, just used to check for errors.
1671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001673x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 got_x_error = TRUE;
1676 return 0;
1677}
1678
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001679/*
1680 * Return TRUE when connection to the X server is desired.
1681 */
1682 static int
1683x_connect_to_server(void)
1684{
1685 // No point in connecting if we are exiting or dying.
1686 if (exiting || v_dying)
1687 return FALSE;
1688
1689#if defined(FEAT_CLIENTSERVER)
1690 if (x_force_connect)
1691 return TRUE;
1692#endif
1693 if (x_no_connect)
1694 return FALSE;
1695
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001696 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001697 if (clip_exclude_prog != NULL)
1698 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001699 // Just in case we get called recursively, return FALSE. This could
1700 // happen if vpeekc() is used while executing the prog and it causes a
1701 // related callback to be invoked.
1702 if (regprog_in_use(clip_exclude_prog))
1703 return FALSE;
1704
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001705 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1706 return FALSE;
1707 }
1708 return TRUE;
1709}
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001712# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713/*
1714 * An X IO Error handler, used to catch error while opening the display.
1715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001717x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 /* This function should not return, it causes exit(). Longjump instead. */
1720 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001721# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001722 return 0; /* avoid the compiler complains about missing return value */
1723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724}
1725# endif
1726
1727/*
1728 * An X IO Error handler, used to catch terminal errors.
1729 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001730static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001733x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001736 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 x11_window = 0;
1738 x11_display = NULL;
1739 xterm_Shell = (Widget)0;
1740
1741 /* This function should not return, it causes exit(). Longjump instead. */
1742 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001743# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001744 return 0; /* avoid the compiler complains about missing return value */
1745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001747
1748/*
1749 * If the X11 connection was lost try to restore it.
1750 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001751 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001752 */
1753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001754may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001755{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001756 // No point in restoring the connecting if we are exiting or dying.
1757 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001760
1761# ifndef LESSTIF_VERSION
1762 /* This has been reported to avoid Vim getting stuck. */
1763 if (app_context != (XtAppContext)NULL)
1764 {
1765 XtDestroyApplicationContext(app_context);
1766 app_context = (XtAppContext)NULL;
1767 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1768 }
1769# endif
1770
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001771 setup_term_clip();
1772 get_x11_title(FALSE);
1773 }
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 * Test if "dpy" and x11_window are valid by getting the window title.
1779 * I don't actually want it yet, so there may be a simpler call to use, but
1780 * this will cause the error handler x_error_check() to be called if anything
1781 * is wrong, such as the window pointer being invalid (as can happen when the
1782 * user changes his DISPLAY, but not his WINDOWID) -- webb
1783 */
1784 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001785test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 int (*old_handler)();
1788 XTextProperty text_prop;
1789
1790 old_handler = XSetErrorHandler(x_error_check);
1791 got_x_error = FALSE;
1792 if (XGetWMName(dpy, x11_window, &text_prop))
1793 XFree((void *)text_prop.value);
1794 XSync(dpy, False);
1795 (void)XSetErrorHandler(old_handler);
1796
1797 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001798 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 return (got_x_error ? FAIL : OK);
1801}
1802#endif
1803
1804#ifdef FEAT_TITLE
1805
1806#ifdef FEAT_X11
1807
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001808static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810/*
1811 * try to get x11 window and display
1812 *
1813 * return FAIL for failure, OK otherwise
1814 */
1815 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001816get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 char *winid;
1819 static int result = -1;
1820#define XD_NONE 0 /* x11_display not set here */
1821#define XD_HERE 1 /* x11_display opened here */
1822#define XD_GUI 2 /* x11_display used from gui.dpy */
1823#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1824 static int x11_display_from = XD_NONE;
1825 static int did_set_error_handler = FALSE;
1826
1827 if (!did_set_error_handler)
1828 {
1829 /* X just exits if it finds an error otherwise! */
1830 (void)XSetErrorHandler(x_error_handler);
1831 did_set_error_handler = TRUE;
1832 }
1833
Bram Moolenaar9372a112005-12-06 19:59:18 +00001834#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (gui.in_use)
1836 {
1837 /*
1838 * If the X11 display was opened here before, for the window where Vim
1839 * was started, close that one now to avoid a memory leak.
1840 */
1841 if (x11_display_from == XD_HERE && x11_display != NULL)
1842 {
1843 XCloseDisplay(x11_display);
1844 x11_display_from = XD_NONE;
1845 }
1846 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1847 {
1848 x11_display_from = XD_GUI;
1849 return OK;
1850 }
1851 x11_display = NULL;
1852 return FAIL;
1853 }
1854 else if (x11_display_from == XD_GUI)
1855 {
1856 /* GUI must have stopped somehow, clear x11_display */
1857 x11_window = 0;
1858 x11_display = NULL;
1859 x11_display_from = XD_NONE;
1860 }
1861#endif
1862
1863 /* When started with the "-X" argument, don't try connecting. */
1864 if (!x_connect_to_server())
1865 return FAIL;
1866
1867 /*
1868 * If WINDOWID not set, should try another method to find out
1869 * what the current window number is. The only code I know for
1870 * this is very complicated.
1871 * We assume that zero is invalid for WINDOWID.
1872 */
1873 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1874 x11_window = (Window)atol(winid);
1875
1876#ifdef FEAT_XCLIPBOARD
1877 if (xterm_dpy != NULL && x11_window != 0)
1878 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001879 /* We may have checked it already, but Gnome terminal can move us to
1880 * another window, so we need to check every time. */
1881 if (x11_display_from != XD_XTERM)
1882 {
1883 /*
1884 * If the X11 display was opened here before, for the window where
1885 * Vim was started, close that one now to avoid a memory leak.
1886 */
1887 if (x11_display_from == XD_HERE && x11_display != NULL)
1888 XCloseDisplay(x11_display);
1889 x11_display = xterm_dpy;
1890 x11_display_from = XD_XTERM;
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (test_x11_window(x11_display) == FAIL)
1893 {
1894 /* probably bad $WINDOWID */
1895 x11_window = 0;
1896 x11_display = NULL;
1897 x11_display_from = XD_NONE;
1898 return FAIL;
1899 }
1900 return OK;
1901 }
1902#endif
1903
1904 if (x11_window == 0 || x11_display == NULL)
1905 result = -1;
1906
1907 if (result != -1) /* Have already been here and set this */
1908 return result; /* Don't do all these X calls again */
1909
1910 if (x11_window != 0 && x11_display == NULL)
1911 {
1912#ifdef SET_SIG_ALARM
1913 RETSIGTYPE (*sig_save)();
1914#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001915#ifdef ELAPSED_FUNC
1916 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001919 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
1921
1922#ifdef SET_SIG_ALARM
1923 /*
1924 * Opening the Display may hang if the DISPLAY setting is wrong, or
1925 * the network connection is bad. Set an alarm timer to get out.
1926 */
1927 sig_alarm_called = FALSE;
1928 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1929 (RETSIGTYPE (*)())sig_alarm);
1930 alarm(2);
1931#endif
1932 x11_display = XOpenDisplay(NULL);
1933
1934#ifdef SET_SIG_ALARM
1935 alarm(0);
1936 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1937 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001938 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939#endif
1940 if (x11_display != NULL)
1941 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001942# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001944 {
1945 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001946 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001947 verbose_leave();
1948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949# endif
1950 if (test_x11_window(x11_display) == FAIL)
1951 {
1952 /* Maybe window id is bad */
1953 x11_window = 0;
1954 XCloseDisplay(x11_display);
1955 x11_display = NULL;
1956 }
1957 else
1958 x11_display_from = XD_HERE;
1959 }
1960 }
1961 if (x11_window == 0 || x11_display == NULL)
1962 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001963
1964# ifdef FEAT_EVAL
1965 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1966# endif
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 return (result = OK);
1969}
1970
1971/*
1972 * Determine original x11 Window Title
1973 */
1974 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001975get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001977 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978}
1979
1980/*
1981 * Determine original x11 Window icon
1982 */
1983 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001984get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
1986 int retval = FALSE;
1987
1988 retval = get_x11_thing(FALSE, test_only);
1989
1990 /* could not get old icon, use terminal name */
1991 if (oldicon == NULL && !test_only)
1992 {
1993 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001994 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001996 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998
1999 return retval;
2000}
2001
2002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002003get_x11_thing(
2004 int get_title, /* get title string */
2005 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 XTextProperty text_prop;
2008 int retval = FALSE;
2009 Status status;
2010
2011 if (get_x11_windis() == OK)
2012 {
2013 /* Get window/icon name if any */
2014 if (get_title)
2015 status = XGetWMName(x11_display, x11_window, &text_prop);
2016 else
2017 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2018
2019 /*
2020 * If terminal is xterm, then x11_window may be a child window of the
2021 * outer xterm window that actually contains the window/icon name, so
2022 * keep traversing up the tree until a window with a title/icon is
2023 * found.
2024 */
2025 /* Previously this was only done for xterm and alikes. I don't see a
2026 * reason why it would fail for other terminal emulators.
2027 * if (term_is_xterm) */
2028 {
2029 Window root;
2030 Window parent;
2031 Window win = x11_window;
2032 Window *children;
2033 unsigned int num_children;
2034
2035 while (!status || text_prop.value == NULL)
2036 {
2037 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2038 &num_children))
2039 break;
2040 if (children)
2041 XFree((void *)children);
2042 if (parent == root || parent == 0)
2043 break;
2044
2045 win = parent;
2046 if (get_title)
2047 status = XGetWMName(x11_display, win, &text_prop);
2048 else
2049 status = XGetWMIconName(x11_display, win, &text_prop);
2050 }
2051 }
2052 if (status && text_prop.value != NULL)
2053 {
2054 retval = TRUE;
2055 if (!test_only)
2056 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01002057 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (get_title)
2060 oldtitle = vim_strsave((char_u *)text_prop.value);
2061 else
2062 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 else
2065 {
2066 char **cl;
2067 Status transform_status;
2068 int n = 0;
2069
2070 transform_status = XmbTextPropertyToTextList(x11_display,
2071 &text_prop,
2072 &cl, &n);
2073 if (transform_status >= Success && n > 0 && cl[0])
2074 {
2075 if (get_title)
2076 oldtitle = vim_strsave((char_u *) cl[0]);
2077 else
2078 oldicon = vim_strsave((char_u *) cl[0]);
2079 XFreeStringList(cl);
2080 }
2081 else
2082 {
2083 if (get_title)
2084 oldtitle = vim_strsave((char_u *)text_prop.value);
2085 else
2086 oldicon = vim_strsave((char_u *)text_prop.value);
2087 }
2088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 XFree((void *)text_prop.value);
2091 }
2092 }
2093 return retval;
2094}
2095
Bram Moolenaarb2148f52019-01-20 23:43:57 +01002096/* Xutf8 functions are not available on older systems. Note that on some
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002097 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2098 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2099 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01002100#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002101# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102# define USE_UTF8_STRING
2103# endif
2104#endif
2105
2106/*
2107 * Set x11 Window Title
2108 *
2109 * get_x11_windis() must be called before this and have returned OK
2110 */
2111 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002112set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2115 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2116 * supported everywhere and STRING doesn't work for multi-byte titles.
2117 */
2118#ifdef USE_UTF8_STRING
2119 if (enc_utf8)
2120 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2121 NULL, NULL, 0, NULL, NULL, NULL);
2122 else
2123#endif
2124 {
2125#if XtSpecificationRelease >= 4
2126# ifdef FEAT_XFONTSET
2127 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2128 NULL, NULL, 0, NULL, NULL, NULL);
2129# else
2130 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002131 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002134 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 XSetWMProperties(x11_display, x11_window, &text_prop,
2136 NULL, NULL, 0, NULL, NULL, NULL);
2137# endif
2138#else
2139 XStoreName(x11_display, x11_window, (char *)title);
2140#endif
2141 }
2142 XFlush(x11_display);
2143}
2144
2145/*
2146 * Set x11 Window icon
2147 *
2148 * get_x11_windis() must be called before this and have returned OK
2149 */
2150 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002151set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 /* See above for comments about using X*SetWMProperties(). */
2154#ifdef USE_UTF8_STRING
2155 if (enc_utf8)
2156 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2157 NULL, 0, NULL, NULL, NULL);
2158 else
2159#endif
2160 {
2161#if XtSpecificationRelease >= 4
2162# ifdef FEAT_XFONTSET
2163 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2164 NULL, 0, NULL, NULL, NULL);
2165# else
2166 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002167 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002169 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2171 NULL, 0, NULL, NULL, NULL);
2172# endif
2173#else
2174 XSetIconName(x11_display, x11_window, (char *)icon);
2175#endif
2176 }
2177 XFlush(x11_display);
2178}
2179
2180#else /* FEAT_X11 */
2181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002183get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185 return FALSE;
2186}
2187
2188 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002189get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 if (!test_only)
2192 {
2193 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002194 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002196 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198 return FALSE;
2199}
2200
2201#endif /* FEAT_X11 */
2202
2203 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002204mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 return get_x11_title(TRUE);
2207}
2208
2209 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002210mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 return get_x11_icon(TRUE);
2213}
2214
2215/*
2216 * Set the window title and icon.
2217 */
2218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002219mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 int type = 0;
2222 static int recursive = 0;
2223
2224 if (T_NAME == NULL) /* no terminal name (yet) */
2225 return;
2226 if (title == NULL && icon == NULL) /* nothing to do */
2227 return;
2228
2229 /* When one of the X11 functions causes a deadly signal, we get here again
2230 * recursively. Avoid hanging then (something is probably locked). */
2231 if (recursive)
2232 return;
2233 ++recursive;
2234
2235 /*
2236 * if the window ID and the display is known, we may use X11 calls
2237 */
2238#ifdef FEAT_X11
2239 if (get_x11_windis() == OK)
2240 type = 1;
2241#else
2242# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2243 if (gui.in_use)
2244 type = 1;
2245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#endif
2247
2248 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002249 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 * than x11 calls, because the x11 calls don't always work
2251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if ((type || *T_TS != NUL) && title != NULL)
2253 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002254 if (oldtitle_outdated)
2255 {
2256 oldtitle_outdated = FALSE;
2257 VIM_CLEAR(oldtitle);
2258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (oldtitle == NULL
2260#ifdef FEAT_GUI
2261 && !gui.in_use
2262#endif
2263 ) /* first call but not in GUI, save title */
2264 (void)get_x11_title(FALSE);
2265
2266 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2267 term_settitle(title);
2268#ifdef FEAT_X11
2269 else
2270# ifdef FEAT_GUI_GTK
2271 if (!gui.in_use) /* don't do this if GTK+ is running */
2272# endif
2273 set_x11_title(title); /* x11 */
2274#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002275#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2277 else
2278 gui_mch_settitle(title, icon);
2279#endif
2280 did_set_title = TRUE;
2281 }
2282
2283 if ((type || *T_CIS != NUL) && icon != NULL)
2284 {
2285 if (oldicon == NULL
2286#ifdef FEAT_GUI
2287 && !gui.in_use
2288#endif
2289 ) /* first call, save icon */
2290 get_x11_icon(FALSE);
2291
2292 if (*T_CIS != NUL)
2293 {
2294 out_str(T_CIS); /* set icon start */
2295 out_str_nf(icon);
2296 out_str(T_CIE); /* set icon end */
2297 out_flush();
2298 }
2299#ifdef FEAT_X11
2300 else
2301# ifdef FEAT_GUI_GTK
2302 if (!gui.in_use) /* don't do this if GTK+ is running */
2303# endif
2304 set_x11_icon(icon); /* x11 */
2305#endif
2306 did_set_icon = TRUE;
2307 }
2308 --recursive;
2309}
2310
2311/*
2312 * Restore the window/icon title.
2313 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002314 * SAVE_RESTORE_TITLE only restore title
2315 * SAVE_RESTORE_ICON only restore icon
2316 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 */
2318 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002319mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002322 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002324 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2325
2326 // pop and push from/to the stack
2327 term_pop_title(which);
2328 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329}
2330
2331#endif /* FEAT_TITLE */
2332
2333/*
2334 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002335 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 */
2337 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002338vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 if (name == NULL)
2341 return FALSE;
2342 return (STRNICMP(name, "xterm", 5) == 0
2343 || STRNICMP(name, "nxterm", 6) == 0
2344 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002345 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002347 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 || STRCMP(name, "builtin_xterm") == 0);
2349}
2350
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002351#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2352/*
2353 * Return TRUE if "name" appears to be that of a terminal
2354 * known to support the xterm-style mouse protocol.
2355 * Relies on term_is_xterm having been set to its correct value.
2356 */
2357 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002358use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002359{
2360 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002361 && (term_is_xterm
2362 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002363 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002364 || STRICMP(name, "st") == 0
2365 || STRNICMP(name, "st-", 3) == 0
2366 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002367}
2368#endif
2369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2371/*
2372 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2373 * Return 1 for "xterm".
2374 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002375 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002376 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
2378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002379use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002381 if (ttym_flags == TTYM_SGR)
2382 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002383 if (ttym_flags == TTYM_URXVT)
2384 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (ttym_flags == TTYM_XTERM2)
2386 return 2;
2387 if (ttym_flags == TTYM_XTERM)
2388 return 1;
2389 return 0;
2390}
2391#endif
2392
2393 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002394vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 if (name == NULL)
2397 return FALSE;
2398 return (STRNICMP(name, "iris-ansi", 9) == 0
2399 || STRCMP(name, "builtin_iris-ansi") == 0);
2400}
2401
2402 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002403vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
2405 if (name == NULL)
2406 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002407 /* catch VT100 - VT5xx */
2408 return ((STRNICMP(name, "vt", 2) == 0
2409 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 || STRCMP(name, "builtin_vt320") == 0);
2411}
2412
2413/*
2414 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2415 * This should include all windowed terminal emulators.
2416 */
2417 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002418vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 if (name == NULL)
2421 return FALSE;
2422 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2423 return TRUE;
2424 return ( STRNICMP(name, "hpterm", 6) == 0
2425 || STRNICMP(name, "sun-cmd", 7) == 0
2426 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002427 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 || STRNICMP(name, "dtterm", 6) == 0);
2429}
2430
2431/*
2432 * Insert user name in s[len].
2433 * Return OK if a name found.
2434 */
2435 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002436mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002439 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return OK;
2441#else
2442 return mch_get_uname(getuid(), s, len);
2443#endif
2444}
2445
2446/*
2447 * Insert user name for "uid" in s[len].
2448 * Return OK if a name found.
2449 */
2450 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002451mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2454 struct passwd *pw;
2455
2456 if ((pw = getpwuid(uid)) != NULL
2457 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2458 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002459 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 return OK;
2461 }
2462#endif
2463 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2464 return FAIL; /* a number is not a name */
2465}
2466
2467/*
2468 * Insert host name is s[len].
2469 */
2470
2471#ifdef HAVE_SYS_UTSNAME_H
2472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002473mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 struct utsname vutsname;
2476
2477 if (uname(&vutsname) < 0)
2478 *s = NUL;
2479 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002480 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481}
2482#else /* HAVE_SYS_UTSNAME_H */
2483
2484# ifdef HAVE_SYS_SYSTEMINFO_H
2485# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2486# endif
2487
2488 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002489mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491# ifdef VAXC
2492 vaxc$gethostname((char *)s, len);
2493# else
2494 gethostname((char *)s, len);
2495# endif
2496 s[len - 1] = NUL; /* make sure it's terminated */
2497}
2498#endif /* HAVE_SYS_UTSNAME_H */
2499
2500/*
2501 * return process ID
2502 */
2503 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002504mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
2506 return (long)getpid();
2507}
2508
2509#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002511strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 extern int sys_nerr;
2514 extern char *sys_errlist[];
2515 static char er[20];
2516
2517 if (err > 0 && err < sys_nerr)
2518 return (sys_errlist[err]);
2519 sprintf(er, "Error %d", err);
2520 return er;
2521}
2522#endif
2523
2524/*
2525 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2526 * Return OK for success, FAIL for failure.
2527 */
2528 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002529mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531#if defined(USE_GETCWD)
2532 if (getcwd((char *)buf, len) == NULL)
2533 {
2534 STRCPY(buf, strerror(errno));
2535 return FAIL;
2536 }
2537 return OK;
2538#else
2539 return (getwd((char *)buf) != NULL ? OK : FAIL);
2540#endif
2541}
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002544 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 *
2546 * return FAIL for failure, OK for success
2547 */
2548 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002549mch_FullName(
2550 char_u *fname,
2551 char_u *buf,
2552 int len,
2553 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002556#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 int fd = -1;
2558 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 char_u olddir[MAXPATHL];
2561 char_u *p;
2562 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002563#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002564 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2565 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002566#endif
2567
Bram Moolenaar38323e42007-03-06 19:22:53 +00002568#ifdef VMS
2569 fname = vms_fixfilename(fname);
2570#endif
2571
Bram Moolenaara2442432007-04-26 14:26:37 +00002572#ifdef __CYGWIN__
2573 /*
2574 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2575 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002576# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002577 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2578 * a forward slash. */
2579 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2580 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002581# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002582 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002583# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002584 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002585#endif
2586
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002587 /* Expand it if forced or not an absolute path.
2588 * Do not do it for "/file", the result is always "/". */
2589 if ((force || !mch_isFullName(fname))
2590 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 /*
2593 * If the file name has a path, change to that directory for a moment,
2594 * and then do the getwd() (and get back to where we were).
2595 * This will get the correct path name with "../" things.
2596 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002597 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002599#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 /*
2601 * Use fchdir() if possible, it's said to be faster and more
2602 * reliable. But on SunOS 4 it might not work. Check this by
2603 * doing a fchdir() right now.
2604 */
2605 if (!dont_fchdir)
2606 {
2607 fd = open(".", O_RDONLY | O_EXTRA, 0);
2608 if (fd >= 0 && fchdir(fd) < 0)
2609 {
2610 close(fd);
2611 fd = -1;
2612 dont_fchdir = TRUE; /* don't try again */
2613 }
2614 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 /* Only change directory when we are sure we can return to where
2618 * we are now. After doing "su" chdir(".") might not work. */
2619 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002620#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 (mch_dirname(olddir, MAXPATHL) == FAIL
2624 || mch_chdir((char *)olddir) != 0))
2625 {
2626 p = NULL; /* can't get current dir: don't chdir */
2627 retval = FAIL;
2628 }
2629 else
2630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 /* The directory is copied into buf[], to be able to remove
2632 * the file name without changing it (could be a string in
2633 * read-only memory) */
2634 if (p - fname >= len)
2635 retval = FAIL;
2636 else
2637 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002638 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (mch_chdir((char *)buf))
2640 retval = FAIL;
2641 else
2642 fname = p + 1;
2643 *buf = NUL;
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646 }
2647 if (mch_dirname(buf, len) == FAIL)
2648 {
2649 retval = FAIL;
2650 *buf = NUL;
2651 }
2652 if (p != NULL)
2653 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002654#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (fd >= 0)
2656 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002657 if (p_verbose >= 5)
2658 {
2659 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002660 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002661 verbose_leave();
2662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 l = fchdir(fd);
2664 close(fd);
2665 }
2666 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 l = mch_chdir((char *)olddir);
2669 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002670 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672
2673 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002674 if (l >= len - 1)
2675 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002676#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002677 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002679 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002684 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return FAIL;
2686
2687 /* Do not append ".", "/dir/." is equal to "/dir". */
2688 if (STRCMP(fname, ".") != 0)
2689 STRCAT(buf, fname);
2690
2691 return OK;
2692}
2693
2694/*
2695 * Return TRUE if "fname" does not depend on the current directory.
2696 */
2697 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002698mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002700#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 return ( fname[0] == '/' || fname[0] == '.' ||
2702 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2703 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2704 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002705#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708}
2709
Bram Moolenaar24552be2005-12-10 20:17:30 +00002710#if defined(USE_FNAME_CASE) || defined(PROTO)
2711/*
2712 * Set the case of the file name, if it already exists. This will cause the
2713 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002714 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002715 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002717fname_case(
2718 char_u *name,
2719 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002720{
2721 struct stat st;
2722 char_u *slash, *tail;
2723 DIR *dirp;
2724 struct dirent *dp;
2725
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002726 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002727 {
2728 /* Open the directory where the file is located. */
2729 slash = vim_strrchr(name, '/');
2730 if (slash == NULL)
2731 {
2732 dirp = opendir(".");
2733 tail = name;
2734 }
2735 else
2736 {
2737 *slash = NUL;
2738 dirp = opendir((char *)name);
2739 *slash = '/';
2740 tail = slash + 1;
2741 }
2742
2743 if (dirp != NULL)
2744 {
2745 while ((dp = readdir(dirp)) != NULL)
2746 {
2747 /* Only accept names that differ in case and are the same byte
2748 * length. TODO: accept different length name. */
2749 if (STRICMP(tail, dp->d_name) == 0
2750 && STRLEN(tail) == STRLEN(dp->d_name))
2751 {
2752 char_u newname[MAXPATHL + 1];
2753 struct stat st2;
2754
2755 /* Verify the inode is equal. */
2756 vim_strncpy(newname, name, MAXPATHL);
2757 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2758 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002759 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002760 && st.st_ino == st2.st_ino
2761 && st.st_dev == st2.st_dev)
2762 {
2763 STRCPY(tail, dp->d_name);
2764 break;
2765 }
2766 }
2767 }
2768
2769 closedir(dirp);
2770 }
2771 }
2772}
2773#endif
2774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775/*
2776 * Get file permissions for 'name'.
2777 * Returns -1 when it doesn't exist.
2778 */
2779 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002780mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 struct stat statb;
2783
2784 /* Keep the #ifdef outside of stat(), it may be a macro. */
2785#ifdef VMS
2786 if (stat((char *)vms_fixfilename(name), &statb))
2787#else
2788 if (stat((char *)name, &statb))
2789#endif
2790 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002791#ifdef __INTERIX
2792 /* The top bit makes the value negative, which means the file doesn't
2793 * exist. Remove the bit, we don't use it. */
2794 return statb.st_mode & ~S_ADDACE;
2795#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798}
2799
2800/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002801 * Set file permission for "name" to "perm".
2802 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002805mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
2807 return (chmod((char *)
2808#ifdef VMS
2809 vms_fixfilename(name),
2810#else
2811 name,
2812#endif
2813 (mode_t)perm) == 0 ? OK : FAIL);
2814}
2815
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002816#if defined(HAVE_FCHMOD) || defined(PROTO)
2817/*
2818 * Set file permission for open file "fd" to "perm".
2819 * Return FAIL for failure, OK otherwise.
2820 */
2821 int
2822mch_fsetperm(int fd, long perm)
2823{
2824 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2825}
2826#endif
2827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828#if defined(HAVE_ACL) || defined(PROTO)
2829# ifdef HAVE_SYS_ACL_H
2830# include <sys/acl.h>
2831# endif
2832# ifdef HAVE_SYS_ACCESS_H
2833# include <sys/access.h>
2834# endif
2835
2836# ifdef HAVE_SOLARIS_ACL
2837typedef struct vim_acl_solaris_T {
2838 int acl_cnt;
2839 aclent_t *acl_entry;
2840} vim_acl_solaris_T;
2841# endif
2842
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002843#if defined(HAVE_SELINUX) || defined(PROTO)
2844/*
2845 * Copy security info from "from_file" to "to_file".
2846 */
2847 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002848mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002849{
2850 if (from_file == NULL)
2851 return;
2852
2853 if (selinux_enabled == -1)
2854 selinux_enabled = is_selinux_enabled();
2855
2856 if (selinux_enabled > 0)
2857 {
2858 security_context_t from_context = NULL;
2859 security_context_t to_context = NULL;
2860
2861 if (getfilecon((char *)from_file, &from_context) < 0)
2862 {
2863 /* If the filesystem doesn't support extended attributes,
2864 the original had no special security context and the
2865 target cannot have one either. */
2866 if (errno == EOPNOTSUPP)
2867 return;
2868
Bram Moolenaar32526b32019-01-19 17:43:09 +01002869 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002870 msg_outtrans(from_file);
2871 msg_putchar('\n');
2872 return;
2873 }
2874 if (getfilecon((char *)to_file, &to_context) < 0)
2875 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002876 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002877 msg_outtrans(to_file);
2878 msg_putchar('\n');
2879 freecon (from_context);
2880 return ;
2881 }
2882 if (strcmp(from_context, to_context) != 0)
2883 {
2884 if (setfilecon((char *)to_file, from_context) < 0)
2885 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002886 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002887 msg_outtrans(to_file);
2888 msg_putchar('\n');
2889 }
2890 }
2891 freecon(to_context);
2892 freecon(from_context);
2893 }
2894}
2895#endif /* HAVE_SELINUX */
2896
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002897#if defined(HAVE_SMACK) && !defined(PROTO)
2898/*
2899 * Copy security info from "from_file" to "to_file".
2900 */
2901 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002902mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002903{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002904 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002905 {
2906 XATTR_NAME_SMACK,
2907 XATTR_NAME_SMACKEXEC,
2908 XATTR_NAME_SMACKMMAP
2909 };
2910
2911 char buffer[SMACK_LABEL_LEN];
2912 const char *name;
2913 int index;
2914 int ret;
2915 ssize_t size;
2916
2917 if (from_file == NULL)
2918 return;
2919
2920 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2921 / sizeof(smack_copied_attributes)[0]) ; index++)
2922 {
2923 /* get the name of the attribute to copy */
2924 name = smack_copied_attributes[index];
2925
2926 /* get the value of the attribute in buffer */
2927 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2928 if (size >= 0)
2929 {
2930 /* copy the attribute value of buffer */
2931 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2932 if (ret < 0)
2933 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002934 vim_snprintf((char *)IObuff, IOSIZE,
2935 _("Could not set security context %s for %s"),
2936 name, to_file);
2937 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002938 msg_putchar('\n');
2939 }
2940 }
2941 else
2942 {
2943 /* what reason of not having the attribute value? */
2944 switch (errno)
2945 {
2946 case ENOTSUP:
2947 /* extended attributes aren't supported or enabled */
2948 /* should a message be echoed? not sure... */
Bram Moolenaarb2148f52019-01-20 23:43:57 +01002949 return; /* leave because it isn't useful to continue */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002950
2951 case ERANGE:
2952 default:
2953 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002954 vim_snprintf((char *)IObuff, IOSIZE,
2955 _("Could not get security context %s for %s. Removing it!"),
2956 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002957 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002958 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002959 /* FALLTHROUGH to remove the attribute */
2960
2961 case ENODATA:
2962 /* no attribute of this name */
2963 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002964 /* Silently ignore errors, apparently this happens when
2965 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002966 break;
2967 }
2968 }
2969 }
2970}
2971#endif /* HAVE_SMACK */
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973/*
2974 * Return a pointer to the ACL of file "fname" in allocated memory.
2975 * Return NULL if the ACL is not available for whatever reason.
2976 */
2977 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002978mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 vim_acl_T ret = NULL;
2981#ifdef HAVE_POSIX_ACL
2982 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2983#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002984#ifdef HAVE_SOLARIS_ZFS_ACL
2985 acl_t *aclent;
2986
2987 if (acl_get((char *)fname, 0, &aclent) < 0)
2988 return NULL;
2989 ret = (vim_acl_T)aclent;
2990#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#ifdef HAVE_SOLARIS_ACL
2992 vim_acl_solaris_T *aclent;
2993
2994 aclent = malloc(sizeof(vim_acl_solaris_T));
2995 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2996 {
2997 free(aclent);
2998 return NULL;
2999 }
3000 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3001 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3002 {
3003 free(aclent->acl_entry);
3004 free(aclent);
3005 return NULL;
3006 }
3007 ret = (vim_acl_T)aclent;
3008#else
3009#if defined(HAVE_AIX_ACL)
3010 int aclsize;
3011 struct acl *aclent;
3012
3013 aclsize = sizeof(struct acl);
3014 aclent = malloc(aclsize);
3015 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3016 {
3017 if (errno == ENOSPC)
3018 {
3019 aclsize = aclent->acl_len;
3020 aclent = realloc(aclent, aclsize);
3021 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3022 {
3023 free(aclent);
3024 return NULL;
3025 }
3026 }
3027 else
3028 {
3029 free(aclent);
3030 return NULL;
3031 }
3032 }
3033 ret = (vim_acl_T)aclent;
3034#endif /* HAVE_AIX_ACL */
3035#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003036#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037#endif /* HAVE_POSIX_ACL */
3038 return ret;
3039}
3040
3041/*
3042 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3043 */
3044 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003045mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 if (aclent == NULL)
3048 return;
3049#ifdef HAVE_POSIX_ACL
3050 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3051#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003052#ifdef HAVE_SOLARIS_ZFS_ACL
3053 acl_set((char *)fname, (acl_t *)aclent);
3054#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#ifdef HAVE_SOLARIS_ACL
3056 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3057 ((vim_acl_solaris_T *)aclent)->acl_entry);
3058#else
3059#ifdef HAVE_AIX_ACL
3060 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3061#endif /* HAVE_AIX_ACL */
3062#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003063#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif /* HAVE_POSIX_ACL */
3065}
3066
3067 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003068mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 if (aclent == NULL)
3071 return;
3072#ifdef HAVE_POSIX_ACL
3073 acl_free((acl_t)aclent);
3074#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003075#ifdef HAVE_SOLARIS_ZFS_ACL
3076 acl_free((acl_t *)aclent);
3077#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#ifdef HAVE_SOLARIS_ACL
3079 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3080 free(aclent);
3081#else
3082#ifdef HAVE_AIX_ACL
3083 free(aclent);
3084#endif /* HAVE_AIX_ACL */
3085#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003086#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#endif /* HAVE_POSIX_ACL */
3088}
3089#endif
3090
3091/*
3092 * Set hidden flag for "name".
3093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003095mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 /* can't hide a file */
3098}
3099
3100/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003101 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * return FALSE if "name" is not a directory
3103 * return FALSE for error
3104 */
3105 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003106mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 struct stat statb;
3109
3110 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3111 return FALSE;
3112 if (stat((char *)name, &statb))
3113 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115}
3116
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003117/*
3118 * return TRUE if "name" is a directory, NOT a symlink to a directory
3119 * return FALSE if "name" is not a directory
3120 * return FALSE for error
3121 */
3122 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003123mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003124{
3125 struct stat statb;
3126
3127 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3128 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003129 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003130 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003131 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003132}
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3136 */
3137 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003138executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 struct stat st;
3141
3142 if (stat((char *)name, &st))
3143 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003144#ifdef VMS
3145 /* Like on Unix system file can have executable rights but not necessarily
3146 * be an executable, but on Unix is not a default for an ordianry file to
3147 * have an executable flag - on VMS it is in most cases.
3148 * Therefore, this check does not have any sense - let keep us to the
3149 * conventions instead:
3150 * *.COM and *.EXE files are the executables - the rest are not. This is
3151 * not ideal but better then it was.
3152 */
3153 int vms_executable = 0;
3154 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3155 {
3156 if (strstr(vms_tolower((char*)name),".exe") != NULL
3157 || strstr(vms_tolower((char*)name),".com")!= NULL)
3158 vms_executable = 1;
3159 }
3160 return vms_executable;
3161#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164}
3165
3166/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003167 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003168 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * Return -1 if unknown.
3170 */
3171 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003172mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 char_u *buf;
3175 char_u *p, *e;
3176 int retval;
3177
Bram Moolenaarb5971142015-03-21 17:32:19 +01003178 /* When "use_path" is false and if it's an absolute or relative path don't
3179 * need to use $PATH. */
3180 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3181 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003182 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003183 /* There must be a path separator, files in the current directory
3184 * can't be executed. */
3185 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003186 {
3187 if (path != NULL)
3188 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003189 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003190 *path = FullName_save(name, TRUE);
3191 else
3192 *path = vim_strsave(name);
3193 }
3194 return TRUE;
3195 }
3196 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 p = (char_u *)getenv("PATH");
3200 if (p == NULL || *p == NUL)
3201 return -1;
3202 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3203 if (buf == NULL)
3204 return -1;
3205
3206 /*
3207 * Walk through all entries in $PATH to check if "name" exists there and
3208 * is an executable file.
3209 */
3210 for (;;)
3211 {
3212 e = (char_u *)strchr((char *)p, ':');
3213 if (e == NULL)
3214 e = p + STRLEN(p);
3215 if (e - p <= 1) /* empty entry means current dir */
3216 STRCPY(buf, "./");
3217 else
3218 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003219 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 add_pathsep(buf);
3221 }
3222 STRCAT(buf, name);
3223 retval = executable_file(buf);
3224 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003225 {
3226 if (path != NULL)
3227 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003228 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003229 *path = FullName_save(buf, TRUE);
3230 else
3231 *path = vim_strsave(buf);
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 if (*e != ':')
3237 break;
3238 p = e + 1;
3239 }
3240
3241 vim_free(buf);
3242 return retval;
3243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245/*
3246 * Check what "name" is:
3247 * NODE_NORMAL: file or directory (or doesn't exist)
3248 * NODE_WRITABLE: writable device, socket, fifo, etc.
3249 * NODE_OTHER: non-writable things
3250 */
3251 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003252mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 struct stat st;
3255
3256 if (stat((char *)name, &st))
3257 return NODE_NORMAL;
3258 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3259 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3261 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 /* Everything else is writable? */
3263 return NODE_WRITABLE;
3264}
3265
3266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003267mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269#ifdef HAVE_CHECK_STACK_GROWTH
3270 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 check_stack_growth((char *)&i);
3273
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003274# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 get_stack_limit();
3276# endif
3277
3278#endif
3279
3280 /*
3281 * Setup an alternative stack for signals. Helps to catch signals when
3282 * running out of stack space.
3283 * Use of sigaltstack() is preferred, it's more portable.
3284 * Ignore any errors.
3285 */
3286#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003287 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 init_signal_stack();
3289#endif
3290}
3291
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003292#if defined(EXITFREE) || defined(PROTO)
3293 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003294mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003295{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3297 if (clip_star.owned)
3298 clip_lose_selection(&clip_star);
3299 if (clip_plus.owned)
3300 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003301# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003302# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003303 if (xterm_Shell != (Widget)0)
3304 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003305# ifndef LESSTIF_VERSION
3306 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003307 if (xterm_dpy != NULL)
3308 XtCloseDisplay(xterm_dpy);
3309 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003310 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003311 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003312# ifdef FEAT_X11
3313 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3314# endif
3315 }
3316# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003317# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003318# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003319 if (x11_display != NULL
3320# ifdef FEAT_XCLIPBOARD
3321 && x11_display != xterm_dpy
3322# endif
3323 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 XCloseDisplay(x11_display);
3325# endif
3326# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003327 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328# endif
3329# ifdef FEAT_TITLE
3330 vim_free(oldtitle);
3331 vim_free(oldicon);
3332# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003333}
3334#endif
3335
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336/*
3337 * Output a newline when exiting.
3338 * Make sure the newline goes to the same stream as the text.
3339 */
3340 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003341exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003343 if (silent_mode)
3344 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (newline_on_exit || msg_didout)
3346 {
3347 if (msg_use_printf())
3348 {
3349 if (info_message)
3350 mch_msg("\n");
3351 else
3352 mch_errmsg("\r\n");
3353 }
3354 else
3355 out_char('\n');
3356 }
3357 else
3358 {
3359 restore_cterm_colors(); /* get original colors back */
3360 msg_clr_eos_force(); /* clear the rest of the display */
3361 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3362 }
3363}
3364
3365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003366mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 exiting = TRUE;
3369
3370#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3371 x11_export_final_selection();
3372#endif
3373
3374#ifdef FEAT_GUI
3375 if (!gui.in_use)
3376#endif
3377 {
3378 settmode(TMODE_COOK);
3379#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003380 // restore xterm title and icon name
3381 mch_restore_title(SAVE_RESTORE_BOTH);
3382 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#endif
3384 /*
3385 * When t_ti is not empty but it doesn't cause swapping terminal
3386 * pages, need to output a newline when msg_didout is set. But when
3387 * t_ti does swap pages it should not go to the shell page. Do this
3388 * before stoptermcap().
3389 */
3390 if (swapping_screen() && !newline_on_exit)
3391 exit_scroll();
3392
3393 /* Stop termcap: May need to check for T_CRV response, which
3394 * requires RAW mode. */
3395 stoptermcap();
3396
3397 /*
3398 * A newline is only required after a message in the alternate screen.
3399 * This is set to TRUE by wait_return().
3400 */
3401 if (!swapping_screen() || newline_on_exit)
3402 exit_scroll();
3403
3404 /* Cursor may have been switched off without calling starttermcap()
3405 * when doing "vim -u vimrc" and vimrc contains ":q". */
3406 if (full_screen)
3407 cursor_on();
3408 }
3409 out_flush();
3410 ml_close_all(TRUE); /* remove all memfiles */
3411 may_core_dump();
3412#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 gui_exit(r);
3415#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003416
Bram Moolenaar56718732006-03-15 22:53:57 +00003417#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003418 mac_conv_cleanup();
3419#endif
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#ifdef __QNX__
3422 /* A core dump won't be created if the signal handler
3423 * doesn't return, so we can't call exit() */
3424 if (deadly_signal != 0)
3425 return;
3426#endif
3427
Bram Moolenaar009b2592004-10-24 19:18:58 +00003428#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003429 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003430#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003431
3432#ifdef EXITFREE
3433 free_all_mem();
3434#endif
3435
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 exit(r);
3437}
3438
3439 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003440may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 if (deadly_signal != 0)
3443 {
3444 signal(deadly_signal, SIG_DFL);
3445 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3446 }
3447}
3448
3449#ifndef VMS
3450
3451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003452mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 static int first = TRUE;
3455
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003456#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457# ifdef HAVE_TERMIOS_H
3458 static struct termios told;
3459 struct termios tnew;
3460# else
3461 static struct termio told;
3462 struct termio tnew;
3463# endif
3464
3465 if (first)
3466 {
3467 first = FALSE;
3468# if defined(HAVE_TERMIOS_H)
3469 tcgetattr(read_cmd_fd, &told);
3470# else
3471 ioctl(read_cmd_fd, TCGETA, &told);
3472# endif
3473 }
3474
3475 tnew = told;
3476 if (tmode == TMODE_RAW)
3477 {
3478 /*
3479 * ~ICRNL enables typing ^V^M
3480 */
3481 tnew.c_iflag &= ~ICRNL;
3482 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3483# if defined(IEXTEN) && !defined(__MINT__)
3484 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3485 /* but it breaks function keys on MINT */
3486# endif
3487 );
3488# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3489 tnew.c_oflag &= ~ONLCR;
3490# endif
3491 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3492 tnew.c_cc[VTIME] = 0; /* don't wait */
3493 }
3494 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003495 {
3496 /* Also reset ICANON here, otherwise on Solaris select() won't see
3497 * typeahead characters. */
3498 tnew.c_lflag &= ~(ICANON | ECHO);
3499 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3500 tnew.c_cc[VTIME] = 0; /* don't wait */
3501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503# if defined(HAVE_TERMIOS_H)
3504 {
3505 int n = 10;
3506
3507 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3508 * few times. */
3509 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3510 && errno == EINTR && n > 0)
3511 --n;
3512 }
3513# else
3514 ioctl(read_cmd_fd, TCSETA, &tnew);
3515# endif
3516
3517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 /*
3519 * for "old" tty systems
3520 */
3521# ifndef TIOCSETN
3522# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3523# endif
3524 static struct sgttyb ttybold;
3525 struct sgttyb ttybnew;
3526
3527 if (first)
3528 {
3529 first = FALSE;
3530 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3531 }
3532
3533 ttybnew = ttybold;
3534 if (tmode == TMODE_RAW)
3535 {
3536 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3537 ttybnew.sg_flags |= RAW;
3538 }
3539 else if (tmode == TMODE_SLEEP)
3540 ttybnew.sg_flags &= ~(ECHO);
3541 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3542#endif
3543 curr_tmode = tmode;
3544}
3545
3546/*
3547 * Try to get the code for "t_kb" from the stty setting
3548 *
3549 * Even if termcap claims a backspace key, the user's setting *should*
3550 * prevail. stty knows more about reality than termcap does, and if
3551 * somebody's usual erase key is DEL (which, for most BSD users, it will
3552 * be), they're going to get really annoyed if their erase key starts
3553 * doing forward deletes for no reason. (Eric Fischer)
3554 */
3555 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003556get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003558 ttyinfo_T info;
3559 char_u buf[2];
3560 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003562 if (get_tty_info(read_cmd_fd, &info) == OK)
3563 {
3564 intr_char = info.interrupt;
3565 buf[0] = info.backspace;
3566 buf[1] = NUL;
3567 add_termcode((char_u *)"kb", buf, FALSE);
3568
3569 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3570 p = find_termcode((char_u *)"kD");
3571 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3572 do_fixdel(NULL);
3573 }
3574}
3575
3576/*
3577 * Obtain the characters that Backspace and Enter produce on "fd".
3578 * Returns OK or FAIL.
3579 */
3580 int
3581get_tty_info(int fd, ttyinfo_T *info)
3582{
3583#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584# ifdef HAVE_TERMIOS_H
3585 struct termios keys;
3586# else
3587 struct termio keys;
3588# endif
3589
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003590 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003592 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003594 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003596 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003598 info->backspace = keys.c_cc[VERASE];
3599 info->interrupt = keys.c_cc[VINTR];
3600 if (keys.c_iflag & ICRNL)
3601 info->enter = NL;
3602 else
3603 info->enter = CAR;
3604 if (keys.c_oflag & ONLCR)
3605 info->nl_does_cr = TRUE;
3606 else
3607 info->nl_does_cr = FALSE;
3608 return OK;
3609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610#else
3611 /* for "old" tty systems */
3612 struct sgttyb keys;
3613
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003614 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003616 info->backspace = keys.sg_erase;
3617 info->interrupt = keys.sg_kill;
3618 info->enter = CAR;
3619 info->nl_does_cr = TRUE;
3620 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624}
3625
3626#endif /* VMS */
3627
3628#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003629static int mouse_ison = FALSE;
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631/*
3632 * Set mouse clicks on or off.
3633 */
3634 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003635mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003637# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003638 static int bevalterm_ison = FALSE;
3639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 int xterm_mouse_vers;
3641
Bram Moolenaara06afc72018-08-27 23:24:16 +02003642# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3643 if (!on)
3644 // Make sure not tracing mouse movements. Important when a button-down
3645 // was received but no release yet.
3646 stop_xterm_trace();
3647# endif
3648
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003649 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003650# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003651 && p_bevalterm == bevalterm_ison
3652# endif
3653 )
3654 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return;
3656
3657 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003658
3659# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003660 if (ttym_flags == TTYM_URXVT)
3661 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003662 out_str_nf((char_u *)
3663 (on
3664 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3665 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003666 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003667 }
3668# endif
3669
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003670# ifdef FEAT_MOUSE_SGR
3671 if (ttym_flags == TTYM_SGR)
3672 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003673 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003674 out_str_nf((char_u *)
3675 (on
3676 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3677 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003678 mouse_ison = on;
3679 }
3680# endif
3681
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003682# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003683 if (bevalterm_ison != (p_bevalterm && on))
3684 {
3685 bevalterm_ison = (p_bevalterm && on);
3686 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3687 /* disable mouse movement events, enabling is below */
3688 out_str_nf((char_u *)
3689 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003690 }
3691# endif
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (xterm_mouse_vers > 0)
3694 {
3695 if (on) /* enable mouse events, use mouse tracking if available */
3696 out_str_nf((char_u *)
3697 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003698 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003699# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003700 bevalterm_ison
3701 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3702# endif
3703 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3705 else /* disable mouse events, could probably always send the same */
3706 out_str_nf((char_u *)
3707 (xterm_mouse_vers > 1
3708 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3709 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003710 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712
3713# ifdef FEAT_MOUSE_DEC
3714 else if (ttym_flags == TTYM_DEC)
3715 {
3716 if (on) /* enable mouse events */
3717 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3718 else /* disable mouse events */
3719 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003720 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722# endif
3723
3724# ifdef FEAT_MOUSE_GPM
3725 else
3726 {
3727 if (on)
3728 {
3729 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003730 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else
3733 {
3734 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738# endif
3739
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003740# ifdef FEAT_SYSMOUSE
3741 else
3742 {
3743 if (on)
3744 {
3745 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003746 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003747 }
3748 else
3749 {
3750 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003751 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003752 }
3753 }
3754# endif
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756# ifdef FEAT_MOUSE_JSB
3757 else
3758 {
3759 if (on)
3760 {
3761 /* D - Enable Mouse up/down messages
3762 * L - Enable Left Button Reporting
3763 * M - Enable Middle Button Reporting
3764 * R - Enable Right Button Reporting
3765 * K - Enable SHIFT and CTRL key Reporting
3766 * + - Enable Advanced messaging of mouse moves and up/down messages
3767 * Q - Quiet No Ack
3768 * # - Numeric value of mouse pointer required
3769 * 0 = Multiview 2000 cursor, used as standard
3770 * 1 = Windows Arrow
3771 * 2 = Windows I Beam
3772 * 3 = Windows Hour Glass
3773 * 4 = Windows Cross Hair
3774 * 5 = Windows UP Arrow
3775 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003776# ifdef JSBTERM_MOUSE_NONADVANCED
3777 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3779 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003780# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3782 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003783# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003784 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 else
3787 {
3788 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3789 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 }
3793# endif
3794# ifdef FEAT_MOUSE_PTERM
3795 else
3796 {
3797 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3798 if (on)
3799 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3800 else
3801 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003802 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804# endif
3805}
3806
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003807#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003808/*
3809 * Called when 'balloonevalterm' changed.
3810 */
3811 void
3812mch_bevalterm_changed(void)
3813{
3814 mch_setmouse(mouse_ison);
3815}
3816#endif
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818/*
3819 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3820 */
3821 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003822check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824# ifdef FEAT_MOUSE_XTERM
3825 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003826# ifdef FEAT_MOUSE_URXVT
3827 && use_xterm_mouse() != 3
3828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829# ifdef FEAT_GUI
3830 && !gui.in_use
3831# endif
3832 )
3833 {
3834 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003835 ? IF_EB("\233M", CSI_STR "M")
3836 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (*p_mouse != NUL)
3838 {
3839 /* force mouse off and maybe on to send possibly new mouse
3840 * activation sequence to the xterm, with(out) drag tracing. */
3841 mch_setmouse(FALSE);
3842 setmouse();
3843 }
3844 }
3845 else
3846 del_mouse_termcode(KS_MOUSE);
3847# endif
3848
3849# ifdef FEAT_MOUSE_GPM
3850 if (!use_xterm_mouse()
3851# ifdef FEAT_GUI
3852 && !gui.in_use
3853# endif
3854 )
3855 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3856# endif
3857
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003858# ifdef FEAT_SYSMOUSE
3859 if (!use_xterm_mouse()
3860# ifdef FEAT_GUI
3861 && !gui.in_use
3862# endif
3863 )
3864 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3865# endif
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003868 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (!use_xterm_mouse()
3870# ifdef FEAT_GUI
3871 && !gui.in_use
3872# endif
3873 )
3874 set_mouse_termcode(KS_JSBTERM_MOUSE,
3875 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3876 else
3877 del_mouse_termcode(KS_JSBTERM_MOUSE);
3878# endif
3879
3880# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003881 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 * define it in the GUI or when using an xterm. */
3883 if (!use_xterm_mouse()
3884# ifdef FEAT_GUI
3885 && !gui.in_use
3886# endif
3887 )
3888 set_mouse_termcode(KS_NETTERM_MOUSE,
3889 (char_u *)IF_EB("\033}", ESC_STR "}"));
3890 else
3891 del_mouse_termcode(KS_NETTERM_MOUSE);
3892# endif
3893
3894# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003895 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003896 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897# ifdef FEAT_GUI
3898 && !gui.in_use
3899# endif
3900 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003901 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3902 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
3904 del_mouse_termcode(KS_DEC_MOUSE);
3905# endif
3906# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003907 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003908 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909# ifdef FEAT_GUI
3910 && !gui.in_use
3911# endif
3912 )
3913 set_mouse_termcode(KS_PTERM_MOUSE,
3914 (char_u *) IF_EB("\033[", ESC_STR "["));
3915 else
3916 del_mouse_termcode(KS_PTERM_MOUSE);
3917# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003918# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003919 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003920# ifdef FEAT_GUI
3921 && !gui.in_use
3922# endif
3923 )
3924 {
3925 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003926 ? IF_EB("\233*M", CSI_STR "*M")
3927 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003928
3929 if (*p_mouse != NUL)
3930 {
3931 mch_setmouse(FALSE);
3932 setmouse();
3933 }
3934 }
3935 else
3936 del_mouse_termcode(KS_URXVT_MOUSE);
3937# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003938# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003939 if (use_xterm_mouse() == 4
3940# ifdef FEAT_GUI
3941 && !gui.in_use
3942# endif
3943 )
3944 {
3945 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003946 ? IF_EB("\233<*M", CSI_STR "<*M")
3947 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3948
3949 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3950 ? IF_EB("\233<*m", CSI_STR "<*m")
3951 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003952
3953 if (*p_mouse != NUL)
3954 {
3955 mch_setmouse(FALSE);
3956 setmouse();
3957 }
3958 }
3959 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003960 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003961 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003962 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3963 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965}
3966#endif
3967
3968/*
3969 * set screen mode, always fails.
3970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003972mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003974 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return FAIL;
3976}
3977
3978#ifndef VMS
3979
3980/*
3981 * Try to get the current window size:
3982 * 1. with an ioctl(), most accurate method
3983 * 2. from the environment variables LINES and COLUMNS
3984 * 3. from the termcap
3985 * 4. keep using the old values
3986 * Return OK when size could be determined, FAIL otherwise.
3987 */
3988 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003989mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 long rows = 0;
3992 long columns = 0;
3993 char_u *p;
3994
3995 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * 1. try using an ioctl. It is the most accurate method.
3997 *
3998 * Try using TIOCGWINSZ first, some systems that have it also define
3999 * TIOCGSIZE but don't have a struct ttysize.
4000 */
4001# ifdef TIOCGWINSZ
4002 {
4003 struct winsize ws;
4004 int fd = 1;
4005
4006 /* When stdout is not a tty, use stdin for the ioctl(). */
4007 if (!isatty(fd) && isatty(read_cmd_fd))
4008 fd = read_cmd_fd;
4009 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4010 {
4011 columns = ws.ws_col;
4012 rows = ws.ws_row;
4013 }
4014 }
4015# else /* TIOCGWINSZ */
4016# ifdef TIOCGSIZE
4017 {
4018 struct ttysize ts;
4019 int fd = 1;
4020
4021 /* When stdout is not a tty, use stdin for the ioctl(). */
4022 if (!isatty(fd) && isatty(read_cmd_fd))
4023 fd = read_cmd_fd;
4024 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4025 {
4026 columns = ts.ts_cols;
4027 rows = ts.ts_lines;
4028 }
4029 }
4030# endif /* TIOCGSIZE */
4031# endif /* TIOCGWINSZ */
4032
4033 /*
4034 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004035 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4036 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004038 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
4040 if ((p = (char_u *)getenv("LINES")))
4041 rows = atoi((char *)p);
4042 if ((p = (char_u *)getenv("COLUMNS")))
4043 columns = atoi((char *)p);
4044 }
4045
4046#ifdef HAVE_TGETENT
4047 /*
4048 * 3. try reading "co" and "li" entries from termcap
4049 */
4050 if (columns == 0 || rows == 0)
4051 getlinecol(&columns, &rows);
4052#endif
4053
4054 /*
4055 * 4. If everything fails, use the old values
4056 */
4057 if (columns <= 0 || rows <= 0)
4058 return FAIL;
4059
4060 Rows = rows;
4061 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004062 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return OK;
4064}
4065
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004066#if defined(FEAT_TERMINAL) || defined(PROTO)
4067/*
4068 * Report the windows size "rows" and "cols" to tty "fd".
4069 */
4070 int
4071mch_report_winsize(int fd, int rows, int cols)
4072{
4073# ifdef TIOCSWINSZ
4074 struct winsize ws;
4075
4076 ws.ws_col = cols;
4077 ws.ws_row = rows;
4078 ws.ws_xpixel = cols * 5;
4079 ws.ws_ypixel = rows * 10;
4080 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4081 {
4082 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4083 return OK;
4084 }
4085 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4086# else
4087# ifdef TIOCSSIZE
4088 struct ttysize ts;
4089
4090 ts.ts_cols = cols;
4091 ts.ts_lines = rows;
4092 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4093 {
4094 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4095 return OK;
4096 }
4097 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4098# endif
4099# endif
4100 return FAIL;
4101}
4102#endif
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104/*
4105 * Try to set the window size to Rows and Columns.
4106 */
4107 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004108mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 if (*T_CWS)
4111 {
4112 /*
4113 * NOTE: if you get an error here that term_set_winsize() is
4114 * undefined, check the output of configure. It could probably not
4115 * find a ncurses, termcap or termlib library.
4116 */
4117 term_set_winsize((int)Rows, (int)Columns);
4118 out_flush();
4119 screen_start(); /* don't know where cursor is now */
4120 }
4121}
4122
4123#endif /* VMS */
4124
4125/*
4126 * Rows and/or Columns has changed.
4127 */
4128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004129mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
4131 /* Nothing to do. */
4132}
4133
Bram Moolenaar205b8862011-09-07 15:04:31 +02004134/*
4135 * Wait for process "child" to end.
4136 * Return "child" if it exited properly, <= 0 on error.
4137 */
4138 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004139wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004140{
4141 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004142 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004143
4144 while (wait_pid != child)
4145 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004146 /* When compiled with Python threads are probably used, in which case
4147 * wait() sometimes hangs for no obvious reason. Use waitpid()
4148 * instead and loop (like the GUI). Also needed for other interfaces,
4149 * they might call system(). */
4150# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004151 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004152# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004153 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004154# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004155 if (wait_pid == 0)
4156 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004157 /* Wait for 1 to 10 msec before trying again. */
4158 mch_delay(delay_msec, TRUE);
4159 if (++delay_msec > 10)
4160 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004161 continue;
4162 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004163 if (wait_pid <= 0
4164# ifdef ECHILD
4165 && errno == ECHILD
4166# endif
4167 )
4168 break;
4169 }
4170 return wait_pid;
4171}
4172
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004173#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004174/*
4175 * Set the environment for a child process.
4176 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004177 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004178set_child_environment(
4179 long rows,
4180 long columns,
4181 char *term,
4182 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004183{
4184# ifdef HAVE_SETENV
4185 char envbuf[50];
4186# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004187 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004188 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004189 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004190 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004191 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004192# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004193 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004194# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004195# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004196 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004197# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004198# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004199 long colors =
4200# ifdef FEAT_GUI
4201 gui.in_use ? 256*256*256 :
4202# endif
4203 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004204
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004205# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004206 setenv("TERM", term, 1);
4207 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004208 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004209 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004210 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004211 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004212 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004213 sprintf((char *)envbuf, "%ld", colors);
4214 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004215# ifdef FEAT_TERMINAL
4216 if (is_terminal)
4217 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004218 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004219 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4220 }
4221# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004222# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004223 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004224# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004225# else
4226 /*
4227 * Putenv does not copy the string, it has to remain valid.
4228 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004229 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004230 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004231 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4232 putenv(envbuf_Term);
4233 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004234 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004235 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4236 putenv(envbuf_Lines);
4237 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4238 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004239 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004240 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4241 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004242# ifdef FEAT_TERMINAL
4243 if (is_terminal)
4244 {
4245 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004246 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004247 putenv(envbuf_Version);
4248 }
4249# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004250# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004251 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4252 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4253 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004254# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004255# endif
4256}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004257
4258 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004259set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004260{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004261 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004262}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004263#endif
4264
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004265#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004266/*
4267 * Open a PTY, with FD for the master and slave side.
4268 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4269 * When successful both file descriptors are stored.
4270 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004271 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004272open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004273{
4274 char *tty_name;
4275
4276 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4277 if (*pty_master_fd >= 0)
4278 {
4279 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4280 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4281 * adding O_NOCTTY always works when defined. */
4282#ifdef O_NOCTTY
4283 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4284#else
4285 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4286#endif
4287 if (*pty_slave_fd < 0)
4288 {
4289 close(*pty_master_fd);
4290 *pty_master_fd = -1;
4291 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004292 else if (namep != NULL)
4293 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004294 }
4295}
4296#endif
4297
Bram Moolenaarfae42832017-08-01 22:24:26 +02004298/*
4299 * Send SIGINT to a child process if "c" is an interrupt character.
4300 */
4301 void
4302may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4303{
4304# ifdef SIGINT
4305 if (c == Ctrl_C || c == intr_char)
4306 {
4307# ifdef HAVE_SETSID
4308 kill(-pid, SIGINT);
4309# else
4310 kill(0, SIGINT);
4311# endif
4312 if (wpid > 0)
4313 kill(wpid, SIGINT);
4314 }
4315# endif
4316}
4317
Bram Moolenaar13568252018-03-16 20:46:58 +01004318#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4319
4320 static int
4321build_argv(
4322 char_u *cmd,
4323 char ***argvp,
4324 char_u **sh_tofree,
4325 char_u **shcf_tofree)
4326{
4327 char **argv = NULL;
4328 int argc;
4329
4330 *sh_tofree = vim_strsave(p_sh);
4331 if (*sh_tofree == NULL) /* out of memory */
4332 return FAIL;
4333
4334 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4335 return FAIL;
4336 *argvp = argv;
4337
4338 if (cmd != NULL)
4339 {
4340 char_u *s;
4341 char_u *p;
4342
4343 if (extra_shell_arg != NULL)
4344 argv[argc++] = (char *)extra_shell_arg;
4345
4346 /* Break 'shellcmdflag' into white separated parts. This doesn't
4347 * handle quoted strings, they are very unlikely to appear. */
4348 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4349 if (*shcf_tofree == NULL) /* out of memory */
4350 return FAIL;
4351 s = *shcf_tofree;
4352 p = p_shcf;
4353 while (*p != NUL)
4354 {
4355 argv[argc++] = (char *)s;
4356 while (*p && *p != ' ' && *p != TAB)
4357 *s++ = *p++;
4358 *s++ = NUL;
4359 p = skipwhite(p);
4360 }
4361
4362 argv[argc++] = (char *)cmd;
4363 }
4364 argv[argc] = NULL;
4365 return OK;
4366}
4367#endif
4368
4369#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4370/*
4371 * Use a terminal window to run a shell command in.
4372 */
4373 static int
4374mch_call_shell_terminal(
4375 char_u *cmd,
4376 int options UNUSED) /* SHELL_*, see vim.h */
4377{
4378 jobopt_T opt;
4379 char **argv = NULL;
4380 char_u *tofree1 = NULL;
4381 char_u *tofree2 = NULL;
4382 int retval = -1;
4383 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004384 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004385 aco_save_T aco;
4386 oparg_T oa; /* operator arguments */
4387
4388 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4389 goto theend;
4390
4391 init_job_options(&opt);
4392 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4393 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004394 if (buf == NULL)
4395 goto theend;
4396
4397 job = term_getjob(buf->b_term);
4398 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004399
4400 /* Find a window to make "buf" curbuf. */
4401 aucmd_prepbuf(&aco, buf);
4402
4403 clear_oparg(&oa);
4404 while (term_use_loop())
4405 {
4406 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4407 {
4408 /* If terminal_loop() returns OK we got a key that is handled
4409 * in Normal model. We don't do redrawing anyway. */
4410 if (terminal_loop(TRUE) == OK)
4411 normal_cmd(&oa, TRUE);
4412 }
4413 else
4414 normal_cmd(&oa, TRUE);
4415 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004416 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004417 ch_log(NULL, "system command finished");
4418
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004419 job_unref(job);
4420
Bram Moolenaar13568252018-03-16 20:46:58 +01004421 /* restore curwin/curbuf and a few other things */
4422 aucmd_restbuf(&aco);
4423
4424 wait_return(TRUE);
4425 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4426
4427theend:
4428 vim_free(argv);
4429 vim_free(tofree1);
4430 vim_free(tofree2);
4431 return retval;
4432}
4433#endif
4434
4435#ifdef USE_SYSTEM
4436/*
4437 * Use system() to start the shell: simple but slow.
4438 */
4439 static int
4440mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004441 char_u *cmd,
4442 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444#ifdef VMS
4445 char *ifn = NULL;
4446 char *ofn = NULL;
4447#endif
4448 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004449 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004450 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 out_flush();
4453
4454 if (options & SHELL_COOKED)
4455 settmode(TMODE_COOK); /* set to normal mode */
4456
Bram Moolenaar62b42182010-09-21 22:09:37 +02004457# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004458 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004459 loose_clipboard();
4460# endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (cmd == NULL)
4463 x = system((char *)p_sh);
4464 else
4465 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004466# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (ofn = strchr((char *)cmd, '>'))
4468 *ofn++ = '\0';
4469 if (ifn = strchr((char *)cmd, '<'))
4470 {
4471 char *p;
4472
4473 *ifn++ = '\0';
4474 p = strchr(ifn,' '); /* chop off any trailing spaces */
4475 if (p)
4476 *p = '\0';
4477 }
4478 if (ofn)
4479 x = vms_sys((char *)cmd, ofn, ifn);
4480 else
4481 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004482# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 newcmd = lalloc(STRLEN(p_sh)
4484 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4485 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4486 if (newcmd == NULL)
4487 x = 0;
4488 else
4489 {
4490 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4491 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4492 (char *)p_shcf,
4493 (char *)cmd);
4494 x = system((char *)newcmd);
4495 vim_free(newcmd);
4496 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499# ifdef VMS
4500 x = vms_sys_status(x);
4501# endif
4502 if (emsg_silent)
4503 ;
4504 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004505 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 else if (x && !(options & SHELL_SILENT))
4507 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004508 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 msg_outnum((long)x);
4510 msg_putchar('\n');
4511 }
4512
4513 if (tmode == TMODE_RAW)
4514 settmode(TMODE_RAW); /* set to raw mode */
4515# ifdef FEAT_TITLE
4516 resettitle();
4517# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004518# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4519 restore_clipboard();
4520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004522}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
Bram Moolenaar13568252018-03-16 20:46:58 +01004524#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaardf177f62005-02-22 08:39:57 +00004526# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4527 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004528# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaar13568252018-03-16 20:46:58 +01004530/*
4531 * Don't use system(), use fork()/exec().
4532 */
4533 static int
4534mch_call_shell_fork(
4535 char_u *cmd,
4536 int options) /* SHELL_*, see vim.h */
4537{
4538 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004540 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 pid_t wait_pid = 0;
4542# ifdef HAVE_UNION_WAIT
4543 union wait status;
4544# else
4545 int status = -1;
4546# endif
4547 int retval = -1;
4548 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004549 char_u *tofree1 = NULL;
4550 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004553# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004555# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004556 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 int fd_fromshell[2];
4558 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004559 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 out_flush();
4562 if (options & SHELL_COOKED)
4563 settmode(TMODE_COOK); /* set to normal mode */
4564
Bram Moolenaar13568252018-03-16 20:46:58 +01004565 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004566 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569 * For the GUI, when writing the output into the buffer and when reading
4570 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4571 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004573 if ((options & (SHELL_READ|SHELL_WRITE))
4574# ifdef FEAT_GUI
4575 || (gui.in_use && show_shell_mess)
4576# endif
4577 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 /*
4581 * Try to open a master pty.
4582 * If this works, open the slave pty.
4583 * If the slave can't be opened, close the master pty.
4584 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004585 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004586 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 /*
4588 * If not opening a pty or it didn't work, try using pipes.
4589 */
4590 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
4593 pipe_error = (pipe(fd_toshell) < 0);
4594 if (!pipe_error) /* pipe create OK */
4595 {
4596 pipe_error = (pipe(fd_fromshell) < 0);
4597 if (pipe_error) /* pipe create failed */
4598 {
4599 close(fd_toshell[0]);
4600 close(fd_toshell[1]);
4601 }
4602 }
4603 if (pipe_error)
4604 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004605 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 out_flush();
4607 }
4608 }
4609 }
4610
4611 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004613 SIGSET_DECL(curset)
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615# ifdef __BEOS__
4616 beos_cleanup_read_thread();
4617# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004618
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004619 BLOCK_SIGNALS(&curset);
4620 pid = fork(); /* maybe we should use vfork() */
4621 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004623 UNBLOCK_SIGNALS(&curset);
4624
Bram Moolenaar32526b32019-01-19 17:43:09 +01004625 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004626 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004628 || (gui.in_use && show_shell_mess)
4629# endif
4630 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004632# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (pty_master_fd >= 0) /* close the pseudo tty */
4634 {
4635 close(pty_master_fd);
4636 close(pty_slave_fd);
4637 }
4638 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 close(fd_toshell[0]);
4642 close(fd_toshell[1]);
4643 close(fd_fromshell[0]);
4644 close(fd_fromshell[1]);
4645 }
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 else if (pid == 0) /* child */
4649 {
4650 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004651 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
Bram Moolenaar819524702018-02-27 19:10:00 +01004653# ifdef FEAT_JOB_CHANNEL
4654 if (ch_log_active())
4655 /* close the log file in the child */
4656 ch_logfile((char_u *)"", (char_u *)"");
4657# endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (!show_shell_mess || (options & SHELL_EXPAND))
4660 {
4661 int fd;
4662
4663 /*
4664 * Don't want to show any message from the shell. Can't just
4665 * close stdout and stderr though, because some systems will
4666 * break if you try to write to them after that, so we must
4667 * use dup() to replace them with something else -- webb
4668 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4669 * waiting for input.
4670 */
4671 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4672 fclose(stdin);
4673 fclose(stdout);
4674 fclose(stderr);
4675
4676 /*
4677 * If any of these open()'s and dup()'s fail, we just continue
4678 * anyway. It's not fatal, and on most systems it will make
4679 * no difference at all. On a few it will cause the execvp()
4680 * to exit with a non-zero status even when the completion
4681 * could be done, which is nothing too serious. If the open()
4682 * or dup() failed we'd just do the same thing ourselves
4683 * anyway -- webb
4684 */
4685 if (fd >= 0)
4686 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004687 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4688 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4689 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
4691 /* Don't need this now that we've duplicated it */
4692 close(fd);
4693 }
4694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004695 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004697 || gui.in_use
4698# endif
4699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701
Bram Moolenaardf177f62005-02-22 08:39:57 +00004702# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004703 /* Create our own process group, so that the child and all its
4704 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004705 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004706 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004707 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004708 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004709# if defined(SIGHUP)
4710 /* When doing "!xterm&" and 'shell' is bash: the shell
4711 * will exit and send SIGHUP to all processes in its
4712 * group, killing the just started process. Ignore SIGHUP
4713 * to avoid that. (suggested by Simon Schubert)
4714 */
4715 signal(SIGHUP, SIG_IGN);
4716# endif
4717 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004718# endif
4719# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004720 if (pty_slave_fd >= 0)
4721 {
4722 /* push stream discipline modules */
4723 if (options & SHELL_COOKED)
4724 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004726 /* Try to become controlling tty (probably doesn't work,
4727 * unless run by root) */
4728 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004730 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004731# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004732 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733
Bram Moolenaara5792f52005-11-23 21:25:05 +00004734 /*
4735 * stderr is only redirected when using the GUI, so that a
4736 * program like gpg can still access the terminal to get a
4737 * passphrase using stderr.
4738 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004739# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (pty_master_fd >= 0)
4741 {
4742 close(pty_master_fd); /* close master side of pty */
4743
4744 /* set up stdin/stdout/stderr for the child */
4745 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004746 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004748 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004749 if (gui.in_use)
4750 {
4751 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004752 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 close(pty_slave_fd); /* has been dupped, close it now */
4756 }
4757 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
4760 /* set up stdin for the child */
4761 close(fd_toshell[1]);
4762 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004763 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 close(fd_toshell[0]);
4765
4766 /* set up stdout for the child */
4767 close(fd_fromshell[0]);
4768 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004769 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 close(fd_fromshell[1]);
4771
Bram Moolenaara5792f52005-11-23 21:25:05 +00004772# ifdef FEAT_GUI
4773 if (gui.in_use)
4774 {
4775 /* set up stderr for the child */
4776 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004777 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004778 }
4779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 /*
4784 * There is no type cast for the argv, because the type may be
4785 * different on different machines. This may cause a warning
4786 * message with strict compilers, don't worry about it.
4787 * Call _exit() instead of exit() to avoid closing the connection
4788 * to the X server (esp. with GTK, which uses atexit()).
4789 */
4790 execvp(argv[0], argv);
4791 _exit(EXEC_FAILED); /* exec failed, return failure code */
4792 }
4793 else /* parent */
4794 {
4795 /*
4796 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004797 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 */
4799 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004801 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004802# ifdef FEAT_JOB_CHANNEL
4803 ++dont_check_job_ended;
4804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 /*
4806 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807 * This is also used to pipe stdin/stdout to/from the external
4808 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004810 if ((options & (SHELL_READ|SHELL_WRITE))
4811# ifdef FEAT_GUI
4812 || (gui.in_use && show_shell_mess)
4813# endif
4814 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004816# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 char_u buffer[BUFLEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4820 int ta_len = 0; /* valid bytes in ta_buf[] */
4821 int len;
4822 int p_more_save;
4823 int old_State;
4824 int c;
4825 int toshell_fd;
4826 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004827 garray_T ga;
4828 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004829# ifdef ELAPSED_FUNC
4830 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
Bram Moolenaardf177f62005-02-22 08:39:57 +00004833# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if (pty_master_fd >= 0)
4835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 fromshell_fd = pty_master_fd;
4837 toshell_fd = dup(pty_master_fd);
4838 }
4839 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 close(fd_toshell[0]);
4843 close(fd_fromshell[1]);
4844 toshell_fd = fd_toshell[1];
4845 fromshell_fd = fd_fromshell[0];
4846 }
4847
4848 /*
4849 * Write to the child if there are typed characters.
4850 * Read from the child if there are characters available.
4851 * Repeat the reading a few times if more characters are
4852 * available. Need to check for typed keys now and then, but
4853 * not too often (delays when no chars are available).
4854 * This loop is quit if no characters can be read from the pty
4855 * (WaitForChar detected special condition), or there are no
4856 * characters available and the child has exited.
4857 * Only check if the child has exited when there is no more
4858 * output. The child may exit before all the output has
4859 * been printed.
4860 *
4861 * Currently this busy loops!
4862 * This can probably dead-lock when the write blocks!
4863 */
4864 p_more_save = p_more;
4865 p_more = FALSE;
4866 old_State = State;
4867 State = EXTERNCMD; /* don't redraw at window resize */
4868
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004869 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004870 {
4871 /* Fork a process that will write the lines to the
4872 * external program. */
4873 if ((wpid = fork()) == -1)
4874 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004875 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004876 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004877 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004878 {
4879 linenr_T lnum = curbuf->b_op_start.lnum;
4880 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004881 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004882 size_t l;
4883
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004884 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 for (;;)
4886 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004887 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004888 if (l == 0)
4889 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004890 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 /* NL -> NUL translation */
4892 len = write(toshell_fd, "", (size_t)1);
4893 else
4894 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004895 char_u *s = vim_strchr(lp + written, NL);
4896
Bram Moolenaar89d40322006-08-29 15:30:07 +00004897 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004898 s == NULL ? l
4899 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004900 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004901 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004902 {
4903 /* Finished a line, add a NL, unless this line
4904 * should not have one. */
4905 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004906 || (!curbuf->b_p_bin
4907 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004908 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 && (lnum !=
4910 curbuf->b_ml.ml_line_count
4911 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004912 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004913 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 ++lnum;
4915 if (lnum > curbuf->b_op_end.lnum)
4916 {
4917 /* finished all the lines, close pipe */
4918 close(toshell_fd);
4919 toshell_fd = -1;
4920 break;
4921 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004922 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 written = 0;
4924 }
4925 else if (len > 0)
4926 written += len;
4927 }
4928 _exit(0);
4929 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004930 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004931 {
4932 close(toshell_fd);
4933 toshell_fd = -1;
4934 }
4935 }
4936
4937 if (options & SHELL_READ)
4938 ga_init2(&ga, 1, BUFLEN);
4939
4940 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004941# ifdef ELAPSED_FUNC
4942 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 for (;;)
4945 {
4946 /*
4947 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004948 * if there are any.
4949 * Don't do this if we are expanding wild cards (would eat
4950 * typeahead).
4951 * Don't do this when filtering and terminal is in cooked
4952 * mode, the shell command will handle the I/O. Avoids
4953 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004954 * Don't get characters when the child has already
4955 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004956 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004957 * while (noread_cnt > 4), avoids that ":r !ls" eats
4958 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
4960 len = 0;
4961 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004962 && ((options &
4963 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4964 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004965# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004966 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004967# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004968 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004969 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004970 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004972 if (ta_len == 0)
4973 {
4974 /* Get extra characters when we don't have any.
4975 * Reset the counter and timer. */
4976 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004977# ifdef ELAPSED_FUNC
4978 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004979# endif
4980 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4981 }
4982 if (ta_len > 0 || len > 0)
4983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 /*
4985 * For pipes:
4986 * Check for CTRL-C: send interrupt signal to child.
4987 * Check for CTRL-D: EOF, close pipe to child.
4988 */
4989 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /*
4992 * Send SIGINT to the child's group or all
4993 * processes in our group.
4994 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004995 may_send_sigint(ta_buf[ta_len], pid, wpid);
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (pty_master_fd < 0 && toshell_fd >= 0
4998 && ta_buf[ta_len] == Ctrl_D)
4999 {
5000 close(toshell_fd);
5001 toshell_fd = -1;
5002 }
5003 }
5004
5005 /* replace K_BS by <BS> and K_DEL by <DEL> */
5006 for (i = ta_len; i < ta_len + len; ++i)
5007 {
5008 if (ta_buf[i] == CSI && len - i > 2)
5009 {
5010 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5011 if (c == K_DEL || c == K_KDEL || c == K_BS)
5012 {
5013 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5014 (size_t)(len - i - 2));
5015 if (c == K_DEL || c == K_KDEL)
5016 ta_buf[i] = DEL;
5017 else
5018 ta_buf[i] = Ctrl_H;
5019 len -= 2;
5020 }
5021 }
5022 else if (ta_buf[i] == '\r')
5023 ta_buf[i] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005025 i += (*mb_ptr2len_len)(ta_buf + i,
5026 ta_len + len - i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028
5029 /*
5030 * For pipes: echo the typed characters.
5031 * For a pty this does not seem to work.
5032 */
5033 if (pty_master_fd < 0)
5034 {
5035 for (i = ta_len; i < ta_len + len; ++i)
5036 {
5037 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5038 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 else if (has_mbyte)
5040 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005041 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 msg_outtrans_len(ta_buf + i, l);
5044 i += l - 1;
5045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 else
5047 msg_outtrans_len(ta_buf + i, 1);
5048 }
5049 windgoto(msg_row, msg_col);
5050 out_flush();
5051 }
5052
5053 ta_len += len;
5054
5055 /*
5056 * Write the characters to the child, unless EOF has
5057 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005058 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005059 * When writing buffer lines, drop the typed
5060 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005062 if (options & SHELL_WRITE)
5063 ta_len = 0;
5064 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5067 if (len > 0)
5068 {
5069 ta_len -= len;
5070 mch_memmove(ta_buf, ta_buf + len, ta_len);
5071 }
5072 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076 if (got_int)
5077 {
5078 /* CTRL-C sends a signal to the child, we ignore it
5079 * ourselves */
5080# ifdef HAVE_SETSID
5081 kill(-pid, SIGINT);
5082# else
5083 kill(0, SIGINT);
5084# endif
5085 if (wpid > 0)
5086 kill(wpid, SIGINT);
5087 got_int = FALSE;
5088 }
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 /*
5091 * Check if the child has any characters to be printed.
5092 * Read them and write them to our window. Repeat this as
5093 * long as there is something to do, avoid the 10ms wait
5094 * for mch_inchar(), or sending typeahead characters to
5095 * the external process.
5096 * TODO: This should handle escape sequences, compatible
5097 * to some terminal (vt52?).
5098 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005099 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005100 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005102 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 );
5105 if (len <= 0) /* end of file or error */
5106 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005107
5108 noread_cnt = 0;
5109 if (options & SHELL_READ)
5110 {
5111 /* Do NUL -> NL translation, append NL separated
5112 * lines to the current buffer. */
5113 for (i = 0; i < len; ++i)
5114 {
5115 if (buffer[i] == NL)
5116 append_ga_line(&ga);
5117 else if (buffer[i] == NUL)
5118 ga_append(&ga, NL);
5119 else
5120 ga_append(&ga, buffer[i]);
5121 }
5122 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005123 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005126 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
Bram Moolenaardf177f62005-02-22 08:39:57 +00005128 len += buffer_off;
5129 buffer[len] = NUL;
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 /* Check if the last character in buffer[] is
5132 * incomplete, keep these bytes for the next
5133 * round. */
5134 for (p = buffer; p < buffer + len; p += l)
5135 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005136 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (l == 0)
5138 l = 1; /* NUL byte? */
5139 else if (MB_BYTE2LEN(*p) != l)
5140 break;
5141 }
5142 if (p == buffer) /* no complete character */
5143 {
5144 /* avoid getting stuck at an illegal byte */
5145 if (len >= 12)
5146 ++p;
5147 else
5148 {
5149 buffer_off = len;
5150 continue;
5151 }
5152 }
5153 c = *p;
5154 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005155 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (p < buffer + len)
5157 {
5158 *p = c;
5159 buffer_off = (buffer + len) - p;
5160 mch_memmove(buffer, p, buffer_off);
5161 continue;
5162 }
5163 buffer_off = 0;
5164 }
5165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005168 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
5171 windgoto(msg_row, msg_col);
5172 cursor_on();
5173 out_flush();
5174 if (got_int)
5175 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005176
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005177# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005178 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005179 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005180 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005181
5182 /* Avoid that we keep looping here without
5183 * checking for a CTRL-C for a long time. Don't
5184 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005185 if (msec > 2000)
5186 {
5187 noread_cnt = 5;
5188 break;
5189 }
5190 }
5191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005194 /* If we already detected the child has finished, continue
5195 * reading output for a short while. Some text may be
5196 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005197 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005198 {
5199 if (noread_cnt < 5)
5200 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005201 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005202 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /*
5205 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005206 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005208# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005209 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005210# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5214 || (wait_pid == pid && WIFEXITED(status)))
5215 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005216 /* Don't break the loop yet, try reading more
5217 * characters from "fromshell_fd" first. When using
5218 * pipes there might still be something to read and
5219 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005222 else
5223 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005224
Bram Moolenaar95a51352013-03-21 22:53:50 +01005225# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005226 /* Handle any X events, e.g. serving the clipboard. */
5227 clip_update();
5228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230finished:
5231 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005232 if (options & SHELL_READ)
5233 {
5234 if (ga.ga_len > 0)
5235 {
5236 append_ga_line(&ga);
5237 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005238 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005239 }
5240 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005241 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005242 ga_clear(&ga);
5243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /*
5246 * Give all typeahead that wasn't used back to ui_inchar().
5247 */
5248 if (ta_len)
5249 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 State = old_State;
5251 if (toshell_fd >= 0)
5252 close(toshell_fd);
5253 close(fromshell_fd);
5254 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005255# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005256 else
5257 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005258 long delay_msec = 1;
5259
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005260 /*
5261 * Similar to the loop above, but only handle X events, no
5262 * I/O.
5263 */
5264 for (;;)
5265 {
5266 if (got_int)
5267 {
5268 /* CTRL-C sends a signal to the child, we ignore it
5269 * ourselves */
5270# ifdef HAVE_SETSID
5271 kill(-pid, SIGINT);
5272# else
5273 kill(0, SIGINT);
5274# endif
5275 got_int = FALSE;
5276 }
5277# ifdef __NeXT__
5278 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5279# else
5280 wait_pid = waitpid(pid, &status, WNOHANG);
5281# endif
5282 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5283 || (wait_pid == pid && WIFEXITED(status)))
5284 {
5285 wait_pid = pid;
5286 break;
5287 }
5288
5289 /* Handle any X events, e.g. serving the clipboard. */
5290 clip_update();
5291
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005292 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5293 * less time. */
5294 mch_delay(delay_msec, TRUE);
5295 if (++delay_msec > 10)
5296 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005297 }
5298 }
5299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Wait until our child has exited.
5303 * Ignore wait() returning pids of other children and returning
5304 * because of some signal like SIGWINCH.
5305 * Don't wait if wait_pid was already set above, indicating the
5306 * child already exited.
5307 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005308 if (wait_pid != pid)
5309 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
Bram Moolenaar624891f2010-10-13 16:22:09 +02005311# ifdef FEAT_GUI
5312 /* Close slave side of pty. Only do this after the child has
5313 * exited, otherwise the child may hang when it tries to write on
5314 * the pty. */
5315 if (pty_master_fd >= 0)
5316 close(pty_slave_fd);
5317# endif
5318
Bram Moolenaardf177f62005-02-22 08:39:57 +00005319 /* Make sure the child that writes to the external program is
5320 * dead. */
5321 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005322 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005323 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005324 wait4pid(wpid, NULL);
5325 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005326
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005327# ifdef FEAT_JOB_CHANNEL
5328 --dont_check_job_ended;
5329# endif
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /*
5332 * Set to raw mode right now, otherwise a CTRL-C after
5333 * catch_signals() will kill Vim.
5334 */
5335 if (tmode == TMODE_RAW)
5336 settmode(TMODE_RAW);
5337 did_settmode = TRUE;
5338 set_signals();
5339
5340 if (WIFEXITED(status))
5341 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005342 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005344 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
5346 if (retval == EXEC_FAILED)
5347 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005348 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 msg_outtrans(p_sh);
5350 msg_putchar('\n');
5351 }
5352 else if (!(options & SHELL_SILENT))
5353 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005354 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 msg_outnum((long)retval);
5356 msg_putchar('\n');
5357 }
5358 }
5359 }
5360 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005361 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365error:
5366 if (!did_settmode)
5367 if (tmode == TMODE_RAW)
5368 settmode(TMODE_RAW); /* set to raw mode */
5369# ifdef FEAT_TITLE
5370 resettitle();
5371# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005372 vim_free(argv);
5373 vim_free(tofree1);
5374 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005379
5380 int
5381mch_call_shell(
5382 char_u *cmd,
5383 int options) /* SHELL_*, see vim.h */
5384{
5385#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5386 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5387 return mch_call_shell_terminal(cmd, options);
5388#endif
5389#ifdef USE_SYSTEM
5390 return mch_call_shell_system(cmd, options);
5391#else
5392 return mch_call_shell_fork(cmd, options);
5393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
5395
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005396#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005397 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005398mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005399{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005400 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005401 int fd_in[2] = {-1, -1}; /* for stdin */
5402 int fd_out[2] = {-1, -1}; /* for stdout */
5403 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005404 int pty_master_fd = -1;
5405 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005406 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005407 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5408 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5409 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005410 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005411 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5412 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005413 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005414 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005415 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005416
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005417 if (use_out_for_err && use_null_for_out)
5418 use_null_for_err = TRUE;
5419
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005420 /* default is to fail */
5421 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005422
Bram Moolenaarb2412082017-08-20 18:09:14 +02005423 if (options->jo_pty
5424 && (!(use_file_for_in || use_null_for_in)
5425 || !(use_file_for_in || use_null_for_out)
5426 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005427 {
5428 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5429 if (job->jv_tty_out != NULL)
5430 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5431 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005432
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005433 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005434 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005435 if (use_file_for_in)
5436 {
5437 char_u *fname = options->jo_io_name[PART_IN];
5438
5439 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5440 if (fd_in[0] < 0)
5441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005442 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005443 goto failed;
5444 }
5445 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005446 else
5447 /* When writing buffer lines to the input don't use the pty, so that
5448 * the pipe can be closed when all lines were written. */
5449 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5450 && pipe(fd_in) < 0)
5451 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005452
5453 if (use_file_for_out)
5454 {
5455 char_u *fname = options->jo_io_name[PART_OUT];
5456
5457 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5458 if (fd_out[1] < 0)
5459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005460 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005461 goto failed;
5462 }
5463 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005464 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005465 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005466
5467 if (use_file_for_err)
5468 {
5469 char_u *fname = options->jo_io_name[PART_ERR];
5470
5471 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5472 if (fd_err[1] < 0)
5473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005474 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005475 goto failed;
5476 }
5477 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005478 else if (!use_out_for_err && !use_null_for_err
5479 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005480 goto failed;
5481
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005482 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5483 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005484 if (options->jo_set & JO_CHANNEL)
5485 {
5486 channel = options->jo_channel;
5487 if (channel != NULL)
5488 ++channel->ch_refcount;
5489 }
5490 else
5491 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005492 if (channel == NULL)
5493 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005494 if (job->jv_tty_out != NULL)
5495 ch_log(channel, "using pty %s on fd %d",
5496 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005497 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005498
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005499 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005500 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005501 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005502 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005503 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005504 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005505 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005507 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005509 int null_fd = -1;
5510 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005511
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512 /* child */
5513 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005514 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005515
Bram Moolenaar819524702018-02-27 19:10:00 +01005516# ifdef FEAT_JOB_CHANNEL
5517 if (ch_log_active())
5518 /* close the log file in the child */
5519 ch_logfile((char_u *)"", (char_u *)"");
5520# endif
5521
Bram Moolenaar835dc632016-02-07 14:27:38 +01005522# ifdef HAVE_SETSID
5523 /* Create our own process group, so that the child and all its
5524 * children can be kill()ed. Don't do this when using pipes,
5525 * because stdin is not a tty, we would lose /dev/tty. */
5526 (void)setsid();
5527# endif
5528
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005529# ifdef FEAT_TERMINAL
5530 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005531 {
5532 char *term = (char *)T_NAME;
5533
5534#ifdef FEAT_GUI
5535 if (term_is_gui(T_NAME))
5536 /* In the GUI 'term' is not what we want, use $TERM. */
5537 term = getenv("TERM");
5538#endif
5539 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5540 * back to "xterm". */
5541 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5542 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005543 set_child_environment(
5544 (long)options->jo_term_rows,
5545 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005546 term,
5547 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005548 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005549 else
5550# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005551 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005552
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005553 if (options->jo_env != NULL)
5554 {
5555 dict_T *dict = options->jo_env;
5556 hashitem_T *hi;
5557 int todo = (int)dict->dv_hashtab.ht_used;
5558
5559 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5560 if (!HASHITEM_EMPTY(hi))
5561 {
5562 typval_T *item = &dict_lookup(hi)->di_tv;
5563
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005564 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005565 --todo;
5566 }
5567 }
5568
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005569 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005570 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005571 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005572 if (null_fd < 0)
5573 {
5574 perror("opening /dev/null failed");
5575 _exit(OPEN_NULL_FAILED);
5576 }
5577 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005578
Bram Moolenaar223896d2017-08-02 22:33:28 +02005579 if (pty_slave_fd >= 0)
5580 {
5581 /* push stream discipline modules */
5582 SetupSlavePTY(pty_slave_fd);
5583# ifdef TIOCSCTTY
5584 /* Try to become controlling tty (probably doesn't work,
5585 * unless run by root) */
5586 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5587# endif
5588 }
5589
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005590 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005591 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005592 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005593 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005594 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005595 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005596 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005597 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005598
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005599 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005600 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005601 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005602 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005603 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005604 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 }
5606 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005607 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005608 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005609 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005610 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005611 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005612
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005613 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005614 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005615 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005616 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005617 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005618 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005619 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005620 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005621
5622 if (fd_in[0] >= 0)
5623 close(fd_in[0]);
5624 if (fd_in[1] >= 0)
5625 close(fd_in[1]);
5626 if (fd_out[0] >= 0)
5627 close(fd_out[0]);
5628 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005629 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005630 if (fd_err[0] >= 0)
5631 close(fd_err[0]);
5632 if (fd_err[1] >= 0)
5633 close(fd_err[1]);
5634 if (pty_master_fd >= 0)
5635 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005636 close(pty_master_fd); /* not used in the child */
5637 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005638 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005639
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005640 if (null_fd >= 0)
5641 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005642
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005643 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5644 _exit(EXEC_FAILED);
5645
Bram Moolenaar835dc632016-02-07 14:27:38 +01005646 /* See above for type of argv. */
5647 execvp(argv[0], argv);
5648
Bram Moolenaar4694a172016-04-21 14:05:23 +02005649 if (stderr_works)
5650 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005651# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005652 /* calling free_all_mem() here causes problems. Ignore valgrind
5653 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005654# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005655 _exit(EXEC_FAILED); /* exec failed, return failure code */
5656 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005657
5658 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005659 UNBLOCK_SIGNALS(&curset);
5660
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005661 job->jv_pid = pid;
5662 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005663 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005664
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005665 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005666 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005667 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005668 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005669 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005670 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005671 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005672 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005673 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005674 if (channel != NULL)
5675 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005676 int in_fd = use_file_for_in || use_null_for_in
5677 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5678 int out_fd = use_file_for_out || use_null_for_out
5679 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005680 /* When using pty_master_fd only set it for stdout, do not duplicate it
5681 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005682 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005683 ? INVALID_FD
5684 : fd_err[0] >= 0
5685 ? fd_err[0]
5686 : (out_fd == pty_master_fd
5687 ? INVALID_FD
5688 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005689
5690 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005691 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005692 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005693 else
5694 {
5695 if (fd_in[1] >= 0)
5696 close(fd_in[1]);
5697 if (fd_out[0] >= 0)
5698 close(fd_out[0]);
5699 if (fd_err[0] >= 0)
5700 close(fd_err[0]);
5701 if (pty_master_fd >= 0)
5702 close(pty_master_fd);
5703 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005705 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706 return;
5707
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005708failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005709 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005710 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005711 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005712 if (fd_in[1] >= 0)
5713 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005714 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005716 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005717 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005718 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005720 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005722 if (pty_master_fd >= 0)
5723 close(pty_master_fd);
5724 if (pty_slave_fd >= 0)
5725 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005726}
5727
5728 char *
5729mch_job_status(job_T *job)
5730{
5731# ifdef HAVE_UNION_WAIT
5732 union wait status;
5733# else
5734 int status = -1;
5735# endif
5736 pid_t wait_pid = 0;
5737
5738# ifdef __NeXT__
5739 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5740# else
5741 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5742# endif
5743 if (wait_pid == -1)
5744 {
5745 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005746 if (job->jv_status < JOB_ENDED)
5747 ch_log(job->jv_channel, "Job no longer exists: %s",
5748 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005749 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005750 }
5751 if (wait_pid == 0)
5752 return "run";
5753 if (WIFEXITED(status))
5754 {
5755 /* LINTED avoid "bitwise operation on signed value" */
5756 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005757 if (job->jv_status < JOB_ENDED)
5758 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005759 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005760 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005761 if (WIFSIGNALED(status))
5762 {
5763 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005764 if (job->jv_status < JOB_ENDED)
5765 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005766 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005767 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005769
5770return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005771 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005772 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005773 return "dead";
5774}
5775
5776 job_T *
5777mch_detect_ended_job(job_T *job_list)
5778{
5779# ifdef HAVE_UNION_WAIT
5780 union wait status;
5781# else
5782 int status = -1;
5783# endif
5784 pid_t wait_pid = 0;
5785 job_T *job;
5786
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005787# ifndef USE_SYSTEM
5788 /* Do not do this when waiting for a shell command to finish, we would get
5789 * the exit value here (and discard it), the exit value obtained there
5790 * would then be wrong. */
5791 if (dont_check_job_ended > 0)
5792 return NULL;
5793# endif
5794
Bram Moolenaar97792de2016-10-15 18:36:49 +02005795# ifdef __NeXT__
5796 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5797# else
5798 wait_pid = waitpid(-1, &status, WNOHANG);
5799# endif
5800 if (wait_pid <= 0)
5801 /* no process ended */
5802 return NULL;
5803 for (job = job_list; job != NULL; job = job->jv_next)
5804 {
5805 if (job->jv_pid == wait_pid)
5806 {
5807 if (WIFEXITED(status))
5808 /* LINTED avoid "bitwise operation on signed value" */
5809 job->jv_exitval = WEXITSTATUS(status);
5810 else if (WIFSIGNALED(status))
5811 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005812 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005813 {
5814 ch_log(job->jv_channel, "Job ended");
5815 job->jv_status = JOB_ENDED;
5816 }
5817 return job;
5818 }
5819 }
5820 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005821}
5822
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005823/*
5824 * Send a (deadly) signal to "job".
5825 * Return FAIL if "how" is not a valid name.
5826 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005827 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005828mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005829{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005830 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005831
Bram Moolenaar923d9262016-02-25 20:56:01 +01005832 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005833 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005834 else if (STRCMP(how, "hup") == 0)
5835 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005836 else if (STRCMP(how, "quit") == 0)
5837 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005838 else if (STRCMP(how, "int") == 0)
5839 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840 else if (STRCMP(how, "kill") == 0)
5841 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005842#ifdef SIGWINCH
5843 else if (STRCMP(how, "winch") == 0)
5844 sig = SIGWINCH;
5845#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846 else if (isdigit(*how))
5847 sig = atoi((char *)how);
5848 else
5849 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005850
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005851 // Never kill ourselves!
5852 if (job->jv_pid != 0)
5853 {
5854 // TODO: have an option to only kill the process, not the group?
5855 kill(-job->jv_pid, sig);
5856 kill(job->jv_pid, sig);
5857 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005858
Bram Moolenaar835dc632016-02-07 14:27:38 +01005859 return OK;
5860}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005861
5862/*
5863 * Clear the data related to "job".
5864 */
5865 void
5866mch_clear_job(job_T *job)
5867{
5868 /* call waitpid because child process may become zombie */
5869# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005870 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005871# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005872 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005873# endif
5874}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875#endif
5876
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005877#if defined(FEAT_TERMINAL) || defined(PROTO)
5878 int
5879mch_create_pty_channel(job_T *job, jobopt_T *options)
5880{
5881 int pty_master_fd = -1;
5882 int pty_slave_fd = -1;
5883 channel_T *channel;
5884
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005885 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5886 if (job->jv_tty_out != NULL)
5887 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005888 close(pty_slave_fd);
5889
5890 channel = add_channel();
5891 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005892 {
5893 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005894 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005895 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005896 if (job->jv_tty_out != NULL)
5897 ch_log(channel, "using pty %s on fd %d",
5898 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005899 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5900 channel->ch_keep_open = TRUE;
5901
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005902 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5903 * it only needs to be read once. */
5904 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005905 channel_set_job(channel, job, options);
5906 return OK;
5907}
5908#endif
5909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910/*
5911 * Check for CTRL-C typed by reading all available characters.
5912 * In cooked mode we should get SIGINT, no need to check.
5913 */
5914 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005915mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005917 if ((curr_tmode == TMODE_RAW || force)
5918 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 fill_input_buf(FALSE);
5920}
5921
5922/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005923 * Wait "msec" msec until a character is available from the mouse, keyboard,
5924 * from inbuf[].
5925 * "msec" == -1 will block forever.
5926 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005927 * When "ignore_input" is TRUE even check for pending input when input is
5928 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005929 * "interrupted" (if not NULL) is set to TRUE when no character is available
5930 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005931 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005932 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 */
5934 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005935WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005937#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005938 return ui_wait_for_chars_or_timer(
5939 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005940#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005941 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005942#endif
5943}
5944
5945/*
5946 * Wait "msec" msec until a character is available from the mouse or keyboard
5947 * or from inbuf[].
5948 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005949 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005950 * "interrupted" (if not NULL) is set to TRUE when no character is available
5951 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005952 * When a GUI is being used, this will never get called -- webb
5953 */
5954 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005955WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005956{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#ifdef FEAT_MOUSE_GPM
5958 int gpm_process_wanted;
5959#endif
5960#ifdef FEAT_XCLIPBOARD
5961 int rest;
5962#endif
5963 int avail;
5964
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005965 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 return 1;
5967
5968#if defined(FEAT_MOUSE_DEC)
5969 /* May need to query the mouse position. */
5970 if (WantQueryMouse)
5971 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005972 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5974 }
5975#endif
5976
5977 /*
5978 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5979 * events. This is a bit complicated, because they might both be defined.
5980 */
5981#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5982# ifdef FEAT_XCLIPBOARD
5983 rest = 0;
5984 if (do_xterm_trace())
5985 rest = msec;
5986# endif
5987 do
5988 {
5989# ifdef FEAT_XCLIPBOARD
5990 if (rest != 0)
5991 {
5992 msec = XT_TRACE_DELAY;
5993 if (rest >= 0 && rest < XT_TRACE_DELAY)
5994 msec = rest;
5995 if (rest >= 0)
5996 rest -= msec;
5997 }
5998# endif
5999# ifdef FEAT_MOUSE_GPM
6000 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006001 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006002 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006004 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005# endif
6006 if (!avail)
6007 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006008 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 return 1;
6010# ifdef FEAT_XCLIPBOARD
6011 if (rest == 0 || !do_xterm_trace())
6012# endif
6013 break;
6014 }
6015 }
6016 while (FALSE
6017# ifdef FEAT_MOUSE_GPM
6018 || (gpm_process_wanted && mch_gpm_process() == 0)
6019# endif
6020# ifdef FEAT_XCLIPBOARD
6021 || (!avail && rest != 0)
6022# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006023 )
6024 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025
6026#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006027 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028#endif
6029 return avail;
6030}
6031
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006032#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033/*
6034 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006035 * "msec" == 0 will check for characters once.
6036 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006039 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006040 * "interrupted" (if not NULL) is set to TRUE when no character is available
6041 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#if defined(__BEOS__)
6044 int
6045#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006046 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006048RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006051 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006052#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 static int busy = FALSE;
6054
6055 /* May retry getting characters after an event was handled. */
6056# define MAY_LOOP
6057
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006058# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 /* Remember at what time we started, so that we know how much longer we
6060 * should wait after being interrupted. */
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006061 long start_msec = msec;
6062 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006064 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006065 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066# endif
6067
6068 /* Handle being called recursively. This may happen for the session
6069 * manager stuff, it may save the file, which does a breakcheck. */
6070 if (busy)
6071 return 0;
6072#endif
6073
6074#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006075 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#endif
6077 {
6078#ifdef MAY_LOOP
6079 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006080# ifdef FEAT_MZSCHEME
6081 int mzquantum_used = FALSE;
6082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#endif
6084#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006085 /* each channel may use in, out and err */
6086 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 int nfd;
6088# ifdef FEAT_XCLIPBOARD
6089 int xterm_idx = -1;
6090# endif
6091# ifdef FEAT_MOUSE_GPM
6092 int gpm_idx = -1;
6093# endif
6094# ifdef USE_XSMP
6095 int xsmp_idx = -1;
6096# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006097 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006099# ifdef FEAT_MZSCHEME
6100 mzvim_check_threads();
6101 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6102 {
6103 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6104 mzquantum_used = TRUE;
6105 }
6106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 fds[0].fd = fd;
6108 fds[0].events = POLLIN;
6109 nfd = 1;
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006112 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (xterm_Shell != (Widget)0)
6114 {
6115 xterm_idx = nfd;
6116 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6117 fds[nfd].events = POLLIN;
6118 nfd++;
6119 }
6120# endif
6121# ifdef FEAT_MOUSE_GPM
6122 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6123 {
6124 gpm_idx = nfd;
6125 fds[nfd].fd = gpm_fd;
6126 fds[nfd].events = POLLIN;
6127 nfd++;
6128 }
6129# endif
6130# ifdef USE_XSMP
6131 if (xsmp_icefd != -1)
6132 {
6133 xsmp_idx = nfd;
6134 fds[nfd].fd = xsmp_icefd;
6135 fds[nfd].events = POLLIN;
6136 nfd++;
6137 }
6138# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006140 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006141#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006142 if (interrupted != NULL)
6143 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006145 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006146
6147 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006148 if (result == 0 && interrupted != NULL && ret > 0)
6149 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006150
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006151# ifdef FEAT_MZSCHEME
6152 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006153 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006154 finished = FALSE;
6155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157# ifdef FEAT_XCLIPBOARD
6158 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6159 {
6160 xterm_update(); /* Maybe we should hand out clipboard */
6161 if (--ret == 0 && !input_available())
6162 /* Try again */
6163 finished = FALSE;
6164 }
6165# endif
6166# ifdef FEAT_MOUSE_GPM
6167 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6168 {
6169 *check_for_gpm = 1;
6170 }
6171# endif
6172# ifdef USE_XSMP
6173 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6174 {
6175 if (fds[xsmp_idx].revents & POLLIN)
6176 {
6177 busy = TRUE;
6178 xsmp_handle_requests();
6179 busy = FALSE;
6180 }
6181 else if (fds[xsmp_idx].revents & POLLHUP)
6182 {
6183 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006184 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 xsmp_close();
6186 }
6187 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006188 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006191#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006192 /* also call when ret == 0, we may be polling a keep-open channel */
6193 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006194 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197#else /* HAVE_SELECT */
6198
6199 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006200 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006201 // These are static because they can take 8 Kbyte each and cause the
6202 // signal stack to run out with -O3.
6203 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006205 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006207# ifdef FEAT_MZSCHEME
6208 mzvim_check_threads();
6209 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6210 {
6211 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6212 mzquantum_used = TRUE;
6213 }
6214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006216 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006218 tv.tv_sec = towait / 1000;
6219 tv.tv_usec = (towait % 1000) * (1000000/1000);
6220 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006222 else
6223 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
6225 /*
6226 * Select on ready for reading and exceptional condition (end of file).
6227 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006228select_eintr:
6229 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006230 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 FD_ZERO(&efds);
6232 FD_SET(fd, &rfds);
6233# if !defined(__QNX__) && !defined(__CYGWIN32__)
6234 /* For QNX select() always returns 1 if this is set. Why? */
6235 FD_SET(fd, &efds);
6236# endif
6237 maxfd = fd;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006240 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (xterm_Shell != (Widget)0)
6242 {
6243 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6244 if (maxfd < ConnectionNumber(xterm_dpy))
6245 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006246
6247 /* An event may have already been read but not handled. In
6248 * particulary, XFlush may cause this. */
6249 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251# endif
6252# ifdef FEAT_MOUSE_GPM
6253 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6254 {
6255 FD_SET(gpm_fd, &rfds);
6256 FD_SET(gpm_fd, &efds);
6257 if (maxfd < gpm_fd)
6258 maxfd = gpm_fd;
6259 }
6260# endif
6261# ifdef USE_XSMP
6262 if (xsmp_icefd != -1)
6263 {
6264 FD_SET(xsmp_icefd, &rfds);
6265 FD_SET(xsmp_icefd, &efds);
6266 if (maxfd < xsmp_icefd)
6267 maxfd = xsmp_icefd;
6268 }
6269# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006270# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006271 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006272# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006273 if (interrupted != NULL)
6274 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
Bram Moolenaar643b6142018-09-12 20:29:09 +02006276 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6277 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006278 result = ret > 0 && FD_ISSET(fd, &rfds);
6279 if (result)
6280 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006281 else if (interrupted != NULL && ret > 0)
6282 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006283
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006284# ifdef EINTR
6285 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006286 {
6287 /* Check whether window has been resized, EINTR may be caused by
6288 * SIGWINCH. */
6289 if (do_resize)
6290 handle_resize();
6291
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006292 /* Interrupted by a signal, need to try again. We ignore msec
6293 * here, because we do want to check even after a timeout if
6294 * characters are available. Needed for reading output of an
6295 * external command after the process has finished. */
6296 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006297 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006298# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006299# ifdef __TANDEM
6300 if (ret == -1 && errno == ENOTSUP)
6301 {
6302 FD_ZERO(&rfds);
6303 FD_ZERO(&efds);
6304 ret = 0;
6305 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006306# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006307# ifdef FEAT_MZSCHEME
6308 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006309 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006310 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311# endif
6312
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313# ifdef FEAT_XCLIPBOARD
6314 if (ret > 0 && xterm_Shell != (Widget)0
6315 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6316 {
6317 xterm_update(); /* Maybe we should hand out clipboard */
6318 /* continue looping when we only got the X event and the input
6319 * buffer is empty */
6320 if (--ret == 0 && !input_available())
6321 {
6322 /* Try again */
6323 finished = FALSE;
6324 }
6325 }
6326# endif
6327# ifdef FEAT_MOUSE_GPM
6328 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6329 {
6330 if (FD_ISSET(gpm_fd, &efds))
6331 gpm_close();
6332 else if (FD_ISSET(gpm_fd, &rfds))
6333 *check_for_gpm = 1;
6334 }
6335# endif
6336# ifdef USE_XSMP
6337 if (ret > 0 && xsmp_icefd != -1)
6338 {
6339 if (FD_ISSET(xsmp_icefd, &efds))
6340 {
6341 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006342 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 xsmp_close();
6344 if (--ret == 0)
6345 finished = FALSE; /* keep going if event was only one */
6346 }
6347 else if (FD_ISSET(xsmp_icefd, &rfds))
6348 {
6349 busy = TRUE;
6350 xsmp_handle_requests();
6351 busy = FALSE;
6352 if (--ret == 0)
6353 finished = FALSE; /* keep going if event was only one */
6354 }
6355 }
6356# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006357#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006358 /* also call when ret == 0, we may be polling a keep-open channel */
6359 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006360 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363#endif /* HAVE_SELECT */
6364
6365#ifdef MAY_LOOP
6366 if (finished || msec == 0)
6367 break;
6368
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006369# ifdef FEAT_CLIENTSERVER
6370 if (server_waiting())
6371 break;
6372# endif
6373
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 /* We're going to loop around again, find out for how long */
6375 if (msec > 0)
6376 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006377# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006379 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380# else
6381 /* Guess we got interrupted halfway. */
6382 msec = msec / 2;
6383# endif
6384 if (msec <= 0)
6385 break; /* waited long enough */
6386 }
6387#endif
6388 }
6389
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006390 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391}
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006395 * Expand a path into all matching files and/or directories. Handles "*",
6396 * "?", "[a-z]", "**", etc.
6397 * "path" has backslashes before chars that are not to be expanded.
6398 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 */
6400 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006401mch_expandpath(
6402 garray_T *gap,
6403 char_u *path,
6404 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
Bram Moolenaar02743632005-07-25 20:42:36 +00006406 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407}
6408#endif
6409
6410/*
6411 * mch_expand_wildcards() - this code does wild-card pattern matching using
6412 * the shell
6413 *
6414 * return OK for success, FAIL for error (you may lose some memory) and put
6415 * an error message in *file.
6416 *
6417 * num_pat is number of input patterns
6418 * pat is array of pointers to input patterns
6419 * num_file is pointer to number of matched file names
6420 * file is pointer to array of pointers to matched file names
6421 */
6422
6423#ifndef SEEK_SET
6424# define SEEK_SET 0
6425#endif
6426#ifndef SEEK_END
6427# define SEEK_END 2
6428#endif
6429
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006430#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006433mch_expand_wildcards(
6434 int num_pat,
6435 char_u **pat,
6436 int *num_file,
6437 char_u ***file,
6438 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 int i;
6441 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006442 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 char_u *p;
6444 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
Bram Moolenaarc7247912008-01-13 12:54:11 +00006446 /*
6447 * This is the non-OS/2 implementation (really Unix).
6448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 int j;
6450 char_u *tempname;
6451 char_u *command;
6452 FILE *fd;
6453 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006454#define STYLE_ECHO 0 /* use "echo", the default */
6455#define STYLE_GLOB 1 /* use "glob", for csh */
6456#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6457#define STYLE_PRINT 3 /* use "print -N", for zsh */
6458#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6459 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 int shell_style = STYLE_ECHO;
6461 int check_spaces;
6462 static int did_find_nul = FALSE;
6463 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006464 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006465 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 *num_file = 0; /* default: no files found */
6468 *file = NULL;
6469
6470 /*
6471 * If there are no wildcards, just copy the names to allocated memory.
6472 * Saves a lot of time, because we don't have to start a new shell.
6473 */
6474 if (!have_wildcard(num_pat, pat))
6475 return save_patterns(num_pat, pat, num_file, file);
6476
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006477# ifdef HAVE_SANDBOX
6478 /* Don't allow any shell command in the sandbox. */
6479 if (sandbox != 0 && check_secure())
6480 return FAIL;
6481# endif
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 /*
6484 * Don't allow the use of backticks in secure and restricted mode.
6485 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006486 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 for (i = 0; i < num_pat; ++i)
6488 if (vim_strchr(pat[i], '`') != NULL
6489 && (check_restricted() || check_secure()))
6490 return FAIL;
6491
6492 /*
6493 * get a name for the temp file
6494 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006495 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006497 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 return FAIL;
6499 }
6500
6501 /*
6502 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006503 * file.
6504 * STYLE_BT: NL separated
6505 * If expanding `cmd` execute it directly.
6506 * STYLE_GLOB: NUL separated
6507 * If we use *csh, "glob" will work better than "echo".
6508 * STYLE_PRINT: NL or NUL separated
6509 * If we use *zsh, "print -N" will work better than "glob".
6510 * STYLE_VIMGLOB: NL separated
6511 * If we use *sh*, we define "vimglob()".
6512 * STYLE_ECHO: space separated.
6513 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 */
6515 if (num_pat == 1 && *pat[0] == '`'
6516 && (len = STRLEN(pat[0])) > 2
6517 && *(pat[0] + len - 1) == '`')
6518 shell_style = STYLE_BT;
6519 else if ((len = STRLEN(p_sh)) >= 3)
6520 {
6521 if (STRCMP(p_sh + len - 3, "csh") == 0)
6522 shell_style = STYLE_GLOB;
6523 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6524 shell_style = STYLE_PRINT;
6525 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006526 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6527 "sh") != NULL)
6528 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaarc7247912008-01-13 12:54:11 +00006530 /* Compute the length of the command. We need 2 extra bytes: for the
6531 * optional '&' and for the NUL.
6532 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006534 if (shell_style == STYLE_VIMGLOB)
6535 len += STRLEN(sh_vimglob_func);
6536
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006537 for (i = 0; i < num_pat; ++i)
6538 {
6539 /* Count the length of the patterns in the same way as they are put in
6540 * "command" below. */
6541#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006543#else
6544 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006545 for (j = 0; pat[i][j] != NUL; ++j)
6546 {
6547 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6548 ++len; /* may add a backslash */
6549 ++len;
6550 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006551#endif
6552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 command = alloc(len);
6554 if (command == NULL)
6555 {
6556 /* out of memory */
6557 vim_free(tempname);
6558 return FAIL;
6559 }
6560
6561 /*
6562 * Build the shell command:
6563 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6564 * recognizes this).
6565 * - Add the shell command to print the expanded names.
6566 * - Add the temp file name.
6567 * - Add the file name patterns.
6568 */
6569 if (shell_style == STYLE_BT)
6570 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006571 /* change `command; command& ` to (command; command ) */
6572 STRCPY(command, "(");
6573 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006575 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006576 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 --p;
6578 if (*p == '&') /* remove trailing '&' */
6579 {
6580 ampersent = TRUE;
6581 *p = ' ';
6582 }
6583 STRCAT(command, ">");
6584 }
6585 else
6586 {
6587 if (flags & EW_NOTFOUND)
6588 STRCPY(command, "set nonomatch; ");
6589 else
6590 STRCPY(command, "unset nonomatch; ");
6591 if (shell_style == STYLE_GLOB)
6592 STRCAT(command, "glob >");
6593 else if (shell_style == STYLE_PRINT)
6594 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006595 else if (shell_style == STYLE_VIMGLOB)
6596 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 else
6598 STRCAT(command, "echo >");
6599 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006600
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (shell_style != STYLE_BT)
6604 for (i = 0; i < num_pat; ++i)
6605 {
6606 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006607 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006608 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609#ifdef USE_SYSTEM
6610 STRCAT(command, " \"");
6611 STRCAT(command, pat[i]);
6612 STRCAT(command, "\"");
6613#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006614 int intick = FALSE;
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 p = command + STRLEN(command);
6617 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006618 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006619 {
6620 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006621 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006622 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6623 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006624 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006625 * backslash inside backticks, before a special character
6626 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006627 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006628 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6629 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006630 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006631 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006632 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006633 else if (!intick
6634 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6635 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006636 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006637 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6638 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006639 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006640
6641 /* Copy one character. */
6642 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 *p = NUL;
6645#endif
6646 }
6647 if (flags & EW_SILENT)
6648 show_shell_mess = FALSE;
6649 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006650 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 /*
6653 * Using zsh -G: If a pattern has no matches, it is just deleted from
6654 * the argument list, otherwise zsh gives an error message and doesn't
6655 * expand any other pattern.
6656 */
6657 if (shell_style == STYLE_PRINT)
6658 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6659
6660 /*
6661 * If we use -f then shell variables set in .cshrc won't get expanded.
6662 * vi can do it, so we will too, but it is only necessary if there is a "$"
6663 * in one of the patterns, otherwise we can still use the fast option.
6664 */
6665 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6666 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6667
6668 /*
6669 * execute the shell command
6670 */
6671 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6672
6673 /* When running in the background, give it some time to create the temp
6674 * file, but don't wait for it to finish. */
6675 if (ampersent)
6676 mch_delay(10L, TRUE);
6677
6678 extra_shell_arg = NULL; /* cleanup */
6679 show_shell_mess = TRUE;
6680 vim_free(command);
6681
Bram Moolenaarc7247912008-01-13 12:54:11 +00006682 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
6684 mch_remove(tempname);
6685 vim_free(tempname);
6686 /*
6687 * With interactive completion, the error message is not printed.
6688 * However with USE_SYSTEM, I don't know how to turn off error messages
6689 * from the shell, so screen may still get messed up -- webb.
6690 */
6691#ifndef USE_SYSTEM
6692 if (!(flags & EW_SILENT))
6693#endif
6694 {
6695 redraw_later_clear(); /* probably messed up screen */
6696 msg_putchar('\n'); /* clear bottom line quickly */
6697 cmdline_row = Rows - 1; /* continue on last line */
6698#ifdef USE_SYSTEM
6699 if (!(flags & EW_SILENT))
6700#endif
6701 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006702 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 msg_start(); /* don't overwrite this message */
6704 }
6705 }
6706 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6707 * EW_NOTFOUND is given */
6708 if (shell_style == STYLE_BT)
6709 return FAIL;
6710 goto notfound;
6711 }
6712
6713 /*
6714 * read the names from the file into memory
6715 */
6716 fd = fopen((char *)tempname, READBIN);
6717 if (fd == NULL)
6718 {
6719 /* Something went wrong, perhaps a file name with a special char. */
6720 if (!(flags & EW_SILENT))
6721 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006722 msg(_(e_wildexpand));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 msg_start(); /* don't overwrite this message */
6724 }
6725 vim_free(tempname);
6726 goto notfound;
6727 }
6728 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006729 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006731 if (llen < 0)
6732 /* just in case ftell() would fail */
6733 buffer = NULL;
6734 else
6735 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (buffer == NULL)
6737 {
6738 /* out of memory */
6739 mch_remove(tempname);
6740 vim_free(tempname);
6741 fclose(fd);
6742 return FAIL;
6743 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006744 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 i = fread((char *)buffer, 1, len, fd);
6746 fclose(fd);
6747 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006748 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {
6750 /* unexpected read error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006751 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 vim_free(tempname);
6753 vim_free(buffer);
6754 return FAIL;
6755 }
6756 vim_free(tempname);
6757
Bram Moolenaarc7247912008-01-13 12:54:11 +00006758# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6760 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006761 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6763 *p++ = buffer[i];
6764 len = p - buffer;
6765# endif
6766
6767
6768 /* file names are separated with Space */
6769 if (shell_style == STYLE_ECHO)
6770 {
6771 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6772 p = buffer;
6773 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6774 {
6775 while (*p != ' ' && *p != '\n')
6776 ++p;
6777 p = skipwhite(p); /* skip to next entry */
6778 }
6779 }
6780 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006781 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6784 p = buffer;
6785 for (i = 0; *p != NUL; ++i) /* count number of entries */
6786 {
6787 while (*p != '\n' && *p != NUL)
6788 ++p;
6789 if (*p != NUL)
6790 ++p;
6791 p = skipwhite(p); /* skip leading white space */
6792 }
6793 }
6794 /* file names are separated with NUL */
6795 else
6796 {
6797 /*
6798 * Some versions of zsh use spaces instead of NULs to separate
6799 * results. Only do this when there is no NUL before the end of the
6800 * buffer, otherwise we would never be able to use file names with
6801 * embedded spaces when zsh does use NULs.
6802 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6803 * don't check for spaces again.
6804 */
6805 check_spaces = FALSE;
6806 if (shell_style == STYLE_PRINT && !did_find_nul)
6807 {
6808 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006809 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006810 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 did_find_nul = TRUE;
6812 else
6813 check_spaces = TRUE;
6814 }
6815
6816 /*
6817 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6818 * already is one, for STYLE_GLOB it needs to be added.
6819 */
6820 if (len && buffer[len - 1] == NUL)
6821 --len;
6822 else
6823 buffer[len] = NUL;
6824 i = 0;
6825 for (p = buffer; p < buffer + len; ++p)
6826 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6827 {
6828 ++i;
6829 *p = NUL;
6830 }
6831 if (len)
6832 ++i; /* count last entry */
6833 }
6834 if (i == 0)
6835 {
6836 /*
6837 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6838 * /bin/sh will happily expand it to nothing rather than returning an
6839 * error; and hey, it's good to check anyway -- webb.
6840 */
6841 vim_free(buffer);
6842 goto notfound;
6843 }
6844 *num_file = i;
6845 *file = (char_u **)alloc(sizeof(char_u *) * i);
6846 if (*file == NULL)
6847 {
6848 /* out of memory */
6849 vim_free(buffer);
6850 return FAIL;
6851 }
6852
6853 /*
6854 * Isolate the individual file names.
6855 */
6856 p = buffer;
6857 for (i = 0; i < *num_file; ++i)
6858 {
6859 (*file)[i] = p;
6860 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006861 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6862 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006864 while (!(shell_style == STYLE_ECHO && *p == ' ')
6865 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 ++p;
6867 if (p == buffer + len) /* last entry */
6868 *p = NUL;
6869 else
6870 {
6871 *p++ = NUL;
6872 p = skipwhite(p); /* skip to next entry */
6873 }
6874 }
6875 else /* NUL separates */
6876 {
6877 while (*p && p < buffer + len) /* skip entry */
6878 ++p;
6879 ++p; /* skip NUL */
6880 }
6881 }
6882
6883 /*
6884 * Move the file names to allocated memory.
6885 */
6886 for (j = 0, i = 0; i < *num_file; ++i)
6887 {
6888 /* Require the files to exist. Helps when using /bin/sh */
6889 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6890 continue;
6891
6892 /* check if this entry should be included */
6893 dir = (mch_isdir((*file)[i]));
6894 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6895 continue;
6896
Bram Moolenaara2031822006-03-07 22:29:51 +00006897 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006898 if (!dir && (flags & EW_EXEC)
6899 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006900 continue;
6901
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6903 if (p)
6904 {
6905 STRCPY(p, (*file)[i]);
6906 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006907 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 (*file)[j++] = p;
6909 }
6910 }
6911 vim_free(buffer);
6912 *num_file = j;
6913
6914 if (*num_file == 0) /* rejected all entries */
6915 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006916 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 goto notfound;
6918 }
6919
6920 return OK;
6921
6922notfound:
6923 if (flags & EW_NOTFOUND)
6924 return save_patterns(num_pat, pat, num_file, file);
6925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
6927
6928#endif /* VMS */
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006931save_patterns(
6932 int num_pat,
6933 char_u **pat,
6934 int *num_file,
6935 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006938 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
6940 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6941 if (*file == NULL)
6942 return FAIL;
6943 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006944 {
6945 s = vim_strsave(pat[i]);
6946 if (s != NULL)
6947 /* Be compatible with expand_filename(): halve the number of
6948 * backslashes. */
6949 backslash_halve(s);
6950 (*file)[i] = s;
6951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 *num_file = num_pat;
6953 return OK;
6954}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956/*
6957 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6958 * expand.
6959 */
6960 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006961mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006963 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (*p == '\\' && p[1] != NUL)
6966 ++p;
6967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (vim_strchr((char_u *)
6969#ifdef VMS
6970 "*?%"
6971#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
6974 , *p) != NULL)
6975 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977 return FALSE;
6978}
6979
6980/*
6981 * Return TRUE if the string "p" contains a wildcard.
6982 * Don't recognize '~' at the end as a wildcard.
6983 */
6984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006985mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006987 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (*p == '\\' && p[1] != NUL)
6990 ++p;
6991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (vim_strchr((char_u *)
6993#ifdef VMS
6994 "*?%$"
6995#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997#endif
6998 , *p) != NULL
6999 || (*p == '~' && p[1] != NUL))
7000 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002 return FALSE;
7003}
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007006have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 int i;
7009
7010 for (i = 0; i < num; i++)
7011 if (mch_has_wildcard(file[i]))
7012 return 1;
7013 return 0;
7014}
7015
7016 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007017have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 int i;
7020
7021 for (i = 0; i < num; i++)
7022 if (vim_strchr(file[i], '$') != NULL)
7023 return TRUE;
7024 return FALSE;
7025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007027#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028/*
7029 * Scaled-down version of rename(), which is missing in Xenix.
7030 * This version can only move regular files and will fail if the
7031 * destination exists.
7032 */
7033 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007034mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 struct stat st;
7037
7038 if (stat(dest, &st) >= 0) /* fail if destination exists */
7039 return -1;
7040 if (link(src, dest) != 0) /* link file to new name */
7041 return -1;
7042 if (mch_remove(src) == 0) /* delete link to old name */
7043 return 0;
7044 return -1;
7045}
7046#endif /* !HAVE_RENAME */
7047
7048#ifdef FEAT_MOUSE_GPM
7049/*
7050 * Initializes connection with gpm (if it isn't already opened)
7051 * Return 1 if succeeded (or connection already opened), 0 if failed
7052 */
7053 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007054gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7057
7058 if (!gpm_flag)
7059 {
7060 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7061 gpm_connect.defaultMask = ~GPM_HARD;
7062 /* Default handling for mouse move*/
7063 gpm_connect.minMod = 0; /* Handle any modifier keys */
7064 gpm_connect.maxMod = 0xffff;
7065 if (Gpm_Open(&gpm_connect, 0) > 0)
7066 {
7067 /* gpm library tries to handling TSTP causes
7068 * problems. Anyways, we close connection to Gpm whenever
7069 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007070 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007072# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 return 1; /* succeed */
7076 }
7077 if (gpm_fd == -2)
7078 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7079 return 0;
7080 }
7081 return 1; /* already open */
7082}
7083
7084/*
7085 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 */
7087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007088gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089{
7090 if (gpm_flag && gpm_fd >= 0) /* if Open */
7091 Gpm_Close();
7092}
7093
7094/* Reads gpm event and adds special keys to input buf. Returns length of
7095 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007096 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 */
7098 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007099mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 int button;
7102 static Gpm_Event gpm_event;
7103 char_u string[6];
7104 int_u vim_modifiers;
7105 int row,col;
7106 unsigned char buttons_mask;
7107 unsigned char gpm_modifiers;
7108 static unsigned char old_buttons = 0;
7109
7110 Gpm_GetEvent(&gpm_event);
7111
7112#ifdef FEAT_GUI
7113 /* Don't put events in the input queue now. */
7114 if (hold_gui_events)
7115 return 0;
7116#endif
7117
7118 row = gpm_event.y - 1;
7119 col = gpm_event.x - 1;
7120
7121 string[0] = ESC; /* Our termcode */
7122 string[1] = 'M';
7123 string[2] = 'G';
7124 switch (GPM_BARE_EVENTS(gpm_event.type))
7125 {
7126 case GPM_DRAG:
7127 string[3] = MOUSE_DRAG;
7128 break;
7129 case GPM_DOWN:
7130 buttons_mask = gpm_event.buttons & ~old_buttons;
7131 old_buttons = gpm_event.buttons;
7132 switch (buttons_mask)
7133 {
7134 case GPM_B_LEFT:
7135 button = MOUSE_LEFT;
7136 break;
7137 case GPM_B_MIDDLE:
7138 button = MOUSE_MIDDLE;
7139 break;
7140 case GPM_B_RIGHT:
7141 button = MOUSE_RIGHT;
7142 break;
7143 default:
7144 return 0;
7145 /*Don't know what to do. Can more than one button be
7146 * reported in one event? */
7147 }
7148 string[3] = (char_u)(button | 0x20);
7149 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7150 break;
7151 case GPM_UP:
7152 string[3] = MOUSE_RELEASE;
7153 old_buttons &= ~gpm_event.buttons;
7154 break;
7155 default:
7156 return 0;
7157 }
7158 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7159 gpm_modifiers = gpm_event.modifiers;
7160 vim_modifiers = 0x0;
7161 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7162 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7163 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7164 */
7165 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7166 vim_modifiers |= MOUSE_SHIFT;
7167
7168 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7169 vim_modifiers |= MOUSE_CTRL;
7170 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7171 vim_modifiers |= MOUSE_ALT;
7172 string[3] |= vim_modifiers;
7173 string[4] = (char_u)(col + ' ' + 1);
7174 string[5] = (char_u)(row + ' ' + 1);
7175 add_to_input_buf(string, 6);
7176 return 6;
7177}
7178#endif /* FEAT_MOUSE_GPM */
7179
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007180#ifdef FEAT_SYSMOUSE
7181/*
7182 * Initialize connection with sysmouse.
7183 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7184 * output, any sysmouse output than will be processed via sig_sysmouse().
7185 * Return OK if succeeded, FAIL if failed.
7186 */
7187 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007188sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007189{
7190 struct mouse_info mouse;
7191
7192 mouse.operation = MOUSE_MODE;
7193 mouse.u.mode.mode = 0;
7194 mouse.u.mode.signal = SIGUSR2;
7195 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7196 {
7197 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7198 mouse.operation = MOUSE_SHOW;
7199 ioctl(1, CONS_MOUSECTL, &mouse);
7200 return OK;
7201 }
7202 return FAIL;
7203}
7204
7205/*
7206 * Stop processing SIGUSR2 signals, and also make sure that
7207 * virtual console do not send us any sysmouse related signal.
7208 */
7209 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007210sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007211{
7212 struct mouse_info mouse;
7213
7214 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7215 mouse.operation = MOUSE_MODE;
7216 mouse.u.mode.mode = 0;
7217 mouse.u.mode.signal = 0;
7218 ioctl(1, CONS_MOUSECTL, &mouse);
7219}
7220
7221/*
7222 * Gets info from sysmouse and adds special keys to input buf.
7223 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007224 static RETSIGTYPE
7225sig_sysmouse SIGDEFARG(sigarg)
7226{
7227 struct mouse_info mouse;
7228 struct video_info video;
7229 char_u string[6];
7230 int row, col;
7231 int button;
7232 int buttons;
7233 static int oldbuttons = 0;
7234
7235#ifdef FEAT_GUI
7236 /* Don't put events in the input queue now. */
7237 if (hold_gui_events)
7238 return;
7239#endif
7240
7241 mouse.operation = MOUSE_GETINFO;
7242 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7243 && ioctl(1, FBIO_MODEINFO, &video) != -1
7244 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7245 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7246 {
7247 row = mouse.u.data.y / video.vi_cheight;
7248 col = mouse.u.data.x / video.vi_cwidth;
7249 buttons = mouse.u.data.buttons;
7250 string[0] = ESC; /* Our termcode */
7251 string[1] = 'M';
7252 string[2] = 'S';
7253 if (oldbuttons == buttons && buttons != 0)
7254 {
7255 button = MOUSE_DRAG;
7256 }
7257 else
7258 {
7259 switch (buttons)
7260 {
7261 case 0:
7262 button = MOUSE_RELEASE;
7263 break;
7264 case 1:
7265 button = MOUSE_LEFT;
7266 break;
7267 case 2:
7268 button = MOUSE_MIDDLE;
7269 break;
7270 case 4:
7271 button = MOUSE_RIGHT;
7272 break;
7273 default:
7274 return;
7275 }
7276 oldbuttons = buttons;
7277 }
7278 string[3] = (char_u)(button);
7279 string[4] = (char_u)(col + ' ' + 1);
7280 string[5] = (char_u)(row + ' ' + 1);
7281 add_to_input_buf(string, 6);
7282 }
7283 return;
7284}
7285#endif /* FEAT_SYSMOUSE */
7286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007288typedef char_u * (*STRPROCSTR)(char_u *);
7289typedef char_u * (*INTPROCSTR)(int);
7290typedef int (*STRPROCINT)(char_u *);
7291typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
7293/*
7294 * Call a DLL routine which takes either a string or int param
7295 * and returns an allocated string.
7296 */
7297 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007298mch_libcall(
7299 char_u *libname,
7300 char_u *funcname,
7301 char_u *argstring, /* NULL when using a argint */
7302 int argint,
7303 char_u **string_result,/* NULL when using number_result */
7304 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
7306# if defined(USE_DLOPEN)
7307 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007308 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309# else
7310 shl_t hinstLib;
7311# endif
7312 STRPROCSTR ProcAdd;
7313 INTPROCSTR ProcAddI;
7314 char_u *retval_str = NULL;
7315 int retval_int = 0;
7316 int success = FALSE;
7317
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007318 /*
7319 * Get a handle to the DLL module.
7320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007322 /* First clear any error, it's not cleared by the dlopen() call. */
7323 (void)dlerror();
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 hinstLib = dlopen((char *)libname, RTLD_LAZY
7326# ifdef RTLD_LOCAL
7327 | RTLD_LOCAL
7328# endif
7329 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007330 if (hinstLib == NULL)
7331 {
7332 /* "dlerr" must be used before dlclose() */
7333 dlerr = (char *)dlerror();
7334 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337# else
7338 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7339# endif
7340
7341 /* If the handle is valid, try to get the function address. */
7342 if (hinstLib != NULL)
7343 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007344# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 /*
7346 * Catch a crash when calling the library function. For example when
7347 * using a number where a string pointer is expected.
7348 */
7349 mch_startjmp();
7350 if (SETJMP(lc_jump_env) != 0)
7351 {
7352 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007353# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007354 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 mch_didjmp();
7357 }
7358 else
7359# endif
7360 {
7361 retval_str = NULL;
7362 retval_int = 0;
7363
7364 if (argstring != NULL)
7365 {
7366# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007367 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007368 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369# else
7370 if (shl_findsym(&hinstLib, (const char *)funcname,
7371 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7372 ProcAdd = NULL;
7373# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007374 if ((success = (ProcAdd != NULL
7375# if defined(USE_DLOPEN)
7376 && dlerr == NULL
7377# endif
7378 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 {
7380 if (string_result == NULL)
7381 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7382 else
7383 retval_str = (ProcAdd)(argstring);
7384 }
7385 }
7386 else
7387 {
7388# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007389 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007390 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391# else
7392 if (shl_findsym(&hinstLib, (const char *)funcname,
7393 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7394 ProcAddI = NULL;
7395# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007396 if ((success = (ProcAddI != NULL
7397# if defined(USE_DLOPEN)
7398 && dlerr == NULL
7399# endif
7400 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
7402 if (string_result == NULL)
7403 retval_int = ((INTPROCINT)ProcAddI)(argint);
7404 else
7405 retval_str = (ProcAddI)(argint);
7406 }
7407 }
7408
7409 /* Save the string before we free the library. */
7410 /* Assume that a "1" or "-1" result is an illegal pointer. */
7411 if (string_result == NULL)
7412 *number_result = retval_int;
7413 else if (retval_str != NULL
7414 && retval_str != (char_u *)1
7415 && retval_str != (char_u *)-1)
7416 *string_result = vim_strsave(retval_str);
7417 }
7418
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007419# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 mch_endjmp();
7421# ifdef SIGHASARG
7422 if (lc_signal != 0)
7423 {
7424 int i;
7425
7426 /* try to find the name of this signal */
7427 for (i = 0; signal_info[i].sig != -1; i++)
7428 if (lc_signal == signal_info[i].sig)
7429 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432# endif
7433# endif
7434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007436 /* "dlerr" must be used before dlclose() */
7437 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007438 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007439
7440 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 (void)dlclose(hinstLib);
7442# else
7443 (void)shl_unload(hinstLib);
7444# endif
7445 }
7446
7447 if (!success)
7448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007449 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 return FAIL;
7451 }
7452
7453 return OK;
7454}
7455#endif
7456
7457#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7458static int xterm_trace = -1; /* default: disabled */
7459static int xterm_button;
7460
7461/*
7462 * Setup a dummy window for X selections in a terminal.
7463 */
7464 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007465setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
7467 int z = 0;
7468 char *strp = "";
7469 Widget AppShell;
7470
7471 if (!x_connect_to_server())
7472 return;
7473
7474 open_app_context();
7475 if (app_context != NULL && xterm_Shell == (Widget)0)
7476 {
7477 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007478# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007480# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007481# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007482 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
7484 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007485 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486# endif
7487
7488 /* Ignore X errors while opening the display */
7489 oldhandler = XSetErrorHandler(x_error_check);
7490
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007491# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 /* Ignore X IO errors while opening the display */
7493 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7494 mch_startjmp();
7495 if (SETJMP(lc_jump_env) != 0)
7496 {
7497 mch_didjmp();
7498 xterm_dpy = NULL;
7499 }
7500 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
7503 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7504 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007505 if (xterm_dpy != NULL)
7506 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007507# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007512# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 /* Now handle X IO errors normally. */
7514 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 /* Now handle X errors normally. */
7517 (void)XSetErrorHandler(oldhandler);
7518
7519 if (xterm_dpy == NULL)
7520 {
7521 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007522 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 return;
7524 }
7525
7526 /* Catch terminating error of the X server connection. */
7527 (void)XSetIOErrorHandler(x_IOerror_handler);
7528
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007529# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007531 {
7532 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007533 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007534 verbose_leave();
7535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536# endif
7537
7538 /* Create a Shell to make converters work. */
7539 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7540 applicationShellWidgetClass, xterm_dpy,
7541 NULL);
7542 if (AppShell == (Widget)0)
7543 return;
7544 xterm_Shell = XtVaCreatePopupShell("VIM",
7545 topLevelShellWidgetClass, AppShell,
7546 XtNmappedWhenManaged, 0,
7547 XtNwidth, 1,
7548 XtNheight, 1,
7549 NULL);
7550 if (xterm_Shell == (Widget)0)
7551 return;
7552
7553 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007554 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 if (x11_display == NULL)
7556 x11_display = xterm_dpy;
7557
7558 XtRealizeWidget(xterm_Shell);
7559 XSync(xterm_dpy, False);
7560 xterm_update();
7561 }
7562 if (xterm_Shell != (Widget)0)
7563 {
7564 clip_init(TRUE);
7565 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7566 x11_window = (Window)atol(strp);
7567 /* Check if $WINDOWID is valid. */
7568 if (test_x11_window(xterm_dpy) == FAIL)
7569 x11_window = 0;
7570 if (x11_window != 0)
7571 xterm_trace = 0;
7572 }
7573}
7574
7575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007576start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
7578 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7579 return;
7580 xterm_trace = 1;
7581 xterm_button = button;
7582 do_xterm_trace();
7583}
7584
7585
7586 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007587stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 if (xterm_trace < 0)
7590 return;
7591 xterm_trace = 0;
7592}
7593
7594/*
7595 * Query the xterm pointer and generate mouse termcodes if necessary
7596 * return TRUE if dragging is active, else FALSE
7597 */
7598 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007599do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601 Window root, child;
7602 int root_x, root_y;
7603 int win_x, win_y;
7604 int row, col;
7605 int_u mask_return;
7606 char_u buf[50];
7607 char_u *strp;
7608 long got_hints;
7609 static char_u *mouse_code;
7610 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7611 static int prev_row = 0, prev_col = 0;
7612 static XSizeHints xterm_hints;
7613
7614 if (xterm_trace <= 0)
7615 return FALSE;
7616
7617 if (xterm_trace == 1)
7618 {
7619 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007620 * have changed recently. */
7621 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7622 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 || xterm_hints.width_inc <= 1
7624 || xterm_hints.height_inc <= 1)
7625 {
7626 xterm_trace = -1; /* Not enough data -- disable tracing */
7627 return FALSE;
7628 }
7629
7630 /* Rely on the same mouse code for the duration of this */
7631 mouse_code = find_termcode(mouse_name);
7632 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007633 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 xterm_trace = 2;
7635
7636 /* Find the offset of the chars, there might be a scrollbar on the
7637 * left of the window and/or a menu on the top (eterm etc.) */
7638 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7639 &win_x, &win_y, &mask_return);
7640 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7641 - (xterm_hints.height_inc / 2);
7642 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7643 xterm_hints.y = 2;
7644 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7645 - (xterm_hints.width_inc / 2);
7646 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7647 xterm_hints.x = 2;
7648 return TRUE;
7649 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007650 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 xterm_trace = 0;
7653 return FALSE;
7654 }
7655
7656 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7657 &win_x, &win_y, &mask_return);
7658
7659 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7660 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7661 if (row == prev_row && col == prev_col)
7662 return TRUE;
7663
7664 STRCPY(buf, mouse_code);
7665 strp = buf + STRLEN(buf);
7666 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7667 *strp++ = (char_u)(col + ' ' + 1);
7668 *strp++ = (char_u)(row + ' ' + 1);
7669 *strp = 0;
7670 add_to_input_buf(buf, STRLEN(buf));
7671
7672 prev_row = row;
7673 prev_col = col;
7674 return TRUE;
7675}
7676
7677# if defined(FEAT_GUI) || defined(PROTO)
7678/*
7679 * Destroy the display, window and app_context. Required for GTK.
7680 */
7681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 if (xterm_Shell != (Widget)0)
7685 {
7686 XtDestroyWidget(xterm_Shell);
7687 xterm_Shell = (Widget)0;
7688 }
7689 if (xterm_dpy != NULL)
7690 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007691# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 /* Lesstif and Solaris crash here, lose some memory */
7693 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 if (x11_display == xterm_dpy)
7696 x11_display = NULL;
7697 xterm_dpy = NULL;
7698 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007699# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (app_context != (XtAppContext)NULL)
7701 {
7702 /* Lesstif and Solaris crash here, lose some memory */
7703 XtDestroyApplicationContext(app_context);
7704 app_context = (XtAppContext)NULL;
7705 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707}
7708# endif
7709
7710/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007711 * Catch up with GUI or X events.
7712 */
7713 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007714clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007715{
7716# ifdef FEAT_GUI
7717 if (gui.in_use)
7718 gui_mch_update();
7719 else
7720# endif
7721 if (xterm_Shell != (Widget)0)
7722 xterm_update();
7723}
7724
7725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 * Catch up with any queued X events. This may put keyboard input into the
7727 * input buffer, call resize call-backs, trigger timers etc. If there is
7728 * nothing in the X event queue (& no timers pending), then we return
7729 * immediately.
7730 */
7731 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007732xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 XEvent event;
7735
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007736 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007738 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007740 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007741 break;
7742
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007743 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007744 {
7745 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007746 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007747#ifdef FEAT_CLIENTSERVER
7748 {
7749 XPropertyEvent *e = (XPropertyEvent *)&event;
7750
7751 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007753 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007756 XtDispatchEvent(&event);
7757 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007758 else
7759 {
7760 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007761 XtAppProcessEvent(app_context, mask);
7762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764}
7765
7766 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007767clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 if (xterm_Shell != (Widget)0)
7770 return clip_x11_own_selection(xterm_Shell, cbd);
7771 return FAIL;
7772}
7773
7774 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007775clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 if (xterm_Shell != (Widget)0)
7778 clip_x11_lose_selection(xterm_Shell, cbd);
7779}
7780
7781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007782clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 if (xterm_Shell != (Widget)0)
7785 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7786}
7787
7788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 clip_x11_set_selection(cbd);
7792}
7793#endif
7794
7795
7796#if defined(USE_XSMP) || defined(PROTO)
7797/*
7798 * Code for X Session Management Protocol.
7799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800
7801# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802/*
7803 * This is our chance to ask the user if they want to save,
7804 * or abort the logout
7805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007807xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809 cmdmod_T save_cmdmod;
7810 int cancel_shutdown = False;
7811
7812 save_cmdmod = cmdmod;
7813 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007814 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 /* Mustn't logout */
7816 cancel_shutdown = True;
7817 cmdmod = save_cmdmod;
7818 setcursor(); /* position cursor */
7819 out_flush();
7820
7821 /* Done interaction */
7822 SmcInteractDone(smc_conn, cancel_shutdown);
7823
7824 /* Finish off
7825 * Only end save-yourself here if we're not cancelling shutdown;
7826 * we'll get a cancelled callback later in which we'll end it.
7827 * Hopefully get around glitchy SMs (like GNOME-1)
7828 */
7829 if (!cancel_shutdown)
7830 {
7831 xsmp.save_yourself = False;
7832 SmcSaveYourselfDone(smc_conn, True);
7833 }
7834}
7835# endif
7836
7837/*
7838 * Callback that starts save-yourself.
7839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007841xsmp_handle_save_yourself(
7842 SmcConn smc_conn,
7843 SmPointer client_data UNUSED,
7844 int save_type UNUSED,
7845 Bool shutdown,
7846 int interact_style UNUSED,
7847 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 /* Handle already being in saveyourself */
7850 if (xsmp.save_yourself)
7851 SmcSaveYourselfDone(smc_conn, True);
7852 xsmp.save_yourself = True;
7853 xsmp.shutdown = shutdown;
7854
7855 /* First up, preserve all files */
7856 out_flush();
7857 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7858
7859 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007860 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
7862# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7863 /* Now see if we can ask about unsaved files */
7864 if (shutdown && !fast && gui.in_use)
7865 /* Need to interact with user, but need SM's permission */
7866 SmcInteractRequest(smc_conn, SmDialogError,
7867 xsmp_handle_interaction, client_data);
7868 else
7869# endif
7870 {
7871 /* Can stop the cycle here */
7872 SmcSaveYourselfDone(smc_conn, True);
7873 xsmp.save_yourself = False;
7874 }
7875}
7876
7877
7878/*
7879 * Callback to warn us of imminent death.
7880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007882xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 xsmp_close();
7885
7886 /* quit quickly leaving swapfiles for modified buffers behind */
7887 getout_preserve_modified(0);
7888}
7889
7890
7891/*
7892 * Callback to tell us that save-yourself has completed.
7893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007895xsmp_save_complete(
7896 SmcConn smc_conn UNUSED,
7897 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 xsmp.save_yourself = False;
7900}
7901
7902
7903/*
7904 * Callback to tell us that an instigated shutdown was cancelled
7905 * (maybe even by us)
7906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007908xsmp_shutdown_cancelled(
7909 SmcConn smc_conn,
7910 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 if (xsmp.save_yourself)
7913 SmcSaveYourselfDone(smc_conn, True);
7914 xsmp.save_yourself = False;
7915 xsmp.shutdown = False;
7916}
7917
7918
7919/*
7920 * Callback to tell us that a new ICE connection has been established.
7921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007923xsmp_ice_connection(
7924 IceConn iceConn,
7925 IcePointer clientData UNUSED,
7926 Bool opening,
7927 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 /* Intercept creation of ICE connection fd */
7930 if (opening)
7931 {
7932 xsmp_icefd = IceConnectionNumber(iceConn);
7933 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7934 }
7935}
7936
7937
7938/* Handle any ICE processing that's required; return FAIL if SM lost */
7939 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007940xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
7942 Bool rep;
7943
7944 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7945 == IceProcessMessagesIOError)
7946 {
7947 /* Lost ICE */
7948 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007949 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 xsmp_close();
7951 return FAIL;
7952 }
7953 else
7954 return OK;
7955}
7956
7957static int dummy;
7958
7959/* Set up X Session Management Protocol */
7960 void
7961xsmp_init(void)
7962{
7963 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 SmcCallbacks smcallbacks;
7965#if 0
7966 SmPropValue smname;
7967 SmProp smnameprop;
7968 SmProp *smprops[1];
7969#endif
7970
7971 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007972 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
7974 xsmp.save_yourself = xsmp.shutdown = False;
7975
7976 /* Set up SM callbacks - must have all, even if they're not used */
7977 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7978 smcallbacks.save_yourself.client_data = NULL;
7979 smcallbacks.die.callback = xsmp_die;
7980 smcallbacks.die.client_data = NULL;
7981 smcallbacks.save_complete.callback = xsmp_save_complete;
7982 smcallbacks.save_complete.client_data = NULL;
7983 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7984 smcallbacks.shutdown_cancelled.client_data = NULL;
7985
7986 /* Set up a watch on ICE connection creations. The "dummy" argument is
7987 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7988 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7989 {
7990 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007991 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 return;
7993 }
7994
7995 /* Create an SM connection */
7996 xsmp.smcconn = SmcOpenConnection(
7997 NULL,
7998 NULL,
7999 SmProtoMajor,
8000 SmProtoMinor,
8001 SmcSaveYourselfProcMask | SmcDieProcMask
8002 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8003 &smcallbacks,
8004 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008005 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008006 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 errorstring);
8008 if (xsmp.smcconn == NULL)
8009 {
8010 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008011
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008013 {
8014 vim_snprintf(errorreport, sizeof(errorreport),
8015 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008016 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 return;
8019 }
8020 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8021
8022#if 0
8023 /* ID ourselves */
8024 smname.value = "vim";
8025 smname.length = 3;
8026 smnameprop.name = "SmProgram";
8027 smnameprop.type = "SmARRAY8";
8028 smnameprop.num_vals = 1;
8029 smnameprop.vals = &smname;
8030
8031 smprops[0] = &smnameprop;
8032 SmcSetProperties(xsmp.smcconn, 1, smprops);
8033#endif
8034}
8035
8036
8037/* Shut down XSMP comms. */
8038 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008039xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 if (xsmp_icefd != -1)
8042 {
8043 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008044 if (xsmp.clientid != NULL)
8045 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008046 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 xsmp_icefd = -1;
8048 }
8049}
8050#endif /* USE_XSMP */
8051
8052
8053#ifdef EBCDIC
8054/* Translate character to its CTRL- value */
8055char CtrlTable[] =
8056{
8057/* 00 - 5E */
8058 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8059 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8060 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8061 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8062 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8063 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8064/* ^ */ 0x1E,
8065/* - */ 0x1F,
8066/* 61 - 6C */
8067 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8068/* _ */ 0x1F,
8069/* 6E - 80 */
8070 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8071/* a */ 0x01,
8072/* b */ 0x02,
8073/* c */ 0x03,
8074/* d */ 0x37,
8075/* e */ 0x2D,
8076/* f */ 0x2E,
8077/* g */ 0x2F,
8078/* h */ 0x16,
8079/* i */ 0x05,
8080/* 8A - 90 */
8081 0, 0, 0, 0, 0, 0, 0,
8082/* j */ 0x15,
8083/* k */ 0x0B,
8084/* l */ 0x0C,
8085/* m */ 0x0D,
8086/* n */ 0x0E,
8087/* o */ 0x0F,
8088/* p */ 0x10,
8089/* q */ 0x11,
8090/* r */ 0x12,
8091/* 9A - A1 */
8092 0, 0, 0, 0, 0, 0, 0, 0,
8093/* s */ 0x13,
8094/* t */ 0x3C,
8095/* u */ 0x3D,
8096/* v */ 0x32,
8097/* w */ 0x26,
8098/* x */ 0x18,
8099/* y */ 0x19,
8100/* z */ 0x3F,
8101/* AA - AC */
8102 0, 0, 0,
8103/* [ */ 0x27,
8104/* AE - BC */
8105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8106/* ] */ 0x1D,
8107/* BE - C0 */ 0, 0, 0,
8108/* A */ 0x01,
8109/* B */ 0x02,
8110/* C */ 0x03,
8111/* D */ 0x37,
8112/* E */ 0x2D,
8113/* F */ 0x2E,
8114/* G */ 0x2F,
8115/* H */ 0x16,
8116/* I */ 0x05,
8117/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8118/* J */ 0x15,
8119/* K */ 0x0B,
8120/* L */ 0x0C,
8121/* M */ 0x0D,
8122/* N */ 0x0E,
8123/* O */ 0x0F,
8124/* P */ 0x10,
8125/* Q */ 0x11,
8126/* R */ 0x12,
8127/* DA - DF */ 0, 0, 0, 0, 0, 0,
8128/* \ */ 0x1C,
8129/* E1 */ 0,
8130/* S */ 0x13,
8131/* T */ 0x3C,
8132/* U */ 0x3D,
8133/* V */ 0x32,
8134/* W */ 0x26,
8135/* X */ 0x18,
8136/* Y */ 0x19,
8137/* Z */ 0x3F,
8138/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8140};
8141
8142char MetaCharTable[]=
8143{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8144 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8145 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8146 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8147 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8148};
8149
8150
8151/* TODO: Use characters NOT numbers!!! */
8152char CtrlCharTable[]=
8153{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8154 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8155 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8156 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8157 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8158};
8159
8160
8161#endif