blob: 91e302a9e61925af9acf0198b9c5ef95cae1f81d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
15 * Also for BeOS and Atari MiNT.
16 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#include "os_unixx.h" /* unix includes for os_unix.c only */
28
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaar643b6142018-09-12 20:29:09 +020046#ifdef __BEOS__
Bram Moolenaar311d9822007-02-27 15:48:28 +000047# undef select
Bram Moolenaar643b6142018-09-12 20:29:09 +020048# define select beos_select
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
Bram Moolenaara2442432007-04-26 14:26:37 +000051#ifdef __CYGWIN__
52# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000053# include <cygwin/version.h>
54# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
55 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010056# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
57# define WIN32_LEAN_AND_MEAN
58# include <windows.h>
59# include "winclip.pro"
60# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000061# endif
62#endif
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_MOUSE_GPM
65# include <gpm.h>
66/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
67 * I just copied relevant defines here. A cleaner solution would be to put gpm
68 * code into separate file and include there linux/keyboard.h
69 */
70/* #include <linux/keyboard.h> */
71# define KG_SHIFT 0
72# define KG_CTRL 2
73# define KG_ALT 3
74# define KG_ALTGR 1
75# define KG_SHIFTL 4
76# define KG_SHIFTR 5
77# define KG_CTRLL 6
78# define KG_CTRLR 7
79# define KG_CAPSSHIFT 8
80
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static void gpm_close(void);
82static int gpm_open(void);
83static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
85
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000086#ifdef FEAT_SYSMOUSE
87# include <sys/consio.h>
88# include <sys/fbio.h>
89
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int sysmouse_open(void);
91static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010092static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000093#endif
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * end of autoconf section. To be extended...
97 */
98
99/* Are the following #ifdefs still required? And why? Is that for X11? */
100
101#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
102# ifdef SIGWINCH
103# undef SIGWINCH
104# endif
105# ifdef TIOCGWINSZ
106# undef TIOCGWINSZ
107# endif
108#endif
109
110#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
111# define SIGWINCH SIGWINDOW
112#endif
113
114#ifdef FEAT_X11
115# include <X11/Xlib.h>
116# include <X11/Xutil.h>
117# include <X11/Xatom.h>
118# ifdef FEAT_XCLIPBOARD
119# include <X11/Intrinsic.h>
120# include <X11/Shell.h>
121# include <X11/StringDefs.h>
122static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void clip_update(void);
124static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# endif
126
127# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
128Window x11_window = 0;
129# endif
130Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
133#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200137static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static int did_set_title = FALSE;
139static char_u *oldicon = NULL;
140static int did_set_icon = FALSE;
141#endif
142
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar205b8862011-09-07 15:04:31 +0200145#ifdef HAVE_UNION_WAIT
146typedef union wait waitstatus;
147#else
148typedef int waitstatus;
149#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200150static int WaitForChar(long msec, int *interrupted, int ignore_input);
151static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100152#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200153int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200155static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157
158#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000160# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#endif
162
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
165#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100166static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100169static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100172static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174#if defined(SIGALRM) && defined(FEAT_X11) \
175 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
176# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100177static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000178/* volatile because it is used in signal handler sig_alarm(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200179static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100181static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static void catch_int_signal(void);
184static void set_signals(void);
185static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200186#ifdef HAVE_SIGPROCMASK
187# define SIGSET_DECL(set) sigset_t set;
188# define BLOCK_SIGNALS(set) block_signals(set)
189# define UNBLOCK_SIGNALS(set) unblock_signals(set)
190#else
191# define SIGSET_DECL(set)
192# define BLOCK_SIGNALS(set) do { /**/ } while (0)
193# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
194#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100195static int have_wildcard(int, char_u **);
196static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100198static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000201# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000204/* volatile because it is used in signal handler sig_winch(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200205static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206static char_u *extra_shell_arg = NULL;
207static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000208/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200209static volatile sig_atomic_t deadly_signal = 0; /* The signal we caught */
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000210/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200211static volatile sig_atomic_t in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100213#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
214static int dont_check_job_ended = 0;
215#endif
216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
218
219#ifdef USE_XSMP
220typedef struct
221{
222 SmcConn smcconn; /* The SM connection ID */
223 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200224 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 Bool save_yourself; /* If we're in the middle of a save_yourself */
226 Bool shutdown; /* If we're in shutdown mode */
227} xsmp_config_T;
228
229static xsmp_config_T xsmp;
230#endif
231
232#ifdef SYS_SIGLIST_DECLARED
233/*
234 * I have seen
235 * extern char *_sys_siglist[NSIG];
236 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
237 * that describe the signals. That is nearly what we want here. But
238 * autoconf does only check for sys_siglist (without the underscore), I
239 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100240 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242#endif
243
244static struct signalinfo
245{
246 int sig; /* Signal number, eg. SIGSEGV etc */
247 char *name; /* Signal name (not char_u!). */
248 char deadly; /* Catch as a deadly signal? */
249} signal_info[] =
250{
251#ifdef SIGHUP
252 {SIGHUP, "HUP", TRUE},
253#endif
254#ifdef SIGQUIT
255 {SIGQUIT, "QUIT", TRUE},
256#endif
257#ifdef SIGILL
258 {SIGILL, "ILL", TRUE},
259#endif
260#ifdef SIGTRAP
261 {SIGTRAP, "TRAP", TRUE},
262#endif
263#ifdef SIGABRT
264 {SIGABRT, "ABRT", TRUE},
265#endif
266#ifdef SIGEMT
267 {SIGEMT, "EMT", TRUE},
268#endif
269#ifdef SIGFPE
270 {SIGFPE, "FPE", TRUE},
271#endif
272#ifdef SIGBUS
273 {SIGBUS, "BUS", TRUE},
274#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100275#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
276 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {SIGSEGV, "SEGV", TRUE},
278#endif
279#ifdef SIGSYS
280 {SIGSYS, "SYS", TRUE},
281#endif
282#ifdef SIGALRM
283 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
284#endif
285#ifdef SIGTERM
286 {SIGTERM, "TERM", TRUE},
287#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100288#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {SIGVTALRM, "VTALRM", TRUE},
290#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000291#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
292 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
293 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {SIGPROF, "PROF", TRUE},
295#endif
296#ifdef SIGXCPU
297 {SIGXCPU, "XCPU", TRUE},
298#endif
299#ifdef SIGXFSZ
300 {SIGXFSZ, "XFSZ", TRUE},
301#endif
302#ifdef SIGUSR1
303 {SIGUSR1, "USR1", TRUE},
304#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000305#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
306 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {SIGUSR2, "USR2", TRUE},
308#endif
309#ifdef SIGINT
310 {SIGINT, "INT", FALSE},
311#endif
312#ifdef SIGWINCH
313 {SIGWINCH, "WINCH", FALSE},
314#endif
315#ifdef SIGTSTP
316 {SIGTSTP, "TSTP", FALSE},
317#endif
318#ifdef SIGPIPE
319 {SIGPIPE, "PIPE", FALSE},
320#endif
321 {-1, "Unknown!", FALSE}
322};
323
Bram Moolenaar25724922009-07-14 15:38:41 +0000324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000326{
327 if (p_verbose >= 5)
328 {
329 verbose_enter();
330 smsg((char_u *)"chdir(%s)", path);
331 verbose_leave();
332 }
333# ifdef VMS
334 return chdir(vms_fixfilename(path));
335# else
336 return chdir(path);
337# endif
338}
339
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200340/* Why is NeXT excluded here (and not in os_unixx.h)? */
341#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
342# define NEW_TTY_SYSTEM
343#endif
344
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000345/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100346 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100349mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200351 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100353 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000357 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 * Get a characters from the keyboard.
359 * Return the number of characters that are available.
360 * If wtime == 0 do not wait for characters.
361 * If wtime == n wait a short time for characters.
362 * If wtime == -1 wait forever for characters.
363 */
364 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100365mch_inchar(
366 char_u *buf,
367 int maxlen,
368 long wtime, /* don't use "time", MIPS cannot handle it */
369 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
371 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200372 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200373 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200374 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200375 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100376#ifdef ELAPSED_FUNC
377 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200378
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100379 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200382 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200383 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200385 /* Check if window changed size while we were busy, perhaps the ":set
386 * columns=99" command was used. */
387 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200389
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200390#ifdef MESSAGE_QUEUE
Bram Moolenaared5a9d62018-09-06 13:14:43 +0200391 // Only process messages when waiting.
392 if (wtime != 0)
393 {
394 parse_queued_messages();
395 // If input was put directly in typeahead buffer bail out here.
396 if (typebuf_changed(tb_change_cnt))
397 return 0;
398 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200399#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200400 if (wtime < 0 && did_start_blocking)
401 /* blocking and already waited for p_ut */
402 wait_time = -1;
403 else
404 {
405 if (wtime >= 0)
406 wait_time = wtime;
407 else
408 /* going to block after p_ut */
409 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100410#ifdef ELAPSED_FUNC
411 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200412#endif
413 wait_time -= elapsed_time;
414 if (wait_time < 0)
415 {
416 if (wtime >= 0)
417 /* no character available within "wtime" */
418 return 0;
419
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200420 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200421 {
422 /* no character available within 'updatetime' */
423 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200424 if (trigger_cursorhold() && maxlen >= 3
425 && !typebuf_changed(tb_change_cnt))
426 {
427 buf[0] = K_SPECIAL;
428 buf[1] = KS_EXTRA;
429 buf[2] = (int)KE_CURSORHOLD;
430 return 3;
431 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200432 /*
433 * If there is no character available within 'updatetime'
434 * seconds flush all the swap files to disk.
435 * Also done when interrupted by SIGWINCH.
436 */
437 before_blocking();
438 continue;
439 }
440 }
441 }
442
443#ifdef FEAT_JOB_CHANNEL
444 /* Checking if a job ended requires polling. Do this every 100 msec. */
445 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
446 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100447 /* If there is readahead then parse_queued_messages() timed out and we
448 * should call it again soon. */
449 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
450 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200451#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100452#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100453 if (p_beval && wait_time > 100L)
454 /* The 'balloonexpr' may indirectly invoke a callback while waiting
455 * for a character, need to check often. */
456 wait_time = 100L;
457#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200460 * We want to be interrupted by the winch signal
461 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200463 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200464 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200465 /* If input was put directly in typeahead buffer bail out here. */
466 if (typebuf_changed(tb_change_cnt))
467 return 0;
468
469 /*
470 * For some terminals we only get one character at a time.
471 * We want the get all available characters, so we could keep on
472 * trying until none is available
473 * For some other terminals this is quite slow, that's why we don't
474 * do it.
475 */
476 len = read_from_input_buf(buf, (long)maxlen);
477 if (len > 0)
478 return len;
479 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200482 /* no character available */
483#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
484 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100485 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200488 if (do_resize /* interrupted by SIGWINCH signal */
489#ifdef FEAT_CLIENTSERVER
490 || server_waiting()
491#endif
492#ifdef MESSAGE_QUEUE
493 || interrupted
494#endif
495 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100496 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200497 continue;
498
499 /* no character available or interrupted */
500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200502 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503}
504
505 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100506handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 do_resize = FALSE;
509 shell_resized();
510}
511
512/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200513 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
515 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100516mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200518 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200521#if defined(FEAT_TERMINAL) || defined(PROTO)
522/*
523 * Check for any pending input or messages.
524 */
525 int
526mch_check_messages(void)
527{
528 return WaitForChar(0L, NULL, TRUE);
529}
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
533# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000534# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535# endif
536# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
537# include <sys/sysctl.h>
538# endif
539# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
540# include <sys/sysinfo.h>
541# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100542# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100543# include <mach/mach_host.h>
544# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
547/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000548 * Return total amount of memory available in Kbyte.
549 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100552mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000555 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar362dc332018-03-05 21:59:37 +0100557# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100558 {
559 /* Mac (Darwin) way of getting the amount of RAM available */
560 mach_port_t host = mach_host_self();
561 kern_return_t kret;
562# ifdef HOST_VM_INFO64
563 struct vm_statistics64 vm_stat;
564 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar988615f2018-02-27 17:58:20 +0100566 kret = host_statistics64(host, HOST_VM_INFO64,
567 (host_info64_t)&vm_stat, &count);
568# else
569 struct vm_statistics vm_stat;
570 natural_t count = HOST_VM_INFO_COUNT;
571
572 kret = host_statistics(host, HOST_VM_INFO,
573 (host_info_t)&vm_stat, &count);
574# endif
575 if (kret == KERN_SUCCESS)
576 /* get the amount of user memory by summing each usage */
577 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
578 + vm_stat.inactive_count
579# ifdef MAC_OS_X_VERSION_10_9
580 + vm_stat.compressor_page_count
581# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100582 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100583 mach_port_deallocate(mach_task_self(), host);
584 }
585# endif
586
587# ifdef HAVE_SYSCTL
588 if (mem == 0)
589 {
590 /* BSD way of getting the amount of RAM available. */
591 int mib[2];
592 size_t len = sizeof(long_u);
593# ifdef HW_USERMEM64
594 long_u physmem;
595# else
596 /* sysctl() may return 32 bit or 64 bit, accept both */
597 union {
598 int_u u32;
599 long_u u64;
600 } physmem;
601# endif
602
603 mib[0] = CTL_HW;
604# ifdef HW_USERMEM64
605 mib[1] = HW_USERMEM64;
606# else
607 mib[1] = HW_USERMEM;
608# endif
609 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
610 {
611# ifdef HW_USERMEM64
612 mem = (long_u)physmem;
613# else
614 if (len == sizeof(physmem.u64))
615 mem = (long_u)physmem.u64;
616 else
617 mem = (long_u)physmem.u32;
618# endif
619 }
620 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200623# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (mem == 0)
625 {
626 struct sysinfo sinfo;
627
628 /* Linux way of getting amount of RAM available */
629 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000630 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200631# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000632 /* avoid overflow as much as possible */
633 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
634 {
635 sinfo.mem_unit = sinfo.mem_unit >> 1;
636 --shiftright;
637 }
638 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200639# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200641# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if (mem == 0)
648 {
649 long pagesize, pagecount;
650
651 /* Solaris way of getting amount of RAM available */
652 pagesize = sysconf(_SC_PAGESIZE);
653 pagecount = sysconf(_SC_PHYS_PAGES);
654 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
656 /* avoid overflow as much as possible */
657 while (shiftright > 0 && (pagesize & 1) == 0)
658 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000659 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000660 --shiftright;
661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667 /* Return the minimum of the physical memory and the user limit, because
668 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200669# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
671 struct rlimit rlp;
672
673 if (getrlimit(RLIMIT_DATA, &rlp) == 0
674 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200675# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200677# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000678 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 {
681 mem = (long_u)rlp.rlim_cur;
682 shiftright = 10;
683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
687 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000688 return mem >> shiftright;
689 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691#endif
692
693 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100694mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000697#ifdef FEAT_MZSCHEME
698 long total = msec; /* remember original value */
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701 if (ignoreinput)
702 {
703 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000704 * here. But we don't want QUIT to kill us (CTRL-\ used in a
705 * shell may produce SIGQUIT). */
706 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 old_tmode = curr_tmode;
708 if (curr_tmode == TMODE_RAW)
709 settmode(TMODE_SLEEP);
710
711 /*
712 * Everybody sleeps in a different way...
713 * Prefer nanosleep(), some versions of usleep() can only sleep up to
714 * one second.
715 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000716#ifdef FEAT_MZSCHEME
717 do
718 {
719 /* if total is large enough, wait by portions in p_mzq */
720 if (total > p_mzq)
721 msec = p_mzq;
722 else
723 msec = total;
724 total -= msec;
725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#ifdef HAVE_NANOSLEEP
727 {
728 struct timespec ts;
729
730 ts.tv_sec = msec / 1000;
731 ts.tv_nsec = (msec % 1000) * 1000000;
732 (void)nanosleep(&ts, NULL);
733 }
734#else
735# ifdef HAVE_USLEEP
736 while (msec >= 1000)
737 {
738 usleep((unsigned int)(999 * 1000));
739 msec -= 999;
740 }
741 usleep((unsigned int)(msec * 1000));
742# else
743# ifndef HAVE_SELECT
744 poll(NULL, 0, (int)msec);
745# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 struct timeval tv;
748
749 tv.tv_sec = msec / 1000;
750 tv.tv_usec = (msec % 1000) * 1000;
751 /*
752 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
753 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
754 */
755 select(0, NULL, NULL, NULL, &tv);
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757# endif /* HAVE_SELECT */
758# endif /* HAVE_NANOSLEEP */
759#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000760#ifdef FEAT_MZSCHEME
761 }
762 while (total > 0);
763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000766 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200769 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000772#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
774# define HAVE_CHECK_STACK_GROWTH
775/*
776 * Support for checking for an almost-out-of-stack-space situation.
777 */
778
779/*
780 * Return a pointer to an item on the stack. Used to find out if the stack
781 * grows up or down.
782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783static int stack_grows_downwards;
784
785/*
786 * Find out if the stack grows upwards or downwards.
787 * "p" points to a variable on the stack of the caller.
788 */
789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100790check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 int i;
793
794 stack_grows_downwards = (p > (char *)&i);
795}
796#endif
797
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000798#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799static char *stack_limit = NULL;
800
801#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
802# include <pthread.h>
803# include <pthread_np.h>
804#endif
805
806/*
807 * Find out until how var the stack can grow without getting into trouble.
808 * Called when starting up and when switching to the signal stack in
809 * deathtrap().
810 */
811 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100812get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 struct rlimit rlp;
815 int i;
816 long lim;
817
818 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
819 * limit doesn't fit in a long (rlim_cur might be "long long"). */
820 if (getrlimit(RLIMIT_STACK, &rlp) == 0
821 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
822# ifdef RLIM_INFINITY
823 && rlp.rlim_cur != RLIM_INFINITY
824# endif
825 )
826 {
827 lim = (long)rlp.rlim_cur;
828#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
829 {
830 pthread_attr_t attr;
831 size_t size;
832
833 /* On FreeBSD the initial thread always has a fixed stack size, no
834 * matter what the limits are set to. Normally it's 1 Mbyte. */
835 pthread_attr_init(&attr);
836 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
837 {
838 pthread_attr_getstacksize(&attr, &size);
839 if (lim > (long)size)
840 lim = (long)size;
841 }
842 pthread_attr_destroy(&attr);
843 }
844#endif
845 if (stack_grows_downwards)
846 {
847 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
848 if (stack_limit >= (char *)&i)
849 /* overflow, set to 1/16 of current stack position */
850 stack_limit = (char *)((long)&i / 16L);
851 }
852 else
853 {
854 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
855 if (stack_limit <= (char *)&i)
856 stack_limit = NULL; /* overflow */
857 }
858 }
859}
860
861/*
862 * Return FAIL when running out of stack space.
863 * "p" must point to any variable local to the caller that's on the stack.
864 */
865 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100866mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 if (stack_limit != NULL)
869 {
870 if (stack_grows_downwards)
871 {
872 if (p < stack_limit)
873 return FAIL;
874 }
875 else if (p > stack_limit)
876 return FAIL;
877 }
878 return OK;
879}
880#endif
881
882#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
883/*
884 * Support for using the signal stack.
885 * This helps when we run out of stack space, which causes a SIGSEGV. The
886 * signal handler then must run on another stack, since the normal stack is
887 * completely full.
888 */
889
890#ifndef SIGSTKSZ
891# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
892#endif
893
894# ifdef HAVE_SIGALTSTACK
895static stack_t sigstk; /* for sigaltstack() */
896# else
897static struct sigstack sigstk; /* for sigstack() */
898# endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900static char *signal_stack;
901
902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100903init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 if (signal_stack != NULL)
906 {
907# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908# ifdef HAVE_SS_BASE
909 sigstk.ss_base = signal_stack;
910# else
911 sigstk.ss_sp = signal_stack;
912# endif
913 sigstk.ss_size = SIGSTKSZ;
914 sigstk.ss_flags = 0;
915 (void)sigaltstack(&sigstk, NULL);
916# else
917 sigstk.ss_sp = signal_stack;
918 if (stack_grows_downwards)
919 sigstk.ss_sp += SIGSTKSZ - 1;
920 sigstk.ss_onstack = 0;
921 (void)sigstack(&sigstk, NULL);
922# endif
923 }
924}
925#endif
926
927/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000928 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * will barf when the second argument to signal() is ``wrong''.
930 * Let me try it with a few tricky defines from my own osdef.h (jw).
931 */
932#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 static RETSIGTYPE
934sig_winch SIGDEFARG(sigarg)
935{
936 /* this is not required on all systems, but it doesn't hurt anybody */
937 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
938 do_resize = TRUE;
939 SIGRETURN;
940}
941#endif
942
943#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 static RETSIGTYPE
945catch_sigint SIGDEFARG(sigarg)
946{
947 /* this is not required on all systems, but it doesn't hurt anybody */
948 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
949 got_int = TRUE;
950 SIGRETURN;
951}
952#endif
953
954#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 static RETSIGTYPE
956catch_sigpwr SIGDEFARG(sigarg)
957{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000958 /* this is not required on all systems, but it doesn't hurt anybody */
959 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /*
961 * I'm not sure we get the SIGPWR signal when the system is really going
962 * down or when the batteries are almost empty. Just preserve the swap
963 * files and don't exit, that can't do any harm.
964 */
965 ml_sync_all(FALSE, FALSE);
966 SIGRETURN;
967}
968#endif
969
970#ifdef SET_SIG_ALARM
971/*
972 * signal function for alarm().
973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 static RETSIGTYPE
975sig_alarm SIGDEFARG(sigarg)
976{
977 /* doesn't do anything, just to break a system call */
978 sig_alarm_called = TRUE;
979 SIGRETURN;
980}
981#endif
982
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100983#if defined(HAVE_SETJMP_H) || defined(PROTO)
984// argument to SETJMP()
985static JMP_BUF lc_jump_env;
986
987# ifdef SIGHASARG
988// Caught signal number, 0 when no was signal caught; used for mch_libcall().
989// Volatile because it is used in signal handlers.
990static volatile sig_atomic_t lc_signal;
991# endif
992
993// TRUE when lc_jump_env is valid.
994// Volatile because it is used in signal handler deathtrap().
995static volatile sig_atomic_t lc_active INIT(= FALSE);
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/*
998 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100999 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001001 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Usage:
1003 * mch_startjmp();
1004 * if (SETJMP(lc_jump_env) != 0)
1005 * {
1006 * mch_didjmp();
1007 * EMSG("crash!");
1008 * }
1009 * else
1010 * {
1011 * do_the_work;
1012 * mch_endjmp();
1013 * }
1014 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1015 * not return before the saved environment is used.
1016 * Returns OK for normal return, FAIL when the protected code caused a
1017 * problem and LONGJMP() was used.
1018 */
1019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001020mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022#ifdef SIGHASARG
1023 lc_signal = 0;
1024#endif
1025 lc_active = TRUE;
1026}
1027
1028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001029mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 lc_active = FALSE;
1032}
1033
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +01001038 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
1039 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 init_signal_stack();
1041# endif
1042}
1043#endif
1044
1045/*
1046 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001047 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001049 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1050 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
1052 static RETSIGTYPE
1053deathtrap SIGDEFARG(sigarg)
1054{
1055 static int entered = 0; /* count the number of times we got here.
1056 Note: when memory has been corrupted
1057 this may get an arbitrary value! */
1058#ifdef SIGHASARG
1059 int i;
1060#endif
1061
1062#if defined(HAVE_SETJMP_H)
1063 /*
1064 * Catch a crash in protected code.
1065 * Restores the environment saved in lc_jump_env, which looks like
1066 * SETJMP() returns 1.
1067 */
1068 if (lc_active)
1069 {
1070# if defined(SIGHASARG)
1071 lc_signal = sigarg;
1072# endif
1073 lc_active = FALSE; /* don't jump again */
1074 LONGJMP(lc_jump_env, 1);
1075 /* NOTREACHED */
1076 }
1077#endif
1078
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001079#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001080# ifdef SIGQUIT
1081 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1082 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1083 * pressing CTRL-\, but we don't want Vim to exit then. */
1084 if (in_mch_delay && sigarg == SIGQUIT)
1085 SIGRETURN;
1086# endif
1087
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001088 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1089 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1090 * free(). Calling free() again may then cause a crash. */
1091 if (entered == 0
1092 && (0
1093# ifdef SIGHUP
1094 || sigarg == SIGHUP
1095# endif
1096# ifdef SIGQUIT
1097 || sigarg == SIGQUIT
1098# endif
1099# ifdef SIGTERM
1100 || sigarg == SIGTERM
1101# endif
1102# ifdef SIGPWR
1103 || sigarg == SIGPWR
1104# endif
1105# ifdef SIGUSR1
1106 || sigarg == SIGUSR1
1107# endif
1108# ifdef SIGUSR2
1109 || sigarg == SIGUSR2
1110# endif
1111 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001112 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001113 SIGRETURN;
1114#endif
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /* Remember how often we have been called. */
1117 ++entered;
1118
Bram Moolenaare429e702016-06-10 19:49:14 +02001119 /* Executing autocommands is likely to use more stack space than we have
1120 * available in the signal stack. */
1121 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#ifdef FEAT_EVAL
1124 /* Set the v:dying variable. */
1125 set_vim_var_nr(VV_DYING, (long)entered);
1126#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001127 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /* Since we are now using the signal stack, need to reset the stack
1131 * limit. Otherwise using a regexp will fail. */
1132 get_stack_limit();
1133#endif
1134
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001135#if 0
1136 /* This is for opening gdb the moment Vim crashes.
1137 * You need to manually adjust the file name and Vim executable name.
1138 * Suggested by SungHyun Nam. */
1139 {
1140# define VI_GDB_FILE "/tmp/vimgdb"
1141# define VIM_NAME "/usr/bin/vim"
1142 FILE *fp = fopen(VI_GDB_FILE, "w");
1143 if (fp)
1144 {
1145 fprintf(fp,
1146 "file %s\n"
1147 "attach %d\n"
1148 "set height 1000\n"
1149 "bt full\n"
1150 , VIM_NAME, getpid());
1151 fclose(fp);
1152 system("xterm -e gdb -x "VI_GDB_FILE);
1153 unlink(VI_GDB_FILE);
1154 }
1155 }
1156#endif
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef SIGHASARG
1159 /* try to find the name of this signal */
1160 for (i = 0; signal_info[i].sig != -1; i++)
1161 if (sigarg == signal_info[i].sig)
1162 break;
1163 deadly_signal = sigarg;
1164#endif
1165
1166 full_screen = FALSE; /* don't write message to the GUI, it might be
1167 * part of the problem... */
1168 /*
1169 * If something goes wrong after entering here, we may get here again.
1170 * When this happens, give a message and try to exit nicely (resetting the
1171 * terminal mode, etc.)
1172 * When this happens twice, just exit, don't even try to give a message,
1173 * stack may be corrupt or something weird.
1174 * When this still happens again (or memory was corrupted in such a way
1175 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1176 */
1177 if (entered >= 3)
1178 {
1179 reset_signals(); /* don't catch any signals anymore */
1180 may_core_dump();
1181 if (entered >= 4)
1182 _exit(8);
1183 exit(7);
1184 }
1185 if (entered == 2)
1186 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001187 /* No translation, it may call malloc(). */
1188 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 out_flush();
1190 getout(1);
1191 }
1192
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001193 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001195 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 signal_info[i].name);
1197#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001198 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001200
1201 /* Preserve files and exit. This sets the really_exiting flag to prevent
1202 * calling free(). */
1203 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
Bram Moolenaare429e702016-06-10 19:49:14 +02001205 /* NOTREACHED */
1206
Bram Moolenaar009b2592004-10-24 19:18:58 +00001207#ifdef NBDEBUG
1208 reset_signals();
1209 may_core_dump();
1210 abort();
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 SIGRETURN;
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001217 * Invoked after receiving SIGCONT. We don't know what happened while
1218 * sleeping, deal with part of that.
1219 */
1220 static void
1221after_sigcont(void)
1222{
1223# ifdef FEAT_TITLE
1224 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1225 // again later.
1226 oldtitle_outdated = TRUE;
1227# endif
1228 settmode(TMODE_RAW);
1229 need_check_timestamps = TRUE;
1230 did_check_timestamps = FALSE;
1231}
1232
1233#if defined(SIGCONT)
1234static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001235static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001236
1237/*
1238 * With multi-threading, suspending might not work immediately. Catch the
1239 * SIGCONT signal, which will be used as an indication whether the suspending
1240 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001241 *
1242 * On Linux, signal is not always handled immediately either.
1243 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001244 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001245 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001246 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001248static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001249static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251/*
1252 * signal handler for SIGCONT
1253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 static RETSIGTYPE
1255sigcont_handler SIGDEFARG(sigarg)
1256{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001257 if (in_mch_suspend)
1258 {
1259 sigcont_received = TRUE;
1260 }
1261 else
1262 {
1263 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1264 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1265 // back to a sane mode. We should redraw, but we can't really do that
1266 // in a signal handler, do a redraw later.
1267 after_sigcont();
1268 redraw_later(CLEAR);
1269 cursor_on_force();
1270 out_flush();
1271 }
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 SIGRETURN;
1274}
1275#endif
1276
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001277#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001278# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001279static void *clip_star_save = NULL;
1280static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001281# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001282
1283/*
1284 * Called when Vim is going to sleep or execute a shell command.
1285 * We can't respond to requests for the X selections. Lose them, otherwise
1286 * other applications will hang. But first copy the text to cut buffer 0.
1287 */
1288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001290{
1291 if (clip_star.owned || clip_plus.owned)
1292 {
1293 x11_export_final_selection();
1294 if (clip_star.owned)
1295 clip_lose_selection(&clip_star);
1296 if (clip_plus.owned)
1297 clip_lose_selection(&clip_plus);
1298 if (x11_display != NULL)
1299 XFlush(x11_display);
1300 }
1301}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001302
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001303# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001304/*
1305 * Save clipboard text to restore later.
1306 */
1307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001309{
1310 if (clip_star.owned)
1311 clip_star_save = get_register('*', TRUE);
1312 if (clip_plus.owned)
1313 clip_plus_save = get_register('+', TRUE);
1314}
1315
1316/*
1317 * Restore clipboard text if no one own the X selection.
1318 */
1319 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001320restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001321{
1322 if (clip_star_save != NULL)
1323 {
1324 if (!clip_gen_owner_exists(&clip_star))
1325 put_register('*', clip_star_save);
1326 else
1327 free_register(clip_star_save);
1328 clip_star_save = NULL;
1329 }
1330 if (clip_plus_save != NULL)
1331 {
1332 if (!clip_gen_owner_exists(&clip_plus))
1333 put_register('+', clip_plus_save);
1334 else
1335 free_register(clip_plus_save);
1336 clip_plus_save = NULL;
1337 }
1338}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001339# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001340#endif
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342/*
1343 * If the machine has job control, use it to suspend the program,
1344 * otherwise fake it by starting a new shell.
1345 */
1346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001347mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 /* BeOS does have SIGTSTP, but it doesn't work. */
1350#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001351 in_mch_suspend = TRUE;
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 out_flush(); /* needed to make cursor visible on some systems */
1354 settmode(TMODE_COOK);
1355 out_flush(); /* needed to disable mouse on some systems */
1356
1357# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001358 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001360# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 sigcont_received = FALSE;
1362# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001365
1366# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001367 /*
1368 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001369 * immediately, but somehow not all the time, probably because it's handled
1370 * in another thread. Do not call pause() because there would be race
1371 * condition which would hang Vim if signal happened in between the test of
1372 * sigcont_received and the call to pause(). If signal is not yet received,
1373 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1374 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001375 */
1376 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001377 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001378
Bram Moolenaar262735e2009-07-14 10:20:22 +00001379 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001380 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
Bram Moolenaar2e310482018-08-21 13:09:10 +02001385 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#else
1387 suspend_shell();
1388#endif
1389}
1390
1391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001392mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 Columns = 80;
1395 Rows = 24;
1396
1397 out_flush();
1398 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001399
Bram Moolenaar56718732006-03-15 22:53:57 +00001400#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001401 mac_conv_init();
1402#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001403#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1404 win_clip_init();
1405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001409set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411#if defined(SIGWINCH)
1412 /*
1413 * WINDOW CHANGE signal is handled with sig_winch().
1414 */
1415 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1416#endif
1417
1418 /*
1419 * We want the STOP signal to work, to make mch_suspend() work.
1420 * For "rvim" the STOP signal is ignored.
1421 */
1422#ifdef SIGTSTP
1423 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1424#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001425#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 signal(SIGCONT, sigcont_handler);
1427#endif
1428
1429 /*
1430 * We want to ignore breaking of PIPEs.
1431 */
1432#ifdef SIGPIPE
1433 signal(SIGPIPE, SIG_IGN);
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001437 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438#endif
1439
1440 /*
1441 * Ignore alarm signals (Perl's alarm() generates it).
1442 */
1443#ifdef SIGALRM
1444 signal(SIGALRM, SIG_IGN);
1445#endif
1446
1447 /*
1448 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1449 * work will be lost.
1450 */
1451#ifdef SIGPWR
1452 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1453#endif
1454
1455 /*
1456 * Arrange for other signals to gracefully shutdown Vim.
1457 */
1458 catch_signals(deathtrap, SIG_ERR);
1459
1460#if defined(FEAT_GUI) && defined(SIGHUP)
1461 /*
1462 * When the GUI is running, ignore the hangup signal.
1463 */
1464 if (gui.in_use)
1465 signal(SIGHUP, SIG_IGN);
1466#endif
1467}
1468
Bram Moolenaardf177f62005-02-22 08:39:57 +00001469#if defined(SIGINT) || defined(PROTO)
1470/*
1471 * Catch CTRL-C (only works while in Cooked mode).
1472 */
1473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001474catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001475{
1476 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1477}
1478#endif
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001481reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001484#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 /* SIGCONT isn't in the list, because its default action is ignore */
1486 signal(SIGCONT, SIG_DFL);
1487#endif
1488}
1489
1490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001491catch_signals(
1492 RETSIGTYPE (*func_deadly)(),
1493 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 int i;
1496
1497 for (i = 0; signal_info[i].sig != -1; i++)
1498 if (signal_info[i].deadly)
1499 {
1500#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1501 struct sigaction sa;
1502
1503 /* Setup to use the alternate stack for the signal function. */
1504 sa.sa_handler = func_deadly;
1505 sigemptyset(&sa.sa_mask);
1506# if defined(__linux__) && defined(_REENTRANT)
1507 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1508 * thread handling in combination with using the alternate stack:
1509 * pthread library functions try to use the stack pointer to
1510 * identify the current thread, causing a SEGV signal, which
1511 * recursively calls deathtrap() and hangs. */
1512 sa.sa_flags = 0;
1513# else
1514 sa.sa_flags = SA_ONSTACK;
1515# endif
1516 sigaction(signal_info[i].sig, &sa, NULL);
1517#else
1518# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1519 struct sigvec sv;
1520
1521 /* Setup to use the alternate stack for the signal function. */
1522 sv.sv_handler = func_deadly;
1523 sv.sv_mask = 0;
1524 sv.sv_flags = SV_ONSTACK;
1525 sigvec(signal_info[i].sig, &sv, NULL);
1526# else
1527 signal(signal_info[i].sig, func_deadly);
1528# endif
1529#endif
1530 }
1531 else if (func_other != SIG_ERR)
1532 signal(signal_info[i].sig, func_other);
1533}
1534
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001535#ifdef HAVE_SIGPROCMASK
1536 static void
1537block_signals(sigset_t *set)
1538{
1539 sigset_t newset;
1540 int i;
1541
1542 sigemptyset(&newset);
1543
1544 for (i = 0; signal_info[i].sig != -1; i++)
1545 sigaddset(&newset, signal_info[i].sig);
1546
Bram Moolenaar2e310482018-08-21 13:09:10 +02001547# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001548 /* SIGCONT isn't in the list, because its default action is ignore */
1549 sigaddset(&newset, SIGCONT);
1550# endif
1551
1552 sigprocmask(SIG_BLOCK, &newset, set);
1553}
1554
1555 static void
1556unblock_signals(sigset_t *set)
1557{
1558 sigprocmask(SIG_SETMASK, set, NULL);
1559}
1560#endif
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001564 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1565 * return TRUE
1566 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1567 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001568 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 * Returns TRUE when Vim should exit.
1570 */
1571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001572vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001574 static int got_signal = 0;
1575 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001576
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001577 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001579 case SIGNAL_BLOCK: blocked = TRUE;
1580 break;
1581
1582 case SIGNAL_UNBLOCK: blocked = FALSE;
1583 if (got_signal != 0)
1584 {
1585 kill(getpid(), got_signal);
1586 got_signal = 0;
1587 }
1588 break;
1589
1590 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001591 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001592 got_signal = sig;
1593#ifdef SIGPWR
1594 if (sig != SIGPWR)
1595#endif
1596 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001597 break;
1598 }
1599 return FALSE;
1600}
1601
1602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * Check_win checks whether we have an interactive stdout.
1604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001606mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (isatty(1))
1609 return OK;
1610 return FAIL;
1611}
1612
1613/*
1614 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1615 */
1616 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001617mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 if (isatty(read_cmd_fd))
1620 return TRUE;
1621 return FALSE;
1622}
1623
1624#ifdef FEAT_X11
1625
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001626# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629/*
1630 * Give a message about the elapsed time for opening the X window.
1631 */
1632 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001633xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001635 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
1637# endif
1638#endif
1639
1640#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1641/*
1642 * A few functions shared by X11 title and clipboard code.
1643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645static int got_x_error = FALSE;
1646
1647/*
1648 * X Error handler, otherwise X just exits! (very rude) -- webb
1649 */
1650 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001651x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001653 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 STRCAT(IObuff, _("\nVim: Got X error\n"));
1655
1656 /* We cannot print a message and continue, because no X calls are allowed
1657 * here (causes my system to hang). Silently continuing might be an
1658 * alternative... */
1659 preserve_exit(); /* preserve files and exit */
1660
1661 return 0; /* NOTREACHED */
1662}
1663
1664/*
1665 * Another X Error handler, just used to check for errors.
1666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001668x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 got_x_error = TRUE;
1671 return 0;
1672}
1673
1674#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1675# if defined(HAVE_SETJMP_H)
1676/*
1677 * An X IO Error handler, used to catch error while opening the display.
1678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001680x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 /* This function should not return, it causes exit(). Longjump instead. */
1683 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001684# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001685 return 0; /* avoid the compiler complains about missing return value */
1686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687}
1688# endif
1689
1690/*
1691 * An X IO Error handler, used to catch terminal errors.
1692 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001693static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001696x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001699 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 x11_window = 0;
1701 x11_display = NULL;
1702 xterm_Shell = (Widget)0;
1703
1704 /* This function should not return, it causes exit(). Longjump instead. */
1705 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001706# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001707 return 0; /* avoid the compiler complains about missing return value */
1708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001710
1711/*
1712 * If the X11 connection was lost try to restore it.
1713 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001714 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001715 */
1716 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001717may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001718{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719 if (xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001721 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001722
1723# ifndef LESSTIF_VERSION
1724 /* This has been reported to avoid Vim getting stuck. */
1725 if (app_context != (XtAppContext)NULL)
1726 {
1727 XtDestroyApplicationContext(app_context);
1728 app_context = (XtAppContext)NULL;
1729 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1730 }
1731# endif
1732
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001733 setup_term_clip();
1734 get_x11_title(FALSE);
1735 }
1736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
1738
1739/*
1740 * Return TRUE when connection to the X server is desired.
1741 */
1742 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001743x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#if defined(FEAT_CLIENTSERVER)
1746 if (x_force_connect)
1747 return TRUE;
1748#endif
1749 if (x_no_connect)
1750 return FALSE;
1751
1752 /* Check for a match with "exclude:" from 'clipboard'. */
1753 if (clip_exclude_prog != NULL)
1754 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001755 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 return FALSE;
1757 }
1758 return TRUE;
1759}
1760
1761/*
1762 * Test if "dpy" and x11_window are valid by getting the window title.
1763 * I don't actually want it yet, so there may be a simpler call to use, but
1764 * this will cause the error handler x_error_check() to be called if anything
1765 * is wrong, such as the window pointer being invalid (as can happen when the
1766 * user changes his DISPLAY, but not his WINDOWID) -- webb
1767 */
1768 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001769test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 int (*old_handler)();
1772 XTextProperty text_prop;
1773
1774 old_handler = XSetErrorHandler(x_error_check);
1775 got_x_error = FALSE;
1776 if (XGetWMName(dpy, x11_window, &text_prop))
1777 XFree((void *)text_prop.value);
1778 XSync(dpy, False);
1779 (void)XSetErrorHandler(old_handler);
1780
1781 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001782 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 return (got_x_error ? FAIL : OK);
1785}
1786#endif
1787
1788#ifdef FEAT_TITLE
1789
1790#ifdef FEAT_X11
1791
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001792static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794/*
1795 * try to get x11 window and display
1796 *
1797 * return FAIL for failure, OK otherwise
1798 */
1799 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001800get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 char *winid;
1803 static int result = -1;
1804#define XD_NONE 0 /* x11_display not set here */
1805#define XD_HERE 1 /* x11_display opened here */
1806#define XD_GUI 2 /* x11_display used from gui.dpy */
1807#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1808 static int x11_display_from = XD_NONE;
1809 static int did_set_error_handler = FALSE;
1810
1811 if (!did_set_error_handler)
1812 {
1813 /* X just exits if it finds an error otherwise! */
1814 (void)XSetErrorHandler(x_error_handler);
1815 did_set_error_handler = TRUE;
1816 }
1817
Bram Moolenaar9372a112005-12-06 19:59:18 +00001818#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (gui.in_use)
1820 {
1821 /*
1822 * If the X11 display was opened here before, for the window where Vim
1823 * was started, close that one now to avoid a memory leak.
1824 */
1825 if (x11_display_from == XD_HERE && x11_display != NULL)
1826 {
1827 XCloseDisplay(x11_display);
1828 x11_display_from = XD_NONE;
1829 }
1830 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1831 {
1832 x11_display_from = XD_GUI;
1833 return OK;
1834 }
1835 x11_display = NULL;
1836 return FAIL;
1837 }
1838 else if (x11_display_from == XD_GUI)
1839 {
1840 /* GUI must have stopped somehow, clear x11_display */
1841 x11_window = 0;
1842 x11_display = NULL;
1843 x11_display_from = XD_NONE;
1844 }
1845#endif
1846
1847 /* When started with the "-X" argument, don't try connecting. */
1848 if (!x_connect_to_server())
1849 return FAIL;
1850
1851 /*
1852 * If WINDOWID not set, should try another method to find out
1853 * what the current window number is. The only code I know for
1854 * this is very complicated.
1855 * We assume that zero is invalid for WINDOWID.
1856 */
1857 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1858 x11_window = (Window)atol(winid);
1859
1860#ifdef FEAT_XCLIPBOARD
1861 if (xterm_dpy != NULL && x11_window != 0)
1862 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001863 /* We may have checked it already, but Gnome terminal can move us to
1864 * another window, so we need to check every time. */
1865 if (x11_display_from != XD_XTERM)
1866 {
1867 /*
1868 * If the X11 display was opened here before, for the window where
1869 * Vim was started, close that one now to avoid a memory leak.
1870 */
1871 if (x11_display_from == XD_HERE && x11_display != NULL)
1872 XCloseDisplay(x11_display);
1873 x11_display = xterm_dpy;
1874 x11_display_from = XD_XTERM;
1875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (test_x11_window(x11_display) == FAIL)
1877 {
1878 /* probably bad $WINDOWID */
1879 x11_window = 0;
1880 x11_display = NULL;
1881 x11_display_from = XD_NONE;
1882 return FAIL;
1883 }
1884 return OK;
1885 }
1886#endif
1887
1888 if (x11_window == 0 || x11_display == NULL)
1889 result = -1;
1890
1891 if (result != -1) /* Have already been here and set this */
1892 return result; /* Don't do all these X calls again */
1893
1894 if (x11_window != 0 && x11_display == NULL)
1895 {
1896#ifdef SET_SIG_ALARM
1897 RETSIGTYPE (*sig_save)();
1898#endif
1899#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1900 struct timeval start_tv;
1901
1902 if (p_verbose > 0)
1903 gettimeofday(&start_tv, NULL);
1904#endif
1905
1906#ifdef SET_SIG_ALARM
1907 /*
1908 * Opening the Display may hang if the DISPLAY setting is wrong, or
1909 * the network connection is bad. Set an alarm timer to get out.
1910 */
1911 sig_alarm_called = FALSE;
1912 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1913 (RETSIGTYPE (*)())sig_alarm);
1914 alarm(2);
1915#endif
1916 x11_display = XOpenDisplay(NULL);
1917
1918#ifdef SET_SIG_ALARM
1919 alarm(0);
1920 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1921 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001922 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924 if (x11_display != NULL)
1925 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001926# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001928 {
1929 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001930 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001931 verbose_leave();
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933# endif
1934 if (test_x11_window(x11_display) == FAIL)
1935 {
1936 /* Maybe window id is bad */
1937 x11_window = 0;
1938 XCloseDisplay(x11_display);
1939 x11_display = NULL;
1940 }
1941 else
1942 x11_display_from = XD_HERE;
1943 }
1944 }
1945 if (x11_window == 0 || x11_display == NULL)
1946 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001947
1948# ifdef FEAT_EVAL
1949 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1950# endif
1951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 return (result = OK);
1953}
1954
1955/*
1956 * Determine original x11 Window Title
1957 */
1958 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001959get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001961 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962}
1963
1964/*
1965 * Determine original x11 Window icon
1966 */
1967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001968get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 int retval = FALSE;
1971
1972 retval = get_x11_thing(FALSE, test_only);
1973
1974 /* could not get old icon, use terminal name */
1975 if (oldicon == NULL && !test_only)
1976 {
1977 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001978 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001980 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983 return retval;
1984}
1985
1986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001987get_x11_thing(
1988 int get_title, /* get title string */
1989 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 XTextProperty text_prop;
1992 int retval = FALSE;
1993 Status status;
1994
1995 if (get_x11_windis() == OK)
1996 {
1997 /* Get window/icon name if any */
1998 if (get_title)
1999 status = XGetWMName(x11_display, x11_window, &text_prop);
2000 else
2001 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2002
2003 /*
2004 * If terminal is xterm, then x11_window may be a child window of the
2005 * outer xterm window that actually contains the window/icon name, so
2006 * keep traversing up the tree until a window with a title/icon is
2007 * found.
2008 */
2009 /* Previously this was only done for xterm and alikes. I don't see a
2010 * reason why it would fail for other terminal emulators.
2011 * if (term_is_xterm) */
2012 {
2013 Window root;
2014 Window parent;
2015 Window win = x11_window;
2016 Window *children;
2017 unsigned int num_children;
2018
2019 while (!status || text_prop.value == NULL)
2020 {
2021 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2022 &num_children))
2023 break;
2024 if (children)
2025 XFree((void *)children);
2026 if (parent == root || parent == 0)
2027 break;
2028
2029 win = parent;
2030 if (get_title)
2031 status = XGetWMName(x11_display, win, &text_prop);
2032 else
2033 status = XGetWMIconName(x11_display, win, &text_prop);
2034 }
2035 }
2036 if (status && text_prop.value != NULL)
2037 {
2038 retval = TRUE;
2039 if (!test_only)
2040 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002041#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2042 if (text_prop.encoding == XA_STRING
2043# ifdef FEAT_MBYTE
2044 && !has_mbyte
2045# endif
2046 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
2048#endif
2049 if (get_title)
2050 oldtitle = vim_strsave((char_u *)text_prop.value);
2051 else
2052 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002053#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 else
2056 {
2057 char **cl;
2058 Status transform_status;
2059 int n = 0;
2060
2061 transform_status = XmbTextPropertyToTextList(x11_display,
2062 &text_prop,
2063 &cl, &n);
2064 if (transform_status >= Success && n > 0 && cl[0])
2065 {
2066 if (get_title)
2067 oldtitle = vim_strsave((char_u *) cl[0]);
2068 else
2069 oldicon = vim_strsave((char_u *) cl[0]);
2070 XFreeStringList(cl);
2071 }
2072 else
2073 {
2074 if (get_title)
2075 oldtitle = vim_strsave((char_u *)text_prop.value);
2076 else
2077 oldicon = vim_strsave((char_u *)text_prop.value);
2078 }
2079 }
2080#endif
2081 }
2082 XFree((void *)text_prop.value);
2083 }
2084 }
2085 return retval;
2086}
2087
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002088/* Xutf8 functions are not avaialble on older systems. Note that on some
2089 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2090 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2091 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002093# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094# define USE_UTF8_STRING
2095# endif
2096#endif
2097
2098/*
2099 * Set x11 Window Title
2100 *
2101 * get_x11_windis() must be called before this and have returned OK
2102 */
2103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002104set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105{
2106 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2107 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2108 * supported everywhere and STRING doesn't work for multi-byte titles.
2109 */
2110#ifdef USE_UTF8_STRING
2111 if (enc_utf8)
2112 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2113 NULL, NULL, 0, NULL, NULL, NULL);
2114 else
2115#endif
2116 {
2117#if XtSpecificationRelease >= 4
2118# ifdef FEAT_XFONTSET
2119 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2120 NULL, NULL, 0, NULL, NULL, NULL);
2121# else
2122 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002123 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002126 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 XSetWMProperties(x11_display, x11_window, &text_prop,
2128 NULL, NULL, 0, NULL, NULL, NULL);
2129# endif
2130#else
2131 XStoreName(x11_display, x11_window, (char *)title);
2132#endif
2133 }
2134 XFlush(x11_display);
2135}
2136
2137/*
2138 * Set x11 Window icon
2139 *
2140 * get_x11_windis() must be called before this and have returned OK
2141 */
2142 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002143set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 /* See above for comments about using X*SetWMProperties(). */
2146#ifdef USE_UTF8_STRING
2147 if (enc_utf8)
2148 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2149 NULL, 0, NULL, NULL, NULL);
2150 else
2151#endif
2152 {
2153#if XtSpecificationRelease >= 4
2154# ifdef FEAT_XFONTSET
2155 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2156 NULL, 0, NULL, NULL, NULL);
2157# else
2158 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002159 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002161 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2163 NULL, 0, NULL, NULL, NULL);
2164# endif
2165#else
2166 XSetIconName(x11_display, x11_window, (char *)icon);
2167#endif
2168 }
2169 XFlush(x11_display);
2170}
2171
2172#else /* FEAT_X11 */
2173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002175get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 return FALSE;
2178}
2179
2180 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002181get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 if (!test_only)
2184 {
2185 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002186 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002188 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190 return FALSE;
2191}
2192
2193#endif /* FEAT_X11 */
2194
2195 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002196mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 return get_x11_title(TRUE);
2199}
2200
2201 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002202mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 return get_x11_icon(TRUE);
2205}
2206
2207/*
2208 * Set the window title and icon.
2209 */
2210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002211mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 int type = 0;
2214 static int recursive = 0;
2215
2216 if (T_NAME == NULL) /* no terminal name (yet) */
2217 return;
2218 if (title == NULL && icon == NULL) /* nothing to do */
2219 return;
2220
2221 /* When one of the X11 functions causes a deadly signal, we get here again
2222 * recursively. Avoid hanging then (something is probably locked). */
2223 if (recursive)
2224 return;
2225 ++recursive;
2226
2227 /*
2228 * if the window ID and the display is known, we may use X11 calls
2229 */
2230#ifdef FEAT_X11
2231 if (get_x11_windis() == OK)
2232 type = 1;
2233#else
2234# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2235 if (gui.in_use)
2236 type = 1;
2237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239
2240 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002241 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 * than x11 calls, because the x11 calls don't always work
2243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if ((type || *T_TS != NUL) && title != NULL)
2245 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002246 if (oldtitle_outdated)
2247 {
2248 oldtitle_outdated = FALSE;
2249 VIM_CLEAR(oldtitle);
2250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 if (oldtitle == NULL
2252#ifdef FEAT_GUI
2253 && !gui.in_use
2254#endif
2255 ) /* first call but not in GUI, save title */
2256 (void)get_x11_title(FALSE);
2257
2258 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2259 term_settitle(title);
2260#ifdef FEAT_X11
2261 else
2262# ifdef FEAT_GUI_GTK
2263 if (!gui.in_use) /* don't do this if GTK+ is running */
2264# endif
2265 set_x11_title(title); /* x11 */
2266#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002267#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2269 else
2270 gui_mch_settitle(title, icon);
2271#endif
2272 did_set_title = TRUE;
2273 }
2274
2275 if ((type || *T_CIS != NUL) && icon != NULL)
2276 {
2277 if (oldicon == NULL
2278#ifdef FEAT_GUI
2279 && !gui.in_use
2280#endif
2281 ) /* first call, save icon */
2282 get_x11_icon(FALSE);
2283
2284 if (*T_CIS != NUL)
2285 {
2286 out_str(T_CIS); /* set icon start */
2287 out_str_nf(icon);
2288 out_str(T_CIE); /* set icon end */
2289 out_flush();
2290 }
2291#ifdef FEAT_X11
2292 else
2293# ifdef FEAT_GUI_GTK
2294 if (!gui.in_use) /* don't do this if GTK+ is running */
2295# endif
2296 set_x11_icon(icon); /* x11 */
2297#endif
2298 did_set_icon = TRUE;
2299 }
2300 --recursive;
2301}
2302
2303/*
2304 * Restore the window/icon title.
2305 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002306 * SAVE_RESTORE_TITLE only restore title
2307 * SAVE_RESTORE_ICON only restore icon
2308 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 */
2310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002311mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002314 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002316 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2317
2318 // pop and push from/to the stack
2319 term_pop_title(which);
2320 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321}
2322
2323#endif /* FEAT_TITLE */
2324
2325/*
2326 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002327 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
2329 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002330vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
2332 if (name == NULL)
2333 return FALSE;
2334 return (STRNICMP(name, "xterm", 5) == 0
2335 || STRNICMP(name, "nxterm", 6) == 0
2336 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002337 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002339 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 || STRCMP(name, "builtin_xterm") == 0);
2341}
2342
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002343#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2344/*
2345 * Return TRUE if "name" appears to be that of a terminal
2346 * known to support the xterm-style mouse protocol.
2347 * Relies on term_is_xterm having been set to its correct value.
2348 */
2349 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002350use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002351{
2352 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002353 && (term_is_xterm
2354 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002355 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002356 || STRICMP(name, "st") == 0
2357 || STRNICMP(name, "st-", 3) == 0
2358 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002359}
2360#endif
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2363/*
2364 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2365 * Return 1 for "xterm".
2366 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002367 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002368 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 */
2370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002371use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002373 if (ttym_flags == TTYM_SGR)
2374 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002375 if (ttym_flags == TTYM_URXVT)
2376 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (ttym_flags == TTYM_XTERM2)
2378 return 2;
2379 if (ttym_flags == TTYM_XTERM)
2380 return 1;
2381 return 0;
2382}
2383#endif
2384
2385 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002386vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 if (name == NULL)
2389 return FALSE;
2390 return (STRNICMP(name, "iris-ansi", 9) == 0
2391 || STRCMP(name, "builtin_iris-ansi") == 0);
2392}
2393
2394 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002395vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 if (name == NULL)
2398 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002399 /* catch VT100 - VT5xx */
2400 return ((STRNICMP(name, "vt", 2) == 0
2401 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 || STRCMP(name, "builtin_vt320") == 0);
2403}
2404
2405/*
2406 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2407 * This should include all windowed terminal emulators.
2408 */
2409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002410vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 if (name == NULL)
2413 return FALSE;
2414 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2415 return TRUE;
2416 return ( STRNICMP(name, "hpterm", 6) == 0
2417 || STRNICMP(name, "sun-cmd", 7) == 0
2418 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002419 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 || STRNICMP(name, "dtterm", 6) == 0);
2421}
2422
2423/*
2424 * Insert user name in s[len].
2425 * Return OK if a name found.
2426 */
2427 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002431 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 return OK;
2433#else
2434 return mch_get_uname(getuid(), s, len);
2435#endif
2436}
2437
2438/*
2439 * Insert user name for "uid" in s[len].
2440 * Return OK if a name found.
2441 */
2442 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002443mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2446 struct passwd *pw;
2447
2448 if ((pw = getpwuid(uid)) != NULL
2449 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2450 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002451 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return OK;
2453 }
2454#endif
2455 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2456 return FAIL; /* a number is not a name */
2457}
2458
2459/*
2460 * Insert host name is s[len].
2461 */
2462
2463#ifdef HAVE_SYS_UTSNAME_H
2464 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002465mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 struct utsname vutsname;
2468
2469 if (uname(&vutsname) < 0)
2470 *s = NUL;
2471 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002472 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474#else /* HAVE_SYS_UTSNAME_H */
2475
2476# ifdef HAVE_SYS_SYSTEMINFO_H
2477# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2478# endif
2479
2480 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002481mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483# ifdef VAXC
2484 vaxc$gethostname((char *)s, len);
2485# else
2486 gethostname((char *)s, len);
2487# endif
2488 s[len - 1] = NUL; /* make sure it's terminated */
2489}
2490#endif /* HAVE_SYS_UTSNAME_H */
2491
2492/*
2493 * return process ID
2494 */
2495 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002496mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 return (long)getpid();
2499}
2500
2501#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002503strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 extern int sys_nerr;
2506 extern char *sys_errlist[];
2507 static char er[20];
2508
2509 if (err > 0 && err < sys_nerr)
2510 return (sys_errlist[err]);
2511 sprintf(er, "Error %d", err);
2512 return er;
2513}
2514#endif
2515
2516/*
2517 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2518 * Return OK for success, FAIL for failure.
2519 */
2520 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002521mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523#if defined(USE_GETCWD)
2524 if (getcwd((char *)buf, len) == NULL)
2525 {
2526 STRCPY(buf, strerror(errno));
2527 return FAIL;
2528 }
2529 return OK;
2530#else
2531 return (getwd((char *)buf) != NULL ? OK : FAIL);
2532#endif
2533}
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002536 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 *
2538 * return FAIL for failure, OK for success
2539 */
2540 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002541mch_FullName(
2542 char_u *fname,
2543 char_u *buf,
2544 int len,
2545 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002548#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 int fd = -1;
2550 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 char_u olddir[MAXPATHL];
2553 char_u *p;
2554 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002555#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002556 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2557 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002558#endif
2559
Bram Moolenaar38323e42007-03-06 19:22:53 +00002560#ifdef VMS
2561 fname = vms_fixfilename(fname);
2562#endif
2563
Bram Moolenaara2442432007-04-26 14:26:37 +00002564#ifdef __CYGWIN__
2565 /*
2566 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2567 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002568# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002569 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2570 * a forward slash. */
2571 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2572 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002573# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002574 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002575# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002576 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002577#endif
2578
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002579 /* Expand it if forced or not an absolute path.
2580 * Do not do it for "/file", the result is always "/". */
2581 if ((force || !mch_isFullName(fname))
2582 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
2584 /*
2585 * If the file name has a path, change to that directory for a moment,
2586 * and then do the getwd() (and get back to where we were).
2587 * This will get the correct path name with "../" things.
2588 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002589 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002591#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /*
2593 * Use fchdir() if possible, it's said to be faster and more
2594 * reliable. But on SunOS 4 it might not work. Check this by
2595 * doing a fchdir() right now.
2596 */
2597 if (!dont_fchdir)
2598 {
2599 fd = open(".", O_RDONLY | O_EXTRA, 0);
2600 if (fd >= 0 && fchdir(fd) < 0)
2601 {
2602 close(fd);
2603 fd = -1;
2604 dont_fchdir = TRUE; /* don't try again */
2605 }
2606 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 /* Only change directory when we are sure we can return to where
2610 * we are now. After doing "su" chdir(".") might not work. */
2611 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002612#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 (mch_dirname(olddir, MAXPATHL) == FAIL
2616 || mch_chdir((char *)olddir) != 0))
2617 {
2618 p = NULL; /* can't get current dir: don't chdir */
2619 retval = FAIL;
2620 }
2621 else
2622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 /* The directory is copied into buf[], to be able to remove
2624 * the file name without changing it (could be a string in
2625 * read-only memory) */
2626 if (p - fname >= len)
2627 retval = FAIL;
2628 else
2629 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002630 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (mch_chdir((char *)buf))
2632 retval = FAIL;
2633 else
2634 fname = p + 1;
2635 *buf = NUL;
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 }
2639 if (mch_dirname(buf, len) == FAIL)
2640 {
2641 retval = FAIL;
2642 *buf = NUL;
2643 }
2644 if (p != NULL)
2645 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002646#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (fd >= 0)
2648 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002649 if (p_verbose >= 5)
2650 {
2651 verbose_enter();
2652 MSG("fchdir() to previous dir");
2653 verbose_leave();
2654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 l = fchdir(fd);
2656 close(fd);
2657 }
2658 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 l = mch_chdir((char *)olddir);
2661 if (l != 0)
2662 EMSG(_(e_prev_dir));
2663 }
2664
2665 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002666 if (l >= len - 1)
2667 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002668#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002669 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002671 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002676 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return FAIL;
2678
2679 /* Do not append ".", "/dir/." is equal to "/dir". */
2680 if (STRCMP(fname, ".") != 0)
2681 STRCAT(buf, fname);
2682
2683 return OK;
2684}
2685
2686/*
2687 * Return TRUE if "fname" does not depend on the current directory.
2688 */
2689 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002690mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002692#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 return ( fname[0] == '/' || fname[0] == '.' ||
2694 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2695 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2696 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002697#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699#endif
2700}
2701
Bram Moolenaar24552be2005-12-10 20:17:30 +00002702#if defined(USE_FNAME_CASE) || defined(PROTO)
2703/*
2704 * Set the case of the file name, if it already exists. This will cause the
2705 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002706 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002707 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002708 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002709fname_case(
2710 char_u *name,
2711 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002712{
2713 struct stat st;
2714 char_u *slash, *tail;
2715 DIR *dirp;
2716 struct dirent *dp;
2717
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002718 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002719 {
2720 /* Open the directory where the file is located. */
2721 slash = vim_strrchr(name, '/');
2722 if (slash == NULL)
2723 {
2724 dirp = opendir(".");
2725 tail = name;
2726 }
2727 else
2728 {
2729 *slash = NUL;
2730 dirp = opendir((char *)name);
2731 *slash = '/';
2732 tail = slash + 1;
2733 }
2734
2735 if (dirp != NULL)
2736 {
2737 while ((dp = readdir(dirp)) != NULL)
2738 {
2739 /* Only accept names that differ in case and are the same byte
2740 * length. TODO: accept different length name. */
2741 if (STRICMP(tail, dp->d_name) == 0
2742 && STRLEN(tail) == STRLEN(dp->d_name))
2743 {
2744 char_u newname[MAXPATHL + 1];
2745 struct stat st2;
2746
2747 /* Verify the inode is equal. */
2748 vim_strncpy(newname, name, MAXPATHL);
2749 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2750 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002751 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002752 && st.st_ino == st2.st_ino
2753 && st.st_dev == st2.st_dev)
2754 {
2755 STRCPY(tail, dp->d_name);
2756 break;
2757 }
2758 }
2759 }
2760
2761 closedir(dirp);
2762 }
2763 }
2764}
2765#endif
2766
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767/*
2768 * Get file permissions for 'name'.
2769 * Returns -1 when it doesn't exist.
2770 */
2771 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002772mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774 struct stat statb;
2775
2776 /* Keep the #ifdef outside of stat(), it may be a macro. */
2777#ifdef VMS
2778 if (stat((char *)vms_fixfilename(name), &statb))
2779#else
2780 if (stat((char *)name, &statb))
2781#endif
2782 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002783#ifdef __INTERIX
2784 /* The top bit makes the value negative, which means the file doesn't
2785 * exist. Remove the bit, we don't use it. */
2786 return statb.st_mode & ~S_ADDACE;
2787#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
2792/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002793 * Set file permission for "name" to "perm".
2794 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 */
2796 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002797mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 return (chmod((char *)
2800#ifdef VMS
2801 vms_fixfilename(name),
2802#else
2803 name,
2804#endif
2805 (mode_t)perm) == 0 ? OK : FAIL);
2806}
2807
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002808#if defined(HAVE_FCHMOD) || defined(PROTO)
2809/*
2810 * Set file permission for open file "fd" to "perm".
2811 * Return FAIL for failure, OK otherwise.
2812 */
2813 int
2814mch_fsetperm(int fd, long perm)
2815{
2816 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2817}
2818#endif
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820#if defined(HAVE_ACL) || defined(PROTO)
2821# ifdef HAVE_SYS_ACL_H
2822# include <sys/acl.h>
2823# endif
2824# ifdef HAVE_SYS_ACCESS_H
2825# include <sys/access.h>
2826# endif
2827
2828# ifdef HAVE_SOLARIS_ACL
2829typedef struct vim_acl_solaris_T {
2830 int acl_cnt;
2831 aclent_t *acl_entry;
2832} vim_acl_solaris_T;
2833# endif
2834
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002835#if defined(HAVE_SELINUX) || defined(PROTO)
2836/*
2837 * Copy security info from "from_file" to "to_file".
2838 */
2839 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002840mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002841{
2842 if (from_file == NULL)
2843 return;
2844
2845 if (selinux_enabled == -1)
2846 selinux_enabled = is_selinux_enabled();
2847
2848 if (selinux_enabled > 0)
2849 {
2850 security_context_t from_context = NULL;
2851 security_context_t to_context = NULL;
2852
2853 if (getfilecon((char *)from_file, &from_context) < 0)
2854 {
2855 /* If the filesystem doesn't support extended attributes,
2856 the original had no special security context and the
2857 target cannot have one either. */
2858 if (errno == EOPNOTSUPP)
2859 return;
2860
2861 MSG_PUTS(_("\nCould not get security context for "));
2862 msg_outtrans(from_file);
2863 msg_putchar('\n');
2864 return;
2865 }
2866 if (getfilecon((char *)to_file, &to_context) < 0)
2867 {
2868 MSG_PUTS(_("\nCould not get security context for "));
2869 msg_outtrans(to_file);
2870 msg_putchar('\n');
2871 freecon (from_context);
2872 return ;
2873 }
2874 if (strcmp(from_context, to_context) != 0)
2875 {
2876 if (setfilecon((char *)to_file, from_context) < 0)
2877 {
2878 MSG_PUTS(_("\nCould not set security context for "));
2879 msg_outtrans(to_file);
2880 msg_putchar('\n');
2881 }
2882 }
2883 freecon(to_context);
2884 freecon(from_context);
2885 }
2886}
2887#endif /* HAVE_SELINUX */
2888
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002889#if defined(HAVE_SMACK) && !defined(PROTO)
2890/*
2891 * Copy security info from "from_file" to "to_file".
2892 */
2893 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002894mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002895{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002896 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002897 {
2898 XATTR_NAME_SMACK,
2899 XATTR_NAME_SMACKEXEC,
2900 XATTR_NAME_SMACKMMAP
2901 };
2902
2903 char buffer[SMACK_LABEL_LEN];
2904 const char *name;
2905 int index;
2906 int ret;
2907 ssize_t size;
2908
2909 if (from_file == NULL)
2910 return;
2911
2912 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2913 / sizeof(smack_copied_attributes)[0]) ; index++)
2914 {
2915 /* get the name of the attribute to copy */
2916 name = smack_copied_attributes[index];
2917
2918 /* get the value of the attribute in buffer */
2919 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2920 if (size >= 0)
2921 {
2922 /* copy the attribute value of buffer */
2923 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2924 if (ret < 0)
2925 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002926 vim_snprintf((char *)IObuff, IOSIZE,
2927 _("Could not set security context %s for %s"),
2928 name, to_file);
2929 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002930 msg_putchar('\n');
2931 }
2932 }
2933 else
2934 {
2935 /* what reason of not having the attribute value? */
2936 switch (errno)
2937 {
2938 case ENOTSUP:
2939 /* extended attributes aren't supported or enabled */
2940 /* should a message be echoed? not sure... */
2941 return; /* leave because it isn't usefull to continue */
2942
2943 case ERANGE:
2944 default:
2945 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002946 vim_snprintf((char *)IObuff, IOSIZE,
2947 _("Could not get security context %s for %s. Removing it!"),
2948 name, from_file);
2949 msg_puts(IObuff);
2950 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002951 /* FALLTHROUGH to remove the attribute */
2952
2953 case ENODATA:
2954 /* no attribute of this name */
2955 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002956 /* Silently ignore errors, apparently this happens when
2957 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002958 break;
2959 }
2960 }
2961 }
2962}
2963#endif /* HAVE_SMACK */
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965/*
2966 * Return a pointer to the ACL of file "fname" in allocated memory.
2967 * Return NULL if the ACL is not available for whatever reason.
2968 */
2969 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002970mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 vim_acl_T ret = NULL;
2973#ifdef HAVE_POSIX_ACL
2974 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2975#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002976#ifdef HAVE_SOLARIS_ZFS_ACL
2977 acl_t *aclent;
2978
2979 if (acl_get((char *)fname, 0, &aclent) < 0)
2980 return NULL;
2981 ret = (vim_acl_T)aclent;
2982#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983#ifdef HAVE_SOLARIS_ACL
2984 vim_acl_solaris_T *aclent;
2985
2986 aclent = malloc(sizeof(vim_acl_solaris_T));
2987 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2988 {
2989 free(aclent);
2990 return NULL;
2991 }
2992 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2993 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2994 {
2995 free(aclent->acl_entry);
2996 free(aclent);
2997 return NULL;
2998 }
2999 ret = (vim_acl_T)aclent;
3000#else
3001#if defined(HAVE_AIX_ACL)
3002 int aclsize;
3003 struct acl *aclent;
3004
3005 aclsize = sizeof(struct acl);
3006 aclent = malloc(aclsize);
3007 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3008 {
3009 if (errno == ENOSPC)
3010 {
3011 aclsize = aclent->acl_len;
3012 aclent = realloc(aclent, aclsize);
3013 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3014 {
3015 free(aclent);
3016 return NULL;
3017 }
3018 }
3019 else
3020 {
3021 free(aclent);
3022 return NULL;
3023 }
3024 }
3025 ret = (vim_acl_T)aclent;
3026#endif /* HAVE_AIX_ACL */
3027#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003028#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029#endif /* HAVE_POSIX_ACL */
3030 return ret;
3031}
3032
3033/*
3034 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3035 */
3036 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003037mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 if (aclent == NULL)
3040 return;
3041#ifdef HAVE_POSIX_ACL
3042 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3043#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003044#ifdef HAVE_SOLARIS_ZFS_ACL
3045 acl_set((char *)fname, (acl_t *)aclent);
3046#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#ifdef HAVE_SOLARIS_ACL
3048 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3049 ((vim_acl_solaris_T *)aclent)->acl_entry);
3050#else
3051#ifdef HAVE_AIX_ACL
3052 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3053#endif /* HAVE_AIX_ACL */
3054#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003055#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056#endif /* HAVE_POSIX_ACL */
3057}
3058
3059 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003060mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 if (aclent == NULL)
3063 return;
3064#ifdef HAVE_POSIX_ACL
3065 acl_free((acl_t)aclent);
3066#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003067#ifdef HAVE_SOLARIS_ZFS_ACL
3068 acl_free((acl_t *)aclent);
3069#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070#ifdef HAVE_SOLARIS_ACL
3071 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3072 free(aclent);
3073#else
3074#ifdef HAVE_AIX_ACL
3075 free(aclent);
3076#endif /* HAVE_AIX_ACL */
3077#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003078#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079#endif /* HAVE_POSIX_ACL */
3080}
3081#endif
3082
3083/*
3084 * Set hidden flag for "name".
3085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003087mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 /* can't hide a file */
3090}
3091
3092/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003093 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 * return FALSE if "name" is not a directory
3095 * return FALSE for error
3096 */
3097 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003098mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 struct stat statb;
3101
3102 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3103 return FALSE;
3104 if (stat((char *)name, &statb))
3105 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107}
3108
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003109/*
3110 * return TRUE if "name" is a directory, NOT a symlink to a directory
3111 * return FALSE if "name" is not a directory
3112 * return FALSE for error
3113 */
3114 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003115mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003116{
3117 struct stat statb;
3118
3119 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3120 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003121 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003122 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003123 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003124}
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126/*
3127 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3128 */
3129 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003130executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 struct stat st;
3133
3134 if (stat((char *)name, &st))
3135 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003136#ifdef VMS
3137 /* Like on Unix system file can have executable rights but not necessarily
3138 * be an executable, but on Unix is not a default for an ordianry file to
3139 * have an executable flag - on VMS it is in most cases.
3140 * Therefore, this check does not have any sense - let keep us to the
3141 * conventions instead:
3142 * *.COM and *.EXE files are the executables - the rest are not. This is
3143 * not ideal but better then it was.
3144 */
3145 int vms_executable = 0;
3146 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3147 {
3148 if (strstr(vms_tolower((char*)name),".exe") != NULL
3149 || strstr(vms_tolower((char*)name),".com")!= NULL)
3150 vms_executable = 1;
3151 }
3152 return vms_executable;
3153#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003159 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003160 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * Return -1 if unknown.
3162 */
3163 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003164mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 char_u *buf;
3167 char_u *p, *e;
3168 int retval;
3169
Bram Moolenaarb5971142015-03-21 17:32:19 +01003170 /* When "use_path" is false and if it's an absolute or relative path don't
3171 * need to use $PATH. */
3172 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3173 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003174 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003175 /* There must be a path separator, files in the current directory
3176 * can't be executed. */
3177 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003178 {
3179 if (path != NULL)
3180 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003181 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003182 *path = FullName_save(name, TRUE);
3183 else
3184 *path = vim_strsave(name);
3185 }
3186 return TRUE;
3187 }
3188 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191 p = (char_u *)getenv("PATH");
3192 if (p == NULL || *p == NUL)
3193 return -1;
3194 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3195 if (buf == NULL)
3196 return -1;
3197
3198 /*
3199 * Walk through all entries in $PATH to check if "name" exists there and
3200 * is an executable file.
3201 */
3202 for (;;)
3203 {
3204 e = (char_u *)strchr((char *)p, ':');
3205 if (e == NULL)
3206 e = p + STRLEN(p);
3207 if (e - p <= 1) /* empty entry means current dir */
3208 STRCPY(buf, "./");
3209 else
3210 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003211 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 add_pathsep(buf);
3213 }
3214 STRCAT(buf, name);
3215 retval = executable_file(buf);
3216 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003217 {
3218 if (path != NULL)
3219 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003220 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003221 *path = FullName_save(buf, TRUE);
3222 else
3223 *path = vim_strsave(buf);
3224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 if (*e != ':')
3229 break;
3230 p = e + 1;
3231 }
3232
3233 vim_free(buf);
3234 return retval;
3235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237/*
3238 * Check what "name" is:
3239 * NODE_NORMAL: file or directory (or doesn't exist)
3240 * NODE_WRITABLE: writable device, socket, fifo, etc.
3241 * NODE_OTHER: non-writable things
3242 */
3243 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003244mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 struct stat st;
3247
3248 if (stat((char *)name, &st))
3249 return NODE_NORMAL;
3250 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3251 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3253 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 /* Everything else is writable? */
3255 return NODE_WRITABLE;
3256}
3257
3258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003259mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261#ifdef HAVE_CHECK_STACK_GROWTH
3262 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 check_stack_growth((char *)&i);
3265
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003266# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 get_stack_limit();
3268# endif
3269
3270#endif
3271
3272 /*
3273 * Setup an alternative stack for signals. Helps to catch signals when
3274 * running out of stack space.
3275 * Use of sigaltstack() is preferred, it's more portable.
3276 * Ignore any errors.
3277 */
3278#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003279 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 init_signal_stack();
3281#endif
3282}
3283
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003284#if defined(EXITFREE) || defined(PROTO)
3285 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003286mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003287{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003288# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3289 if (clip_star.owned)
3290 clip_lose_selection(&clip_star);
3291 if (clip_plus.owned)
3292 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003293# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003294# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003295 if (xterm_Shell != (Widget)0)
3296 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003297# ifndef LESSTIF_VERSION
3298 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003299 if (xterm_dpy != NULL)
3300 XtCloseDisplay(xterm_dpy);
3301 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003302 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003303 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003304# ifdef FEAT_X11
3305 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3306# endif
3307 }
3308# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003309# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003310# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003311 if (x11_display != NULL
3312# ifdef FEAT_XCLIPBOARD
3313 && x11_display != xterm_dpy
3314# endif
3315 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 XCloseDisplay(x11_display);
3317# endif
3318# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003319 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320# endif
3321# ifdef FEAT_TITLE
3322 vim_free(oldtitle);
3323 vim_free(oldicon);
3324# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003325}
3326#endif
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328/*
3329 * Output a newline when exiting.
3330 * Make sure the newline goes to the same stream as the text.
3331 */
3332 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003333exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003335 if (silent_mode)
3336 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (newline_on_exit || msg_didout)
3338 {
3339 if (msg_use_printf())
3340 {
3341 if (info_message)
3342 mch_msg("\n");
3343 else
3344 mch_errmsg("\r\n");
3345 }
3346 else
3347 out_char('\n');
3348 }
3349 else
3350 {
3351 restore_cterm_colors(); /* get original colors back */
3352 msg_clr_eos_force(); /* clear the rest of the display */
3353 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3354 }
3355}
3356
3357 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003358mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 exiting = TRUE;
3361
3362#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3363 x11_export_final_selection();
3364#endif
3365
3366#ifdef FEAT_GUI
3367 if (!gui.in_use)
3368#endif
3369 {
3370 settmode(TMODE_COOK);
3371#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003372 // restore xterm title and icon name
3373 mch_restore_title(SAVE_RESTORE_BOTH);
3374 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
3376 /*
3377 * When t_ti is not empty but it doesn't cause swapping terminal
3378 * pages, need to output a newline when msg_didout is set. But when
3379 * t_ti does swap pages it should not go to the shell page. Do this
3380 * before stoptermcap().
3381 */
3382 if (swapping_screen() && !newline_on_exit)
3383 exit_scroll();
3384
3385 /* Stop termcap: May need to check for T_CRV response, which
3386 * requires RAW mode. */
3387 stoptermcap();
3388
3389 /*
3390 * A newline is only required after a message in the alternate screen.
3391 * This is set to TRUE by wait_return().
3392 */
3393 if (!swapping_screen() || newline_on_exit)
3394 exit_scroll();
3395
3396 /* Cursor may have been switched off without calling starttermcap()
3397 * when doing "vim -u vimrc" and vimrc contains ":q". */
3398 if (full_screen)
3399 cursor_on();
3400 }
3401 out_flush();
3402 ml_close_all(TRUE); /* remove all memfiles */
3403 may_core_dump();
3404#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 gui_exit(r);
3407#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003408
Bram Moolenaar56718732006-03-15 22:53:57 +00003409#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003410 mac_conv_cleanup();
3411#endif
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#ifdef __QNX__
3414 /* A core dump won't be created if the signal handler
3415 * doesn't return, so we can't call exit() */
3416 if (deadly_signal != 0)
3417 return;
3418#endif
3419
Bram Moolenaar009b2592004-10-24 19:18:58 +00003420#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003421 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003422#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003423
3424#ifdef EXITFREE
3425 free_all_mem();
3426#endif
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 exit(r);
3429}
3430
3431 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003432may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 if (deadly_signal != 0)
3435 {
3436 signal(deadly_signal, SIG_DFL);
3437 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3438 }
3439}
3440
3441#ifndef VMS
3442
3443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003444mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 static int first = TRUE;
3447
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003448#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449# ifdef HAVE_TERMIOS_H
3450 static struct termios told;
3451 struct termios tnew;
3452# else
3453 static struct termio told;
3454 struct termio tnew;
3455# endif
3456
3457 if (first)
3458 {
3459 first = FALSE;
3460# if defined(HAVE_TERMIOS_H)
3461 tcgetattr(read_cmd_fd, &told);
3462# else
3463 ioctl(read_cmd_fd, TCGETA, &told);
3464# endif
3465 }
3466
3467 tnew = told;
3468 if (tmode == TMODE_RAW)
3469 {
3470 /*
3471 * ~ICRNL enables typing ^V^M
3472 */
3473 tnew.c_iflag &= ~ICRNL;
3474 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3475# if defined(IEXTEN) && !defined(__MINT__)
3476 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3477 /* but it breaks function keys on MINT */
3478# endif
3479 );
3480# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3481 tnew.c_oflag &= ~ONLCR;
3482# endif
3483 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3484 tnew.c_cc[VTIME] = 0; /* don't wait */
3485 }
3486 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003487 {
3488 /* Also reset ICANON here, otherwise on Solaris select() won't see
3489 * typeahead characters. */
3490 tnew.c_lflag &= ~(ICANON | ECHO);
3491 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3492 tnew.c_cc[VTIME] = 0; /* don't wait */
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495# if defined(HAVE_TERMIOS_H)
3496 {
3497 int n = 10;
3498
3499 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3500 * few times. */
3501 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3502 && errno == EINTR && n > 0)
3503 --n;
3504 }
3505# else
3506 ioctl(read_cmd_fd, TCSETA, &tnew);
3507# endif
3508
3509#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /*
3511 * for "old" tty systems
3512 */
3513# ifndef TIOCSETN
3514# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3515# endif
3516 static struct sgttyb ttybold;
3517 struct sgttyb ttybnew;
3518
3519 if (first)
3520 {
3521 first = FALSE;
3522 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3523 }
3524
3525 ttybnew = ttybold;
3526 if (tmode == TMODE_RAW)
3527 {
3528 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3529 ttybnew.sg_flags |= RAW;
3530 }
3531 else if (tmode == TMODE_SLEEP)
3532 ttybnew.sg_flags &= ~(ECHO);
3533 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3534#endif
3535 curr_tmode = tmode;
3536}
3537
3538/*
3539 * Try to get the code for "t_kb" from the stty setting
3540 *
3541 * Even if termcap claims a backspace key, the user's setting *should*
3542 * prevail. stty knows more about reality than termcap does, and if
3543 * somebody's usual erase key is DEL (which, for most BSD users, it will
3544 * be), they're going to get really annoyed if their erase key starts
3545 * doing forward deletes for no reason. (Eric Fischer)
3546 */
3547 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003548get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003550 ttyinfo_T info;
3551 char_u buf[2];
3552 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003554 if (get_tty_info(read_cmd_fd, &info) == OK)
3555 {
3556 intr_char = info.interrupt;
3557 buf[0] = info.backspace;
3558 buf[1] = NUL;
3559 add_termcode((char_u *)"kb", buf, FALSE);
3560
3561 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3562 p = find_termcode((char_u *)"kD");
3563 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3564 do_fixdel(NULL);
3565 }
3566}
3567
3568/*
3569 * Obtain the characters that Backspace and Enter produce on "fd".
3570 * Returns OK or FAIL.
3571 */
3572 int
3573get_tty_info(int fd, ttyinfo_T *info)
3574{
3575#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576# ifdef HAVE_TERMIOS_H
3577 struct termios keys;
3578# else
3579 struct termio keys;
3580# endif
3581
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003582 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003584 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003586 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003588 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003590 info->backspace = keys.c_cc[VERASE];
3591 info->interrupt = keys.c_cc[VINTR];
3592 if (keys.c_iflag & ICRNL)
3593 info->enter = NL;
3594 else
3595 info->enter = CAR;
3596 if (keys.c_oflag & ONLCR)
3597 info->nl_does_cr = TRUE;
3598 else
3599 info->nl_does_cr = FALSE;
3600 return OK;
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#else
3603 /* for "old" tty systems */
3604 struct sgttyb keys;
3605
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003606 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003608 info->backspace = keys.sg_erase;
3609 info->interrupt = keys.sg_kill;
3610 info->enter = CAR;
3611 info->nl_does_cr = TRUE;
3612 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616}
3617
3618#endif /* VMS */
3619
3620#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003621static int mouse_ison = FALSE;
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
3624 * Set mouse clicks on or off.
3625 */
3626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003627mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003629# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003630 static int bevalterm_ison = FALSE;
3631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 int xterm_mouse_vers;
3633
Bram Moolenaara06afc72018-08-27 23:24:16 +02003634# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3635 if (!on)
3636 // Make sure not tracing mouse movements. Important when a button-down
3637 // was received but no release yet.
3638 stop_xterm_trace();
3639# endif
3640
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003641 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003642# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003643 && p_bevalterm == bevalterm_ison
3644# endif
3645 )
3646 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 return;
3648
3649 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003650
3651# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003652 if (ttym_flags == TTYM_URXVT)
3653 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003654 out_str_nf((char_u *)
3655 (on
3656 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3657 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003658 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003659 }
3660# endif
3661
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003662# ifdef FEAT_MOUSE_SGR
3663 if (ttym_flags == TTYM_SGR)
3664 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003665 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003666 out_str_nf((char_u *)
3667 (on
3668 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3669 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003670 mouse_ison = on;
3671 }
3672# endif
3673
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003674# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003675 if (bevalterm_ison != (p_bevalterm && on))
3676 {
3677 bevalterm_ison = (p_bevalterm && on);
3678 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3679 /* disable mouse movement events, enabling is below */
3680 out_str_nf((char_u *)
3681 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003682 }
3683# endif
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (xterm_mouse_vers > 0)
3686 {
3687 if (on) /* enable mouse events, use mouse tracking if available */
3688 out_str_nf((char_u *)
3689 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003691# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003692 bevalterm_ison
3693 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3694# endif
3695 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3697 else /* disable mouse events, could probably always send the same */
3698 out_str_nf((char_u *)
3699 (xterm_mouse_vers > 1
3700 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3701 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704
3705# ifdef FEAT_MOUSE_DEC
3706 else if (ttym_flags == TTYM_DEC)
3707 {
3708 if (on) /* enable mouse events */
3709 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3710 else /* disable mouse events */
3711 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003712 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714# endif
3715
3716# ifdef FEAT_MOUSE_GPM
3717 else
3718 {
3719 if (on)
3720 {
3721 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003722 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724 else
3725 {
3726 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 }
3730# endif
3731
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003732# ifdef FEAT_SYSMOUSE
3733 else
3734 {
3735 if (on)
3736 {
3737 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003738 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003739 }
3740 else
3741 {
3742 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003743 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003744 }
3745 }
3746# endif
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748# ifdef FEAT_MOUSE_JSB
3749 else
3750 {
3751 if (on)
3752 {
3753 /* D - Enable Mouse up/down messages
3754 * L - Enable Left Button Reporting
3755 * M - Enable Middle Button Reporting
3756 * R - Enable Right Button Reporting
3757 * K - Enable SHIFT and CTRL key Reporting
3758 * + - Enable Advanced messaging of mouse moves and up/down messages
3759 * Q - Quiet No Ack
3760 * # - Numeric value of mouse pointer required
3761 * 0 = Multiview 2000 cursor, used as standard
3762 * 1 = Windows Arrow
3763 * 2 = Windows I Beam
3764 * 3 = Windows Hour Glass
3765 * 4 = Windows Cross Hair
3766 * 5 = Windows UP Arrow
3767 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003768# ifdef JSBTERM_MOUSE_NONADVANCED
3769 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3771 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003772# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3774 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003775# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 else
3779 {
3780 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3781 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003782 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785# endif
3786# ifdef FEAT_MOUSE_PTERM
3787 else
3788 {
3789 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3790 if (on)
3791 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3792 else
3793 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003794 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796# endif
3797}
3798
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003799#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003800/*
3801 * Called when 'balloonevalterm' changed.
3802 */
3803 void
3804mch_bevalterm_changed(void)
3805{
3806 mch_setmouse(mouse_ison);
3807}
3808#endif
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810/*
3811 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3812 */
3813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003814check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816# ifdef FEAT_MOUSE_XTERM
3817 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003818# ifdef FEAT_MOUSE_URXVT
3819 && use_xterm_mouse() != 3
3820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821# ifdef FEAT_GUI
3822 && !gui.in_use
3823# endif
3824 )
3825 {
3826 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003827 ? IF_EB("\233M", CSI_STR "M")
3828 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (*p_mouse != NUL)
3830 {
3831 /* force mouse off and maybe on to send possibly new mouse
3832 * activation sequence to the xterm, with(out) drag tracing. */
3833 mch_setmouse(FALSE);
3834 setmouse();
3835 }
3836 }
3837 else
3838 del_mouse_termcode(KS_MOUSE);
3839# endif
3840
3841# ifdef FEAT_MOUSE_GPM
3842 if (!use_xterm_mouse()
3843# ifdef FEAT_GUI
3844 && !gui.in_use
3845# endif
3846 )
3847 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3848# endif
3849
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003850# ifdef FEAT_SYSMOUSE
3851 if (!use_xterm_mouse()
3852# ifdef FEAT_GUI
3853 && !gui.in_use
3854# endif
3855 )
3856 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3857# endif
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003860 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (!use_xterm_mouse()
3862# ifdef FEAT_GUI
3863 && !gui.in_use
3864# endif
3865 )
3866 set_mouse_termcode(KS_JSBTERM_MOUSE,
3867 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3868 else
3869 del_mouse_termcode(KS_JSBTERM_MOUSE);
3870# endif
3871
3872# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003873 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * define it in the GUI or when using an xterm. */
3875 if (!use_xterm_mouse()
3876# ifdef FEAT_GUI
3877 && !gui.in_use
3878# endif
3879 )
3880 set_mouse_termcode(KS_NETTERM_MOUSE,
3881 (char_u *)IF_EB("\033}", ESC_STR "}"));
3882 else
3883 del_mouse_termcode(KS_NETTERM_MOUSE);
3884# endif
3885
3886# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003887 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003888 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889# ifdef FEAT_GUI
3890 && !gui.in_use
3891# endif
3892 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003893 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3894 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else
3896 del_mouse_termcode(KS_DEC_MOUSE);
3897# endif
3898# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003899 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003900 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901# ifdef FEAT_GUI
3902 && !gui.in_use
3903# endif
3904 )
3905 set_mouse_termcode(KS_PTERM_MOUSE,
3906 (char_u *) IF_EB("\033[", ESC_STR "["));
3907 else
3908 del_mouse_termcode(KS_PTERM_MOUSE);
3909# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003910# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003911 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003912# ifdef FEAT_GUI
3913 && !gui.in_use
3914# endif
3915 )
3916 {
3917 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003918 ? IF_EB("\233*M", CSI_STR "*M")
3919 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003920
3921 if (*p_mouse != NUL)
3922 {
3923 mch_setmouse(FALSE);
3924 setmouse();
3925 }
3926 }
3927 else
3928 del_mouse_termcode(KS_URXVT_MOUSE);
3929# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003930# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003931 if (use_xterm_mouse() == 4
3932# ifdef FEAT_GUI
3933 && !gui.in_use
3934# endif
3935 )
3936 {
3937 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003938 ? IF_EB("\233<*M", CSI_STR "<*M")
3939 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3940
3941 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3942 ? IF_EB("\233<*m", CSI_STR "<*m")
3943 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003944
3945 if (*p_mouse != NUL)
3946 {
3947 mch_setmouse(FALSE);
3948 setmouse();
3949 }
3950 }
3951 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003952 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003953 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003954 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3955 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957}
3958#endif
3959
3960/*
3961 * set screen mode, always fails.
3962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003964mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
3966 EMSG(_(e_screenmode));
3967 return FAIL;
3968}
3969
3970#ifndef VMS
3971
3972/*
3973 * Try to get the current window size:
3974 * 1. with an ioctl(), most accurate method
3975 * 2. from the environment variables LINES and COLUMNS
3976 * 3. from the termcap
3977 * 4. keep using the old values
3978 * Return OK when size could be determined, FAIL otherwise.
3979 */
3980 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003981mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
3983 long rows = 0;
3984 long columns = 0;
3985 char_u *p;
3986
3987 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * 1. try using an ioctl. It is the most accurate method.
3989 *
3990 * Try using TIOCGWINSZ first, some systems that have it also define
3991 * TIOCGSIZE but don't have a struct ttysize.
3992 */
3993# ifdef TIOCGWINSZ
3994 {
3995 struct winsize ws;
3996 int fd = 1;
3997
3998 /* When stdout is not a tty, use stdin for the ioctl(). */
3999 if (!isatty(fd) && isatty(read_cmd_fd))
4000 fd = read_cmd_fd;
4001 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4002 {
4003 columns = ws.ws_col;
4004 rows = ws.ws_row;
4005 }
4006 }
4007# else /* TIOCGWINSZ */
4008# ifdef TIOCGSIZE
4009 {
4010 struct ttysize ts;
4011 int fd = 1;
4012
4013 /* When stdout is not a tty, use stdin for the ioctl(). */
4014 if (!isatty(fd) && isatty(read_cmd_fd))
4015 fd = read_cmd_fd;
4016 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4017 {
4018 columns = ts.ts_cols;
4019 rows = ts.ts_lines;
4020 }
4021 }
4022# endif /* TIOCGSIZE */
4023# endif /* TIOCGWINSZ */
4024
4025 /*
4026 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004027 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4028 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004030 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
4032 if ((p = (char_u *)getenv("LINES")))
4033 rows = atoi((char *)p);
4034 if ((p = (char_u *)getenv("COLUMNS")))
4035 columns = atoi((char *)p);
4036 }
4037
4038#ifdef HAVE_TGETENT
4039 /*
4040 * 3. try reading "co" and "li" entries from termcap
4041 */
4042 if (columns == 0 || rows == 0)
4043 getlinecol(&columns, &rows);
4044#endif
4045
4046 /*
4047 * 4. If everything fails, use the old values
4048 */
4049 if (columns <= 0 || rows <= 0)
4050 return FAIL;
4051
4052 Rows = rows;
4053 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004054 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return OK;
4056}
4057
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004058#if defined(FEAT_TERMINAL) || defined(PROTO)
4059/*
4060 * Report the windows size "rows" and "cols" to tty "fd".
4061 */
4062 int
4063mch_report_winsize(int fd, int rows, int cols)
4064{
4065# ifdef TIOCSWINSZ
4066 struct winsize ws;
4067
4068 ws.ws_col = cols;
4069 ws.ws_row = rows;
4070 ws.ws_xpixel = cols * 5;
4071 ws.ws_ypixel = rows * 10;
4072 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4073 {
4074 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4075 return OK;
4076 }
4077 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4078# else
4079# ifdef TIOCSSIZE
4080 struct ttysize ts;
4081
4082 ts.ts_cols = cols;
4083 ts.ts_lines = rows;
4084 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4085 {
4086 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4087 return OK;
4088 }
4089 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4090# endif
4091# endif
4092 return FAIL;
4093}
4094#endif
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096/*
4097 * Try to set the window size to Rows and Columns.
4098 */
4099 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004100mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 if (*T_CWS)
4103 {
4104 /*
4105 * NOTE: if you get an error here that term_set_winsize() is
4106 * undefined, check the output of configure. It could probably not
4107 * find a ncurses, termcap or termlib library.
4108 */
4109 term_set_winsize((int)Rows, (int)Columns);
4110 out_flush();
4111 screen_start(); /* don't know where cursor is now */
4112 }
4113}
4114
4115#endif /* VMS */
4116
4117/*
4118 * Rows and/or Columns has changed.
4119 */
4120 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004121mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 /* Nothing to do. */
4124}
4125
Bram Moolenaar205b8862011-09-07 15:04:31 +02004126/*
4127 * Wait for process "child" to end.
4128 * Return "child" if it exited properly, <= 0 on error.
4129 */
4130 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004131wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004132{
4133 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004134 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004135
4136 while (wait_pid != child)
4137 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004138 /* When compiled with Python threads are probably used, in which case
4139 * wait() sometimes hangs for no obvious reason. Use waitpid()
4140 * instead and loop (like the GUI). Also needed for other interfaces,
4141 * they might call system(). */
4142# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004143 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004144# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004145 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004146# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004147 if (wait_pid == 0)
4148 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004149 /* Wait for 1 to 10 msec before trying again. */
4150 mch_delay(delay_msec, TRUE);
4151 if (++delay_msec > 10)
4152 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004153 continue;
4154 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004155 if (wait_pid <= 0
4156# ifdef ECHILD
4157 && errno == ECHILD
4158# endif
4159 )
4160 break;
4161 }
4162 return wait_pid;
4163}
4164
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004165#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004166/*
4167 * Set the environment for a child process.
4168 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004169 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004170set_child_environment(
4171 long rows,
4172 long columns,
4173 char *term,
4174 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004175{
4176# ifdef HAVE_SETENV
4177 char envbuf[50];
4178# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004179 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004180 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004181 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004182 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004183 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004184# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004185 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004186# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004187# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004188 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004189# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004190# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004191 long colors =
4192# ifdef FEAT_GUI
4193 gui.in_use ? 256*256*256 :
4194# endif
4195 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004196
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004197# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004198 setenv("TERM", term, 1);
4199 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004200 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004201 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004203 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004204 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004205 sprintf((char *)envbuf, "%ld", colors);
4206 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004207# ifdef FEAT_TERMINAL
4208 if (is_terminal)
4209 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004210 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004211 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4212 }
4213# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004214# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004215 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004216# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004217# else
4218 /*
4219 * Putenv does not copy the string, it has to remain valid.
4220 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004221 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004223 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4224 putenv(envbuf_Term);
4225 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004226 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004227 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4228 putenv(envbuf_Lines);
4229 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4230 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004231 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004232 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4233 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004234# ifdef FEAT_TERMINAL
4235 if (is_terminal)
4236 {
4237 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004238 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004239 putenv(envbuf_Version);
4240 }
4241# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004242# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004243 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4244 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4245 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004246# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247# endif
4248}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004249
4250 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004251set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004252{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004253 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004255#endif
4256
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004257#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004258/*
4259 * Open a PTY, with FD for the master and slave side.
4260 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4261 * When successful both file descriptors are stored.
4262 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004263 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004264open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004265{
4266 char *tty_name;
4267
4268 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4269 if (*pty_master_fd >= 0)
4270 {
4271 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4272 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4273 * adding O_NOCTTY always works when defined. */
4274#ifdef O_NOCTTY
4275 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4276#else
4277 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4278#endif
4279 if (*pty_slave_fd < 0)
4280 {
4281 close(*pty_master_fd);
4282 *pty_master_fd = -1;
4283 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004284 else if (namep != NULL)
4285 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004286 }
4287}
4288#endif
4289
Bram Moolenaarfae42832017-08-01 22:24:26 +02004290/*
4291 * Send SIGINT to a child process if "c" is an interrupt character.
4292 */
4293 void
4294may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4295{
4296# ifdef SIGINT
4297 if (c == Ctrl_C || c == intr_char)
4298 {
4299# ifdef HAVE_SETSID
4300 kill(-pid, SIGINT);
4301# else
4302 kill(0, SIGINT);
4303# endif
4304 if (wpid > 0)
4305 kill(wpid, SIGINT);
4306 }
4307# endif
4308}
4309
Bram Moolenaar13568252018-03-16 20:46:58 +01004310#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4311
4312 static int
4313build_argv(
4314 char_u *cmd,
4315 char ***argvp,
4316 char_u **sh_tofree,
4317 char_u **shcf_tofree)
4318{
4319 char **argv = NULL;
4320 int argc;
4321
4322 *sh_tofree = vim_strsave(p_sh);
4323 if (*sh_tofree == NULL) /* out of memory */
4324 return FAIL;
4325
4326 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4327 return FAIL;
4328 *argvp = argv;
4329
4330 if (cmd != NULL)
4331 {
4332 char_u *s;
4333 char_u *p;
4334
4335 if (extra_shell_arg != NULL)
4336 argv[argc++] = (char *)extra_shell_arg;
4337
4338 /* Break 'shellcmdflag' into white separated parts. This doesn't
4339 * handle quoted strings, they are very unlikely to appear. */
4340 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4341 if (*shcf_tofree == NULL) /* out of memory */
4342 return FAIL;
4343 s = *shcf_tofree;
4344 p = p_shcf;
4345 while (*p != NUL)
4346 {
4347 argv[argc++] = (char *)s;
4348 while (*p && *p != ' ' && *p != TAB)
4349 *s++ = *p++;
4350 *s++ = NUL;
4351 p = skipwhite(p);
4352 }
4353
4354 argv[argc++] = (char *)cmd;
4355 }
4356 argv[argc] = NULL;
4357 return OK;
4358}
4359#endif
4360
4361#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4362/*
4363 * Use a terminal window to run a shell command in.
4364 */
4365 static int
4366mch_call_shell_terminal(
4367 char_u *cmd,
4368 int options UNUSED) /* SHELL_*, see vim.h */
4369{
4370 jobopt_T opt;
4371 char **argv = NULL;
4372 char_u *tofree1 = NULL;
4373 char_u *tofree2 = NULL;
4374 int retval = -1;
4375 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004376 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004377 aco_save_T aco;
4378 oparg_T oa; /* operator arguments */
4379
4380 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4381 goto theend;
4382
4383 init_job_options(&opt);
4384 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4385 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004386 if (buf == NULL)
4387 goto theend;
4388
4389 job = term_getjob(buf->b_term);
4390 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004391
4392 /* Find a window to make "buf" curbuf. */
4393 aucmd_prepbuf(&aco, buf);
4394
4395 clear_oparg(&oa);
4396 while (term_use_loop())
4397 {
4398 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4399 {
4400 /* If terminal_loop() returns OK we got a key that is handled
4401 * in Normal model. We don't do redrawing anyway. */
4402 if (terminal_loop(TRUE) == OK)
4403 normal_cmd(&oa, TRUE);
4404 }
4405 else
4406 normal_cmd(&oa, TRUE);
4407 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004408 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004409 ch_log(NULL, "system command finished");
4410
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004411 job_unref(job);
4412
Bram Moolenaar13568252018-03-16 20:46:58 +01004413 /* restore curwin/curbuf and a few other things */
4414 aucmd_restbuf(&aco);
4415
4416 wait_return(TRUE);
4417 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4418
4419theend:
4420 vim_free(argv);
4421 vim_free(tofree1);
4422 vim_free(tofree2);
4423 return retval;
4424}
4425#endif
4426
4427#ifdef USE_SYSTEM
4428/*
4429 * Use system() to start the shell: simple but slow.
4430 */
4431 static int
4432mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004433 char_u *cmd,
4434 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
4436#ifdef VMS
4437 char *ifn = NULL;
4438 char *ofn = NULL;
4439#endif
4440 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004441 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004442 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 out_flush();
4445
4446 if (options & SHELL_COOKED)
4447 settmode(TMODE_COOK); /* set to normal mode */
4448
Bram Moolenaar62b42182010-09-21 22:09:37 +02004449# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004450 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004451 loose_clipboard();
4452# endif
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (cmd == NULL)
4455 x = system((char *)p_sh);
4456 else
4457 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004458# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (ofn = strchr((char *)cmd, '>'))
4460 *ofn++ = '\0';
4461 if (ifn = strchr((char *)cmd, '<'))
4462 {
4463 char *p;
4464
4465 *ifn++ = '\0';
4466 p = strchr(ifn,' '); /* chop off any trailing spaces */
4467 if (p)
4468 *p = '\0';
4469 }
4470 if (ofn)
4471 x = vms_sys((char *)cmd, ofn, ifn);
4472 else
4473 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004474# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 newcmd = lalloc(STRLEN(p_sh)
4476 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4477 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4478 if (newcmd == NULL)
4479 x = 0;
4480 else
4481 {
4482 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4483 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4484 (char *)p_shcf,
4485 (char *)cmd);
4486 x = system((char *)newcmd);
4487 vim_free(newcmd);
4488 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491# ifdef VMS
4492 x = vms_sys_status(x);
4493# endif
4494 if (emsg_silent)
4495 ;
4496 else if (x == 127)
4497 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else if (x && !(options & SHELL_SILENT))
4499 {
4500 MSG_PUTS(_("\nshell returned "));
4501 msg_outnum((long)x);
4502 msg_putchar('\n');
4503 }
4504
4505 if (tmode == TMODE_RAW)
4506 settmode(TMODE_RAW); /* set to raw mode */
4507# ifdef FEAT_TITLE
4508 resettitle();
4509# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004510# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4511 restore_clipboard();
4512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004514}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar13568252018-03-16 20:46:58 +01004516#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
Bram Moolenaardf177f62005-02-22 08:39:57 +00004518# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4519 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004520# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar13568252018-03-16 20:46:58 +01004522/*
4523 * Don't use system(), use fork()/exec().
4524 */
4525 static int
4526mch_call_shell_fork(
4527 char_u *cmd,
4528 int options) /* SHELL_*, see vim.h */
4529{
4530 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004532 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 pid_t wait_pid = 0;
4534# ifdef HAVE_UNION_WAIT
4535 union wait status;
4536# else
4537 int status = -1;
4538# endif
4539 int retval = -1;
4540 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004541 char_u *tofree1 = NULL;
4542 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004545# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004547# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004548 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 int fd_fromshell[2];
4550 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004551 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 out_flush();
4554 if (options & SHELL_COOKED)
4555 settmode(TMODE_COOK); /* set to normal mode */
4556
Bram Moolenaar13568252018-03-16 20:46:58 +01004557 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004558 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004561 * For the GUI, when writing the output into the buffer and when reading
4562 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4563 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565 if ((options & (SHELL_READ|SHELL_WRITE))
4566# ifdef FEAT_GUI
4567 || (gui.in_use && show_shell_mess)
4568# endif
4569 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004571# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 /*
4573 * Try to open a master pty.
4574 * If this works, open the slave pty.
4575 * If the slave can't be opened, close the master pty.
4576 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004578 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 /*
4580 * If not opening a pty or it didn't work, try using pipes.
4581 */
4582 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 pipe_error = (pipe(fd_toshell) < 0);
4586 if (!pipe_error) /* pipe create OK */
4587 {
4588 pipe_error = (pipe(fd_fromshell) < 0);
4589 if (pipe_error) /* pipe create failed */
4590 {
4591 close(fd_toshell[0]);
4592 close(fd_toshell[1]);
4593 }
4594 }
4595 if (pipe_error)
4596 {
4597 MSG_PUTS(_("\nCannot create pipes\n"));
4598 out_flush();
4599 }
4600 }
4601 }
4602
4603 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004605 SIGSET_DECL(curset)
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607# ifdef __BEOS__
4608 beos_cleanup_read_thread();
4609# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004610
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004611 BLOCK_SIGNALS(&curset);
4612 pid = fork(); /* maybe we should use vfork() */
4613 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004615 UNBLOCK_SIGNALS(&curset);
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004618 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004620 || (gui.in_use && show_shell_mess)
4621# endif
4622 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004624# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (pty_master_fd >= 0) /* close the pseudo tty */
4626 {
4627 close(pty_master_fd);
4628 close(pty_slave_fd);
4629 }
4630 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
4633 close(fd_toshell[0]);
4634 close(fd_toshell[1]);
4635 close(fd_fromshell[0]);
4636 close(fd_fromshell[1]);
4637 }
4638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 else if (pid == 0) /* child */
4641 {
4642 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004643 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaar819524702018-02-27 19:10:00 +01004645# ifdef FEAT_JOB_CHANNEL
4646 if (ch_log_active())
4647 /* close the log file in the child */
4648 ch_logfile((char_u *)"", (char_u *)"");
4649# endif
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (!show_shell_mess || (options & SHELL_EXPAND))
4652 {
4653 int fd;
4654
4655 /*
4656 * Don't want to show any message from the shell. Can't just
4657 * close stdout and stderr though, because some systems will
4658 * break if you try to write to them after that, so we must
4659 * use dup() to replace them with something else -- webb
4660 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4661 * waiting for input.
4662 */
4663 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4664 fclose(stdin);
4665 fclose(stdout);
4666 fclose(stderr);
4667
4668 /*
4669 * If any of these open()'s and dup()'s fail, we just continue
4670 * anyway. It's not fatal, and on most systems it will make
4671 * no difference at all. On a few it will cause the execvp()
4672 * to exit with a non-zero status even when the completion
4673 * could be done, which is nothing too serious. If the open()
4674 * or dup() failed we'd just do the same thing ourselves
4675 * anyway -- webb
4676 */
4677 if (fd >= 0)
4678 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02004679 vim_ignored = dup(fd); /* To replace stdin (fd 0) */
4680 vim_ignored = dup(fd); /* To replace stdout (fd 1) */
4681 vim_ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 /* Don't need this now that we've duplicated it */
4684 close(fd);
4685 }
4686 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004687 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004689 || gui.in_use
4690# endif
4691 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693
Bram Moolenaardf177f62005-02-22 08:39:57 +00004694# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004695 /* Create our own process group, so that the child and all its
4696 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004697 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004698 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004699 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004700 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004701# if defined(SIGHUP)
4702 /* When doing "!xterm&" and 'shell' is bash: the shell
4703 * will exit and send SIGHUP to all processes in its
4704 * group, killing the just started process. Ignore SIGHUP
4705 * to avoid that. (suggested by Simon Schubert)
4706 */
4707 signal(SIGHUP, SIG_IGN);
4708# endif
4709 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004710# endif
4711# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004712 if (pty_slave_fd >= 0)
4713 {
4714 /* push stream discipline modules */
4715 if (options & SHELL_COOKED)
4716 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004718 /* Try to become controlling tty (probably doesn't work,
4719 * unless run by root) */
4720 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004722 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004723# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004724 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaara5792f52005-11-23 21:25:05 +00004726 /*
4727 * stderr is only redirected when using the GUI, so that a
4728 * program like gpg can still access the terminal to get a
4729 * passphrase using stderr.
4730 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004731# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (pty_master_fd >= 0)
4733 {
4734 close(pty_master_fd); /* close master side of pty */
4735
4736 /* set up stdin/stdout/stderr for the child */
4737 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004738 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004740 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004741 if (gui.in_use)
4742 {
4743 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004744 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 close(pty_slave_fd); /* has been dupped, close it now */
4748 }
4749 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 /* set up stdin for the child */
4753 close(fd_toshell[1]);
4754 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004755 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 close(fd_toshell[0]);
4757
4758 /* set up stdout for the child */
4759 close(fd_fromshell[0]);
4760 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004761 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 close(fd_fromshell[1]);
4763
Bram Moolenaara5792f52005-11-23 21:25:05 +00004764# ifdef FEAT_GUI
4765 if (gui.in_use)
4766 {
4767 /* set up stderr for the child */
4768 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004769 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004770 }
4771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 /*
4776 * There is no type cast for the argv, because the type may be
4777 * different on different machines. This may cause a warning
4778 * message with strict compilers, don't worry about it.
4779 * Call _exit() instead of exit() to avoid closing the connection
4780 * to the X server (esp. with GTK, which uses atexit()).
4781 */
4782 execvp(argv[0], argv);
4783 _exit(EXEC_FAILED); /* exec failed, return failure code */
4784 }
4785 else /* parent */
4786 {
4787 /*
4788 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 */
4791 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004792 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004793 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004794# ifdef FEAT_JOB_CHANNEL
4795 ++dont_check_job_ended;
4796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 /*
4798 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004799 * This is also used to pipe stdin/stdout to/from the external
4800 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004802 if ((options & (SHELL_READ|SHELL_WRITE))
4803# ifdef FEAT_GUI
4804 || (gui.in_use && show_shell_mess)
4805# endif
4806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004810# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4814 int ta_len = 0; /* valid bytes in ta_buf[] */
4815 int len;
4816 int p_more_save;
4817 int old_State;
4818 int c;
4819 int toshell_fd;
4820 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004821 garray_T ga;
4822 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004823# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4824 struct timeval start_tv;
4825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaardf177f62005-02-22 08:39:57 +00004827# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (pty_master_fd >= 0)
4829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 fromshell_fd = pty_master_fd;
4831 toshell_fd = dup(pty_master_fd);
4832 }
4833 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 close(fd_toshell[0]);
4837 close(fd_fromshell[1]);
4838 toshell_fd = fd_toshell[1];
4839 fromshell_fd = fd_fromshell[0];
4840 }
4841
4842 /*
4843 * Write to the child if there are typed characters.
4844 * Read from the child if there are characters available.
4845 * Repeat the reading a few times if more characters are
4846 * available. Need to check for typed keys now and then, but
4847 * not too often (delays when no chars are available).
4848 * This loop is quit if no characters can be read from the pty
4849 * (WaitForChar detected special condition), or there are no
4850 * characters available and the child has exited.
4851 * Only check if the child has exited when there is no more
4852 * output. The child may exit before all the output has
4853 * been printed.
4854 *
4855 * Currently this busy loops!
4856 * This can probably dead-lock when the write blocks!
4857 */
4858 p_more_save = p_more;
4859 p_more = FALSE;
4860 old_State = State;
4861 State = EXTERNCMD; /* don't redraw at window resize */
4862
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004863 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004864 {
4865 /* Fork a process that will write the lines to the
4866 * external program. */
4867 if ((wpid = fork()) == -1)
4868 {
4869 MSG_PUTS(_("\nCannot fork\n"));
4870 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004871 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004872 {
4873 linenr_T lnum = curbuf->b_op_start.lnum;
4874 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004875 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004876 size_t l;
4877
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004878 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004879 for (;;)
4880 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004881 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004882 if (l == 0)
4883 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004884 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 /* NL -> NUL translation */
4886 len = write(toshell_fd, "", (size_t)1);
4887 else
4888 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004889 char_u *s = vim_strchr(lp + written, NL);
4890
Bram Moolenaar89d40322006-08-29 15:30:07 +00004891 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004892 s == NULL ? l
4893 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004895 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004896 {
4897 /* Finished a line, add a NL, unless this line
4898 * should not have one. */
4899 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004900 || (!curbuf->b_p_bin
4901 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004902 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004903 && (lnum !=
4904 curbuf->b_ml.ml_line_count
4905 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004906 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004907 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 ++lnum;
4909 if (lnum > curbuf->b_op_end.lnum)
4910 {
4911 /* finished all the lines, close pipe */
4912 close(toshell_fd);
4913 toshell_fd = -1;
4914 break;
4915 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004916 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917 written = 0;
4918 }
4919 else if (len > 0)
4920 written += len;
4921 }
4922 _exit(0);
4923 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004924 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004925 {
4926 close(toshell_fd);
4927 toshell_fd = -1;
4928 }
4929 }
4930
4931 if (options & SHELL_READ)
4932 ga_init2(&ga, 1, BUFLEN);
4933
4934 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004935# ifdef ELAPSED_FUNC
4936 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 for (;;)
4939 {
4940 /*
4941 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004942 * if there are any.
4943 * Don't do this if we are expanding wild cards (would eat
4944 * typeahead).
4945 * Don't do this when filtering and terminal is in cooked
4946 * mode, the shell command will handle the I/O. Avoids
4947 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004948 * Don't get characters when the child has already
4949 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004951 * while (noread_cnt > 4), avoids that ":r !ls" eats
4952 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 */
4954 len = 0;
4955 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004956 && ((options &
4957 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4958 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004959# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004960 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004961# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004962 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004963 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004964 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004966 if (ta_len == 0)
4967 {
4968 /* Get extra characters when we don't have any.
4969 * Reset the counter and timer. */
4970 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004971# ifdef ELAPSED_FUNC
4972 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004973# endif
4974 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4975 }
4976 if (ta_len > 0 || len > 0)
4977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /*
4979 * For pipes:
4980 * Check for CTRL-C: send interrupt signal to child.
4981 * Check for CTRL-D: EOF, close pipe to child.
4982 */
4983 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 /*
4986 * Send SIGINT to the child's group or all
4987 * processes in our group.
4988 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004989 may_send_sigint(ta_buf[ta_len], pid, wpid);
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (pty_master_fd < 0 && toshell_fd >= 0
4992 && ta_buf[ta_len] == Ctrl_D)
4993 {
4994 close(toshell_fd);
4995 toshell_fd = -1;
4996 }
4997 }
4998
4999 /* replace K_BS by <BS> and K_DEL by <DEL> */
5000 for (i = ta_len; i < ta_len + len; ++i)
5001 {
5002 if (ta_buf[i] == CSI && len - i > 2)
5003 {
5004 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5005 if (c == K_DEL || c == K_KDEL || c == K_BS)
5006 {
5007 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5008 (size_t)(len - i - 2));
5009 if (c == K_DEL || c == K_KDEL)
5010 ta_buf[i] = DEL;
5011 else
5012 ta_buf[i] = Ctrl_H;
5013 len -= 2;
5014 }
5015 }
5016 else if (ta_buf[i] == '\r')
5017 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005018# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005020 i += (*mb_ptr2len_len)(ta_buf + i,
5021 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024
5025 /*
5026 * For pipes: echo the typed characters.
5027 * For a pty this does not seem to work.
5028 */
5029 if (pty_master_fd < 0)
5030 {
5031 for (i = ta_len; i < ta_len + len; ++i)
5032 {
5033 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5034 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005035# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else if (has_mbyte)
5037 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005038 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040 msg_outtrans_len(ta_buf + i, l);
5041 i += l - 1;
5042 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else
5045 msg_outtrans_len(ta_buf + i, 1);
5046 }
5047 windgoto(msg_row, msg_col);
5048 out_flush();
5049 }
5050
5051 ta_len += len;
5052
5053 /*
5054 * Write the characters to the child, unless EOF has
5055 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005056 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057 * When writing buffer lines, drop the typed
5058 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005060 if (options & SHELL_WRITE)
5061 ta_len = 0;
5062 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5065 if (len > 0)
5066 {
5067 ta_len -= len;
5068 mch_memmove(ta_buf, ta_buf + len, ta_len);
5069 }
5070 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073
Bram Moolenaardf177f62005-02-22 08:39:57 +00005074 if (got_int)
5075 {
5076 /* CTRL-C sends a signal to the child, we ignore it
5077 * ourselves */
5078# ifdef HAVE_SETSID
5079 kill(-pid, SIGINT);
5080# else
5081 kill(0, SIGINT);
5082# endif
5083 if (wpid > 0)
5084 kill(wpid, SIGINT);
5085 got_int = FALSE;
5086 }
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 /*
5089 * Check if the child has any characters to be printed.
5090 * Read them and write them to our window. Repeat this as
5091 * long as there is something to do, avoid the 10ms wait
5092 * for mch_inchar(), or sending typeahead characters to
5093 * the external process.
5094 * TODO: This should handle escape sequences, compatible
5095 * to some terminal (vt52?).
5096 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005097 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005098 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005100 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005103# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 );
5107 if (len <= 0) /* end of file or error */
5108 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005109
5110 noread_cnt = 0;
5111 if (options & SHELL_READ)
5112 {
5113 /* Do NUL -> NL translation, append NL separated
5114 * lines to the current buffer. */
5115 for (i = 0; i < len; ++i)
5116 {
5117 if (buffer[i] == NL)
5118 append_ga_line(&ga);
5119 else if (buffer[i] == NUL)
5120 ga_append(&ga, NL);
5121 else
5122 ga_append(&ga, buffer[i]);
5123 }
5124 }
5125# ifdef FEAT_MBYTE
5126 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005129 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
Bram Moolenaardf177f62005-02-22 08:39:57 +00005131 len += buffer_off;
5132 buffer[len] = NUL;
5133
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 /* Check if the last character in buffer[] is
5135 * incomplete, keep these bytes for the next
5136 * round. */
5137 for (p = buffer; p < buffer + len; p += l)
5138 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005139 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (l == 0)
5141 l = 1; /* NUL byte? */
5142 else if (MB_BYTE2LEN(*p) != l)
5143 break;
5144 }
5145 if (p == buffer) /* no complete character */
5146 {
5147 /* avoid getting stuck at an illegal byte */
5148 if (len >= 12)
5149 ++p;
5150 else
5151 {
5152 buffer_off = len;
5153 continue;
5154 }
5155 }
5156 c = *p;
5157 *p = NUL;
5158 msg_puts(buffer);
5159 if (p < buffer + len)
5160 {
5161 *p = c;
5162 buffer_off = (buffer + len) - p;
5163 mch_memmove(buffer, p, buffer_off);
5164 continue;
5165 }
5166 buffer_off = 0;
5167 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005168# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 buffer[len] = NUL;
5172 msg_puts(buffer);
5173 }
5174
5175 windgoto(msg_row, msg_col);
5176 cursor_on();
5177 out_flush();
5178 if (got_int)
5179 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005180
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005181# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005182 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005183 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005184 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005185
5186 /* Avoid that we keep looping here without
5187 * checking for a CTRL-C for a long time. Don't
5188 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005189 if (msec > 2000)
5190 {
5191 noread_cnt = 5;
5192 break;
5193 }
5194 }
5195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005198 /* If we already detected the child has finished, continue
5199 * reading output for a short while. Some text may be
5200 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005201 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005202 {
5203 if (noread_cnt < 5)
5204 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005205 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005206 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 /*
5209 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005210 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005212# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005213 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005214# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5218 || (wait_pid == pid && WIFEXITED(status)))
5219 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005220 /* Don't break the loop yet, try reading more
5221 * characters from "fromshell_fd" first. When using
5222 * pipes there might still be something to read and
5223 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005226 else
5227 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005228
Bram Moolenaar95a51352013-03-21 22:53:50 +01005229# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005230 /* Handle any X events, e.g. serving the clipboard. */
5231 clip_update();
5232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234finished:
5235 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005236 if (options & SHELL_READ)
5237 {
5238 if (ga.ga_len > 0)
5239 {
5240 append_ga_line(&ga);
5241 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005242 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005243 }
5244 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005245 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005246 ga_clear(&ga);
5247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /*
5250 * Give all typeahead that wasn't used back to ui_inchar().
5251 */
5252 if (ta_len)
5253 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 State = old_State;
5255 if (toshell_fd >= 0)
5256 close(toshell_fd);
5257 close(fromshell_fd);
5258 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005259# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005260 else
5261 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005262 long delay_msec = 1;
5263
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005264 /*
5265 * Similar to the loop above, but only handle X events, no
5266 * I/O.
5267 */
5268 for (;;)
5269 {
5270 if (got_int)
5271 {
5272 /* CTRL-C sends a signal to the child, we ignore it
5273 * ourselves */
5274# ifdef HAVE_SETSID
5275 kill(-pid, SIGINT);
5276# else
5277 kill(0, SIGINT);
5278# endif
5279 got_int = FALSE;
5280 }
5281# ifdef __NeXT__
5282 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5283# else
5284 wait_pid = waitpid(pid, &status, WNOHANG);
5285# endif
5286 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5287 || (wait_pid == pid && WIFEXITED(status)))
5288 {
5289 wait_pid = pid;
5290 break;
5291 }
5292
5293 /* Handle any X events, e.g. serving the clipboard. */
5294 clip_update();
5295
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005296 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5297 * less time. */
5298 mch_delay(delay_msec, TRUE);
5299 if (++delay_msec > 10)
5300 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005301 }
5302 }
5303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305 /*
5306 * Wait until our child has exited.
5307 * Ignore wait() returning pids of other children and returning
5308 * because of some signal like SIGWINCH.
5309 * Don't wait if wait_pid was already set above, indicating the
5310 * child already exited.
5311 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005312 if (wait_pid != pid)
5313 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
Bram Moolenaar624891f2010-10-13 16:22:09 +02005315# ifdef FEAT_GUI
5316 /* Close slave side of pty. Only do this after the child has
5317 * exited, otherwise the child may hang when it tries to write on
5318 * the pty. */
5319 if (pty_master_fd >= 0)
5320 close(pty_slave_fd);
5321# endif
5322
Bram Moolenaardf177f62005-02-22 08:39:57 +00005323 /* Make sure the child that writes to the external program is
5324 * dead. */
5325 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005326 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005327 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005328 wait4pid(wpid, NULL);
5329 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005330
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005331# ifdef FEAT_JOB_CHANNEL
5332 --dont_check_job_ended;
5333# endif
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 /*
5336 * Set to raw mode right now, otherwise a CTRL-C after
5337 * catch_signals() will kill Vim.
5338 */
5339 if (tmode == TMODE_RAW)
5340 settmode(TMODE_RAW);
5341 did_settmode = TRUE;
5342 set_signals();
5343
5344 if (WIFEXITED(status))
5345 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005346 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005348 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 if (retval == EXEC_FAILED)
5351 {
5352 MSG_PUTS(_("\nCannot execute shell "));
5353 msg_outtrans(p_sh);
5354 msg_putchar('\n');
5355 }
5356 else if (!(options & SHELL_SILENT))
5357 {
5358 MSG_PUTS(_("\nshell returned "));
5359 msg_outnum((long)retval);
5360 msg_putchar('\n');
5361 }
5362 }
5363 }
5364 else
5365 MSG_PUTS(_("\nCommand terminated\n"));
5366 }
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369error:
5370 if (!did_settmode)
5371 if (tmode == TMODE_RAW)
5372 settmode(TMODE_RAW); /* set to raw mode */
5373# ifdef FEAT_TITLE
5374 resettitle();
5375# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005376 vim_free(argv);
5377 vim_free(tofree1);
5378 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005383
5384 int
5385mch_call_shell(
5386 char_u *cmd,
5387 int options) /* SHELL_*, see vim.h */
5388{
5389#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5390 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5391 return mch_call_shell_terminal(cmd, options);
5392#endif
5393#ifdef USE_SYSTEM
5394 return mch_call_shell_system(cmd, options);
5395#else
5396 return mch_call_shell_fork(cmd, options);
5397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398}
5399
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005400#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005401 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005402mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005403{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005404 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005405 int fd_in[2] = {-1, -1}; /* for stdin */
5406 int fd_out[2] = {-1, -1}; /* for stdout */
5407 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005408 int pty_master_fd = -1;
5409 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005410 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005411 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5412 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5413 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005414 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005415 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5416 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005417 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005418 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005419 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005420
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005421 if (use_out_for_err && use_null_for_out)
5422 use_null_for_err = TRUE;
5423
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005424 /* default is to fail */
5425 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005426
Bram Moolenaarb2412082017-08-20 18:09:14 +02005427 if (options->jo_pty
5428 && (!(use_file_for_in || use_null_for_in)
5429 || !(use_file_for_in || use_null_for_out)
5430 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005431 {
5432 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5433 if (job->jv_tty_out != NULL)
5434 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5435 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005436
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005437 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005438 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005439 if (use_file_for_in)
5440 {
5441 char_u *fname = options->jo_io_name[PART_IN];
5442
5443 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5444 if (fd_in[0] < 0)
5445 {
5446 EMSG2(_(e_notopen), fname);
5447 goto failed;
5448 }
5449 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005450 else
5451 /* When writing buffer lines to the input don't use the pty, so that
5452 * the pipe can be closed when all lines were written. */
5453 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5454 && pipe(fd_in) < 0)
5455 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005456
5457 if (use_file_for_out)
5458 {
5459 char_u *fname = options->jo_io_name[PART_OUT];
5460
5461 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5462 if (fd_out[1] < 0)
5463 {
5464 EMSG2(_(e_notopen), fname);
5465 goto failed;
5466 }
5467 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005468 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005469 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005470
5471 if (use_file_for_err)
5472 {
5473 char_u *fname = options->jo_io_name[PART_ERR];
5474
5475 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5476 if (fd_err[1] < 0)
5477 {
5478 EMSG2(_(e_notopen), fname);
5479 goto failed;
5480 }
5481 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005482 else if (!use_out_for_err && !use_null_for_err
5483 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005484 goto failed;
5485
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005486 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5487 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005488 if (options->jo_set & JO_CHANNEL)
5489 {
5490 channel = options->jo_channel;
5491 if (channel != NULL)
5492 ++channel->ch_refcount;
5493 }
5494 else
5495 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005496 if (channel == NULL)
5497 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005498 if (job->jv_tty_out != NULL)
5499 ch_log(channel, "using pty %s on fd %d",
5500 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005501 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005502
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005503 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005504 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005505 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005507 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005508 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005509 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005511 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005513 int null_fd = -1;
5514 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005515
Bram Moolenaar835dc632016-02-07 14:27:38 +01005516 /* child */
5517 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005518 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005519
Bram Moolenaar819524702018-02-27 19:10:00 +01005520# ifdef FEAT_JOB_CHANNEL
5521 if (ch_log_active())
5522 /* close the log file in the child */
5523 ch_logfile((char_u *)"", (char_u *)"");
5524# endif
5525
Bram Moolenaar835dc632016-02-07 14:27:38 +01005526# ifdef HAVE_SETSID
5527 /* Create our own process group, so that the child and all its
5528 * children can be kill()ed. Don't do this when using pipes,
5529 * because stdin is not a tty, we would lose /dev/tty. */
5530 (void)setsid();
5531# endif
5532
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005533# ifdef FEAT_TERMINAL
5534 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005535 {
5536 char *term = (char *)T_NAME;
5537
5538#ifdef FEAT_GUI
5539 if (term_is_gui(T_NAME))
5540 /* In the GUI 'term' is not what we want, use $TERM. */
5541 term = getenv("TERM");
5542#endif
5543 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5544 * back to "xterm". */
5545 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5546 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005547 set_child_environment(
5548 (long)options->jo_term_rows,
5549 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005550 term,
5551 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005552 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005553 else
5554# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005555 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005556
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005557 if (options->jo_env != NULL)
5558 {
5559 dict_T *dict = options->jo_env;
5560 hashitem_T *hi;
5561 int todo = (int)dict->dv_hashtab.ht_used;
5562
5563 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5564 if (!HASHITEM_EMPTY(hi))
5565 {
5566 typval_T *item = &dict_lookup(hi)->di_tv;
5567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005568 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005569 --todo;
5570 }
5571 }
5572
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005573 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005574 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005575 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005576 if (null_fd < 0)
5577 {
5578 perror("opening /dev/null failed");
5579 _exit(OPEN_NULL_FAILED);
5580 }
5581 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005582
Bram Moolenaar223896d2017-08-02 22:33:28 +02005583 if (pty_slave_fd >= 0)
5584 {
5585 /* push stream discipline modules */
5586 SetupSlavePTY(pty_slave_fd);
5587# ifdef TIOCSCTTY
5588 /* Try to become controlling tty (probably doesn't work,
5589 * unless run by root) */
5590 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5591# endif
5592 }
5593
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005594 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005595 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005596 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005597 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005598 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005599 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005600 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005601 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005602
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005603 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005604 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005605 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005606 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005607 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005608 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005609 }
5610 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005611 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005612 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005613 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005614 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005615 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005616
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005617 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005618 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005619 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005620 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005621 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005622 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005623 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005624 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005625
5626 if (fd_in[0] >= 0)
5627 close(fd_in[0]);
5628 if (fd_in[1] >= 0)
5629 close(fd_in[1]);
5630 if (fd_out[0] >= 0)
5631 close(fd_out[0]);
5632 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005633 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005634 if (fd_err[0] >= 0)
5635 close(fd_err[0]);
5636 if (fd_err[1] >= 0)
5637 close(fd_err[1]);
5638 if (pty_master_fd >= 0)
5639 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005640 close(pty_master_fd); /* not used in the child */
5641 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005642 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005643
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005644 if (null_fd >= 0)
5645 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005646
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005647 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5648 _exit(EXEC_FAILED);
5649
Bram Moolenaar835dc632016-02-07 14:27:38 +01005650 /* See above for type of argv. */
5651 execvp(argv[0], argv);
5652
Bram Moolenaar4694a172016-04-21 14:05:23 +02005653 if (stderr_works)
5654 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005655# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005656 /* calling free_all_mem() here causes problems. Ignore valgrind
5657 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005658# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005659 _exit(EXEC_FAILED); /* exec failed, return failure code */
5660 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005661
5662 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005663 UNBLOCK_SIGNALS(&curset);
5664
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005665 job->jv_pid = pid;
5666 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005667 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005668
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005669 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005670 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005671 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005672 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005673 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005674 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005675 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005676 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005677 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005678 if (channel != NULL)
5679 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005680 int in_fd = use_file_for_in || use_null_for_in
5681 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5682 int out_fd = use_file_for_out || use_null_for_out
5683 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005684 /* When using pty_master_fd only set it for stdout, do not duplicate it
5685 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005686 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005687 ? INVALID_FD
5688 : fd_err[0] >= 0
5689 ? fd_err[0]
5690 : (out_fd == pty_master_fd
5691 ? INVALID_FD
5692 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005693
5694 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005695 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005696 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005697 else
5698 {
5699 if (fd_in[1] >= 0)
5700 close(fd_in[1]);
5701 if (fd_out[0] >= 0)
5702 close(fd_out[0]);
5703 if (fd_err[0] >= 0)
5704 close(fd_err[0]);
5705 if (pty_master_fd >= 0)
5706 close(pty_master_fd);
5707 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005708
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005709 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005710 return;
5711
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005712failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005713 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005714 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005716 if (fd_in[1] >= 0)
5717 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005718 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005720 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005722 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005723 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005724 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005725 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005726 if (pty_master_fd >= 0)
5727 close(pty_master_fd);
5728 if (pty_slave_fd >= 0)
5729 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005730}
5731
5732 char *
5733mch_job_status(job_T *job)
5734{
5735# ifdef HAVE_UNION_WAIT
5736 union wait status;
5737# else
5738 int status = -1;
5739# endif
5740 pid_t wait_pid = 0;
5741
5742# ifdef __NeXT__
5743 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5744# else
5745 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5746# endif
5747 if (wait_pid == -1)
5748 {
5749 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005750 if (job->jv_status < JOB_ENDED)
5751 ch_log(job->jv_channel, "Job no longer exists: %s",
5752 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005753 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005754 }
5755 if (wait_pid == 0)
5756 return "run";
5757 if (WIFEXITED(status))
5758 {
5759 /* LINTED avoid "bitwise operation on signed value" */
5760 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005761 if (job->jv_status < JOB_ENDED)
5762 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005763 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005764 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005765 if (WIFSIGNALED(status))
5766 {
5767 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005768 if (job->jv_status < JOB_ENDED)
5769 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005770 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005771 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005772 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005773
5774return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005775 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005776 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005777 return "dead";
5778}
5779
5780 job_T *
5781mch_detect_ended_job(job_T *job_list)
5782{
5783# ifdef HAVE_UNION_WAIT
5784 union wait status;
5785# else
5786 int status = -1;
5787# endif
5788 pid_t wait_pid = 0;
5789 job_T *job;
5790
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005791# ifndef USE_SYSTEM
5792 /* Do not do this when waiting for a shell command to finish, we would get
5793 * the exit value here (and discard it), the exit value obtained there
5794 * would then be wrong. */
5795 if (dont_check_job_ended > 0)
5796 return NULL;
5797# endif
5798
Bram Moolenaar97792de2016-10-15 18:36:49 +02005799# ifdef __NeXT__
5800 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5801# else
5802 wait_pid = waitpid(-1, &status, WNOHANG);
5803# endif
5804 if (wait_pid <= 0)
5805 /* no process ended */
5806 return NULL;
5807 for (job = job_list; job != NULL; job = job->jv_next)
5808 {
5809 if (job->jv_pid == wait_pid)
5810 {
5811 if (WIFEXITED(status))
5812 /* LINTED avoid "bitwise operation on signed value" */
5813 job->jv_exitval = WEXITSTATUS(status);
5814 else if (WIFSIGNALED(status))
5815 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005816 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005817 {
5818 ch_log(job->jv_channel, "Job ended");
5819 job->jv_status = JOB_ENDED;
5820 }
5821 return job;
5822 }
5823 }
5824 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005825}
5826
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005827/*
5828 * Send a (deadly) signal to "job".
5829 * Return FAIL if "how" is not a valid name.
5830 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005831 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005832mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005833{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005834 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005835
Bram Moolenaar923d9262016-02-25 20:56:01 +01005836 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005837 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005838 else if (STRCMP(how, "hup") == 0)
5839 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005840 else if (STRCMP(how, "quit") == 0)
5841 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005842 else if (STRCMP(how, "int") == 0)
5843 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005844 else if (STRCMP(how, "kill") == 0)
5845 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005846#ifdef SIGWINCH
5847 else if (STRCMP(how, "winch") == 0)
5848 sig = SIGWINCH;
5849#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850 else if (isdigit(*how))
5851 sig = atoi((char *)how);
5852 else
5853 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005854
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005855 // Never kill ourselves!
5856 if (job->jv_pid != 0)
5857 {
5858 // TODO: have an option to only kill the process, not the group?
5859 kill(-job->jv_pid, sig);
5860 kill(job->jv_pid, sig);
5861 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005862
Bram Moolenaar835dc632016-02-07 14:27:38 +01005863 return OK;
5864}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005865
5866/*
5867 * Clear the data related to "job".
5868 */
5869 void
5870mch_clear_job(job_T *job)
5871{
5872 /* call waitpid because child process may become zombie */
5873# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005874 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005875# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005876 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005877# endif
5878}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005879#endif
5880
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005881#if defined(FEAT_TERMINAL) || defined(PROTO)
5882 int
5883mch_create_pty_channel(job_T *job, jobopt_T *options)
5884{
5885 int pty_master_fd = -1;
5886 int pty_slave_fd = -1;
5887 channel_T *channel;
5888
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005889 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5890 if (job->jv_tty_out != NULL)
5891 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005892 close(pty_slave_fd);
5893
5894 channel = add_channel();
5895 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005896 {
5897 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005898 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005899 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005900 if (job->jv_tty_out != NULL)
5901 ch_log(channel, "using pty %s on fd %d",
5902 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005903 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5904 channel->ch_keep_open = TRUE;
5905
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005906 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5907 * it only needs to be read once. */
5908 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005909 channel_set_job(channel, job, options);
5910 return OK;
5911}
5912#endif
5913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914/*
5915 * Check for CTRL-C typed by reading all available characters.
5916 * In cooked mode we should get SIGINT, no need to check.
5917 */
5918 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005919mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005921 if ((curr_tmode == TMODE_RAW || force)
5922 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 fill_input_buf(FALSE);
5924}
5925
5926/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005927 * Wait "msec" msec until a character is available from the mouse, keyboard,
5928 * from inbuf[].
5929 * "msec" == -1 will block forever.
5930 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005931 * When "ignore_input" is TRUE even check for pending input when input is
5932 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005933 * "interrupted" (if not NULL) is set to TRUE when no character is available
5934 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005935 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005936 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 */
5938 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005939WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005941#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005942 return ui_wait_for_chars_or_timer(
5943 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005944#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005945 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005946#endif
5947}
5948
5949/*
5950 * Wait "msec" msec until a character is available from the mouse or keyboard
5951 * or from inbuf[].
5952 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005953 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005954 * "interrupted" (if not NULL) is set to TRUE when no character is available
5955 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005956 * When a GUI is being used, this will never get called -- webb
5957 */
5958 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005959WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005960{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961#ifdef FEAT_MOUSE_GPM
5962 int gpm_process_wanted;
5963#endif
5964#ifdef FEAT_XCLIPBOARD
5965 int rest;
5966#endif
5967 int avail;
5968
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005969 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 return 1;
5971
5972#if defined(FEAT_MOUSE_DEC)
5973 /* May need to query the mouse position. */
5974 if (WantQueryMouse)
5975 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005976 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
5978 }
5979#endif
5980
5981 /*
5982 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5983 * events. This is a bit complicated, because they might both be defined.
5984 */
5985#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5986# ifdef FEAT_XCLIPBOARD
5987 rest = 0;
5988 if (do_xterm_trace())
5989 rest = msec;
5990# endif
5991 do
5992 {
5993# ifdef FEAT_XCLIPBOARD
5994 if (rest != 0)
5995 {
5996 msec = XT_TRACE_DELAY;
5997 if (rest >= 0 && rest < XT_TRACE_DELAY)
5998 msec = rest;
5999 if (rest >= 0)
6000 rest -= msec;
6001 }
6002# endif
6003# ifdef FEAT_MOUSE_GPM
6004 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006005 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006006 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006008 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009# endif
6010 if (!avail)
6011 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006012 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 return 1;
6014# ifdef FEAT_XCLIPBOARD
6015 if (rest == 0 || !do_xterm_trace())
6016# endif
6017 break;
6018 }
6019 }
6020 while (FALSE
6021# ifdef FEAT_MOUSE_GPM
6022 || (gpm_process_wanted && mch_gpm_process() == 0)
6023# endif
6024# ifdef FEAT_XCLIPBOARD
6025 || (!avail && rest != 0)
6026# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006027 )
6028 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
6030#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006031 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032#endif
6033 return avail;
6034}
6035
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006036#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037/*
6038 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006039 * "msec" == 0 will check for characters once.
6040 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006043 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006044 * "interrupted" (if not NULL) is set to TRUE when no character is available
6045 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047#if defined(__BEOS__)
6048 int
6049#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006050 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006052RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006055 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006056#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 static int busy = FALSE;
6058
6059 /* May retry getting characters after an event was handled. */
6060# define MAY_LOOP
6061
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006062# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 /* Remember at what time we started, so that we know how much longer we
6064 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006065 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006066 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006068 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006069 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070# endif
6071
6072 /* Handle being called recursively. This may happen for the session
6073 * manager stuff, it may save the file, which does a breakcheck. */
6074 if (busy)
6075 return 0;
6076#endif
6077
6078#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006079 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080#endif
6081 {
6082#ifdef MAY_LOOP
6083 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006084# ifdef FEAT_MZSCHEME
6085 int mzquantum_used = FALSE;
6086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#endif
6088#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006089 /* each channel may use in, out and err */
6090 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 int nfd;
6092# ifdef FEAT_XCLIPBOARD
6093 int xterm_idx = -1;
6094# endif
6095# ifdef FEAT_MOUSE_GPM
6096 int gpm_idx = -1;
6097# endif
6098# ifdef USE_XSMP
6099 int xsmp_idx = -1;
6100# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006101 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006103# ifdef FEAT_MZSCHEME
6104 mzvim_check_threads();
6105 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6106 {
6107 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6108 mzquantum_used = TRUE;
6109 }
6110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 fds[0].fd = fd;
6112 fds[0].events = POLLIN;
6113 nfd = 1;
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006116 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (xterm_Shell != (Widget)0)
6118 {
6119 xterm_idx = nfd;
6120 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6121 fds[nfd].events = POLLIN;
6122 nfd++;
6123 }
6124# endif
6125# ifdef FEAT_MOUSE_GPM
6126 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6127 {
6128 gpm_idx = nfd;
6129 fds[nfd].fd = gpm_fd;
6130 fds[nfd].events = POLLIN;
6131 nfd++;
6132 }
6133# endif
6134# ifdef USE_XSMP
6135 if (xsmp_icefd != -1)
6136 {
6137 xsmp_idx = nfd;
6138 fds[nfd].fd = xsmp_icefd;
6139 fds[nfd].events = POLLIN;
6140 nfd++;
6141 }
6142# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006144 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006145#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006146 if (interrupted != NULL)
6147 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006149 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006150
6151 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006152 if (result == 0 && interrupted != NULL && ret > 0)
6153 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006154
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006155# ifdef FEAT_MZSCHEME
6156 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006157 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006158 finished = FALSE;
6159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161# ifdef FEAT_XCLIPBOARD
6162 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6163 {
6164 xterm_update(); /* Maybe we should hand out clipboard */
6165 if (--ret == 0 && !input_available())
6166 /* Try again */
6167 finished = FALSE;
6168 }
6169# endif
6170# ifdef FEAT_MOUSE_GPM
6171 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6172 {
6173 *check_for_gpm = 1;
6174 }
6175# endif
6176# ifdef USE_XSMP
6177 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6178 {
6179 if (fds[xsmp_idx].revents & POLLIN)
6180 {
6181 busy = TRUE;
6182 xsmp_handle_requests();
6183 busy = FALSE;
6184 }
6185 else if (fds[xsmp_idx].revents & POLLHUP)
6186 {
6187 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006188 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 xsmp_close();
6190 }
6191 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006192 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 }
6194# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006195#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006196 /* also call when ret == 0, we may be polling a keep-open channel */
6197 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006198 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201#else /* HAVE_SELECT */
6202
6203 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006204 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006205 // These are static because they can take 8 Kbyte each and cause the
6206 // signal stack to run out with -O3.
6207 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006209 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006211# ifdef FEAT_MZSCHEME
6212 mzvim_check_threads();
6213 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6214 {
6215 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6216 mzquantum_used = TRUE;
6217 }
6218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006220 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006222 tv.tv_sec = towait / 1000;
6223 tv.tv_usec = (towait % 1000) * (1000000/1000);
6224 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006226 else
6227 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
6229 /*
6230 * Select on ready for reading and exceptional condition (end of file).
6231 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006232select_eintr:
6233 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006234 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 FD_ZERO(&efds);
6236 FD_SET(fd, &rfds);
6237# if !defined(__QNX__) && !defined(__CYGWIN32__)
6238 /* For QNX select() always returns 1 if this is set. Why? */
6239 FD_SET(fd, &efds);
6240# endif
6241 maxfd = fd;
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006244 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 if (xterm_Shell != (Widget)0)
6246 {
6247 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6248 if (maxfd < ConnectionNumber(xterm_dpy))
6249 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006250
6251 /* An event may have already been read but not handled. In
6252 * particulary, XFlush may cause this. */
6253 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255# endif
6256# ifdef FEAT_MOUSE_GPM
6257 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6258 {
6259 FD_SET(gpm_fd, &rfds);
6260 FD_SET(gpm_fd, &efds);
6261 if (maxfd < gpm_fd)
6262 maxfd = gpm_fd;
6263 }
6264# endif
6265# ifdef USE_XSMP
6266 if (xsmp_icefd != -1)
6267 {
6268 FD_SET(xsmp_icefd, &rfds);
6269 FD_SET(xsmp_icefd, &efds);
6270 if (maxfd < xsmp_icefd)
6271 maxfd = xsmp_icefd;
6272 }
6273# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006274# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006275 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006276# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006277 if (interrupted != NULL)
6278 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
Bram Moolenaar643b6142018-09-12 20:29:09 +02006280 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6281 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006282 result = ret > 0 && FD_ISSET(fd, &rfds);
6283 if (result)
6284 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006285 else if (interrupted != NULL && ret > 0)
6286 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006287
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006288# ifdef EINTR
6289 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006290 {
6291 /* Check whether window has been resized, EINTR may be caused by
6292 * SIGWINCH. */
6293 if (do_resize)
6294 handle_resize();
6295
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006296 /* Interrupted by a signal, need to try again. We ignore msec
6297 * here, because we do want to check even after a timeout if
6298 * characters are available. Needed for reading output of an
6299 * external command after the process has finished. */
6300 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006301 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006302# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006303# ifdef __TANDEM
6304 if (ret == -1 && errno == ENOTSUP)
6305 {
6306 FD_ZERO(&rfds);
6307 FD_ZERO(&efds);
6308 ret = 0;
6309 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006310# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006311# ifdef FEAT_MZSCHEME
6312 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006313 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006314 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315# endif
6316
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317# ifdef FEAT_XCLIPBOARD
6318 if (ret > 0 && xterm_Shell != (Widget)0
6319 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6320 {
6321 xterm_update(); /* Maybe we should hand out clipboard */
6322 /* continue looping when we only got the X event and the input
6323 * buffer is empty */
6324 if (--ret == 0 && !input_available())
6325 {
6326 /* Try again */
6327 finished = FALSE;
6328 }
6329 }
6330# endif
6331# ifdef FEAT_MOUSE_GPM
6332 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6333 {
6334 if (FD_ISSET(gpm_fd, &efds))
6335 gpm_close();
6336 else if (FD_ISSET(gpm_fd, &rfds))
6337 *check_for_gpm = 1;
6338 }
6339# endif
6340# ifdef USE_XSMP
6341 if (ret > 0 && xsmp_icefd != -1)
6342 {
6343 if (FD_ISSET(xsmp_icefd, &efds))
6344 {
6345 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006346 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 xsmp_close();
6348 if (--ret == 0)
6349 finished = FALSE; /* keep going if event was only one */
6350 }
6351 else if (FD_ISSET(xsmp_icefd, &rfds))
6352 {
6353 busy = TRUE;
6354 xsmp_handle_requests();
6355 busy = FALSE;
6356 if (--ret == 0)
6357 finished = FALSE; /* keep going if event was only one */
6358 }
6359 }
6360# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006361#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006362 /* also call when ret == 0, we may be polling a keep-open channel */
6363 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006364 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
6367#endif /* HAVE_SELECT */
6368
6369#ifdef MAY_LOOP
6370 if (finished || msec == 0)
6371 break;
6372
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006373# ifdef FEAT_CLIENTSERVER
6374 if (server_waiting())
6375 break;
6376# endif
6377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 /* We're going to loop around again, find out for how long */
6379 if (msec > 0)
6380 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006381# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006383 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384# else
6385 /* Guess we got interrupted halfway. */
6386 msec = msec / 2;
6387# endif
6388 if (msec <= 0)
6389 break; /* waited long enough */
6390 }
6391#endif
6392 }
6393
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006394 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395}
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006399 * Expand a path into all matching files and/or directories. Handles "*",
6400 * "?", "[a-z]", "**", etc.
6401 * "path" has backslashes before chars that are not to be expanded.
6402 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 */
6404 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006405mch_expandpath(
6406 garray_T *gap,
6407 char_u *path,
6408 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
Bram Moolenaar02743632005-07-25 20:42:36 +00006410 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411}
6412#endif
6413
6414/*
6415 * mch_expand_wildcards() - this code does wild-card pattern matching using
6416 * the shell
6417 *
6418 * return OK for success, FAIL for error (you may lose some memory) and put
6419 * an error message in *file.
6420 *
6421 * num_pat is number of input patterns
6422 * pat is array of pointers to input patterns
6423 * num_file is pointer to number of matched file names
6424 * file is pointer to array of pointers to matched file names
6425 */
6426
6427#ifndef SEEK_SET
6428# define SEEK_SET 0
6429#endif
6430#ifndef SEEK_END
6431# define SEEK_END 2
6432#endif
6433
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006434#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006435
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006437mch_expand_wildcards(
6438 int num_pat,
6439 char_u **pat,
6440 int *num_file,
6441 char_u ***file,
6442 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 int i;
6445 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006446 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 char_u *p;
6448 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
Bram Moolenaarc7247912008-01-13 12:54:11 +00006450 /*
6451 * This is the non-OS/2 implementation (really Unix).
6452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 int j;
6454 char_u *tempname;
6455 char_u *command;
6456 FILE *fd;
6457 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006458#define STYLE_ECHO 0 /* use "echo", the default */
6459#define STYLE_GLOB 1 /* use "glob", for csh */
6460#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6461#define STYLE_PRINT 3 /* use "print -N", for zsh */
6462#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6463 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 int shell_style = STYLE_ECHO;
6465 int check_spaces;
6466 static int did_find_nul = FALSE;
6467 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006468 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006469 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471 *num_file = 0; /* default: no files found */
6472 *file = NULL;
6473
6474 /*
6475 * If there are no wildcards, just copy the names to allocated memory.
6476 * Saves a lot of time, because we don't have to start a new shell.
6477 */
6478 if (!have_wildcard(num_pat, pat))
6479 return save_patterns(num_pat, pat, num_file, file);
6480
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006481# ifdef HAVE_SANDBOX
6482 /* Don't allow any shell command in the sandbox. */
6483 if (sandbox != 0 && check_secure())
6484 return FAIL;
6485# endif
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 /*
6488 * Don't allow the use of backticks in secure and restricted mode.
6489 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006490 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 for (i = 0; i < num_pat; ++i)
6492 if (vim_strchr(pat[i], '`') != NULL
6493 && (check_restricted() || check_secure()))
6494 return FAIL;
6495
6496 /*
6497 * get a name for the temp file
6498 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006499 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
6501 EMSG(_(e_notmp));
6502 return FAIL;
6503 }
6504
6505 /*
6506 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006507 * file.
6508 * STYLE_BT: NL separated
6509 * If expanding `cmd` execute it directly.
6510 * STYLE_GLOB: NUL separated
6511 * If we use *csh, "glob" will work better than "echo".
6512 * STYLE_PRINT: NL or NUL separated
6513 * If we use *zsh, "print -N" will work better than "glob".
6514 * STYLE_VIMGLOB: NL separated
6515 * If we use *sh*, we define "vimglob()".
6516 * STYLE_ECHO: space separated.
6517 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 */
6519 if (num_pat == 1 && *pat[0] == '`'
6520 && (len = STRLEN(pat[0])) > 2
6521 && *(pat[0] + len - 1) == '`')
6522 shell_style = STYLE_BT;
6523 else if ((len = STRLEN(p_sh)) >= 3)
6524 {
6525 if (STRCMP(p_sh + len - 3, "csh") == 0)
6526 shell_style = STYLE_GLOB;
6527 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6528 shell_style = STYLE_PRINT;
6529 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006530 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6531 "sh") != NULL)
6532 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533
Bram Moolenaarc7247912008-01-13 12:54:11 +00006534 /* Compute the length of the command. We need 2 extra bytes: for the
6535 * optional '&' and for the NUL.
6536 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006538 if (shell_style == STYLE_VIMGLOB)
6539 len += STRLEN(sh_vimglob_func);
6540
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006541 for (i = 0; i < num_pat; ++i)
6542 {
6543 /* Count the length of the patterns in the same way as they are put in
6544 * "command" below. */
6545#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006547#else
6548 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006549 for (j = 0; pat[i][j] != NUL; ++j)
6550 {
6551 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6552 ++len; /* may add a backslash */
6553 ++len;
6554 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006555#endif
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 command = alloc(len);
6558 if (command == NULL)
6559 {
6560 /* out of memory */
6561 vim_free(tempname);
6562 return FAIL;
6563 }
6564
6565 /*
6566 * Build the shell command:
6567 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6568 * recognizes this).
6569 * - Add the shell command to print the expanded names.
6570 * - Add the temp file name.
6571 * - Add the file name patterns.
6572 */
6573 if (shell_style == STYLE_BT)
6574 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006575 /* change `command; command& ` to (command; command ) */
6576 STRCPY(command, "(");
6577 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006579 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006580 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 --p;
6582 if (*p == '&') /* remove trailing '&' */
6583 {
6584 ampersent = TRUE;
6585 *p = ' ';
6586 }
6587 STRCAT(command, ">");
6588 }
6589 else
6590 {
6591 if (flags & EW_NOTFOUND)
6592 STRCPY(command, "set nonomatch; ");
6593 else
6594 STRCPY(command, "unset nonomatch; ");
6595 if (shell_style == STYLE_GLOB)
6596 STRCAT(command, "glob >");
6597 else if (shell_style == STYLE_PRINT)
6598 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006599 else if (shell_style == STYLE_VIMGLOB)
6600 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 else
6602 STRCAT(command, "echo >");
6603 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006604
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (shell_style != STYLE_BT)
6608 for (i = 0; i < num_pat; ++i)
6609 {
6610 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006611 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006612 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613#ifdef USE_SYSTEM
6614 STRCAT(command, " \"");
6615 STRCAT(command, pat[i]);
6616 STRCAT(command, "\"");
6617#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006618 int intick = FALSE;
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 p = command + STRLEN(command);
6621 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006622 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006623 {
6624 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006625 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006626 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6627 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006628 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006629 * backslash inside backticks, before a special character
6630 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006631 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006632 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6633 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006634 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006635 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006636 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006637 else if (!intick
6638 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6639 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006640 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006641 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6642 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006643 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006644
6645 /* Copy one character. */
6646 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 *p = NUL;
6649#endif
6650 }
6651 if (flags & EW_SILENT)
6652 show_shell_mess = FALSE;
6653 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006654 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656 /*
6657 * Using zsh -G: If a pattern has no matches, it is just deleted from
6658 * the argument list, otherwise zsh gives an error message and doesn't
6659 * expand any other pattern.
6660 */
6661 if (shell_style == STYLE_PRINT)
6662 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6663
6664 /*
6665 * If we use -f then shell variables set in .cshrc won't get expanded.
6666 * vi can do it, so we will too, but it is only necessary if there is a "$"
6667 * in one of the patterns, otherwise we can still use the fast option.
6668 */
6669 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6670 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6671
6672 /*
6673 * execute the shell command
6674 */
6675 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6676
6677 /* When running in the background, give it some time to create the temp
6678 * file, but don't wait for it to finish. */
6679 if (ampersent)
6680 mch_delay(10L, TRUE);
6681
6682 extra_shell_arg = NULL; /* cleanup */
6683 show_shell_mess = TRUE;
6684 vim_free(command);
6685
Bram Moolenaarc7247912008-01-13 12:54:11 +00006686 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {
6688 mch_remove(tempname);
6689 vim_free(tempname);
6690 /*
6691 * With interactive completion, the error message is not printed.
6692 * However with USE_SYSTEM, I don't know how to turn off error messages
6693 * from the shell, so screen may still get messed up -- webb.
6694 */
6695#ifndef USE_SYSTEM
6696 if (!(flags & EW_SILENT))
6697#endif
6698 {
6699 redraw_later_clear(); /* probably messed up screen */
6700 msg_putchar('\n'); /* clear bottom line quickly */
6701 cmdline_row = Rows - 1; /* continue on last line */
6702#ifdef USE_SYSTEM
6703 if (!(flags & EW_SILENT))
6704#endif
6705 {
6706 MSG(_(e_wildexpand));
6707 msg_start(); /* don't overwrite this message */
6708 }
6709 }
6710 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6711 * EW_NOTFOUND is given */
6712 if (shell_style == STYLE_BT)
6713 return FAIL;
6714 goto notfound;
6715 }
6716
6717 /*
6718 * read the names from the file into memory
6719 */
6720 fd = fopen((char *)tempname, READBIN);
6721 if (fd == NULL)
6722 {
6723 /* Something went wrong, perhaps a file name with a special char. */
6724 if (!(flags & EW_SILENT))
6725 {
6726 MSG(_(e_wildexpand));
6727 msg_start(); /* don't overwrite this message */
6728 }
6729 vim_free(tempname);
6730 goto notfound;
6731 }
6732 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006733 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006735 if (llen < 0)
6736 /* just in case ftell() would fail */
6737 buffer = NULL;
6738 else
6739 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (buffer == NULL)
6741 {
6742 /* out of memory */
6743 mch_remove(tempname);
6744 vim_free(tempname);
6745 fclose(fd);
6746 return FAIL;
6747 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006748 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 i = fread((char *)buffer, 1, len, fd);
6750 fclose(fd);
6751 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006752 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
6754 /* unexpected read error */
6755 EMSG2(_(e_notread), tempname);
6756 vim_free(tempname);
6757 vim_free(buffer);
6758 return FAIL;
6759 }
6760 vim_free(tempname);
6761
Bram Moolenaarc7247912008-01-13 12:54:11 +00006762# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6764 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006765 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6767 *p++ = buffer[i];
6768 len = p - buffer;
6769# endif
6770
6771
6772 /* file names are separated with Space */
6773 if (shell_style == STYLE_ECHO)
6774 {
6775 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6776 p = buffer;
6777 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6778 {
6779 while (*p != ' ' && *p != '\n')
6780 ++p;
6781 p = skipwhite(p); /* skip to next entry */
6782 }
6783 }
6784 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006785 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
6787 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6788 p = buffer;
6789 for (i = 0; *p != NUL; ++i) /* count number of entries */
6790 {
6791 while (*p != '\n' && *p != NUL)
6792 ++p;
6793 if (*p != NUL)
6794 ++p;
6795 p = skipwhite(p); /* skip leading white space */
6796 }
6797 }
6798 /* file names are separated with NUL */
6799 else
6800 {
6801 /*
6802 * Some versions of zsh use spaces instead of NULs to separate
6803 * results. Only do this when there is no NUL before the end of the
6804 * buffer, otherwise we would never be able to use file names with
6805 * embedded spaces when zsh does use NULs.
6806 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6807 * don't check for spaces again.
6808 */
6809 check_spaces = FALSE;
6810 if (shell_style == STYLE_PRINT && !did_find_nul)
6811 {
6812 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006813 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006814 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 did_find_nul = TRUE;
6816 else
6817 check_spaces = TRUE;
6818 }
6819
6820 /*
6821 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6822 * already is one, for STYLE_GLOB it needs to be added.
6823 */
6824 if (len && buffer[len - 1] == NUL)
6825 --len;
6826 else
6827 buffer[len] = NUL;
6828 i = 0;
6829 for (p = buffer; p < buffer + len; ++p)
6830 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6831 {
6832 ++i;
6833 *p = NUL;
6834 }
6835 if (len)
6836 ++i; /* count last entry */
6837 }
6838 if (i == 0)
6839 {
6840 /*
6841 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6842 * /bin/sh will happily expand it to nothing rather than returning an
6843 * error; and hey, it's good to check anyway -- webb.
6844 */
6845 vim_free(buffer);
6846 goto notfound;
6847 }
6848 *num_file = i;
6849 *file = (char_u **)alloc(sizeof(char_u *) * i);
6850 if (*file == NULL)
6851 {
6852 /* out of memory */
6853 vim_free(buffer);
6854 return FAIL;
6855 }
6856
6857 /*
6858 * Isolate the individual file names.
6859 */
6860 p = buffer;
6861 for (i = 0; i < *num_file; ++i)
6862 {
6863 (*file)[i] = p;
6864 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006865 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6866 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006868 while (!(shell_style == STYLE_ECHO && *p == ' ')
6869 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 ++p;
6871 if (p == buffer + len) /* last entry */
6872 *p = NUL;
6873 else
6874 {
6875 *p++ = NUL;
6876 p = skipwhite(p); /* skip to next entry */
6877 }
6878 }
6879 else /* NUL separates */
6880 {
6881 while (*p && p < buffer + len) /* skip entry */
6882 ++p;
6883 ++p; /* skip NUL */
6884 }
6885 }
6886
6887 /*
6888 * Move the file names to allocated memory.
6889 */
6890 for (j = 0, i = 0; i < *num_file; ++i)
6891 {
6892 /* Require the files to exist. Helps when using /bin/sh */
6893 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6894 continue;
6895
6896 /* check if this entry should be included */
6897 dir = (mch_isdir((*file)[i]));
6898 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6899 continue;
6900
Bram Moolenaara2031822006-03-07 22:29:51 +00006901 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006902 if (!dir && (flags & EW_EXEC)
6903 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006904 continue;
6905
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6907 if (p)
6908 {
6909 STRCPY(p, (*file)[i]);
6910 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006911 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 (*file)[j++] = p;
6913 }
6914 }
6915 vim_free(buffer);
6916 *num_file = j;
6917
6918 if (*num_file == 0) /* rejected all entries */
6919 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006920 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 goto notfound;
6922 }
6923
6924 return OK;
6925
6926notfound:
6927 if (flags & EW_NOTFOUND)
6928 return save_patterns(num_pat, pat, num_file, file);
6929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930}
6931
6932#endif /* VMS */
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006935save_patterns(
6936 int num_pat,
6937 char_u **pat,
6938 int *num_file,
6939 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006942 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
6944 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6945 if (*file == NULL)
6946 return FAIL;
6947 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006948 {
6949 s = vim_strsave(pat[i]);
6950 if (s != NULL)
6951 /* Be compatible with expand_filename(): halve the number of
6952 * backslashes. */
6953 backslash_halve(s);
6954 (*file)[i] = s;
6955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 *num_file = num_pat;
6957 return OK;
6958}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960/*
6961 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6962 * expand.
6963 */
6964 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006965mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006967 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 if (*p == '\\' && p[1] != NUL)
6970 ++p;
6971 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (vim_strchr((char_u *)
6973#ifdef VMS
6974 "*?%"
6975#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977#endif
6978 , *p) != NULL)
6979 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981 return FALSE;
6982}
6983
6984/*
6985 * Return TRUE if the string "p" contains a wildcard.
6986 * Don't recognize '~' at the end as a wildcard.
6987 */
6988 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006989mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006991 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (*p == '\\' && p[1] != NUL)
6994 ++p;
6995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (vim_strchr((char_u *)
6997#ifdef VMS
6998 "*?%$"
6999#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#endif
7002 , *p) != NULL
7003 || (*p == '~' && p[1] != NUL))
7004 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 }
7006 return FALSE;
7007}
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007010have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 int i;
7013
7014 for (i = 0; i < num; i++)
7015 if (mch_has_wildcard(file[i]))
7016 return 1;
7017 return 0;
7018}
7019
7020 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007021have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 int i;
7024
7025 for (i = 0; i < num; i++)
7026 if (vim_strchr(file[i], '$') != NULL)
7027 return TRUE;
7028 return FALSE;
7029}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007031#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032/*
7033 * Scaled-down version of rename(), which is missing in Xenix.
7034 * This version can only move regular files and will fail if the
7035 * destination exists.
7036 */
7037 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007038mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 struct stat st;
7041
7042 if (stat(dest, &st) >= 0) /* fail if destination exists */
7043 return -1;
7044 if (link(src, dest) != 0) /* link file to new name */
7045 return -1;
7046 if (mch_remove(src) == 0) /* delete link to old name */
7047 return 0;
7048 return -1;
7049}
7050#endif /* !HAVE_RENAME */
7051
7052#ifdef FEAT_MOUSE_GPM
7053/*
7054 * Initializes connection with gpm (if it isn't already opened)
7055 * Return 1 if succeeded (or connection already opened), 0 if failed
7056 */
7057 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007058gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7061
7062 if (!gpm_flag)
7063 {
7064 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7065 gpm_connect.defaultMask = ~GPM_HARD;
7066 /* Default handling for mouse move*/
7067 gpm_connect.minMod = 0; /* Handle any modifier keys */
7068 gpm_connect.maxMod = 0xffff;
7069 if (Gpm_Open(&gpm_connect, 0) > 0)
7070 {
7071 /* gpm library tries to handling TSTP causes
7072 * problems. Anyways, we close connection to Gpm whenever
7073 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007074 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007076# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 return 1; /* succeed */
7080 }
7081 if (gpm_fd == -2)
7082 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7083 return 0;
7084 }
7085 return 1; /* already open */
7086}
7087
7088/*
7089 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 */
7091 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007092gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094 if (gpm_flag && gpm_fd >= 0) /* if Open */
7095 Gpm_Close();
7096}
7097
7098/* Reads gpm event and adds special keys to input buf. Returns length of
7099 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007100 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 */
7102 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007103mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 int button;
7106 static Gpm_Event gpm_event;
7107 char_u string[6];
7108 int_u vim_modifiers;
7109 int row,col;
7110 unsigned char buttons_mask;
7111 unsigned char gpm_modifiers;
7112 static unsigned char old_buttons = 0;
7113
7114 Gpm_GetEvent(&gpm_event);
7115
7116#ifdef FEAT_GUI
7117 /* Don't put events in the input queue now. */
7118 if (hold_gui_events)
7119 return 0;
7120#endif
7121
7122 row = gpm_event.y - 1;
7123 col = gpm_event.x - 1;
7124
7125 string[0] = ESC; /* Our termcode */
7126 string[1] = 'M';
7127 string[2] = 'G';
7128 switch (GPM_BARE_EVENTS(gpm_event.type))
7129 {
7130 case GPM_DRAG:
7131 string[3] = MOUSE_DRAG;
7132 break;
7133 case GPM_DOWN:
7134 buttons_mask = gpm_event.buttons & ~old_buttons;
7135 old_buttons = gpm_event.buttons;
7136 switch (buttons_mask)
7137 {
7138 case GPM_B_LEFT:
7139 button = MOUSE_LEFT;
7140 break;
7141 case GPM_B_MIDDLE:
7142 button = MOUSE_MIDDLE;
7143 break;
7144 case GPM_B_RIGHT:
7145 button = MOUSE_RIGHT;
7146 break;
7147 default:
7148 return 0;
7149 /*Don't know what to do. Can more than one button be
7150 * reported in one event? */
7151 }
7152 string[3] = (char_u)(button | 0x20);
7153 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7154 break;
7155 case GPM_UP:
7156 string[3] = MOUSE_RELEASE;
7157 old_buttons &= ~gpm_event.buttons;
7158 break;
7159 default:
7160 return 0;
7161 }
7162 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7163 gpm_modifiers = gpm_event.modifiers;
7164 vim_modifiers = 0x0;
7165 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7166 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7167 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7168 */
7169 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7170 vim_modifiers |= MOUSE_SHIFT;
7171
7172 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7173 vim_modifiers |= MOUSE_CTRL;
7174 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7175 vim_modifiers |= MOUSE_ALT;
7176 string[3] |= vim_modifiers;
7177 string[4] = (char_u)(col + ' ' + 1);
7178 string[5] = (char_u)(row + ' ' + 1);
7179 add_to_input_buf(string, 6);
7180 return 6;
7181}
7182#endif /* FEAT_MOUSE_GPM */
7183
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007184#ifdef FEAT_SYSMOUSE
7185/*
7186 * Initialize connection with sysmouse.
7187 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7188 * output, any sysmouse output than will be processed via sig_sysmouse().
7189 * Return OK if succeeded, FAIL if failed.
7190 */
7191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007192sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007193{
7194 struct mouse_info mouse;
7195
7196 mouse.operation = MOUSE_MODE;
7197 mouse.u.mode.mode = 0;
7198 mouse.u.mode.signal = SIGUSR2;
7199 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7200 {
7201 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7202 mouse.operation = MOUSE_SHOW;
7203 ioctl(1, CONS_MOUSECTL, &mouse);
7204 return OK;
7205 }
7206 return FAIL;
7207}
7208
7209/*
7210 * Stop processing SIGUSR2 signals, and also make sure that
7211 * virtual console do not send us any sysmouse related signal.
7212 */
7213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007214sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007215{
7216 struct mouse_info mouse;
7217
7218 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7219 mouse.operation = MOUSE_MODE;
7220 mouse.u.mode.mode = 0;
7221 mouse.u.mode.signal = 0;
7222 ioctl(1, CONS_MOUSECTL, &mouse);
7223}
7224
7225/*
7226 * Gets info from sysmouse and adds special keys to input buf.
7227 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007228 static RETSIGTYPE
7229sig_sysmouse SIGDEFARG(sigarg)
7230{
7231 struct mouse_info mouse;
7232 struct video_info video;
7233 char_u string[6];
7234 int row, col;
7235 int button;
7236 int buttons;
7237 static int oldbuttons = 0;
7238
7239#ifdef FEAT_GUI
7240 /* Don't put events in the input queue now. */
7241 if (hold_gui_events)
7242 return;
7243#endif
7244
7245 mouse.operation = MOUSE_GETINFO;
7246 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7247 && ioctl(1, FBIO_MODEINFO, &video) != -1
7248 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7249 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7250 {
7251 row = mouse.u.data.y / video.vi_cheight;
7252 col = mouse.u.data.x / video.vi_cwidth;
7253 buttons = mouse.u.data.buttons;
7254 string[0] = ESC; /* Our termcode */
7255 string[1] = 'M';
7256 string[2] = 'S';
7257 if (oldbuttons == buttons && buttons != 0)
7258 {
7259 button = MOUSE_DRAG;
7260 }
7261 else
7262 {
7263 switch (buttons)
7264 {
7265 case 0:
7266 button = MOUSE_RELEASE;
7267 break;
7268 case 1:
7269 button = MOUSE_LEFT;
7270 break;
7271 case 2:
7272 button = MOUSE_MIDDLE;
7273 break;
7274 case 4:
7275 button = MOUSE_RIGHT;
7276 break;
7277 default:
7278 return;
7279 }
7280 oldbuttons = buttons;
7281 }
7282 string[3] = (char_u)(button);
7283 string[4] = (char_u)(col + ' ' + 1);
7284 string[5] = (char_u)(row + ' ' + 1);
7285 add_to_input_buf(string, 6);
7286 }
7287 return;
7288}
7289#endif /* FEAT_SYSMOUSE */
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007292typedef char_u * (*STRPROCSTR)(char_u *);
7293typedef char_u * (*INTPROCSTR)(int);
7294typedef int (*STRPROCINT)(char_u *);
7295typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
7297/*
7298 * Call a DLL routine which takes either a string or int param
7299 * and returns an allocated string.
7300 */
7301 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007302mch_libcall(
7303 char_u *libname,
7304 char_u *funcname,
7305 char_u *argstring, /* NULL when using a argint */
7306 int argint,
7307 char_u **string_result,/* NULL when using number_result */
7308 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
7310# if defined(USE_DLOPEN)
7311 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007312 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313# else
7314 shl_t hinstLib;
7315# endif
7316 STRPROCSTR ProcAdd;
7317 INTPROCSTR ProcAddI;
7318 char_u *retval_str = NULL;
7319 int retval_int = 0;
7320 int success = FALSE;
7321
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007322 /*
7323 * Get a handle to the DLL module.
7324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007326 /* First clear any error, it's not cleared by the dlopen() call. */
7327 (void)dlerror();
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 hinstLib = dlopen((char *)libname, RTLD_LAZY
7330# ifdef RTLD_LOCAL
7331 | RTLD_LOCAL
7332# endif
7333 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007334 if (hinstLib == NULL)
7335 {
7336 /* "dlerr" must be used before dlclose() */
7337 dlerr = (char *)dlerror();
7338 if (dlerr != NULL)
7339 EMSG2(_("dlerror = \"%s\""), dlerr);
7340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341# else
7342 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7343# endif
7344
7345 /* If the handle is valid, try to get the function address. */
7346 if (hinstLib != NULL)
7347 {
7348# ifdef HAVE_SETJMP_H
7349 /*
7350 * Catch a crash when calling the library function. For example when
7351 * using a number where a string pointer is expected.
7352 */
7353 mch_startjmp();
7354 if (SETJMP(lc_jump_env) != 0)
7355 {
7356 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007357# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007358 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 mch_didjmp();
7361 }
7362 else
7363# endif
7364 {
7365 retval_str = NULL;
7366 retval_int = 0;
7367
7368 if (argstring != NULL)
7369 {
7370# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007371 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007372 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373# else
7374 if (shl_findsym(&hinstLib, (const char *)funcname,
7375 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7376 ProcAdd = NULL;
7377# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007378 if ((success = (ProcAdd != NULL
7379# if defined(USE_DLOPEN)
7380 && dlerr == NULL
7381# endif
7382 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
7384 if (string_result == NULL)
7385 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7386 else
7387 retval_str = (ProcAdd)(argstring);
7388 }
7389 }
7390 else
7391 {
7392# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007393 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007394 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395# else
7396 if (shl_findsym(&hinstLib, (const char *)funcname,
7397 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7398 ProcAddI = NULL;
7399# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007400 if ((success = (ProcAddI != NULL
7401# if defined(USE_DLOPEN)
7402 && dlerr == NULL
7403# endif
7404 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 {
7406 if (string_result == NULL)
7407 retval_int = ((INTPROCINT)ProcAddI)(argint);
7408 else
7409 retval_str = (ProcAddI)(argint);
7410 }
7411 }
7412
7413 /* Save the string before we free the library. */
7414 /* Assume that a "1" or "-1" result is an illegal pointer. */
7415 if (string_result == NULL)
7416 *number_result = retval_int;
7417 else if (retval_str != NULL
7418 && retval_str != (char_u *)1
7419 && retval_str != (char_u *)-1)
7420 *string_result = vim_strsave(retval_str);
7421 }
7422
7423# ifdef HAVE_SETJMP_H
7424 mch_endjmp();
7425# ifdef SIGHASARG
7426 if (lc_signal != 0)
7427 {
7428 int i;
7429
7430 /* try to find the name of this signal */
7431 for (i = 0; signal_info[i].sig != -1; i++)
7432 if (lc_signal == signal_info[i].sig)
7433 break;
7434 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7435 }
7436# endif
7437# endif
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007440 /* "dlerr" must be used before dlclose() */
7441 if (dlerr != NULL)
7442 EMSG2(_("dlerror = \"%s\""), dlerr);
7443
7444 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 (void)dlclose(hinstLib);
7446# else
7447 (void)shl_unload(hinstLib);
7448# endif
7449 }
7450
7451 if (!success)
7452 {
7453 EMSG2(_(e_libcall), funcname);
7454 return FAIL;
7455 }
7456
7457 return OK;
7458}
7459#endif
7460
7461#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7462static int xterm_trace = -1; /* default: disabled */
7463static int xterm_button;
7464
7465/*
7466 * Setup a dummy window for X selections in a terminal.
7467 */
7468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007469setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
7471 int z = 0;
7472 char *strp = "";
7473 Widget AppShell;
7474
7475 if (!x_connect_to_server())
7476 return;
7477
7478 open_app_context();
7479 if (app_context != NULL && xterm_Shell == (Widget)0)
7480 {
7481 int (*oldhandler)();
7482#if defined(HAVE_SETJMP_H)
7483 int (*oldIOhandler)();
7484#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007485# ifdef ELAPSED_FUNC
7486 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007489 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490# endif
7491
7492 /* Ignore X errors while opening the display */
7493 oldhandler = XSetErrorHandler(x_error_check);
7494
7495#if defined(HAVE_SETJMP_H)
7496 /* Ignore X IO errors while opening the display */
7497 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7498 mch_startjmp();
7499 if (SETJMP(lc_jump_env) != 0)
7500 {
7501 mch_didjmp();
7502 xterm_dpy = NULL;
7503 }
7504 else
7505#endif
7506 {
7507 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7508 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007509 if (xterm_dpy != NULL)
7510 xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511#if defined(HAVE_SETJMP_H)
7512 mch_endjmp();
7513#endif
7514 }
7515
7516#if defined(HAVE_SETJMP_H)
7517 /* Now handle X IO errors normally. */
7518 (void)XSetIOErrorHandler(oldIOhandler);
7519#endif
7520 /* Now handle X errors normally. */
7521 (void)XSetErrorHandler(oldhandler);
7522
7523 if (xterm_dpy == NULL)
7524 {
7525 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007526 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 return;
7528 }
7529
7530 /* Catch terminating error of the X server connection. */
7531 (void)XSetIOErrorHandler(x_IOerror_handler);
7532
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007533# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007535 {
7536 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007537 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007538 verbose_leave();
7539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540# endif
7541
7542 /* Create a Shell to make converters work. */
7543 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7544 applicationShellWidgetClass, xterm_dpy,
7545 NULL);
7546 if (AppShell == (Widget)0)
7547 return;
7548 xterm_Shell = XtVaCreatePopupShell("VIM",
7549 topLevelShellWidgetClass, AppShell,
7550 XtNmappedWhenManaged, 0,
7551 XtNwidth, 1,
7552 XtNheight, 1,
7553 NULL);
7554 if (xterm_Shell == (Widget)0)
7555 return;
7556
7557 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007558 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (x11_display == NULL)
7560 x11_display = xterm_dpy;
7561
7562 XtRealizeWidget(xterm_Shell);
7563 XSync(xterm_dpy, False);
7564 xterm_update();
7565 }
7566 if (xterm_Shell != (Widget)0)
7567 {
7568 clip_init(TRUE);
7569 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7570 x11_window = (Window)atol(strp);
7571 /* Check if $WINDOWID is valid. */
7572 if (test_x11_window(xterm_dpy) == FAIL)
7573 x11_window = 0;
7574 if (x11_window != 0)
7575 xterm_trace = 0;
7576 }
7577}
7578
7579 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007580start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7583 return;
7584 xterm_trace = 1;
7585 xterm_button = button;
7586 do_xterm_trace();
7587}
7588
7589
7590 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007591stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 if (xterm_trace < 0)
7594 return;
7595 xterm_trace = 0;
7596}
7597
7598/*
7599 * Query the xterm pointer and generate mouse termcodes if necessary
7600 * return TRUE if dragging is active, else FALSE
7601 */
7602 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007603do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 Window root, child;
7606 int root_x, root_y;
7607 int win_x, win_y;
7608 int row, col;
7609 int_u mask_return;
7610 char_u buf[50];
7611 char_u *strp;
7612 long got_hints;
7613 static char_u *mouse_code;
7614 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7615 static int prev_row = 0, prev_col = 0;
7616 static XSizeHints xterm_hints;
7617
7618 if (xterm_trace <= 0)
7619 return FALSE;
7620
7621 if (xterm_trace == 1)
7622 {
7623 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007624 * have changed recently. */
7625 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7626 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 || xterm_hints.width_inc <= 1
7628 || xterm_hints.height_inc <= 1)
7629 {
7630 xterm_trace = -1; /* Not enough data -- disable tracing */
7631 return FALSE;
7632 }
7633
7634 /* Rely on the same mouse code for the duration of this */
7635 mouse_code = find_termcode(mouse_name);
7636 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007637 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 xterm_trace = 2;
7639
7640 /* Find the offset of the chars, there might be a scrollbar on the
7641 * left of the window and/or a menu on the top (eterm etc.) */
7642 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7643 &win_x, &win_y, &mask_return);
7644 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7645 - (xterm_hints.height_inc / 2);
7646 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7647 xterm_hints.y = 2;
7648 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7649 - (xterm_hints.width_inc / 2);
7650 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7651 xterm_hints.x = 2;
7652 return TRUE;
7653 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007654 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656 xterm_trace = 0;
7657 return FALSE;
7658 }
7659
7660 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7661 &win_x, &win_y, &mask_return);
7662
7663 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7664 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7665 if (row == prev_row && col == prev_col)
7666 return TRUE;
7667
7668 STRCPY(buf, mouse_code);
7669 strp = buf + STRLEN(buf);
7670 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7671 *strp++ = (char_u)(col + ' ' + 1);
7672 *strp++ = (char_u)(row + ' ' + 1);
7673 *strp = 0;
7674 add_to_input_buf(buf, STRLEN(buf));
7675
7676 prev_row = row;
7677 prev_col = col;
7678 return TRUE;
7679}
7680
7681# if defined(FEAT_GUI) || defined(PROTO)
7682/*
7683 * Destroy the display, window and app_context. Required for GTK.
7684 */
7685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007686clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
7688 if (xterm_Shell != (Widget)0)
7689 {
7690 XtDestroyWidget(xterm_Shell);
7691 xterm_Shell = (Widget)0;
7692 }
7693 if (xterm_dpy != NULL)
7694 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007695# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 /* Lesstif and Solaris crash here, lose some memory */
7697 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (x11_display == xterm_dpy)
7700 x11_display = NULL;
7701 xterm_dpy = NULL;
7702 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007703# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (app_context != (XtAppContext)NULL)
7705 {
7706 /* Lesstif and Solaris crash here, lose some memory */
7707 XtDestroyApplicationContext(app_context);
7708 app_context = (XtAppContext)NULL;
7709 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712# endif
7713
7714/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007715 * Catch up with GUI or X events.
7716 */
7717 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007718clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007719{
7720# ifdef FEAT_GUI
7721 if (gui.in_use)
7722 gui_mch_update();
7723 else
7724# endif
7725 if (xterm_Shell != (Widget)0)
7726 xterm_update();
7727}
7728
7729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 * Catch up with any queued X events. This may put keyboard input into the
7731 * input buffer, call resize call-backs, trigger timers etc. If there is
7732 * nothing in the X event queue (& no timers pending), then we return
7733 * immediately.
7734 */
7735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007736xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738 XEvent event;
7739
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007740 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007742 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007744 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007745 break;
7746
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007747 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007748 {
7749 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007750 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007751#ifdef FEAT_CLIENTSERVER
7752 {
7753 XPropertyEvent *e = (XPropertyEvent *)&event;
7754
7755 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007757 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007760 XtDispatchEvent(&event);
7761 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007762 else
7763 {
7764 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007765 XtAppProcessEvent(app_context, mask);
7766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 }
7768}
7769
7770 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007771clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 if (xterm_Shell != (Widget)0)
7774 return clip_x11_own_selection(xterm_Shell, cbd);
7775 return FAIL;
7776}
7777
7778 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007779clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 if (xterm_Shell != (Widget)0)
7782 clip_x11_lose_selection(xterm_Shell, cbd);
7783}
7784
7785 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007786clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 if (xterm_Shell != (Widget)0)
7789 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7790}
7791
7792 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007793clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 clip_x11_set_selection(cbd);
7796}
7797#endif
7798
7799
7800#if defined(USE_XSMP) || defined(PROTO)
7801/*
7802 * Code for X Session Management Protocol.
7803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806/*
7807 * This is our chance to ask the user if they want to save,
7808 * or abort the logout
7809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007811xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 cmdmod_T save_cmdmod;
7814 int cancel_shutdown = False;
7815
7816 save_cmdmod = cmdmod;
7817 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007818 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 /* Mustn't logout */
7820 cancel_shutdown = True;
7821 cmdmod = save_cmdmod;
7822 setcursor(); /* position cursor */
7823 out_flush();
7824
7825 /* Done interaction */
7826 SmcInteractDone(smc_conn, cancel_shutdown);
7827
7828 /* Finish off
7829 * Only end save-yourself here if we're not cancelling shutdown;
7830 * we'll get a cancelled callback later in which we'll end it.
7831 * Hopefully get around glitchy SMs (like GNOME-1)
7832 */
7833 if (!cancel_shutdown)
7834 {
7835 xsmp.save_yourself = False;
7836 SmcSaveYourselfDone(smc_conn, True);
7837 }
7838}
7839# endif
7840
7841/*
7842 * Callback that starts save-yourself.
7843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007845xsmp_handle_save_yourself(
7846 SmcConn smc_conn,
7847 SmPointer client_data UNUSED,
7848 int save_type UNUSED,
7849 Bool shutdown,
7850 int interact_style UNUSED,
7851 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 /* Handle already being in saveyourself */
7854 if (xsmp.save_yourself)
7855 SmcSaveYourselfDone(smc_conn, True);
7856 xsmp.save_yourself = True;
7857 xsmp.shutdown = shutdown;
7858
7859 /* First up, preserve all files */
7860 out_flush();
7861 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7862
7863 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007864 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
7866# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7867 /* Now see if we can ask about unsaved files */
7868 if (shutdown && !fast && gui.in_use)
7869 /* Need to interact with user, but need SM's permission */
7870 SmcInteractRequest(smc_conn, SmDialogError,
7871 xsmp_handle_interaction, client_data);
7872 else
7873# endif
7874 {
7875 /* Can stop the cycle here */
7876 SmcSaveYourselfDone(smc_conn, True);
7877 xsmp.save_yourself = False;
7878 }
7879}
7880
7881
7882/*
7883 * Callback to warn us of imminent death.
7884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007886xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 xsmp_close();
7889
7890 /* quit quickly leaving swapfiles for modified buffers behind */
7891 getout_preserve_modified(0);
7892}
7893
7894
7895/*
7896 * Callback to tell us that save-yourself has completed.
7897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007899xsmp_save_complete(
7900 SmcConn smc_conn UNUSED,
7901 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 xsmp.save_yourself = False;
7904}
7905
7906
7907/*
7908 * Callback to tell us that an instigated shutdown was cancelled
7909 * (maybe even by us)
7910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007912xsmp_shutdown_cancelled(
7913 SmcConn smc_conn,
7914 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 if (xsmp.save_yourself)
7917 SmcSaveYourselfDone(smc_conn, True);
7918 xsmp.save_yourself = False;
7919 xsmp.shutdown = False;
7920}
7921
7922
7923/*
7924 * Callback to tell us that a new ICE connection has been established.
7925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007927xsmp_ice_connection(
7928 IceConn iceConn,
7929 IcePointer clientData UNUSED,
7930 Bool opening,
7931 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 /* Intercept creation of ICE connection fd */
7934 if (opening)
7935 {
7936 xsmp_icefd = IceConnectionNumber(iceConn);
7937 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7938 }
7939}
7940
7941
7942/* Handle any ICE processing that's required; return FAIL if SM lost */
7943 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007944xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 Bool rep;
7947
7948 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7949 == IceProcessMessagesIOError)
7950 {
7951 /* Lost ICE */
7952 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007953 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 xsmp_close();
7955 return FAIL;
7956 }
7957 else
7958 return OK;
7959}
7960
7961static int dummy;
7962
7963/* Set up X Session Management Protocol */
7964 void
7965xsmp_init(void)
7966{
7967 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 SmcCallbacks smcallbacks;
7969#if 0
7970 SmPropValue smname;
7971 SmProp smnameprop;
7972 SmProp *smprops[1];
7973#endif
7974
7975 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007976 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 xsmp.save_yourself = xsmp.shutdown = False;
7979
7980 /* Set up SM callbacks - must have all, even if they're not used */
7981 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7982 smcallbacks.save_yourself.client_data = NULL;
7983 smcallbacks.die.callback = xsmp_die;
7984 smcallbacks.die.client_data = NULL;
7985 smcallbacks.save_complete.callback = xsmp_save_complete;
7986 smcallbacks.save_complete.client_data = NULL;
7987 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7988 smcallbacks.shutdown_cancelled.client_data = NULL;
7989
7990 /* Set up a watch on ICE connection creations. The "dummy" argument is
7991 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7992 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7993 {
7994 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007995 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 return;
7997 }
7998
7999 /* Create an SM connection */
8000 xsmp.smcconn = SmcOpenConnection(
8001 NULL,
8002 NULL,
8003 SmProtoMajor,
8004 SmProtoMinor,
8005 SmcSaveYourselfProcMask | SmcDieProcMask
8006 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8007 &smcallbacks,
8008 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008009 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008010 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 errorstring);
8012 if (xsmp.smcconn == NULL)
8013 {
8014 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008015
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008017 {
8018 vim_snprintf(errorreport, sizeof(errorreport),
8019 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8020 verb_msg((char_u *)errorreport);
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 return;
8023 }
8024 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8025
8026#if 0
8027 /* ID ourselves */
8028 smname.value = "vim";
8029 smname.length = 3;
8030 smnameprop.name = "SmProgram";
8031 smnameprop.type = "SmARRAY8";
8032 smnameprop.num_vals = 1;
8033 smnameprop.vals = &smname;
8034
8035 smprops[0] = &smnameprop;
8036 SmcSetProperties(xsmp.smcconn, 1, smprops);
8037#endif
8038}
8039
8040
8041/* Shut down XSMP comms. */
8042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008043xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 if (xsmp_icefd != -1)
8046 {
8047 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008048 if (xsmp.clientid != NULL)
8049 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008050 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 xsmp_icefd = -1;
8052 }
8053}
8054#endif /* USE_XSMP */
8055
8056
8057#ifdef EBCDIC
8058/* Translate character to its CTRL- value */
8059char CtrlTable[] =
8060{
8061/* 00 - 5E */
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, 0,
8064 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8065 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8066 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8067 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8068/* ^ */ 0x1E,
8069/* - */ 0x1F,
8070/* 61 - 6C */
8071 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8072/* _ */ 0x1F,
8073/* 6E - 80 */
8074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8075/* a */ 0x01,
8076/* b */ 0x02,
8077/* c */ 0x03,
8078/* d */ 0x37,
8079/* e */ 0x2D,
8080/* f */ 0x2E,
8081/* g */ 0x2F,
8082/* h */ 0x16,
8083/* i */ 0x05,
8084/* 8A - 90 */
8085 0, 0, 0, 0, 0, 0, 0,
8086/* j */ 0x15,
8087/* k */ 0x0B,
8088/* l */ 0x0C,
8089/* m */ 0x0D,
8090/* n */ 0x0E,
8091/* o */ 0x0F,
8092/* p */ 0x10,
8093/* q */ 0x11,
8094/* r */ 0x12,
8095/* 9A - A1 */
8096 0, 0, 0, 0, 0, 0, 0, 0,
8097/* s */ 0x13,
8098/* t */ 0x3C,
8099/* u */ 0x3D,
8100/* v */ 0x32,
8101/* w */ 0x26,
8102/* x */ 0x18,
8103/* y */ 0x19,
8104/* z */ 0x3F,
8105/* AA - AC */
8106 0, 0, 0,
8107/* [ */ 0x27,
8108/* AE - BC */
8109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8110/* ] */ 0x1D,
8111/* BE - C0 */ 0, 0, 0,
8112/* A */ 0x01,
8113/* B */ 0x02,
8114/* C */ 0x03,
8115/* D */ 0x37,
8116/* E */ 0x2D,
8117/* F */ 0x2E,
8118/* G */ 0x2F,
8119/* H */ 0x16,
8120/* I */ 0x05,
8121/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8122/* J */ 0x15,
8123/* K */ 0x0B,
8124/* L */ 0x0C,
8125/* M */ 0x0D,
8126/* N */ 0x0E,
8127/* O */ 0x0F,
8128/* P */ 0x10,
8129/* Q */ 0x11,
8130/* R */ 0x12,
8131/* DA - DF */ 0, 0, 0, 0, 0, 0,
8132/* \ */ 0x1C,
8133/* E1 */ 0,
8134/* S */ 0x13,
8135/* T */ 0x3C,
8136/* U */ 0x3D,
8137/* V */ 0x32,
8138/* W */ 0x26,
8139/* X */ 0x18,
8140/* Y */ 0x19,
8141/* Z */ 0x3F,
8142/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8144};
8145
8146char MetaCharTable[]=
8147{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8148 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8149 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8150 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8151 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8152};
8153
8154
8155/* TODO: Use characters NOT numbers!!! */
8156char CtrlCharTable[]=
8157{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8158 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8159 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8160 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8161 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8162};
8163
8164
8165#endif