blob: 39ace2e295177ab01cb98965ea1cfd906a8e7ef5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
15 * Also for BeOS and Atari MiNT.
16 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
21/*
22 * Some systems have a prototype for select() that has (int *) instead of
23 * (fd_set *), which is wrong. This define removes that prototype. We define
24 * our own prototype below.
25 * Don't use it for the Mac, it causes a warning for precompiled headers.
26 * TODO: use a configure check for precompiled headers?
27 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000028#if !defined(__APPLE__) && !defined(__TANDEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +000029# define select select_declared_wrong
30#endif
31
32#include "vim.h"
33
Bram Moolenaar325b7a22004-07-05 15:58:32 +000034#ifdef FEAT_MZSCHEME
35# include "if_mzsch.h"
36#endif
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#include "os_unixx.h" /* unix includes for os_unix.c only */
39
40#ifdef USE_XSMP
41# include <X11/SM/SMlib.h>
42#endif
43
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000044#ifdef HAVE_SELINUX
45# include <selinux/selinux.h>
46static int selinux_enabled = -1;
47#endif
48
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020049#ifdef HAVE_SMACK
50# include <attr/xattr.h>
51# include <linux/xattr.h>
52# ifndef SMACK_LABEL_LEN
53# define SMACK_LABEL_LEN 1024
54# endif
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057/*
58 * Use this prototype for select, some include files have a wrong prototype
59 */
Bram Moolenaar311d9822007-02-27 15:48:28 +000060#ifndef __TANDEM
61# undef select
62# ifdef __BEOS__
63# define select beos_select
64# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
66
Bram Moolenaara2442432007-04-26 14:26:37 +000067#ifdef __CYGWIN__
68# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000069# include <cygwin/version.h>
70# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
71 * for cygwin_conv_path() */
Bram Moolenaar693e40c2013-02-26 14:56:42 +010072# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
73# define WIN32_LEAN_AND_MEAN
74# include <windows.h>
75# include "winclip.pro"
76# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000077# endif
78#endif
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#if defined(HAVE_SELECT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
83
84#ifdef FEAT_MOUSE_GPM
85# include <gpm.h>
86/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
87 * I just copied relevant defines here. A cleaner solution would be to put gpm
88 * code into separate file and include there linux/keyboard.h
89 */
90/* #include <linux/keyboard.h> */
91# define KG_SHIFT 0
92# define KG_CTRL 2
93# define KG_ALT 3
94# define KG_ALTGR 1
95# define KG_SHIFTL 4
96# define KG_SHIFTR 5
97# define KG_CTRLL 6
98# define KG_CTRLR 7
99# define KG_CAPSSHIFT 8
100
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void gpm_close(void);
102static int gpm_open(void);
103static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000106#ifdef FEAT_SYSMOUSE
107# include <sys/consio.h>
108# include <sys/fbio.h>
109
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static int sysmouse_open(void);
111static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +0100112static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000113#endif
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115/*
116 * end of autoconf section. To be extended...
117 */
118
119/* Are the following #ifdefs still required? And why? Is that for X11? */
120
121#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
122# ifdef SIGWINCH
123# undef SIGWINCH
124# endif
125# ifdef TIOCGWINSZ
126# undef TIOCGWINSZ
127# endif
128#endif
129
130#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
131# define SIGWINCH SIGWINDOW
132#endif
133
134#ifdef FEAT_X11
135# include <X11/Xlib.h>
136# include <X11/Xutil.h>
137# include <X11/Xatom.h>
138# ifdef FEAT_XCLIPBOARD
139# include <X11/Intrinsic.h>
140# include <X11/Shell.h>
141# include <X11/StringDefs.h>
142static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void clip_update(void);
144static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
146
147# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
148Window x11_window = 0;
149# endif
150Display *x11_display = NULL;
151
152# ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static int get_x11_windis(void);
154static void set_x11_title(char_u *);
155static void set_x11_icon(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156# endif
157#endif
158
159#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int get_x11_title(int);
161static int get_x11_icon(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163static char_u *oldtitle = NULL;
Bram Moolenaard8f0cef2018-08-19 22:20:16 +0200164static volatile int oldtitle_outdated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static int did_set_title = FALSE;
166static char_u *oldicon = NULL;
167static int did_set_icon = FALSE;
168#endif
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar205b8862011-09-07 15:04:31 +0200172#ifdef HAVE_UNION_WAIT
173typedef union wait waitstatus;
174#else
175typedef int waitstatus;
176#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static pid_t wait4pid(pid_t, waitstatus *);
Bram Moolenaar205b8862011-09-07 15:04:31 +0200178
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200179static int WaitForChar(long msec, int *interrupted, int ignore_input);
180static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100181#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200182int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200184static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186
187#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100188static int do_xterm_trace(void);
Bram Moolenaarcf851ce2005-06-16 21:52:47 +0000189# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
191
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100192static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100195static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif
197#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100198static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#endif
200#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100201static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203#if defined(SIGALRM) && defined(FEAT_X11) \
204 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
205# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100206static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000207/* volatile because it is used in signal handler sig_alarm(). */
208static volatile int sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100210static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100212static void catch_int_signal(void);
213static void set_signals(void);
214static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200215#ifdef HAVE_SIGPROCMASK
216# define SIGSET_DECL(set) sigset_t set;
217# define BLOCK_SIGNALS(set) block_signals(set)
218# define UNBLOCK_SIGNALS(set) unblock_signals(set)
219#else
220# define SIGSET_DECL(set)
221# define BLOCK_SIGNALS(set) do { /**/ } while (0)
222# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
223#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100224static int have_wildcard(int, char_u **);
225static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100227static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000230# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#endif
232
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000233/* volatile because it is used in signal handler sig_winch(). */
234static volatile int do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235static char_u *extra_shell_arg = NULL;
236static int show_shell_mess = TRUE;
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000237/* volatile because it is used in signal handler deathtrap(). */
238static volatile int deadly_signal = 0; /* The signal we caught */
239/* volatile because it is used in signal handler deathtrap(). */
240static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100242#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
243static int dont_check_job_ended = 0;
244#endif
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
247
248#ifdef USE_XSMP
249typedef struct
250{
251 SmcConn smcconn; /* The SM connection ID */
252 IceConn iceconn; /* The ICE connection ID */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200253 char *clientid; /* The client ID for the current smc session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 Bool save_yourself; /* If we're in the middle of a save_yourself */
255 Bool shutdown; /* If we're in shutdown mode */
256} xsmp_config_T;
257
258static xsmp_config_T xsmp;
259#endif
260
261#ifdef SYS_SIGLIST_DECLARED
262/*
263 * I have seen
264 * extern char *_sys_siglist[NSIG];
265 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
266 * that describe the signals. That is nearly what we want here. But
267 * autoconf does only check for sys_siglist (without the underscore), I
268 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100269 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 */
271#endif
272
273static struct signalinfo
274{
275 int sig; /* Signal number, eg. SIGSEGV etc */
276 char *name; /* Signal name (not char_u!). */
277 char deadly; /* Catch as a deadly signal? */
278} signal_info[] =
279{
280#ifdef SIGHUP
281 {SIGHUP, "HUP", TRUE},
282#endif
283#ifdef SIGQUIT
284 {SIGQUIT, "QUIT", TRUE},
285#endif
286#ifdef SIGILL
287 {SIGILL, "ILL", TRUE},
288#endif
289#ifdef SIGTRAP
290 {SIGTRAP, "TRAP", TRUE},
291#endif
292#ifdef SIGABRT
293 {SIGABRT, "ABRT", TRUE},
294#endif
295#ifdef SIGEMT
296 {SIGEMT, "EMT", TRUE},
297#endif
298#ifdef SIGFPE
299 {SIGFPE, "FPE", TRUE},
300#endif
301#ifdef SIGBUS
302 {SIGBUS, "BUS", TRUE},
303#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100304#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
305 /* MzScheme uses SEGV in its garbage collector */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 {SIGSEGV, "SEGV", TRUE},
307#endif
308#ifdef SIGSYS
309 {SIGSYS, "SYS", TRUE},
310#endif
311#ifdef SIGALRM
312 {SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
313#endif
314#ifdef SIGTERM
315 {SIGTERM, "TERM", TRUE},
316#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100317#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 {SIGVTALRM, "VTALRM", TRUE},
319#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000320#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
321 /* MzScheme uses SIGPROF for its own needs; On Linux with profiling
322 * this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {SIGPROF, "PROF", TRUE},
324#endif
325#ifdef SIGXCPU
326 {SIGXCPU, "XCPU", TRUE},
327#endif
328#ifdef SIGXFSZ
329 {SIGXFSZ, "XFSZ", TRUE},
330#endif
331#ifdef SIGUSR1
332 {SIGUSR1, "USR1", TRUE},
333#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000334#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
335 /* Used for sysmouse handling */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {SIGUSR2, "USR2", TRUE},
337#endif
338#ifdef SIGINT
339 {SIGINT, "INT", FALSE},
340#endif
341#ifdef SIGWINCH
342 {SIGWINCH, "WINCH", FALSE},
343#endif
344#ifdef SIGTSTP
345 {SIGTSTP, "TSTP", FALSE},
346#endif
347#ifdef SIGPIPE
348 {SIGPIPE, "PIPE", FALSE},
349#endif
350 {-1, "Unknown!", FALSE}
351};
352
Bram Moolenaar25724922009-07-14 15:38:41 +0000353 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100354mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000355{
356 if (p_verbose >= 5)
357 {
358 verbose_enter();
359 smsg((char_u *)"chdir(%s)", path);
360 verbose_leave();
361 }
362# ifdef VMS
363 return chdir(vms_fixfilename(path));
364# else
365 return chdir(path);
366# endif
367}
368
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200369/* Why is NeXT excluded here (and not in os_unixx.h)? */
370#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
371# define NEW_TTY_SYSTEM
372#endif
373
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000374/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100375 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100378mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000380 ignored = (int)write(1, (char *)s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (p_wd) /* Unix is too fast, slow down a bit more */
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100382 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383}
384
385/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000386 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 * Get a characters from the keyboard.
388 * Return the number of characters that are available.
389 * If wtime == 0 do not wait for characters.
390 * If wtime == n wait a short time for characters.
391 * If wtime == -1 wait forever for characters.
392 */
393 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100394mch_inchar(
395 char_u *buf,
396 int maxlen,
397 long wtime, /* don't use "time", MIPS cannot handle it */
398 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
400 int len;
Bram Moolenaarcda77642016-06-04 13:32:35 +0200401 int interrupted = FALSE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200402 int did_start_blocking = FALSE;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200403 long wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200404 long elapsed_time = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100405#ifdef ELAPSED_FUNC
406 ELAPSED_TYPE start_tv;
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200407
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100408 ELAPSED_INIT(start_tv);
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200411 /* repeat until we got a character or waited long enough */
Bram Moolenaare30a3d02016-06-04 14:11:20 +0200412 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200414 /* Check if window changed size while we were busy, perhaps the ":set
415 * columns=99" command was used. */
416 while (do_resize)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 handle_resize();
Bram Moolenaar67c53842010-05-22 18:28:27 +0200418
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200419#ifdef MESSAGE_QUEUE
420 parse_queued_messages();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200421 /* If input was put directly in typeahead buffer bail out here. */
422 if (typebuf_changed(tb_change_cnt))
423 return 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200424#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200425 if (wtime < 0 && did_start_blocking)
426 /* blocking and already waited for p_ut */
427 wait_time = -1;
428 else
429 {
430 if (wtime >= 0)
431 wait_time = wtime;
432 else
433 /* going to block after p_ut */
434 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100435#ifdef ELAPSED_FUNC
436 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200437#endif
438 wait_time -= elapsed_time;
439 if (wait_time < 0)
440 {
441 if (wtime >= 0)
442 /* no character available within "wtime" */
443 return 0;
444
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200445 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200446 {
447 /* no character available within 'updatetime' */
448 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200449 if (trigger_cursorhold() && maxlen >= 3
450 && !typebuf_changed(tb_change_cnt))
451 {
452 buf[0] = K_SPECIAL;
453 buf[1] = KS_EXTRA;
454 buf[2] = (int)KE_CURSORHOLD;
455 return 3;
456 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200457 /*
458 * If there is no character available within 'updatetime'
459 * seconds flush all the swap files to disk.
460 * Also done when interrupted by SIGWINCH.
461 */
462 before_blocking();
463 continue;
464 }
465 }
466 }
467
468#ifdef FEAT_JOB_CHANNEL
469 /* Checking if a job ended requires polling. Do this every 100 msec. */
470 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
471 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100472 /* If there is readahead then parse_queued_messages() timed out and we
473 * should call it again soon. */
474 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
475 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200476#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100477#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100478 if (p_beval && wait_time > 100L)
479 /* The 'balloonexpr' may indirectly invoke a callback while waiting
480 * for a character, need to check often. */
481 wait_time = 100L;
482#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200485 * We want to be interrupted by the winch signal
486 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200488 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200489 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200490 /* If input was put directly in typeahead buffer bail out here. */
491 if (typebuf_changed(tb_change_cnt))
492 return 0;
493
494 /*
495 * For some terminals we only get one character at a time.
496 * We want the get all available characters, so we could keep on
497 * trying until none is available
498 * For some other terminals this is quite slow, that's why we don't
499 * do it.
500 */
501 len = read_from_input_buf(buf, (long)maxlen);
502 if (len > 0)
503 return len;
504 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200507 /* no character available */
508#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
509 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100510 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200513 if (do_resize /* interrupted by SIGWINCH signal */
514#ifdef FEAT_CLIENTSERVER
515 || server_waiting()
516#endif
517#ifdef MESSAGE_QUEUE
518 || interrupted
519#endif
520 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100521 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200522 continue;
523
524 /* no character available or interrupted */
525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200527 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528}
529
530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100531handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533 do_resize = FALSE;
534 shell_resized();
535}
536
537/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200538 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
540 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100541mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200543 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544}
545
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200546#if defined(FEAT_TERMINAL) || defined(PROTO)
547/*
548 * Check for any pending input or messages.
549 */
550 int
551mch_check_messages(void)
552{
553 return WaitForChar(0L, NULL, TRUE);
554}
555#endif
556
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
558# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000559# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560# endif
561# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
562# include <sys/sysctl.h>
563# endif
564# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
565# include <sys/sysinfo.h>
566# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100567# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100568# include <mach/mach_host.h>
569# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000573 * Return total amount of memory available in Kbyte.
574 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100577mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000580 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar362dc332018-03-05 21:59:37 +0100582# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100583 {
584 /* Mac (Darwin) way of getting the amount of RAM available */
585 mach_port_t host = mach_host_self();
586 kern_return_t kret;
587# ifdef HOST_VM_INFO64
588 struct vm_statistics64 vm_stat;
589 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar988615f2018-02-27 17:58:20 +0100591 kret = host_statistics64(host, HOST_VM_INFO64,
592 (host_info64_t)&vm_stat, &count);
593# else
594 struct vm_statistics vm_stat;
595 natural_t count = HOST_VM_INFO_COUNT;
596
597 kret = host_statistics(host, HOST_VM_INFO,
598 (host_info_t)&vm_stat, &count);
599# endif
600 if (kret == KERN_SUCCESS)
601 /* get the amount of user memory by summing each usage */
602 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
603 + vm_stat.inactive_count
604# ifdef MAC_OS_X_VERSION_10_9
605 + vm_stat.compressor_page_count
606# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100607 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100608 mach_port_deallocate(mach_task_self(), host);
609 }
610# endif
611
612# ifdef HAVE_SYSCTL
613 if (mem == 0)
614 {
615 /* BSD way of getting the amount of RAM available. */
616 int mib[2];
617 size_t len = sizeof(long_u);
618# ifdef HW_USERMEM64
619 long_u physmem;
620# else
621 /* sysctl() may return 32 bit or 64 bit, accept both */
622 union {
623 int_u u32;
624 long_u u64;
625 } physmem;
626# endif
627
628 mib[0] = CTL_HW;
629# ifdef HW_USERMEM64
630 mib[1] = HW_USERMEM64;
631# else
632 mib[1] = HW_USERMEM;
633# endif
634 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
635 {
636# ifdef HW_USERMEM64
637 mem = (long_u)physmem;
638# else
639 if (len == sizeof(physmem.u64))
640 mem = (long_u)physmem.u64;
641 else
642 mem = (long_u)physmem.u32;
643# endif
644 }
645 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (mem == 0)
650 {
651 struct sysinfo sinfo;
652
653 /* Linux way of getting amount of RAM available */
654 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200656# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000657 /* avoid overflow as much as possible */
658 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
659 {
660 sinfo.mem_unit = sinfo.mem_unit >> 1;
661 --shiftright;
662 }
663 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200664# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200666# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200671# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (mem == 0)
673 {
674 long pagesize, pagecount;
675
676 /* Solaris way of getting amount of RAM available */
677 pagesize = sysconf(_SC_PAGESIZE);
678 pagecount = sysconf(_SC_PHYS_PAGES);
679 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 {
681 /* avoid overflow as much as possible */
682 while (shiftright > 0 && (pagesize & 1) == 0)
683 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000684 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000685 --shiftright;
686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* Return the minimum of the physical memory and the user limit, because
693 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200694# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
696 struct rlimit rlp;
697
698 if (getrlimit(RLIMIT_DATA, &rlp) == 0
699 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200700# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200702# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000703 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000705 {
706 mem = (long_u)rlp.rlim_cur;
707 shiftright = 10;
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000713 return mem >> shiftright;
714 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715}
716#endif
717
718 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100719mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000722#ifdef FEAT_MZSCHEME
723 long total = msec; /* remember original value */
724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
726 if (ignoreinput)
727 {
728 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000729 * here. But we don't want QUIT to kill us (CTRL-\ used in a
730 * shell may produce SIGQUIT). */
731 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 old_tmode = curr_tmode;
733 if (curr_tmode == TMODE_RAW)
734 settmode(TMODE_SLEEP);
735
736 /*
737 * Everybody sleeps in a different way...
738 * Prefer nanosleep(), some versions of usleep() can only sleep up to
739 * one second.
740 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000741#ifdef FEAT_MZSCHEME
742 do
743 {
744 /* if total is large enough, wait by portions in p_mzq */
745 if (total > p_mzq)
746 msec = p_mzq;
747 else
748 msec = total;
749 total -= msec;
750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#ifdef HAVE_NANOSLEEP
752 {
753 struct timespec ts;
754
755 ts.tv_sec = msec / 1000;
756 ts.tv_nsec = (msec % 1000) * 1000000;
757 (void)nanosleep(&ts, NULL);
758 }
759#else
760# ifdef HAVE_USLEEP
761 while (msec >= 1000)
762 {
763 usleep((unsigned int)(999 * 1000));
764 msec -= 999;
765 }
766 usleep((unsigned int)(msec * 1000));
767# else
768# ifndef HAVE_SELECT
769 poll(NULL, 0, (int)msec);
770# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 struct timeval tv;
773
774 tv.tv_sec = msec / 1000;
775 tv.tv_usec = (msec % 1000) * 1000;
776 /*
777 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
778 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
779 */
780 select(0, NULL, NULL, NULL, &tv);
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782# endif /* HAVE_SELECT */
783# endif /* HAVE_NANOSLEEP */
784#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000785#ifdef FEAT_MZSCHEME
786 }
787 while (total > 0);
788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000791 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200794 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000797#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
799# define HAVE_CHECK_STACK_GROWTH
800/*
801 * Support for checking for an almost-out-of-stack-space situation.
802 */
803
804/*
805 * Return a pointer to an item on the stack. Used to find out if the stack
806 * grows up or down.
807 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100808static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809static int stack_grows_downwards;
810
811/*
812 * Find out if the stack grows upwards or downwards.
813 * "p" points to a variable on the stack of the caller.
814 */
815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100816check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 int i;
819
820 stack_grows_downwards = (p > (char *)&i);
821}
822#endif
823
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000824#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825static char *stack_limit = NULL;
826
827#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
828# include <pthread.h>
829# include <pthread_np.h>
830#endif
831
832/*
833 * Find out until how var the stack can grow without getting into trouble.
834 * Called when starting up and when switching to the signal stack in
835 * deathtrap().
836 */
837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100838get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 struct rlimit rlp;
841 int i;
842 long lim;
843
844 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
845 * limit doesn't fit in a long (rlim_cur might be "long long"). */
846 if (getrlimit(RLIMIT_STACK, &rlp) == 0
847 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
848# ifdef RLIM_INFINITY
849 && rlp.rlim_cur != RLIM_INFINITY
850# endif
851 )
852 {
853 lim = (long)rlp.rlim_cur;
854#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
855 {
856 pthread_attr_t attr;
857 size_t size;
858
859 /* On FreeBSD the initial thread always has a fixed stack size, no
860 * matter what the limits are set to. Normally it's 1 Mbyte. */
861 pthread_attr_init(&attr);
862 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
863 {
864 pthread_attr_getstacksize(&attr, &size);
865 if (lim > (long)size)
866 lim = (long)size;
867 }
868 pthread_attr_destroy(&attr);
869 }
870#endif
871 if (stack_grows_downwards)
872 {
873 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
874 if (stack_limit >= (char *)&i)
875 /* overflow, set to 1/16 of current stack position */
876 stack_limit = (char *)((long)&i / 16L);
877 }
878 else
879 {
880 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
881 if (stack_limit <= (char *)&i)
882 stack_limit = NULL; /* overflow */
883 }
884 }
885}
886
887/*
888 * Return FAIL when running out of stack space.
889 * "p" must point to any variable local to the caller that's on the stack.
890 */
891 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100892mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 if (stack_limit != NULL)
895 {
896 if (stack_grows_downwards)
897 {
898 if (p < stack_limit)
899 return FAIL;
900 }
901 else if (p > stack_limit)
902 return FAIL;
903 }
904 return OK;
905}
906#endif
907
908#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
909/*
910 * Support for using the signal stack.
911 * This helps when we run out of stack space, which causes a SIGSEGV. The
912 * signal handler then must run on another stack, since the normal stack is
913 * completely full.
914 */
915
916#ifndef SIGSTKSZ
917# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
918#endif
919
920# ifdef HAVE_SIGALTSTACK
921static stack_t sigstk; /* for sigaltstack() */
922# else
923static struct sigstack sigstk; /* for sigstack() */
924# endif
925
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100926static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927static char *signal_stack;
928
929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100930init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 if (signal_stack != NULL)
933 {
934# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935# ifdef HAVE_SS_BASE
936 sigstk.ss_base = signal_stack;
937# else
938 sigstk.ss_sp = signal_stack;
939# endif
940 sigstk.ss_size = SIGSTKSZ;
941 sigstk.ss_flags = 0;
942 (void)sigaltstack(&sigstk, NULL);
943# else
944 sigstk.ss_sp = signal_stack;
945 if (stack_grows_downwards)
946 sigstk.ss_sp += SIGSTKSZ - 1;
947 sigstk.ss_onstack = 0;
948 (void)sigstack(&sigstk, NULL);
949# endif
950 }
951}
952#endif
953
954/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000955 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * will barf when the second argument to signal() is ``wrong''.
957 * Let me try it with a few tricky defines from my own osdef.h (jw).
958 */
959#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 static RETSIGTYPE
961sig_winch SIGDEFARG(sigarg)
962{
963 /* this is not required on all systems, but it doesn't hurt anybody */
964 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
965 do_resize = TRUE;
966 SIGRETURN;
967}
968#endif
969
970#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 static RETSIGTYPE
972catch_sigint SIGDEFARG(sigarg)
973{
974 /* this is not required on all systems, but it doesn't hurt anybody */
975 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
976 got_int = TRUE;
977 SIGRETURN;
978}
979#endif
980
981#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 static RETSIGTYPE
983catch_sigpwr SIGDEFARG(sigarg)
984{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000985 /* this is not required on all systems, but it doesn't hurt anybody */
986 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 /*
988 * I'm not sure we get the SIGPWR signal when the system is really going
989 * down or when the batteries are almost empty. Just preserve the swap
990 * files and don't exit, that can't do any harm.
991 */
992 ml_sync_all(FALSE, FALSE);
993 SIGRETURN;
994}
995#endif
996
997#ifdef SET_SIG_ALARM
998/*
999 * signal function for alarm().
1000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 static RETSIGTYPE
1002sig_alarm SIGDEFARG(sigarg)
1003{
1004 /* doesn't do anything, just to break a system call */
1005 sig_alarm_called = TRUE;
1006 SIGRETURN;
1007}
1008#endif
1009
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001010#if (defined(HAVE_SETJMP_H) \
1011 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1012 || defined(FEAT_LIBCALL))) \
1013 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/*
1015 * A simplistic version of setjmp() that only allows one level of using.
1016 * Don't call twice before calling mch_endjmp()!.
1017 * Usage:
1018 * mch_startjmp();
1019 * if (SETJMP(lc_jump_env) != 0)
1020 * {
1021 * mch_didjmp();
1022 * EMSG("crash!");
1023 * }
1024 * else
1025 * {
1026 * do_the_work;
1027 * mch_endjmp();
1028 * }
1029 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1030 * not return before the saved environment is used.
1031 * Returns OK for normal return, FAIL when the protected code caused a
1032 * problem and LONGJMP() was used.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037#ifdef SIGHASARG
1038 lc_signal = 0;
1039#endif
1040 lc_active = TRUE;
1041}
1042
1043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001044mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 lc_active = FALSE;
1047}
1048
1049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001050mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1053 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1054 * otherwise catching the signal only works once. */
1055 init_signal_stack();
1056# endif
1057}
1058#endif
1059
1060/*
1061 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001062 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001064 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1065 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 static RETSIGTYPE
1068deathtrap SIGDEFARG(sigarg)
1069{
1070 static int entered = 0; /* count the number of times we got here.
1071 Note: when memory has been corrupted
1072 this may get an arbitrary value! */
1073#ifdef SIGHASARG
1074 int i;
1075#endif
1076
1077#if defined(HAVE_SETJMP_H)
1078 /*
1079 * Catch a crash in protected code.
1080 * Restores the environment saved in lc_jump_env, which looks like
1081 * SETJMP() returns 1.
1082 */
1083 if (lc_active)
1084 {
1085# if defined(SIGHASARG)
1086 lc_signal = sigarg;
1087# endif
1088 lc_active = FALSE; /* don't jump again */
1089 LONGJMP(lc_jump_env, 1);
1090 /* NOTREACHED */
1091 }
1092#endif
1093
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001094#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001095# ifdef SIGQUIT
1096 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1097 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1098 * pressing CTRL-\, but we don't want Vim to exit then. */
1099 if (in_mch_delay && sigarg == SIGQUIT)
1100 SIGRETURN;
1101# endif
1102
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001103 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1104 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1105 * free(). Calling free() again may then cause a crash. */
1106 if (entered == 0
1107 && (0
1108# ifdef SIGHUP
1109 || sigarg == SIGHUP
1110# endif
1111# ifdef SIGQUIT
1112 || sigarg == SIGQUIT
1113# endif
1114# ifdef SIGTERM
1115 || sigarg == SIGTERM
1116# endif
1117# ifdef SIGPWR
1118 || sigarg == SIGPWR
1119# endif
1120# ifdef SIGUSR1
1121 || sigarg == SIGUSR1
1122# endif
1123# ifdef SIGUSR2
1124 || sigarg == SIGUSR2
1125# endif
1126 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001127 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001128 SIGRETURN;
1129#endif
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /* Remember how often we have been called. */
1132 ++entered;
1133
Bram Moolenaare429e702016-06-10 19:49:14 +02001134 /* Executing autocommands is likely to use more stack space than we have
1135 * available in the signal stack. */
1136 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_EVAL
1139 /* Set the v:dying variable. */
1140 set_vim_var_nr(VV_DYING, (long)entered);
1141#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001142 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001144#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /* Since we are now using the signal stack, need to reset the stack
1146 * limit. Otherwise using a regexp will fail. */
1147 get_stack_limit();
1148#endif
1149
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001150#if 0
1151 /* This is for opening gdb the moment Vim crashes.
1152 * You need to manually adjust the file name and Vim executable name.
1153 * Suggested by SungHyun Nam. */
1154 {
1155# define VI_GDB_FILE "/tmp/vimgdb"
1156# define VIM_NAME "/usr/bin/vim"
1157 FILE *fp = fopen(VI_GDB_FILE, "w");
1158 if (fp)
1159 {
1160 fprintf(fp,
1161 "file %s\n"
1162 "attach %d\n"
1163 "set height 1000\n"
1164 "bt full\n"
1165 , VIM_NAME, getpid());
1166 fclose(fp);
1167 system("xterm -e gdb -x "VI_GDB_FILE);
1168 unlink(VI_GDB_FILE);
1169 }
1170 }
1171#endif
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#ifdef SIGHASARG
1174 /* try to find the name of this signal */
1175 for (i = 0; signal_info[i].sig != -1; i++)
1176 if (sigarg == signal_info[i].sig)
1177 break;
1178 deadly_signal = sigarg;
1179#endif
1180
1181 full_screen = FALSE; /* don't write message to the GUI, it might be
1182 * part of the problem... */
1183 /*
1184 * If something goes wrong after entering here, we may get here again.
1185 * When this happens, give a message and try to exit nicely (resetting the
1186 * terminal mode, etc.)
1187 * When this happens twice, just exit, don't even try to give a message,
1188 * stack may be corrupt or something weird.
1189 * When this still happens again (or memory was corrupted in such a way
1190 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1191 */
1192 if (entered >= 3)
1193 {
1194 reset_signals(); /* don't catch any signals anymore */
1195 may_core_dump();
1196 if (entered >= 4)
1197 _exit(8);
1198 exit(7);
1199 }
1200 if (entered == 2)
1201 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001202 /* No translation, it may call malloc(). */
1203 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 out_flush();
1205 getout(1);
1206 }
1207
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001208 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001210 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 signal_info[i].name);
1212#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001213 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001215
1216 /* Preserve files and exit. This sets the really_exiting flag to prevent
1217 * calling free(). */
1218 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaare429e702016-06-10 19:49:14 +02001220 /* NOTREACHED */
1221
Bram Moolenaar009b2592004-10-24 19:18:58 +00001222#ifdef NBDEBUG
1223 reset_signals();
1224 may_core_dump();
1225 abort();
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 SIGRETURN;
1229}
1230
Bram Moolenaarf1883472018-08-20 21:58:57 +02001231#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * On Solaris with multi-threading, suspending might not work immediately.
1234 * Catch the SIGCONT signal, which will be used as an indication whether the
1235 * suspending has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001236 *
1237 * On Linux, signal is not always handled immediately either.
1238 * See https://bugs.launchpad.net/bugs/291373
1239 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001240 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001242static volatile int sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001243static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245/*
1246 * signal handler for SIGCONT
1247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 static RETSIGTYPE
1249sigcont_handler SIGDEFARG(sigarg)
1250{
Bram Moolenaarf1883472018-08-20 21:58:57 +02001251 sigcont_received = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 SIGRETURN;
1253}
1254#endif
1255
Bram Moolenaar62b42182010-09-21 22:09:37 +02001256# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001257static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001258# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001259static void save_clipboard(void);
1260static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001261
1262static void *clip_star_save = NULL;
1263static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001264# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001265
1266/*
1267 * Called when Vim is going to sleep or execute a shell command.
1268 * We can't respond to requests for the X selections. Lose them, otherwise
1269 * other applications will hang. But first copy the text to cut buffer 0.
1270 */
1271 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001272loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001273{
1274 if (clip_star.owned || clip_plus.owned)
1275 {
1276 x11_export_final_selection();
1277 if (clip_star.owned)
1278 clip_lose_selection(&clip_star);
1279 if (clip_plus.owned)
1280 clip_lose_selection(&clip_plus);
1281 if (x11_display != NULL)
1282 XFlush(x11_display);
1283 }
1284}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001285
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001286# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001287/*
1288 * Save clipboard text to restore later.
1289 */
1290 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001291save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001292{
1293 if (clip_star.owned)
1294 clip_star_save = get_register('*', TRUE);
1295 if (clip_plus.owned)
1296 clip_plus_save = get_register('+', TRUE);
1297}
1298
1299/*
1300 * Restore clipboard text if no one own the X selection.
1301 */
1302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001303restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001304{
1305 if (clip_star_save != NULL)
1306 {
1307 if (!clip_gen_owner_exists(&clip_star))
1308 put_register('*', clip_star_save);
1309 else
1310 free_register(clip_star_save);
1311 clip_star_save = NULL;
1312 }
1313 if (clip_plus_save != NULL)
1314 {
1315 if (!clip_gen_owner_exists(&clip_plus))
1316 put_register('+', clip_plus_save);
1317 else
1318 free_register(clip_plus_save);
1319 clip_plus_save = NULL;
1320 }
1321}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001322# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001323#endif
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325/*
1326 * If the machine has job control, use it to suspend the program,
1327 * otherwise fake it by starting a new shell.
1328 */
1329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001330mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 /* BeOS does have SIGTSTP, but it doesn't work. */
1333#if defined(SIGTSTP) && !defined(__BEOS__)
1334 out_flush(); /* needed to make cursor visible on some systems */
1335 settmode(TMODE_COOK);
1336 out_flush(); /* needed to disable mouse on some systems */
1337
1338# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001339 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340# endif
1341
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001342# if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 sigcont_received = FALSE;
1344# endif
1345 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001346# if defined(_REENTRANT) && defined(SIGCONT)
1347 /*
1348 * Wait for the SIGCONT signal to be handled. It generally happens
1349 * immediately, but somehow not all the time. Do not call pause()
1350 * because there would be race condition which would hang Vim if
1351 * signal happened in between the test of sigcont_received and the
1352 * call to pause(). If signal is not yet received, call sleep(0)
1353 * to just yield CPU. Signal should then be received. If somehow
1354 * it's still not received, sleep 1, 2, 3 ms. Don't bother waiting
1355 * further if signal is not received after 1+2+3+4 ms (not expected
1356 * to happen).
1357 */
1358 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001359 long wait_time;
1360 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001361 /* Loop is not entered most of the time */
Bram Moolenaar262735e2009-07-14 10:20:22 +00001362 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364# endif
1365
Bram Moolenaarf1883472018-08-20 21:58:57 +02001366# ifdef FEAT_TITLE
1367 /*
1368 * Set oldtitle to NULL, so the current title is obtained again.
1369 */
1370 VIM_CLEAR(oldtitle);
1371# endif
1372 settmode(TMODE_RAW);
1373 need_check_timestamps = TRUE;
1374 did_check_timestamps = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#else
1376 suspend_shell();
1377#endif
1378}
1379
1380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001381mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 Columns = 80;
1384 Rows = 24;
1385
1386 out_flush();
1387 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001388
Bram Moolenaar56718732006-03-15 22:53:57 +00001389#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001390 mac_conv_init();
1391#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001392#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1393 win_clip_init();
1394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395}
1396
1397 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001398set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400#if defined(SIGWINCH)
1401 /*
1402 * WINDOW CHANGE signal is handled with sig_winch().
1403 */
1404 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1405#endif
1406
1407 /*
1408 * We want the STOP signal to work, to make mch_suspend() work.
1409 * For "rvim" the STOP signal is ignored.
1410 */
1411#ifdef SIGTSTP
1412 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1413#endif
Bram Moolenaarf1883472018-08-20 21:58:57 +02001414#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 signal(SIGCONT, sigcont_handler);
1416#endif
1417
1418 /*
1419 * We want to ignore breaking of PIPEs.
1420 */
1421#ifdef SIGPIPE
1422 signal(SIGPIPE, SIG_IGN);
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001426 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#endif
1428
1429 /*
1430 * Ignore alarm signals (Perl's alarm() generates it).
1431 */
1432#ifdef SIGALRM
1433 signal(SIGALRM, SIG_IGN);
1434#endif
1435
1436 /*
1437 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1438 * work will be lost.
1439 */
1440#ifdef SIGPWR
1441 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1442#endif
1443
1444 /*
1445 * Arrange for other signals to gracefully shutdown Vim.
1446 */
1447 catch_signals(deathtrap, SIG_ERR);
1448
1449#if defined(FEAT_GUI) && defined(SIGHUP)
1450 /*
1451 * When the GUI is running, ignore the hangup signal.
1452 */
1453 if (gui.in_use)
1454 signal(SIGHUP, SIG_IGN);
1455#endif
1456}
1457
Bram Moolenaardf177f62005-02-22 08:39:57 +00001458#if defined(SIGINT) || defined(PROTO)
1459/*
1460 * Catch CTRL-C (only works while in Cooked mode).
1461 */
1462 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001463catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001464{
1465 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1466}
1467#endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001470reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001473#if defined(_REENTRANT) && defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 /* SIGCONT isn't in the list, because its default action is ignore */
1475 signal(SIGCONT, SIG_DFL);
1476#endif
1477}
1478
1479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001480catch_signals(
1481 RETSIGTYPE (*func_deadly)(),
1482 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 int i;
1485
1486 for (i = 0; signal_info[i].sig != -1; i++)
1487 if (signal_info[i].deadly)
1488 {
1489#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1490 struct sigaction sa;
1491
1492 /* Setup to use the alternate stack for the signal function. */
1493 sa.sa_handler = func_deadly;
1494 sigemptyset(&sa.sa_mask);
1495# if defined(__linux__) && defined(_REENTRANT)
1496 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1497 * thread handling in combination with using the alternate stack:
1498 * pthread library functions try to use the stack pointer to
1499 * identify the current thread, causing a SEGV signal, which
1500 * recursively calls deathtrap() and hangs. */
1501 sa.sa_flags = 0;
1502# else
1503 sa.sa_flags = SA_ONSTACK;
1504# endif
1505 sigaction(signal_info[i].sig, &sa, NULL);
1506#else
1507# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1508 struct sigvec sv;
1509
1510 /* Setup to use the alternate stack for the signal function. */
1511 sv.sv_handler = func_deadly;
1512 sv.sv_mask = 0;
1513 sv.sv_flags = SV_ONSTACK;
1514 sigvec(signal_info[i].sig, &sv, NULL);
1515# else
1516 signal(signal_info[i].sig, func_deadly);
1517# endif
1518#endif
1519 }
1520 else if (func_other != SIG_ERR)
1521 signal(signal_info[i].sig, func_other);
1522}
1523
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001524#ifdef HAVE_SIGPROCMASK
1525 static void
1526block_signals(sigset_t *set)
1527{
1528 sigset_t newset;
1529 int i;
1530
1531 sigemptyset(&newset);
1532
1533 for (i = 0; signal_info[i].sig != -1; i++)
1534 sigaddset(&newset, signal_info[i].sig);
1535
1536# if defined(_REENTRANT) && defined(SIGCONT)
1537 /* SIGCONT isn't in the list, because its default action is ignore */
1538 sigaddset(&newset, SIGCONT);
1539# endif
1540
1541 sigprocmask(SIG_BLOCK, &newset, set);
1542}
1543
1544 static void
1545unblock_signals(sigset_t *set)
1546{
1547 sigprocmask(SIG_SETMASK, set, NULL);
1548}
1549#endif
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001552 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001553 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1554 * return TRUE
1555 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1556 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001557 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001558 * Returns TRUE when Vim should exit.
1559 */
1560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001561vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001562{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 static int got_signal = 0;
1564 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001565
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001566 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001567 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001568 case SIGNAL_BLOCK: blocked = TRUE;
1569 break;
1570
1571 case SIGNAL_UNBLOCK: blocked = FALSE;
1572 if (got_signal != 0)
1573 {
1574 kill(getpid(), got_signal);
1575 got_signal = 0;
1576 }
1577 break;
1578
1579 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001580 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001581 got_signal = sig;
1582#ifdef SIGPWR
1583 if (sig != SIGPWR)
1584#endif
1585 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001586 break;
1587 }
1588 return FALSE;
1589}
1590
1591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * Check_win checks whether we have an interactive stdout.
1593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001595mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (isatty(1))
1598 return OK;
1599 return FAIL;
1600}
1601
1602/*
1603 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1604 */
1605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001606mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 if (isatty(read_cmd_fd))
1609 return TRUE;
1610 return FALSE;
1611}
1612
1613#ifdef FEAT_X11
1614
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001615# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
1619 * Give a message about the elapsed time for opening the X window.
1620 */
1621 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001622xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001624 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625}
1626# endif
1627#endif
1628
1629#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1630/*
1631 * A few functions shared by X11 title and clipboard code.
1632 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001633static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1634static int x_error_check(Display *dpy, XErrorEvent *error_event);
1635static int x_connect_to_server(void);
1636static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638static int got_x_error = FALSE;
1639
1640/*
1641 * X Error handler, otherwise X just exits! (very rude) -- webb
1642 */
1643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001644x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001646 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 STRCAT(IObuff, _("\nVim: Got X error\n"));
1648
1649 /* We cannot print a message and continue, because no X calls are allowed
1650 * here (causes my system to hang). Silently continuing might be an
1651 * alternative... */
1652 preserve_exit(); /* preserve files and exit */
1653
1654 return 0; /* NOTREACHED */
1655}
1656
1657/*
1658 * Another X Error handler, just used to check for errors.
1659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001661x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 got_x_error = TRUE;
1664 return 0;
1665}
1666
1667#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1668# if defined(HAVE_SETJMP_H)
1669/*
1670 * An X IO Error handler, used to catch error while opening the display.
1671 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001672static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001675x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 /* This function should not return, it causes exit(). Longjump instead. */
1678 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001679# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001680 return 0; /* avoid the compiler complains about missing return value */
1681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
1683# endif
1684
1685/*
1686 * An X IO Error handler, used to catch terminal errors.
1687 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001688static int x_IOerror_handler(Display *dpy);
1689static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001690static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001693x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001696 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 x11_window = 0;
1698 x11_display = NULL;
1699 xterm_Shell = (Widget)0;
1700
1701 /* This function should not return, it causes exit(). Longjump instead. */
1702 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001703# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001704 return 0; /* avoid the compiler complains about missing return value */
1705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001707
1708/*
1709 * If the X11 connection was lost try to restore it.
1710 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001711 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001712 */
1713 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001714may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001715{
1716 if (xterm_dpy_was_reset)
1717 {
1718 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001719
1720# ifndef LESSTIF_VERSION
1721 /* This has been reported to avoid Vim getting stuck. */
1722 if (app_context != (XtAppContext)NULL)
1723 {
1724 XtDestroyApplicationContext(app_context);
1725 app_context = (XtAppContext)NULL;
1726 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1727 }
1728# endif
1729
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001730 setup_term_clip();
1731 get_x11_title(FALSE);
1732 }
1733}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#endif
1735
1736/*
1737 * Return TRUE when connection to the X server is desired.
1738 */
1739 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001740x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#if defined(FEAT_CLIENTSERVER)
1743 if (x_force_connect)
1744 return TRUE;
1745#endif
1746 if (x_no_connect)
1747 return FALSE;
1748
1749 /* Check for a match with "exclude:" from 'clipboard'. */
1750 if (clip_exclude_prog != NULL)
1751 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001752 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 return FALSE;
1754 }
1755 return TRUE;
1756}
1757
1758/*
1759 * Test if "dpy" and x11_window are valid by getting the window title.
1760 * I don't actually want it yet, so there may be a simpler call to use, but
1761 * this will cause the error handler x_error_check() to be called if anything
1762 * is wrong, such as the window pointer being invalid (as can happen when the
1763 * user changes his DISPLAY, but not his WINDOWID) -- webb
1764 */
1765 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001766test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 int (*old_handler)();
1769 XTextProperty text_prop;
1770
1771 old_handler = XSetErrorHandler(x_error_check);
1772 got_x_error = FALSE;
1773 if (XGetWMName(dpy, x11_window, &text_prop))
1774 XFree((void *)text_prop.value);
1775 XSync(dpy, False);
1776 (void)XSetErrorHandler(old_handler);
1777
1778 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001779 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 return (got_x_error ? FAIL : OK);
1782}
1783#endif
1784
1785#ifdef FEAT_TITLE
1786
1787#ifdef FEAT_X11
1788
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001789static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791/*
1792 * try to get x11 window and display
1793 *
1794 * return FAIL for failure, OK otherwise
1795 */
1796 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001797get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 char *winid;
1800 static int result = -1;
1801#define XD_NONE 0 /* x11_display not set here */
1802#define XD_HERE 1 /* x11_display opened here */
1803#define XD_GUI 2 /* x11_display used from gui.dpy */
1804#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1805 static int x11_display_from = XD_NONE;
1806 static int did_set_error_handler = FALSE;
1807
1808 if (!did_set_error_handler)
1809 {
1810 /* X just exits if it finds an error otherwise! */
1811 (void)XSetErrorHandler(x_error_handler);
1812 did_set_error_handler = TRUE;
1813 }
1814
Bram Moolenaar9372a112005-12-06 19:59:18 +00001815#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (gui.in_use)
1817 {
1818 /*
1819 * If the X11 display was opened here before, for the window where Vim
1820 * was started, close that one now to avoid a memory leak.
1821 */
1822 if (x11_display_from == XD_HERE && x11_display != NULL)
1823 {
1824 XCloseDisplay(x11_display);
1825 x11_display_from = XD_NONE;
1826 }
1827 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1828 {
1829 x11_display_from = XD_GUI;
1830 return OK;
1831 }
1832 x11_display = NULL;
1833 return FAIL;
1834 }
1835 else if (x11_display_from == XD_GUI)
1836 {
1837 /* GUI must have stopped somehow, clear x11_display */
1838 x11_window = 0;
1839 x11_display = NULL;
1840 x11_display_from = XD_NONE;
1841 }
1842#endif
1843
1844 /* When started with the "-X" argument, don't try connecting. */
1845 if (!x_connect_to_server())
1846 return FAIL;
1847
1848 /*
1849 * If WINDOWID not set, should try another method to find out
1850 * what the current window number is. The only code I know for
1851 * this is very complicated.
1852 * We assume that zero is invalid for WINDOWID.
1853 */
1854 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1855 x11_window = (Window)atol(winid);
1856
1857#ifdef FEAT_XCLIPBOARD
1858 if (xterm_dpy != NULL && x11_window != 0)
1859 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001860 /* We may have checked it already, but Gnome terminal can move us to
1861 * another window, so we need to check every time. */
1862 if (x11_display_from != XD_XTERM)
1863 {
1864 /*
1865 * If the X11 display was opened here before, for the window where
1866 * Vim was started, close that one now to avoid a memory leak.
1867 */
1868 if (x11_display_from == XD_HERE && x11_display != NULL)
1869 XCloseDisplay(x11_display);
1870 x11_display = xterm_dpy;
1871 x11_display_from = XD_XTERM;
1872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (test_x11_window(x11_display) == FAIL)
1874 {
1875 /* probably bad $WINDOWID */
1876 x11_window = 0;
1877 x11_display = NULL;
1878 x11_display_from = XD_NONE;
1879 return FAIL;
1880 }
1881 return OK;
1882 }
1883#endif
1884
1885 if (x11_window == 0 || x11_display == NULL)
1886 result = -1;
1887
1888 if (result != -1) /* Have already been here and set this */
1889 return result; /* Don't do all these X calls again */
1890
1891 if (x11_window != 0 && x11_display == NULL)
1892 {
1893#ifdef SET_SIG_ALARM
1894 RETSIGTYPE (*sig_save)();
1895#endif
1896#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1897 struct timeval start_tv;
1898
1899 if (p_verbose > 0)
1900 gettimeofday(&start_tv, NULL);
1901#endif
1902
1903#ifdef SET_SIG_ALARM
1904 /*
1905 * Opening the Display may hang if the DISPLAY setting is wrong, or
1906 * the network connection is bad. Set an alarm timer to get out.
1907 */
1908 sig_alarm_called = FALSE;
1909 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1910 (RETSIGTYPE (*)())sig_alarm);
1911 alarm(2);
1912#endif
1913 x11_display = XOpenDisplay(NULL);
1914
1915#ifdef SET_SIG_ALARM
1916 alarm(0);
1917 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1918 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001919 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
1921 if (x11_display != NULL)
1922 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001923# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001925 {
1926 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001927 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001928 verbose_leave();
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930# endif
1931 if (test_x11_window(x11_display) == FAIL)
1932 {
1933 /* Maybe window id is bad */
1934 x11_window = 0;
1935 XCloseDisplay(x11_display);
1936 x11_display = NULL;
1937 }
1938 else
1939 x11_display_from = XD_HERE;
1940 }
1941 }
1942 if (x11_window == 0 || x11_display == NULL)
1943 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001944
1945# ifdef FEAT_EVAL
1946 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1947# endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 return (result = OK);
1950}
1951
1952/*
1953 * Determine original x11 Window Title
1954 */
1955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001956get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001958 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959}
1960
1961/*
1962 * Determine original x11 Window icon
1963 */
1964 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001965get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 int retval = FALSE;
1968
1969 retval = get_x11_thing(FALSE, test_only);
1970
1971 /* could not get old icon, use terminal name */
1972 if (oldicon == NULL && !test_only)
1973 {
1974 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001975 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001977 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979
1980 return retval;
1981}
1982
1983 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001984get_x11_thing(
1985 int get_title, /* get title string */
1986 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 XTextProperty text_prop;
1989 int retval = FALSE;
1990 Status status;
1991
1992 if (get_x11_windis() == OK)
1993 {
1994 /* Get window/icon name if any */
1995 if (get_title)
1996 status = XGetWMName(x11_display, x11_window, &text_prop);
1997 else
1998 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1999
2000 /*
2001 * If terminal is xterm, then x11_window may be a child window of the
2002 * outer xterm window that actually contains the window/icon name, so
2003 * keep traversing up the tree until a window with a title/icon is
2004 * found.
2005 */
2006 /* Previously this was only done for xterm and alikes. I don't see a
2007 * reason why it would fail for other terminal emulators.
2008 * if (term_is_xterm) */
2009 {
2010 Window root;
2011 Window parent;
2012 Window win = x11_window;
2013 Window *children;
2014 unsigned int num_children;
2015
2016 while (!status || text_prop.value == NULL)
2017 {
2018 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2019 &num_children))
2020 break;
2021 if (children)
2022 XFree((void *)children);
2023 if (parent == root || parent == 0)
2024 break;
2025
2026 win = parent;
2027 if (get_title)
2028 status = XGetWMName(x11_display, win, &text_prop);
2029 else
2030 status = XGetWMIconName(x11_display, win, &text_prop);
2031 }
2032 }
2033 if (status && text_prop.value != NULL)
2034 {
2035 retval = TRUE;
2036 if (!test_only)
2037 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002038#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2039 if (text_prop.encoding == XA_STRING
2040# ifdef FEAT_MBYTE
2041 && !has_mbyte
2042# endif
2043 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
2045#endif
2046 if (get_title)
2047 oldtitle = vim_strsave((char_u *)text_prop.value);
2048 else
2049 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002050#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 else
2053 {
2054 char **cl;
2055 Status transform_status;
2056 int n = 0;
2057
2058 transform_status = XmbTextPropertyToTextList(x11_display,
2059 &text_prop,
2060 &cl, &n);
2061 if (transform_status >= Success && n > 0 && cl[0])
2062 {
2063 if (get_title)
2064 oldtitle = vim_strsave((char_u *) cl[0]);
2065 else
2066 oldicon = vim_strsave((char_u *) cl[0]);
2067 XFreeStringList(cl);
2068 }
2069 else
2070 {
2071 if (get_title)
2072 oldtitle = vim_strsave((char_u *)text_prop.value);
2073 else
2074 oldicon = vim_strsave((char_u *)text_prop.value);
2075 }
2076 }
2077#endif
2078 }
2079 XFree((void *)text_prop.value);
2080 }
2081 }
2082 return retval;
2083}
2084
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002085/* Xutf8 functions are not avaialble on older systems. Note that on some
2086 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2087 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2088 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002090# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091# define USE_UTF8_STRING
2092# endif
2093#endif
2094
2095/*
2096 * Set x11 Window Title
2097 *
2098 * get_x11_windis() must be called before this and have returned OK
2099 */
2100 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002101set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2104 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2105 * supported everywhere and STRING doesn't work for multi-byte titles.
2106 */
2107#ifdef USE_UTF8_STRING
2108 if (enc_utf8)
2109 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2110 NULL, NULL, 0, NULL, NULL, NULL);
2111 else
2112#endif
2113 {
2114#if XtSpecificationRelease >= 4
2115# ifdef FEAT_XFONTSET
2116 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2117 NULL, NULL, 0, NULL, NULL, NULL);
2118# else
2119 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002120 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002123 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 XSetWMProperties(x11_display, x11_window, &text_prop,
2125 NULL, NULL, 0, NULL, NULL, NULL);
2126# endif
2127#else
2128 XStoreName(x11_display, x11_window, (char *)title);
2129#endif
2130 }
2131 XFlush(x11_display);
2132}
2133
2134/*
2135 * Set x11 Window icon
2136 *
2137 * get_x11_windis() must be called before this and have returned OK
2138 */
2139 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002140set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 /* See above for comments about using X*SetWMProperties(). */
2143#ifdef USE_UTF8_STRING
2144 if (enc_utf8)
2145 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2146 NULL, 0, NULL, NULL, NULL);
2147 else
2148#endif
2149 {
2150#if XtSpecificationRelease >= 4
2151# ifdef FEAT_XFONTSET
2152 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2153 NULL, 0, NULL, NULL, NULL);
2154# else
2155 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002156 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002158 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2160 NULL, 0, NULL, NULL, NULL);
2161# endif
2162#else
2163 XSetIconName(x11_display, x11_window, (char *)icon);
2164#endif
2165 }
2166 XFlush(x11_display);
2167}
2168
2169#else /* FEAT_X11 */
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002172get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 return FALSE;
2175}
2176
2177 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002178get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 if (!test_only)
2181 {
2182 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002183 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002185 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 return FALSE;
2188}
2189
2190#endif /* FEAT_X11 */
2191
2192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002193mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 return get_x11_title(TRUE);
2196}
2197
2198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002199mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 return get_x11_icon(TRUE);
2202}
2203
2204/*
2205 * Set the window title and icon.
2206 */
2207 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002208mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 int type = 0;
2211 static int recursive = 0;
2212
2213 if (T_NAME == NULL) /* no terminal name (yet) */
2214 return;
2215 if (title == NULL && icon == NULL) /* nothing to do */
2216 return;
2217
2218 /* When one of the X11 functions causes a deadly signal, we get here again
2219 * recursively. Avoid hanging then (something is probably locked). */
2220 if (recursive)
2221 return;
2222 ++recursive;
2223
2224 /*
2225 * if the window ID and the display is known, we may use X11 calls
2226 */
2227#ifdef FEAT_X11
2228 if (get_x11_windis() == OK)
2229 type = 1;
2230#else
2231# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2232 if (gui.in_use)
2233 type = 1;
2234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#endif
2236
2237 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002238 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 * than x11 calls, because the x11 calls don't always work
2240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if ((type || *T_TS != NUL) && title != NULL)
2242 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002243 if (oldtitle_outdated)
2244 {
2245 oldtitle_outdated = FALSE;
2246 VIM_CLEAR(oldtitle);
2247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (oldtitle == NULL
2249#ifdef FEAT_GUI
2250 && !gui.in_use
2251#endif
2252 ) /* first call but not in GUI, save title */
2253 (void)get_x11_title(FALSE);
2254
2255 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2256 term_settitle(title);
2257#ifdef FEAT_X11
2258 else
2259# ifdef FEAT_GUI_GTK
2260 if (!gui.in_use) /* don't do this if GTK+ is running */
2261# endif
2262 set_x11_title(title); /* x11 */
2263#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002264#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2266 else
2267 gui_mch_settitle(title, icon);
2268#endif
2269 did_set_title = TRUE;
2270 }
2271
2272 if ((type || *T_CIS != NUL) && icon != NULL)
2273 {
2274 if (oldicon == NULL
2275#ifdef FEAT_GUI
2276 && !gui.in_use
2277#endif
2278 ) /* first call, save icon */
2279 get_x11_icon(FALSE);
2280
2281 if (*T_CIS != NUL)
2282 {
2283 out_str(T_CIS); /* set icon start */
2284 out_str_nf(icon);
2285 out_str(T_CIE); /* set icon end */
2286 out_flush();
2287 }
2288#ifdef FEAT_X11
2289 else
2290# ifdef FEAT_GUI_GTK
2291 if (!gui.in_use) /* don't do this if GTK+ is running */
2292# endif
2293 set_x11_icon(icon); /* x11 */
2294#endif
2295 did_set_icon = TRUE;
2296 }
2297 --recursive;
2298}
2299
2300/*
2301 * Restore the window/icon title.
2302 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002303 * SAVE_RESTORE_TITLE only restore title
2304 * SAVE_RESTORE_ICON only restore icon
2305 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 */
2307 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002308mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002311 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002313 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2314
2315 // pop and push from/to the stack
2316 term_pop_title(which);
2317 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318}
2319
2320#endif /* FEAT_TITLE */
2321
2322/*
2323 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002324 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
2326 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002327vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 if (name == NULL)
2330 return FALSE;
2331 return (STRNICMP(name, "xterm", 5) == 0
2332 || STRNICMP(name, "nxterm", 6) == 0
2333 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002334 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002336 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 || STRCMP(name, "builtin_xterm") == 0);
2338}
2339
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002340#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2341/*
2342 * Return TRUE if "name" appears to be that of a terminal
2343 * known to support the xterm-style mouse protocol.
2344 * Relies on term_is_xterm having been set to its correct value.
2345 */
2346 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002347use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002348{
2349 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002350 && (term_is_xterm
2351 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002352 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002353 || STRICMP(name, "st") == 0
2354 || STRNICMP(name, "st-", 3) == 0
2355 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002356}
2357#endif
2358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2360/*
2361 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2362 * Return 1 for "xterm".
2363 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002364 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002365 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
2367 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002368use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002370 if (ttym_flags == TTYM_SGR)
2371 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002372 if (ttym_flags == TTYM_URXVT)
2373 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (ttym_flags == TTYM_XTERM2)
2375 return 2;
2376 if (ttym_flags == TTYM_XTERM)
2377 return 1;
2378 return 0;
2379}
2380#endif
2381
2382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002383vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 if (name == NULL)
2386 return FALSE;
2387 return (STRNICMP(name, "iris-ansi", 9) == 0
2388 || STRCMP(name, "builtin_iris-ansi") == 0);
2389}
2390
2391 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002392vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394 if (name == NULL)
2395 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002396 /* catch VT100 - VT5xx */
2397 return ((STRNICMP(name, "vt", 2) == 0
2398 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 || STRCMP(name, "builtin_vt320") == 0);
2400}
2401
2402/*
2403 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2404 * This should include all windowed terminal emulators.
2405 */
2406 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002407vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 if (name == NULL)
2410 return FALSE;
2411 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2412 return TRUE;
2413 return ( STRNICMP(name, "hpterm", 6) == 0
2414 || STRNICMP(name, "sun-cmd", 7) == 0
2415 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002416 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 || STRNICMP(name, "dtterm", 6) == 0);
2418}
2419
2420/*
2421 * Insert user name in s[len].
2422 * Return OK if a name found.
2423 */
2424 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002425mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002428 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return OK;
2430#else
2431 return mch_get_uname(getuid(), s, len);
2432#endif
2433}
2434
2435/*
2436 * Insert user name for "uid" in s[len].
2437 * Return OK if a name found.
2438 */
2439 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002440mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2443 struct passwd *pw;
2444
2445 if ((pw = getpwuid(uid)) != NULL
2446 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2447 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002448 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return OK;
2450 }
2451#endif
2452 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2453 return FAIL; /* a number is not a name */
2454}
2455
2456/*
2457 * Insert host name is s[len].
2458 */
2459
2460#ifdef HAVE_SYS_UTSNAME_H
2461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002462mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 struct utsname vutsname;
2465
2466 if (uname(&vutsname) < 0)
2467 *s = NUL;
2468 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002469 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470}
2471#else /* HAVE_SYS_UTSNAME_H */
2472
2473# ifdef HAVE_SYS_SYSTEMINFO_H
2474# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2475# endif
2476
2477 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002478mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480# ifdef VAXC
2481 vaxc$gethostname((char *)s, len);
2482# else
2483 gethostname((char *)s, len);
2484# endif
2485 s[len - 1] = NUL; /* make sure it's terminated */
2486}
2487#endif /* HAVE_SYS_UTSNAME_H */
2488
2489/*
2490 * return process ID
2491 */
2492 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002493mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
2495 return (long)getpid();
2496}
2497
2498#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002499static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002502strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 extern int sys_nerr;
2505 extern char *sys_errlist[];
2506 static char er[20];
2507
2508 if (err > 0 && err < sys_nerr)
2509 return (sys_errlist[err]);
2510 sprintf(er, "Error %d", err);
2511 return er;
2512}
2513#endif
2514
2515/*
2516 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2517 * Return OK for success, FAIL for failure.
2518 */
2519 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002520mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
2522#if defined(USE_GETCWD)
2523 if (getcwd((char *)buf, len) == NULL)
2524 {
2525 STRCPY(buf, strerror(errno));
2526 return FAIL;
2527 }
2528 return OK;
2529#else
2530 return (getwd((char *)buf) != NULL ? OK : FAIL);
2531#endif
2532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002535 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 *
2537 * return FAIL for failure, OK for success
2538 */
2539 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002540mch_FullName(
2541 char_u *fname,
2542 char_u *buf,
2543 int len,
2544 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002547#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 int fd = -1;
2549 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 char_u olddir[MAXPATHL];
2552 char_u *p;
2553 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002554#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002555 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2556 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002557#endif
2558
Bram Moolenaar38323e42007-03-06 19:22:53 +00002559#ifdef VMS
2560 fname = vms_fixfilename(fname);
2561#endif
2562
Bram Moolenaara2442432007-04-26 14:26:37 +00002563#ifdef __CYGWIN__
2564 /*
2565 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2566 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002567# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002568 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2569 * a forward slash. */
2570 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2571 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002572# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002573 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002574# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002575 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002576#endif
2577
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002578 /* Expand it if forced or not an absolute path.
2579 * Do not do it for "/file", the result is always "/". */
2580 if ((force || !mch_isFullName(fname))
2581 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /*
2584 * If the file name has a path, change to that directory for a moment,
2585 * and then do the getwd() (and get back to where we were).
2586 * This will get the correct path name with "../" things.
2587 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002588 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002590#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 /*
2592 * Use fchdir() if possible, it's said to be faster and more
2593 * reliable. But on SunOS 4 it might not work. Check this by
2594 * doing a fchdir() right now.
2595 */
2596 if (!dont_fchdir)
2597 {
2598 fd = open(".", O_RDONLY | O_EXTRA, 0);
2599 if (fd >= 0 && fchdir(fd) < 0)
2600 {
2601 close(fd);
2602 fd = -1;
2603 dont_fchdir = TRUE; /* don't try again */
2604 }
2605 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
2608 /* Only change directory when we are sure we can return to where
2609 * we are now. After doing "su" chdir(".") might not work. */
2610 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002611#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 (mch_dirname(olddir, MAXPATHL) == FAIL
2615 || mch_chdir((char *)olddir) != 0))
2616 {
2617 p = NULL; /* can't get current dir: don't chdir */
2618 retval = FAIL;
2619 }
2620 else
2621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 /* The directory is copied into buf[], to be able to remove
2623 * the file name without changing it (could be a string in
2624 * read-only memory) */
2625 if (p - fname >= len)
2626 retval = FAIL;
2627 else
2628 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002629 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (mch_chdir((char *)buf))
2631 retval = FAIL;
2632 else
2633 fname = p + 1;
2634 *buf = NUL;
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 }
2638 if (mch_dirname(buf, len) == FAIL)
2639 {
2640 retval = FAIL;
2641 *buf = NUL;
2642 }
2643 if (p != NULL)
2644 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002645#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (fd >= 0)
2647 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002648 if (p_verbose >= 5)
2649 {
2650 verbose_enter();
2651 MSG("fchdir() to previous dir");
2652 verbose_leave();
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 l = fchdir(fd);
2655 close(fd);
2656 }
2657 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 l = mch_chdir((char *)olddir);
2660 if (l != 0)
2661 EMSG(_(e_prev_dir));
2662 }
2663
2664 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002665 if (l >= len - 1)
2666 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002667#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002668 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002670 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002675 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return FAIL;
2677
2678 /* Do not append ".", "/dir/." is equal to "/dir". */
2679 if (STRCMP(fname, ".") != 0)
2680 STRCAT(buf, fname);
2681
2682 return OK;
2683}
2684
2685/*
2686 * Return TRUE if "fname" does not depend on the current directory.
2687 */
2688 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002689mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002691#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return ( fname[0] == '/' || fname[0] == '.' ||
2693 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2694 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2695 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002696#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#endif
2699}
2700
Bram Moolenaar24552be2005-12-10 20:17:30 +00002701#if defined(USE_FNAME_CASE) || defined(PROTO)
2702/*
2703 * Set the case of the file name, if it already exists. This will cause the
2704 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002705 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002706 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002707 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002708fname_case(
2709 char_u *name,
2710 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002711{
2712 struct stat st;
2713 char_u *slash, *tail;
2714 DIR *dirp;
2715 struct dirent *dp;
2716
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002717 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002718 {
2719 /* Open the directory where the file is located. */
2720 slash = vim_strrchr(name, '/');
2721 if (slash == NULL)
2722 {
2723 dirp = opendir(".");
2724 tail = name;
2725 }
2726 else
2727 {
2728 *slash = NUL;
2729 dirp = opendir((char *)name);
2730 *slash = '/';
2731 tail = slash + 1;
2732 }
2733
2734 if (dirp != NULL)
2735 {
2736 while ((dp = readdir(dirp)) != NULL)
2737 {
2738 /* Only accept names that differ in case and are the same byte
2739 * length. TODO: accept different length name. */
2740 if (STRICMP(tail, dp->d_name) == 0
2741 && STRLEN(tail) == STRLEN(dp->d_name))
2742 {
2743 char_u newname[MAXPATHL + 1];
2744 struct stat st2;
2745
2746 /* Verify the inode is equal. */
2747 vim_strncpy(newname, name, MAXPATHL);
2748 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2749 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002750 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002751 && st.st_ino == st2.st_ino
2752 && st.st_dev == st2.st_dev)
2753 {
2754 STRCPY(tail, dp->d_name);
2755 break;
2756 }
2757 }
2758 }
2759
2760 closedir(dirp);
2761 }
2762 }
2763}
2764#endif
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766/*
2767 * Get file permissions for 'name'.
2768 * Returns -1 when it doesn't exist.
2769 */
2770 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002771mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 struct stat statb;
2774
2775 /* Keep the #ifdef outside of stat(), it may be a macro. */
2776#ifdef VMS
2777 if (stat((char *)vms_fixfilename(name), &statb))
2778#else
2779 if (stat((char *)name, &statb))
2780#endif
2781 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002782#ifdef __INTERIX
2783 /* The top bit makes the value negative, which means the file doesn't
2784 * exist. Remove the bit, we don't use it. */
2785 return statb.st_mode & ~S_ADDACE;
2786#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790
2791/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002792 * Set file permission for "name" to "perm".
2793 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 */
2795 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002796mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 return (chmod((char *)
2799#ifdef VMS
2800 vms_fixfilename(name),
2801#else
2802 name,
2803#endif
2804 (mode_t)perm) == 0 ? OK : FAIL);
2805}
2806
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002807#if defined(HAVE_FCHMOD) || defined(PROTO)
2808/*
2809 * Set file permission for open file "fd" to "perm".
2810 * Return FAIL for failure, OK otherwise.
2811 */
2812 int
2813mch_fsetperm(int fd, long perm)
2814{
2815 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2816}
2817#endif
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#if defined(HAVE_ACL) || defined(PROTO)
2820# ifdef HAVE_SYS_ACL_H
2821# include <sys/acl.h>
2822# endif
2823# ifdef HAVE_SYS_ACCESS_H
2824# include <sys/access.h>
2825# endif
2826
2827# ifdef HAVE_SOLARIS_ACL
2828typedef struct vim_acl_solaris_T {
2829 int acl_cnt;
2830 aclent_t *acl_entry;
2831} vim_acl_solaris_T;
2832# endif
2833
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002834#if defined(HAVE_SELINUX) || defined(PROTO)
2835/*
2836 * Copy security info from "from_file" to "to_file".
2837 */
2838 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002839mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002840{
2841 if (from_file == NULL)
2842 return;
2843
2844 if (selinux_enabled == -1)
2845 selinux_enabled = is_selinux_enabled();
2846
2847 if (selinux_enabled > 0)
2848 {
2849 security_context_t from_context = NULL;
2850 security_context_t to_context = NULL;
2851
2852 if (getfilecon((char *)from_file, &from_context) < 0)
2853 {
2854 /* If the filesystem doesn't support extended attributes,
2855 the original had no special security context and the
2856 target cannot have one either. */
2857 if (errno == EOPNOTSUPP)
2858 return;
2859
2860 MSG_PUTS(_("\nCould not get security context for "));
2861 msg_outtrans(from_file);
2862 msg_putchar('\n');
2863 return;
2864 }
2865 if (getfilecon((char *)to_file, &to_context) < 0)
2866 {
2867 MSG_PUTS(_("\nCould not get security context for "));
2868 msg_outtrans(to_file);
2869 msg_putchar('\n');
2870 freecon (from_context);
2871 return ;
2872 }
2873 if (strcmp(from_context, to_context) != 0)
2874 {
2875 if (setfilecon((char *)to_file, from_context) < 0)
2876 {
2877 MSG_PUTS(_("\nCould not set security context for "));
2878 msg_outtrans(to_file);
2879 msg_putchar('\n');
2880 }
2881 }
2882 freecon(to_context);
2883 freecon(from_context);
2884 }
2885}
2886#endif /* HAVE_SELINUX */
2887
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002888#if defined(HAVE_SMACK) && !defined(PROTO)
2889/*
2890 * Copy security info from "from_file" to "to_file".
2891 */
2892 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002893mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002894{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002895 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002896 {
2897 XATTR_NAME_SMACK,
2898 XATTR_NAME_SMACKEXEC,
2899 XATTR_NAME_SMACKMMAP
2900 };
2901
2902 char buffer[SMACK_LABEL_LEN];
2903 const char *name;
2904 int index;
2905 int ret;
2906 ssize_t size;
2907
2908 if (from_file == NULL)
2909 return;
2910
2911 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2912 / sizeof(smack_copied_attributes)[0]) ; index++)
2913 {
2914 /* get the name of the attribute to copy */
2915 name = smack_copied_attributes[index];
2916
2917 /* get the value of the attribute in buffer */
2918 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2919 if (size >= 0)
2920 {
2921 /* copy the attribute value of buffer */
2922 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2923 if (ret < 0)
2924 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002925 vim_snprintf((char *)IObuff, IOSIZE,
2926 _("Could not set security context %s for %s"),
2927 name, to_file);
2928 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002929 msg_putchar('\n');
2930 }
2931 }
2932 else
2933 {
2934 /* what reason of not having the attribute value? */
2935 switch (errno)
2936 {
2937 case ENOTSUP:
2938 /* extended attributes aren't supported or enabled */
2939 /* should a message be echoed? not sure... */
2940 return; /* leave because it isn't usefull to continue */
2941
2942 case ERANGE:
2943 default:
2944 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002945 vim_snprintf((char *)IObuff, IOSIZE,
2946 _("Could not get security context %s for %s. Removing it!"),
2947 name, from_file);
2948 msg_puts(IObuff);
2949 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002950 /* FALLTHROUGH to remove the attribute */
2951
2952 case ENODATA:
2953 /* no attribute of this name */
2954 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002955 /* Silently ignore errors, apparently this happens when
2956 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002957 break;
2958 }
2959 }
2960 }
2961}
2962#endif /* HAVE_SMACK */
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964/*
2965 * Return a pointer to the ACL of file "fname" in allocated memory.
2966 * Return NULL if the ACL is not available for whatever reason.
2967 */
2968 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002969mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 vim_acl_T ret = NULL;
2972#ifdef HAVE_POSIX_ACL
2973 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2974#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002975#ifdef HAVE_SOLARIS_ZFS_ACL
2976 acl_t *aclent;
2977
2978 if (acl_get((char *)fname, 0, &aclent) < 0)
2979 return NULL;
2980 ret = (vim_acl_T)aclent;
2981#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#ifdef HAVE_SOLARIS_ACL
2983 vim_acl_solaris_T *aclent;
2984
2985 aclent = malloc(sizeof(vim_acl_solaris_T));
2986 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2987 {
2988 free(aclent);
2989 return NULL;
2990 }
2991 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2992 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2993 {
2994 free(aclent->acl_entry);
2995 free(aclent);
2996 return NULL;
2997 }
2998 ret = (vim_acl_T)aclent;
2999#else
3000#if defined(HAVE_AIX_ACL)
3001 int aclsize;
3002 struct acl *aclent;
3003
3004 aclsize = sizeof(struct acl);
3005 aclent = malloc(aclsize);
3006 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3007 {
3008 if (errno == ENOSPC)
3009 {
3010 aclsize = aclent->acl_len;
3011 aclent = realloc(aclent, aclsize);
3012 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3013 {
3014 free(aclent);
3015 return NULL;
3016 }
3017 }
3018 else
3019 {
3020 free(aclent);
3021 return NULL;
3022 }
3023 }
3024 ret = (vim_acl_T)aclent;
3025#endif /* HAVE_AIX_ACL */
3026#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003027#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#endif /* HAVE_POSIX_ACL */
3029 return ret;
3030}
3031
3032/*
3033 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3034 */
3035 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003036mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 if (aclent == NULL)
3039 return;
3040#ifdef HAVE_POSIX_ACL
3041 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3042#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003043#ifdef HAVE_SOLARIS_ZFS_ACL
3044 acl_set((char *)fname, (acl_t *)aclent);
3045#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#ifdef HAVE_SOLARIS_ACL
3047 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3048 ((vim_acl_solaris_T *)aclent)->acl_entry);
3049#else
3050#ifdef HAVE_AIX_ACL
3051 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3052#endif /* HAVE_AIX_ACL */
3053#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003054#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#endif /* HAVE_POSIX_ACL */
3056}
3057
3058 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003059mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 if (aclent == NULL)
3062 return;
3063#ifdef HAVE_POSIX_ACL
3064 acl_free((acl_t)aclent);
3065#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003066#ifdef HAVE_SOLARIS_ZFS_ACL
3067 acl_free((acl_t *)aclent);
3068#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#ifdef HAVE_SOLARIS_ACL
3070 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3071 free(aclent);
3072#else
3073#ifdef HAVE_AIX_ACL
3074 free(aclent);
3075#endif /* HAVE_AIX_ACL */
3076#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003077#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#endif /* HAVE_POSIX_ACL */
3079}
3080#endif
3081
3082/*
3083 * Set hidden flag for "name".
3084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003086mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 /* can't hide a file */
3089}
3090
3091/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003092 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 * return FALSE if "name" is not a directory
3094 * return FALSE for error
3095 */
3096 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003097mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 struct stat statb;
3100
3101 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3102 return FALSE;
3103 if (stat((char *)name, &statb))
3104 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003108/*
3109 * return TRUE if "name" is a directory, NOT a symlink to a directory
3110 * return FALSE if "name" is not a directory
3111 * return FALSE for error
3112 */
3113 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003114mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003115{
3116 struct stat statb;
3117
3118 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3119 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003120 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003121 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003122 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003123}
3124
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003125static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127/*
3128 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3129 */
3130 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003131executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 struct stat st;
3134
3135 if (stat((char *)name, &st))
3136 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003137#ifdef VMS
3138 /* Like on Unix system file can have executable rights but not necessarily
3139 * be an executable, but on Unix is not a default for an ordianry file to
3140 * have an executable flag - on VMS it is in most cases.
3141 * Therefore, this check does not have any sense - let keep us to the
3142 * conventions instead:
3143 * *.COM and *.EXE files are the executables - the rest are not. This is
3144 * not ideal but better then it was.
3145 */
3146 int vms_executable = 0;
3147 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3148 {
3149 if (strstr(vms_tolower((char*)name),".exe") != NULL
3150 || strstr(vms_tolower((char*)name),".com")!= NULL)
3151 vms_executable = 1;
3152 }
3153 return vms_executable;
3154#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157}
3158
3159/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003160 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003161 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * Return -1 if unknown.
3163 */
3164 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003165mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 char_u *buf;
3168 char_u *p, *e;
3169 int retval;
3170
Bram Moolenaarb5971142015-03-21 17:32:19 +01003171 /* When "use_path" is false and if it's an absolute or relative path don't
3172 * need to use $PATH. */
3173 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3174 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003175 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003176 /* There must be a path separator, files in the current directory
3177 * can't be executed. */
3178 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003179 {
3180 if (path != NULL)
3181 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003182 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003183 *path = FullName_save(name, TRUE);
3184 else
3185 *path = vim_strsave(name);
3186 }
3187 return TRUE;
3188 }
3189 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 p = (char_u *)getenv("PATH");
3193 if (p == NULL || *p == NUL)
3194 return -1;
3195 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3196 if (buf == NULL)
3197 return -1;
3198
3199 /*
3200 * Walk through all entries in $PATH to check if "name" exists there and
3201 * is an executable file.
3202 */
3203 for (;;)
3204 {
3205 e = (char_u *)strchr((char *)p, ':');
3206 if (e == NULL)
3207 e = p + STRLEN(p);
3208 if (e - p <= 1) /* empty entry means current dir */
3209 STRCPY(buf, "./");
3210 else
3211 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003212 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 add_pathsep(buf);
3214 }
3215 STRCAT(buf, name);
3216 retval = executable_file(buf);
3217 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003218 {
3219 if (path != NULL)
3220 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003221 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003222 *path = FullName_save(buf, TRUE);
3223 else
3224 *path = vim_strsave(buf);
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229 if (*e != ':')
3230 break;
3231 p = e + 1;
3232 }
3233
3234 vim_free(buf);
3235 return retval;
3236}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238/*
3239 * Check what "name" is:
3240 * NODE_NORMAL: file or directory (or doesn't exist)
3241 * NODE_WRITABLE: writable device, socket, fifo, etc.
3242 * NODE_OTHER: non-writable things
3243 */
3244 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003245mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 struct stat st;
3248
3249 if (stat((char *)name, &st))
3250 return NODE_NORMAL;
3251 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3252 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3254 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 /* Everything else is writable? */
3256 return NODE_WRITABLE;
3257}
3258
3259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003260mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262#ifdef HAVE_CHECK_STACK_GROWTH
3263 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 check_stack_growth((char *)&i);
3266
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003267# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 get_stack_limit();
3269# endif
3270
3271#endif
3272
3273 /*
3274 * Setup an alternative stack for signals. Helps to catch signals when
3275 * running out of stack space.
3276 * Use of sigaltstack() is preferred, it's more portable.
3277 * Ignore any errors.
3278 */
3279#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003280 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 init_signal_stack();
3282#endif
3283}
3284
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003285#if defined(EXITFREE) || defined(PROTO)
3286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003287mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003288{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003289# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3290 if (clip_star.owned)
3291 clip_lose_selection(&clip_star);
3292 if (clip_plus.owned)
3293 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003294# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003295# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003296 if (xterm_Shell != (Widget)0)
3297 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003298# ifndef LESSTIF_VERSION
3299 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003300 if (xterm_dpy != NULL)
3301 XtCloseDisplay(xterm_dpy);
3302 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003303 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003304 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003305# ifdef FEAT_X11
3306 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3307# endif
3308 }
3309# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003310# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003311# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003312 if (x11_display != NULL
3313# ifdef FEAT_XCLIPBOARD
3314 && x11_display != xterm_dpy
3315# endif
3316 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003317 XCloseDisplay(x11_display);
3318# endif
3319# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003320 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321# endif
3322# ifdef FEAT_TITLE
3323 vim_free(oldtitle);
3324 vim_free(oldicon);
3325# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003326}
3327#endif
3328
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003329static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331/*
3332 * Output a newline when exiting.
3333 * Make sure the newline goes to the same stream as the text.
3334 */
3335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003336exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003338 if (silent_mode)
3339 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (newline_on_exit || msg_didout)
3341 {
3342 if (msg_use_printf())
3343 {
3344 if (info_message)
3345 mch_msg("\n");
3346 else
3347 mch_errmsg("\r\n");
3348 }
3349 else
3350 out_char('\n');
3351 }
3352 else
3353 {
3354 restore_cterm_colors(); /* get original colors back */
3355 msg_clr_eos_force(); /* clear the rest of the display */
3356 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3357 }
3358}
3359
3360 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003361mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 exiting = TRUE;
3364
3365#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3366 x11_export_final_selection();
3367#endif
3368
3369#ifdef FEAT_GUI
3370 if (!gui.in_use)
3371#endif
3372 {
3373 settmode(TMODE_COOK);
3374#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003375 // restore xterm title and icon name
3376 mch_restore_title(SAVE_RESTORE_BOTH);
3377 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#endif
3379 /*
3380 * When t_ti is not empty but it doesn't cause swapping terminal
3381 * pages, need to output a newline when msg_didout is set. But when
3382 * t_ti does swap pages it should not go to the shell page. Do this
3383 * before stoptermcap().
3384 */
3385 if (swapping_screen() && !newline_on_exit)
3386 exit_scroll();
3387
3388 /* Stop termcap: May need to check for T_CRV response, which
3389 * requires RAW mode. */
3390 stoptermcap();
3391
3392 /*
3393 * A newline is only required after a message in the alternate screen.
3394 * This is set to TRUE by wait_return().
3395 */
3396 if (!swapping_screen() || newline_on_exit)
3397 exit_scroll();
3398
3399 /* Cursor may have been switched off without calling starttermcap()
3400 * when doing "vim -u vimrc" and vimrc contains ":q". */
3401 if (full_screen)
3402 cursor_on();
3403 }
3404 out_flush();
3405 ml_close_all(TRUE); /* remove all memfiles */
3406 may_core_dump();
3407#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 gui_exit(r);
3410#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003411
Bram Moolenaar56718732006-03-15 22:53:57 +00003412#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003413 mac_conv_cleanup();
3414#endif
3415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#ifdef __QNX__
3417 /* A core dump won't be created if the signal handler
3418 * doesn't return, so we can't call exit() */
3419 if (deadly_signal != 0)
3420 return;
3421#endif
3422
Bram Moolenaar009b2592004-10-24 19:18:58 +00003423#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003424 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003425#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003426
3427#ifdef EXITFREE
3428 free_all_mem();
3429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 exit(r);
3432}
3433
3434 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003435may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 if (deadly_signal != 0)
3438 {
3439 signal(deadly_signal, SIG_DFL);
3440 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3441 }
3442}
3443
3444#ifndef VMS
3445
3446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003447mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 static int first = TRUE;
3450
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003451#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452# ifdef HAVE_TERMIOS_H
3453 static struct termios told;
3454 struct termios tnew;
3455# else
3456 static struct termio told;
3457 struct termio tnew;
3458# endif
3459
3460 if (first)
3461 {
3462 first = FALSE;
3463# if defined(HAVE_TERMIOS_H)
3464 tcgetattr(read_cmd_fd, &told);
3465# else
3466 ioctl(read_cmd_fd, TCGETA, &told);
3467# endif
3468 }
3469
3470 tnew = told;
3471 if (tmode == TMODE_RAW)
3472 {
3473 /*
3474 * ~ICRNL enables typing ^V^M
3475 */
3476 tnew.c_iflag &= ~ICRNL;
3477 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3478# if defined(IEXTEN) && !defined(__MINT__)
3479 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3480 /* but it breaks function keys on MINT */
3481# endif
3482 );
3483# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3484 tnew.c_oflag &= ~ONLCR;
3485# endif
3486 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3487 tnew.c_cc[VTIME] = 0; /* don't wait */
3488 }
3489 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003490 {
3491 /* Also reset ICANON here, otherwise on Solaris select() won't see
3492 * typeahead characters. */
3493 tnew.c_lflag &= ~(ICANON | ECHO);
3494 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3495 tnew.c_cc[VTIME] = 0; /* don't wait */
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498# if defined(HAVE_TERMIOS_H)
3499 {
3500 int n = 10;
3501
3502 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3503 * few times. */
3504 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3505 && errno == EINTR && n > 0)
3506 --n;
3507 }
3508# else
3509 ioctl(read_cmd_fd, TCSETA, &tnew);
3510# endif
3511
3512#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 /*
3514 * for "old" tty systems
3515 */
3516# ifndef TIOCSETN
3517# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3518# endif
3519 static struct sgttyb ttybold;
3520 struct sgttyb ttybnew;
3521
3522 if (first)
3523 {
3524 first = FALSE;
3525 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3526 }
3527
3528 ttybnew = ttybold;
3529 if (tmode == TMODE_RAW)
3530 {
3531 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3532 ttybnew.sg_flags |= RAW;
3533 }
3534 else if (tmode == TMODE_SLEEP)
3535 ttybnew.sg_flags &= ~(ECHO);
3536 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3537#endif
3538 curr_tmode = tmode;
3539}
3540
3541/*
3542 * Try to get the code for "t_kb" from the stty setting
3543 *
3544 * Even if termcap claims a backspace key, the user's setting *should*
3545 * prevail. stty knows more about reality than termcap does, and if
3546 * somebody's usual erase key is DEL (which, for most BSD users, it will
3547 * be), they're going to get really annoyed if their erase key starts
3548 * doing forward deletes for no reason. (Eric Fischer)
3549 */
3550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003551get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003553 ttyinfo_T info;
3554 char_u buf[2];
3555 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003557 if (get_tty_info(read_cmd_fd, &info) == OK)
3558 {
3559 intr_char = info.interrupt;
3560 buf[0] = info.backspace;
3561 buf[1] = NUL;
3562 add_termcode((char_u *)"kb", buf, FALSE);
3563
3564 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3565 p = find_termcode((char_u *)"kD");
3566 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3567 do_fixdel(NULL);
3568 }
3569}
3570
3571/*
3572 * Obtain the characters that Backspace and Enter produce on "fd".
3573 * Returns OK or FAIL.
3574 */
3575 int
3576get_tty_info(int fd, ttyinfo_T *info)
3577{
3578#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# ifdef HAVE_TERMIOS_H
3580 struct termios keys;
3581# else
3582 struct termio keys;
3583# endif
3584
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003585 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003589 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003591 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003593 info->backspace = keys.c_cc[VERASE];
3594 info->interrupt = keys.c_cc[VINTR];
3595 if (keys.c_iflag & ICRNL)
3596 info->enter = NL;
3597 else
3598 info->enter = CAR;
3599 if (keys.c_oflag & ONLCR)
3600 info->nl_does_cr = TRUE;
3601 else
3602 info->nl_does_cr = FALSE;
3603 return OK;
3604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605#else
3606 /* for "old" tty systems */
3607 struct sgttyb keys;
3608
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003609 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003611 info->backspace = keys.sg_erase;
3612 info->interrupt = keys.sg_kill;
3613 info->enter = CAR;
3614 info->nl_does_cr = TRUE;
3615 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003618 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619}
3620
3621#endif /* VMS */
3622
3623#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003624static int mouse_ison = FALSE;
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626/*
3627 * Set mouse clicks on or off.
3628 */
3629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003630mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003632# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003633 static int bevalterm_ison = FALSE;
3634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 int xterm_mouse_vers;
3636
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003637 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003638# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003639 && p_bevalterm == bevalterm_ison
3640# endif
3641 )
3642 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 return;
3644
3645 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003646
3647# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003648 if (ttym_flags == TTYM_URXVT)
3649 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003650 out_str_nf((char_u *)
3651 (on
3652 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3653 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003654 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003655 }
3656# endif
3657
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003658# ifdef FEAT_MOUSE_SGR
3659 if (ttym_flags == TTYM_SGR)
3660 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003661 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003662 out_str_nf((char_u *)
3663 (on
3664 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3665 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003666 mouse_ison = on;
3667 }
3668# endif
3669
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003670# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003671 if (bevalterm_ison != (p_bevalterm && on))
3672 {
3673 bevalterm_ison = (p_bevalterm && on);
3674 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3675 /* disable mouse movement events, enabling is below */
3676 out_str_nf((char_u *)
3677 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003678 }
3679# endif
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (xterm_mouse_vers > 0)
3682 {
3683 if (on) /* enable mouse events, use mouse tracking if available */
3684 out_str_nf((char_u *)
3685 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003686 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003687# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003688 bevalterm_ison
3689 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3690# endif
3691 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3693 else /* disable mouse events, could probably always send the same */
3694 out_str_nf((char_u *)
3695 (xterm_mouse_vers > 1
3696 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3697 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003698 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700
3701# ifdef FEAT_MOUSE_DEC
3702 else if (ttym_flags == TTYM_DEC)
3703 {
3704 if (on) /* enable mouse events */
3705 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3706 else /* disable mouse events */
3707 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003708 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710# endif
3711
3712# ifdef FEAT_MOUSE_GPM
3713 else
3714 {
3715 if (on)
3716 {
3717 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003718 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else
3721 {
3722 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003723 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 }
3726# endif
3727
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003728# ifdef FEAT_SYSMOUSE
3729 else
3730 {
3731 if (on)
3732 {
3733 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003734 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003735 }
3736 else
3737 {
3738 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003739 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003740 }
3741 }
3742# endif
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744# ifdef FEAT_MOUSE_JSB
3745 else
3746 {
3747 if (on)
3748 {
3749 /* D - Enable Mouse up/down messages
3750 * L - Enable Left Button Reporting
3751 * M - Enable Middle Button Reporting
3752 * R - Enable Right Button Reporting
3753 * K - Enable SHIFT and CTRL key Reporting
3754 * + - Enable Advanced messaging of mouse moves and up/down messages
3755 * Q - Quiet No Ack
3756 * # - Numeric value of mouse pointer required
3757 * 0 = Multiview 2000 cursor, used as standard
3758 * 1 = Windows Arrow
3759 * 2 = Windows I Beam
3760 * 3 = Windows Hour Glass
3761 * 4 = Windows Cross Hair
3762 * 5 = Windows UP Arrow
3763 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003764# ifdef JSBTERM_MOUSE_NONADVANCED
3765 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3767 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003768# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3770 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003771# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003772 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 else
3775 {
3776 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3777 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003778 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
3781# endif
3782# ifdef FEAT_MOUSE_PTERM
3783 else
3784 {
3785 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3786 if (on)
3787 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3788 else
3789 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003790 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792# endif
3793}
3794
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003795#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003796/*
3797 * Called when 'balloonevalterm' changed.
3798 */
3799 void
3800mch_bevalterm_changed(void)
3801{
3802 mch_setmouse(mouse_ison);
3803}
3804#endif
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806/*
3807 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3808 */
3809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003810check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812# ifdef FEAT_MOUSE_XTERM
3813 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003814# ifdef FEAT_MOUSE_URXVT
3815 && use_xterm_mouse() != 3
3816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817# ifdef FEAT_GUI
3818 && !gui.in_use
3819# endif
3820 )
3821 {
3822 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003823 ? IF_EB("\233M", CSI_STR "M")
3824 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (*p_mouse != NUL)
3826 {
3827 /* force mouse off and maybe on to send possibly new mouse
3828 * activation sequence to the xterm, with(out) drag tracing. */
3829 mch_setmouse(FALSE);
3830 setmouse();
3831 }
3832 }
3833 else
3834 del_mouse_termcode(KS_MOUSE);
3835# endif
3836
3837# ifdef FEAT_MOUSE_GPM
3838 if (!use_xterm_mouse()
3839# ifdef FEAT_GUI
3840 && !gui.in_use
3841# endif
3842 )
3843 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3844# endif
3845
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003846# ifdef FEAT_SYSMOUSE
3847 if (!use_xterm_mouse()
3848# ifdef FEAT_GUI
3849 && !gui.in_use
3850# endif
3851 )
3852 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3853# endif
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003856 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (!use_xterm_mouse()
3858# ifdef FEAT_GUI
3859 && !gui.in_use
3860# endif
3861 )
3862 set_mouse_termcode(KS_JSBTERM_MOUSE,
3863 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3864 else
3865 del_mouse_termcode(KS_JSBTERM_MOUSE);
3866# endif
3867
3868# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003869 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * define it in the GUI or when using an xterm. */
3871 if (!use_xterm_mouse()
3872# ifdef FEAT_GUI
3873 && !gui.in_use
3874# endif
3875 )
3876 set_mouse_termcode(KS_NETTERM_MOUSE,
3877 (char_u *)IF_EB("\033}", ESC_STR "}"));
3878 else
3879 del_mouse_termcode(KS_NETTERM_MOUSE);
3880# endif
3881
3882# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003883 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003884 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885# ifdef FEAT_GUI
3886 && !gui.in_use
3887# endif
3888 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003889 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3890 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else
3892 del_mouse_termcode(KS_DEC_MOUSE);
3893# endif
3894# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003895 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003896 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897# ifdef FEAT_GUI
3898 && !gui.in_use
3899# endif
3900 )
3901 set_mouse_termcode(KS_PTERM_MOUSE,
3902 (char_u *) IF_EB("\033[", ESC_STR "["));
3903 else
3904 del_mouse_termcode(KS_PTERM_MOUSE);
3905# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003906# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003907 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
3912 {
3913 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003914 ? IF_EB("\233*M", CSI_STR "*M")
3915 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003916
3917 if (*p_mouse != NUL)
3918 {
3919 mch_setmouse(FALSE);
3920 setmouse();
3921 }
3922 }
3923 else
3924 del_mouse_termcode(KS_URXVT_MOUSE);
3925# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003926# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003927 if (use_xterm_mouse() == 4
3928# ifdef FEAT_GUI
3929 && !gui.in_use
3930# endif
3931 )
3932 {
3933 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003934 ? IF_EB("\233<*M", CSI_STR "<*M")
3935 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3936
3937 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3938 ? IF_EB("\233<*m", CSI_STR "<*m")
3939 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003940
3941 if (*p_mouse != NUL)
3942 {
3943 mch_setmouse(FALSE);
3944 setmouse();
3945 }
3946 }
3947 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003948 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003950 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3951 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956/*
3957 * set screen mode, always fails.
3958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003960mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962 EMSG(_(e_screenmode));
3963 return FAIL;
3964}
3965
3966#ifndef VMS
3967
3968/*
3969 * Try to get the current window size:
3970 * 1. with an ioctl(), most accurate method
3971 * 2. from the environment variables LINES and COLUMNS
3972 * 3. from the termcap
3973 * 4. keep using the old values
3974 * Return OK when size could be determined, FAIL otherwise.
3975 */
3976 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003977mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 long rows = 0;
3980 long columns = 0;
3981 char_u *p;
3982
3983 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 * 1. try using an ioctl. It is the most accurate method.
3985 *
3986 * Try using TIOCGWINSZ first, some systems that have it also define
3987 * TIOCGSIZE but don't have a struct ttysize.
3988 */
3989# ifdef TIOCGWINSZ
3990 {
3991 struct winsize ws;
3992 int fd = 1;
3993
3994 /* When stdout is not a tty, use stdin for the ioctl(). */
3995 if (!isatty(fd) && isatty(read_cmd_fd))
3996 fd = read_cmd_fd;
3997 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3998 {
3999 columns = ws.ws_col;
4000 rows = ws.ws_row;
4001 }
4002 }
4003# else /* TIOCGWINSZ */
4004# ifdef TIOCGSIZE
4005 {
4006 struct ttysize ts;
4007 int fd = 1;
4008
4009 /* When stdout is not a tty, use stdin for the ioctl(). */
4010 if (!isatty(fd) && isatty(read_cmd_fd))
4011 fd = read_cmd_fd;
4012 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4013 {
4014 columns = ts.ts_cols;
4015 rows = ts.ts_lines;
4016 }
4017 }
4018# endif /* TIOCGSIZE */
4019# endif /* TIOCGWINSZ */
4020
4021 /*
4022 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004023 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4024 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004026 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
4028 if ((p = (char_u *)getenv("LINES")))
4029 rows = atoi((char *)p);
4030 if ((p = (char_u *)getenv("COLUMNS")))
4031 columns = atoi((char *)p);
4032 }
4033
4034#ifdef HAVE_TGETENT
4035 /*
4036 * 3. try reading "co" and "li" entries from termcap
4037 */
4038 if (columns == 0 || rows == 0)
4039 getlinecol(&columns, &rows);
4040#endif
4041
4042 /*
4043 * 4. If everything fails, use the old values
4044 */
4045 if (columns <= 0 || rows <= 0)
4046 return FAIL;
4047
4048 Rows = rows;
4049 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004050 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return OK;
4052}
4053
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004054#if defined(FEAT_TERMINAL) || defined(PROTO)
4055/*
4056 * Report the windows size "rows" and "cols" to tty "fd".
4057 */
4058 int
4059mch_report_winsize(int fd, int rows, int cols)
4060{
4061# ifdef TIOCSWINSZ
4062 struct winsize ws;
4063
4064 ws.ws_col = cols;
4065 ws.ws_row = rows;
4066 ws.ws_xpixel = cols * 5;
4067 ws.ws_ypixel = rows * 10;
4068 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4069 {
4070 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4071 return OK;
4072 }
4073 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4074# else
4075# ifdef TIOCSSIZE
4076 struct ttysize ts;
4077
4078 ts.ts_cols = cols;
4079 ts.ts_lines = rows;
4080 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4081 {
4082 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4083 return OK;
4084 }
4085 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4086# endif
4087# endif
4088 return FAIL;
4089}
4090#endif
4091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092/*
4093 * Try to set the window size to Rows and Columns.
4094 */
4095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004096mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
4098 if (*T_CWS)
4099 {
4100 /*
4101 * NOTE: if you get an error here that term_set_winsize() is
4102 * undefined, check the output of configure. It could probably not
4103 * find a ncurses, termcap or termlib library.
4104 */
4105 term_set_winsize((int)Rows, (int)Columns);
4106 out_flush();
4107 screen_start(); /* don't know where cursor is now */
4108 }
4109}
4110
4111#endif /* VMS */
4112
4113/*
4114 * Rows and/or Columns has changed.
4115 */
4116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004117mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 /* Nothing to do. */
4120}
4121
Bram Moolenaar205b8862011-09-07 15:04:31 +02004122/*
4123 * Wait for process "child" to end.
4124 * Return "child" if it exited properly, <= 0 on error.
4125 */
4126 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004127wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004128{
4129 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004130 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004131
4132 while (wait_pid != child)
4133 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004134 /* When compiled with Python threads are probably used, in which case
4135 * wait() sometimes hangs for no obvious reason. Use waitpid()
4136 * instead and loop (like the GUI). Also needed for other interfaces,
4137 * they might call system(). */
4138# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004139 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004140# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004141 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004142# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004143 if (wait_pid == 0)
4144 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004145 /* Wait for 1 to 10 msec before trying again. */
4146 mch_delay(delay_msec, TRUE);
4147 if (++delay_msec > 10)
4148 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004149 continue;
4150 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004151 if (wait_pid <= 0
4152# ifdef ECHILD
4153 && errno == ECHILD
4154# endif
4155 )
4156 break;
4157 }
4158 return wait_pid;
4159}
4160
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004161#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004162/*
4163 * Set the environment for a child process.
4164 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004165 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004166set_child_environment(
4167 long rows,
4168 long columns,
4169 char *term,
4170 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004171{
4172# ifdef HAVE_SETENV
4173 char envbuf[50];
4174# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004175 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004176 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004177 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004178 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004179 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004180# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004181 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004182# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004183# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004184 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004185# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004186# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004187 long colors =
4188# ifdef FEAT_GUI
4189 gui.in_use ? 256*256*256 :
4190# endif
4191 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004192
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004193# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004194 setenv("TERM", term, 1);
4195 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004196 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004197 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004198 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004199 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004200 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004201 sprintf((char *)envbuf, "%ld", colors);
4202 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004203# ifdef FEAT_TERMINAL
4204 if (is_terminal)
4205 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004206 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004207 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4208 }
4209# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004210# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004211 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004212# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004213# else
4214 /*
4215 * Putenv does not copy the string, it has to remain valid.
4216 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004217 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004218 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004219 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4220 putenv(envbuf_Term);
4221 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004223 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4224 putenv(envbuf_Lines);
4225 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4226 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004227 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004228 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4229 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004230# ifdef FEAT_TERMINAL
4231 if (is_terminal)
4232 {
4233 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004234 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004235 putenv(envbuf_Version);
4236 }
4237# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004238# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004239 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4240 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4241 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004242# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004243# endif
4244}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004245
4246 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004247set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004248{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004249 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004250}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004251#endif
4252
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004253#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004254/*
4255 * Open a PTY, with FD for the master and slave side.
4256 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4257 * When successful both file descriptors are stored.
4258 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004259 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004260open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004261{
4262 char *tty_name;
4263
4264 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4265 if (*pty_master_fd >= 0)
4266 {
4267 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4268 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4269 * adding O_NOCTTY always works when defined. */
4270#ifdef O_NOCTTY
4271 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4272#else
4273 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4274#endif
4275 if (*pty_slave_fd < 0)
4276 {
4277 close(*pty_master_fd);
4278 *pty_master_fd = -1;
4279 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004280 else if (namep != NULL)
4281 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004282 }
4283}
4284#endif
4285
Bram Moolenaarfae42832017-08-01 22:24:26 +02004286/*
4287 * Send SIGINT to a child process if "c" is an interrupt character.
4288 */
4289 void
4290may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4291{
4292# ifdef SIGINT
4293 if (c == Ctrl_C || c == intr_char)
4294 {
4295# ifdef HAVE_SETSID
4296 kill(-pid, SIGINT);
4297# else
4298 kill(0, SIGINT);
4299# endif
4300 if (wpid > 0)
4301 kill(wpid, SIGINT);
4302 }
4303# endif
4304}
4305
Bram Moolenaar13568252018-03-16 20:46:58 +01004306#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4307
4308 static int
4309build_argv(
4310 char_u *cmd,
4311 char ***argvp,
4312 char_u **sh_tofree,
4313 char_u **shcf_tofree)
4314{
4315 char **argv = NULL;
4316 int argc;
4317
4318 *sh_tofree = vim_strsave(p_sh);
4319 if (*sh_tofree == NULL) /* out of memory */
4320 return FAIL;
4321
4322 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4323 return FAIL;
4324 *argvp = argv;
4325
4326 if (cmd != NULL)
4327 {
4328 char_u *s;
4329 char_u *p;
4330
4331 if (extra_shell_arg != NULL)
4332 argv[argc++] = (char *)extra_shell_arg;
4333
4334 /* Break 'shellcmdflag' into white separated parts. This doesn't
4335 * handle quoted strings, they are very unlikely to appear. */
4336 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4337 if (*shcf_tofree == NULL) /* out of memory */
4338 return FAIL;
4339 s = *shcf_tofree;
4340 p = p_shcf;
4341 while (*p != NUL)
4342 {
4343 argv[argc++] = (char *)s;
4344 while (*p && *p != ' ' && *p != TAB)
4345 *s++ = *p++;
4346 *s++ = NUL;
4347 p = skipwhite(p);
4348 }
4349
4350 argv[argc++] = (char *)cmd;
4351 }
4352 argv[argc] = NULL;
4353 return OK;
4354}
4355#endif
4356
4357#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4358/*
4359 * Use a terminal window to run a shell command in.
4360 */
4361 static int
4362mch_call_shell_terminal(
4363 char_u *cmd,
4364 int options UNUSED) /* SHELL_*, see vim.h */
4365{
4366 jobopt_T opt;
4367 char **argv = NULL;
4368 char_u *tofree1 = NULL;
4369 char_u *tofree2 = NULL;
4370 int retval = -1;
4371 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004372 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004373 aco_save_T aco;
4374 oparg_T oa; /* operator arguments */
4375
4376 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4377 goto theend;
4378
4379 init_job_options(&opt);
4380 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4381 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004382 if (buf == NULL)
4383 goto theend;
4384
4385 job = term_getjob(buf->b_term);
4386 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004387
4388 /* Find a window to make "buf" curbuf. */
4389 aucmd_prepbuf(&aco, buf);
4390
4391 clear_oparg(&oa);
4392 while (term_use_loop())
4393 {
4394 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4395 {
4396 /* If terminal_loop() returns OK we got a key that is handled
4397 * in Normal model. We don't do redrawing anyway. */
4398 if (terminal_loop(TRUE) == OK)
4399 normal_cmd(&oa, TRUE);
4400 }
4401 else
4402 normal_cmd(&oa, TRUE);
4403 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004404 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004405 ch_log(NULL, "system command finished");
4406
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004407 job_unref(job);
4408
Bram Moolenaar13568252018-03-16 20:46:58 +01004409 /* restore curwin/curbuf and a few other things */
4410 aucmd_restbuf(&aco);
4411
4412 wait_return(TRUE);
4413 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4414
4415theend:
4416 vim_free(argv);
4417 vim_free(tofree1);
4418 vim_free(tofree2);
4419 return retval;
4420}
4421#endif
4422
4423#ifdef USE_SYSTEM
4424/*
4425 * Use system() to start the shell: simple but slow.
4426 */
4427 static int
4428mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004429 char_u *cmd,
4430 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
4432#ifdef VMS
4433 char *ifn = NULL;
4434 char *ofn = NULL;
4435#endif
4436 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004437 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004438 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 out_flush();
4441
4442 if (options & SHELL_COOKED)
4443 settmode(TMODE_COOK); /* set to normal mode */
4444
Bram Moolenaar62b42182010-09-21 22:09:37 +02004445# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004446 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004447 loose_clipboard();
4448# endif
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (cmd == NULL)
4451 x = system((char *)p_sh);
4452 else
4453 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004454# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (ofn = strchr((char *)cmd, '>'))
4456 *ofn++ = '\0';
4457 if (ifn = strchr((char *)cmd, '<'))
4458 {
4459 char *p;
4460
4461 *ifn++ = '\0';
4462 p = strchr(ifn,' '); /* chop off any trailing spaces */
4463 if (p)
4464 *p = '\0';
4465 }
4466 if (ofn)
4467 x = vms_sys((char *)cmd, ofn, ifn);
4468 else
4469 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004470# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 newcmd = lalloc(STRLEN(p_sh)
4472 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4473 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4474 if (newcmd == NULL)
4475 x = 0;
4476 else
4477 {
4478 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4479 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4480 (char *)p_shcf,
4481 (char *)cmd);
4482 x = system((char *)newcmd);
4483 vim_free(newcmd);
4484 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487# ifdef VMS
4488 x = vms_sys_status(x);
4489# endif
4490 if (emsg_silent)
4491 ;
4492 else if (x == 127)
4493 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 else if (x && !(options & SHELL_SILENT))
4495 {
4496 MSG_PUTS(_("\nshell returned "));
4497 msg_outnum((long)x);
4498 msg_putchar('\n');
4499 }
4500
4501 if (tmode == TMODE_RAW)
4502 settmode(TMODE_RAW); /* set to raw mode */
4503# ifdef FEAT_TITLE
4504 resettitle();
4505# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004506# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4507 restore_clipboard();
4508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar13568252018-03-16 20:46:58 +01004512#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaardf177f62005-02-22 08:39:57 +00004514# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4515 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004516# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
Bram Moolenaar13568252018-03-16 20:46:58 +01004518/*
4519 * Don't use system(), use fork()/exec().
4520 */
4521 static int
4522mch_call_shell_fork(
4523 char_u *cmd,
4524 int options) /* SHELL_*, see vim.h */
4525{
4526 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004528 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 pid_t wait_pid = 0;
4530# ifdef HAVE_UNION_WAIT
4531 union wait status;
4532# else
4533 int status = -1;
4534# endif
4535 int retval = -1;
4536 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004537 char_u *tofree1 = NULL;
4538 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004541# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004543# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004544 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 int fd_fromshell[2];
4546 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004547 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 out_flush();
4550 if (options & SHELL_COOKED)
4551 settmode(TMODE_COOK); /* set to normal mode */
4552
Bram Moolenaar13568252018-03-16 20:46:58 +01004553 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004554 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004557 * For the GUI, when writing the output into the buffer and when reading
4558 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4559 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004561 if ((options & (SHELL_READ|SHELL_WRITE))
4562# ifdef FEAT_GUI
4563 || (gui.in_use && show_shell_mess)
4564# endif
4565 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004567# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 /*
4569 * Try to open a master pty.
4570 * If this works, open the slave pty.
4571 * If the slave can't be opened, close the master pty.
4572 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004573 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004574 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 /*
4576 * If not opening a pty or it didn't work, try using pipes.
4577 */
4578 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 pipe_error = (pipe(fd_toshell) < 0);
4582 if (!pipe_error) /* pipe create OK */
4583 {
4584 pipe_error = (pipe(fd_fromshell) < 0);
4585 if (pipe_error) /* pipe create failed */
4586 {
4587 close(fd_toshell[0]);
4588 close(fd_toshell[1]);
4589 }
4590 }
4591 if (pipe_error)
4592 {
4593 MSG_PUTS(_("\nCannot create pipes\n"));
4594 out_flush();
4595 }
4596 }
4597 }
4598
4599 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004601 SIGSET_DECL(curset)
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603# ifdef __BEOS__
4604 beos_cleanup_read_thread();
4605# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004606
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004607 BLOCK_SIGNALS(&curset);
4608 pid = fork(); /* maybe we should use vfork() */
4609 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004611 UNBLOCK_SIGNALS(&curset);
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004616 || (gui.in_use && show_shell_mess)
4617# endif
4618 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004620# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (pty_master_fd >= 0) /* close the pseudo tty */
4622 {
4623 close(pty_master_fd);
4624 close(pty_slave_fd);
4625 }
4626 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 close(fd_toshell[0]);
4630 close(fd_toshell[1]);
4631 close(fd_fromshell[0]);
4632 close(fd_fromshell[1]);
4633 }
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 else if (pid == 0) /* child */
4637 {
4638 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004639 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar819524702018-02-27 19:10:00 +01004641# ifdef FEAT_JOB_CHANNEL
4642 if (ch_log_active())
4643 /* close the log file in the child */
4644 ch_logfile((char_u *)"", (char_u *)"");
4645# endif
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (!show_shell_mess || (options & SHELL_EXPAND))
4648 {
4649 int fd;
4650
4651 /*
4652 * Don't want to show any message from the shell. Can't just
4653 * close stdout and stderr though, because some systems will
4654 * break if you try to write to them after that, so we must
4655 * use dup() to replace them with something else -- webb
4656 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4657 * waiting for input.
4658 */
4659 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4660 fclose(stdin);
4661 fclose(stdout);
4662 fclose(stderr);
4663
4664 /*
4665 * If any of these open()'s and dup()'s fail, we just continue
4666 * anyway. It's not fatal, and on most systems it will make
4667 * no difference at all. On a few it will cause the execvp()
4668 * to exit with a non-zero status even when the completion
4669 * could be done, which is nothing too serious. If the open()
4670 * or dup() failed we'd just do the same thing ourselves
4671 * anyway -- webb
4672 */
4673 if (fd >= 0)
4674 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004675 ignored = dup(fd); /* To replace stdin (fd 0) */
4676 ignored = dup(fd); /* To replace stdout (fd 1) */
4677 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /* Don't need this now that we've duplicated it */
4680 close(fd);
4681 }
4682 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004683 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004685 || gui.in_use
4686# endif
4687 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689
Bram Moolenaardf177f62005-02-22 08:39:57 +00004690# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004691 /* Create our own process group, so that the child and all its
4692 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004693 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004694 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004695 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004696 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004697# if defined(SIGHUP)
4698 /* When doing "!xterm&" and 'shell' is bash: the shell
4699 * will exit and send SIGHUP to all processes in its
4700 * group, killing the just started process. Ignore SIGHUP
4701 * to avoid that. (suggested by Simon Schubert)
4702 */
4703 signal(SIGHUP, SIG_IGN);
4704# endif
4705 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004706# endif
4707# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004708 if (pty_slave_fd >= 0)
4709 {
4710 /* push stream discipline modules */
4711 if (options & SHELL_COOKED)
4712 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004714 /* Try to become controlling tty (probably doesn't work,
4715 * unless run by root) */
4716 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004718 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004719# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004720 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
Bram Moolenaara5792f52005-11-23 21:25:05 +00004722 /*
4723 * stderr is only redirected when using the GUI, so that a
4724 * program like gpg can still access the terminal to get a
4725 * passphrase using stderr.
4726 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (pty_master_fd >= 0)
4729 {
4730 close(pty_master_fd); /* close master side of pty */
4731
4732 /* set up stdin/stdout/stderr for the child */
4733 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004734 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004736 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004737 if (gui.in_use)
4738 {
4739 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004740 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
4743 close(pty_slave_fd); /* has been dupped, close it now */
4744 }
4745 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 /* set up stdin for the child */
4749 close(fd_toshell[1]);
4750 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004751 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 close(fd_toshell[0]);
4753
4754 /* set up stdout for the child */
4755 close(fd_fromshell[0]);
4756 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004757 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 close(fd_fromshell[1]);
4759
Bram Moolenaara5792f52005-11-23 21:25:05 +00004760# ifdef FEAT_GUI
4761 if (gui.in_use)
4762 {
4763 /* set up stderr for the child */
4764 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004765 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004766 }
4767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /*
4772 * There is no type cast for the argv, because the type may be
4773 * different on different machines. This may cause a warning
4774 * message with strict compilers, don't worry about it.
4775 * Call _exit() instead of exit() to avoid closing the connection
4776 * to the X server (esp. with GTK, which uses atexit()).
4777 */
4778 execvp(argv[0], argv);
4779 _exit(EXEC_FAILED); /* exec failed, return failure code */
4780 }
4781 else /* parent */
4782 {
4783 /*
4784 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004785 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 */
4787 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004788 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004789 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004790# ifdef FEAT_JOB_CHANNEL
4791 ++dont_check_job_ended;
4792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 /*
4794 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004795 * This is also used to pipe stdin/stdout to/from the external
4796 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004798 if ((options & (SHELL_READ|SHELL_WRITE))
4799# ifdef FEAT_GUI
4800 || (gui.in_use && show_shell_mess)
4801# endif
4802 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004804# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4810 int ta_len = 0; /* valid bytes in ta_buf[] */
4811 int len;
4812 int p_more_save;
4813 int old_State;
4814 int c;
4815 int toshell_fd;
4816 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004817 garray_T ga;
4818 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004819# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4820 struct timeval start_tv;
4821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (pty_master_fd >= 0)
4825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 fromshell_fd = pty_master_fd;
4827 toshell_fd = dup(pty_master_fd);
4828 }
4829 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832 close(fd_toshell[0]);
4833 close(fd_fromshell[1]);
4834 toshell_fd = fd_toshell[1];
4835 fromshell_fd = fd_fromshell[0];
4836 }
4837
4838 /*
4839 * Write to the child if there are typed characters.
4840 * Read from the child if there are characters available.
4841 * Repeat the reading a few times if more characters are
4842 * available. Need to check for typed keys now and then, but
4843 * not too often (delays when no chars are available).
4844 * This loop is quit if no characters can be read from the pty
4845 * (WaitForChar detected special condition), or there are no
4846 * characters available and the child has exited.
4847 * Only check if the child has exited when there is no more
4848 * output. The child may exit before all the output has
4849 * been printed.
4850 *
4851 * Currently this busy loops!
4852 * This can probably dead-lock when the write blocks!
4853 */
4854 p_more_save = p_more;
4855 p_more = FALSE;
4856 old_State = State;
4857 State = EXTERNCMD; /* don't redraw at window resize */
4858
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004859 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004860 {
4861 /* Fork a process that will write the lines to the
4862 * external program. */
4863 if ((wpid = fork()) == -1)
4864 {
4865 MSG_PUTS(_("\nCannot fork\n"));
4866 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004867 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004868 {
4869 linenr_T lnum = curbuf->b_op_start.lnum;
4870 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004871 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004872 size_t l;
4873
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004874 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004875 for (;;)
4876 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004877 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004878 if (l == 0)
4879 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004880 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 /* NL -> NUL translation */
4882 len = write(toshell_fd, "", (size_t)1);
4883 else
4884 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004885 char_u *s = vim_strchr(lp + written, NL);
4886
Bram Moolenaar89d40322006-08-29 15:30:07 +00004887 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004888 s == NULL ? l
4889 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004891 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004892 {
4893 /* Finished a line, add a NL, unless this line
4894 * should not have one. */
4895 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004896 || (!curbuf->b_p_bin
4897 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004898 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004899 && (lnum !=
4900 curbuf->b_ml.ml_line_count
4901 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004902 ignored = write(toshell_fd, "\n",
4903 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004904 ++lnum;
4905 if (lnum > curbuf->b_op_end.lnum)
4906 {
4907 /* finished all the lines, close pipe */
4908 close(toshell_fd);
4909 toshell_fd = -1;
4910 break;
4911 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004912 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 written = 0;
4914 }
4915 else if (len > 0)
4916 written += len;
4917 }
4918 _exit(0);
4919 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004920 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 {
4922 close(toshell_fd);
4923 toshell_fd = -1;
4924 }
4925 }
4926
4927 if (options & SHELL_READ)
4928 ga_init2(&ga, 1, BUFLEN);
4929
4930 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004931# ifdef ELAPSED_FUNC
4932 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 for (;;)
4935 {
4936 /*
4937 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004938 * if there are any.
4939 * Don't do this if we are expanding wild cards (would eat
4940 * typeahead).
4941 * Don't do this when filtering and terminal is in cooked
4942 * mode, the shell command will handle the I/O. Avoids
4943 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004944 * Don't get characters when the child has already
4945 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004946 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004947 * while (noread_cnt > 4), avoids that ":r !ls" eats
4948 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 */
4950 len = 0;
4951 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004952 && ((options &
4953 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4954 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004955# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004956 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004957# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004958 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004959 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004960 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004962 if (ta_len == 0)
4963 {
4964 /* Get extra characters when we don't have any.
4965 * Reset the counter and timer. */
4966 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004967# ifdef ELAPSED_FUNC
4968 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004969# endif
4970 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4971 }
4972 if (ta_len > 0 || len > 0)
4973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 /*
4975 * For pipes:
4976 * Check for CTRL-C: send interrupt signal to child.
4977 * Check for CTRL-D: EOF, close pipe to child.
4978 */
4979 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /*
4982 * Send SIGINT to the child's group or all
4983 * processes in our group.
4984 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004985 may_send_sigint(ta_buf[ta_len], pid, wpid);
4986
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (pty_master_fd < 0 && toshell_fd >= 0
4988 && ta_buf[ta_len] == Ctrl_D)
4989 {
4990 close(toshell_fd);
4991 toshell_fd = -1;
4992 }
4993 }
4994
4995 /* replace K_BS by <BS> and K_DEL by <DEL> */
4996 for (i = ta_len; i < ta_len + len; ++i)
4997 {
4998 if (ta_buf[i] == CSI && len - i > 2)
4999 {
5000 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5001 if (c == K_DEL || c == K_KDEL || c == K_BS)
5002 {
5003 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5004 (size_t)(len - i - 2));
5005 if (c == K_DEL || c == K_KDEL)
5006 ta_buf[i] = DEL;
5007 else
5008 ta_buf[i] = Ctrl_H;
5009 len -= 2;
5010 }
5011 }
5012 else if (ta_buf[i] == '\r')
5013 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005014# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005016 i += (*mb_ptr2len_len)(ta_buf + i,
5017 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020
5021 /*
5022 * For pipes: echo the typed characters.
5023 * For a pty this does not seem to work.
5024 */
5025 if (pty_master_fd < 0)
5026 {
5027 for (i = ta_len; i < ta_len + len; ++i)
5028 {
5029 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5030 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005031# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 else if (has_mbyte)
5033 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005034 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
5036 msg_outtrans_len(ta_buf + i, l);
5037 i += l - 1;
5038 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 else
5041 msg_outtrans_len(ta_buf + i, 1);
5042 }
5043 windgoto(msg_row, msg_col);
5044 out_flush();
5045 }
5046
5047 ta_len += len;
5048
5049 /*
5050 * Write the characters to the child, unless EOF has
5051 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005052 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005053 * When writing buffer lines, drop the typed
5054 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005056 if (options & SHELL_WRITE)
5057 ta_len = 0;
5058 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5061 if (len > 0)
5062 {
5063 ta_len -= len;
5064 mch_memmove(ta_buf, ta_buf + len, ta_len);
5065 }
5066 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
Bram Moolenaardf177f62005-02-22 08:39:57 +00005070 if (got_int)
5071 {
5072 /* CTRL-C sends a signal to the child, we ignore it
5073 * ourselves */
5074# ifdef HAVE_SETSID
5075 kill(-pid, SIGINT);
5076# else
5077 kill(0, SIGINT);
5078# endif
5079 if (wpid > 0)
5080 kill(wpid, SIGINT);
5081 got_int = FALSE;
5082 }
5083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 /*
5085 * Check if the child has any characters to be printed.
5086 * Read them and write them to our window. Repeat this as
5087 * long as there is something to do, avoid the 10ms wait
5088 * for mch_inchar(), or sending typeahead characters to
5089 * the external process.
5090 * TODO: This should handle escape sequences, compatible
5091 * to some terminal (vt52?).
5092 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005093 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005094 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005096 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005097# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005099# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 );
5103 if (len <= 0) /* end of file or error */
5104 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005105
5106 noread_cnt = 0;
5107 if (options & SHELL_READ)
5108 {
5109 /* Do NUL -> NL translation, append NL separated
5110 * lines to the current buffer. */
5111 for (i = 0; i < len; ++i)
5112 {
5113 if (buffer[i] == NL)
5114 append_ga_line(&ga);
5115 else if (buffer[i] == NUL)
5116 ga_append(&ga, NL);
5117 else
5118 ga_append(&ga, buffer[i]);
5119 }
5120 }
5121# ifdef FEAT_MBYTE
5122 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005125 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaardf177f62005-02-22 08:39:57 +00005127 len += buffer_off;
5128 buffer[len] = NUL;
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 /* Check if the last character in buffer[] is
5131 * incomplete, keep these bytes for the next
5132 * round. */
5133 for (p = buffer; p < buffer + len; p += l)
5134 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005135 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (l == 0)
5137 l = 1; /* NUL byte? */
5138 else if (MB_BYTE2LEN(*p) != l)
5139 break;
5140 }
5141 if (p == buffer) /* no complete character */
5142 {
5143 /* avoid getting stuck at an illegal byte */
5144 if (len >= 12)
5145 ++p;
5146 else
5147 {
5148 buffer_off = len;
5149 continue;
5150 }
5151 }
5152 c = *p;
5153 *p = NUL;
5154 msg_puts(buffer);
5155 if (p < buffer + len)
5156 {
5157 *p = c;
5158 buffer_off = (buffer + len) - p;
5159 mch_memmove(buffer, p, buffer_off);
5160 continue;
5161 }
5162 buffer_off = 0;
5163 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005164# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 buffer[len] = NUL;
5168 msg_puts(buffer);
5169 }
5170
5171 windgoto(msg_row, msg_col);
5172 cursor_on();
5173 out_flush();
5174 if (got_int)
5175 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005176
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005177# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005178 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005179 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005180 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005181
5182 /* Avoid that we keep looping here without
5183 * checking for a CTRL-C for a long time. Don't
5184 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005185 if (msec > 2000)
5186 {
5187 noread_cnt = 5;
5188 break;
5189 }
5190 }
5191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005194 /* If we already detected the child has finished, continue
5195 * reading output for a short while. Some text may be
5196 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005197 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005198 {
5199 if (noread_cnt < 5)
5200 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005201 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005202 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /*
5205 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005206 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005208# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005209 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005210# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5214 || (wait_pid == pid && WIFEXITED(status)))
5215 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005216 /* Don't break the loop yet, try reading more
5217 * characters from "fromshell_fd" first. When using
5218 * pipes there might still be something to read and
5219 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005222 else
5223 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005224
Bram Moolenaar95a51352013-03-21 22:53:50 +01005225# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005226 /* Handle any X events, e.g. serving the clipboard. */
5227 clip_update();
5228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230finished:
5231 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005232 if (options & SHELL_READ)
5233 {
5234 if (ga.ga_len > 0)
5235 {
5236 append_ga_line(&ga);
5237 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005238 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005239 }
5240 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005241 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005242 ga_clear(&ga);
5243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /*
5246 * Give all typeahead that wasn't used back to ui_inchar().
5247 */
5248 if (ta_len)
5249 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 State = old_State;
5251 if (toshell_fd >= 0)
5252 close(toshell_fd);
5253 close(fromshell_fd);
5254 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005255# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005256 else
5257 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005258 long delay_msec = 1;
5259
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005260 /*
5261 * Similar to the loop above, but only handle X events, no
5262 * I/O.
5263 */
5264 for (;;)
5265 {
5266 if (got_int)
5267 {
5268 /* CTRL-C sends a signal to the child, we ignore it
5269 * ourselves */
5270# ifdef HAVE_SETSID
5271 kill(-pid, SIGINT);
5272# else
5273 kill(0, SIGINT);
5274# endif
5275 got_int = FALSE;
5276 }
5277# ifdef __NeXT__
5278 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5279# else
5280 wait_pid = waitpid(pid, &status, WNOHANG);
5281# endif
5282 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5283 || (wait_pid == pid && WIFEXITED(status)))
5284 {
5285 wait_pid = pid;
5286 break;
5287 }
5288
5289 /* Handle any X events, e.g. serving the clipboard. */
5290 clip_update();
5291
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005292 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5293 * less time. */
5294 mch_delay(delay_msec, TRUE);
5295 if (++delay_msec > 10)
5296 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005297 }
5298 }
5299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Wait until our child has exited.
5303 * Ignore wait() returning pids of other children and returning
5304 * because of some signal like SIGWINCH.
5305 * Don't wait if wait_pid was already set above, indicating the
5306 * child already exited.
5307 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005308 if (wait_pid != pid)
5309 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
Bram Moolenaar624891f2010-10-13 16:22:09 +02005311# ifdef FEAT_GUI
5312 /* Close slave side of pty. Only do this after the child has
5313 * exited, otherwise the child may hang when it tries to write on
5314 * the pty. */
5315 if (pty_master_fd >= 0)
5316 close(pty_slave_fd);
5317# endif
5318
Bram Moolenaardf177f62005-02-22 08:39:57 +00005319 /* Make sure the child that writes to the external program is
5320 * dead. */
5321 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005322 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005323 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005324 wait4pid(wpid, NULL);
5325 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005326
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005327# ifdef FEAT_JOB_CHANNEL
5328 --dont_check_job_ended;
5329# endif
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /*
5332 * Set to raw mode right now, otherwise a CTRL-C after
5333 * catch_signals() will kill Vim.
5334 */
5335 if (tmode == TMODE_RAW)
5336 settmode(TMODE_RAW);
5337 did_settmode = TRUE;
5338 set_signals();
5339
5340 if (WIFEXITED(status))
5341 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005342 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005344 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
5346 if (retval == EXEC_FAILED)
5347 {
5348 MSG_PUTS(_("\nCannot execute shell "));
5349 msg_outtrans(p_sh);
5350 msg_putchar('\n');
5351 }
5352 else if (!(options & SHELL_SILENT))
5353 {
5354 MSG_PUTS(_("\nshell returned "));
5355 msg_outnum((long)retval);
5356 msg_putchar('\n');
5357 }
5358 }
5359 }
5360 else
5361 MSG_PUTS(_("\nCommand terminated\n"));
5362 }
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365error:
5366 if (!did_settmode)
5367 if (tmode == TMODE_RAW)
5368 settmode(TMODE_RAW); /* set to raw mode */
5369# ifdef FEAT_TITLE
5370 resettitle();
5371# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005372 vim_free(argv);
5373 vim_free(tofree1);
5374 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005379
5380 int
5381mch_call_shell(
5382 char_u *cmd,
5383 int options) /* SHELL_*, see vim.h */
5384{
5385#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5386 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5387 return mch_call_shell_terminal(cmd, options);
5388#endif
5389#ifdef USE_SYSTEM
5390 return mch_call_shell_system(cmd, options);
5391#else
5392 return mch_call_shell_fork(cmd, options);
5393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
5395
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005396#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005397 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005398mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005399{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005400 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005401 int fd_in[2] = {-1, -1}; /* for stdin */
5402 int fd_out[2] = {-1, -1}; /* for stdout */
5403 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005404 int pty_master_fd = -1;
5405 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005406 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005407 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5408 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5409 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005410 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005411 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5412 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005413 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005414 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005415 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005416
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005417 if (use_out_for_err && use_null_for_out)
5418 use_null_for_err = TRUE;
5419
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005420 /* default is to fail */
5421 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005422
Bram Moolenaarb2412082017-08-20 18:09:14 +02005423 if (options->jo_pty
5424 && (!(use_file_for_in || use_null_for_in)
5425 || !(use_file_for_in || use_null_for_out)
5426 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005427 {
5428 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5429 if (job->jv_tty_out != NULL)
5430 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5431 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005432
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005433 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005434 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005435 if (use_file_for_in)
5436 {
5437 char_u *fname = options->jo_io_name[PART_IN];
5438
5439 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5440 if (fd_in[0] < 0)
5441 {
5442 EMSG2(_(e_notopen), fname);
5443 goto failed;
5444 }
5445 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005446 else
5447 /* When writing buffer lines to the input don't use the pty, so that
5448 * the pipe can be closed when all lines were written. */
5449 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5450 && pipe(fd_in) < 0)
5451 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005452
5453 if (use_file_for_out)
5454 {
5455 char_u *fname = options->jo_io_name[PART_OUT];
5456
5457 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5458 if (fd_out[1] < 0)
5459 {
5460 EMSG2(_(e_notopen), fname);
5461 goto failed;
5462 }
5463 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005464 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005465 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005466
5467 if (use_file_for_err)
5468 {
5469 char_u *fname = options->jo_io_name[PART_ERR];
5470
5471 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5472 if (fd_err[1] < 0)
5473 {
5474 EMSG2(_(e_notopen), fname);
5475 goto failed;
5476 }
5477 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005478 else if (!use_out_for_err && !use_null_for_err
5479 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005480 goto failed;
5481
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005482 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5483 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005484 if (options->jo_set & JO_CHANNEL)
5485 {
5486 channel = options->jo_channel;
5487 if (channel != NULL)
5488 ++channel->ch_refcount;
5489 }
5490 else
5491 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005492 if (channel == NULL)
5493 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005494 if (job->jv_tty_out != NULL)
5495 ch_log(channel, "using pty %s on fd %d",
5496 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005497 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005498
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005499 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005500 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005501 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005502 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005503 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005504 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005505 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005507 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005509 int null_fd = -1;
5510 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005511
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512 /* child */
5513 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005514 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005515
Bram Moolenaar819524702018-02-27 19:10:00 +01005516# ifdef FEAT_JOB_CHANNEL
5517 if (ch_log_active())
5518 /* close the log file in the child */
5519 ch_logfile((char_u *)"", (char_u *)"");
5520# endif
5521
Bram Moolenaar835dc632016-02-07 14:27:38 +01005522# ifdef HAVE_SETSID
5523 /* Create our own process group, so that the child and all its
5524 * children can be kill()ed. Don't do this when using pipes,
5525 * because stdin is not a tty, we would lose /dev/tty. */
5526 (void)setsid();
5527# endif
5528
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005529# ifdef FEAT_TERMINAL
5530 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005531 {
5532 char *term = (char *)T_NAME;
5533
5534#ifdef FEAT_GUI
5535 if (term_is_gui(T_NAME))
5536 /* In the GUI 'term' is not what we want, use $TERM. */
5537 term = getenv("TERM");
5538#endif
5539 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5540 * back to "xterm". */
5541 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5542 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005543 set_child_environment(
5544 (long)options->jo_term_rows,
5545 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005546 term,
5547 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005548 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005549 else
5550# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005551 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005552
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005553 if (options->jo_env != NULL)
5554 {
5555 dict_T *dict = options->jo_env;
5556 hashitem_T *hi;
5557 int todo = (int)dict->dv_hashtab.ht_used;
5558
5559 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5560 if (!HASHITEM_EMPTY(hi))
5561 {
5562 typval_T *item = &dict_lookup(hi)->di_tv;
5563
5564 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5565 --todo;
5566 }
5567 }
5568
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005569 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005570 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005571 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005572 if (null_fd < 0)
5573 {
5574 perror("opening /dev/null failed");
5575 _exit(OPEN_NULL_FAILED);
5576 }
5577 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005578
Bram Moolenaar223896d2017-08-02 22:33:28 +02005579 if (pty_slave_fd >= 0)
5580 {
5581 /* push stream discipline modules */
5582 SetupSlavePTY(pty_slave_fd);
5583# ifdef TIOCSCTTY
5584 /* Try to become controlling tty (probably doesn't work,
5585 * unless run by root) */
5586 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5587# endif
5588 }
5589
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005590 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005591 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005592 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005593 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005594 else if (fd_in[0] < 0)
5595 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005596 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005597 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005598
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005599 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005600 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005601 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005602 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005603 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005604 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005605 }
5606 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005607 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005608 else if (fd_err[1] < 0)
5609 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005610 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005611 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005612
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005613 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005614 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005615 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005616 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005617 else if (fd_out[1] < 0)
5618 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005619 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005620 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005621
5622 if (fd_in[0] >= 0)
5623 close(fd_in[0]);
5624 if (fd_in[1] >= 0)
5625 close(fd_in[1]);
5626 if (fd_out[0] >= 0)
5627 close(fd_out[0]);
5628 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005629 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005630 if (fd_err[0] >= 0)
5631 close(fd_err[0]);
5632 if (fd_err[1] >= 0)
5633 close(fd_err[1]);
5634 if (pty_master_fd >= 0)
5635 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005636 close(pty_master_fd); /* not used in the child */
5637 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005638 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005639
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005640 if (null_fd >= 0)
5641 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005642
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005643 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5644 _exit(EXEC_FAILED);
5645
Bram Moolenaar835dc632016-02-07 14:27:38 +01005646 /* See above for type of argv. */
5647 execvp(argv[0], argv);
5648
Bram Moolenaar4694a172016-04-21 14:05:23 +02005649 if (stderr_works)
5650 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005651# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005652 /* calling free_all_mem() here causes problems. Ignore valgrind
5653 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005654# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005655 _exit(EXEC_FAILED); /* exec failed, return failure code */
5656 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005657
5658 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005659 UNBLOCK_SIGNALS(&curset);
5660
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005661 job->jv_pid = pid;
5662 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005663 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005664
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005665 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005666 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005667 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005668 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005669 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005670 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005671 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005672 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005673 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005674 if (channel != NULL)
5675 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005676 int in_fd = use_file_for_in || use_null_for_in
5677 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5678 int out_fd = use_file_for_out || use_null_for_out
5679 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005680 /* When using pty_master_fd only set it for stdout, do not duplicate it
5681 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005682 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005683 ? INVALID_FD
5684 : fd_err[0] >= 0
5685 ? fd_err[0]
5686 : (out_fd == pty_master_fd
5687 ? INVALID_FD
5688 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005689
5690 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005691 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005692 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005693 else
5694 {
5695 if (fd_in[1] >= 0)
5696 close(fd_in[1]);
5697 if (fd_out[0] >= 0)
5698 close(fd_out[0]);
5699 if (fd_err[0] >= 0)
5700 close(fd_err[0]);
5701 if (pty_master_fd >= 0)
5702 close(pty_master_fd);
5703 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005705 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706 return;
5707
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005708failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005709 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005710 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005711 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005712 if (fd_in[1] >= 0)
5713 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005714 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005716 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005717 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005718 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005720 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005721 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005722 if (pty_master_fd >= 0)
5723 close(pty_master_fd);
5724 if (pty_slave_fd >= 0)
5725 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005726}
5727
5728 char *
5729mch_job_status(job_T *job)
5730{
5731# ifdef HAVE_UNION_WAIT
5732 union wait status;
5733# else
5734 int status = -1;
5735# endif
5736 pid_t wait_pid = 0;
5737
5738# ifdef __NeXT__
5739 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5740# else
5741 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5742# endif
5743 if (wait_pid == -1)
5744 {
5745 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005746 if (job->jv_status < JOB_ENDED)
5747 ch_log(job->jv_channel, "Job no longer exists: %s",
5748 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005749 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005750 }
5751 if (wait_pid == 0)
5752 return "run";
5753 if (WIFEXITED(status))
5754 {
5755 /* LINTED avoid "bitwise operation on signed value" */
5756 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005757 if (job->jv_status < JOB_ENDED)
5758 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005759 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005760 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005761 if (WIFSIGNALED(status))
5762 {
5763 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005764 if (job->jv_status < JOB_ENDED)
5765 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005766 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005767 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005769
5770return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005771 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005772 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005773 return "dead";
5774}
5775
5776 job_T *
5777mch_detect_ended_job(job_T *job_list)
5778{
5779# ifdef HAVE_UNION_WAIT
5780 union wait status;
5781# else
5782 int status = -1;
5783# endif
5784 pid_t wait_pid = 0;
5785 job_T *job;
5786
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005787# ifndef USE_SYSTEM
5788 /* Do not do this when waiting for a shell command to finish, we would get
5789 * the exit value here (and discard it), the exit value obtained there
5790 * would then be wrong. */
5791 if (dont_check_job_ended > 0)
5792 return NULL;
5793# endif
5794
Bram Moolenaar97792de2016-10-15 18:36:49 +02005795# ifdef __NeXT__
5796 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5797# else
5798 wait_pid = waitpid(-1, &status, WNOHANG);
5799# endif
5800 if (wait_pid <= 0)
5801 /* no process ended */
5802 return NULL;
5803 for (job = job_list; job != NULL; job = job->jv_next)
5804 {
5805 if (job->jv_pid == wait_pid)
5806 {
5807 if (WIFEXITED(status))
5808 /* LINTED avoid "bitwise operation on signed value" */
5809 job->jv_exitval = WEXITSTATUS(status);
5810 else if (WIFSIGNALED(status))
5811 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005812 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005813 {
5814 ch_log(job->jv_channel, "Job ended");
5815 job->jv_status = JOB_ENDED;
5816 }
5817 return job;
5818 }
5819 }
5820 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005821}
5822
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005823/*
5824 * Send a (deadly) signal to "job".
5825 * Return FAIL if "how" is not a valid name.
5826 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005827 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005828mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005829{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005830 int sig = -1;
5831 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005832
Bram Moolenaar923d9262016-02-25 20:56:01 +01005833 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005834 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005835 else if (STRCMP(how, "hup") == 0)
5836 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005837 else if (STRCMP(how, "quit") == 0)
5838 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005839 else if (STRCMP(how, "int") == 0)
5840 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841 else if (STRCMP(how, "kill") == 0)
5842 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005843#ifdef SIGWINCH
5844 else if (STRCMP(how, "winch") == 0)
5845 sig = SIGWINCH;
5846#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 else if (isdigit(*how))
5848 sig = atoi((char *)how);
5849 else
5850 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005851
Bram Moolenaar72801402016-02-09 11:37:50 +01005852 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005853 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005854#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005855 if (job_pid == getpgid(job_pid))
5856 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005857#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005858
Bram Moolenaar61a66052017-07-22 18:39:00 +02005859 /* Never kill ourselves! */
5860 if (job_pid != 0)
5861 kill(job_pid, sig);
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 Moolenaar8b877ac2016-03-28 19:16:20 +02006205 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006207 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006209# ifdef FEAT_MZSCHEME
6210 mzvim_check_threads();
6211 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6212 {
6213 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6214 mzquantum_used = TRUE;
6215 }
6216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006218 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006220 tv.tv_sec = towait / 1000;
6221 tv.tv_usec = (towait % 1000) * (1000000/1000);
6222 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006224 else
6225 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 /*
6228 * Select on ready for reading and exceptional condition (end of file).
6229 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006230select_eintr:
6231 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006232 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 FD_ZERO(&efds);
6234 FD_SET(fd, &rfds);
6235# if !defined(__QNX__) && !defined(__CYGWIN32__)
6236 /* For QNX select() always returns 1 if this is set. Why? */
6237 FD_SET(fd, &efds);
6238# endif
6239 maxfd = fd;
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006242 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (xterm_Shell != (Widget)0)
6244 {
6245 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6246 if (maxfd < ConnectionNumber(xterm_dpy))
6247 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006248
6249 /* An event may have already been read but not handled. In
6250 * particulary, XFlush may cause this. */
6251 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
6253# endif
6254# ifdef FEAT_MOUSE_GPM
6255 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6256 {
6257 FD_SET(gpm_fd, &rfds);
6258 FD_SET(gpm_fd, &efds);
6259 if (maxfd < gpm_fd)
6260 maxfd = gpm_fd;
6261 }
6262# endif
6263# ifdef USE_XSMP
6264 if (xsmp_icefd != -1)
6265 {
6266 FD_SET(xsmp_icefd, &rfds);
6267 FD_SET(xsmp_icefd, &efds);
6268 if (maxfd < xsmp_icefd)
6269 maxfd = xsmp_icefd;
6270 }
6271# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006272# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006273 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006274# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006275 if (interrupted != NULL)
6276 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006278 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006279 result = ret > 0 && FD_ISSET(fd, &rfds);
6280 if (result)
6281 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006282 else if (interrupted != NULL && ret > 0)
6283 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006284
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006285# ifdef EINTR
6286 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006287 {
6288 /* Check whether window has been resized, EINTR may be caused by
6289 * SIGWINCH. */
6290 if (do_resize)
6291 handle_resize();
6292
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006293 /* Interrupted by a signal, need to try again. We ignore msec
6294 * here, because we do want to check even after a timeout if
6295 * characters are available. Needed for reading output of an
6296 * external command after the process has finished. */
6297 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006298 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006299# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006300# ifdef __TANDEM
6301 if (ret == -1 && errno == ENOTSUP)
6302 {
6303 FD_ZERO(&rfds);
6304 FD_ZERO(&efds);
6305 ret = 0;
6306 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006307# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006308# ifdef FEAT_MZSCHEME
6309 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006310 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006311 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312# endif
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314# ifdef FEAT_XCLIPBOARD
6315 if (ret > 0 && xterm_Shell != (Widget)0
6316 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6317 {
6318 xterm_update(); /* Maybe we should hand out clipboard */
6319 /* continue looping when we only got the X event and the input
6320 * buffer is empty */
6321 if (--ret == 0 && !input_available())
6322 {
6323 /* Try again */
6324 finished = FALSE;
6325 }
6326 }
6327# endif
6328# ifdef FEAT_MOUSE_GPM
6329 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6330 {
6331 if (FD_ISSET(gpm_fd, &efds))
6332 gpm_close();
6333 else if (FD_ISSET(gpm_fd, &rfds))
6334 *check_for_gpm = 1;
6335 }
6336# endif
6337# ifdef USE_XSMP
6338 if (ret > 0 && xsmp_icefd != -1)
6339 {
6340 if (FD_ISSET(xsmp_icefd, &efds))
6341 {
6342 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006343 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 xsmp_close();
6345 if (--ret == 0)
6346 finished = FALSE; /* keep going if event was only one */
6347 }
6348 else if (FD_ISSET(xsmp_icefd, &rfds))
6349 {
6350 busy = TRUE;
6351 xsmp_handle_requests();
6352 busy = FALSE;
6353 if (--ret == 0)
6354 finished = FALSE; /* keep going if event was only one */
6355 }
6356 }
6357# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006358#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006359 /* also call when ret == 0, we may be polling a keep-open channel */
6360 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006361 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
6364#endif /* HAVE_SELECT */
6365
6366#ifdef MAY_LOOP
6367 if (finished || msec == 0)
6368 break;
6369
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006370# ifdef FEAT_CLIENTSERVER
6371 if (server_waiting())
6372 break;
6373# endif
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 /* We're going to loop around again, find out for how long */
6376 if (msec > 0)
6377 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006378# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006380 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381# else
6382 /* Guess we got interrupted halfway. */
6383 msec = msec / 2;
6384# endif
6385 if (msec <= 0)
6386 break; /* waited long enough */
6387 }
6388#endif
6389 }
6390
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006391 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392}
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006396 * Expand a path into all matching files and/or directories. Handles "*",
6397 * "?", "[a-z]", "**", etc.
6398 * "path" has backslashes before chars that are not to be expanded.
6399 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 */
6401 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006402mch_expandpath(
6403 garray_T *gap,
6404 char_u *path,
6405 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
Bram Moolenaar02743632005-07-25 20:42:36 +00006407 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408}
6409#endif
6410
6411/*
6412 * mch_expand_wildcards() - this code does wild-card pattern matching using
6413 * the shell
6414 *
6415 * return OK for success, FAIL for error (you may lose some memory) and put
6416 * an error message in *file.
6417 *
6418 * num_pat is number of input patterns
6419 * pat is array of pointers to input patterns
6420 * num_file is pointer to number of matched file names
6421 * file is pointer to array of pointers to matched file names
6422 */
6423
6424#ifndef SEEK_SET
6425# define SEEK_SET 0
6426#endif
6427#ifndef SEEK_END
6428# define SEEK_END 2
6429#endif
6430
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006431#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006434mch_expand_wildcards(
6435 int num_pat,
6436 char_u **pat,
6437 int *num_file,
6438 char_u ***file,
6439 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 int i;
6442 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006443 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 char_u *p;
6445 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
Bram Moolenaarc7247912008-01-13 12:54:11 +00006447 /*
6448 * This is the non-OS/2 implementation (really Unix).
6449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 int j;
6451 char_u *tempname;
6452 char_u *command;
6453 FILE *fd;
6454 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006455#define STYLE_ECHO 0 /* use "echo", the default */
6456#define STYLE_GLOB 1 /* use "glob", for csh */
6457#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6458#define STYLE_PRINT 3 /* use "print -N", for zsh */
6459#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6460 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 int shell_style = STYLE_ECHO;
6462 int check_spaces;
6463 static int did_find_nul = FALSE;
6464 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006465 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006466 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468 *num_file = 0; /* default: no files found */
6469 *file = NULL;
6470
6471 /*
6472 * If there are no wildcards, just copy the names to allocated memory.
6473 * Saves a lot of time, because we don't have to start a new shell.
6474 */
6475 if (!have_wildcard(num_pat, pat))
6476 return save_patterns(num_pat, pat, num_file, file);
6477
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006478# ifdef HAVE_SANDBOX
6479 /* Don't allow any shell command in the sandbox. */
6480 if (sandbox != 0 && check_secure())
6481 return FAIL;
6482# endif
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 /*
6485 * Don't allow the use of backticks in secure and restricted mode.
6486 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006487 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 for (i = 0; i < num_pat; ++i)
6489 if (vim_strchr(pat[i], '`') != NULL
6490 && (check_restricted() || check_secure()))
6491 return FAIL;
6492
6493 /*
6494 * get a name for the temp file
6495 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006496 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 {
6498 EMSG(_(e_notmp));
6499 return FAIL;
6500 }
6501
6502 /*
6503 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006504 * file.
6505 * STYLE_BT: NL separated
6506 * If expanding `cmd` execute it directly.
6507 * STYLE_GLOB: NUL separated
6508 * If we use *csh, "glob" will work better than "echo".
6509 * STYLE_PRINT: NL or NUL separated
6510 * If we use *zsh, "print -N" will work better than "glob".
6511 * STYLE_VIMGLOB: NL separated
6512 * If we use *sh*, we define "vimglob()".
6513 * STYLE_ECHO: space separated.
6514 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
6516 if (num_pat == 1 && *pat[0] == '`'
6517 && (len = STRLEN(pat[0])) > 2
6518 && *(pat[0] + len - 1) == '`')
6519 shell_style = STYLE_BT;
6520 else if ((len = STRLEN(p_sh)) >= 3)
6521 {
6522 if (STRCMP(p_sh + len - 3, "csh") == 0)
6523 shell_style = STYLE_GLOB;
6524 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6525 shell_style = STYLE_PRINT;
6526 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006527 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6528 "sh") != NULL)
6529 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaarc7247912008-01-13 12:54:11 +00006531 /* Compute the length of the command. We need 2 extra bytes: for the
6532 * optional '&' and for the NUL.
6533 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006535 if (shell_style == STYLE_VIMGLOB)
6536 len += STRLEN(sh_vimglob_func);
6537
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006538 for (i = 0; i < num_pat; ++i)
6539 {
6540 /* Count the length of the patterns in the same way as they are put in
6541 * "command" below. */
6542#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006544#else
6545 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006546 for (j = 0; pat[i][j] != NUL; ++j)
6547 {
6548 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6549 ++len; /* may add a backslash */
6550 ++len;
6551 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006552#endif
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 command = alloc(len);
6555 if (command == NULL)
6556 {
6557 /* out of memory */
6558 vim_free(tempname);
6559 return FAIL;
6560 }
6561
6562 /*
6563 * Build the shell command:
6564 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6565 * recognizes this).
6566 * - Add the shell command to print the expanded names.
6567 * - Add the temp file name.
6568 * - Add the file name patterns.
6569 */
6570 if (shell_style == STYLE_BT)
6571 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006572 /* change `command; command& ` to (command; command ) */
6573 STRCPY(command, "(");
6574 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006576 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006577 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 --p;
6579 if (*p == '&') /* remove trailing '&' */
6580 {
6581 ampersent = TRUE;
6582 *p = ' ';
6583 }
6584 STRCAT(command, ">");
6585 }
6586 else
6587 {
6588 if (flags & EW_NOTFOUND)
6589 STRCPY(command, "set nonomatch; ");
6590 else
6591 STRCPY(command, "unset nonomatch; ");
6592 if (shell_style == STYLE_GLOB)
6593 STRCAT(command, "glob >");
6594 else if (shell_style == STYLE_PRINT)
6595 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006596 else if (shell_style == STYLE_VIMGLOB)
6597 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 else
6599 STRCAT(command, "echo >");
6600 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006601
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 if (shell_style != STYLE_BT)
6605 for (i = 0; i < num_pat; ++i)
6606 {
6607 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006608 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006609 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610#ifdef USE_SYSTEM
6611 STRCAT(command, " \"");
6612 STRCAT(command, pat[i]);
6613 STRCAT(command, "\"");
6614#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006615 int intick = FALSE;
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 p = command + STRLEN(command);
6618 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006619 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006620 {
6621 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006622 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006623 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6624 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006625 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006626 * backslash inside backticks, before a special character
6627 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006628 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006629 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6630 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006631 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006632 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006633 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006634 else if (!intick
6635 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6636 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006637 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006638 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6639 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006640 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006641
6642 /* Copy one character. */
6643 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 *p = NUL;
6646#endif
6647 }
6648 if (flags & EW_SILENT)
6649 show_shell_mess = FALSE;
6650 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006651 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 /*
6654 * Using zsh -G: If a pattern has no matches, it is just deleted from
6655 * the argument list, otherwise zsh gives an error message and doesn't
6656 * expand any other pattern.
6657 */
6658 if (shell_style == STYLE_PRINT)
6659 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6660
6661 /*
6662 * If we use -f then shell variables set in .cshrc won't get expanded.
6663 * vi can do it, so we will too, but it is only necessary if there is a "$"
6664 * in one of the patterns, otherwise we can still use the fast option.
6665 */
6666 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6667 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6668
6669 /*
6670 * execute the shell command
6671 */
6672 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6673
6674 /* When running in the background, give it some time to create the temp
6675 * file, but don't wait for it to finish. */
6676 if (ampersent)
6677 mch_delay(10L, TRUE);
6678
6679 extra_shell_arg = NULL; /* cleanup */
6680 show_shell_mess = TRUE;
6681 vim_free(command);
6682
Bram Moolenaarc7247912008-01-13 12:54:11 +00006683 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 mch_remove(tempname);
6686 vim_free(tempname);
6687 /*
6688 * With interactive completion, the error message is not printed.
6689 * However with USE_SYSTEM, I don't know how to turn off error messages
6690 * from the shell, so screen may still get messed up -- webb.
6691 */
6692#ifndef USE_SYSTEM
6693 if (!(flags & EW_SILENT))
6694#endif
6695 {
6696 redraw_later_clear(); /* probably messed up screen */
6697 msg_putchar('\n'); /* clear bottom line quickly */
6698 cmdline_row = Rows - 1; /* continue on last line */
6699#ifdef USE_SYSTEM
6700 if (!(flags & EW_SILENT))
6701#endif
6702 {
6703 MSG(_(e_wildexpand));
6704 msg_start(); /* don't overwrite this message */
6705 }
6706 }
6707 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6708 * EW_NOTFOUND is given */
6709 if (shell_style == STYLE_BT)
6710 return FAIL;
6711 goto notfound;
6712 }
6713
6714 /*
6715 * read the names from the file into memory
6716 */
6717 fd = fopen((char *)tempname, READBIN);
6718 if (fd == NULL)
6719 {
6720 /* Something went wrong, perhaps a file name with a special char. */
6721 if (!(flags & EW_SILENT))
6722 {
6723 MSG(_(e_wildexpand));
6724 msg_start(); /* don't overwrite this message */
6725 }
6726 vim_free(tempname);
6727 goto notfound;
6728 }
6729 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006730 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006732 if (llen < 0)
6733 /* just in case ftell() would fail */
6734 buffer = NULL;
6735 else
6736 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (buffer == NULL)
6738 {
6739 /* out of memory */
6740 mch_remove(tempname);
6741 vim_free(tempname);
6742 fclose(fd);
6743 return FAIL;
6744 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006745 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 i = fread((char *)buffer, 1, len, fd);
6747 fclose(fd);
6748 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006749 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 /* unexpected read error */
6752 EMSG2(_(e_notread), tempname);
6753 vim_free(tempname);
6754 vim_free(buffer);
6755 return FAIL;
6756 }
6757 vim_free(tempname);
6758
Bram Moolenaarc7247912008-01-13 12:54:11 +00006759# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6761 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006762 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6764 *p++ = buffer[i];
6765 len = p - buffer;
6766# endif
6767
6768
6769 /* file names are separated with Space */
6770 if (shell_style == STYLE_ECHO)
6771 {
6772 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6773 p = buffer;
6774 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6775 {
6776 while (*p != ' ' && *p != '\n')
6777 ++p;
6778 p = skipwhite(p); /* skip to next entry */
6779 }
6780 }
6781 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006782 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {
6784 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6785 p = buffer;
6786 for (i = 0; *p != NUL; ++i) /* count number of entries */
6787 {
6788 while (*p != '\n' && *p != NUL)
6789 ++p;
6790 if (*p != NUL)
6791 ++p;
6792 p = skipwhite(p); /* skip leading white space */
6793 }
6794 }
6795 /* file names are separated with NUL */
6796 else
6797 {
6798 /*
6799 * Some versions of zsh use spaces instead of NULs to separate
6800 * results. Only do this when there is no NUL before the end of the
6801 * buffer, otherwise we would never be able to use file names with
6802 * embedded spaces when zsh does use NULs.
6803 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6804 * don't check for spaces again.
6805 */
6806 check_spaces = FALSE;
6807 if (shell_style == STYLE_PRINT && !did_find_nul)
6808 {
6809 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006810 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006811 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 did_find_nul = TRUE;
6813 else
6814 check_spaces = TRUE;
6815 }
6816
6817 /*
6818 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6819 * already is one, for STYLE_GLOB it needs to be added.
6820 */
6821 if (len && buffer[len - 1] == NUL)
6822 --len;
6823 else
6824 buffer[len] = NUL;
6825 i = 0;
6826 for (p = buffer; p < buffer + len; ++p)
6827 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6828 {
6829 ++i;
6830 *p = NUL;
6831 }
6832 if (len)
6833 ++i; /* count last entry */
6834 }
6835 if (i == 0)
6836 {
6837 /*
6838 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6839 * /bin/sh will happily expand it to nothing rather than returning an
6840 * error; and hey, it's good to check anyway -- webb.
6841 */
6842 vim_free(buffer);
6843 goto notfound;
6844 }
6845 *num_file = i;
6846 *file = (char_u **)alloc(sizeof(char_u *) * i);
6847 if (*file == NULL)
6848 {
6849 /* out of memory */
6850 vim_free(buffer);
6851 return FAIL;
6852 }
6853
6854 /*
6855 * Isolate the individual file names.
6856 */
6857 p = buffer;
6858 for (i = 0; i < *num_file; ++i)
6859 {
6860 (*file)[i] = p;
6861 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006862 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6863 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006865 while (!(shell_style == STYLE_ECHO && *p == ' ')
6866 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 ++p;
6868 if (p == buffer + len) /* last entry */
6869 *p = NUL;
6870 else
6871 {
6872 *p++ = NUL;
6873 p = skipwhite(p); /* skip to next entry */
6874 }
6875 }
6876 else /* NUL separates */
6877 {
6878 while (*p && p < buffer + len) /* skip entry */
6879 ++p;
6880 ++p; /* skip NUL */
6881 }
6882 }
6883
6884 /*
6885 * Move the file names to allocated memory.
6886 */
6887 for (j = 0, i = 0; i < *num_file; ++i)
6888 {
6889 /* Require the files to exist. Helps when using /bin/sh */
6890 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6891 continue;
6892
6893 /* check if this entry should be included */
6894 dir = (mch_isdir((*file)[i]));
6895 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6896 continue;
6897
Bram Moolenaara2031822006-03-07 22:29:51 +00006898 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006899 if (!dir && (flags & EW_EXEC)
6900 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006901 continue;
6902
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6904 if (p)
6905 {
6906 STRCPY(p, (*file)[i]);
6907 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006908 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 (*file)[j++] = p;
6910 }
6911 }
6912 vim_free(buffer);
6913 *num_file = j;
6914
6915 if (*num_file == 0) /* rejected all entries */
6916 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006917 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 goto notfound;
6919 }
6920
6921 return OK;
6922
6923notfound:
6924 if (flags & EW_NOTFOUND)
6925 return save_patterns(num_pat, pat, num_file, file);
6926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927}
6928
6929#endif /* VMS */
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006932save_patterns(
6933 int num_pat,
6934 char_u **pat,
6935 int *num_file,
6936 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006939 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6942 if (*file == NULL)
6943 return FAIL;
6944 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006945 {
6946 s = vim_strsave(pat[i]);
6947 if (s != NULL)
6948 /* Be compatible with expand_filename(): halve the number of
6949 * backslashes. */
6950 backslash_halve(s);
6951 (*file)[i] = s;
6952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 *num_file = num_pat;
6954 return OK;
6955}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957/*
6958 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6959 * expand.
6960 */
6961 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006962mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006964 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (*p == '\\' && p[1] != NUL)
6967 ++p;
6968 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 if (vim_strchr((char_u *)
6970#ifdef VMS
6971 "*?%"
6972#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974#endif
6975 , *p) != NULL)
6976 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 return FALSE;
6979}
6980
6981/*
6982 * Return TRUE if the string "p" contains a wildcard.
6983 * Don't recognize '~' at the end as a wildcard.
6984 */
6985 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006986mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006988 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (*p == '\\' && p[1] != NUL)
6991 ++p;
6992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (vim_strchr((char_u *)
6994#ifdef VMS
6995 "*?%$"
6996#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
6999 , *p) != NULL
7000 || (*p == '~' && p[1] != NUL))
7001 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003 return FALSE;
7004}
7005
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007007have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 int i;
7010
7011 for (i = 0; i < num; i++)
7012 if (mch_has_wildcard(file[i]))
7013 return 1;
7014 return 0;
7015}
7016
7017 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007018have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 int i;
7021
7022 for (i = 0; i < num; i++)
7023 if (vim_strchr(file[i], '$') != NULL)
7024 return TRUE;
7025 return FALSE;
7026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007028#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029/*
7030 * Scaled-down version of rename(), which is missing in Xenix.
7031 * This version can only move regular files and will fail if the
7032 * destination exists.
7033 */
7034 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007035mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 struct stat st;
7038
7039 if (stat(dest, &st) >= 0) /* fail if destination exists */
7040 return -1;
7041 if (link(src, dest) != 0) /* link file to new name */
7042 return -1;
7043 if (mch_remove(src) == 0) /* delete link to old name */
7044 return 0;
7045 return -1;
7046}
7047#endif /* !HAVE_RENAME */
7048
7049#ifdef FEAT_MOUSE_GPM
7050/*
7051 * Initializes connection with gpm (if it isn't already opened)
7052 * Return 1 if succeeded (or connection already opened), 0 if failed
7053 */
7054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007055gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7058
7059 if (!gpm_flag)
7060 {
7061 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7062 gpm_connect.defaultMask = ~GPM_HARD;
7063 /* Default handling for mouse move*/
7064 gpm_connect.minMod = 0; /* Handle any modifier keys */
7065 gpm_connect.maxMod = 0xffff;
7066 if (Gpm_Open(&gpm_connect, 0) > 0)
7067 {
7068 /* gpm library tries to handling TSTP causes
7069 * problems. Anyways, we close connection to Gpm whenever
7070 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007071 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007073# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 return 1; /* succeed */
7077 }
7078 if (gpm_fd == -2)
7079 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7080 return 0;
7081 }
7082 return 1; /* already open */
7083}
7084
7085/*
7086 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 */
7088 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007089gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 if (gpm_flag && gpm_fd >= 0) /* if Open */
7092 Gpm_Close();
7093}
7094
7095/* Reads gpm event and adds special keys to input buf. Returns length of
7096 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007097 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 */
7099 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007100mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
7102 int button;
7103 static Gpm_Event gpm_event;
7104 char_u string[6];
7105 int_u vim_modifiers;
7106 int row,col;
7107 unsigned char buttons_mask;
7108 unsigned char gpm_modifiers;
7109 static unsigned char old_buttons = 0;
7110
7111 Gpm_GetEvent(&gpm_event);
7112
7113#ifdef FEAT_GUI
7114 /* Don't put events in the input queue now. */
7115 if (hold_gui_events)
7116 return 0;
7117#endif
7118
7119 row = gpm_event.y - 1;
7120 col = gpm_event.x - 1;
7121
7122 string[0] = ESC; /* Our termcode */
7123 string[1] = 'M';
7124 string[2] = 'G';
7125 switch (GPM_BARE_EVENTS(gpm_event.type))
7126 {
7127 case GPM_DRAG:
7128 string[3] = MOUSE_DRAG;
7129 break;
7130 case GPM_DOWN:
7131 buttons_mask = gpm_event.buttons & ~old_buttons;
7132 old_buttons = gpm_event.buttons;
7133 switch (buttons_mask)
7134 {
7135 case GPM_B_LEFT:
7136 button = MOUSE_LEFT;
7137 break;
7138 case GPM_B_MIDDLE:
7139 button = MOUSE_MIDDLE;
7140 break;
7141 case GPM_B_RIGHT:
7142 button = MOUSE_RIGHT;
7143 break;
7144 default:
7145 return 0;
7146 /*Don't know what to do. Can more than one button be
7147 * reported in one event? */
7148 }
7149 string[3] = (char_u)(button | 0x20);
7150 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7151 break;
7152 case GPM_UP:
7153 string[3] = MOUSE_RELEASE;
7154 old_buttons &= ~gpm_event.buttons;
7155 break;
7156 default:
7157 return 0;
7158 }
7159 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7160 gpm_modifiers = gpm_event.modifiers;
7161 vim_modifiers = 0x0;
7162 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7163 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7164 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7165 */
7166 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7167 vim_modifiers |= MOUSE_SHIFT;
7168
7169 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7170 vim_modifiers |= MOUSE_CTRL;
7171 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7172 vim_modifiers |= MOUSE_ALT;
7173 string[3] |= vim_modifiers;
7174 string[4] = (char_u)(col + ' ' + 1);
7175 string[5] = (char_u)(row + ' ' + 1);
7176 add_to_input_buf(string, 6);
7177 return 6;
7178}
7179#endif /* FEAT_MOUSE_GPM */
7180
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007181#ifdef FEAT_SYSMOUSE
7182/*
7183 * Initialize connection with sysmouse.
7184 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7185 * output, any sysmouse output than will be processed via sig_sysmouse().
7186 * Return OK if succeeded, FAIL if failed.
7187 */
7188 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007189sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007190{
7191 struct mouse_info mouse;
7192
7193 mouse.operation = MOUSE_MODE;
7194 mouse.u.mode.mode = 0;
7195 mouse.u.mode.signal = SIGUSR2;
7196 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7197 {
7198 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7199 mouse.operation = MOUSE_SHOW;
7200 ioctl(1, CONS_MOUSECTL, &mouse);
7201 return OK;
7202 }
7203 return FAIL;
7204}
7205
7206/*
7207 * Stop processing SIGUSR2 signals, and also make sure that
7208 * virtual console do not send us any sysmouse related signal.
7209 */
7210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007211sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007212{
7213 struct mouse_info mouse;
7214
7215 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7216 mouse.operation = MOUSE_MODE;
7217 mouse.u.mode.mode = 0;
7218 mouse.u.mode.signal = 0;
7219 ioctl(1, CONS_MOUSECTL, &mouse);
7220}
7221
7222/*
7223 * Gets info from sysmouse and adds special keys to input buf.
7224 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007225 static RETSIGTYPE
7226sig_sysmouse SIGDEFARG(sigarg)
7227{
7228 struct mouse_info mouse;
7229 struct video_info video;
7230 char_u string[6];
7231 int row, col;
7232 int button;
7233 int buttons;
7234 static int oldbuttons = 0;
7235
7236#ifdef FEAT_GUI
7237 /* Don't put events in the input queue now. */
7238 if (hold_gui_events)
7239 return;
7240#endif
7241
7242 mouse.operation = MOUSE_GETINFO;
7243 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7244 && ioctl(1, FBIO_MODEINFO, &video) != -1
7245 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7246 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7247 {
7248 row = mouse.u.data.y / video.vi_cheight;
7249 col = mouse.u.data.x / video.vi_cwidth;
7250 buttons = mouse.u.data.buttons;
7251 string[0] = ESC; /* Our termcode */
7252 string[1] = 'M';
7253 string[2] = 'S';
7254 if (oldbuttons == buttons && buttons != 0)
7255 {
7256 button = MOUSE_DRAG;
7257 }
7258 else
7259 {
7260 switch (buttons)
7261 {
7262 case 0:
7263 button = MOUSE_RELEASE;
7264 break;
7265 case 1:
7266 button = MOUSE_LEFT;
7267 break;
7268 case 2:
7269 button = MOUSE_MIDDLE;
7270 break;
7271 case 4:
7272 button = MOUSE_RIGHT;
7273 break;
7274 default:
7275 return;
7276 }
7277 oldbuttons = buttons;
7278 }
7279 string[3] = (char_u)(button);
7280 string[4] = (char_u)(col + ' ' + 1);
7281 string[5] = (char_u)(row + ' ' + 1);
7282 add_to_input_buf(string, 6);
7283 }
7284 return;
7285}
7286#endif /* FEAT_SYSMOUSE */
7287
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007289typedef char_u * (*STRPROCSTR)(char_u *);
7290typedef char_u * (*INTPROCSTR)(int);
7291typedef int (*STRPROCINT)(char_u *);
7292typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294/*
7295 * Call a DLL routine which takes either a string or int param
7296 * and returns an allocated string.
7297 */
7298 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007299mch_libcall(
7300 char_u *libname,
7301 char_u *funcname,
7302 char_u *argstring, /* NULL when using a argint */
7303 int argint,
7304 char_u **string_result,/* NULL when using number_result */
7305 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
7307# if defined(USE_DLOPEN)
7308 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007309 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310# else
7311 shl_t hinstLib;
7312# endif
7313 STRPROCSTR ProcAdd;
7314 INTPROCSTR ProcAddI;
7315 char_u *retval_str = NULL;
7316 int retval_int = 0;
7317 int success = FALSE;
7318
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007319 /*
7320 * Get a handle to the DLL module.
7321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007323 /* First clear any error, it's not cleared by the dlopen() call. */
7324 (void)dlerror();
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 hinstLib = dlopen((char *)libname, RTLD_LAZY
7327# ifdef RTLD_LOCAL
7328 | RTLD_LOCAL
7329# endif
7330 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007331 if (hinstLib == NULL)
7332 {
7333 /* "dlerr" must be used before dlclose() */
7334 dlerr = (char *)dlerror();
7335 if (dlerr != NULL)
7336 EMSG2(_("dlerror = \"%s\""), dlerr);
7337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338# else
7339 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7340# endif
7341
7342 /* If the handle is valid, try to get the function address. */
7343 if (hinstLib != NULL)
7344 {
7345# ifdef HAVE_SETJMP_H
7346 /*
7347 * Catch a crash when calling the library function. For example when
7348 * using a number where a string pointer is expected.
7349 */
7350 mch_startjmp();
7351 if (SETJMP(lc_jump_env) != 0)
7352 {
7353 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007354# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007355 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 mch_didjmp();
7358 }
7359 else
7360# endif
7361 {
7362 retval_str = NULL;
7363 retval_int = 0;
7364
7365 if (argstring != NULL)
7366 {
7367# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007368 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007369 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370# else
7371 if (shl_findsym(&hinstLib, (const char *)funcname,
7372 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7373 ProcAdd = NULL;
7374# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007375 if ((success = (ProcAdd != NULL
7376# if defined(USE_DLOPEN)
7377 && dlerr == NULL
7378# endif
7379 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 if (string_result == NULL)
7382 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7383 else
7384 retval_str = (ProcAdd)(argstring);
7385 }
7386 }
7387 else
7388 {
7389# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007390 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007391 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392# else
7393 if (shl_findsym(&hinstLib, (const char *)funcname,
7394 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7395 ProcAddI = NULL;
7396# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007397 if ((success = (ProcAddI != NULL
7398# if defined(USE_DLOPEN)
7399 && dlerr == NULL
7400# endif
7401 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
7403 if (string_result == NULL)
7404 retval_int = ((INTPROCINT)ProcAddI)(argint);
7405 else
7406 retval_str = (ProcAddI)(argint);
7407 }
7408 }
7409
7410 /* Save the string before we free the library. */
7411 /* Assume that a "1" or "-1" result is an illegal pointer. */
7412 if (string_result == NULL)
7413 *number_result = retval_int;
7414 else if (retval_str != NULL
7415 && retval_str != (char_u *)1
7416 && retval_str != (char_u *)-1)
7417 *string_result = vim_strsave(retval_str);
7418 }
7419
7420# ifdef HAVE_SETJMP_H
7421 mch_endjmp();
7422# ifdef SIGHASARG
7423 if (lc_signal != 0)
7424 {
7425 int i;
7426
7427 /* try to find the name of this signal */
7428 for (i = 0; signal_info[i].sig != -1; i++)
7429 if (lc_signal == signal_info[i].sig)
7430 break;
7431 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7432 }
7433# endif
7434# endif
7435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007437 /* "dlerr" must be used before dlclose() */
7438 if (dlerr != NULL)
7439 EMSG2(_("dlerror = \"%s\""), dlerr);
7440
7441 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 (void)dlclose(hinstLib);
7443# else
7444 (void)shl_unload(hinstLib);
7445# endif
7446 }
7447
7448 if (!success)
7449 {
7450 EMSG2(_(e_libcall), funcname);
7451 return FAIL;
7452 }
7453
7454 return OK;
7455}
7456#endif
7457
7458#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7459static int xterm_trace = -1; /* default: disabled */
7460static int xterm_button;
7461
7462/*
7463 * Setup a dummy window for X selections in a terminal.
7464 */
7465 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007466setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 int z = 0;
7469 char *strp = "";
7470 Widget AppShell;
7471
7472 if (!x_connect_to_server())
7473 return;
7474
7475 open_app_context();
7476 if (app_context != NULL && xterm_Shell == (Widget)0)
7477 {
7478 int (*oldhandler)();
7479#if defined(HAVE_SETJMP_H)
7480 int (*oldIOhandler)();
7481#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007482# ifdef ELAPSED_FUNC
7483 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484
7485 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007486 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487# endif
7488
7489 /* Ignore X errors while opening the display */
7490 oldhandler = XSetErrorHandler(x_error_check);
7491
7492#if defined(HAVE_SETJMP_H)
7493 /* Ignore X IO errors while opening the display */
7494 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7495 mch_startjmp();
7496 if (SETJMP(lc_jump_env) != 0)
7497 {
7498 mch_didjmp();
7499 xterm_dpy = NULL;
7500 }
7501 else
7502#endif
7503 {
7504 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7505 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7506#if defined(HAVE_SETJMP_H)
7507 mch_endjmp();
7508#endif
7509 }
7510
7511#if defined(HAVE_SETJMP_H)
7512 /* Now handle X IO errors normally. */
7513 (void)XSetIOErrorHandler(oldIOhandler);
7514#endif
7515 /* Now handle X errors normally. */
7516 (void)XSetErrorHandler(oldhandler);
7517
7518 if (xterm_dpy == NULL)
7519 {
7520 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007521 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 return;
7523 }
7524
7525 /* Catch terminating error of the X server connection. */
7526 (void)XSetIOErrorHandler(x_IOerror_handler);
7527
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007528# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007530 {
7531 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007532 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007533 verbose_leave();
7534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535# endif
7536
7537 /* Create a Shell to make converters work. */
7538 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7539 applicationShellWidgetClass, xterm_dpy,
7540 NULL);
7541 if (AppShell == (Widget)0)
7542 return;
7543 xterm_Shell = XtVaCreatePopupShell("VIM",
7544 topLevelShellWidgetClass, AppShell,
7545 XtNmappedWhenManaged, 0,
7546 XtNwidth, 1,
7547 XtNheight, 1,
7548 NULL);
7549 if (xterm_Shell == (Widget)0)
7550 return;
7551
7552 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007553 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 if (x11_display == NULL)
7555 x11_display = xterm_dpy;
7556
7557 XtRealizeWidget(xterm_Shell);
7558 XSync(xterm_dpy, False);
7559 xterm_update();
7560 }
7561 if (xterm_Shell != (Widget)0)
7562 {
7563 clip_init(TRUE);
7564 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7565 x11_window = (Window)atol(strp);
7566 /* Check if $WINDOWID is valid. */
7567 if (test_x11_window(xterm_dpy) == FAIL)
7568 x11_window = 0;
7569 if (x11_window != 0)
7570 xterm_trace = 0;
7571 }
7572}
7573
7574 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007575start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7578 return;
7579 xterm_trace = 1;
7580 xterm_button = button;
7581 do_xterm_trace();
7582}
7583
7584
7585 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007586stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
7588 if (xterm_trace < 0)
7589 return;
7590 xterm_trace = 0;
7591}
7592
7593/*
7594 * Query the xterm pointer and generate mouse termcodes if necessary
7595 * return TRUE if dragging is active, else FALSE
7596 */
7597 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007598do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
7600 Window root, child;
7601 int root_x, root_y;
7602 int win_x, win_y;
7603 int row, col;
7604 int_u mask_return;
7605 char_u buf[50];
7606 char_u *strp;
7607 long got_hints;
7608 static char_u *mouse_code;
7609 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7610 static int prev_row = 0, prev_col = 0;
7611 static XSizeHints xterm_hints;
7612
7613 if (xterm_trace <= 0)
7614 return FALSE;
7615
7616 if (xterm_trace == 1)
7617 {
7618 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007619 * have changed recently. */
7620 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7621 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 || xterm_hints.width_inc <= 1
7623 || xterm_hints.height_inc <= 1)
7624 {
7625 xterm_trace = -1; /* Not enough data -- disable tracing */
7626 return FALSE;
7627 }
7628
7629 /* Rely on the same mouse code for the duration of this */
7630 mouse_code = find_termcode(mouse_name);
7631 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007632 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 xterm_trace = 2;
7634
7635 /* Find the offset of the chars, there might be a scrollbar on the
7636 * left of the window and/or a menu on the top (eterm etc.) */
7637 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7638 &win_x, &win_y, &mask_return);
7639 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7640 - (xterm_hints.height_inc / 2);
7641 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7642 xterm_hints.y = 2;
7643 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7644 - (xterm_hints.width_inc / 2);
7645 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7646 xterm_hints.x = 2;
7647 return TRUE;
7648 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007649 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {
7651 xterm_trace = 0;
7652 return FALSE;
7653 }
7654
7655 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7656 &win_x, &win_y, &mask_return);
7657
7658 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7659 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7660 if (row == prev_row && col == prev_col)
7661 return TRUE;
7662
7663 STRCPY(buf, mouse_code);
7664 strp = buf + STRLEN(buf);
7665 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7666 *strp++ = (char_u)(col + ' ' + 1);
7667 *strp++ = (char_u)(row + ' ' + 1);
7668 *strp = 0;
7669 add_to_input_buf(buf, STRLEN(buf));
7670
7671 prev_row = row;
7672 prev_col = col;
7673 return TRUE;
7674}
7675
7676# if defined(FEAT_GUI) || defined(PROTO)
7677/*
7678 * Destroy the display, window and app_context. Required for GTK.
7679 */
7680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007681clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 if (xterm_Shell != (Widget)0)
7684 {
7685 XtDestroyWidget(xterm_Shell);
7686 xterm_Shell = (Widget)0;
7687 }
7688 if (xterm_dpy != NULL)
7689 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007690# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 /* Lesstif and Solaris crash here, lose some memory */
7692 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (x11_display == xterm_dpy)
7695 x11_display = NULL;
7696 xterm_dpy = NULL;
7697 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007698# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (app_context != (XtAppContext)NULL)
7700 {
7701 /* Lesstif and Solaris crash here, lose some memory */
7702 XtDestroyApplicationContext(app_context);
7703 app_context = (XtAppContext)NULL;
7704 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707# endif
7708
7709/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007710 * Catch up with GUI or X events.
7711 */
7712 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007713clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007714{
7715# ifdef FEAT_GUI
7716 if (gui.in_use)
7717 gui_mch_update();
7718 else
7719# endif
7720 if (xterm_Shell != (Widget)0)
7721 xterm_update();
7722}
7723
7724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 * Catch up with any queued X events. This may put keyboard input into the
7726 * input buffer, call resize call-backs, trigger timers etc. If there is
7727 * nothing in the X event queue (& no timers pending), then we return
7728 * immediately.
7729 */
7730 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007731xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 XEvent event;
7734
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007735 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007737 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007739 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007740 break;
7741
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007742 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007743 {
7744 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007745 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007746#ifdef FEAT_CLIENTSERVER
7747 {
7748 XPropertyEvent *e = (XPropertyEvent *)&event;
7749
7750 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007752 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007755 XtDispatchEvent(&event);
7756 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007757 else
7758 {
7759 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007760 XtAppProcessEvent(app_context, mask);
7761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763}
7764
7765 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007766clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 if (xterm_Shell != (Widget)0)
7769 return clip_x11_own_selection(xterm_Shell, cbd);
7770 return FAIL;
7771}
7772
7773 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007774clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 if (xterm_Shell != (Widget)0)
7777 clip_x11_lose_selection(xterm_Shell, cbd);
7778}
7779
7780 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007781clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 if (xterm_Shell != (Widget)0)
7784 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7785}
7786
7787 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007788clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 clip_x11_set_selection(cbd);
7791}
7792#endif
7793
7794
7795#if defined(USE_XSMP) || defined(PROTO)
7796/*
7797 * Code for X Session Management Protocol.
7798 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007799static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7800static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7801static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7802static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7803static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805
7806# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007807static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
7809/*
7810 * This is our chance to ask the user if they want to save,
7811 * or abort the logout
7812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007814xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
7816 cmdmod_T save_cmdmod;
7817 int cancel_shutdown = False;
7818
7819 save_cmdmod = cmdmod;
7820 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007821 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 /* Mustn't logout */
7823 cancel_shutdown = True;
7824 cmdmod = save_cmdmod;
7825 setcursor(); /* position cursor */
7826 out_flush();
7827
7828 /* Done interaction */
7829 SmcInteractDone(smc_conn, cancel_shutdown);
7830
7831 /* Finish off
7832 * Only end save-yourself here if we're not cancelling shutdown;
7833 * we'll get a cancelled callback later in which we'll end it.
7834 * Hopefully get around glitchy SMs (like GNOME-1)
7835 */
7836 if (!cancel_shutdown)
7837 {
7838 xsmp.save_yourself = False;
7839 SmcSaveYourselfDone(smc_conn, True);
7840 }
7841}
7842# endif
7843
7844/*
7845 * Callback that starts save-yourself.
7846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007848xsmp_handle_save_yourself(
7849 SmcConn smc_conn,
7850 SmPointer client_data UNUSED,
7851 int save_type UNUSED,
7852 Bool shutdown,
7853 int interact_style UNUSED,
7854 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 /* Handle already being in saveyourself */
7857 if (xsmp.save_yourself)
7858 SmcSaveYourselfDone(smc_conn, True);
7859 xsmp.save_yourself = True;
7860 xsmp.shutdown = shutdown;
7861
7862 /* First up, preserve all files */
7863 out_flush();
7864 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7865
7866 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007867 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
7869# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7870 /* Now see if we can ask about unsaved files */
7871 if (shutdown && !fast && gui.in_use)
7872 /* Need to interact with user, but need SM's permission */
7873 SmcInteractRequest(smc_conn, SmDialogError,
7874 xsmp_handle_interaction, client_data);
7875 else
7876# endif
7877 {
7878 /* Can stop the cycle here */
7879 SmcSaveYourselfDone(smc_conn, True);
7880 xsmp.save_yourself = False;
7881 }
7882}
7883
7884
7885/*
7886 * Callback to warn us of imminent death.
7887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007889xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 xsmp_close();
7892
7893 /* quit quickly leaving swapfiles for modified buffers behind */
7894 getout_preserve_modified(0);
7895}
7896
7897
7898/*
7899 * Callback to tell us that save-yourself has completed.
7900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007902xsmp_save_complete(
7903 SmcConn smc_conn UNUSED,
7904 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 xsmp.save_yourself = False;
7907}
7908
7909
7910/*
7911 * Callback to tell us that an instigated shutdown was cancelled
7912 * (maybe even by us)
7913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007915xsmp_shutdown_cancelled(
7916 SmcConn smc_conn,
7917 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 if (xsmp.save_yourself)
7920 SmcSaveYourselfDone(smc_conn, True);
7921 xsmp.save_yourself = False;
7922 xsmp.shutdown = False;
7923}
7924
7925
7926/*
7927 * Callback to tell us that a new ICE connection has been established.
7928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007930xsmp_ice_connection(
7931 IceConn iceConn,
7932 IcePointer clientData UNUSED,
7933 Bool opening,
7934 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 /* Intercept creation of ICE connection fd */
7937 if (opening)
7938 {
7939 xsmp_icefd = IceConnectionNumber(iceConn);
7940 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7941 }
7942}
7943
7944
7945/* Handle any ICE processing that's required; return FAIL if SM lost */
7946 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007947xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 Bool rep;
7950
7951 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7952 == IceProcessMessagesIOError)
7953 {
7954 /* Lost ICE */
7955 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007956 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 xsmp_close();
7958 return FAIL;
7959 }
7960 else
7961 return OK;
7962}
7963
7964static int dummy;
7965
7966/* Set up X Session Management Protocol */
7967 void
7968xsmp_init(void)
7969{
7970 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 SmcCallbacks smcallbacks;
7972#if 0
7973 SmPropValue smname;
7974 SmProp smnameprop;
7975 SmProp *smprops[1];
7976#endif
7977
7978 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007979 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 xsmp.save_yourself = xsmp.shutdown = False;
7982
7983 /* Set up SM callbacks - must have all, even if they're not used */
7984 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7985 smcallbacks.save_yourself.client_data = NULL;
7986 smcallbacks.die.callback = xsmp_die;
7987 smcallbacks.die.client_data = NULL;
7988 smcallbacks.save_complete.callback = xsmp_save_complete;
7989 smcallbacks.save_complete.client_data = NULL;
7990 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
7991 smcallbacks.shutdown_cancelled.client_data = NULL;
7992
7993 /* Set up a watch on ICE connection creations. The "dummy" argument is
7994 * apparently required for FreeBSD (we get a BUS error when using NULL). */
7995 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
7996 {
7997 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007998 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 return;
8000 }
8001
8002 /* Create an SM connection */
8003 xsmp.smcconn = SmcOpenConnection(
8004 NULL,
8005 NULL,
8006 SmProtoMajor,
8007 SmProtoMinor,
8008 SmcSaveYourselfProcMask | SmcDieProcMask
8009 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8010 &smcallbacks,
8011 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008012 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 sizeof(errorstring),
8014 errorstring);
8015 if (xsmp.smcconn == NULL)
8016 {
8017 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008018
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008020 {
8021 vim_snprintf(errorreport, sizeof(errorreport),
8022 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8023 verb_msg((char_u *)errorreport);
8024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 return;
8026 }
8027 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8028
8029#if 0
8030 /* ID ourselves */
8031 smname.value = "vim";
8032 smname.length = 3;
8033 smnameprop.name = "SmProgram";
8034 smnameprop.type = "SmARRAY8";
8035 smnameprop.num_vals = 1;
8036 smnameprop.vals = &smname;
8037
8038 smprops[0] = &smnameprop;
8039 SmcSetProperties(xsmp.smcconn, 1, smprops);
8040#endif
8041}
8042
8043
8044/* Shut down XSMP comms. */
8045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008046xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 if (xsmp_icefd != -1)
8049 {
8050 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008051 if (xsmp.clientid != NULL)
8052 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008053 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 xsmp_icefd = -1;
8055 }
8056}
8057#endif /* USE_XSMP */
8058
8059
8060#ifdef EBCDIC
8061/* Translate character to its CTRL- value */
8062char CtrlTable[] =
8063{
8064/* 00 - 5E */
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, 0,
8068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8069 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8070 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8071/* ^ */ 0x1E,
8072/* - */ 0x1F,
8073/* 61 - 6C */
8074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8075/* _ */ 0x1F,
8076/* 6E - 80 */
8077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8078/* a */ 0x01,
8079/* b */ 0x02,
8080/* c */ 0x03,
8081/* d */ 0x37,
8082/* e */ 0x2D,
8083/* f */ 0x2E,
8084/* g */ 0x2F,
8085/* h */ 0x16,
8086/* i */ 0x05,
8087/* 8A - 90 */
8088 0, 0, 0, 0, 0, 0, 0,
8089/* j */ 0x15,
8090/* k */ 0x0B,
8091/* l */ 0x0C,
8092/* m */ 0x0D,
8093/* n */ 0x0E,
8094/* o */ 0x0F,
8095/* p */ 0x10,
8096/* q */ 0x11,
8097/* r */ 0x12,
8098/* 9A - A1 */
8099 0, 0, 0, 0, 0, 0, 0, 0,
8100/* s */ 0x13,
8101/* t */ 0x3C,
8102/* u */ 0x3D,
8103/* v */ 0x32,
8104/* w */ 0x26,
8105/* x */ 0x18,
8106/* y */ 0x19,
8107/* z */ 0x3F,
8108/* AA - AC */
8109 0, 0, 0,
8110/* [ */ 0x27,
8111/* AE - BC */
8112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8113/* ] */ 0x1D,
8114/* BE - C0 */ 0, 0, 0,
8115/* A */ 0x01,
8116/* B */ 0x02,
8117/* C */ 0x03,
8118/* D */ 0x37,
8119/* E */ 0x2D,
8120/* F */ 0x2E,
8121/* G */ 0x2F,
8122/* H */ 0x16,
8123/* I */ 0x05,
8124/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8125/* J */ 0x15,
8126/* K */ 0x0B,
8127/* L */ 0x0C,
8128/* M */ 0x0D,
8129/* N */ 0x0E,
8130/* O */ 0x0F,
8131/* P */ 0x10,
8132/* Q */ 0x11,
8133/* R */ 0x12,
8134/* DA - DF */ 0, 0, 0, 0, 0, 0,
8135/* \ */ 0x1C,
8136/* E1 */ 0,
8137/* S */ 0x13,
8138/* T */ 0x3C,
8139/* U */ 0x3D,
8140/* V */ 0x32,
8141/* W */ 0x26,
8142/* X */ 0x18,
8143/* Y */ 0x19,
8144/* Z */ 0x3F,
8145/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8147};
8148
8149char MetaCharTable[]=
8150{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8151 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8152 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8153 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8154 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8155};
8156
8157
8158/* TODO: Use characters NOT numbers!!! */
8159char CtrlCharTable[]=
8160{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8161 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8162 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8163 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8164 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8165};
8166
8167
8168#endif