blob: 80c61755b1819f28f28392c11a0d9dcd443f78cd [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 Moolenaar071d4272004-06-13 20:20:40 +00001231/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001232 * Invoked after receiving SIGCONT. We don't know what happened while
1233 * sleeping, deal with part of that.
1234 */
1235 static void
1236after_sigcont(void)
1237{
1238# ifdef FEAT_TITLE
1239 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1240 // again later.
1241 oldtitle_outdated = TRUE;
1242# endif
1243 settmode(TMODE_RAW);
1244 need_check_timestamps = TRUE;
1245 did_check_timestamps = FALSE;
1246}
1247
1248#if defined(SIGCONT)
1249static RETSIGTYPE sigcont_handler SIGPROTOARG;
1250static volatile int in_mch_suspend = FALSE;
1251
1252/*
1253 * With multi-threading, suspending might not work immediately. Catch the
1254 * SIGCONT signal, which will be used as an indication whether the suspending
1255 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001256 *
1257 * On Linux, signal is not always handled immediately either.
1258 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001259 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001260 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001261 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001263static volatile int sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001264static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266/*
1267 * signal handler for SIGCONT
1268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 static RETSIGTYPE
1270sigcont_handler SIGDEFARG(sigarg)
1271{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001272 if (in_mch_suspend)
1273 {
1274 sigcont_received = TRUE;
1275 }
1276 else
1277 {
1278 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1279 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1280 // back to a sane mode. We should redraw, but we can't really do that
1281 // in a signal handler, do a redraw later.
1282 after_sigcont();
1283 redraw_later(CLEAR);
1284 cursor_on_force();
1285 out_flush();
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 SIGRETURN;
1289}
1290#endif
1291
Bram Moolenaar62b42182010-09-21 22:09:37 +02001292# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001293static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001294# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001295static void save_clipboard(void);
1296static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001297
1298static void *clip_star_save = NULL;
1299static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001300# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001301
1302/*
1303 * Called when Vim is going to sleep or execute a shell command.
1304 * We can't respond to requests for the X selections. Lose them, otherwise
1305 * other applications will hang. But first copy the text to cut buffer 0.
1306 */
1307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001309{
1310 if (clip_star.owned || clip_plus.owned)
1311 {
1312 x11_export_final_selection();
1313 if (clip_star.owned)
1314 clip_lose_selection(&clip_star);
1315 if (clip_plus.owned)
1316 clip_lose_selection(&clip_plus);
1317 if (x11_display != NULL)
1318 XFlush(x11_display);
1319 }
1320}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001321
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001322# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001323/*
1324 * Save clipboard text to restore later.
1325 */
1326 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001327save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001328{
1329 if (clip_star.owned)
1330 clip_star_save = get_register('*', TRUE);
1331 if (clip_plus.owned)
1332 clip_plus_save = get_register('+', TRUE);
1333}
1334
1335/*
1336 * Restore clipboard text if no one own the X selection.
1337 */
1338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001339restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001340{
1341 if (clip_star_save != NULL)
1342 {
1343 if (!clip_gen_owner_exists(&clip_star))
1344 put_register('*', clip_star_save);
1345 else
1346 free_register(clip_star_save);
1347 clip_star_save = NULL;
1348 }
1349 if (clip_plus_save != NULL)
1350 {
1351 if (!clip_gen_owner_exists(&clip_plus))
1352 put_register('+', clip_plus_save);
1353 else
1354 free_register(clip_plus_save);
1355 clip_plus_save = NULL;
1356 }
1357}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001358# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001359#endif
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361/*
1362 * If the machine has job control, use it to suspend the program,
1363 * otherwise fake it by starting a new shell.
1364 */
1365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001366mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
1368 /* BeOS does have SIGTSTP, but it doesn't work. */
1369#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001370 in_mch_suspend = TRUE;
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 out_flush(); /* needed to make cursor visible on some systems */
1373 settmode(TMODE_COOK);
1374 out_flush(); /* needed to disable mouse on some systems */
1375
1376# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001377 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001379# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 sigcont_received = FALSE;
1381# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001384
1385# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001386 /*
1387 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388 * immediately, but somehow not all the time, probably because it's handled
1389 * in another thread. Do not call pause() because there would be race
1390 * condition which would hang Vim if signal happened in between the test of
1391 * sigcont_received and the call to pause(). If signal is not yet received,
1392 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1393 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001394 */
1395 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001396 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001397
Bram Moolenaar262735e2009-07-14 10:20:22 +00001398 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001399 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001402 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaar2e310482018-08-21 13:09:10 +02001404 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#else
1406 suspend_shell();
1407#endif
1408}
1409
1410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001411mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 Columns = 80;
1414 Rows = 24;
1415
1416 out_flush();
1417 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001418
Bram Moolenaar56718732006-03-15 22:53:57 +00001419#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001420 mac_conv_init();
1421#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001422#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1423 win_clip_init();
1424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425}
1426
1427 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001428set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430#if defined(SIGWINCH)
1431 /*
1432 * WINDOW CHANGE signal is handled with sig_winch().
1433 */
1434 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1435#endif
1436
1437 /*
1438 * We want the STOP signal to work, to make mch_suspend() work.
1439 * For "rvim" the STOP signal is ignored.
1440 */
1441#ifdef SIGTSTP
1442 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1443#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001444#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 signal(SIGCONT, sigcont_handler);
1446#endif
1447
1448 /*
1449 * We want to ignore breaking of PIPEs.
1450 */
1451#ifdef SIGPIPE
1452 signal(SIGPIPE, SIG_IGN);
1453#endif
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001456 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#endif
1458
1459 /*
1460 * Ignore alarm signals (Perl's alarm() generates it).
1461 */
1462#ifdef SIGALRM
1463 signal(SIGALRM, SIG_IGN);
1464#endif
1465
1466 /*
1467 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1468 * work will be lost.
1469 */
1470#ifdef SIGPWR
1471 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1472#endif
1473
1474 /*
1475 * Arrange for other signals to gracefully shutdown Vim.
1476 */
1477 catch_signals(deathtrap, SIG_ERR);
1478
1479#if defined(FEAT_GUI) && defined(SIGHUP)
1480 /*
1481 * When the GUI is running, ignore the hangup signal.
1482 */
1483 if (gui.in_use)
1484 signal(SIGHUP, SIG_IGN);
1485#endif
1486}
1487
Bram Moolenaardf177f62005-02-22 08:39:57 +00001488#if defined(SIGINT) || defined(PROTO)
1489/*
1490 * Catch CTRL-C (only works while in Cooked mode).
1491 */
1492 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001493catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001494{
1495 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1496}
1497#endif
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001500reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001503#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 /* SIGCONT isn't in the list, because its default action is ignore */
1505 signal(SIGCONT, SIG_DFL);
1506#endif
1507}
1508
1509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001510catch_signals(
1511 RETSIGTYPE (*func_deadly)(),
1512 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 int i;
1515
1516 for (i = 0; signal_info[i].sig != -1; i++)
1517 if (signal_info[i].deadly)
1518 {
1519#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1520 struct sigaction sa;
1521
1522 /* Setup to use the alternate stack for the signal function. */
1523 sa.sa_handler = func_deadly;
1524 sigemptyset(&sa.sa_mask);
1525# if defined(__linux__) && defined(_REENTRANT)
1526 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1527 * thread handling in combination with using the alternate stack:
1528 * pthread library functions try to use the stack pointer to
1529 * identify the current thread, causing a SEGV signal, which
1530 * recursively calls deathtrap() and hangs. */
1531 sa.sa_flags = 0;
1532# else
1533 sa.sa_flags = SA_ONSTACK;
1534# endif
1535 sigaction(signal_info[i].sig, &sa, NULL);
1536#else
1537# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1538 struct sigvec sv;
1539
1540 /* Setup to use the alternate stack for the signal function. */
1541 sv.sv_handler = func_deadly;
1542 sv.sv_mask = 0;
1543 sv.sv_flags = SV_ONSTACK;
1544 sigvec(signal_info[i].sig, &sv, NULL);
1545# else
1546 signal(signal_info[i].sig, func_deadly);
1547# endif
1548#endif
1549 }
1550 else if (func_other != SIG_ERR)
1551 signal(signal_info[i].sig, func_other);
1552}
1553
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001554#ifdef HAVE_SIGPROCMASK
1555 static void
1556block_signals(sigset_t *set)
1557{
1558 sigset_t newset;
1559 int i;
1560
1561 sigemptyset(&newset);
1562
1563 for (i = 0; signal_info[i].sig != -1; i++)
1564 sigaddset(&newset, signal_info[i].sig);
1565
Bram Moolenaar2e310482018-08-21 13:09:10 +02001566# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001567 /* SIGCONT isn't in the list, because its default action is ignore */
1568 sigaddset(&newset, SIGCONT);
1569# endif
1570
1571 sigprocmask(SIG_BLOCK, &newset, set);
1572}
1573
1574 static void
1575unblock_signals(sigset_t *set)
1576{
1577 sigprocmask(SIG_SETMASK, set, NULL);
1578}
1579#endif
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001582 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001583 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1584 * return TRUE
1585 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1586 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001587 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001588 * Returns TRUE when Vim should exit.
1589 */
1590 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001592{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001593 static int got_signal = 0;
1594 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001595
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001596 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001597 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001598 case SIGNAL_BLOCK: blocked = TRUE;
1599 break;
1600
1601 case SIGNAL_UNBLOCK: blocked = FALSE;
1602 if (got_signal != 0)
1603 {
1604 kill(getpid(), got_signal);
1605 got_signal = 0;
1606 }
1607 break;
1608
1609 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001610 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001611 got_signal = sig;
1612#ifdef SIGPWR
1613 if (sig != SIGPWR)
1614#endif
1615 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001616 break;
1617 }
1618 return FALSE;
1619}
1620
1621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 * Check_win checks whether we have an interactive stdout.
1623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001625mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (isatty(1))
1628 return OK;
1629 return FAIL;
1630}
1631
1632/*
1633 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1634 */
1635 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001636mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 if (isatty(read_cmd_fd))
1639 return TRUE;
1640 return FALSE;
1641}
1642
1643#ifdef FEAT_X11
1644
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001645# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1647
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648/*
1649 * Give a message about the elapsed time for opening the X window.
1650 */
1651 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001652xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001654 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655}
1656# endif
1657#endif
1658
1659#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1660/*
1661 * A few functions shared by X11 title and clipboard code.
1662 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001663static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1664static int x_error_check(Display *dpy, XErrorEvent *error_event);
1665static int x_connect_to_server(void);
1666static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668static int got_x_error = FALSE;
1669
1670/*
1671 * X Error handler, otherwise X just exits! (very rude) -- webb
1672 */
1673 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001674x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001676 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 STRCAT(IObuff, _("\nVim: Got X error\n"));
1678
1679 /* We cannot print a message and continue, because no X calls are allowed
1680 * here (causes my system to hang). Silently continuing might be an
1681 * alternative... */
1682 preserve_exit(); /* preserve files and exit */
1683
1684 return 0; /* NOTREACHED */
1685}
1686
1687/*
1688 * Another X Error handler, just used to check for errors.
1689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001691x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 got_x_error = TRUE;
1694 return 0;
1695}
1696
1697#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1698# if defined(HAVE_SETJMP_H)
1699/*
1700 * An X IO Error handler, used to catch error while opening the display.
1701 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001702static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001705x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 /* This function should not return, it causes exit(). Longjump instead. */
1708 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001709# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001710 return 0; /* avoid the compiler complains about missing return value */
1711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713# endif
1714
1715/*
1716 * An X IO Error handler, used to catch terminal errors.
1717 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001718static int x_IOerror_handler(Display *dpy);
1719static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001720static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001723x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001726 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 x11_window = 0;
1728 x11_display = NULL;
1729 xterm_Shell = (Widget)0;
1730
1731 /* This function should not return, it causes exit(). Longjump instead. */
1732 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001733# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001734 return 0; /* avoid the compiler complains about missing return value */
1735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001737
1738/*
1739 * If the X11 connection was lost try to restore it.
1740 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001741 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001742 */
1743 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001744may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001745{
1746 if (xterm_dpy_was_reset)
1747 {
1748 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001749
1750# ifndef LESSTIF_VERSION
1751 /* This has been reported to avoid Vim getting stuck. */
1752 if (app_context != (XtAppContext)NULL)
1753 {
1754 XtDestroyApplicationContext(app_context);
1755 app_context = (XtAppContext)NULL;
1756 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1757 }
1758# endif
1759
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001760 setup_term_clip();
1761 get_x11_title(FALSE);
1762 }
1763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
1765
1766/*
1767 * Return TRUE when connection to the X server is desired.
1768 */
1769 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001770x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#if defined(FEAT_CLIENTSERVER)
1773 if (x_force_connect)
1774 return TRUE;
1775#endif
1776 if (x_no_connect)
1777 return FALSE;
1778
1779 /* Check for a match with "exclude:" from 'clipboard'. */
1780 if (clip_exclude_prog != NULL)
1781 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001782 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 return FALSE;
1784 }
1785 return TRUE;
1786}
1787
1788/*
1789 * Test if "dpy" and x11_window are valid by getting the window title.
1790 * I don't actually want it yet, so there may be a simpler call to use, but
1791 * this will cause the error handler x_error_check() to be called if anything
1792 * is wrong, such as the window pointer being invalid (as can happen when the
1793 * user changes his DISPLAY, but not his WINDOWID) -- webb
1794 */
1795 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001796test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 int (*old_handler)();
1799 XTextProperty text_prop;
1800
1801 old_handler = XSetErrorHandler(x_error_check);
1802 got_x_error = FALSE;
1803 if (XGetWMName(dpy, x11_window, &text_prop))
1804 XFree((void *)text_prop.value);
1805 XSync(dpy, False);
1806 (void)XSetErrorHandler(old_handler);
1807
1808 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001809 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 return (got_x_error ? FAIL : OK);
1812}
1813#endif
1814
1815#ifdef FEAT_TITLE
1816
1817#ifdef FEAT_X11
1818
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001819static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821/*
1822 * try to get x11 window and display
1823 *
1824 * return FAIL for failure, OK otherwise
1825 */
1826 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001827get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 char *winid;
1830 static int result = -1;
1831#define XD_NONE 0 /* x11_display not set here */
1832#define XD_HERE 1 /* x11_display opened here */
1833#define XD_GUI 2 /* x11_display used from gui.dpy */
1834#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1835 static int x11_display_from = XD_NONE;
1836 static int did_set_error_handler = FALSE;
1837
1838 if (!did_set_error_handler)
1839 {
1840 /* X just exits if it finds an error otherwise! */
1841 (void)XSetErrorHandler(x_error_handler);
1842 did_set_error_handler = TRUE;
1843 }
1844
Bram Moolenaar9372a112005-12-06 19:59:18 +00001845#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (gui.in_use)
1847 {
1848 /*
1849 * If the X11 display was opened here before, for the window where Vim
1850 * was started, close that one now to avoid a memory leak.
1851 */
1852 if (x11_display_from == XD_HERE && x11_display != NULL)
1853 {
1854 XCloseDisplay(x11_display);
1855 x11_display_from = XD_NONE;
1856 }
1857 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1858 {
1859 x11_display_from = XD_GUI;
1860 return OK;
1861 }
1862 x11_display = NULL;
1863 return FAIL;
1864 }
1865 else if (x11_display_from == XD_GUI)
1866 {
1867 /* GUI must have stopped somehow, clear x11_display */
1868 x11_window = 0;
1869 x11_display = NULL;
1870 x11_display_from = XD_NONE;
1871 }
1872#endif
1873
1874 /* When started with the "-X" argument, don't try connecting. */
1875 if (!x_connect_to_server())
1876 return FAIL;
1877
1878 /*
1879 * If WINDOWID not set, should try another method to find out
1880 * what the current window number is. The only code I know for
1881 * this is very complicated.
1882 * We assume that zero is invalid for WINDOWID.
1883 */
1884 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1885 x11_window = (Window)atol(winid);
1886
1887#ifdef FEAT_XCLIPBOARD
1888 if (xterm_dpy != NULL && x11_window != 0)
1889 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001890 /* We may have checked it already, but Gnome terminal can move us to
1891 * another window, so we need to check every time. */
1892 if (x11_display_from != XD_XTERM)
1893 {
1894 /*
1895 * If the X11 display was opened here before, for the window where
1896 * Vim was started, close that one now to avoid a memory leak.
1897 */
1898 if (x11_display_from == XD_HERE && x11_display != NULL)
1899 XCloseDisplay(x11_display);
1900 x11_display = xterm_dpy;
1901 x11_display_from = XD_XTERM;
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (test_x11_window(x11_display) == FAIL)
1904 {
1905 /* probably bad $WINDOWID */
1906 x11_window = 0;
1907 x11_display = NULL;
1908 x11_display_from = XD_NONE;
1909 return FAIL;
1910 }
1911 return OK;
1912 }
1913#endif
1914
1915 if (x11_window == 0 || x11_display == NULL)
1916 result = -1;
1917
1918 if (result != -1) /* Have already been here and set this */
1919 return result; /* Don't do all these X calls again */
1920
1921 if (x11_window != 0 && x11_display == NULL)
1922 {
1923#ifdef SET_SIG_ALARM
1924 RETSIGTYPE (*sig_save)();
1925#endif
1926#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1927 struct timeval start_tv;
1928
1929 if (p_verbose > 0)
1930 gettimeofday(&start_tv, NULL);
1931#endif
1932
1933#ifdef SET_SIG_ALARM
1934 /*
1935 * Opening the Display may hang if the DISPLAY setting is wrong, or
1936 * the network connection is bad. Set an alarm timer to get out.
1937 */
1938 sig_alarm_called = FALSE;
1939 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1940 (RETSIGTYPE (*)())sig_alarm);
1941 alarm(2);
1942#endif
1943 x11_display = XOpenDisplay(NULL);
1944
1945#ifdef SET_SIG_ALARM
1946 alarm(0);
1947 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1948 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001949 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#endif
1951 if (x11_display != NULL)
1952 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001953# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001955 {
1956 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001957 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001958 verbose_leave();
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960# endif
1961 if (test_x11_window(x11_display) == FAIL)
1962 {
1963 /* Maybe window id is bad */
1964 x11_window = 0;
1965 XCloseDisplay(x11_display);
1966 x11_display = NULL;
1967 }
1968 else
1969 x11_display_from = XD_HERE;
1970 }
1971 }
1972 if (x11_window == 0 || x11_display == NULL)
1973 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001974
1975# ifdef FEAT_EVAL
1976 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1977# endif
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 return (result = OK);
1980}
1981
1982/*
1983 * Determine original x11 Window Title
1984 */
1985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001986get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001988 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989}
1990
1991/*
1992 * Determine original x11 Window icon
1993 */
1994 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001995get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int retval = FALSE;
1998
1999 retval = get_x11_thing(FALSE, test_only);
2000
2001 /* could not get old icon, use terminal name */
2002 if (oldicon == NULL && !test_only)
2003 {
2004 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002005 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002007 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
2010 return retval;
2011}
2012
2013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002014get_x11_thing(
2015 int get_title, /* get title string */
2016 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 XTextProperty text_prop;
2019 int retval = FALSE;
2020 Status status;
2021
2022 if (get_x11_windis() == OK)
2023 {
2024 /* Get window/icon name if any */
2025 if (get_title)
2026 status = XGetWMName(x11_display, x11_window, &text_prop);
2027 else
2028 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2029
2030 /*
2031 * If terminal is xterm, then x11_window may be a child window of the
2032 * outer xterm window that actually contains the window/icon name, so
2033 * keep traversing up the tree until a window with a title/icon is
2034 * found.
2035 */
2036 /* Previously this was only done for xterm and alikes. I don't see a
2037 * reason why it would fail for other terminal emulators.
2038 * if (term_is_xterm) */
2039 {
2040 Window root;
2041 Window parent;
2042 Window win = x11_window;
2043 Window *children;
2044 unsigned int num_children;
2045
2046 while (!status || text_prop.value == NULL)
2047 {
2048 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2049 &num_children))
2050 break;
2051 if (children)
2052 XFree((void *)children);
2053 if (parent == root || parent == 0)
2054 break;
2055
2056 win = parent;
2057 if (get_title)
2058 status = XGetWMName(x11_display, win, &text_prop);
2059 else
2060 status = XGetWMIconName(x11_display, win, &text_prop);
2061 }
2062 }
2063 if (status && text_prop.value != NULL)
2064 {
2065 retval = TRUE;
2066 if (!test_only)
2067 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002068#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2069 if (text_prop.encoding == XA_STRING
2070# ifdef FEAT_MBYTE
2071 && !has_mbyte
2072# endif
2073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075#endif
2076 if (get_title)
2077 oldtitle = vim_strsave((char_u *)text_prop.value);
2078 else
2079 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002080#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 else
2083 {
2084 char **cl;
2085 Status transform_status;
2086 int n = 0;
2087
2088 transform_status = XmbTextPropertyToTextList(x11_display,
2089 &text_prop,
2090 &cl, &n);
2091 if (transform_status >= Success && n > 0 && cl[0])
2092 {
2093 if (get_title)
2094 oldtitle = vim_strsave((char_u *) cl[0]);
2095 else
2096 oldicon = vim_strsave((char_u *) cl[0]);
2097 XFreeStringList(cl);
2098 }
2099 else
2100 {
2101 if (get_title)
2102 oldtitle = vim_strsave((char_u *)text_prop.value);
2103 else
2104 oldicon = vim_strsave((char_u *)text_prop.value);
2105 }
2106 }
2107#endif
2108 }
2109 XFree((void *)text_prop.value);
2110 }
2111 }
2112 return retval;
2113}
2114
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002115/* Xutf8 functions are not avaialble on older systems. Note that on some
2116 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2117 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2118 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002120# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121# define USE_UTF8_STRING
2122# endif
2123#endif
2124
2125/*
2126 * Set x11 Window Title
2127 *
2128 * get_x11_windis() must be called before this and have returned OK
2129 */
2130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002131set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2134 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2135 * supported everywhere and STRING doesn't work for multi-byte titles.
2136 */
2137#ifdef USE_UTF8_STRING
2138 if (enc_utf8)
2139 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2140 NULL, NULL, 0, NULL, NULL, NULL);
2141 else
2142#endif
2143 {
2144#if XtSpecificationRelease >= 4
2145# ifdef FEAT_XFONTSET
2146 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2147 NULL, NULL, 0, NULL, NULL, NULL);
2148# else
2149 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002150 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002153 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 XSetWMProperties(x11_display, x11_window, &text_prop,
2155 NULL, NULL, 0, NULL, NULL, NULL);
2156# endif
2157#else
2158 XStoreName(x11_display, x11_window, (char *)title);
2159#endif
2160 }
2161 XFlush(x11_display);
2162}
2163
2164/*
2165 * Set x11 Window icon
2166 *
2167 * get_x11_windis() must be called before this and have returned OK
2168 */
2169 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002170set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 /* See above for comments about using X*SetWMProperties(). */
2173#ifdef USE_UTF8_STRING
2174 if (enc_utf8)
2175 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2176 NULL, 0, NULL, NULL, NULL);
2177 else
2178#endif
2179 {
2180#if XtSpecificationRelease >= 4
2181# ifdef FEAT_XFONTSET
2182 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2183 NULL, 0, NULL, NULL, NULL);
2184# else
2185 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002186 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002188 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2190 NULL, 0, NULL, NULL, NULL);
2191# endif
2192#else
2193 XSetIconName(x11_display, x11_window, (char *)icon);
2194#endif
2195 }
2196 XFlush(x11_display);
2197}
2198
2199#else /* FEAT_X11 */
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002202get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 return FALSE;
2205}
2206
2207 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002208get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 if (!test_only)
2211 {
2212 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002213 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002215 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 return FALSE;
2218}
2219
2220#endif /* FEAT_X11 */
2221
2222 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002223mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 return get_x11_title(TRUE);
2226}
2227
2228 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002229mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 return get_x11_icon(TRUE);
2232}
2233
2234/*
2235 * Set the window title and icon.
2236 */
2237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002238mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 int type = 0;
2241 static int recursive = 0;
2242
2243 if (T_NAME == NULL) /* no terminal name (yet) */
2244 return;
2245 if (title == NULL && icon == NULL) /* nothing to do */
2246 return;
2247
2248 /* When one of the X11 functions causes a deadly signal, we get here again
2249 * recursively. Avoid hanging then (something is probably locked). */
2250 if (recursive)
2251 return;
2252 ++recursive;
2253
2254 /*
2255 * if the window ID and the display is known, we may use X11 calls
2256 */
2257#ifdef FEAT_X11
2258 if (get_x11_windis() == OK)
2259 type = 1;
2260#else
2261# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2262 if (gui.in_use)
2263 type = 1;
2264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265#endif
2266
2267 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002268 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * than x11 calls, because the x11 calls don't always work
2270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if ((type || *T_TS != NUL) && title != NULL)
2272 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002273 if (oldtitle_outdated)
2274 {
2275 oldtitle_outdated = FALSE;
2276 VIM_CLEAR(oldtitle);
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (oldtitle == NULL
2279#ifdef FEAT_GUI
2280 && !gui.in_use
2281#endif
2282 ) /* first call but not in GUI, save title */
2283 (void)get_x11_title(FALSE);
2284
2285 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2286 term_settitle(title);
2287#ifdef FEAT_X11
2288 else
2289# ifdef FEAT_GUI_GTK
2290 if (!gui.in_use) /* don't do this if GTK+ is running */
2291# endif
2292 set_x11_title(title); /* x11 */
2293#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002294#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2296 else
2297 gui_mch_settitle(title, icon);
2298#endif
2299 did_set_title = TRUE;
2300 }
2301
2302 if ((type || *T_CIS != NUL) && icon != NULL)
2303 {
2304 if (oldicon == NULL
2305#ifdef FEAT_GUI
2306 && !gui.in_use
2307#endif
2308 ) /* first call, save icon */
2309 get_x11_icon(FALSE);
2310
2311 if (*T_CIS != NUL)
2312 {
2313 out_str(T_CIS); /* set icon start */
2314 out_str_nf(icon);
2315 out_str(T_CIE); /* set icon end */
2316 out_flush();
2317 }
2318#ifdef FEAT_X11
2319 else
2320# ifdef FEAT_GUI_GTK
2321 if (!gui.in_use) /* don't do this if GTK+ is running */
2322# endif
2323 set_x11_icon(icon); /* x11 */
2324#endif
2325 did_set_icon = TRUE;
2326 }
2327 --recursive;
2328}
2329
2330/*
2331 * Restore the window/icon title.
2332 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002333 * SAVE_RESTORE_TITLE only restore title
2334 * SAVE_RESTORE_ICON only restore icon
2335 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 */
2337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002338mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002341 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002343 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2344
2345 // pop and push from/to the stack
2346 term_pop_title(which);
2347 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348}
2349
2350#endif /* FEAT_TITLE */
2351
2352/*
2353 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002354 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
2356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002357vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 if (name == NULL)
2360 return FALSE;
2361 return (STRNICMP(name, "xterm", 5) == 0
2362 || STRNICMP(name, "nxterm", 6) == 0
2363 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002364 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002366 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 || STRCMP(name, "builtin_xterm") == 0);
2368}
2369
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002370#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2371/*
2372 * Return TRUE if "name" appears to be that of a terminal
2373 * known to support the xterm-style mouse protocol.
2374 * Relies on term_is_xterm having been set to its correct value.
2375 */
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002378{
2379 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002380 && (term_is_xterm
2381 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002382 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002383 || STRICMP(name, "st") == 0
2384 || STRNICMP(name, "st-", 3) == 0
2385 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002386}
2387#endif
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2390/*
2391 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2392 * Return 1 for "xterm".
2393 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002394 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002395 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002398use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002400 if (ttym_flags == TTYM_SGR)
2401 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002402 if (ttym_flags == TTYM_URXVT)
2403 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (ttym_flags == TTYM_XTERM2)
2405 return 2;
2406 if (ttym_flags == TTYM_XTERM)
2407 return 1;
2408 return 0;
2409}
2410#endif
2411
2412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002413vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
2415 if (name == NULL)
2416 return FALSE;
2417 return (STRNICMP(name, "iris-ansi", 9) == 0
2418 || STRCMP(name, "builtin_iris-ansi") == 0);
2419}
2420
2421 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002422vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 if (name == NULL)
2425 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002426 /* catch VT100 - VT5xx */
2427 return ((STRNICMP(name, "vt", 2) == 0
2428 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 || STRCMP(name, "builtin_vt320") == 0);
2430}
2431
2432/*
2433 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2434 * This should include all windowed terminal emulators.
2435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (name == NULL)
2440 return FALSE;
2441 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2442 return TRUE;
2443 return ( STRNICMP(name, "hpterm", 6) == 0
2444 || STRNICMP(name, "sun-cmd", 7) == 0
2445 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002446 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 || STRNICMP(name, "dtterm", 6) == 0);
2448}
2449
2450/*
2451 * Insert user name in s[len].
2452 * Return OK if a name found.
2453 */
2454 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002455mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002458 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return OK;
2460#else
2461 return mch_get_uname(getuid(), s, len);
2462#endif
2463}
2464
2465/*
2466 * Insert user name for "uid" in s[len].
2467 * Return OK if a name found.
2468 */
2469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002470mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2473 struct passwd *pw;
2474
2475 if ((pw = getpwuid(uid)) != NULL
2476 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2477 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002478 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return OK;
2480 }
2481#endif
2482 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2483 return FAIL; /* a number is not a name */
2484}
2485
2486/*
2487 * Insert host name is s[len].
2488 */
2489
2490#ifdef HAVE_SYS_UTSNAME_H
2491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002492mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 struct utsname vutsname;
2495
2496 if (uname(&vutsname) < 0)
2497 *s = NUL;
2498 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002499 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501#else /* HAVE_SYS_UTSNAME_H */
2502
2503# ifdef HAVE_SYS_SYSTEMINFO_H
2504# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2505# endif
2506
2507 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002508mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510# ifdef VAXC
2511 vaxc$gethostname((char *)s, len);
2512# else
2513 gethostname((char *)s, len);
2514# endif
2515 s[len - 1] = NUL; /* make sure it's terminated */
2516}
2517#endif /* HAVE_SYS_UTSNAME_H */
2518
2519/*
2520 * return process ID
2521 */
2522 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002523mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 return (long)getpid();
2526}
2527
2528#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002529static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002532strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534 extern int sys_nerr;
2535 extern char *sys_errlist[];
2536 static char er[20];
2537
2538 if (err > 0 && err < sys_nerr)
2539 return (sys_errlist[err]);
2540 sprintf(er, "Error %d", err);
2541 return er;
2542}
2543#endif
2544
2545/*
2546 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2547 * Return OK for success, FAIL for failure.
2548 */
2549 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002550mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552#if defined(USE_GETCWD)
2553 if (getcwd((char *)buf, len) == NULL)
2554 {
2555 STRCPY(buf, strerror(errno));
2556 return FAIL;
2557 }
2558 return OK;
2559#else
2560 return (getwd((char *)buf) != NULL ? OK : FAIL);
2561#endif
2562}
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002565 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 *
2567 * return FAIL for failure, OK for success
2568 */
2569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002570mch_FullName(
2571 char_u *fname,
2572 char_u *buf,
2573 int len,
2574 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002577#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 int fd = -1;
2579 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 char_u olddir[MAXPATHL];
2582 char_u *p;
2583 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002584#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002585 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2586 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002587#endif
2588
Bram Moolenaar38323e42007-03-06 19:22:53 +00002589#ifdef VMS
2590 fname = vms_fixfilename(fname);
2591#endif
2592
Bram Moolenaara2442432007-04-26 14:26:37 +00002593#ifdef __CYGWIN__
2594 /*
2595 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2596 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002597# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002598 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2599 * a forward slash. */
2600 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2601 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002602# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002603 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002604# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002605 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002606#endif
2607
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002608 /* Expand it if forced or not an absolute path.
2609 * Do not do it for "/file", the result is always "/". */
2610 if ((force || !mch_isFullName(fname))
2611 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
2614 * If the file name has a path, change to that directory for a moment,
2615 * and then do the getwd() (and get back to where we were).
2616 * This will get the correct path name with "../" things.
2617 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002618 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002620#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 /*
2622 * Use fchdir() if possible, it's said to be faster and more
2623 * reliable. But on SunOS 4 it might not work. Check this by
2624 * doing a fchdir() right now.
2625 */
2626 if (!dont_fchdir)
2627 {
2628 fd = open(".", O_RDONLY | O_EXTRA, 0);
2629 if (fd >= 0 && fchdir(fd) < 0)
2630 {
2631 close(fd);
2632 fd = -1;
2633 dont_fchdir = TRUE; /* don't try again */
2634 }
2635 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 /* Only change directory when we are sure we can return to where
2639 * we are now. After doing "su" chdir(".") might not work. */
2640 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002641#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 (mch_dirname(olddir, MAXPATHL) == FAIL
2645 || mch_chdir((char *)olddir) != 0))
2646 {
2647 p = NULL; /* can't get current dir: don't chdir */
2648 retval = FAIL;
2649 }
2650 else
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 /* The directory is copied into buf[], to be able to remove
2653 * the file name without changing it (could be a string in
2654 * read-only memory) */
2655 if (p - fname >= len)
2656 retval = FAIL;
2657 else
2658 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002659 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (mch_chdir((char *)buf))
2661 retval = FAIL;
2662 else
2663 fname = p + 1;
2664 *buf = NUL;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 }
2668 if (mch_dirname(buf, len) == FAIL)
2669 {
2670 retval = FAIL;
2671 *buf = NUL;
2672 }
2673 if (p != NULL)
2674 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002675#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (fd >= 0)
2677 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002678 if (p_verbose >= 5)
2679 {
2680 verbose_enter();
2681 MSG("fchdir() to previous dir");
2682 verbose_leave();
2683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 l = fchdir(fd);
2685 close(fd);
2686 }
2687 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 l = mch_chdir((char *)olddir);
2690 if (l != 0)
2691 EMSG(_(e_prev_dir));
2692 }
2693
2694 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002695 if (l >= len - 1)
2696 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002697#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002698 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002700 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002705 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
2707
2708 /* Do not append ".", "/dir/." is equal to "/dir". */
2709 if (STRCMP(fname, ".") != 0)
2710 STRCAT(buf, fname);
2711
2712 return OK;
2713}
2714
2715/*
2716 * Return TRUE if "fname" does not depend on the current directory.
2717 */
2718 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002719mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002721#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 return ( fname[0] == '/' || fname[0] == '.' ||
2723 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2724 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2725 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002726#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif
2729}
2730
Bram Moolenaar24552be2005-12-10 20:17:30 +00002731#if defined(USE_FNAME_CASE) || defined(PROTO)
2732/*
2733 * Set the case of the file name, if it already exists. This will cause the
2734 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002735 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002736 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002738fname_case(
2739 char_u *name,
2740 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002741{
2742 struct stat st;
2743 char_u *slash, *tail;
2744 DIR *dirp;
2745 struct dirent *dp;
2746
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002747 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002748 {
2749 /* Open the directory where the file is located. */
2750 slash = vim_strrchr(name, '/');
2751 if (slash == NULL)
2752 {
2753 dirp = opendir(".");
2754 tail = name;
2755 }
2756 else
2757 {
2758 *slash = NUL;
2759 dirp = opendir((char *)name);
2760 *slash = '/';
2761 tail = slash + 1;
2762 }
2763
2764 if (dirp != NULL)
2765 {
2766 while ((dp = readdir(dirp)) != NULL)
2767 {
2768 /* Only accept names that differ in case and are the same byte
2769 * length. TODO: accept different length name. */
2770 if (STRICMP(tail, dp->d_name) == 0
2771 && STRLEN(tail) == STRLEN(dp->d_name))
2772 {
2773 char_u newname[MAXPATHL + 1];
2774 struct stat st2;
2775
2776 /* Verify the inode is equal. */
2777 vim_strncpy(newname, name, MAXPATHL);
2778 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2779 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002780 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002781 && st.st_ino == st2.st_ino
2782 && st.st_dev == st2.st_dev)
2783 {
2784 STRCPY(tail, dp->d_name);
2785 break;
2786 }
2787 }
2788 }
2789
2790 closedir(dirp);
2791 }
2792 }
2793}
2794#endif
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796/*
2797 * Get file permissions for 'name'.
2798 * Returns -1 when it doesn't exist.
2799 */
2800 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002801mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 struct stat statb;
2804
2805 /* Keep the #ifdef outside of stat(), it may be a macro. */
2806#ifdef VMS
2807 if (stat((char *)vms_fixfilename(name), &statb))
2808#else
2809 if (stat((char *)name, &statb))
2810#endif
2811 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002812#ifdef __INTERIX
2813 /* The top bit makes the value negative, which means the file doesn't
2814 * exist. Remove the bit, we don't use it. */
2815 return statb.st_mode & ~S_ADDACE;
2816#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819}
2820
2821/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002822 * Set file permission for "name" to "perm".
2823 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 */
2825 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002826mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 return (chmod((char *)
2829#ifdef VMS
2830 vms_fixfilename(name),
2831#else
2832 name,
2833#endif
2834 (mode_t)perm) == 0 ? OK : FAIL);
2835}
2836
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002837#if defined(HAVE_FCHMOD) || defined(PROTO)
2838/*
2839 * Set file permission for open file "fd" to "perm".
2840 * Return FAIL for failure, OK otherwise.
2841 */
2842 int
2843mch_fsetperm(int fd, long perm)
2844{
2845 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2846}
2847#endif
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#if defined(HAVE_ACL) || defined(PROTO)
2850# ifdef HAVE_SYS_ACL_H
2851# include <sys/acl.h>
2852# endif
2853# ifdef HAVE_SYS_ACCESS_H
2854# include <sys/access.h>
2855# endif
2856
2857# ifdef HAVE_SOLARIS_ACL
2858typedef struct vim_acl_solaris_T {
2859 int acl_cnt;
2860 aclent_t *acl_entry;
2861} vim_acl_solaris_T;
2862# endif
2863
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002864#if defined(HAVE_SELINUX) || defined(PROTO)
2865/*
2866 * Copy security info from "from_file" to "to_file".
2867 */
2868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002869mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002870{
2871 if (from_file == NULL)
2872 return;
2873
2874 if (selinux_enabled == -1)
2875 selinux_enabled = is_selinux_enabled();
2876
2877 if (selinux_enabled > 0)
2878 {
2879 security_context_t from_context = NULL;
2880 security_context_t to_context = NULL;
2881
2882 if (getfilecon((char *)from_file, &from_context) < 0)
2883 {
2884 /* If the filesystem doesn't support extended attributes,
2885 the original had no special security context and the
2886 target cannot have one either. */
2887 if (errno == EOPNOTSUPP)
2888 return;
2889
2890 MSG_PUTS(_("\nCould not get security context for "));
2891 msg_outtrans(from_file);
2892 msg_putchar('\n');
2893 return;
2894 }
2895 if (getfilecon((char *)to_file, &to_context) < 0)
2896 {
2897 MSG_PUTS(_("\nCould not get security context for "));
2898 msg_outtrans(to_file);
2899 msg_putchar('\n');
2900 freecon (from_context);
2901 return ;
2902 }
2903 if (strcmp(from_context, to_context) != 0)
2904 {
2905 if (setfilecon((char *)to_file, from_context) < 0)
2906 {
2907 MSG_PUTS(_("\nCould not set security context for "));
2908 msg_outtrans(to_file);
2909 msg_putchar('\n');
2910 }
2911 }
2912 freecon(to_context);
2913 freecon(from_context);
2914 }
2915}
2916#endif /* HAVE_SELINUX */
2917
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002918#if defined(HAVE_SMACK) && !defined(PROTO)
2919/*
2920 * Copy security info from "from_file" to "to_file".
2921 */
2922 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002923mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002924{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002925 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002926 {
2927 XATTR_NAME_SMACK,
2928 XATTR_NAME_SMACKEXEC,
2929 XATTR_NAME_SMACKMMAP
2930 };
2931
2932 char buffer[SMACK_LABEL_LEN];
2933 const char *name;
2934 int index;
2935 int ret;
2936 ssize_t size;
2937
2938 if (from_file == NULL)
2939 return;
2940
2941 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2942 / sizeof(smack_copied_attributes)[0]) ; index++)
2943 {
2944 /* get the name of the attribute to copy */
2945 name = smack_copied_attributes[index];
2946
2947 /* get the value of the attribute in buffer */
2948 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2949 if (size >= 0)
2950 {
2951 /* copy the attribute value of buffer */
2952 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2953 if (ret < 0)
2954 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002955 vim_snprintf((char *)IObuff, IOSIZE,
2956 _("Could not set security context %s for %s"),
2957 name, to_file);
2958 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002959 msg_putchar('\n');
2960 }
2961 }
2962 else
2963 {
2964 /* what reason of not having the attribute value? */
2965 switch (errno)
2966 {
2967 case ENOTSUP:
2968 /* extended attributes aren't supported or enabled */
2969 /* should a message be echoed? not sure... */
2970 return; /* leave because it isn't usefull to continue */
2971
2972 case ERANGE:
2973 default:
2974 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002975 vim_snprintf((char *)IObuff, IOSIZE,
2976 _("Could not get security context %s for %s. Removing it!"),
2977 name, from_file);
2978 msg_puts(IObuff);
2979 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002980 /* FALLTHROUGH to remove the attribute */
2981
2982 case ENODATA:
2983 /* no attribute of this name */
2984 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002985 /* Silently ignore errors, apparently this happens when
2986 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002987 break;
2988 }
2989 }
2990 }
2991}
2992#endif /* HAVE_SMACK */
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
2995 * Return a pointer to the ACL of file "fname" in allocated memory.
2996 * Return NULL if the ACL is not available for whatever reason.
2997 */
2998 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002999mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 vim_acl_T ret = NULL;
3002#ifdef HAVE_POSIX_ACL
3003 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3004#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003005#ifdef HAVE_SOLARIS_ZFS_ACL
3006 acl_t *aclent;
3007
3008 if (acl_get((char *)fname, 0, &aclent) < 0)
3009 return NULL;
3010 ret = (vim_acl_T)aclent;
3011#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#ifdef HAVE_SOLARIS_ACL
3013 vim_acl_solaris_T *aclent;
3014
3015 aclent = malloc(sizeof(vim_acl_solaris_T));
3016 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3017 {
3018 free(aclent);
3019 return NULL;
3020 }
3021 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3022 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3023 {
3024 free(aclent->acl_entry);
3025 free(aclent);
3026 return NULL;
3027 }
3028 ret = (vim_acl_T)aclent;
3029#else
3030#if defined(HAVE_AIX_ACL)
3031 int aclsize;
3032 struct acl *aclent;
3033
3034 aclsize = sizeof(struct acl);
3035 aclent = malloc(aclsize);
3036 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3037 {
3038 if (errno == ENOSPC)
3039 {
3040 aclsize = aclent->acl_len;
3041 aclent = realloc(aclent, aclsize);
3042 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3043 {
3044 free(aclent);
3045 return NULL;
3046 }
3047 }
3048 else
3049 {
3050 free(aclent);
3051 return NULL;
3052 }
3053 }
3054 ret = (vim_acl_T)aclent;
3055#endif /* HAVE_AIX_ACL */
3056#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003057#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#endif /* HAVE_POSIX_ACL */
3059 return ret;
3060}
3061
3062/*
3063 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3064 */
3065 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003066mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 if (aclent == NULL)
3069 return;
3070#ifdef HAVE_POSIX_ACL
3071 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3072#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003073#ifdef HAVE_SOLARIS_ZFS_ACL
3074 acl_set((char *)fname, (acl_t *)aclent);
3075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#ifdef HAVE_SOLARIS_ACL
3077 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3078 ((vim_acl_solaris_T *)aclent)->acl_entry);
3079#else
3080#ifdef HAVE_AIX_ACL
3081 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3082#endif /* HAVE_AIX_ACL */
3083#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003084#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#endif /* HAVE_POSIX_ACL */
3086}
3087
3088 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003089mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 if (aclent == NULL)
3092 return;
3093#ifdef HAVE_POSIX_ACL
3094 acl_free((acl_t)aclent);
3095#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003096#ifdef HAVE_SOLARIS_ZFS_ACL
3097 acl_free((acl_t *)aclent);
3098#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#ifdef HAVE_SOLARIS_ACL
3100 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3101 free(aclent);
3102#else
3103#ifdef HAVE_AIX_ACL
3104 free(aclent);
3105#endif /* HAVE_AIX_ACL */
3106#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003107#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif /* HAVE_POSIX_ACL */
3109}
3110#endif
3111
3112/*
3113 * Set hidden flag for "name".
3114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 /* can't hide a file */
3119}
3120
3121/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003122 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * return FALSE if "name" is not a directory
3124 * return FALSE for error
3125 */
3126 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003127mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128{
3129 struct stat statb;
3130
3131 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3132 return FALSE;
3133 if (stat((char *)name, &statb))
3134 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136}
3137
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003138/*
3139 * return TRUE if "name" is a directory, NOT a symlink to a directory
3140 * return FALSE if "name" is not a directory
3141 * return FALSE for error
3142 */
3143 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003144mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003145{
3146 struct stat statb;
3147
3148 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3149 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003150 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003151 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003152 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003153}
3154
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003155static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157/*
3158 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3159 */
3160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003161executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162{
3163 struct stat st;
3164
3165 if (stat((char *)name, &st))
3166 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003167#ifdef VMS
3168 /* Like on Unix system file can have executable rights but not necessarily
3169 * be an executable, but on Unix is not a default for an ordianry file to
3170 * have an executable flag - on VMS it is in most cases.
3171 * Therefore, this check does not have any sense - let keep us to the
3172 * conventions instead:
3173 * *.COM and *.EXE files are the executables - the rest are not. This is
3174 * not ideal but better then it was.
3175 */
3176 int vms_executable = 0;
3177 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3178 {
3179 if (strstr(vms_tolower((char*)name),".exe") != NULL
3180 || strstr(vms_tolower((char*)name),".com")!= NULL)
3181 vms_executable = 1;
3182 }
3183 return vms_executable;
3184#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187}
3188
3189/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003190 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003191 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * Return -1 if unknown.
3193 */
3194 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003195mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 char_u *buf;
3198 char_u *p, *e;
3199 int retval;
3200
Bram Moolenaarb5971142015-03-21 17:32:19 +01003201 /* When "use_path" is false and if it's an absolute or relative path don't
3202 * need to use $PATH. */
3203 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3204 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003205 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003206 /* There must be a path separator, files in the current directory
3207 * can't be executed. */
3208 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003209 {
3210 if (path != NULL)
3211 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003212 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003213 *path = FullName_save(name, TRUE);
3214 else
3215 *path = vim_strsave(name);
3216 }
3217 return TRUE;
3218 }
3219 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 p = (char_u *)getenv("PATH");
3223 if (p == NULL || *p == NUL)
3224 return -1;
3225 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3226 if (buf == NULL)
3227 return -1;
3228
3229 /*
3230 * Walk through all entries in $PATH to check if "name" exists there and
3231 * is an executable file.
3232 */
3233 for (;;)
3234 {
3235 e = (char_u *)strchr((char *)p, ':');
3236 if (e == NULL)
3237 e = p + STRLEN(p);
3238 if (e - p <= 1) /* empty entry means current dir */
3239 STRCPY(buf, "./");
3240 else
3241 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003242 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 add_pathsep(buf);
3244 }
3245 STRCAT(buf, name);
3246 retval = executable_file(buf);
3247 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003248 {
3249 if (path != NULL)
3250 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003251 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003252 *path = FullName_save(buf, TRUE);
3253 else
3254 *path = vim_strsave(buf);
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 if (*e != ':')
3260 break;
3261 p = e + 1;
3262 }
3263
3264 vim_free(buf);
3265 return retval;
3266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268/*
3269 * Check what "name" is:
3270 * NODE_NORMAL: file or directory (or doesn't exist)
3271 * NODE_WRITABLE: writable device, socket, fifo, etc.
3272 * NODE_OTHER: non-writable things
3273 */
3274 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003275mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 struct stat st;
3278
3279 if (stat((char *)name, &st))
3280 return NODE_NORMAL;
3281 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3282 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3284 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /* Everything else is writable? */
3286 return NODE_WRITABLE;
3287}
3288
3289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003290mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292#ifdef HAVE_CHECK_STACK_GROWTH
3293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 check_stack_growth((char *)&i);
3296
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003297# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 get_stack_limit();
3299# endif
3300
3301#endif
3302
3303 /*
3304 * Setup an alternative stack for signals. Helps to catch signals when
3305 * running out of stack space.
3306 * Use of sigaltstack() is preferred, it's more portable.
3307 * Ignore any errors.
3308 */
3309#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003310 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 init_signal_stack();
3312#endif
3313}
3314
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003315#if defined(EXITFREE) || defined(PROTO)
3316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003317mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003318{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3320 if (clip_star.owned)
3321 clip_lose_selection(&clip_star);
3322 if (clip_plus.owned)
3323 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003324# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003325# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003326 if (xterm_Shell != (Widget)0)
3327 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003328# ifndef LESSTIF_VERSION
3329 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003330 if (xterm_dpy != NULL)
3331 XtCloseDisplay(xterm_dpy);
3332 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003333 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003334 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003335# ifdef FEAT_X11
3336 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3337# endif
3338 }
3339# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003340# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003341# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003342 if (x11_display != NULL
3343# ifdef FEAT_XCLIPBOARD
3344 && x11_display != xterm_dpy
3345# endif
3346 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003347 XCloseDisplay(x11_display);
3348# endif
3349# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003350 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003351# endif
3352# ifdef FEAT_TITLE
3353 vim_free(oldtitle);
3354 vim_free(oldicon);
3355# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003356}
3357#endif
3358
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003359static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361/*
3362 * Output a newline when exiting.
3363 * Make sure the newline goes to the same stream as the text.
3364 */
3365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003366exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003368 if (silent_mode)
3369 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (newline_on_exit || msg_didout)
3371 {
3372 if (msg_use_printf())
3373 {
3374 if (info_message)
3375 mch_msg("\n");
3376 else
3377 mch_errmsg("\r\n");
3378 }
3379 else
3380 out_char('\n');
3381 }
3382 else
3383 {
3384 restore_cterm_colors(); /* get original colors back */
3385 msg_clr_eos_force(); /* clear the rest of the display */
3386 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3387 }
3388}
3389
3390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003391mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 exiting = TRUE;
3394
3395#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3396 x11_export_final_selection();
3397#endif
3398
3399#ifdef FEAT_GUI
3400 if (!gui.in_use)
3401#endif
3402 {
3403 settmode(TMODE_COOK);
3404#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003405 // restore xterm title and icon name
3406 mch_restore_title(SAVE_RESTORE_BOTH);
3407 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#endif
3409 /*
3410 * When t_ti is not empty but it doesn't cause swapping terminal
3411 * pages, need to output a newline when msg_didout is set. But when
3412 * t_ti does swap pages it should not go to the shell page. Do this
3413 * before stoptermcap().
3414 */
3415 if (swapping_screen() && !newline_on_exit)
3416 exit_scroll();
3417
3418 /* Stop termcap: May need to check for T_CRV response, which
3419 * requires RAW mode. */
3420 stoptermcap();
3421
3422 /*
3423 * A newline is only required after a message in the alternate screen.
3424 * This is set to TRUE by wait_return().
3425 */
3426 if (!swapping_screen() || newline_on_exit)
3427 exit_scroll();
3428
3429 /* Cursor may have been switched off without calling starttermcap()
3430 * when doing "vim -u vimrc" and vimrc contains ":q". */
3431 if (full_screen)
3432 cursor_on();
3433 }
3434 out_flush();
3435 ml_close_all(TRUE); /* remove all memfiles */
3436 may_core_dump();
3437#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 gui_exit(r);
3440#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003441
Bram Moolenaar56718732006-03-15 22:53:57 +00003442#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003443 mac_conv_cleanup();
3444#endif
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#ifdef __QNX__
3447 /* A core dump won't be created if the signal handler
3448 * doesn't return, so we can't call exit() */
3449 if (deadly_signal != 0)
3450 return;
3451#endif
3452
Bram Moolenaar009b2592004-10-24 19:18:58 +00003453#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003454 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003455#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003456
3457#ifdef EXITFREE
3458 free_all_mem();
3459#endif
3460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 exit(r);
3462}
3463
3464 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003465may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 if (deadly_signal != 0)
3468 {
3469 signal(deadly_signal, SIG_DFL);
3470 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3471 }
3472}
3473
3474#ifndef VMS
3475
3476 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003477mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 static int first = TRUE;
3480
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003481#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482# ifdef HAVE_TERMIOS_H
3483 static struct termios told;
3484 struct termios tnew;
3485# else
3486 static struct termio told;
3487 struct termio tnew;
3488# endif
3489
3490 if (first)
3491 {
3492 first = FALSE;
3493# if defined(HAVE_TERMIOS_H)
3494 tcgetattr(read_cmd_fd, &told);
3495# else
3496 ioctl(read_cmd_fd, TCGETA, &told);
3497# endif
3498 }
3499
3500 tnew = told;
3501 if (tmode == TMODE_RAW)
3502 {
3503 /*
3504 * ~ICRNL enables typing ^V^M
3505 */
3506 tnew.c_iflag &= ~ICRNL;
3507 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3508# if defined(IEXTEN) && !defined(__MINT__)
3509 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3510 /* but it breaks function keys on MINT */
3511# endif
3512 );
3513# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3514 tnew.c_oflag &= ~ONLCR;
3515# endif
3516 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3517 tnew.c_cc[VTIME] = 0; /* don't wait */
3518 }
3519 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003520 {
3521 /* Also reset ICANON here, otherwise on Solaris select() won't see
3522 * typeahead characters. */
3523 tnew.c_lflag &= ~(ICANON | ECHO);
3524 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3525 tnew.c_cc[VTIME] = 0; /* don't wait */
3526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528# if defined(HAVE_TERMIOS_H)
3529 {
3530 int n = 10;
3531
3532 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3533 * few times. */
3534 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3535 && errno == EINTR && n > 0)
3536 --n;
3537 }
3538# else
3539 ioctl(read_cmd_fd, TCSETA, &tnew);
3540# endif
3541
3542#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * for "old" tty systems
3545 */
3546# ifndef TIOCSETN
3547# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3548# endif
3549 static struct sgttyb ttybold;
3550 struct sgttyb ttybnew;
3551
3552 if (first)
3553 {
3554 first = FALSE;
3555 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3556 }
3557
3558 ttybnew = ttybold;
3559 if (tmode == TMODE_RAW)
3560 {
3561 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3562 ttybnew.sg_flags |= RAW;
3563 }
3564 else if (tmode == TMODE_SLEEP)
3565 ttybnew.sg_flags &= ~(ECHO);
3566 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3567#endif
3568 curr_tmode = tmode;
3569}
3570
3571/*
3572 * Try to get the code for "t_kb" from the stty setting
3573 *
3574 * Even if termcap claims a backspace key, the user's setting *should*
3575 * prevail. stty knows more about reality than termcap does, and if
3576 * somebody's usual erase key is DEL (which, for most BSD users, it will
3577 * be), they're going to get really annoyed if their erase key starts
3578 * doing forward deletes for no reason. (Eric Fischer)
3579 */
3580 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003581get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003583 ttyinfo_T info;
3584 char_u buf[2];
3585 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 if (get_tty_info(read_cmd_fd, &info) == OK)
3588 {
3589 intr_char = info.interrupt;
3590 buf[0] = info.backspace;
3591 buf[1] = NUL;
3592 add_termcode((char_u *)"kb", buf, FALSE);
3593
3594 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3595 p = find_termcode((char_u *)"kD");
3596 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3597 do_fixdel(NULL);
3598 }
3599}
3600
3601/*
3602 * Obtain the characters that Backspace and Enter produce on "fd".
3603 * Returns OK or FAIL.
3604 */
3605 int
3606get_tty_info(int fd, ttyinfo_T *info)
3607{
3608#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609# ifdef HAVE_TERMIOS_H
3610 struct termios keys;
3611# else
3612 struct termio keys;
3613# endif
3614
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003615 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003617 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003619 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003621 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003623 info->backspace = keys.c_cc[VERASE];
3624 info->interrupt = keys.c_cc[VINTR];
3625 if (keys.c_iflag & ICRNL)
3626 info->enter = NL;
3627 else
3628 info->enter = CAR;
3629 if (keys.c_oflag & ONLCR)
3630 info->nl_does_cr = TRUE;
3631 else
3632 info->nl_does_cr = FALSE;
3633 return OK;
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#else
3636 /* for "old" tty systems */
3637 struct sgttyb keys;
3638
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003639 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003641 info->backspace = keys.sg_erase;
3642 info->interrupt = keys.sg_kill;
3643 info->enter = CAR;
3644 info->nl_does_cr = TRUE;
3645 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003648 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649}
3650
3651#endif /* VMS */
3652
3653#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003654static int mouse_ison = FALSE;
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * Set mouse clicks on or off.
3658 */
3659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003660mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003662# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003663 static int bevalterm_ison = FALSE;
3664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 int xterm_mouse_vers;
3666
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003667 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003668# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003669 && p_bevalterm == bevalterm_ison
3670# endif
3671 )
3672 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return;
3674
3675 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003676
3677# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003678 if (ttym_flags == TTYM_URXVT)
3679 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003680 out_str_nf((char_u *)
3681 (on
3682 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3683 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003684 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003685 }
3686# endif
3687
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003688# ifdef FEAT_MOUSE_SGR
3689 if (ttym_flags == TTYM_SGR)
3690 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003691 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003692 out_str_nf((char_u *)
3693 (on
3694 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3695 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003696 mouse_ison = on;
3697 }
3698# endif
3699
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003700# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003701 if (bevalterm_ison != (p_bevalterm && on))
3702 {
3703 bevalterm_ison = (p_bevalterm && on);
3704 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3705 /* disable mouse movement events, enabling is below */
3706 out_str_nf((char_u *)
3707 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003708 }
3709# endif
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (xterm_mouse_vers > 0)
3712 {
3713 if (on) /* enable mouse events, use mouse tracking if available */
3714 out_str_nf((char_u *)
3715 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003716 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003717# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003718 bevalterm_ison
3719 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3720# endif
3721 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3723 else /* disable mouse events, could probably always send the same */
3724 out_str_nf((char_u *)
3725 (xterm_mouse_vers > 1
3726 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3727 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003728 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730
3731# ifdef FEAT_MOUSE_DEC
3732 else if (ttym_flags == TTYM_DEC)
3733 {
3734 if (on) /* enable mouse events */
3735 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3736 else /* disable mouse events */
3737 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003738 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740# endif
3741
3742# ifdef FEAT_MOUSE_GPM
3743 else
3744 {
3745 if (on)
3746 {
3747 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003748 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750 else
3751 {
3752 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003753 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 }
3756# endif
3757
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003758# ifdef FEAT_SYSMOUSE
3759 else
3760 {
3761 if (on)
3762 {
3763 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003764 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003765 }
3766 else
3767 {
3768 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003769 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003770 }
3771 }
3772# endif
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774# ifdef FEAT_MOUSE_JSB
3775 else
3776 {
3777 if (on)
3778 {
3779 /* D - Enable Mouse up/down messages
3780 * L - Enable Left Button Reporting
3781 * M - Enable Middle Button Reporting
3782 * R - Enable Right Button Reporting
3783 * K - Enable SHIFT and CTRL key Reporting
3784 * + - Enable Advanced messaging of mouse moves and up/down messages
3785 * Q - Quiet No Ack
3786 * # - Numeric value of mouse pointer required
3787 * 0 = Multiview 2000 cursor, used as standard
3788 * 1 = Windows Arrow
3789 * 2 = Windows I Beam
3790 * 3 = Windows Hour Glass
3791 * 4 = Windows Cross Hair
3792 * 5 = Windows UP Arrow
3793 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003794# ifdef JSBTERM_MOUSE_NONADVANCED
3795 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3797 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003798# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3800 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003801# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003802 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 else
3805 {
3806 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3807 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003808 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 }
3811# endif
3812# ifdef FEAT_MOUSE_PTERM
3813 else
3814 {
3815 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3816 if (on)
3817 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3818 else
3819 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003820 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822# endif
3823}
3824
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003825#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003826/*
3827 * Called when 'balloonevalterm' changed.
3828 */
3829 void
3830mch_bevalterm_changed(void)
3831{
3832 mch_setmouse(mouse_ison);
3833}
3834#endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836/*
3837 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3838 */
3839 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003840check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
3842# ifdef FEAT_MOUSE_XTERM
3843 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003844# ifdef FEAT_MOUSE_URXVT
3845 && use_xterm_mouse() != 3
3846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847# ifdef FEAT_GUI
3848 && !gui.in_use
3849# endif
3850 )
3851 {
3852 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003853 ? IF_EB("\233M", CSI_STR "M")
3854 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (*p_mouse != NUL)
3856 {
3857 /* force mouse off and maybe on to send possibly new mouse
3858 * activation sequence to the xterm, with(out) drag tracing. */
3859 mch_setmouse(FALSE);
3860 setmouse();
3861 }
3862 }
3863 else
3864 del_mouse_termcode(KS_MOUSE);
3865# endif
3866
3867# ifdef FEAT_MOUSE_GPM
3868 if (!use_xterm_mouse()
3869# ifdef FEAT_GUI
3870 && !gui.in_use
3871# endif
3872 )
3873 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3874# endif
3875
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003876# ifdef FEAT_SYSMOUSE
3877 if (!use_xterm_mouse()
3878# ifdef FEAT_GUI
3879 && !gui.in_use
3880# endif
3881 )
3882 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3883# endif
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003886 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (!use_xterm_mouse()
3888# ifdef FEAT_GUI
3889 && !gui.in_use
3890# endif
3891 )
3892 set_mouse_termcode(KS_JSBTERM_MOUSE,
3893 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3894 else
3895 del_mouse_termcode(KS_JSBTERM_MOUSE);
3896# endif
3897
3898# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003899 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 * define it in the GUI or when using an xterm. */
3901 if (!use_xterm_mouse()
3902# ifdef FEAT_GUI
3903 && !gui.in_use
3904# endif
3905 )
3906 set_mouse_termcode(KS_NETTERM_MOUSE,
3907 (char_u *)IF_EB("\033}", ESC_STR "}"));
3908 else
3909 del_mouse_termcode(KS_NETTERM_MOUSE);
3910# endif
3911
3912# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003913 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003914 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915# ifdef FEAT_GUI
3916 && !gui.in_use
3917# endif
3918 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003919 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3920 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 else
3922 del_mouse_termcode(KS_DEC_MOUSE);
3923# endif
3924# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003925 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003926 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927# ifdef FEAT_GUI
3928 && !gui.in_use
3929# endif
3930 )
3931 set_mouse_termcode(KS_PTERM_MOUSE,
3932 (char_u *) IF_EB("\033[", ESC_STR "["));
3933 else
3934 del_mouse_termcode(KS_PTERM_MOUSE);
3935# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003936# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003937 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003938# ifdef FEAT_GUI
3939 && !gui.in_use
3940# endif
3941 )
3942 {
3943 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003944 ? IF_EB("\233*M", CSI_STR "*M")
3945 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003946
3947 if (*p_mouse != NUL)
3948 {
3949 mch_setmouse(FALSE);
3950 setmouse();
3951 }
3952 }
3953 else
3954 del_mouse_termcode(KS_URXVT_MOUSE);
3955# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003956# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003957 if (use_xterm_mouse() == 4
3958# ifdef FEAT_GUI
3959 && !gui.in_use
3960# endif
3961 )
3962 {
3963 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003964 ? IF_EB("\233<*M", CSI_STR "<*M")
3965 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3966
3967 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3968 ? IF_EB("\233<*m", CSI_STR "<*m")
3969 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003970
3971 if (*p_mouse != NUL)
3972 {
3973 mch_setmouse(FALSE);
3974 setmouse();
3975 }
3976 }
3977 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003978 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003979 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003980 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3981 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983}
3984#endif
3985
3986/*
3987 * set screen mode, always fails.
3988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003990mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 EMSG(_(e_screenmode));
3993 return FAIL;
3994}
3995
3996#ifndef VMS
3997
3998/*
3999 * Try to get the current window size:
4000 * 1. with an ioctl(), most accurate method
4001 * 2. from the environment variables LINES and COLUMNS
4002 * 3. from the termcap
4003 * 4. keep using the old values
4004 * Return OK when size could be determined, FAIL otherwise.
4005 */
4006 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004007mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
4009 long rows = 0;
4010 long columns = 0;
4011 char_u *p;
4012
4013 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * 1. try using an ioctl. It is the most accurate method.
4015 *
4016 * Try using TIOCGWINSZ first, some systems that have it also define
4017 * TIOCGSIZE but don't have a struct ttysize.
4018 */
4019# ifdef TIOCGWINSZ
4020 {
4021 struct winsize ws;
4022 int fd = 1;
4023
4024 /* When stdout is not a tty, use stdin for the ioctl(). */
4025 if (!isatty(fd) && isatty(read_cmd_fd))
4026 fd = read_cmd_fd;
4027 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4028 {
4029 columns = ws.ws_col;
4030 rows = ws.ws_row;
4031 }
4032 }
4033# else /* TIOCGWINSZ */
4034# ifdef TIOCGSIZE
4035 {
4036 struct ttysize ts;
4037 int fd = 1;
4038
4039 /* When stdout is not a tty, use stdin for the ioctl(). */
4040 if (!isatty(fd) && isatty(read_cmd_fd))
4041 fd = read_cmd_fd;
4042 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4043 {
4044 columns = ts.ts_cols;
4045 rows = ts.ts_lines;
4046 }
4047 }
4048# endif /* TIOCGSIZE */
4049# endif /* TIOCGWINSZ */
4050
4051 /*
4052 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004053 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4054 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004056 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
4058 if ((p = (char_u *)getenv("LINES")))
4059 rows = atoi((char *)p);
4060 if ((p = (char_u *)getenv("COLUMNS")))
4061 columns = atoi((char *)p);
4062 }
4063
4064#ifdef HAVE_TGETENT
4065 /*
4066 * 3. try reading "co" and "li" entries from termcap
4067 */
4068 if (columns == 0 || rows == 0)
4069 getlinecol(&columns, &rows);
4070#endif
4071
4072 /*
4073 * 4. If everything fails, use the old values
4074 */
4075 if (columns <= 0 || rows <= 0)
4076 return FAIL;
4077
4078 Rows = rows;
4079 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004080 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return OK;
4082}
4083
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004084#if defined(FEAT_TERMINAL) || defined(PROTO)
4085/*
4086 * Report the windows size "rows" and "cols" to tty "fd".
4087 */
4088 int
4089mch_report_winsize(int fd, int rows, int cols)
4090{
4091# ifdef TIOCSWINSZ
4092 struct winsize ws;
4093
4094 ws.ws_col = cols;
4095 ws.ws_row = rows;
4096 ws.ws_xpixel = cols * 5;
4097 ws.ws_ypixel = rows * 10;
4098 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4099 {
4100 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4101 return OK;
4102 }
4103 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4104# else
4105# ifdef TIOCSSIZE
4106 struct ttysize ts;
4107
4108 ts.ts_cols = cols;
4109 ts.ts_lines = rows;
4110 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4111 {
4112 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4113 return OK;
4114 }
4115 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4116# endif
4117# endif
4118 return FAIL;
4119}
4120#endif
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122/*
4123 * Try to set the window size to Rows and Columns.
4124 */
4125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004126mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
4128 if (*T_CWS)
4129 {
4130 /*
4131 * NOTE: if you get an error here that term_set_winsize() is
4132 * undefined, check the output of configure. It could probably not
4133 * find a ncurses, termcap or termlib library.
4134 */
4135 term_set_winsize((int)Rows, (int)Columns);
4136 out_flush();
4137 screen_start(); /* don't know where cursor is now */
4138 }
4139}
4140
4141#endif /* VMS */
4142
4143/*
4144 * Rows and/or Columns has changed.
4145 */
4146 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004147mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
4149 /* Nothing to do. */
4150}
4151
Bram Moolenaar205b8862011-09-07 15:04:31 +02004152/*
4153 * Wait for process "child" to end.
4154 * Return "child" if it exited properly, <= 0 on error.
4155 */
4156 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004157wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004158{
4159 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004160 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004161
4162 while (wait_pid != child)
4163 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004164 /* When compiled with Python threads are probably used, in which case
4165 * wait() sometimes hangs for no obvious reason. Use waitpid()
4166 * instead and loop (like the GUI). Also needed for other interfaces,
4167 * they might call system(). */
4168# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004169 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004170# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004171 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004172# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004173 if (wait_pid == 0)
4174 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004175 /* Wait for 1 to 10 msec before trying again. */
4176 mch_delay(delay_msec, TRUE);
4177 if (++delay_msec > 10)
4178 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004179 continue;
4180 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004181 if (wait_pid <= 0
4182# ifdef ECHILD
4183 && errno == ECHILD
4184# endif
4185 )
4186 break;
4187 }
4188 return wait_pid;
4189}
4190
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004191#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004192/*
4193 * Set the environment for a child process.
4194 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004195 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004196set_child_environment(
4197 long rows,
4198 long columns,
4199 char *term,
4200 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004201{
4202# ifdef HAVE_SETENV
4203 char envbuf[50];
4204# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004205 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004206 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004207 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004208 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004209 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004210# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004211 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004212# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004213# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004214 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004215# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004216# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004217 long colors =
4218# ifdef FEAT_GUI
4219 gui.in_use ? 256*256*256 :
4220# endif
4221 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004222
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004223# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004224 setenv("TERM", term, 1);
4225 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004226 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004227 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004228 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004229 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004230 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004231 sprintf((char *)envbuf, "%ld", colors);
4232 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004233# ifdef FEAT_TERMINAL
4234 if (is_terminal)
4235 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004236 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004237 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4238 }
4239# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004240# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004241 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004242# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004243# else
4244 /*
4245 * Putenv does not copy the string, it has to remain valid.
4246 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004247 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004248 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004249 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4250 putenv(envbuf_Term);
4251 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004252 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004253 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4254 putenv(envbuf_Lines);
4255 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4256 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004257 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004258 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4259 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004260# ifdef FEAT_TERMINAL
4261 if (is_terminal)
4262 {
4263 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004264 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004265 putenv(envbuf_Version);
4266 }
4267# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004268# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004269 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4270 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4271 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004272# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004273# endif
4274}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004275
4276 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004277set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004278{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004279 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004280}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004281#endif
4282
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004283#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004284/*
4285 * Open a PTY, with FD for the master and slave side.
4286 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4287 * When successful both file descriptors are stored.
4288 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004289 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004290open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004291{
4292 char *tty_name;
4293
4294 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4295 if (*pty_master_fd >= 0)
4296 {
4297 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4298 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4299 * adding O_NOCTTY always works when defined. */
4300#ifdef O_NOCTTY
4301 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4302#else
4303 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4304#endif
4305 if (*pty_slave_fd < 0)
4306 {
4307 close(*pty_master_fd);
4308 *pty_master_fd = -1;
4309 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004310 else if (namep != NULL)
4311 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004312 }
4313}
4314#endif
4315
Bram Moolenaarfae42832017-08-01 22:24:26 +02004316/*
4317 * Send SIGINT to a child process if "c" is an interrupt character.
4318 */
4319 void
4320may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4321{
4322# ifdef SIGINT
4323 if (c == Ctrl_C || c == intr_char)
4324 {
4325# ifdef HAVE_SETSID
4326 kill(-pid, SIGINT);
4327# else
4328 kill(0, SIGINT);
4329# endif
4330 if (wpid > 0)
4331 kill(wpid, SIGINT);
4332 }
4333# endif
4334}
4335
Bram Moolenaar13568252018-03-16 20:46:58 +01004336#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4337
4338 static int
4339build_argv(
4340 char_u *cmd,
4341 char ***argvp,
4342 char_u **sh_tofree,
4343 char_u **shcf_tofree)
4344{
4345 char **argv = NULL;
4346 int argc;
4347
4348 *sh_tofree = vim_strsave(p_sh);
4349 if (*sh_tofree == NULL) /* out of memory */
4350 return FAIL;
4351
4352 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4353 return FAIL;
4354 *argvp = argv;
4355
4356 if (cmd != NULL)
4357 {
4358 char_u *s;
4359 char_u *p;
4360
4361 if (extra_shell_arg != NULL)
4362 argv[argc++] = (char *)extra_shell_arg;
4363
4364 /* Break 'shellcmdflag' into white separated parts. This doesn't
4365 * handle quoted strings, they are very unlikely to appear. */
4366 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4367 if (*shcf_tofree == NULL) /* out of memory */
4368 return FAIL;
4369 s = *shcf_tofree;
4370 p = p_shcf;
4371 while (*p != NUL)
4372 {
4373 argv[argc++] = (char *)s;
4374 while (*p && *p != ' ' && *p != TAB)
4375 *s++ = *p++;
4376 *s++ = NUL;
4377 p = skipwhite(p);
4378 }
4379
4380 argv[argc++] = (char *)cmd;
4381 }
4382 argv[argc] = NULL;
4383 return OK;
4384}
4385#endif
4386
4387#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4388/*
4389 * Use a terminal window to run a shell command in.
4390 */
4391 static int
4392mch_call_shell_terminal(
4393 char_u *cmd,
4394 int options UNUSED) /* SHELL_*, see vim.h */
4395{
4396 jobopt_T opt;
4397 char **argv = NULL;
4398 char_u *tofree1 = NULL;
4399 char_u *tofree2 = NULL;
4400 int retval = -1;
4401 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004402 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004403 aco_save_T aco;
4404 oparg_T oa; /* operator arguments */
4405
4406 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4407 goto theend;
4408
4409 init_job_options(&opt);
4410 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4411 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004412 if (buf == NULL)
4413 goto theend;
4414
4415 job = term_getjob(buf->b_term);
4416 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004417
4418 /* Find a window to make "buf" curbuf. */
4419 aucmd_prepbuf(&aco, buf);
4420
4421 clear_oparg(&oa);
4422 while (term_use_loop())
4423 {
4424 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4425 {
4426 /* If terminal_loop() returns OK we got a key that is handled
4427 * in Normal model. We don't do redrawing anyway. */
4428 if (terminal_loop(TRUE) == OK)
4429 normal_cmd(&oa, TRUE);
4430 }
4431 else
4432 normal_cmd(&oa, TRUE);
4433 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004434 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004435 ch_log(NULL, "system command finished");
4436
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004437 job_unref(job);
4438
Bram Moolenaar13568252018-03-16 20:46:58 +01004439 /* restore curwin/curbuf and a few other things */
4440 aucmd_restbuf(&aco);
4441
4442 wait_return(TRUE);
4443 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4444
4445theend:
4446 vim_free(argv);
4447 vim_free(tofree1);
4448 vim_free(tofree2);
4449 return retval;
4450}
4451#endif
4452
4453#ifdef USE_SYSTEM
4454/*
4455 * Use system() to start the shell: simple but slow.
4456 */
4457 static int
4458mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004459 char_u *cmd,
4460 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462#ifdef VMS
4463 char *ifn = NULL;
4464 char *ofn = NULL;
4465#endif
4466 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004467 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004468 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 out_flush();
4471
4472 if (options & SHELL_COOKED)
4473 settmode(TMODE_COOK); /* set to normal mode */
4474
Bram Moolenaar62b42182010-09-21 22:09:37 +02004475# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004476 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004477 loose_clipboard();
4478# endif
4479
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (cmd == NULL)
4481 x = system((char *)p_sh);
4482 else
4483 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004484# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (ofn = strchr((char *)cmd, '>'))
4486 *ofn++ = '\0';
4487 if (ifn = strchr((char *)cmd, '<'))
4488 {
4489 char *p;
4490
4491 *ifn++ = '\0';
4492 p = strchr(ifn,' '); /* chop off any trailing spaces */
4493 if (p)
4494 *p = '\0';
4495 }
4496 if (ofn)
4497 x = vms_sys((char *)cmd, ofn, ifn);
4498 else
4499 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004500# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 newcmd = lalloc(STRLEN(p_sh)
4502 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4503 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4504 if (newcmd == NULL)
4505 x = 0;
4506 else
4507 {
4508 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4509 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4510 (char *)p_shcf,
4511 (char *)cmd);
4512 x = system((char *)newcmd);
4513 vim_free(newcmd);
4514 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517# ifdef VMS
4518 x = vms_sys_status(x);
4519# endif
4520 if (emsg_silent)
4521 ;
4522 else if (x == 127)
4523 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else if (x && !(options & SHELL_SILENT))
4525 {
4526 MSG_PUTS(_("\nshell returned "));
4527 msg_outnum((long)x);
4528 msg_putchar('\n');
4529 }
4530
4531 if (tmode == TMODE_RAW)
4532 settmode(TMODE_RAW); /* set to raw mode */
4533# ifdef FEAT_TITLE
4534 resettitle();
4535# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004536# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4537 restore_clipboard();
4538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaar13568252018-03-16 20:46:58 +01004542#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaardf177f62005-02-22 08:39:57 +00004544# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4545 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004546# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Bram Moolenaar13568252018-03-16 20:46:58 +01004548/*
4549 * Don't use system(), use fork()/exec().
4550 */
4551 static int
4552mch_call_shell_fork(
4553 char_u *cmd,
4554 int options) /* SHELL_*, see vim.h */
4555{
4556 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004558 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 pid_t wait_pid = 0;
4560# ifdef HAVE_UNION_WAIT
4561 union wait status;
4562# else
4563 int status = -1;
4564# endif
4565 int retval = -1;
4566 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004567 char_u *tofree1 = NULL;
4568 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004571# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004573# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004574 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 int fd_fromshell[2];
4576 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004577 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 out_flush();
4580 if (options & SHELL_COOKED)
4581 settmode(TMODE_COOK); /* set to normal mode */
4582
Bram Moolenaar13568252018-03-16 20:46:58 +01004583 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004584 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004587 * For the GUI, when writing the output into the buffer and when reading
4588 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4589 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591 if ((options & (SHELL_READ|SHELL_WRITE))
4592# ifdef FEAT_GUI
4593 || (gui.in_use && show_shell_mess)
4594# endif
4595 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004597# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 /*
4599 * Try to open a master pty.
4600 * If this works, open the slave pty.
4601 * If the slave can't be opened, close the master pty.
4602 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004604 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 /*
4606 * If not opening a pty or it didn't work, try using pipes.
4607 */
4608 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 pipe_error = (pipe(fd_toshell) < 0);
4612 if (!pipe_error) /* pipe create OK */
4613 {
4614 pipe_error = (pipe(fd_fromshell) < 0);
4615 if (pipe_error) /* pipe create failed */
4616 {
4617 close(fd_toshell[0]);
4618 close(fd_toshell[1]);
4619 }
4620 }
4621 if (pipe_error)
4622 {
4623 MSG_PUTS(_("\nCannot create pipes\n"));
4624 out_flush();
4625 }
4626 }
4627 }
4628
4629 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004631 SIGSET_DECL(curset)
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633# ifdef __BEOS__
4634 beos_cleanup_read_thread();
4635# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004636
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004637 BLOCK_SIGNALS(&curset);
4638 pid = fork(); /* maybe we should use vfork() */
4639 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004641 UNBLOCK_SIGNALS(&curset);
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004644 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004646 || (gui.in_use && show_shell_mess)
4647# endif
4648 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004650# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (pty_master_fd >= 0) /* close the pseudo tty */
4652 {
4653 close(pty_master_fd);
4654 close(pty_slave_fd);
4655 }
4656 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
4659 close(fd_toshell[0]);
4660 close(fd_toshell[1]);
4661 close(fd_fromshell[0]);
4662 close(fd_fromshell[1]);
4663 }
4664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666 else if (pid == 0) /* child */
4667 {
4668 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004669 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
Bram Moolenaar819524702018-02-27 19:10:00 +01004671# ifdef FEAT_JOB_CHANNEL
4672 if (ch_log_active())
4673 /* close the log file in the child */
4674 ch_logfile((char_u *)"", (char_u *)"");
4675# endif
4676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if (!show_shell_mess || (options & SHELL_EXPAND))
4678 {
4679 int fd;
4680
4681 /*
4682 * Don't want to show any message from the shell. Can't just
4683 * close stdout and stderr though, because some systems will
4684 * break if you try to write to them after that, so we must
4685 * use dup() to replace them with something else -- webb
4686 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4687 * waiting for input.
4688 */
4689 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4690 fclose(stdin);
4691 fclose(stdout);
4692 fclose(stderr);
4693
4694 /*
4695 * If any of these open()'s and dup()'s fail, we just continue
4696 * anyway. It's not fatal, and on most systems it will make
4697 * no difference at all. On a few it will cause the execvp()
4698 * to exit with a non-zero status even when the completion
4699 * could be done, which is nothing too serious. If the open()
4700 * or dup() failed we'd just do the same thing ourselves
4701 * anyway -- webb
4702 */
4703 if (fd >= 0)
4704 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004705 ignored = dup(fd); /* To replace stdin (fd 0) */
4706 ignored = dup(fd); /* To replace stdout (fd 1) */
4707 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 /* Don't need this now that we've duplicated it */
4710 close(fd);
4711 }
4712 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004713 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004715 || gui.in_use
4716# endif
4717 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004721 /* Create our own process group, so that the child and all its
4722 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004723 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004724 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004725 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004726 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004727# if defined(SIGHUP)
4728 /* When doing "!xterm&" and 'shell' is bash: the shell
4729 * will exit and send SIGHUP to all processes in its
4730 * group, killing the just started process. Ignore SIGHUP
4731 * to avoid that. (suggested by Simon Schubert)
4732 */
4733 signal(SIGHUP, SIG_IGN);
4734# endif
4735 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004736# endif
4737# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004738 if (pty_slave_fd >= 0)
4739 {
4740 /* push stream discipline modules */
4741 if (options & SHELL_COOKED)
4742 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004744 /* Try to become controlling tty (probably doesn't work,
4745 * unless run by root) */
4746 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004748 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004749# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004750 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
Bram Moolenaara5792f52005-11-23 21:25:05 +00004752 /*
4753 * stderr is only redirected when using the GUI, so that a
4754 * program like gpg can still access the terminal to get a
4755 * passphrase using stderr.
4756 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004757# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (pty_master_fd >= 0)
4759 {
4760 close(pty_master_fd); /* close master side of pty */
4761
4762 /* set up stdin/stdout/stderr for the child */
4763 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004764 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004766 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004767 if (gui.in_use)
4768 {
4769 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004770 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 close(pty_slave_fd); /* has been dupped, close it now */
4774 }
4775 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 /* set up stdin for the child */
4779 close(fd_toshell[1]);
4780 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004781 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 close(fd_toshell[0]);
4783
4784 /* set up stdout for the child */
4785 close(fd_fromshell[0]);
4786 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004787 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 close(fd_fromshell[1]);
4789
Bram Moolenaara5792f52005-11-23 21:25:05 +00004790# ifdef FEAT_GUI
4791 if (gui.in_use)
4792 {
4793 /* set up stderr for the child */
4794 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004795 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004796 }
4797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 /*
4802 * There is no type cast for the argv, because the type may be
4803 * different on different machines. This may cause a warning
4804 * message with strict compilers, don't worry about it.
4805 * Call _exit() instead of exit() to avoid closing the connection
4806 * to the X server (esp. with GTK, which uses atexit()).
4807 */
4808 execvp(argv[0], argv);
4809 _exit(EXEC_FAILED); /* exec failed, return failure code */
4810 }
4811 else /* parent */
4812 {
4813 /*
4814 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004815 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 */
4817 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004818 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004819 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004820# ifdef FEAT_JOB_CHANNEL
4821 ++dont_check_job_ended;
4822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 /*
4824 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004825 * This is also used to pipe stdin/stdout to/from the external
4826 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004828 if ((options & (SHELL_READ|SHELL_WRITE))
4829# ifdef FEAT_GUI
4830 || (gui.in_use && show_shell_mess)
4831# endif
4832 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004834# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4840 int ta_len = 0; /* valid bytes in ta_buf[] */
4841 int len;
4842 int p_more_save;
4843 int old_State;
4844 int c;
4845 int toshell_fd;
4846 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004847 garray_T ga;
4848 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004849# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4850 struct timeval start_tv;
4851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Bram Moolenaardf177f62005-02-22 08:39:57 +00004853# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (pty_master_fd >= 0)
4855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 fromshell_fd = pty_master_fd;
4857 toshell_fd = dup(pty_master_fd);
4858 }
4859 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 close(fd_toshell[0]);
4863 close(fd_fromshell[1]);
4864 toshell_fd = fd_toshell[1];
4865 fromshell_fd = fd_fromshell[0];
4866 }
4867
4868 /*
4869 * Write to the child if there are typed characters.
4870 * Read from the child if there are characters available.
4871 * Repeat the reading a few times if more characters are
4872 * available. Need to check for typed keys now and then, but
4873 * not too often (delays when no chars are available).
4874 * This loop is quit if no characters can be read from the pty
4875 * (WaitForChar detected special condition), or there are no
4876 * characters available and the child has exited.
4877 * Only check if the child has exited when there is no more
4878 * output. The child may exit before all the output has
4879 * been printed.
4880 *
4881 * Currently this busy loops!
4882 * This can probably dead-lock when the write blocks!
4883 */
4884 p_more_save = p_more;
4885 p_more = FALSE;
4886 old_State = State;
4887 State = EXTERNCMD; /* don't redraw at window resize */
4888
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004889 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004890 {
4891 /* Fork a process that will write the lines to the
4892 * external program. */
4893 if ((wpid = fork()) == -1)
4894 {
4895 MSG_PUTS(_("\nCannot fork\n"));
4896 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004897 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898 {
4899 linenr_T lnum = curbuf->b_op_start.lnum;
4900 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004901 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004902 size_t l;
4903
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004904 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905 for (;;)
4906 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004907 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 if (l == 0)
4909 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004910 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 /* NL -> NUL translation */
4912 len = write(toshell_fd, "", (size_t)1);
4913 else
4914 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004915 char_u *s = vim_strchr(lp + written, NL);
4916
Bram Moolenaar89d40322006-08-29 15:30:07 +00004917 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004918 s == NULL ? l
4919 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004920 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004921 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922 {
4923 /* Finished a line, add a NL, unless this line
4924 * should not have one. */
4925 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004926 || (!curbuf->b_p_bin
4927 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004928 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004929 && (lnum !=
4930 curbuf->b_ml.ml_line_count
4931 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004932 ignored = write(toshell_fd, "\n",
4933 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004934 ++lnum;
4935 if (lnum > curbuf->b_op_end.lnum)
4936 {
4937 /* finished all the lines, close pipe */
4938 close(toshell_fd);
4939 toshell_fd = -1;
4940 break;
4941 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004942 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004943 written = 0;
4944 }
4945 else if (len > 0)
4946 written += len;
4947 }
4948 _exit(0);
4949 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004950 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004951 {
4952 close(toshell_fd);
4953 toshell_fd = -1;
4954 }
4955 }
4956
4957 if (options & SHELL_READ)
4958 ga_init2(&ga, 1, BUFLEN);
4959
4960 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004961# ifdef ELAPSED_FUNC
4962 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 for (;;)
4965 {
4966 /*
4967 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004968 * if there are any.
4969 * Don't do this if we are expanding wild cards (would eat
4970 * typeahead).
4971 * Don't do this when filtering and terminal is in cooked
4972 * mode, the shell command will handle the I/O. Avoids
4973 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004974 * Don't get characters when the child has already
4975 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004976 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004977 * while (noread_cnt > 4), avoids that ":r !ls" eats
4978 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 */
4980 len = 0;
4981 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004982 && ((options &
4983 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4984 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004985# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004986 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004987# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004988 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004989 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004990 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004992 if (ta_len == 0)
4993 {
4994 /* Get extra characters when we don't have any.
4995 * Reset the counter and timer. */
4996 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004997# ifdef ELAPSED_FUNC
4998 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004999# endif
5000 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5001 }
5002 if (ta_len > 0 || len > 0)
5003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /*
5005 * For pipes:
5006 * Check for CTRL-C: send interrupt signal to child.
5007 * Check for CTRL-D: EOF, close pipe to child.
5008 */
5009 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /*
5012 * Send SIGINT to the child's group or all
5013 * processes in our group.
5014 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005015 may_send_sigint(ta_buf[ta_len], pid, wpid);
5016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (pty_master_fd < 0 && toshell_fd >= 0
5018 && ta_buf[ta_len] == Ctrl_D)
5019 {
5020 close(toshell_fd);
5021 toshell_fd = -1;
5022 }
5023 }
5024
5025 /* replace K_BS by <BS> and K_DEL by <DEL> */
5026 for (i = ta_len; i < ta_len + len; ++i)
5027 {
5028 if (ta_buf[i] == CSI && len - i > 2)
5029 {
5030 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5031 if (c == K_DEL || c == K_KDEL || c == K_BS)
5032 {
5033 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5034 (size_t)(len - i - 2));
5035 if (c == K_DEL || c == K_KDEL)
5036 ta_buf[i] = DEL;
5037 else
5038 ta_buf[i] = Ctrl_H;
5039 len -= 2;
5040 }
5041 }
5042 else if (ta_buf[i] == '\r')
5043 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005044# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005046 i += (*mb_ptr2len_len)(ta_buf + i,
5047 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050
5051 /*
5052 * For pipes: echo the typed characters.
5053 * For a pty this does not seem to work.
5054 */
5055 if (pty_master_fd < 0)
5056 {
5057 for (i = ta_len; i < ta_len + len; ++i)
5058 {
5059 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5060 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005061# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else if (has_mbyte)
5063 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005064 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 msg_outtrans_len(ta_buf + i, l);
5067 i += l - 1;
5068 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else
5071 msg_outtrans_len(ta_buf + i, 1);
5072 }
5073 windgoto(msg_row, msg_col);
5074 out_flush();
5075 }
5076
5077 ta_len += len;
5078
5079 /*
5080 * Write the characters to the child, unless EOF has
5081 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005082 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005083 * When writing buffer lines, drop the typed
5084 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005086 if (options & SHELL_WRITE)
5087 ta_len = 0;
5088 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5091 if (len > 0)
5092 {
5093 ta_len -= len;
5094 mch_memmove(ta_buf, ta_buf + len, ta_len);
5095 }
5096 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099
Bram Moolenaardf177f62005-02-22 08:39:57 +00005100 if (got_int)
5101 {
5102 /* CTRL-C sends a signal to the child, we ignore it
5103 * ourselves */
5104# ifdef HAVE_SETSID
5105 kill(-pid, SIGINT);
5106# else
5107 kill(0, SIGINT);
5108# endif
5109 if (wpid > 0)
5110 kill(wpid, SIGINT);
5111 got_int = FALSE;
5112 }
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 /*
5115 * Check if the child has any characters to be printed.
5116 * Read them and write them to our window. Repeat this as
5117 * long as there is something to do, avoid the 10ms wait
5118 * for mch_inchar(), or sending typeahead characters to
5119 * the external process.
5120 * TODO: This should handle escape sequences, compatible
5121 * to some terminal (vt52?).
5122 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005123 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005124 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005126 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005127# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005129# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 );
5133 if (len <= 0) /* end of file or error */
5134 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005135
5136 noread_cnt = 0;
5137 if (options & SHELL_READ)
5138 {
5139 /* Do NUL -> NL translation, append NL separated
5140 * lines to the current buffer. */
5141 for (i = 0; i < len; ++i)
5142 {
5143 if (buffer[i] == NL)
5144 append_ga_line(&ga);
5145 else if (buffer[i] == NUL)
5146 ga_append(&ga, NL);
5147 else
5148 ga_append(&ga, buffer[i]);
5149 }
5150 }
5151# ifdef FEAT_MBYTE
5152 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005155 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaardf177f62005-02-22 08:39:57 +00005157 len += buffer_off;
5158 buffer[len] = NUL;
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 /* Check if the last character in buffer[] is
5161 * incomplete, keep these bytes for the next
5162 * round. */
5163 for (p = buffer; p < buffer + len; p += l)
5164 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005165 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (l == 0)
5167 l = 1; /* NUL byte? */
5168 else if (MB_BYTE2LEN(*p) != l)
5169 break;
5170 }
5171 if (p == buffer) /* no complete character */
5172 {
5173 /* avoid getting stuck at an illegal byte */
5174 if (len >= 12)
5175 ++p;
5176 else
5177 {
5178 buffer_off = len;
5179 continue;
5180 }
5181 }
5182 c = *p;
5183 *p = NUL;
5184 msg_puts(buffer);
5185 if (p < buffer + len)
5186 {
5187 *p = c;
5188 buffer_off = (buffer + len) - p;
5189 mch_memmove(buffer, p, buffer_off);
5190 continue;
5191 }
5192 buffer_off = 0;
5193 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005194# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 buffer[len] = NUL;
5198 msg_puts(buffer);
5199 }
5200
5201 windgoto(msg_row, msg_col);
5202 cursor_on();
5203 out_flush();
5204 if (got_int)
5205 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005206
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005207# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005208 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005209 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005210 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005211
5212 /* Avoid that we keep looping here without
5213 * checking for a CTRL-C for a long time. Don't
5214 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005215 if (msec > 2000)
5216 {
5217 noread_cnt = 5;
5218 break;
5219 }
5220 }
5221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005224 /* If we already detected the child has finished, continue
5225 * reading output for a short while. Some text may be
5226 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005227 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005228 {
5229 if (noread_cnt < 5)
5230 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005231 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005232 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 /*
5235 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005236 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005238# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005239 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005240# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5244 || (wait_pid == pid && WIFEXITED(status)))
5245 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005246 /* Don't break the loop yet, try reading more
5247 * characters from "fromshell_fd" first. When using
5248 * pipes there might still be something to read and
5249 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005252 else
5253 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005254
Bram Moolenaar95a51352013-03-21 22:53:50 +01005255# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005256 /* Handle any X events, e.g. serving the clipboard. */
5257 clip_update();
5258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260finished:
5261 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005262 if (options & SHELL_READ)
5263 {
5264 if (ga.ga_len > 0)
5265 {
5266 append_ga_line(&ga);
5267 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005268 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005269 }
5270 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005271 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005272 ga_clear(&ga);
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /*
5276 * Give all typeahead that wasn't used back to ui_inchar().
5277 */
5278 if (ta_len)
5279 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 State = old_State;
5281 if (toshell_fd >= 0)
5282 close(toshell_fd);
5283 close(fromshell_fd);
5284 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005285# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005286 else
5287 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005288 long delay_msec = 1;
5289
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005290 /*
5291 * Similar to the loop above, but only handle X events, no
5292 * I/O.
5293 */
5294 for (;;)
5295 {
5296 if (got_int)
5297 {
5298 /* CTRL-C sends a signal to the child, we ignore it
5299 * ourselves */
5300# ifdef HAVE_SETSID
5301 kill(-pid, SIGINT);
5302# else
5303 kill(0, SIGINT);
5304# endif
5305 got_int = FALSE;
5306 }
5307# ifdef __NeXT__
5308 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5309# else
5310 wait_pid = waitpid(pid, &status, WNOHANG);
5311# endif
5312 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5313 || (wait_pid == pid && WIFEXITED(status)))
5314 {
5315 wait_pid = pid;
5316 break;
5317 }
5318
5319 /* Handle any X events, e.g. serving the clipboard. */
5320 clip_update();
5321
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005322 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5323 * less time. */
5324 mch_delay(delay_msec, TRUE);
5325 if (++delay_msec > 10)
5326 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005327 }
5328 }
5329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 /*
5332 * Wait until our child has exited.
5333 * Ignore wait() returning pids of other children and returning
5334 * because of some signal like SIGWINCH.
5335 * Don't wait if wait_pid was already set above, indicating the
5336 * child already exited.
5337 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005338 if (wait_pid != pid)
5339 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
Bram Moolenaar624891f2010-10-13 16:22:09 +02005341# ifdef FEAT_GUI
5342 /* Close slave side of pty. Only do this after the child has
5343 * exited, otherwise the child may hang when it tries to write on
5344 * the pty. */
5345 if (pty_master_fd >= 0)
5346 close(pty_slave_fd);
5347# endif
5348
Bram Moolenaardf177f62005-02-22 08:39:57 +00005349 /* Make sure the child that writes to the external program is
5350 * dead. */
5351 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005352 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005353 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005354 wait4pid(wpid, NULL);
5355 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005356
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005357# ifdef FEAT_JOB_CHANNEL
5358 --dont_check_job_ended;
5359# endif
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 /*
5362 * Set to raw mode right now, otherwise a CTRL-C after
5363 * catch_signals() will kill Vim.
5364 */
5365 if (tmode == TMODE_RAW)
5366 settmode(TMODE_RAW);
5367 did_settmode = TRUE;
5368 set_signals();
5369
5370 if (WIFEXITED(status))
5371 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005372 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005374 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
5376 if (retval == EXEC_FAILED)
5377 {
5378 MSG_PUTS(_("\nCannot execute shell "));
5379 msg_outtrans(p_sh);
5380 msg_putchar('\n');
5381 }
5382 else if (!(options & SHELL_SILENT))
5383 {
5384 MSG_PUTS(_("\nshell returned "));
5385 msg_outnum((long)retval);
5386 msg_putchar('\n');
5387 }
5388 }
5389 }
5390 else
5391 MSG_PUTS(_("\nCommand terminated\n"));
5392 }
5393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395error:
5396 if (!did_settmode)
5397 if (tmode == TMODE_RAW)
5398 settmode(TMODE_RAW); /* set to raw mode */
5399# ifdef FEAT_TITLE
5400 resettitle();
5401# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005402 vim_free(argv);
5403 vim_free(tofree1);
5404 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005407}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005409
5410 int
5411mch_call_shell(
5412 char_u *cmd,
5413 int options) /* SHELL_*, see vim.h */
5414{
5415#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5416 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5417 return mch_call_shell_terminal(cmd, options);
5418#endif
5419#ifdef USE_SYSTEM
5420 return mch_call_shell_system(cmd, options);
5421#else
5422 return mch_call_shell_fork(cmd, options);
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424}
5425
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005426#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005427 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005428mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005429{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005430 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005431 int fd_in[2] = {-1, -1}; /* for stdin */
5432 int fd_out[2] = {-1, -1}; /* for stdout */
5433 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005434 int pty_master_fd = -1;
5435 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005436 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005437 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5438 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5439 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005440 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005441 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5442 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005443 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005444 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005445 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005446
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005447 if (use_out_for_err && use_null_for_out)
5448 use_null_for_err = TRUE;
5449
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005450 /* default is to fail */
5451 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005452
Bram Moolenaarb2412082017-08-20 18:09:14 +02005453 if (options->jo_pty
5454 && (!(use_file_for_in || use_null_for_in)
5455 || !(use_file_for_in || use_null_for_out)
5456 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005457 {
5458 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5459 if (job->jv_tty_out != NULL)
5460 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5461 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005462
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005463 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005464 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005465 if (use_file_for_in)
5466 {
5467 char_u *fname = options->jo_io_name[PART_IN];
5468
5469 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5470 if (fd_in[0] < 0)
5471 {
5472 EMSG2(_(e_notopen), fname);
5473 goto failed;
5474 }
5475 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005476 else
5477 /* When writing buffer lines to the input don't use the pty, so that
5478 * the pipe can be closed when all lines were written. */
5479 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5480 && pipe(fd_in) < 0)
5481 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005482
5483 if (use_file_for_out)
5484 {
5485 char_u *fname = options->jo_io_name[PART_OUT];
5486
5487 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5488 if (fd_out[1] < 0)
5489 {
5490 EMSG2(_(e_notopen), fname);
5491 goto failed;
5492 }
5493 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005494 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005495 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005496
5497 if (use_file_for_err)
5498 {
5499 char_u *fname = options->jo_io_name[PART_ERR];
5500
5501 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5502 if (fd_err[1] < 0)
5503 {
5504 EMSG2(_(e_notopen), fname);
5505 goto failed;
5506 }
5507 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005508 else if (!use_out_for_err && !use_null_for_err
5509 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005510 goto failed;
5511
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005512 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5513 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005514 if (options->jo_set & JO_CHANNEL)
5515 {
5516 channel = options->jo_channel;
5517 if (channel != NULL)
5518 ++channel->ch_refcount;
5519 }
5520 else
5521 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005522 if (channel == NULL)
5523 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005524 if (job->jv_tty_out != NULL)
5525 ch_log(channel, "using pty %s on fd %d",
5526 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005527 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005528
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005529 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005530 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005531 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005532 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005533 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005534 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005535 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005536 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005537 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005538 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005539 int null_fd = -1;
5540 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005541
Bram Moolenaar835dc632016-02-07 14:27:38 +01005542 /* child */
5543 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005544 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545
Bram Moolenaar819524702018-02-27 19:10:00 +01005546# ifdef FEAT_JOB_CHANNEL
5547 if (ch_log_active())
5548 /* close the log file in the child */
5549 ch_logfile((char_u *)"", (char_u *)"");
5550# endif
5551
Bram Moolenaar835dc632016-02-07 14:27:38 +01005552# ifdef HAVE_SETSID
5553 /* Create our own process group, so that the child and all its
5554 * children can be kill()ed. Don't do this when using pipes,
5555 * because stdin is not a tty, we would lose /dev/tty. */
5556 (void)setsid();
5557# endif
5558
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005559# ifdef FEAT_TERMINAL
5560 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005561 {
5562 char *term = (char *)T_NAME;
5563
5564#ifdef FEAT_GUI
5565 if (term_is_gui(T_NAME))
5566 /* In the GUI 'term' is not what we want, use $TERM. */
5567 term = getenv("TERM");
5568#endif
5569 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5570 * back to "xterm". */
5571 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5572 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005573 set_child_environment(
5574 (long)options->jo_term_rows,
5575 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005576 term,
5577 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005578 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005579 else
5580# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005581 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005582
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005583 if (options->jo_env != NULL)
5584 {
5585 dict_T *dict = options->jo_env;
5586 hashitem_T *hi;
5587 int todo = (int)dict->dv_hashtab.ht_used;
5588
5589 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5590 if (!HASHITEM_EMPTY(hi))
5591 {
5592 typval_T *item = &dict_lookup(hi)->di_tv;
5593
5594 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5595 --todo;
5596 }
5597 }
5598
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005599 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005600 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005601 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005602 if (null_fd < 0)
5603 {
5604 perror("opening /dev/null failed");
5605 _exit(OPEN_NULL_FAILED);
5606 }
5607 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005608
Bram Moolenaar223896d2017-08-02 22:33:28 +02005609 if (pty_slave_fd >= 0)
5610 {
5611 /* push stream discipline modules */
5612 SetupSlavePTY(pty_slave_fd);
5613# ifdef TIOCSCTTY
5614 /* Try to become controlling tty (probably doesn't work,
5615 * unless run by root) */
5616 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5617# endif
5618 }
5619
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005620 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005621 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005622 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005623 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005624 else if (fd_in[0] < 0)
5625 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005626 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005627 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005628
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005629 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005630 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005631 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005632 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005633 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005634 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005635 }
5636 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005637 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005638 else if (fd_err[1] < 0)
5639 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005640 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005641 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005642
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005643 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005644 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005645 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005646 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005647 else if (fd_out[1] < 0)
5648 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005649 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005650 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005651
5652 if (fd_in[0] >= 0)
5653 close(fd_in[0]);
5654 if (fd_in[1] >= 0)
5655 close(fd_in[1]);
5656 if (fd_out[0] >= 0)
5657 close(fd_out[0]);
5658 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005659 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005660 if (fd_err[0] >= 0)
5661 close(fd_err[0]);
5662 if (fd_err[1] >= 0)
5663 close(fd_err[1]);
5664 if (pty_master_fd >= 0)
5665 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005666 close(pty_master_fd); /* not used in the child */
5667 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005668 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005669
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005670 if (null_fd >= 0)
5671 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005672
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005673 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5674 _exit(EXEC_FAILED);
5675
Bram Moolenaar835dc632016-02-07 14:27:38 +01005676 /* See above for type of argv. */
5677 execvp(argv[0], argv);
5678
Bram Moolenaar4694a172016-04-21 14:05:23 +02005679 if (stderr_works)
5680 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005681# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005682 /* calling free_all_mem() here causes problems. Ignore valgrind
5683 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005684# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005685 _exit(EXEC_FAILED); /* exec failed, return failure code */
5686 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005687
5688 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005689 UNBLOCK_SIGNALS(&curset);
5690
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005691 job->jv_pid = pid;
5692 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005693 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005694
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005695 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005696 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005697 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005698 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005699 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005700 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005701 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005702 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005703 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005704 if (channel != NULL)
5705 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005706 int in_fd = use_file_for_in || use_null_for_in
5707 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5708 int out_fd = use_file_for_out || use_null_for_out
5709 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005710 /* When using pty_master_fd only set it for stdout, do not duplicate it
5711 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005712 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005713 ? INVALID_FD
5714 : fd_err[0] >= 0
5715 ? fd_err[0]
5716 : (out_fd == pty_master_fd
5717 ? INVALID_FD
5718 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005719
5720 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005721 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005722 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005723 else
5724 {
5725 if (fd_in[1] >= 0)
5726 close(fd_in[1]);
5727 if (fd_out[0] >= 0)
5728 close(fd_out[0]);
5729 if (fd_err[0] >= 0)
5730 close(fd_err[0]);
5731 if (pty_master_fd >= 0)
5732 close(pty_master_fd);
5733 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005734
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005735 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005736 return;
5737
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005738failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005739 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005740 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005742 if (fd_in[1] >= 0)
5743 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005744 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005745 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005746 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005747 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005748 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005749 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005750 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005752 if (pty_master_fd >= 0)
5753 close(pty_master_fd);
5754 if (pty_slave_fd >= 0)
5755 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005756}
5757
5758 char *
5759mch_job_status(job_T *job)
5760{
5761# ifdef HAVE_UNION_WAIT
5762 union wait status;
5763# else
5764 int status = -1;
5765# endif
5766 pid_t wait_pid = 0;
5767
5768# ifdef __NeXT__
5769 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5770# else
5771 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5772# endif
5773 if (wait_pid == -1)
5774 {
5775 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005776 if (job->jv_status < JOB_ENDED)
5777 ch_log(job->jv_channel, "Job no longer exists: %s",
5778 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005779 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005780 }
5781 if (wait_pid == 0)
5782 return "run";
5783 if (WIFEXITED(status))
5784 {
5785 /* LINTED avoid "bitwise operation on signed value" */
5786 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005787 if (job->jv_status < JOB_ENDED)
5788 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005789 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005790 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005791 if (WIFSIGNALED(status))
5792 {
5793 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005794 if (job->jv_status < JOB_ENDED)
5795 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005796 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005797 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005798 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005799
5800return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005801 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005802 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005803 return "dead";
5804}
5805
5806 job_T *
5807mch_detect_ended_job(job_T *job_list)
5808{
5809# ifdef HAVE_UNION_WAIT
5810 union wait status;
5811# else
5812 int status = -1;
5813# endif
5814 pid_t wait_pid = 0;
5815 job_T *job;
5816
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005817# ifndef USE_SYSTEM
5818 /* Do not do this when waiting for a shell command to finish, we would get
5819 * the exit value here (and discard it), the exit value obtained there
5820 * would then be wrong. */
5821 if (dont_check_job_ended > 0)
5822 return NULL;
5823# endif
5824
Bram Moolenaar97792de2016-10-15 18:36:49 +02005825# ifdef __NeXT__
5826 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5827# else
5828 wait_pid = waitpid(-1, &status, WNOHANG);
5829# endif
5830 if (wait_pid <= 0)
5831 /* no process ended */
5832 return NULL;
5833 for (job = job_list; job != NULL; job = job->jv_next)
5834 {
5835 if (job->jv_pid == wait_pid)
5836 {
5837 if (WIFEXITED(status))
5838 /* LINTED avoid "bitwise operation on signed value" */
5839 job->jv_exitval = WEXITSTATUS(status);
5840 else if (WIFSIGNALED(status))
5841 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005842 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005843 {
5844 ch_log(job->jv_channel, "Job ended");
5845 job->jv_status = JOB_ENDED;
5846 }
5847 return job;
5848 }
5849 }
5850 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851}
5852
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005853/*
5854 * Send a (deadly) signal to "job".
5855 * Return FAIL if "how" is not a valid name.
5856 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005857 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005858mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005859{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005860 int sig = -1;
5861 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005862
Bram Moolenaar923d9262016-02-25 20:56:01 +01005863 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005864 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005865 else if (STRCMP(how, "hup") == 0)
5866 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867 else if (STRCMP(how, "quit") == 0)
5868 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005869 else if (STRCMP(how, "int") == 0)
5870 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005871 else if (STRCMP(how, "kill") == 0)
5872 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005873#ifdef SIGWINCH
5874 else if (STRCMP(how, "winch") == 0)
5875 sig = SIGWINCH;
5876#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005877 else if (isdigit(*how))
5878 sig = atoi((char *)how);
5879 else
5880 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881
Bram Moolenaar72801402016-02-09 11:37:50 +01005882 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005883 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005884#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005885 if (job_pid == getpgid(job_pid))
5886 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005887#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888
Bram Moolenaar61a66052017-07-22 18:39:00 +02005889 /* Never kill ourselves! */
5890 if (job_pid != 0)
5891 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005892
Bram Moolenaar835dc632016-02-07 14:27:38 +01005893 return OK;
5894}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005895
5896/*
5897 * Clear the data related to "job".
5898 */
5899 void
5900mch_clear_job(job_T *job)
5901{
5902 /* call waitpid because child process may become zombie */
5903# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005904 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005905# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005906 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005907# endif
5908}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005909#endif
5910
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005911#if defined(FEAT_TERMINAL) || defined(PROTO)
5912 int
5913mch_create_pty_channel(job_T *job, jobopt_T *options)
5914{
5915 int pty_master_fd = -1;
5916 int pty_slave_fd = -1;
5917 channel_T *channel;
5918
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005919 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5920 if (job->jv_tty_out != NULL)
5921 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005922 close(pty_slave_fd);
5923
5924 channel = add_channel();
5925 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005926 {
5927 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005928 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005929 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005930 if (job->jv_tty_out != NULL)
5931 ch_log(channel, "using pty %s on fd %d",
5932 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005933 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5934 channel->ch_keep_open = TRUE;
5935
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005936 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5937 * it only needs to be read once. */
5938 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005939 channel_set_job(channel, job, options);
5940 return OK;
5941}
5942#endif
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944/*
5945 * Check for CTRL-C typed by reading all available characters.
5946 * In cooked mode we should get SIGINT, no need to check.
5947 */
5948 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005949mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005951 if ((curr_tmode == TMODE_RAW || force)
5952 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 fill_input_buf(FALSE);
5954}
5955
5956/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005957 * Wait "msec" msec until a character is available from the mouse, keyboard,
5958 * from inbuf[].
5959 * "msec" == -1 will block forever.
5960 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005961 * When "ignore_input" is TRUE even check for pending input when input is
5962 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005963 * "interrupted" (if not NULL) is set to TRUE when no character is available
5964 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005965 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005966 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
5968 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005969WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005971#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005972 return ui_wait_for_chars_or_timer(
5973 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005974#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005975 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005976#endif
5977}
5978
5979/*
5980 * Wait "msec" msec until a character is available from the mouse or keyboard
5981 * or from inbuf[].
5982 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005983 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005984 * "interrupted" (if not NULL) is set to TRUE when no character is available
5985 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005986 * When a GUI is being used, this will never get called -- webb
5987 */
5988 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005989WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#ifdef FEAT_MOUSE_GPM
5992 int gpm_process_wanted;
5993#endif
5994#ifdef FEAT_XCLIPBOARD
5995 int rest;
5996#endif
5997 int avail;
5998
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005999 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 return 1;
6001
6002#if defined(FEAT_MOUSE_DEC)
6003 /* May need to query the mouse position. */
6004 if (WantQueryMouse)
6005 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006006 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
6008 }
6009#endif
6010
6011 /*
6012 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6013 * events. This is a bit complicated, because they might both be defined.
6014 */
6015#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6016# ifdef FEAT_XCLIPBOARD
6017 rest = 0;
6018 if (do_xterm_trace())
6019 rest = msec;
6020# endif
6021 do
6022 {
6023# ifdef FEAT_XCLIPBOARD
6024 if (rest != 0)
6025 {
6026 msec = XT_TRACE_DELAY;
6027 if (rest >= 0 && rest < XT_TRACE_DELAY)
6028 msec = rest;
6029 if (rest >= 0)
6030 rest -= msec;
6031 }
6032# endif
6033# ifdef FEAT_MOUSE_GPM
6034 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006035 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006036 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006038 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039# endif
6040 if (!avail)
6041 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006042 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 return 1;
6044# ifdef FEAT_XCLIPBOARD
6045 if (rest == 0 || !do_xterm_trace())
6046# endif
6047 break;
6048 }
6049 }
6050 while (FALSE
6051# ifdef FEAT_MOUSE_GPM
6052 || (gpm_process_wanted && mch_gpm_process() == 0)
6053# endif
6054# ifdef FEAT_XCLIPBOARD
6055 || (!avail && rest != 0)
6056# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006057 )
6058 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006061 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
6063 return avail;
6064}
6065
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006066#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067/*
6068 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006069 * "msec" == 0 will check for characters once.
6070 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006073 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006074 * "interrupted" (if not NULL) is set to TRUE when no character is available
6075 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077#if defined(__BEOS__)
6078 int
6079#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006080 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006082RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006085 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006086#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 static int busy = FALSE;
6088
6089 /* May retry getting characters after an event was handled. */
6090# define MAY_LOOP
6091
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006092# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 /* Remember at what time we started, so that we know how much longer we
6094 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006095 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006096 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006098 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006099 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100# endif
6101
6102 /* Handle being called recursively. This may happen for the session
6103 * manager stuff, it may save the file, which does a breakcheck. */
6104 if (busy)
6105 return 0;
6106#endif
6107
6108#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006109 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110#endif
6111 {
6112#ifdef MAY_LOOP
6113 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006114# ifdef FEAT_MZSCHEME
6115 int mzquantum_used = FALSE;
6116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#endif
6118#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006119 /* each channel may use in, out and err */
6120 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 int nfd;
6122# ifdef FEAT_XCLIPBOARD
6123 int xterm_idx = -1;
6124# endif
6125# ifdef FEAT_MOUSE_GPM
6126 int gpm_idx = -1;
6127# endif
6128# ifdef USE_XSMP
6129 int xsmp_idx = -1;
6130# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006131 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006133# ifdef FEAT_MZSCHEME
6134 mzvim_check_threads();
6135 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6136 {
6137 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6138 mzquantum_used = TRUE;
6139 }
6140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 fds[0].fd = fd;
6142 fds[0].events = POLLIN;
6143 nfd = 1;
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006146 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 if (xterm_Shell != (Widget)0)
6148 {
6149 xterm_idx = nfd;
6150 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6151 fds[nfd].events = POLLIN;
6152 nfd++;
6153 }
6154# endif
6155# ifdef FEAT_MOUSE_GPM
6156 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6157 {
6158 gpm_idx = nfd;
6159 fds[nfd].fd = gpm_fd;
6160 fds[nfd].events = POLLIN;
6161 nfd++;
6162 }
6163# endif
6164# ifdef USE_XSMP
6165 if (xsmp_icefd != -1)
6166 {
6167 xsmp_idx = nfd;
6168 fds[nfd].fd = xsmp_icefd;
6169 fds[nfd].events = POLLIN;
6170 nfd++;
6171 }
6172# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006173#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006174 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006175#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006176 if (interrupted != NULL)
6177 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006179 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006180
6181 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006182 if (result == 0 && interrupted != NULL && ret > 0)
6183 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006184
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006185# ifdef FEAT_MZSCHEME
6186 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006187 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006188 finished = FALSE;
6189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191# ifdef FEAT_XCLIPBOARD
6192 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6193 {
6194 xterm_update(); /* Maybe we should hand out clipboard */
6195 if (--ret == 0 && !input_available())
6196 /* Try again */
6197 finished = FALSE;
6198 }
6199# endif
6200# ifdef FEAT_MOUSE_GPM
6201 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6202 {
6203 *check_for_gpm = 1;
6204 }
6205# endif
6206# ifdef USE_XSMP
6207 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6208 {
6209 if (fds[xsmp_idx].revents & POLLIN)
6210 {
6211 busy = TRUE;
6212 xsmp_handle_requests();
6213 busy = FALSE;
6214 }
6215 else if (fds[xsmp_idx].revents & POLLHUP)
6216 {
6217 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006218 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 xsmp_close();
6220 }
6221 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006222 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006225#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006226 /* also call when ret == 0, we may be polling a keep-open channel */
6227 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006228 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231#else /* HAVE_SELECT */
6232
6233 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006234 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006235 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006239# ifdef FEAT_MZSCHEME
6240 mzvim_check_threads();
6241 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6242 {
6243 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6244 mzquantum_used = TRUE;
6245 }
6246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006248 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006250 tv.tv_sec = towait / 1000;
6251 tv.tv_usec = (towait % 1000) * (1000000/1000);
6252 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006254 else
6255 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 /*
6258 * Select on ready for reading and exceptional condition (end of file).
6259 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006260select_eintr:
6261 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006262 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 FD_ZERO(&efds);
6264 FD_SET(fd, &rfds);
6265# if !defined(__QNX__) && !defined(__CYGWIN32__)
6266 /* For QNX select() always returns 1 if this is set. Why? */
6267 FD_SET(fd, &efds);
6268# endif
6269 maxfd = fd;
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006272 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (xterm_Shell != (Widget)0)
6274 {
6275 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6276 if (maxfd < ConnectionNumber(xterm_dpy))
6277 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006278
6279 /* An event may have already been read but not handled. In
6280 * particulary, XFlush may cause this. */
6281 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 }
6283# endif
6284# ifdef FEAT_MOUSE_GPM
6285 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6286 {
6287 FD_SET(gpm_fd, &rfds);
6288 FD_SET(gpm_fd, &efds);
6289 if (maxfd < gpm_fd)
6290 maxfd = gpm_fd;
6291 }
6292# endif
6293# ifdef USE_XSMP
6294 if (xsmp_icefd != -1)
6295 {
6296 FD_SET(xsmp_icefd, &rfds);
6297 FD_SET(xsmp_icefd, &efds);
6298 if (maxfd < xsmp_icefd)
6299 maxfd = xsmp_icefd;
6300 }
6301# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006302# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006303 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006304# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006305 if (interrupted != NULL)
6306 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006308 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006309 result = ret > 0 && FD_ISSET(fd, &rfds);
6310 if (result)
6311 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006312 else if (interrupted != NULL && ret > 0)
6313 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006314
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006315# ifdef EINTR
6316 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006317 {
6318 /* Check whether window has been resized, EINTR may be caused by
6319 * SIGWINCH. */
6320 if (do_resize)
6321 handle_resize();
6322
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006323 /* Interrupted by a signal, need to try again. We ignore msec
6324 * here, because we do want to check even after a timeout if
6325 * characters are available. Needed for reading output of an
6326 * external command after the process has finished. */
6327 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006328 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006329# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006330# ifdef __TANDEM
6331 if (ret == -1 && errno == ENOTSUP)
6332 {
6333 FD_ZERO(&rfds);
6334 FD_ZERO(&efds);
6335 ret = 0;
6336 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006337# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006338# ifdef FEAT_MZSCHEME
6339 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006340 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006341 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342# endif
6343
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344# ifdef FEAT_XCLIPBOARD
6345 if (ret > 0 && xterm_Shell != (Widget)0
6346 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6347 {
6348 xterm_update(); /* Maybe we should hand out clipboard */
6349 /* continue looping when we only got the X event and the input
6350 * buffer is empty */
6351 if (--ret == 0 && !input_available())
6352 {
6353 /* Try again */
6354 finished = FALSE;
6355 }
6356 }
6357# endif
6358# ifdef FEAT_MOUSE_GPM
6359 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6360 {
6361 if (FD_ISSET(gpm_fd, &efds))
6362 gpm_close();
6363 else if (FD_ISSET(gpm_fd, &rfds))
6364 *check_for_gpm = 1;
6365 }
6366# endif
6367# ifdef USE_XSMP
6368 if (ret > 0 && xsmp_icefd != -1)
6369 {
6370 if (FD_ISSET(xsmp_icefd, &efds))
6371 {
6372 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006373 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 xsmp_close();
6375 if (--ret == 0)
6376 finished = FALSE; /* keep going if event was only one */
6377 }
6378 else if (FD_ISSET(xsmp_icefd, &rfds))
6379 {
6380 busy = TRUE;
6381 xsmp_handle_requests();
6382 busy = FALSE;
6383 if (--ret == 0)
6384 finished = FALSE; /* keep going if event was only one */
6385 }
6386 }
6387# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006388#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006389 /* also call when ret == 0, we may be polling a keep-open channel */
6390 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006391 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394#endif /* HAVE_SELECT */
6395
6396#ifdef MAY_LOOP
6397 if (finished || msec == 0)
6398 break;
6399
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006400# ifdef FEAT_CLIENTSERVER
6401 if (server_waiting())
6402 break;
6403# endif
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 /* We're going to loop around again, find out for how long */
6406 if (msec > 0)
6407 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006408# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006410 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411# else
6412 /* Guess we got interrupted halfway. */
6413 msec = msec / 2;
6414# endif
6415 if (msec <= 0)
6416 break; /* waited long enough */
6417 }
6418#endif
6419 }
6420
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006421 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422}
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006426 * Expand a path into all matching files and/or directories. Handles "*",
6427 * "?", "[a-z]", "**", etc.
6428 * "path" has backslashes before chars that are not to be expanded.
6429 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 */
6431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006432mch_expandpath(
6433 garray_T *gap,
6434 char_u *path,
6435 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
Bram Moolenaar02743632005-07-25 20:42:36 +00006437 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438}
6439#endif
6440
6441/*
6442 * mch_expand_wildcards() - this code does wild-card pattern matching using
6443 * the shell
6444 *
6445 * return OK for success, FAIL for error (you may lose some memory) and put
6446 * an error message in *file.
6447 *
6448 * num_pat is number of input patterns
6449 * pat is array of pointers to input patterns
6450 * num_file is pointer to number of matched file names
6451 * file is pointer to array of pointers to matched file names
6452 */
6453
6454#ifndef SEEK_SET
6455# define SEEK_SET 0
6456#endif
6457#ifndef SEEK_END
6458# define SEEK_END 2
6459#endif
6460
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006461#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006464mch_expand_wildcards(
6465 int num_pat,
6466 char_u **pat,
6467 int *num_file,
6468 char_u ***file,
6469 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 int i;
6472 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006473 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 char_u *p;
6475 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
Bram Moolenaarc7247912008-01-13 12:54:11 +00006477 /*
6478 * This is the non-OS/2 implementation (really Unix).
6479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 int j;
6481 char_u *tempname;
6482 char_u *command;
6483 FILE *fd;
6484 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006485#define STYLE_ECHO 0 /* use "echo", the default */
6486#define STYLE_GLOB 1 /* use "glob", for csh */
6487#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6488#define STYLE_PRINT 3 /* use "print -N", for zsh */
6489#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6490 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 int shell_style = STYLE_ECHO;
6492 int check_spaces;
6493 static int did_find_nul = FALSE;
6494 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006495 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006496 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 *num_file = 0; /* default: no files found */
6499 *file = NULL;
6500
6501 /*
6502 * If there are no wildcards, just copy the names to allocated memory.
6503 * Saves a lot of time, because we don't have to start a new shell.
6504 */
6505 if (!have_wildcard(num_pat, pat))
6506 return save_patterns(num_pat, pat, num_file, file);
6507
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006508# ifdef HAVE_SANDBOX
6509 /* Don't allow any shell command in the sandbox. */
6510 if (sandbox != 0 && check_secure())
6511 return FAIL;
6512# endif
6513
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 /*
6515 * Don't allow the use of backticks in secure and restricted mode.
6516 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006517 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 for (i = 0; i < num_pat; ++i)
6519 if (vim_strchr(pat[i], '`') != NULL
6520 && (check_restricted() || check_secure()))
6521 return FAIL;
6522
6523 /*
6524 * get a name for the temp file
6525 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006526 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
6528 EMSG(_(e_notmp));
6529 return FAIL;
6530 }
6531
6532 /*
6533 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006534 * file.
6535 * STYLE_BT: NL separated
6536 * If expanding `cmd` execute it directly.
6537 * STYLE_GLOB: NUL separated
6538 * If we use *csh, "glob" will work better than "echo".
6539 * STYLE_PRINT: NL or NUL separated
6540 * If we use *zsh, "print -N" will work better than "glob".
6541 * STYLE_VIMGLOB: NL separated
6542 * If we use *sh*, we define "vimglob()".
6543 * STYLE_ECHO: space separated.
6544 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 */
6546 if (num_pat == 1 && *pat[0] == '`'
6547 && (len = STRLEN(pat[0])) > 2
6548 && *(pat[0] + len - 1) == '`')
6549 shell_style = STYLE_BT;
6550 else if ((len = STRLEN(p_sh)) >= 3)
6551 {
6552 if (STRCMP(p_sh + len - 3, "csh") == 0)
6553 shell_style = STYLE_GLOB;
6554 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6555 shell_style = STYLE_PRINT;
6556 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006557 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6558 "sh") != NULL)
6559 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaarc7247912008-01-13 12:54:11 +00006561 /* Compute the length of the command. We need 2 extra bytes: for the
6562 * optional '&' and for the NUL.
6563 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006565 if (shell_style == STYLE_VIMGLOB)
6566 len += STRLEN(sh_vimglob_func);
6567
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006568 for (i = 0; i < num_pat; ++i)
6569 {
6570 /* Count the length of the patterns in the same way as they are put in
6571 * "command" below. */
6572#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006574#else
6575 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006576 for (j = 0; pat[i][j] != NUL; ++j)
6577 {
6578 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6579 ++len; /* may add a backslash */
6580 ++len;
6581 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006582#endif
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 command = alloc(len);
6585 if (command == NULL)
6586 {
6587 /* out of memory */
6588 vim_free(tempname);
6589 return FAIL;
6590 }
6591
6592 /*
6593 * Build the shell command:
6594 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6595 * recognizes this).
6596 * - Add the shell command to print the expanded names.
6597 * - Add the temp file name.
6598 * - Add the file name patterns.
6599 */
6600 if (shell_style == STYLE_BT)
6601 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006602 /* change `command; command& ` to (command; command ) */
6603 STRCPY(command, "(");
6604 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006606 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006607 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 --p;
6609 if (*p == '&') /* remove trailing '&' */
6610 {
6611 ampersent = TRUE;
6612 *p = ' ';
6613 }
6614 STRCAT(command, ">");
6615 }
6616 else
6617 {
6618 if (flags & EW_NOTFOUND)
6619 STRCPY(command, "set nonomatch; ");
6620 else
6621 STRCPY(command, "unset nonomatch; ");
6622 if (shell_style == STYLE_GLOB)
6623 STRCAT(command, "glob >");
6624 else if (shell_style == STYLE_PRINT)
6625 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006626 else if (shell_style == STYLE_VIMGLOB)
6627 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 else
6629 STRCAT(command, "echo >");
6630 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006633
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (shell_style != STYLE_BT)
6635 for (i = 0; i < num_pat; ++i)
6636 {
6637 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006638 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006639 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640#ifdef USE_SYSTEM
6641 STRCAT(command, " \"");
6642 STRCAT(command, pat[i]);
6643 STRCAT(command, "\"");
6644#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006645 int intick = FALSE;
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 p = command + STRLEN(command);
6648 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006649 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006650 {
6651 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006652 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006653 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6654 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006655 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006656 * backslash inside backticks, before a special character
6657 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006658 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006659 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6660 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006661 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006662 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006663 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006664 else if (!intick
6665 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6666 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006667 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006668 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6669 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006670 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006671
6672 /* Copy one character. */
6673 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 *p = NUL;
6676#endif
6677 }
6678 if (flags & EW_SILENT)
6679 show_shell_mess = FALSE;
6680 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006681 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 /*
6684 * Using zsh -G: If a pattern has no matches, it is just deleted from
6685 * the argument list, otherwise zsh gives an error message and doesn't
6686 * expand any other pattern.
6687 */
6688 if (shell_style == STYLE_PRINT)
6689 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6690
6691 /*
6692 * If we use -f then shell variables set in .cshrc won't get expanded.
6693 * vi can do it, so we will too, but it is only necessary if there is a "$"
6694 * in one of the patterns, otherwise we can still use the fast option.
6695 */
6696 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6697 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6698
6699 /*
6700 * execute the shell command
6701 */
6702 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6703
6704 /* When running in the background, give it some time to create the temp
6705 * file, but don't wait for it to finish. */
6706 if (ampersent)
6707 mch_delay(10L, TRUE);
6708
6709 extra_shell_arg = NULL; /* cleanup */
6710 show_shell_mess = TRUE;
6711 vim_free(command);
6712
Bram Moolenaarc7247912008-01-13 12:54:11 +00006713 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 mch_remove(tempname);
6716 vim_free(tempname);
6717 /*
6718 * With interactive completion, the error message is not printed.
6719 * However with USE_SYSTEM, I don't know how to turn off error messages
6720 * from the shell, so screen may still get messed up -- webb.
6721 */
6722#ifndef USE_SYSTEM
6723 if (!(flags & EW_SILENT))
6724#endif
6725 {
6726 redraw_later_clear(); /* probably messed up screen */
6727 msg_putchar('\n'); /* clear bottom line quickly */
6728 cmdline_row = Rows - 1; /* continue on last line */
6729#ifdef USE_SYSTEM
6730 if (!(flags & EW_SILENT))
6731#endif
6732 {
6733 MSG(_(e_wildexpand));
6734 msg_start(); /* don't overwrite this message */
6735 }
6736 }
6737 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6738 * EW_NOTFOUND is given */
6739 if (shell_style == STYLE_BT)
6740 return FAIL;
6741 goto notfound;
6742 }
6743
6744 /*
6745 * read the names from the file into memory
6746 */
6747 fd = fopen((char *)tempname, READBIN);
6748 if (fd == NULL)
6749 {
6750 /* Something went wrong, perhaps a file name with a special char. */
6751 if (!(flags & EW_SILENT))
6752 {
6753 MSG(_(e_wildexpand));
6754 msg_start(); /* don't overwrite this message */
6755 }
6756 vim_free(tempname);
6757 goto notfound;
6758 }
6759 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006760 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006762 if (llen < 0)
6763 /* just in case ftell() would fail */
6764 buffer = NULL;
6765 else
6766 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (buffer == NULL)
6768 {
6769 /* out of memory */
6770 mch_remove(tempname);
6771 vim_free(tempname);
6772 fclose(fd);
6773 return FAIL;
6774 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006775 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 i = fread((char *)buffer, 1, len, fd);
6777 fclose(fd);
6778 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006779 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 /* unexpected read error */
6782 EMSG2(_(e_notread), tempname);
6783 vim_free(tempname);
6784 vim_free(buffer);
6785 return FAIL;
6786 }
6787 vim_free(tempname);
6788
Bram Moolenaarc7247912008-01-13 12:54:11 +00006789# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6791 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006792 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6794 *p++ = buffer[i];
6795 len = p - buffer;
6796# endif
6797
6798
6799 /* file names are separated with Space */
6800 if (shell_style == STYLE_ECHO)
6801 {
6802 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6803 p = buffer;
6804 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6805 {
6806 while (*p != ' ' && *p != '\n')
6807 ++p;
6808 p = skipwhite(p); /* skip to next entry */
6809 }
6810 }
6811 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006812 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
6814 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6815 p = buffer;
6816 for (i = 0; *p != NUL; ++i) /* count number of entries */
6817 {
6818 while (*p != '\n' && *p != NUL)
6819 ++p;
6820 if (*p != NUL)
6821 ++p;
6822 p = skipwhite(p); /* skip leading white space */
6823 }
6824 }
6825 /* file names are separated with NUL */
6826 else
6827 {
6828 /*
6829 * Some versions of zsh use spaces instead of NULs to separate
6830 * results. Only do this when there is no NUL before the end of the
6831 * buffer, otherwise we would never be able to use file names with
6832 * embedded spaces when zsh does use NULs.
6833 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6834 * don't check for spaces again.
6835 */
6836 check_spaces = FALSE;
6837 if (shell_style == STYLE_PRINT && !did_find_nul)
6838 {
6839 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006840 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006841 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 did_find_nul = TRUE;
6843 else
6844 check_spaces = TRUE;
6845 }
6846
6847 /*
6848 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6849 * already is one, for STYLE_GLOB it needs to be added.
6850 */
6851 if (len && buffer[len - 1] == NUL)
6852 --len;
6853 else
6854 buffer[len] = NUL;
6855 i = 0;
6856 for (p = buffer; p < buffer + len; ++p)
6857 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6858 {
6859 ++i;
6860 *p = NUL;
6861 }
6862 if (len)
6863 ++i; /* count last entry */
6864 }
6865 if (i == 0)
6866 {
6867 /*
6868 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6869 * /bin/sh will happily expand it to nothing rather than returning an
6870 * error; and hey, it's good to check anyway -- webb.
6871 */
6872 vim_free(buffer);
6873 goto notfound;
6874 }
6875 *num_file = i;
6876 *file = (char_u **)alloc(sizeof(char_u *) * i);
6877 if (*file == NULL)
6878 {
6879 /* out of memory */
6880 vim_free(buffer);
6881 return FAIL;
6882 }
6883
6884 /*
6885 * Isolate the individual file names.
6886 */
6887 p = buffer;
6888 for (i = 0; i < *num_file; ++i)
6889 {
6890 (*file)[i] = p;
6891 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006892 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6893 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006895 while (!(shell_style == STYLE_ECHO && *p == ' ')
6896 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 ++p;
6898 if (p == buffer + len) /* last entry */
6899 *p = NUL;
6900 else
6901 {
6902 *p++ = NUL;
6903 p = skipwhite(p); /* skip to next entry */
6904 }
6905 }
6906 else /* NUL separates */
6907 {
6908 while (*p && p < buffer + len) /* skip entry */
6909 ++p;
6910 ++p; /* skip NUL */
6911 }
6912 }
6913
6914 /*
6915 * Move the file names to allocated memory.
6916 */
6917 for (j = 0, i = 0; i < *num_file; ++i)
6918 {
6919 /* Require the files to exist. Helps when using /bin/sh */
6920 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6921 continue;
6922
6923 /* check if this entry should be included */
6924 dir = (mch_isdir((*file)[i]));
6925 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6926 continue;
6927
Bram Moolenaara2031822006-03-07 22:29:51 +00006928 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006929 if (!dir && (flags & EW_EXEC)
6930 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006931 continue;
6932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6934 if (p)
6935 {
6936 STRCPY(p, (*file)[i]);
6937 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006938 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 (*file)[j++] = p;
6940 }
6941 }
6942 vim_free(buffer);
6943 *num_file = j;
6944
6945 if (*num_file == 0) /* rejected all entries */
6946 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006947 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 goto notfound;
6949 }
6950
6951 return OK;
6952
6953notfound:
6954 if (flags & EW_NOTFOUND)
6955 return save_patterns(num_pat, pat, num_file, file);
6956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957}
6958
6959#endif /* VMS */
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006962save_patterns(
6963 int num_pat,
6964 char_u **pat,
6965 int *num_file,
6966 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006969 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6972 if (*file == NULL)
6973 return FAIL;
6974 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006975 {
6976 s = vim_strsave(pat[i]);
6977 if (s != NULL)
6978 /* Be compatible with expand_filename(): halve the number of
6979 * backslashes. */
6980 backslash_halve(s);
6981 (*file)[i] = s;
6982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 *num_file = num_pat;
6984 return OK;
6985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987/*
6988 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6989 * expand.
6990 */
6991 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006992mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006994 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (*p == '\\' && p[1] != NUL)
6997 ++p;
6998 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (vim_strchr((char_u *)
7000#ifdef VMS
7001 "*?%"
7002#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#endif
7005 , *p) != NULL)
7006 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
7008 return FALSE;
7009}
7010
7011/*
7012 * Return TRUE if the string "p" contains a wildcard.
7013 * Don't recognize '~' at the end as a wildcard.
7014 */
7015 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007016mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007018 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (*p == '\\' && p[1] != NUL)
7021 ++p;
7022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (vim_strchr((char_u *)
7024#ifdef VMS
7025 "*?%$"
7026#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#endif
7029 , *p) != NULL
7030 || (*p == '~' && p[1] != NUL))
7031 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 return FALSE;
7034}
7035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007037have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 int i;
7040
7041 for (i = 0; i < num; i++)
7042 if (mch_has_wildcard(file[i]))
7043 return 1;
7044 return 0;
7045}
7046
7047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007048have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
7050 int i;
7051
7052 for (i = 0; i < num; i++)
7053 if (vim_strchr(file[i], '$') != NULL)
7054 return TRUE;
7055 return FALSE;
7056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007058#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059/*
7060 * Scaled-down version of rename(), which is missing in Xenix.
7061 * This version can only move regular files and will fail if the
7062 * destination exists.
7063 */
7064 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007065mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 struct stat st;
7068
7069 if (stat(dest, &st) >= 0) /* fail if destination exists */
7070 return -1;
7071 if (link(src, dest) != 0) /* link file to new name */
7072 return -1;
7073 if (mch_remove(src) == 0) /* delete link to old name */
7074 return 0;
7075 return -1;
7076}
7077#endif /* !HAVE_RENAME */
7078
7079#ifdef FEAT_MOUSE_GPM
7080/*
7081 * Initializes connection with gpm (if it isn't already opened)
7082 * Return 1 if succeeded (or connection already opened), 0 if failed
7083 */
7084 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007085gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7088
7089 if (!gpm_flag)
7090 {
7091 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7092 gpm_connect.defaultMask = ~GPM_HARD;
7093 /* Default handling for mouse move*/
7094 gpm_connect.minMod = 0; /* Handle any modifier keys */
7095 gpm_connect.maxMod = 0xffff;
7096 if (Gpm_Open(&gpm_connect, 0) > 0)
7097 {
7098 /* gpm library tries to handling TSTP causes
7099 * problems. Anyways, we close connection to Gpm whenever
7100 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007101 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007103# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 return 1; /* succeed */
7107 }
7108 if (gpm_fd == -2)
7109 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7110 return 0;
7111 }
7112 return 1; /* already open */
7113}
7114
7115/*
7116 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
7118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007119gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 if (gpm_flag && gpm_fd >= 0) /* if Open */
7122 Gpm_Close();
7123}
7124
7125/* Reads gpm event and adds special keys to input buf. Returns length of
7126 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007127 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 */
7129 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007130mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 int button;
7133 static Gpm_Event gpm_event;
7134 char_u string[6];
7135 int_u vim_modifiers;
7136 int row,col;
7137 unsigned char buttons_mask;
7138 unsigned char gpm_modifiers;
7139 static unsigned char old_buttons = 0;
7140
7141 Gpm_GetEvent(&gpm_event);
7142
7143#ifdef FEAT_GUI
7144 /* Don't put events in the input queue now. */
7145 if (hold_gui_events)
7146 return 0;
7147#endif
7148
7149 row = gpm_event.y - 1;
7150 col = gpm_event.x - 1;
7151
7152 string[0] = ESC; /* Our termcode */
7153 string[1] = 'M';
7154 string[2] = 'G';
7155 switch (GPM_BARE_EVENTS(gpm_event.type))
7156 {
7157 case GPM_DRAG:
7158 string[3] = MOUSE_DRAG;
7159 break;
7160 case GPM_DOWN:
7161 buttons_mask = gpm_event.buttons & ~old_buttons;
7162 old_buttons = gpm_event.buttons;
7163 switch (buttons_mask)
7164 {
7165 case GPM_B_LEFT:
7166 button = MOUSE_LEFT;
7167 break;
7168 case GPM_B_MIDDLE:
7169 button = MOUSE_MIDDLE;
7170 break;
7171 case GPM_B_RIGHT:
7172 button = MOUSE_RIGHT;
7173 break;
7174 default:
7175 return 0;
7176 /*Don't know what to do. Can more than one button be
7177 * reported in one event? */
7178 }
7179 string[3] = (char_u)(button | 0x20);
7180 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7181 break;
7182 case GPM_UP:
7183 string[3] = MOUSE_RELEASE;
7184 old_buttons &= ~gpm_event.buttons;
7185 break;
7186 default:
7187 return 0;
7188 }
7189 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7190 gpm_modifiers = gpm_event.modifiers;
7191 vim_modifiers = 0x0;
7192 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7193 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7194 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7195 */
7196 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7197 vim_modifiers |= MOUSE_SHIFT;
7198
7199 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7200 vim_modifiers |= MOUSE_CTRL;
7201 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7202 vim_modifiers |= MOUSE_ALT;
7203 string[3] |= vim_modifiers;
7204 string[4] = (char_u)(col + ' ' + 1);
7205 string[5] = (char_u)(row + ' ' + 1);
7206 add_to_input_buf(string, 6);
7207 return 6;
7208}
7209#endif /* FEAT_MOUSE_GPM */
7210
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007211#ifdef FEAT_SYSMOUSE
7212/*
7213 * Initialize connection with sysmouse.
7214 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7215 * output, any sysmouse output than will be processed via sig_sysmouse().
7216 * Return OK if succeeded, FAIL if failed.
7217 */
7218 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007219sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007220{
7221 struct mouse_info mouse;
7222
7223 mouse.operation = MOUSE_MODE;
7224 mouse.u.mode.mode = 0;
7225 mouse.u.mode.signal = SIGUSR2;
7226 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7227 {
7228 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7229 mouse.operation = MOUSE_SHOW;
7230 ioctl(1, CONS_MOUSECTL, &mouse);
7231 return OK;
7232 }
7233 return FAIL;
7234}
7235
7236/*
7237 * Stop processing SIGUSR2 signals, and also make sure that
7238 * virtual console do not send us any sysmouse related signal.
7239 */
7240 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007241sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007242{
7243 struct mouse_info mouse;
7244
7245 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7246 mouse.operation = MOUSE_MODE;
7247 mouse.u.mode.mode = 0;
7248 mouse.u.mode.signal = 0;
7249 ioctl(1, CONS_MOUSECTL, &mouse);
7250}
7251
7252/*
7253 * Gets info from sysmouse and adds special keys to input buf.
7254 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007255 static RETSIGTYPE
7256sig_sysmouse SIGDEFARG(sigarg)
7257{
7258 struct mouse_info mouse;
7259 struct video_info video;
7260 char_u string[6];
7261 int row, col;
7262 int button;
7263 int buttons;
7264 static int oldbuttons = 0;
7265
7266#ifdef FEAT_GUI
7267 /* Don't put events in the input queue now. */
7268 if (hold_gui_events)
7269 return;
7270#endif
7271
7272 mouse.operation = MOUSE_GETINFO;
7273 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7274 && ioctl(1, FBIO_MODEINFO, &video) != -1
7275 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7276 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7277 {
7278 row = mouse.u.data.y / video.vi_cheight;
7279 col = mouse.u.data.x / video.vi_cwidth;
7280 buttons = mouse.u.data.buttons;
7281 string[0] = ESC; /* Our termcode */
7282 string[1] = 'M';
7283 string[2] = 'S';
7284 if (oldbuttons == buttons && buttons != 0)
7285 {
7286 button = MOUSE_DRAG;
7287 }
7288 else
7289 {
7290 switch (buttons)
7291 {
7292 case 0:
7293 button = MOUSE_RELEASE;
7294 break;
7295 case 1:
7296 button = MOUSE_LEFT;
7297 break;
7298 case 2:
7299 button = MOUSE_MIDDLE;
7300 break;
7301 case 4:
7302 button = MOUSE_RIGHT;
7303 break;
7304 default:
7305 return;
7306 }
7307 oldbuttons = buttons;
7308 }
7309 string[3] = (char_u)(button);
7310 string[4] = (char_u)(col + ' ' + 1);
7311 string[5] = (char_u)(row + ' ' + 1);
7312 add_to_input_buf(string, 6);
7313 }
7314 return;
7315}
7316#endif /* FEAT_SYSMOUSE */
7317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007319typedef char_u * (*STRPROCSTR)(char_u *);
7320typedef char_u * (*INTPROCSTR)(int);
7321typedef int (*STRPROCINT)(char_u *);
7322typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324/*
7325 * Call a DLL routine which takes either a string or int param
7326 * and returns an allocated string.
7327 */
7328 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007329mch_libcall(
7330 char_u *libname,
7331 char_u *funcname,
7332 char_u *argstring, /* NULL when using a argint */
7333 int argint,
7334 char_u **string_result,/* NULL when using number_result */
7335 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337# if defined(USE_DLOPEN)
7338 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007339 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340# else
7341 shl_t hinstLib;
7342# endif
7343 STRPROCSTR ProcAdd;
7344 INTPROCSTR ProcAddI;
7345 char_u *retval_str = NULL;
7346 int retval_int = 0;
7347 int success = FALSE;
7348
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007349 /*
7350 * Get a handle to the DLL module.
7351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007353 /* First clear any error, it's not cleared by the dlopen() call. */
7354 (void)dlerror();
7355
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 hinstLib = dlopen((char *)libname, RTLD_LAZY
7357# ifdef RTLD_LOCAL
7358 | RTLD_LOCAL
7359# endif
7360 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007361 if (hinstLib == NULL)
7362 {
7363 /* "dlerr" must be used before dlclose() */
7364 dlerr = (char *)dlerror();
7365 if (dlerr != NULL)
7366 EMSG2(_("dlerror = \"%s\""), dlerr);
7367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368# else
7369 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7370# endif
7371
7372 /* If the handle is valid, try to get the function address. */
7373 if (hinstLib != NULL)
7374 {
7375# ifdef HAVE_SETJMP_H
7376 /*
7377 * Catch a crash when calling the library function. For example when
7378 * using a number where a string pointer is expected.
7379 */
7380 mch_startjmp();
7381 if (SETJMP(lc_jump_env) != 0)
7382 {
7383 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007384# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007385 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 mch_didjmp();
7388 }
7389 else
7390# endif
7391 {
7392 retval_str = NULL;
7393 retval_int = 0;
7394
7395 if (argstring != NULL)
7396 {
7397# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007398 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007399 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400# else
7401 if (shl_findsym(&hinstLib, (const char *)funcname,
7402 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7403 ProcAdd = NULL;
7404# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007405 if ((success = (ProcAdd != NULL
7406# if defined(USE_DLOPEN)
7407 && dlerr == NULL
7408# endif
7409 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
7411 if (string_result == NULL)
7412 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7413 else
7414 retval_str = (ProcAdd)(argstring);
7415 }
7416 }
7417 else
7418 {
7419# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007420 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007421 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422# else
7423 if (shl_findsym(&hinstLib, (const char *)funcname,
7424 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7425 ProcAddI = NULL;
7426# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007427 if ((success = (ProcAddI != NULL
7428# if defined(USE_DLOPEN)
7429 && dlerr == NULL
7430# endif
7431 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
7433 if (string_result == NULL)
7434 retval_int = ((INTPROCINT)ProcAddI)(argint);
7435 else
7436 retval_str = (ProcAddI)(argint);
7437 }
7438 }
7439
7440 /* Save the string before we free the library. */
7441 /* Assume that a "1" or "-1" result is an illegal pointer. */
7442 if (string_result == NULL)
7443 *number_result = retval_int;
7444 else if (retval_str != NULL
7445 && retval_str != (char_u *)1
7446 && retval_str != (char_u *)-1)
7447 *string_result = vim_strsave(retval_str);
7448 }
7449
7450# ifdef HAVE_SETJMP_H
7451 mch_endjmp();
7452# ifdef SIGHASARG
7453 if (lc_signal != 0)
7454 {
7455 int i;
7456
7457 /* try to find the name of this signal */
7458 for (i = 0; signal_info[i].sig != -1; i++)
7459 if (lc_signal == signal_info[i].sig)
7460 break;
7461 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7462 }
7463# endif
7464# endif
7465
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007467 /* "dlerr" must be used before dlclose() */
7468 if (dlerr != NULL)
7469 EMSG2(_("dlerror = \"%s\""), dlerr);
7470
7471 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 (void)dlclose(hinstLib);
7473# else
7474 (void)shl_unload(hinstLib);
7475# endif
7476 }
7477
7478 if (!success)
7479 {
7480 EMSG2(_(e_libcall), funcname);
7481 return FAIL;
7482 }
7483
7484 return OK;
7485}
7486#endif
7487
7488#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7489static int xterm_trace = -1; /* default: disabled */
7490static int xterm_button;
7491
7492/*
7493 * Setup a dummy window for X selections in a terminal.
7494 */
7495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007496setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 int z = 0;
7499 char *strp = "";
7500 Widget AppShell;
7501
7502 if (!x_connect_to_server())
7503 return;
7504
7505 open_app_context();
7506 if (app_context != NULL && xterm_Shell == (Widget)0)
7507 {
7508 int (*oldhandler)();
7509#if defined(HAVE_SETJMP_H)
7510 int (*oldIOhandler)();
7511#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007512# ifdef ELAPSED_FUNC
7513 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007516 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517# endif
7518
7519 /* Ignore X errors while opening the display */
7520 oldhandler = XSetErrorHandler(x_error_check);
7521
7522#if defined(HAVE_SETJMP_H)
7523 /* Ignore X IO errors while opening the display */
7524 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7525 mch_startjmp();
7526 if (SETJMP(lc_jump_env) != 0)
7527 {
7528 mch_didjmp();
7529 xterm_dpy = NULL;
7530 }
7531 else
7532#endif
7533 {
7534 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7535 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7536#if defined(HAVE_SETJMP_H)
7537 mch_endjmp();
7538#endif
7539 }
7540
7541#if defined(HAVE_SETJMP_H)
7542 /* Now handle X IO errors normally. */
7543 (void)XSetIOErrorHandler(oldIOhandler);
7544#endif
7545 /* Now handle X errors normally. */
7546 (void)XSetErrorHandler(oldhandler);
7547
7548 if (xterm_dpy == NULL)
7549 {
7550 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007551 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 return;
7553 }
7554
7555 /* Catch terminating error of the X server connection. */
7556 (void)XSetIOErrorHandler(x_IOerror_handler);
7557
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007558# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007560 {
7561 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007562 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007563 verbose_leave();
7564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565# endif
7566
7567 /* Create a Shell to make converters work. */
7568 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7569 applicationShellWidgetClass, xterm_dpy,
7570 NULL);
7571 if (AppShell == (Widget)0)
7572 return;
7573 xterm_Shell = XtVaCreatePopupShell("VIM",
7574 topLevelShellWidgetClass, AppShell,
7575 XtNmappedWhenManaged, 0,
7576 XtNwidth, 1,
7577 XtNheight, 1,
7578 NULL);
7579 if (xterm_Shell == (Widget)0)
7580 return;
7581
7582 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007583 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (x11_display == NULL)
7585 x11_display = xterm_dpy;
7586
7587 XtRealizeWidget(xterm_Shell);
7588 XSync(xterm_dpy, False);
7589 xterm_update();
7590 }
7591 if (xterm_Shell != (Widget)0)
7592 {
7593 clip_init(TRUE);
7594 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7595 x11_window = (Window)atol(strp);
7596 /* Check if $WINDOWID is valid. */
7597 if (test_x11_window(xterm_dpy) == FAIL)
7598 x11_window = 0;
7599 if (x11_window != 0)
7600 xterm_trace = 0;
7601 }
7602}
7603
7604 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007605start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7608 return;
7609 xterm_trace = 1;
7610 xterm_button = button;
7611 do_xterm_trace();
7612}
7613
7614
7615 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007616stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 if (xterm_trace < 0)
7619 return;
7620 xterm_trace = 0;
7621}
7622
7623/*
7624 * Query the xterm pointer and generate mouse termcodes if necessary
7625 * return TRUE if dragging is active, else FALSE
7626 */
7627 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007628do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 Window root, child;
7631 int root_x, root_y;
7632 int win_x, win_y;
7633 int row, col;
7634 int_u mask_return;
7635 char_u buf[50];
7636 char_u *strp;
7637 long got_hints;
7638 static char_u *mouse_code;
7639 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7640 static int prev_row = 0, prev_col = 0;
7641 static XSizeHints xterm_hints;
7642
7643 if (xterm_trace <= 0)
7644 return FALSE;
7645
7646 if (xterm_trace == 1)
7647 {
7648 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007649 * have changed recently. */
7650 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7651 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 || xterm_hints.width_inc <= 1
7653 || xterm_hints.height_inc <= 1)
7654 {
7655 xterm_trace = -1; /* Not enough data -- disable tracing */
7656 return FALSE;
7657 }
7658
7659 /* Rely on the same mouse code for the duration of this */
7660 mouse_code = find_termcode(mouse_name);
7661 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007662 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 xterm_trace = 2;
7664
7665 /* Find the offset of the chars, there might be a scrollbar on the
7666 * left of the window and/or a menu on the top (eterm etc.) */
7667 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7668 &win_x, &win_y, &mask_return);
7669 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7670 - (xterm_hints.height_inc / 2);
7671 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7672 xterm_hints.y = 2;
7673 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7674 - (xterm_hints.width_inc / 2);
7675 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7676 xterm_hints.x = 2;
7677 return TRUE;
7678 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007679 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
7681 xterm_trace = 0;
7682 return FALSE;
7683 }
7684
7685 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7686 &win_x, &win_y, &mask_return);
7687
7688 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7689 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7690 if (row == prev_row && col == prev_col)
7691 return TRUE;
7692
7693 STRCPY(buf, mouse_code);
7694 strp = buf + STRLEN(buf);
7695 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7696 *strp++ = (char_u)(col + ' ' + 1);
7697 *strp++ = (char_u)(row + ' ' + 1);
7698 *strp = 0;
7699 add_to_input_buf(buf, STRLEN(buf));
7700
7701 prev_row = row;
7702 prev_col = col;
7703 return TRUE;
7704}
7705
7706# if defined(FEAT_GUI) || defined(PROTO)
7707/*
7708 * Destroy the display, window and app_context. Required for GTK.
7709 */
7710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007711clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 if (xterm_Shell != (Widget)0)
7714 {
7715 XtDestroyWidget(xterm_Shell);
7716 xterm_Shell = (Widget)0;
7717 }
7718 if (xterm_dpy != NULL)
7719 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007720# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 /* Lesstif and Solaris crash here, lose some memory */
7722 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 if (x11_display == xterm_dpy)
7725 x11_display = NULL;
7726 xterm_dpy = NULL;
7727 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007728# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 if (app_context != (XtAppContext)NULL)
7730 {
7731 /* Lesstif and Solaris crash here, lose some memory */
7732 XtDestroyApplicationContext(app_context);
7733 app_context = (XtAppContext)NULL;
7734 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737# endif
7738
7739/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007740 * Catch up with GUI or X events.
7741 */
7742 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007743clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007744{
7745# ifdef FEAT_GUI
7746 if (gui.in_use)
7747 gui_mch_update();
7748 else
7749# endif
7750 if (xterm_Shell != (Widget)0)
7751 xterm_update();
7752}
7753
7754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 * Catch up with any queued X events. This may put keyboard input into the
7756 * input buffer, call resize call-backs, trigger timers etc. If there is
7757 * nothing in the X event queue (& no timers pending), then we return
7758 * immediately.
7759 */
7760 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007761xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 XEvent event;
7764
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007765 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007767 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007769 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007770 break;
7771
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007772 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007773 {
7774 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007775 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007776#ifdef FEAT_CLIENTSERVER
7777 {
7778 XPropertyEvent *e = (XPropertyEvent *)&event;
7779
7780 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007782 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007785 XtDispatchEvent(&event);
7786 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007787 else
7788 {
7789 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007790 XtAppProcessEvent(app_context, mask);
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793}
7794
7795 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007796clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 if (xterm_Shell != (Widget)0)
7799 return clip_x11_own_selection(xterm_Shell, cbd);
7800 return FAIL;
7801}
7802
7803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007804clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 if (xterm_Shell != (Widget)0)
7807 clip_x11_lose_selection(xterm_Shell, cbd);
7808}
7809
7810 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007811clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 if (xterm_Shell != (Widget)0)
7814 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7815}
7816
7817 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007818clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 clip_x11_set_selection(cbd);
7821}
7822#endif
7823
7824
7825#if defined(USE_XSMP) || defined(PROTO)
7826/*
7827 * Code for X Session Management Protocol.
7828 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007829static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7830static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7831static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7832static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7833static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835
7836# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007837static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838
7839/*
7840 * This is our chance to ask the user if they want to save,
7841 * or abort the logout
7842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007844xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 cmdmod_T save_cmdmod;
7847 int cancel_shutdown = False;
7848
7849 save_cmdmod = cmdmod;
7850 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007851 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 /* Mustn't logout */
7853 cancel_shutdown = True;
7854 cmdmod = save_cmdmod;
7855 setcursor(); /* position cursor */
7856 out_flush();
7857
7858 /* Done interaction */
7859 SmcInteractDone(smc_conn, cancel_shutdown);
7860
7861 /* Finish off
7862 * Only end save-yourself here if we're not cancelling shutdown;
7863 * we'll get a cancelled callback later in which we'll end it.
7864 * Hopefully get around glitchy SMs (like GNOME-1)
7865 */
7866 if (!cancel_shutdown)
7867 {
7868 xsmp.save_yourself = False;
7869 SmcSaveYourselfDone(smc_conn, True);
7870 }
7871}
7872# endif
7873
7874/*
7875 * Callback that starts save-yourself.
7876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007878xsmp_handle_save_yourself(
7879 SmcConn smc_conn,
7880 SmPointer client_data UNUSED,
7881 int save_type UNUSED,
7882 Bool shutdown,
7883 int interact_style UNUSED,
7884 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 /* Handle already being in saveyourself */
7887 if (xsmp.save_yourself)
7888 SmcSaveYourselfDone(smc_conn, True);
7889 xsmp.save_yourself = True;
7890 xsmp.shutdown = shutdown;
7891
7892 /* First up, preserve all files */
7893 out_flush();
7894 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7895
7896 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007897 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
7899# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7900 /* Now see if we can ask about unsaved files */
7901 if (shutdown && !fast && gui.in_use)
7902 /* Need to interact with user, but need SM's permission */
7903 SmcInteractRequest(smc_conn, SmDialogError,
7904 xsmp_handle_interaction, client_data);
7905 else
7906# endif
7907 {
7908 /* Can stop the cycle here */
7909 SmcSaveYourselfDone(smc_conn, True);
7910 xsmp.save_yourself = False;
7911 }
7912}
7913
7914
7915/*
7916 * Callback to warn us of imminent death.
7917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007919xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 xsmp_close();
7922
7923 /* quit quickly leaving swapfiles for modified buffers behind */
7924 getout_preserve_modified(0);
7925}
7926
7927
7928/*
7929 * Callback to tell us that save-yourself has completed.
7930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007932xsmp_save_complete(
7933 SmcConn smc_conn UNUSED,
7934 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 xsmp.save_yourself = False;
7937}
7938
7939
7940/*
7941 * Callback to tell us that an instigated shutdown was cancelled
7942 * (maybe even by us)
7943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007945xsmp_shutdown_cancelled(
7946 SmcConn smc_conn,
7947 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 if (xsmp.save_yourself)
7950 SmcSaveYourselfDone(smc_conn, True);
7951 xsmp.save_yourself = False;
7952 xsmp.shutdown = False;
7953}
7954
7955
7956/*
7957 * Callback to tell us that a new ICE connection has been established.
7958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007960xsmp_ice_connection(
7961 IceConn iceConn,
7962 IcePointer clientData UNUSED,
7963 Bool opening,
7964 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 /* Intercept creation of ICE connection fd */
7967 if (opening)
7968 {
7969 xsmp_icefd = IceConnectionNumber(iceConn);
7970 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7971 }
7972}
7973
7974
7975/* Handle any ICE processing that's required; return FAIL if SM lost */
7976 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007977xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 Bool rep;
7980
7981 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7982 == IceProcessMessagesIOError)
7983 {
7984 /* Lost ICE */
7985 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007986 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 xsmp_close();
7988 return FAIL;
7989 }
7990 else
7991 return OK;
7992}
7993
7994static int dummy;
7995
7996/* Set up X Session Management Protocol */
7997 void
7998xsmp_init(void)
7999{
8000 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 SmcCallbacks smcallbacks;
8002#if 0
8003 SmPropValue smname;
8004 SmProp smnameprop;
8005 SmProp *smprops[1];
8006#endif
8007
8008 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008009 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
8011 xsmp.save_yourself = xsmp.shutdown = False;
8012
8013 /* Set up SM callbacks - must have all, even if they're not used */
8014 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8015 smcallbacks.save_yourself.client_data = NULL;
8016 smcallbacks.die.callback = xsmp_die;
8017 smcallbacks.die.client_data = NULL;
8018 smcallbacks.save_complete.callback = xsmp_save_complete;
8019 smcallbacks.save_complete.client_data = NULL;
8020 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8021 smcallbacks.shutdown_cancelled.client_data = NULL;
8022
8023 /* Set up a watch on ICE connection creations. The "dummy" argument is
8024 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8025 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8026 {
8027 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008028 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 return;
8030 }
8031
8032 /* Create an SM connection */
8033 xsmp.smcconn = SmcOpenConnection(
8034 NULL,
8035 NULL,
8036 SmProtoMajor,
8037 SmProtoMinor,
8038 SmcSaveYourselfProcMask | SmcDieProcMask
8039 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8040 &smcallbacks,
8041 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008042 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 sizeof(errorstring),
8044 errorstring);
8045 if (xsmp.smcconn == NULL)
8046 {
8047 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008050 {
8051 vim_snprintf(errorreport, sizeof(errorreport),
8052 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8053 verb_msg((char_u *)errorreport);
8054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 return;
8056 }
8057 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8058
8059#if 0
8060 /* ID ourselves */
8061 smname.value = "vim";
8062 smname.length = 3;
8063 smnameprop.name = "SmProgram";
8064 smnameprop.type = "SmARRAY8";
8065 smnameprop.num_vals = 1;
8066 smnameprop.vals = &smname;
8067
8068 smprops[0] = &smnameprop;
8069 SmcSetProperties(xsmp.smcconn, 1, smprops);
8070#endif
8071}
8072
8073
8074/* Shut down XSMP comms. */
8075 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008076xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 if (xsmp_icefd != -1)
8079 {
8080 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008081 if (xsmp.clientid != NULL)
8082 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008083 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 xsmp_icefd = -1;
8085 }
8086}
8087#endif /* USE_XSMP */
8088
8089
8090#ifdef EBCDIC
8091/* Translate character to its CTRL- value */
8092char CtrlTable[] =
8093{
8094/* 00 - 5E */
8095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8097 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8099 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8101/* ^ */ 0x1E,
8102/* - */ 0x1F,
8103/* 61 - 6C */
8104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8105/* _ */ 0x1F,
8106/* 6E - 80 */
8107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8108/* a */ 0x01,
8109/* b */ 0x02,
8110/* c */ 0x03,
8111/* d */ 0x37,
8112/* e */ 0x2D,
8113/* f */ 0x2E,
8114/* g */ 0x2F,
8115/* h */ 0x16,
8116/* i */ 0x05,
8117/* 8A - 90 */
8118 0, 0, 0, 0, 0, 0, 0,
8119/* j */ 0x15,
8120/* k */ 0x0B,
8121/* l */ 0x0C,
8122/* m */ 0x0D,
8123/* n */ 0x0E,
8124/* o */ 0x0F,
8125/* p */ 0x10,
8126/* q */ 0x11,
8127/* r */ 0x12,
8128/* 9A - A1 */
8129 0, 0, 0, 0, 0, 0, 0, 0,
8130/* s */ 0x13,
8131/* t */ 0x3C,
8132/* u */ 0x3D,
8133/* v */ 0x32,
8134/* w */ 0x26,
8135/* x */ 0x18,
8136/* y */ 0x19,
8137/* z */ 0x3F,
8138/* AA - AC */
8139 0, 0, 0,
8140/* [ */ 0x27,
8141/* AE - BC */
8142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8143/* ] */ 0x1D,
8144/* BE - C0 */ 0, 0, 0,
8145/* A */ 0x01,
8146/* B */ 0x02,
8147/* C */ 0x03,
8148/* D */ 0x37,
8149/* E */ 0x2D,
8150/* F */ 0x2E,
8151/* G */ 0x2F,
8152/* H */ 0x16,
8153/* I */ 0x05,
8154/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8155/* J */ 0x15,
8156/* K */ 0x0B,
8157/* L */ 0x0C,
8158/* M */ 0x0D,
8159/* N */ 0x0E,
8160/* O */ 0x0F,
8161/* P */ 0x10,
8162/* Q */ 0x11,
8163/* R */ 0x12,
8164/* DA - DF */ 0, 0, 0, 0, 0, 0,
8165/* \ */ 0x1C,
8166/* E1 */ 0,
8167/* S */ 0x13,
8168/* T */ 0x3C,
8169/* U */ 0x3D,
8170/* V */ 0x32,
8171/* W */ 0x26,
8172/* X */ 0x18,
8173/* Y */ 0x19,
8174/* Z */ 0x3F,
8175/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8177};
8178
8179char MetaCharTable[]=
8180{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8181 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8182 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8183 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8184 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8185};
8186
8187
8188/* TODO: Use characters NOT numbers!!! */
8189char CtrlCharTable[]=
8190{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8191 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8192 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8193 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8194 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8195};
8196
8197
8198#endif