blob: f33042675da49772d9fed13bc7bde7d51d9bf219 [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 Moolenaar8e82c052018-08-21 19:47:48 +0200164static volatile sig_atomic_t 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(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200208static volatile sig_atomic_t 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(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200234static volatile sig_atomic_t 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(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200238static volatile sig_atomic_t deadly_signal = 0; /* The signal we caught */
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000239/* volatile because it is used in signal handler deathtrap(). */
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200240static volatile sig_atomic_t 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
Bram Moolenaared5a9d62018-09-06 13:14:43 +0200420 // Only process messages when waiting.
421 if (wtime != 0)
422 {
423 parse_queued_messages();
424 // If input was put directly in typeahead buffer bail out here.
425 if (typebuf_changed(tb_change_cnt))
426 return 0;
427 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200428#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200429 if (wtime < 0 && did_start_blocking)
430 /* blocking and already waited for p_ut */
431 wait_time = -1;
432 else
433 {
434 if (wtime >= 0)
435 wait_time = wtime;
436 else
437 /* going to block after p_ut */
438 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100439#ifdef ELAPSED_FUNC
440 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200441#endif
442 wait_time -= elapsed_time;
443 if (wait_time < 0)
444 {
445 if (wtime >= 0)
446 /* no character available within "wtime" */
447 return 0;
448
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200449 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200450 {
451 /* no character available within 'updatetime' */
452 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200453 if (trigger_cursorhold() && maxlen >= 3
454 && !typebuf_changed(tb_change_cnt))
455 {
456 buf[0] = K_SPECIAL;
457 buf[1] = KS_EXTRA;
458 buf[2] = (int)KE_CURSORHOLD;
459 return 3;
460 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200461 /*
462 * If there is no character available within 'updatetime'
463 * seconds flush all the swap files to disk.
464 * Also done when interrupted by SIGWINCH.
465 */
466 before_blocking();
467 continue;
468 }
469 }
470 }
471
472#ifdef FEAT_JOB_CHANNEL
473 /* Checking if a job ended requires polling. Do this every 100 msec. */
474 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
475 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100476 /* If there is readahead then parse_queued_messages() timed out and we
477 * should call it again soon. */
478 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
479 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200480#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100481#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100482 if (p_beval && wait_time > 100L)
483 /* The 'balloonexpr' may indirectly invoke a callback while waiting
484 * for a character, need to check often. */
485 wait_time = 100L;
486#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200489 * We want to be interrupted by the winch signal
490 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200492 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200493 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200494 /* If input was put directly in typeahead buffer bail out here. */
495 if (typebuf_changed(tb_change_cnt))
496 return 0;
497
498 /*
499 * For some terminals we only get one character at a time.
500 * We want the get all available characters, so we could keep on
501 * trying until none is available
502 * For some other terminals this is quite slow, that's why we don't
503 * do it.
504 */
505 len = read_from_input_buf(buf, (long)maxlen);
506 if (len > 0)
507 return len;
508 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200511 /* no character available */
512#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
513 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100514 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200517 if (do_resize /* interrupted by SIGWINCH signal */
518#ifdef FEAT_CLIENTSERVER
519 || server_waiting()
520#endif
521#ifdef MESSAGE_QUEUE
522 || interrupted
523#endif
524 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100525 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200526 continue;
527
528 /* no character available or interrupted */
529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200531 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532}
533
534 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100535handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 do_resize = FALSE;
538 shell_resized();
539}
540
541/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200542 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
544 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100545mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200547 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548}
549
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200550#if defined(FEAT_TERMINAL) || defined(PROTO)
551/*
552 * Check for any pending input or messages.
553 */
554 int
555mch_check_messages(void)
556{
557 return WaitForChar(0L, NULL, TRUE);
558}
559#endif
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
562# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000563# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564# endif
565# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
566# include <sys/sysctl.h>
567# endif
568# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
569# include <sys/sysinfo.h>
570# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100571# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100572# include <mach/mach_host.h>
573# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000577 * Return total amount of memory available in Kbyte.
578 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100581mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000584 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar362dc332018-03-05 21:59:37 +0100586# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100587 {
588 /* Mac (Darwin) way of getting the amount of RAM available */
589 mach_port_t host = mach_host_self();
590 kern_return_t kret;
591# ifdef HOST_VM_INFO64
592 struct vm_statistics64 vm_stat;
593 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
Bram Moolenaar988615f2018-02-27 17:58:20 +0100595 kret = host_statistics64(host, HOST_VM_INFO64,
596 (host_info64_t)&vm_stat, &count);
597# else
598 struct vm_statistics vm_stat;
599 natural_t count = HOST_VM_INFO_COUNT;
600
601 kret = host_statistics(host, HOST_VM_INFO,
602 (host_info_t)&vm_stat, &count);
603# endif
604 if (kret == KERN_SUCCESS)
605 /* get the amount of user memory by summing each usage */
606 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
607 + vm_stat.inactive_count
608# ifdef MAC_OS_X_VERSION_10_9
609 + vm_stat.compressor_page_count
610# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100611 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100612 mach_port_deallocate(mach_task_self(), host);
613 }
614# endif
615
616# ifdef HAVE_SYSCTL
617 if (mem == 0)
618 {
619 /* BSD way of getting the amount of RAM available. */
620 int mib[2];
621 size_t len = sizeof(long_u);
622# ifdef HW_USERMEM64
623 long_u physmem;
624# else
625 /* sysctl() may return 32 bit or 64 bit, accept both */
626 union {
627 int_u u32;
628 long_u u64;
629 } physmem;
630# endif
631
632 mib[0] = CTL_HW;
633# ifdef HW_USERMEM64
634 mib[1] = HW_USERMEM64;
635# else
636 mib[1] = HW_USERMEM;
637# endif
638 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
639 {
640# ifdef HW_USERMEM64
641 mem = (long_u)physmem;
642# else
643 if (len == sizeof(physmem.u64))
644 mem = (long_u)physmem.u64;
645 else
646 mem = (long_u)physmem.u32;
647# endif
648 }
649 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200652# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (mem == 0)
654 {
655 struct sysinfo sinfo;
656
657 /* Linux way of getting amount of RAM available */
658 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000659 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200660# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000661 /* avoid overflow as much as possible */
662 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
663 {
664 sinfo.mem_unit = sinfo.mem_unit >> 1;
665 --shiftright;
666 }
667 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200668# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200670# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200675# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (mem == 0)
677 {
678 long pagesize, pagecount;
679
680 /* Solaris way of getting amount of RAM available */
681 pagesize = sysconf(_SC_PAGESIZE);
682 pagecount = sysconf(_SC_PHYS_PAGES);
683 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000684 {
685 /* avoid overflow as much as possible */
686 while (shiftright > 0 && (pagesize & 1) == 0)
687 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000688 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000689 --shiftright;
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696 /* Return the minimum of the physical memory and the user limit, because
697 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200698# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 struct rlimit rlp;
701
702 if (getrlimit(RLIMIT_DATA, &rlp) == 0
703 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200704# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200706# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000707 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000709 {
710 mem = (long_u)rlp.rlim_cur;
711 shiftright = 10;
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
716 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000717 return mem >> shiftright;
718 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719}
720#endif
721
722 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100723mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000726#ifdef FEAT_MZSCHEME
727 long total = msec; /* remember original value */
728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 if (ignoreinput)
731 {
732 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000733 * here. But we don't want QUIT to kill us (CTRL-\ used in a
734 * shell may produce SIGQUIT). */
735 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 old_tmode = curr_tmode;
737 if (curr_tmode == TMODE_RAW)
738 settmode(TMODE_SLEEP);
739
740 /*
741 * Everybody sleeps in a different way...
742 * Prefer nanosleep(), some versions of usleep() can only sleep up to
743 * one second.
744 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000745#ifdef FEAT_MZSCHEME
746 do
747 {
748 /* if total is large enough, wait by portions in p_mzq */
749 if (total > p_mzq)
750 msec = p_mzq;
751 else
752 msec = total;
753 total -= msec;
754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#ifdef HAVE_NANOSLEEP
756 {
757 struct timespec ts;
758
759 ts.tv_sec = msec / 1000;
760 ts.tv_nsec = (msec % 1000) * 1000000;
761 (void)nanosleep(&ts, NULL);
762 }
763#else
764# ifdef HAVE_USLEEP
765 while (msec >= 1000)
766 {
767 usleep((unsigned int)(999 * 1000));
768 msec -= 999;
769 }
770 usleep((unsigned int)(msec * 1000));
771# else
772# ifndef HAVE_SELECT
773 poll(NULL, 0, (int)msec);
774# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
776 struct timeval tv;
777
778 tv.tv_sec = msec / 1000;
779 tv.tv_usec = (msec % 1000) * 1000;
780 /*
781 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
782 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
783 */
784 select(0, NULL, NULL, NULL, &tv);
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# endif /* HAVE_SELECT */
787# endif /* HAVE_NANOSLEEP */
788#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000789#ifdef FEAT_MZSCHEME
790 }
791 while (total > 0);
792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000795 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200798 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000801#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
803# define HAVE_CHECK_STACK_GROWTH
804/*
805 * Support for checking for an almost-out-of-stack-space situation.
806 */
807
808/*
809 * Return a pointer to an item on the stack. Used to find out if the stack
810 * grows up or down.
811 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100812static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813static int stack_grows_downwards;
814
815/*
816 * Find out if the stack grows upwards or downwards.
817 * "p" points to a variable on the stack of the caller.
818 */
819 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100820check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 int i;
823
824 stack_grows_downwards = (p > (char *)&i);
825}
826#endif
827
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000828#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829static char *stack_limit = NULL;
830
831#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
832# include <pthread.h>
833# include <pthread_np.h>
834#endif
835
836/*
837 * Find out until how var the stack can grow without getting into trouble.
838 * Called when starting up and when switching to the signal stack in
839 * deathtrap().
840 */
841 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100842get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 struct rlimit rlp;
845 int i;
846 long lim;
847
848 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
849 * limit doesn't fit in a long (rlim_cur might be "long long"). */
850 if (getrlimit(RLIMIT_STACK, &rlp) == 0
851 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
852# ifdef RLIM_INFINITY
853 && rlp.rlim_cur != RLIM_INFINITY
854# endif
855 )
856 {
857 lim = (long)rlp.rlim_cur;
858#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
859 {
860 pthread_attr_t attr;
861 size_t size;
862
863 /* On FreeBSD the initial thread always has a fixed stack size, no
864 * matter what the limits are set to. Normally it's 1 Mbyte. */
865 pthread_attr_init(&attr);
866 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
867 {
868 pthread_attr_getstacksize(&attr, &size);
869 if (lim > (long)size)
870 lim = (long)size;
871 }
872 pthread_attr_destroy(&attr);
873 }
874#endif
875 if (stack_grows_downwards)
876 {
877 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
878 if (stack_limit >= (char *)&i)
879 /* overflow, set to 1/16 of current stack position */
880 stack_limit = (char *)((long)&i / 16L);
881 }
882 else
883 {
884 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
885 if (stack_limit <= (char *)&i)
886 stack_limit = NULL; /* overflow */
887 }
888 }
889}
890
891/*
892 * Return FAIL when running out of stack space.
893 * "p" must point to any variable local to the caller that's on the stack.
894 */
895 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100896mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898 if (stack_limit != NULL)
899 {
900 if (stack_grows_downwards)
901 {
902 if (p < stack_limit)
903 return FAIL;
904 }
905 else if (p > stack_limit)
906 return FAIL;
907 }
908 return OK;
909}
910#endif
911
912#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
913/*
914 * Support for using the signal stack.
915 * This helps when we run out of stack space, which causes a SIGSEGV. The
916 * signal handler then must run on another stack, since the normal stack is
917 * completely full.
918 */
919
920#ifndef SIGSTKSZ
921# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
922#endif
923
924# ifdef HAVE_SIGALTSTACK
925static stack_t sigstk; /* for sigaltstack() */
926# else
927static struct sigstack sigstk; /* for sigstack() */
928# endif
929
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100930static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931static char *signal_stack;
932
933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 if (signal_stack != NULL)
937 {
938# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939# ifdef HAVE_SS_BASE
940 sigstk.ss_base = signal_stack;
941# else
942 sigstk.ss_sp = signal_stack;
943# endif
944 sigstk.ss_size = SIGSTKSZ;
945 sigstk.ss_flags = 0;
946 (void)sigaltstack(&sigstk, NULL);
947# else
948 sigstk.ss_sp = signal_stack;
949 if (stack_grows_downwards)
950 sigstk.ss_sp += SIGSTKSZ - 1;
951 sigstk.ss_onstack = 0;
952 (void)sigstack(&sigstk, NULL);
953# endif
954 }
955}
956#endif
957
958/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000959 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * will barf when the second argument to signal() is ``wrong''.
961 * Let me try it with a few tricky defines from my own osdef.h (jw).
962 */
963#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 static RETSIGTYPE
965sig_winch SIGDEFARG(sigarg)
966{
967 /* this is not required on all systems, but it doesn't hurt anybody */
968 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
969 do_resize = TRUE;
970 SIGRETURN;
971}
972#endif
973
974#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 static RETSIGTYPE
976catch_sigint SIGDEFARG(sigarg)
977{
978 /* this is not required on all systems, but it doesn't hurt anybody */
979 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
980 got_int = TRUE;
981 SIGRETURN;
982}
983#endif
984
985#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 static RETSIGTYPE
987catch_sigpwr SIGDEFARG(sigarg)
988{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000989 /* this is not required on all systems, but it doesn't hurt anybody */
990 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 /*
992 * I'm not sure we get the SIGPWR signal when the system is really going
993 * down or when the batteries are almost empty. Just preserve the swap
994 * files and don't exit, that can't do any harm.
995 */
996 ml_sync_all(FALSE, FALSE);
997 SIGRETURN;
998}
999#endif
1000
1001#ifdef SET_SIG_ALARM
1002/*
1003 * signal function for alarm().
1004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 static RETSIGTYPE
1006sig_alarm SIGDEFARG(sigarg)
1007{
1008 /* doesn't do anything, just to break a system call */
1009 sig_alarm_called = TRUE;
1010 SIGRETURN;
1011}
1012#endif
1013
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001014#if (defined(HAVE_SETJMP_H) \
1015 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1016 || defined(FEAT_LIBCALL))) \
1017 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018/*
1019 * A simplistic version of setjmp() that only allows one level of using.
1020 * Don't call twice before calling mch_endjmp()!.
1021 * Usage:
1022 * mch_startjmp();
1023 * if (SETJMP(lc_jump_env) != 0)
1024 * {
1025 * mch_didjmp();
1026 * EMSG("crash!");
1027 * }
1028 * else
1029 * {
1030 * do_the_work;
1031 * mch_endjmp();
1032 * }
1033 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1034 * not return before the saved environment is used.
1035 * Returns OK for normal return, FAIL when the protected code caused a
1036 * problem and LONGJMP() was used.
1037 */
1038 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001039mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041#ifdef SIGHASARG
1042 lc_signal = 0;
1043#endif
1044 lc_active = TRUE;
1045}
1046
1047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001048mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 lc_active = FALSE;
1051}
1052
1053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001054mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1057 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1058 * otherwise catching the signal only works once. */
1059 init_signal_stack();
1060# endif
1061}
1062#endif
1063
1064/*
1065 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001066 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001068 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1069 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
1071 static RETSIGTYPE
1072deathtrap SIGDEFARG(sigarg)
1073{
1074 static int entered = 0; /* count the number of times we got here.
1075 Note: when memory has been corrupted
1076 this may get an arbitrary value! */
1077#ifdef SIGHASARG
1078 int i;
1079#endif
1080
1081#if defined(HAVE_SETJMP_H)
1082 /*
1083 * Catch a crash in protected code.
1084 * Restores the environment saved in lc_jump_env, which looks like
1085 * SETJMP() returns 1.
1086 */
1087 if (lc_active)
1088 {
1089# if defined(SIGHASARG)
1090 lc_signal = sigarg;
1091# endif
1092 lc_active = FALSE; /* don't jump again */
1093 LONGJMP(lc_jump_env, 1);
1094 /* NOTREACHED */
1095 }
1096#endif
1097
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001098#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001099# ifdef SIGQUIT
1100 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1101 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1102 * pressing CTRL-\, but we don't want Vim to exit then. */
1103 if (in_mch_delay && sigarg == SIGQUIT)
1104 SIGRETURN;
1105# endif
1106
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001107 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1108 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1109 * free(). Calling free() again may then cause a crash. */
1110 if (entered == 0
1111 && (0
1112# ifdef SIGHUP
1113 || sigarg == SIGHUP
1114# endif
1115# ifdef SIGQUIT
1116 || sigarg == SIGQUIT
1117# endif
1118# ifdef SIGTERM
1119 || sigarg == SIGTERM
1120# endif
1121# ifdef SIGPWR
1122 || sigarg == SIGPWR
1123# endif
1124# ifdef SIGUSR1
1125 || sigarg == SIGUSR1
1126# endif
1127# ifdef SIGUSR2
1128 || sigarg == SIGUSR2
1129# endif
1130 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001131 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001132 SIGRETURN;
1133#endif
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /* Remember how often we have been called. */
1136 ++entered;
1137
Bram Moolenaare429e702016-06-10 19:49:14 +02001138 /* Executing autocommands is likely to use more stack space than we have
1139 * available in the signal stack. */
1140 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#ifdef FEAT_EVAL
1143 /* Set the v:dying variable. */
1144 set_vim_var_nr(VV_DYING, (long)entered);
1145#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001146 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001148#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 /* Since we are now using the signal stack, need to reset the stack
1150 * limit. Otherwise using a regexp will fail. */
1151 get_stack_limit();
1152#endif
1153
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001154#if 0
1155 /* This is for opening gdb the moment Vim crashes.
1156 * You need to manually adjust the file name and Vim executable name.
1157 * Suggested by SungHyun Nam. */
1158 {
1159# define VI_GDB_FILE "/tmp/vimgdb"
1160# define VIM_NAME "/usr/bin/vim"
1161 FILE *fp = fopen(VI_GDB_FILE, "w");
1162 if (fp)
1163 {
1164 fprintf(fp,
1165 "file %s\n"
1166 "attach %d\n"
1167 "set height 1000\n"
1168 "bt full\n"
1169 , VIM_NAME, getpid());
1170 fclose(fp);
1171 system("xterm -e gdb -x "VI_GDB_FILE);
1172 unlink(VI_GDB_FILE);
1173 }
1174 }
1175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef SIGHASARG
1178 /* try to find the name of this signal */
1179 for (i = 0; signal_info[i].sig != -1; i++)
1180 if (sigarg == signal_info[i].sig)
1181 break;
1182 deadly_signal = sigarg;
1183#endif
1184
1185 full_screen = FALSE; /* don't write message to the GUI, it might be
1186 * part of the problem... */
1187 /*
1188 * If something goes wrong after entering here, we may get here again.
1189 * When this happens, give a message and try to exit nicely (resetting the
1190 * terminal mode, etc.)
1191 * When this happens twice, just exit, don't even try to give a message,
1192 * stack may be corrupt or something weird.
1193 * When this still happens again (or memory was corrupted in such a way
1194 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1195 */
1196 if (entered >= 3)
1197 {
1198 reset_signals(); /* don't catch any signals anymore */
1199 may_core_dump();
1200 if (entered >= 4)
1201 _exit(8);
1202 exit(7);
1203 }
1204 if (entered == 2)
1205 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001206 /* No translation, it may call malloc(). */
1207 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 out_flush();
1209 getout(1);
1210 }
1211
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001212 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001214 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 signal_info[i].name);
1216#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001217 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001219
1220 /* Preserve files and exit. This sets the really_exiting flag to prevent
1221 * calling free(). */
1222 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaare429e702016-06-10 19:49:14 +02001224 /* NOTREACHED */
1225
Bram Moolenaar009b2592004-10-24 19:18:58 +00001226#ifdef NBDEBUG
1227 reset_signals();
1228 may_core_dump();
1229 abort();
1230#endif
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 SIGRETURN;
1233}
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001236 * Invoked after receiving SIGCONT. We don't know what happened while
1237 * sleeping, deal with part of that.
1238 */
1239 static void
1240after_sigcont(void)
1241{
1242# ifdef FEAT_TITLE
1243 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1244 // again later.
1245 oldtitle_outdated = TRUE;
1246# endif
1247 settmode(TMODE_RAW);
1248 need_check_timestamps = TRUE;
1249 did_check_timestamps = FALSE;
1250}
1251
1252#if defined(SIGCONT)
1253static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001254static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001255
1256/*
1257 * With multi-threading, suspending might not work immediately. Catch the
1258 * SIGCONT signal, which will be used as an indication whether the suspending
1259 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001260 *
1261 * On Linux, signal is not always handled immediately either.
1262 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001263 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001264 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001265 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001267static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001268static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270/*
1271 * signal handler for SIGCONT
1272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 static RETSIGTYPE
1274sigcont_handler SIGDEFARG(sigarg)
1275{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001276 if (in_mch_suspend)
1277 {
1278 sigcont_received = TRUE;
1279 }
1280 else
1281 {
1282 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1283 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1284 // back to a sane mode. We should redraw, but we can't really do that
1285 // in a signal handler, do a redraw later.
1286 after_sigcont();
1287 redraw_later(CLEAR);
1288 cursor_on_force();
1289 out_flush();
1290 }
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 SIGRETURN;
1293}
1294#endif
1295
Bram Moolenaar62b42182010-09-21 22:09:37 +02001296# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001297static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001298# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001299static void save_clipboard(void);
1300static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001301
1302static void *clip_star_save = NULL;
1303static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001304# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001305
1306/*
1307 * Called when Vim is going to sleep or execute a shell command.
1308 * We can't respond to requests for the X selections. Lose them, otherwise
1309 * other applications will hang. But first copy the text to cut buffer 0.
1310 */
1311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001312loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001313{
1314 if (clip_star.owned || clip_plus.owned)
1315 {
1316 x11_export_final_selection();
1317 if (clip_star.owned)
1318 clip_lose_selection(&clip_star);
1319 if (clip_plus.owned)
1320 clip_lose_selection(&clip_plus);
1321 if (x11_display != NULL)
1322 XFlush(x11_display);
1323 }
1324}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001325
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001326# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001327/*
1328 * Save clipboard text to restore later.
1329 */
1330 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001331save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001332{
1333 if (clip_star.owned)
1334 clip_star_save = get_register('*', TRUE);
1335 if (clip_plus.owned)
1336 clip_plus_save = get_register('+', TRUE);
1337}
1338
1339/*
1340 * Restore clipboard text if no one own the X selection.
1341 */
1342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001343restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001344{
1345 if (clip_star_save != NULL)
1346 {
1347 if (!clip_gen_owner_exists(&clip_star))
1348 put_register('*', clip_star_save);
1349 else
1350 free_register(clip_star_save);
1351 clip_star_save = NULL;
1352 }
1353 if (clip_plus_save != NULL)
1354 {
1355 if (!clip_gen_owner_exists(&clip_plus))
1356 put_register('+', clip_plus_save);
1357 else
1358 free_register(clip_plus_save);
1359 clip_plus_save = NULL;
1360 }
1361}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001362# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
1366 * If the machine has job control, use it to suspend the program,
1367 * otherwise fake it by starting a new shell.
1368 */
1369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001370mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 /* BeOS does have SIGTSTP, but it doesn't work. */
1373#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001374 in_mch_suspend = TRUE;
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 out_flush(); /* needed to make cursor visible on some systems */
1377 settmode(TMODE_COOK);
1378 out_flush(); /* needed to disable mouse on some systems */
1379
1380# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001381 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001383# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 sigcont_received = FALSE;
1385# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388
1389# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001390 /*
1391 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001392 * immediately, but somehow not all the time, probably because it's handled
1393 * in another thread. Do not call pause() because there would be race
1394 * condition which would hang Vim if signal happened in between the test of
1395 * sigcont_received and the call to pause(). If signal is not yet received,
1396 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1397 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001398 */
1399 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001400 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001401
Bram Moolenaar262735e2009-07-14 10:20:22 +00001402 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001403 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001406 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar2e310482018-08-21 13:09:10 +02001408 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#else
1410 suspend_shell();
1411#endif
1412}
1413
1414 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001415mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 Columns = 80;
1418 Rows = 24;
1419
1420 out_flush();
1421 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001422
Bram Moolenaar56718732006-03-15 22:53:57 +00001423#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001424 mac_conv_init();
1425#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001426#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1427 win_clip_init();
1428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429}
1430
1431 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001432set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434#if defined(SIGWINCH)
1435 /*
1436 * WINDOW CHANGE signal is handled with sig_winch().
1437 */
1438 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1439#endif
1440
1441 /*
1442 * We want the STOP signal to work, to make mch_suspend() work.
1443 * For "rvim" the STOP signal is ignored.
1444 */
1445#ifdef SIGTSTP
1446 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1447#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001448#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 signal(SIGCONT, sigcont_handler);
1450#endif
1451
1452 /*
1453 * We want to ignore breaking of PIPEs.
1454 */
1455#ifdef SIGPIPE
1456 signal(SIGPIPE, SIG_IGN);
1457#endif
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001460 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#endif
1462
1463 /*
1464 * Ignore alarm signals (Perl's alarm() generates it).
1465 */
1466#ifdef SIGALRM
1467 signal(SIGALRM, SIG_IGN);
1468#endif
1469
1470 /*
1471 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1472 * work will be lost.
1473 */
1474#ifdef SIGPWR
1475 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1476#endif
1477
1478 /*
1479 * Arrange for other signals to gracefully shutdown Vim.
1480 */
1481 catch_signals(deathtrap, SIG_ERR);
1482
1483#if defined(FEAT_GUI) && defined(SIGHUP)
1484 /*
1485 * When the GUI is running, ignore the hangup signal.
1486 */
1487 if (gui.in_use)
1488 signal(SIGHUP, SIG_IGN);
1489#endif
1490}
1491
Bram Moolenaardf177f62005-02-22 08:39:57 +00001492#if defined(SIGINT) || defined(PROTO)
1493/*
1494 * Catch CTRL-C (only works while in Cooked mode).
1495 */
1496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001497catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001498{
1499 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1500}
1501#endif
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001504reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001507#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 /* SIGCONT isn't in the list, because its default action is ignore */
1509 signal(SIGCONT, SIG_DFL);
1510#endif
1511}
1512
1513 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001514catch_signals(
1515 RETSIGTYPE (*func_deadly)(),
1516 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 int i;
1519
1520 for (i = 0; signal_info[i].sig != -1; i++)
1521 if (signal_info[i].deadly)
1522 {
1523#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1524 struct sigaction sa;
1525
1526 /* Setup to use the alternate stack for the signal function. */
1527 sa.sa_handler = func_deadly;
1528 sigemptyset(&sa.sa_mask);
1529# if defined(__linux__) && defined(_REENTRANT)
1530 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1531 * thread handling in combination with using the alternate stack:
1532 * pthread library functions try to use the stack pointer to
1533 * identify the current thread, causing a SEGV signal, which
1534 * recursively calls deathtrap() and hangs. */
1535 sa.sa_flags = 0;
1536# else
1537 sa.sa_flags = SA_ONSTACK;
1538# endif
1539 sigaction(signal_info[i].sig, &sa, NULL);
1540#else
1541# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1542 struct sigvec sv;
1543
1544 /* Setup to use the alternate stack for the signal function. */
1545 sv.sv_handler = func_deadly;
1546 sv.sv_mask = 0;
1547 sv.sv_flags = SV_ONSTACK;
1548 sigvec(signal_info[i].sig, &sv, NULL);
1549# else
1550 signal(signal_info[i].sig, func_deadly);
1551# endif
1552#endif
1553 }
1554 else if (func_other != SIG_ERR)
1555 signal(signal_info[i].sig, func_other);
1556}
1557
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001558#ifdef HAVE_SIGPROCMASK
1559 static void
1560block_signals(sigset_t *set)
1561{
1562 sigset_t newset;
1563 int i;
1564
1565 sigemptyset(&newset);
1566
1567 for (i = 0; signal_info[i].sig != -1; i++)
1568 sigaddset(&newset, signal_info[i].sig);
1569
Bram Moolenaar2e310482018-08-21 13:09:10 +02001570# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001571 /* SIGCONT isn't in the list, because its default action is ignore */
1572 sigaddset(&newset, SIGCONT);
1573# endif
1574
1575 sigprocmask(SIG_BLOCK, &newset, set);
1576}
1577
1578 static void
1579unblock_signals(sigset_t *set)
1580{
1581 sigprocmask(SIG_SETMASK, set, NULL);
1582}
1583#endif
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001586 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001587 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1588 * return TRUE
1589 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1590 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001591 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001592 * Returns TRUE when Vim should exit.
1593 */
1594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001595vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001596{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001597 static int got_signal = 0;
1598 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001599
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001600 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001601 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001602 case SIGNAL_BLOCK: blocked = TRUE;
1603 break;
1604
1605 case SIGNAL_UNBLOCK: blocked = FALSE;
1606 if (got_signal != 0)
1607 {
1608 kill(getpid(), got_signal);
1609 got_signal = 0;
1610 }
1611 break;
1612
1613 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001614 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001615 got_signal = sig;
1616#ifdef SIGPWR
1617 if (sig != SIGPWR)
1618#endif
1619 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001620 break;
1621 }
1622 return FALSE;
1623}
1624
1625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * Check_win checks whether we have an interactive stdout.
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001629mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (isatty(1))
1632 return OK;
1633 return FAIL;
1634}
1635
1636/*
1637 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1638 */
1639 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001640mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 if (isatty(read_cmd_fd))
1643 return TRUE;
1644 return FALSE;
1645}
1646
1647#ifdef FEAT_X11
1648
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001649# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652/*
1653 * Give a message about the elapsed time for opening the X window.
1654 */
1655 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001656xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001658 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659}
1660# endif
1661#endif
1662
1663#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1664/*
1665 * A few functions shared by X11 title and clipboard code.
1666 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001667static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1668static int x_error_check(Display *dpy, XErrorEvent *error_event);
1669static int x_connect_to_server(void);
1670static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672static int got_x_error = FALSE;
1673
1674/*
1675 * X Error handler, otherwise X just exits! (very rude) -- webb
1676 */
1677 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001678x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001680 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 STRCAT(IObuff, _("\nVim: Got X error\n"));
1682
1683 /* We cannot print a message and continue, because no X calls are allowed
1684 * here (causes my system to hang). Silently continuing might be an
1685 * alternative... */
1686 preserve_exit(); /* preserve files and exit */
1687
1688 return 0; /* NOTREACHED */
1689}
1690
1691/*
1692 * Another X Error handler, just used to check for errors.
1693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001695x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 got_x_error = TRUE;
1698 return 0;
1699}
1700
1701#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1702# if defined(HAVE_SETJMP_H)
1703/*
1704 * An X IO Error handler, used to catch error while opening the display.
1705 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001706static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001709x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 /* This function should not return, it causes exit(). Longjump instead. */
1712 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001713# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001714 return 0; /* avoid the compiler complains about missing return value */
1715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716}
1717# endif
1718
1719/*
1720 * An X IO Error handler, used to catch terminal errors.
1721 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001722static int x_IOerror_handler(Display *dpy);
1723static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001724static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001727x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001730 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 x11_window = 0;
1732 x11_display = NULL;
1733 xterm_Shell = (Widget)0;
1734
1735 /* This function should not return, it causes exit(). Longjump instead. */
1736 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001737# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001738 return 0; /* avoid the compiler complains about missing return value */
1739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001741
1742/*
1743 * If the X11 connection was lost try to restore it.
1744 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001745 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001746 */
1747 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001748may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001749{
1750 if (xterm_dpy_was_reset)
1751 {
1752 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001753
1754# ifndef LESSTIF_VERSION
1755 /* This has been reported to avoid Vim getting stuck. */
1756 if (app_context != (XtAppContext)NULL)
1757 {
1758 XtDestroyApplicationContext(app_context);
1759 app_context = (XtAppContext)NULL;
1760 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1761 }
1762# endif
1763
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001764 setup_term_clip();
1765 get_x11_title(FALSE);
1766 }
1767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769
1770/*
1771 * Return TRUE when connection to the X server is desired.
1772 */
1773 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001774x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#if defined(FEAT_CLIENTSERVER)
1777 if (x_force_connect)
1778 return TRUE;
1779#endif
1780 if (x_no_connect)
1781 return FALSE;
1782
1783 /* Check for a match with "exclude:" from 'clipboard'. */
1784 if (clip_exclude_prog != NULL)
1785 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001786 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 return FALSE;
1788 }
1789 return TRUE;
1790}
1791
1792/*
1793 * Test if "dpy" and x11_window are valid by getting the window title.
1794 * I don't actually want it yet, so there may be a simpler call to use, but
1795 * this will cause the error handler x_error_check() to be called if anything
1796 * is wrong, such as the window pointer being invalid (as can happen when the
1797 * user changes his DISPLAY, but not his WINDOWID) -- webb
1798 */
1799 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001800test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 int (*old_handler)();
1803 XTextProperty text_prop;
1804
1805 old_handler = XSetErrorHandler(x_error_check);
1806 got_x_error = FALSE;
1807 if (XGetWMName(dpy, x11_window, &text_prop))
1808 XFree((void *)text_prop.value);
1809 XSync(dpy, False);
1810 (void)XSetErrorHandler(old_handler);
1811
1812 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001813 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 return (got_x_error ? FAIL : OK);
1816}
1817#endif
1818
1819#ifdef FEAT_TITLE
1820
1821#ifdef FEAT_X11
1822
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001823static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825/*
1826 * try to get x11 window and display
1827 *
1828 * return FAIL for failure, OK otherwise
1829 */
1830 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001831get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
1833 char *winid;
1834 static int result = -1;
1835#define XD_NONE 0 /* x11_display not set here */
1836#define XD_HERE 1 /* x11_display opened here */
1837#define XD_GUI 2 /* x11_display used from gui.dpy */
1838#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1839 static int x11_display_from = XD_NONE;
1840 static int did_set_error_handler = FALSE;
1841
1842 if (!did_set_error_handler)
1843 {
1844 /* X just exits if it finds an error otherwise! */
1845 (void)XSetErrorHandler(x_error_handler);
1846 did_set_error_handler = TRUE;
1847 }
1848
Bram Moolenaar9372a112005-12-06 19:59:18 +00001849#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (gui.in_use)
1851 {
1852 /*
1853 * If the X11 display was opened here before, for the window where Vim
1854 * was started, close that one now to avoid a memory leak.
1855 */
1856 if (x11_display_from == XD_HERE && x11_display != NULL)
1857 {
1858 XCloseDisplay(x11_display);
1859 x11_display_from = XD_NONE;
1860 }
1861 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1862 {
1863 x11_display_from = XD_GUI;
1864 return OK;
1865 }
1866 x11_display = NULL;
1867 return FAIL;
1868 }
1869 else if (x11_display_from == XD_GUI)
1870 {
1871 /* GUI must have stopped somehow, clear x11_display */
1872 x11_window = 0;
1873 x11_display = NULL;
1874 x11_display_from = XD_NONE;
1875 }
1876#endif
1877
1878 /* When started with the "-X" argument, don't try connecting. */
1879 if (!x_connect_to_server())
1880 return FAIL;
1881
1882 /*
1883 * If WINDOWID not set, should try another method to find out
1884 * what the current window number is. The only code I know for
1885 * this is very complicated.
1886 * We assume that zero is invalid for WINDOWID.
1887 */
1888 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1889 x11_window = (Window)atol(winid);
1890
1891#ifdef FEAT_XCLIPBOARD
1892 if (xterm_dpy != NULL && x11_window != 0)
1893 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001894 /* We may have checked it already, but Gnome terminal can move us to
1895 * another window, so we need to check every time. */
1896 if (x11_display_from != XD_XTERM)
1897 {
1898 /*
1899 * If the X11 display was opened here before, for the window where
1900 * Vim was started, close that one now to avoid a memory leak.
1901 */
1902 if (x11_display_from == XD_HERE && x11_display != NULL)
1903 XCloseDisplay(x11_display);
1904 x11_display = xterm_dpy;
1905 x11_display_from = XD_XTERM;
1906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (test_x11_window(x11_display) == FAIL)
1908 {
1909 /* probably bad $WINDOWID */
1910 x11_window = 0;
1911 x11_display = NULL;
1912 x11_display_from = XD_NONE;
1913 return FAIL;
1914 }
1915 return OK;
1916 }
1917#endif
1918
1919 if (x11_window == 0 || x11_display == NULL)
1920 result = -1;
1921
1922 if (result != -1) /* Have already been here and set this */
1923 return result; /* Don't do all these X calls again */
1924
1925 if (x11_window != 0 && x11_display == NULL)
1926 {
1927#ifdef SET_SIG_ALARM
1928 RETSIGTYPE (*sig_save)();
1929#endif
1930#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1931 struct timeval start_tv;
1932
1933 if (p_verbose > 0)
1934 gettimeofday(&start_tv, NULL);
1935#endif
1936
1937#ifdef SET_SIG_ALARM
1938 /*
1939 * Opening the Display may hang if the DISPLAY setting is wrong, or
1940 * the network connection is bad. Set an alarm timer to get out.
1941 */
1942 sig_alarm_called = FALSE;
1943 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1944 (RETSIGTYPE (*)())sig_alarm);
1945 alarm(2);
1946#endif
1947 x11_display = XOpenDisplay(NULL);
1948
1949#ifdef SET_SIG_ALARM
1950 alarm(0);
1951 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1952 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001953 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
1955 if (x11_display != NULL)
1956 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001957# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001959 {
1960 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001961 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001962 verbose_leave();
1963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964# endif
1965 if (test_x11_window(x11_display) == FAIL)
1966 {
1967 /* Maybe window id is bad */
1968 x11_window = 0;
1969 XCloseDisplay(x11_display);
1970 x11_display = NULL;
1971 }
1972 else
1973 x11_display_from = XD_HERE;
1974 }
1975 }
1976 if (x11_window == 0 || x11_display == NULL)
1977 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001978
1979# ifdef FEAT_EVAL
1980 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1981# endif
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 return (result = OK);
1984}
1985
1986/*
1987 * Determine original x11 Window Title
1988 */
1989 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001990get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001992 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993}
1994
1995/*
1996 * Determine original x11 Window icon
1997 */
1998 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001999get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 int retval = FALSE;
2002
2003 retval = get_x11_thing(FALSE, test_only);
2004
2005 /* could not get old icon, use terminal name */
2006 if (oldicon == NULL && !test_only)
2007 {
2008 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002009 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002011 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013
2014 return retval;
2015}
2016
2017 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002018get_x11_thing(
2019 int get_title, /* get title string */
2020 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 XTextProperty text_prop;
2023 int retval = FALSE;
2024 Status status;
2025
2026 if (get_x11_windis() == OK)
2027 {
2028 /* Get window/icon name if any */
2029 if (get_title)
2030 status = XGetWMName(x11_display, x11_window, &text_prop);
2031 else
2032 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2033
2034 /*
2035 * If terminal is xterm, then x11_window may be a child window of the
2036 * outer xterm window that actually contains the window/icon name, so
2037 * keep traversing up the tree until a window with a title/icon is
2038 * found.
2039 */
2040 /* Previously this was only done for xterm and alikes. I don't see a
2041 * reason why it would fail for other terminal emulators.
2042 * if (term_is_xterm) */
2043 {
2044 Window root;
2045 Window parent;
2046 Window win = x11_window;
2047 Window *children;
2048 unsigned int num_children;
2049
2050 while (!status || text_prop.value == NULL)
2051 {
2052 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2053 &num_children))
2054 break;
2055 if (children)
2056 XFree((void *)children);
2057 if (parent == root || parent == 0)
2058 break;
2059
2060 win = parent;
2061 if (get_title)
2062 status = XGetWMName(x11_display, win, &text_prop);
2063 else
2064 status = XGetWMIconName(x11_display, win, &text_prop);
2065 }
2066 }
2067 if (status && text_prop.value != NULL)
2068 {
2069 retval = TRUE;
2070 if (!test_only)
2071 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002072#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2073 if (text_prop.encoding == XA_STRING
2074# ifdef FEAT_MBYTE
2075 && !has_mbyte
2076# endif
2077 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079#endif
2080 if (get_title)
2081 oldtitle = vim_strsave((char_u *)text_prop.value);
2082 else
2083 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002084#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086 else
2087 {
2088 char **cl;
2089 Status transform_status;
2090 int n = 0;
2091
2092 transform_status = XmbTextPropertyToTextList(x11_display,
2093 &text_prop,
2094 &cl, &n);
2095 if (transform_status >= Success && n > 0 && cl[0])
2096 {
2097 if (get_title)
2098 oldtitle = vim_strsave((char_u *) cl[0]);
2099 else
2100 oldicon = vim_strsave((char_u *) cl[0]);
2101 XFreeStringList(cl);
2102 }
2103 else
2104 {
2105 if (get_title)
2106 oldtitle = vim_strsave((char_u *)text_prop.value);
2107 else
2108 oldicon = vim_strsave((char_u *)text_prop.value);
2109 }
2110 }
2111#endif
2112 }
2113 XFree((void *)text_prop.value);
2114 }
2115 }
2116 return retval;
2117}
2118
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002119/* Xutf8 functions are not avaialble on older systems. Note that on some
2120 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2121 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2122 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002124# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125# define USE_UTF8_STRING
2126# endif
2127#endif
2128
2129/*
2130 * Set x11 Window Title
2131 *
2132 * get_x11_windis() must be called before this and have returned OK
2133 */
2134 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002135set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2138 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2139 * supported everywhere and STRING doesn't work for multi-byte titles.
2140 */
2141#ifdef USE_UTF8_STRING
2142 if (enc_utf8)
2143 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2144 NULL, NULL, 0, NULL, NULL, NULL);
2145 else
2146#endif
2147 {
2148#if XtSpecificationRelease >= 4
2149# ifdef FEAT_XFONTSET
2150 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2151 NULL, NULL, 0, NULL, NULL, NULL);
2152# else
2153 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002154 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002157 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 XSetWMProperties(x11_display, x11_window, &text_prop,
2159 NULL, NULL, 0, NULL, NULL, NULL);
2160# endif
2161#else
2162 XStoreName(x11_display, x11_window, (char *)title);
2163#endif
2164 }
2165 XFlush(x11_display);
2166}
2167
2168/*
2169 * Set x11 Window icon
2170 *
2171 * get_x11_windis() must be called before this and have returned OK
2172 */
2173 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002174set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 /* See above for comments about using X*SetWMProperties(). */
2177#ifdef USE_UTF8_STRING
2178 if (enc_utf8)
2179 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2180 NULL, 0, NULL, NULL, NULL);
2181 else
2182#endif
2183 {
2184#if XtSpecificationRelease >= 4
2185# ifdef FEAT_XFONTSET
2186 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2187 NULL, 0, NULL, NULL, NULL);
2188# else
2189 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002190 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002192 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2194 NULL, 0, NULL, NULL, NULL);
2195# endif
2196#else
2197 XSetIconName(x11_display, x11_window, (char *)icon);
2198#endif
2199 }
2200 XFlush(x11_display);
2201}
2202
2203#else /* FEAT_X11 */
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002206get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208 return FALSE;
2209}
2210
2211 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002212get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
2214 if (!test_only)
2215 {
2216 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002217 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002219 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 }
2221 return FALSE;
2222}
2223
2224#endif /* FEAT_X11 */
2225
2226 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002227mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 return get_x11_title(TRUE);
2230}
2231
2232 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002233mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 return get_x11_icon(TRUE);
2236}
2237
2238/*
2239 * Set the window title and icon.
2240 */
2241 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002242mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 int type = 0;
2245 static int recursive = 0;
2246
2247 if (T_NAME == NULL) /* no terminal name (yet) */
2248 return;
2249 if (title == NULL && icon == NULL) /* nothing to do */
2250 return;
2251
2252 /* When one of the X11 functions causes a deadly signal, we get here again
2253 * recursively. Avoid hanging then (something is probably locked). */
2254 if (recursive)
2255 return;
2256 ++recursive;
2257
2258 /*
2259 * if the window ID and the display is known, we may use X11 calls
2260 */
2261#ifdef FEAT_X11
2262 if (get_x11_windis() == OK)
2263 type = 1;
2264#else
2265# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2266 if (gui.in_use)
2267 type = 1;
2268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#endif
2270
2271 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002272 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 * than x11 calls, because the x11 calls don't always work
2274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if ((type || *T_TS != NUL) && title != NULL)
2276 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002277 if (oldtitle_outdated)
2278 {
2279 oldtitle_outdated = FALSE;
2280 VIM_CLEAR(oldtitle);
2281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (oldtitle == NULL
2283#ifdef FEAT_GUI
2284 && !gui.in_use
2285#endif
2286 ) /* first call but not in GUI, save title */
2287 (void)get_x11_title(FALSE);
2288
2289 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2290 term_settitle(title);
2291#ifdef FEAT_X11
2292 else
2293# ifdef FEAT_GUI_GTK
2294 if (!gui.in_use) /* don't do this if GTK+ is running */
2295# endif
2296 set_x11_title(title); /* x11 */
2297#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002298#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2300 else
2301 gui_mch_settitle(title, icon);
2302#endif
2303 did_set_title = TRUE;
2304 }
2305
2306 if ((type || *T_CIS != NUL) && icon != NULL)
2307 {
2308 if (oldicon == NULL
2309#ifdef FEAT_GUI
2310 && !gui.in_use
2311#endif
2312 ) /* first call, save icon */
2313 get_x11_icon(FALSE);
2314
2315 if (*T_CIS != NUL)
2316 {
2317 out_str(T_CIS); /* set icon start */
2318 out_str_nf(icon);
2319 out_str(T_CIE); /* set icon end */
2320 out_flush();
2321 }
2322#ifdef FEAT_X11
2323 else
2324# ifdef FEAT_GUI_GTK
2325 if (!gui.in_use) /* don't do this if GTK+ is running */
2326# endif
2327 set_x11_icon(icon); /* x11 */
2328#endif
2329 did_set_icon = TRUE;
2330 }
2331 --recursive;
2332}
2333
2334/*
2335 * Restore the window/icon title.
2336 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002337 * SAVE_RESTORE_TITLE only restore title
2338 * SAVE_RESTORE_ICON only restore icon
2339 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 */
2341 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002342mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002345 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002347 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2348
2349 // pop and push from/to the stack
2350 term_pop_title(which);
2351 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352}
2353
2354#endif /* FEAT_TITLE */
2355
2356/*
2357 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002358 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
2360 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002361vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 if (name == NULL)
2364 return FALSE;
2365 return (STRNICMP(name, "xterm", 5) == 0
2366 || STRNICMP(name, "nxterm", 6) == 0
2367 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002368 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002370 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 || STRCMP(name, "builtin_xterm") == 0);
2372}
2373
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002374#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2375/*
2376 * Return TRUE if "name" appears to be that of a terminal
2377 * known to support the xterm-style mouse protocol.
2378 * Relies on term_is_xterm having been set to its correct value.
2379 */
2380 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002381use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002382{
2383 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002384 && (term_is_xterm
2385 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002386 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002387 || STRICMP(name, "st") == 0
2388 || STRNICMP(name, "st-", 3) == 0
2389 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002390}
2391#endif
2392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2394/*
2395 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2396 * Return 1 for "xterm".
2397 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002398 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002399 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 */
2401 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002402use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002404 if (ttym_flags == TTYM_SGR)
2405 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002406 if (ttym_flags == TTYM_URXVT)
2407 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (ttym_flags == TTYM_XTERM2)
2409 return 2;
2410 if (ttym_flags == TTYM_XTERM)
2411 return 1;
2412 return 0;
2413}
2414#endif
2415
2416 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002417vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 if (name == NULL)
2420 return FALSE;
2421 return (STRNICMP(name, "iris-ansi", 9) == 0
2422 || STRCMP(name, "builtin_iris-ansi") == 0);
2423}
2424
2425 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002426vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 if (name == NULL)
2429 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002430 /* catch VT100 - VT5xx */
2431 return ((STRNICMP(name, "vt", 2) == 0
2432 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 || STRCMP(name, "builtin_vt320") == 0);
2434}
2435
2436/*
2437 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2438 * This should include all windowed terminal emulators.
2439 */
2440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002441vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 if (name == NULL)
2444 return FALSE;
2445 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2446 return TRUE;
2447 return ( STRNICMP(name, "hpterm", 6) == 0
2448 || STRNICMP(name, "sun-cmd", 7) == 0
2449 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002450 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 || STRNICMP(name, "dtterm", 6) == 0);
2452}
2453
2454/*
2455 * Insert user name in s[len].
2456 * Return OK if a name found.
2457 */
2458 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002459mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002462 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 return OK;
2464#else
2465 return mch_get_uname(getuid(), s, len);
2466#endif
2467}
2468
2469/*
2470 * Insert user name for "uid" in s[len].
2471 * Return OK if a name found.
2472 */
2473 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002474mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2477 struct passwd *pw;
2478
2479 if ((pw = getpwuid(uid)) != NULL
2480 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2481 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002482 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return OK;
2484 }
2485#endif
2486 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2487 return FAIL; /* a number is not a name */
2488}
2489
2490/*
2491 * Insert host name is s[len].
2492 */
2493
2494#ifdef HAVE_SYS_UTSNAME_H
2495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002496mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 struct utsname vutsname;
2499
2500 if (uname(&vutsname) < 0)
2501 *s = NUL;
2502 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002503 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504}
2505#else /* HAVE_SYS_UTSNAME_H */
2506
2507# ifdef HAVE_SYS_SYSTEMINFO_H
2508# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2509# endif
2510
2511 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002512mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514# ifdef VAXC
2515 vaxc$gethostname((char *)s, len);
2516# else
2517 gethostname((char *)s, len);
2518# endif
2519 s[len - 1] = NUL; /* make sure it's terminated */
2520}
2521#endif /* HAVE_SYS_UTSNAME_H */
2522
2523/*
2524 * return process ID
2525 */
2526 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002527mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
2529 return (long)getpid();
2530}
2531
2532#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002533static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002536strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 extern int sys_nerr;
2539 extern char *sys_errlist[];
2540 static char er[20];
2541
2542 if (err > 0 && err < sys_nerr)
2543 return (sys_errlist[err]);
2544 sprintf(er, "Error %d", err);
2545 return er;
2546}
2547#endif
2548
2549/*
2550 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2551 * Return OK for success, FAIL for failure.
2552 */
2553 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002554mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556#if defined(USE_GETCWD)
2557 if (getcwd((char *)buf, len) == NULL)
2558 {
2559 STRCPY(buf, strerror(errno));
2560 return FAIL;
2561 }
2562 return OK;
2563#else
2564 return (getwd((char *)buf) != NULL ? OK : FAIL);
2565#endif
2566}
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002569 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 *
2571 * return FAIL for failure, OK for success
2572 */
2573 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002574mch_FullName(
2575 char_u *fname,
2576 char_u *buf,
2577 int len,
2578 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002581#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 int fd = -1;
2583 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 char_u olddir[MAXPATHL];
2586 char_u *p;
2587 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002588#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002589 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2590 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002591#endif
2592
Bram Moolenaar38323e42007-03-06 19:22:53 +00002593#ifdef VMS
2594 fname = vms_fixfilename(fname);
2595#endif
2596
Bram Moolenaara2442432007-04-26 14:26:37 +00002597#ifdef __CYGWIN__
2598 /*
2599 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2600 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002601# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002602 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2603 * a forward slash. */
2604 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2605 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002606# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002607 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002608# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002609 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002610#endif
2611
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002612 /* Expand it if forced or not an absolute path.
2613 * Do not do it for "/file", the result is always "/". */
2614 if ((force || !mch_isFullName(fname))
2615 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 /*
2618 * If the file name has a path, change to that directory for a moment,
2619 * and then do the getwd() (and get back to where we were).
2620 * This will get the correct path name with "../" things.
2621 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002622 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002624#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /*
2626 * Use fchdir() if possible, it's said to be faster and more
2627 * reliable. But on SunOS 4 it might not work. Check this by
2628 * doing a fchdir() right now.
2629 */
2630 if (!dont_fchdir)
2631 {
2632 fd = open(".", O_RDONLY | O_EXTRA, 0);
2633 if (fd >= 0 && fchdir(fd) < 0)
2634 {
2635 close(fd);
2636 fd = -1;
2637 dont_fchdir = TRUE; /* don't try again */
2638 }
2639 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /* Only change directory when we are sure we can return to where
2643 * we are now. After doing "su" chdir(".") might not work. */
2644 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002645#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 (mch_dirname(olddir, MAXPATHL) == FAIL
2649 || mch_chdir((char *)olddir) != 0))
2650 {
2651 p = NULL; /* can't get current dir: don't chdir */
2652 retval = FAIL;
2653 }
2654 else
2655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 /* The directory is copied into buf[], to be able to remove
2657 * the file name without changing it (could be a string in
2658 * read-only memory) */
2659 if (p - fname >= len)
2660 retval = FAIL;
2661 else
2662 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002663 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (mch_chdir((char *)buf))
2665 retval = FAIL;
2666 else
2667 fname = p + 1;
2668 *buf = NUL;
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671 }
2672 if (mch_dirname(buf, len) == FAIL)
2673 {
2674 retval = FAIL;
2675 *buf = NUL;
2676 }
2677 if (p != NULL)
2678 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002679#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (fd >= 0)
2681 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002682 if (p_verbose >= 5)
2683 {
2684 verbose_enter();
2685 MSG("fchdir() to previous dir");
2686 verbose_leave();
2687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 l = fchdir(fd);
2689 close(fd);
2690 }
2691 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 l = mch_chdir((char *)olddir);
2694 if (l != 0)
2695 EMSG(_(e_prev_dir));
2696 }
2697
2698 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002699 if (l >= len - 1)
2700 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002701#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002702 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002704 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002709 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 return FAIL;
2711
2712 /* Do not append ".", "/dir/." is equal to "/dir". */
2713 if (STRCMP(fname, ".") != 0)
2714 STRCAT(buf, fname);
2715
2716 return OK;
2717}
2718
2719/*
2720 * Return TRUE if "fname" does not depend on the current directory.
2721 */
2722 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002723mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002725#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 return ( fname[0] == '/' || fname[0] == '.' ||
2727 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2728 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2729 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002730#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732#endif
2733}
2734
Bram Moolenaar24552be2005-12-10 20:17:30 +00002735#if defined(USE_FNAME_CASE) || defined(PROTO)
2736/*
2737 * Set the case of the file name, if it already exists. This will cause the
2738 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002739 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002740 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002741 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002742fname_case(
2743 char_u *name,
2744 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002745{
2746 struct stat st;
2747 char_u *slash, *tail;
2748 DIR *dirp;
2749 struct dirent *dp;
2750
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002751 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002752 {
2753 /* Open the directory where the file is located. */
2754 slash = vim_strrchr(name, '/');
2755 if (slash == NULL)
2756 {
2757 dirp = opendir(".");
2758 tail = name;
2759 }
2760 else
2761 {
2762 *slash = NUL;
2763 dirp = opendir((char *)name);
2764 *slash = '/';
2765 tail = slash + 1;
2766 }
2767
2768 if (dirp != NULL)
2769 {
2770 while ((dp = readdir(dirp)) != NULL)
2771 {
2772 /* Only accept names that differ in case and are the same byte
2773 * length. TODO: accept different length name. */
2774 if (STRICMP(tail, dp->d_name) == 0
2775 && STRLEN(tail) == STRLEN(dp->d_name))
2776 {
2777 char_u newname[MAXPATHL + 1];
2778 struct stat st2;
2779
2780 /* Verify the inode is equal. */
2781 vim_strncpy(newname, name, MAXPATHL);
2782 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2783 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002784 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002785 && st.st_ino == st2.st_ino
2786 && st.st_dev == st2.st_dev)
2787 {
2788 STRCPY(tail, dp->d_name);
2789 break;
2790 }
2791 }
2792 }
2793
2794 closedir(dirp);
2795 }
2796 }
2797}
2798#endif
2799
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800/*
2801 * Get file permissions for 'name'.
2802 * Returns -1 when it doesn't exist.
2803 */
2804 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002805mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
2807 struct stat statb;
2808
2809 /* Keep the #ifdef outside of stat(), it may be a macro. */
2810#ifdef VMS
2811 if (stat((char *)vms_fixfilename(name), &statb))
2812#else
2813 if (stat((char *)name, &statb))
2814#endif
2815 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002816#ifdef __INTERIX
2817 /* The top bit makes the value negative, which means the file doesn't
2818 * exist. Remove the bit, we don't use it. */
2819 return statb.st_mode & ~S_ADDACE;
2820#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823}
2824
2825/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002826 * Set file permission for "name" to "perm".
2827 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
2829 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002830mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 return (chmod((char *)
2833#ifdef VMS
2834 vms_fixfilename(name),
2835#else
2836 name,
2837#endif
2838 (mode_t)perm) == 0 ? OK : FAIL);
2839}
2840
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002841#if defined(HAVE_FCHMOD) || defined(PROTO)
2842/*
2843 * Set file permission for open file "fd" to "perm".
2844 * Return FAIL for failure, OK otherwise.
2845 */
2846 int
2847mch_fsetperm(int fd, long perm)
2848{
2849 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2850}
2851#endif
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853#if defined(HAVE_ACL) || defined(PROTO)
2854# ifdef HAVE_SYS_ACL_H
2855# include <sys/acl.h>
2856# endif
2857# ifdef HAVE_SYS_ACCESS_H
2858# include <sys/access.h>
2859# endif
2860
2861# ifdef HAVE_SOLARIS_ACL
2862typedef struct vim_acl_solaris_T {
2863 int acl_cnt;
2864 aclent_t *acl_entry;
2865} vim_acl_solaris_T;
2866# endif
2867
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002868#if defined(HAVE_SELINUX) || defined(PROTO)
2869/*
2870 * Copy security info from "from_file" to "to_file".
2871 */
2872 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002873mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002874{
2875 if (from_file == NULL)
2876 return;
2877
2878 if (selinux_enabled == -1)
2879 selinux_enabled = is_selinux_enabled();
2880
2881 if (selinux_enabled > 0)
2882 {
2883 security_context_t from_context = NULL;
2884 security_context_t to_context = NULL;
2885
2886 if (getfilecon((char *)from_file, &from_context) < 0)
2887 {
2888 /* If the filesystem doesn't support extended attributes,
2889 the original had no special security context and the
2890 target cannot have one either. */
2891 if (errno == EOPNOTSUPP)
2892 return;
2893
2894 MSG_PUTS(_("\nCould not get security context for "));
2895 msg_outtrans(from_file);
2896 msg_putchar('\n');
2897 return;
2898 }
2899 if (getfilecon((char *)to_file, &to_context) < 0)
2900 {
2901 MSG_PUTS(_("\nCould not get security context for "));
2902 msg_outtrans(to_file);
2903 msg_putchar('\n');
2904 freecon (from_context);
2905 return ;
2906 }
2907 if (strcmp(from_context, to_context) != 0)
2908 {
2909 if (setfilecon((char *)to_file, from_context) < 0)
2910 {
2911 MSG_PUTS(_("\nCould not set security context for "));
2912 msg_outtrans(to_file);
2913 msg_putchar('\n');
2914 }
2915 }
2916 freecon(to_context);
2917 freecon(from_context);
2918 }
2919}
2920#endif /* HAVE_SELINUX */
2921
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002922#if defined(HAVE_SMACK) && !defined(PROTO)
2923/*
2924 * Copy security info from "from_file" to "to_file".
2925 */
2926 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002927mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002928{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002929 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002930 {
2931 XATTR_NAME_SMACK,
2932 XATTR_NAME_SMACKEXEC,
2933 XATTR_NAME_SMACKMMAP
2934 };
2935
2936 char buffer[SMACK_LABEL_LEN];
2937 const char *name;
2938 int index;
2939 int ret;
2940 ssize_t size;
2941
2942 if (from_file == NULL)
2943 return;
2944
2945 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2946 / sizeof(smack_copied_attributes)[0]) ; index++)
2947 {
2948 /* get the name of the attribute to copy */
2949 name = smack_copied_attributes[index];
2950
2951 /* get the value of the attribute in buffer */
2952 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2953 if (size >= 0)
2954 {
2955 /* copy the attribute value of buffer */
2956 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2957 if (ret < 0)
2958 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002959 vim_snprintf((char *)IObuff, IOSIZE,
2960 _("Could not set security context %s for %s"),
2961 name, to_file);
2962 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002963 msg_putchar('\n');
2964 }
2965 }
2966 else
2967 {
2968 /* what reason of not having the attribute value? */
2969 switch (errno)
2970 {
2971 case ENOTSUP:
2972 /* extended attributes aren't supported or enabled */
2973 /* should a message be echoed? not sure... */
2974 return; /* leave because it isn't usefull to continue */
2975
2976 case ERANGE:
2977 default:
2978 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002979 vim_snprintf((char *)IObuff, IOSIZE,
2980 _("Could not get security context %s for %s. Removing it!"),
2981 name, from_file);
2982 msg_puts(IObuff);
2983 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002984 /* FALLTHROUGH to remove the attribute */
2985
2986 case ENODATA:
2987 /* no attribute of this name */
2988 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002989 /* Silently ignore errors, apparently this happens when
2990 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002991 break;
2992 }
2993 }
2994 }
2995}
2996#endif /* HAVE_SMACK */
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
2999 * Return a pointer to the ACL of file "fname" in allocated memory.
3000 * Return NULL if the ACL is not available for whatever reason.
3001 */
3002 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003003mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 vim_acl_T ret = NULL;
3006#ifdef HAVE_POSIX_ACL
3007 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3008#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003009#ifdef HAVE_SOLARIS_ZFS_ACL
3010 acl_t *aclent;
3011
3012 if (acl_get((char *)fname, 0, &aclent) < 0)
3013 return NULL;
3014 ret = (vim_acl_T)aclent;
3015#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#ifdef HAVE_SOLARIS_ACL
3017 vim_acl_solaris_T *aclent;
3018
3019 aclent = malloc(sizeof(vim_acl_solaris_T));
3020 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3021 {
3022 free(aclent);
3023 return NULL;
3024 }
3025 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3026 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3027 {
3028 free(aclent->acl_entry);
3029 free(aclent);
3030 return NULL;
3031 }
3032 ret = (vim_acl_T)aclent;
3033#else
3034#if defined(HAVE_AIX_ACL)
3035 int aclsize;
3036 struct acl *aclent;
3037
3038 aclsize = sizeof(struct acl);
3039 aclent = malloc(aclsize);
3040 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3041 {
3042 if (errno == ENOSPC)
3043 {
3044 aclsize = aclent->acl_len;
3045 aclent = realloc(aclent, aclsize);
3046 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3047 {
3048 free(aclent);
3049 return NULL;
3050 }
3051 }
3052 else
3053 {
3054 free(aclent);
3055 return NULL;
3056 }
3057 }
3058 ret = (vim_acl_T)aclent;
3059#endif /* HAVE_AIX_ACL */
3060#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003061#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062#endif /* HAVE_POSIX_ACL */
3063 return ret;
3064}
3065
3066/*
3067 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3068 */
3069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003070mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 if (aclent == NULL)
3073 return;
3074#ifdef HAVE_POSIX_ACL
3075 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3076#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003077#ifdef HAVE_SOLARIS_ZFS_ACL
3078 acl_set((char *)fname, (acl_t *)aclent);
3079#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#ifdef HAVE_SOLARIS_ACL
3081 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3082 ((vim_acl_solaris_T *)aclent)->acl_entry);
3083#else
3084#ifdef HAVE_AIX_ACL
3085 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3086#endif /* HAVE_AIX_ACL */
3087#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003088#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089#endif /* HAVE_POSIX_ACL */
3090}
3091
3092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003093mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 if (aclent == NULL)
3096 return;
3097#ifdef HAVE_POSIX_ACL
3098 acl_free((acl_t)aclent);
3099#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003100#ifdef HAVE_SOLARIS_ZFS_ACL
3101 acl_free((acl_t *)aclent);
3102#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#ifdef HAVE_SOLARIS_ACL
3104 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3105 free(aclent);
3106#else
3107#ifdef HAVE_AIX_ACL
3108 free(aclent);
3109#endif /* HAVE_AIX_ACL */
3110#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003111#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#endif /* HAVE_POSIX_ACL */
3113}
3114#endif
3115
3116/*
3117 * Set hidden flag for "name".
3118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003120mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 /* can't hide a file */
3123}
3124
3125/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003126 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * return FALSE if "name" is not a directory
3128 * return FALSE for error
3129 */
3130 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003131mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 struct stat statb;
3134
3135 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3136 return FALSE;
3137 if (stat((char *)name, &statb))
3138 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003142/*
3143 * return TRUE if "name" is a directory, NOT a symlink to a directory
3144 * return FALSE if "name" is not a directory
3145 * return FALSE for error
3146 */
3147 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003148mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003149{
3150 struct stat statb;
3151
3152 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3153 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003154 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003155 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003156 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003157}
3158
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003159static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161/*
3162 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3163 */
3164 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003165executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 struct stat st;
3168
3169 if (stat((char *)name, &st))
3170 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003171#ifdef VMS
3172 /* Like on Unix system file can have executable rights but not necessarily
3173 * be an executable, but on Unix is not a default for an ordianry file to
3174 * have an executable flag - on VMS it is in most cases.
3175 * Therefore, this check does not have any sense - let keep us to the
3176 * conventions instead:
3177 * *.COM and *.EXE files are the executables - the rest are not. This is
3178 * not ideal but better then it was.
3179 */
3180 int vms_executable = 0;
3181 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3182 {
3183 if (strstr(vms_tolower((char*)name),".exe") != NULL
3184 || strstr(vms_tolower((char*)name),".com")!= NULL)
3185 vms_executable = 1;
3186 }
3187 return vms_executable;
3188#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003194 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003195 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 * Return -1 if unknown.
3197 */
3198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003199mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 char_u *buf;
3202 char_u *p, *e;
3203 int retval;
3204
Bram Moolenaarb5971142015-03-21 17:32:19 +01003205 /* When "use_path" is false and if it's an absolute or relative path don't
3206 * need to use $PATH. */
3207 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3208 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003209 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003210 /* There must be a path separator, files in the current directory
3211 * can't be executed. */
3212 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003213 {
3214 if (path != NULL)
3215 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003216 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003217 *path = FullName_save(name, TRUE);
3218 else
3219 *path = vim_strsave(name);
3220 }
3221 return TRUE;
3222 }
3223 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 p = (char_u *)getenv("PATH");
3227 if (p == NULL || *p == NUL)
3228 return -1;
3229 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3230 if (buf == NULL)
3231 return -1;
3232
3233 /*
3234 * Walk through all entries in $PATH to check if "name" exists there and
3235 * is an executable file.
3236 */
3237 for (;;)
3238 {
3239 e = (char_u *)strchr((char *)p, ':');
3240 if (e == NULL)
3241 e = p + STRLEN(p);
3242 if (e - p <= 1) /* empty entry means current dir */
3243 STRCPY(buf, "./");
3244 else
3245 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003246 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 add_pathsep(buf);
3248 }
3249 STRCAT(buf, name);
3250 retval = executable_file(buf);
3251 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003252 {
3253 if (path != NULL)
3254 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003255 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003256 *path = FullName_save(buf, TRUE);
3257 else
3258 *path = vim_strsave(buf);
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
3263 if (*e != ':')
3264 break;
3265 p = e + 1;
3266 }
3267
3268 vim_free(buf);
3269 return retval;
3270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
3272/*
3273 * Check what "name" is:
3274 * NODE_NORMAL: file or directory (or doesn't exist)
3275 * NODE_WRITABLE: writable device, socket, fifo, etc.
3276 * NODE_OTHER: non-writable things
3277 */
3278 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003279mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 struct stat st;
3282
3283 if (stat((char *)name, &st))
3284 return NODE_NORMAL;
3285 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3286 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3288 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 /* Everything else is writable? */
3290 return NODE_WRITABLE;
3291}
3292
3293 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003294mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
3296#ifdef HAVE_CHECK_STACK_GROWTH
3297 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 check_stack_growth((char *)&i);
3300
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003301# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 get_stack_limit();
3303# endif
3304
3305#endif
3306
3307 /*
3308 * Setup an alternative stack for signals. Helps to catch signals when
3309 * running out of stack space.
3310 * Use of sigaltstack() is preferred, it's more portable.
3311 * Ignore any errors.
3312 */
3313#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003314 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 init_signal_stack();
3316#endif
3317}
3318
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003319#if defined(EXITFREE) || defined(PROTO)
3320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003321mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003322{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3324 if (clip_star.owned)
3325 clip_lose_selection(&clip_star);
3326 if (clip_plus.owned)
3327 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003328# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003329# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003330 if (xterm_Shell != (Widget)0)
3331 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003332# ifndef LESSTIF_VERSION
3333 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003334 if (xterm_dpy != NULL)
3335 XtCloseDisplay(xterm_dpy);
3336 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003337 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003338 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003339# ifdef FEAT_X11
3340 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3341# endif
3342 }
3343# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003344# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003345# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003346 if (x11_display != NULL
3347# ifdef FEAT_XCLIPBOARD
3348 && x11_display != xterm_dpy
3349# endif
3350 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003351 XCloseDisplay(x11_display);
3352# endif
3353# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003354 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003355# endif
3356# ifdef FEAT_TITLE
3357 vim_free(oldtitle);
3358 vim_free(oldicon);
3359# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003360}
3361#endif
3362
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003363static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365/*
3366 * Output a newline when exiting.
3367 * Make sure the newline goes to the same stream as the text.
3368 */
3369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003370exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003372 if (silent_mode)
3373 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (newline_on_exit || msg_didout)
3375 {
3376 if (msg_use_printf())
3377 {
3378 if (info_message)
3379 mch_msg("\n");
3380 else
3381 mch_errmsg("\r\n");
3382 }
3383 else
3384 out_char('\n');
3385 }
3386 else
3387 {
3388 restore_cterm_colors(); /* get original colors back */
3389 msg_clr_eos_force(); /* clear the rest of the display */
3390 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3391 }
3392}
3393
3394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003395mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 exiting = TRUE;
3398
3399#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3400 x11_export_final_selection();
3401#endif
3402
3403#ifdef FEAT_GUI
3404 if (!gui.in_use)
3405#endif
3406 {
3407 settmode(TMODE_COOK);
3408#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003409 // restore xterm title and icon name
3410 mch_restore_title(SAVE_RESTORE_BOTH);
3411 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
3413 /*
3414 * When t_ti is not empty but it doesn't cause swapping terminal
3415 * pages, need to output a newline when msg_didout is set. But when
3416 * t_ti does swap pages it should not go to the shell page. Do this
3417 * before stoptermcap().
3418 */
3419 if (swapping_screen() && !newline_on_exit)
3420 exit_scroll();
3421
3422 /* Stop termcap: May need to check for T_CRV response, which
3423 * requires RAW mode. */
3424 stoptermcap();
3425
3426 /*
3427 * A newline is only required after a message in the alternate screen.
3428 * This is set to TRUE by wait_return().
3429 */
3430 if (!swapping_screen() || newline_on_exit)
3431 exit_scroll();
3432
3433 /* Cursor may have been switched off without calling starttermcap()
3434 * when doing "vim -u vimrc" and vimrc contains ":q". */
3435 if (full_screen)
3436 cursor_on();
3437 }
3438 out_flush();
3439 ml_close_all(TRUE); /* remove all memfiles */
3440 may_core_dump();
3441#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 gui_exit(r);
3444#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003445
Bram Moolenaar56718732006-03-15 22:53:57 +00003446#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003447 mac_conv_cleanup();
3448#endif
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#ifdef __QNX__
3451 /* A core dump won't be created if the signal handler
3452 * doesn't return, so we can't call exit() */
3453 if (deadly_signal != 0)
3454 return;
3455#endif
3456
Bram Moolenaar009b2592004-10-24 19:18:58 +00003457#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003458 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003459#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003460
3461#ifdef EXITFREE
3462 free_all_mem();
3463#endif
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 exit(r);
3466}
3467
3468 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003469may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 if (deadly_signal != 0)
3472 {
3473 signal(deadly_signal, SIG_DFL);
3474 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3475 }
3476}
3477
3478#ifndef VMS
3479
3480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003481mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 static int first = TRUE;
3484
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003485#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486# ifdef HAVE_TERMIOS_H
3487 static struct termios told;
3488 struct termios tnew;
3489# else
3490 static struct termio told;
3491 struct termio tnew;
3492# endif
3493
3494 if (first)
3495 {
3496 first = FALSE;
3497# if defined(HAVE_TERMIOS_H)
3498 tcgetattr(read_cmd_fd, &told);
3499# else
3500 ioctl(read_cmd_fd, TCGETA, &told);
3501# endif
3502 }
3503
3504 tnew = told;
3505 if (tmode == TMODE_RAW)
3506 {
3507 /*
3508 * ~ICRNL enables typing ^V^M
3509 */
3510 tnew.c_iflag &= ~ICRNL;
3511 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3512# if defined(IEXTEN) && !defined(__MINT__)
3513 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3514 /* but it breaks function keys on MINT */
3515# endif
3516 );
3517# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3518 tnew.c_oflag &= ~ONLCR;
3519# endif
3520 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3521 tnew.c_cc[VTIME] = 0; /* don't wait */
3522 }
3523 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003524 {
3525 /* Also reset ICANON here, otherwise on Solaris select() won't see
3526 * typeahead characters. */
3527 tnew.c_lflag &= ~(ICANON | ECHO);
3528 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3529 tnew.c_cc[VTIME] = 0; /* don't wait */
3530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532# if defined(HAVE_TERMIOS_H)
3533 {
3534 int n = 10;
3535
3536 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3537 * few times. */
3538 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3539 && errno == EINTR && n > 0)
3540 --n;
3541 }
3542# else
3543 ioctl(read_cmd_fd, TCSETA, &tnew);
3544# endif
3545
3546#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /*
3548 * for "old" tty systems
3549 */
3550# ifndef TIOCSETN
3551# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3552# endif
3553 static struct sgttyb ttybold;
3554 struct sgttyb ttybnew;
3555
3556 if (first)
3557 {
3558 first = FALSE;
3559 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3560 }
3561
3562 ttybnew = ttybold;
3563 if (tmode == TMODE_RAW)
3564 {
3565 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3566 ttybnew.sg_flags |= RAW;
3567 }
3568 else if (tmode == TMODE_SLEEP)
3569 ttybnew.sg_flags &= ~(ECHO);
3570 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3571#endif
3572 curr_tmode = tmode;
3573}
3574
3575/*
3576 * Try to get the code for "t_kb" from the stty setting
3577 *
3578 * Even if termcap claims a backspace key, the user's setting *should*
3579 * prevail. stty knows more about reality than termcap does, and if
3580 * somebody's usual erase key is DEL (which, for most BSD users, it will
3581 * be), they're going to get really annoyed if their erase key starts
3582 * doing forward deletes for no reason. (Eric Fischer)
3583 */
3584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003585get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 ttyinfo_T info;
3588 char_u buf[2];
3589 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003591 if (get_tty_info(read_cmd_fd, &info) == OK)
3592 {
3593 intr_char = info.interrupt;
3594 buf[0] = info.backspace;
3595 buf[1] = NUL;
3596 add_termcode((char_u *)"kb", buf, FALSE);
3597
3598 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3599 p = find_termcode((char_u *)"kD");
3600 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3601 do_fixdel(NULL);
3602 }
3603}
3604
3605/*
3606 * Obtain the characters that Backspace and Enter produce on "fd".
3607 * Returns OK or FAIL.
3608 */
3609 int
3610get_tty_info(int fd, ttyinfo_T *info)
3611{
3612#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613# ifdef HAVE_TERMIOS_H
3614 struct termios keys;
3615# else
3616 struct termio keys;
3617# endif
3618
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003619 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003621 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003623 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003625 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003627 info->backspace = keys.c_cc[VERASE];
3628 info->interrupt = keys.c_cc[VINTR];
3629 if (keys.c_iflag & ICRNL)
3630 info->enter = NL;
3631 else
3632 info->enter = CAR;
3633 if (keys.c_oflag & ONLCR)
3634 info->nl_does_cr = TRUE;
3635 else
3636 info->nl_does_cr = FALSE;
3637 return OK;
3638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#else
3640 /* for "old" tty systems */
3641 struct sgttyb keys;
3642
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003643 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003645 info->backspace = keys.sg_erase;
3646 info->interrupt = keys.sg_kill;
3647 info->enter = CAR;
3648 info->nl_does_cr = TRUE;
3649 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003652 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653}
3654
3655#endif /* VMS */
3656
3657#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003658static int mouse_ison = FALSE;
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660/*
3661 * Set mouse clicks on or off.
3662 */
3663 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003664mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003666# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003667 static int bevalterm_ison = FALSE;
3668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 int xterm_mouse_vers;
3670
Bram Moolenaara06afc72018-08-27 23:24:16 +02003671# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3672 if (!on)
3673 // Make sure not tracing mouse movements. Important when a button-down
3674 // was received but no release yet.
3675 stop_xterm_trace();
3676# endif
3677
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003678 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003679# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003680 && p_bevalterm == bevalterm_ison
3681# endif
3682 )
3683 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 return;
3685
3686 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003687
3688# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003689 if (ttym_flags == TTYM_URXVT)
3690 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003691 out_str_nf((char_u *)
3692 (on
3693 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3694 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003695 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003696 }
3697# endif
3698
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003699# ifdef FEAT_MOUSE_SGR
3700 if (ttym_flags == TTYM_SGR)
3701 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003703 out_str_nf((char_u *)
3704 (on
3705 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3706 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 mouse_ison = on;
3708 }
3709# endif
3710
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003711# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003712 if (bevalterm_ison != (p_bevalterm && on))
3713 {
3714 bevalterm_ison = (p_bevalterm && on);
3715 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3716 /* disable mouse movement events, enabling is below */
3717 out_str_nf((char_u *)
3718 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003719 }
3720# endif
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (xterm_mouse_vers > 0)
3723 {
3724 if (on) /* enable mouse events, use mouse tracking if available */
3725 out_str_nf((char_u *)
3726 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003727 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003728# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003729 bevalterm_ison
3730 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3731# endif
3732 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3734 else /* disable mouse events, could probably always send the same */
3735 out_str_nf((char_u *)
3736 (xterm_mouse_vers > 1
3737 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3738 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003739 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741
3742# ifdef FEAT_MOUSE_DEC
3743 else if (ttym_flags == TTYM_DEC)
3744 {
3745 if (on) /* enable mouse events */
3746 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3747 else /* disable mouse events */
3748 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003749 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751# endif
3752
3753# ifdef FEAT_MOUSE_GPM
3754 else
3755 {
3756 if (on)
3757 {
3758 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003759 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 else
3762 {
3763 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003764 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 }
3767# endif
3768
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003769# ifdef FEAT_SYSMOUSE
3770 else
3771 {
3772 if (on)
3773 {
3774 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003775 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003776 }
3777 else
3778 {
3779 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003781 }
3782 }
3783# endif
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785# ifdef FEAT_MOUSE_JSB
3786 else
3787 {
3788 if (on)
3789 {
3790 /* D - Enable Mouse up/down messages
3791 * L - Enable Left Button Reporting
3792 * M - Enable Middle Button Reporting
3793 * R - Enable Right Button Reporting
3794 * K - Enable SHIFT and CTRL key Reporting
3795 * + - Enable Advanced messaging of mouse moves and up/down messages
3796 * Q - Quiet No Ack
3797 * # - Numeric value of mouse pointer required
3798 * 0 = Multiview 2000 cursor, used as standard
3799 * 1 = Windows Arrow
3800 * 2 = Windows I Beam
3801 * 3 = Windows Hour Glass
3802 * 4 = Windows Cross Hair
3803 * 5 = Windows UP Arrow
3804 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003805# ifdef JSBTERM_MOUSE_NONADVANCED
3806 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3808 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003809# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3811 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003812# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003813 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815 else
3816 {
3817 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3818 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003819 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 }
3822# endif
3823# ifdef FEAT_MOUSE_PTERM
3824 else
3825 {
3826 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3827 if (on)
3828 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3829 else
3830 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003831 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833# endif
3834}
3835
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003836#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003837/*
3838 * Called when 'balloonevalterm' changed.
3839 */
3840 void
3841mch_bevalterm_changed(void)
3842{
3843 mch_setmouse(mouse_ison);
3844}
3845#endif
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847/*
3848 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3849 */
3850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003851check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853# ifdef FEAT_MOUSE_XTERM
3854 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003855# ifdef FEAT_MOUSE_URXVT
3856 && use_xterm_mouse() != 3
3857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858# ifdef FEAT_GUI
3859 && !gui.in_use
3860# endif
3861 )
3862 {
3863 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003864 ? IF_EB("\233M", CSI_STR "M")
3865 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (*p_mouse != NUL)
3867 {
3868 /* force mouse off and maybe on to send possibly new mouse
3869 * activation sequence to the xterm, with(out) drag tracing. */
3870 mch_setmouse(FALSE);
3871 setmouse();
3872 }
3873 }
3874 else
3875 del_mouse_termcode(KS_MOUSE);
3876# endif
3877
3878# ifdef FEAT_MOUSE_GPM
3879 if (!use_xterm_mouse()
3880# ifdef FEAT_GUI
3881 && !gui.in_use
3882# endif
3883 )
3884 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3885# endif
3886
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003887# ifdef FEAT_SYSMOUSE
3888 if (!use_xterm_mouse()
3889# ifdef FEAT_GUI
3890 && !gui.in_use
3891# endif
3892 )
3893 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3894# endif
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003897 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (!use_xterm_mouse()
3899# ifdef FEAT_GUI
3900 && !gui.in_use
3901# endif
3902 )
3903 set_mouse_termcode(KS_JSBTERM_MOUSE,
3904 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3905 else
3906 del_mouse_termcode(KS_JSBTERM_MOUSE);
3907# endif
3908
3909# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003910 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 * define it in the GUI or when using an xterm. */
3912 if (!use_xterm_mouse()
3913# ifdef FEAT_GUI
3914 && !gui.in_use
3915# endif
3916 )
3917 set_mouse_termcode(KS_NETTERM_MOUSE,
3918 (char_u *)IF_EB("\033}", ESC_STR "}"));
3919 else
3920 del_mouse_termcode(KS_NETTERM_MOUSE);
3921# endif
3922
3923# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003924 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003925 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# ifdef FEAT_GUI
3927 && !gui.in_use
3928# endif
3929 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003930 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3931 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 else
3933 del_mouse_termcode(KS_DEC_MOUSE);
3934# endif
3935# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003936 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003937 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938# ifdef FEAT_GUI
3939 && !gui.in_use
3940# endif
3941 )
3942 set_mouse_termcode(KS_PTERM_MOUSE,
3943 (char_u *) IF_EB("\033[", ESC_STR "["));
3944 else
3945 del_mouse_termcode(KS_PTERM_MOUSE);
3946# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003947# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003948 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003949# ifdef FEAT_GUI
3950 && !gui.in_use
3951# endif
3952 )
3953 {
3954 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003955 ? IF_EB("\233*M", CSI_STR "*M")
3956 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003957
3958 if (*p_mouse != NUL)
3959 {
3960 mch_setmouse(FALSE);
3961 setmouse();
3962 }
3963 }
3964 else
3965 del_mouse_termcode(KS_URXVT_MOUSE);
3966# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003967# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003968 if (use_xterm_mouse() == 4
3969# ifdef FEAT_GUI
3970 && !gui.in_use
3971# endif
3972 )
3973 {
3974 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003975 ? IF_EB("\233<*M", CSI_STR "<*M")
3976 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3977
3978 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3979 ? IF_EB("\233<*m", CSI_STR "<*m")
3980 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003981
3982 if (*p_mouse != NUL)
3983 {
3984 mch_setmouse(FALSE);
3985 setmouse();
3986 }
3987 }
3988 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003989 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003990 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003991 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3992 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994}
3995#endif
3996
3997/*
3998 * set screen mode, always fails.
3999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004001mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 EMSG(_(e_screenmode));
4004 return FAIL;
4005}
4006
4007#ifndef VMS
4008
4009/*
4010 * Try to get the current window size:
4011 * 1. with an ioctl(), most accurate method
4012 * 2. from the environment variables LINES and COLUMNS
4013 * 3. from the termcap
4014 * 4. keep using the old values
4015 * Return OK when size could be determined, FAIL otherwise.
4016 */
4017 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004018mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 long rows = 0;
4021 long columns = 0;
4022 char_u *p;
4023
4024 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 * 1. try using an ioctl. It is the most accurate method.
4026 *
4027 * Try using TIOCGWINSZ first, some systems that have it also define
4028 * TIOCGSIZE but don't have a struct ttysize.
4029 */
4030# ifdef TIOCGWINSZ
4031 {
4032 struct winsize ws;
4033 int fd = 1;
4034
4035 /* When stdout is not a tty, use stdin for the ioctl(). */
4036 if (!isatty(fd) && isatty(read_cmd_fd))
4037 fd = read_cmd_fd;
4038 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4039 {
4040 columns = ws.ws_col;
4041 rows = ws.ws_row;
4042 }
4043 }
4044# else /* TIOCGWINSZ */
4045# ifdef TIOCGSIZE
4046 {
4047 struct ttysize ts;
4048 int fd = 1;
4049
4050 /* When stdout is not a tty, use stdin for the ioctl(). */
4051 if (!isatty(fd) && isatty(read_cmd_fd))
4052 fd = read_cmd_fd;
4053 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4054 {
4055 columns = ts.ts_cols;
4056 rows = ts.ts_lines;
4057 }
4058 }
4059# endif /* TIOCGSIZE */
4060# endif /* TIOCGWINSZ */
4061
4062 /*
4063 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004064 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4065 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004067 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 if ((p = (char_u *)getenv("LINES")))
4070 rows = atoi((char *)p);
4071 if ((p = (char_u *)getenv("COLUMNS")))
4072 columns = atoi((char *)p);
4073 }
4074
4075#ifdef HAVE_TGETENT
4076 /*
4077 * 3. try reading "co" and "li" entries from termcap
4078 */
4079 if (columns == 0 || rows == 0)
4080 getlinecol(&columns, &rows);
4081#endif
4082
4083 /*
4084 * 4. If everything fails, use the old values
4085 */
4086 if (columns <= 0 || rows <= 0)
4087 return FAIL;
4088
4089 Rows = rows;
4090 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004091 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return OK;
4093}
4094
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004095#if defined(FEAT_TERMINAL) || defined(PROTO)
4096/*
4097 * Report the windows size "rows" and "cols" to tty "fd".
4098 */
4099 int
4100mch_report_winsize(int fd, int rows, int cols)
4101{
4102# ifdef TIOCSWINSZ
4103 struct winsize ws;
4104
4105 ws.ws_col = cols;
4106 ws.ws_row = rows;
4107 ws.ws_xpixel = cols * 5;
4108 ws.ws_ypixel = rows * 10;
4109 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4110 {
4111 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4112 return OK;
4113 }
4114 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4115# else
4116# ifdef TIOCSSIZE
4117 struct ttysize ts;
4118
4119 ts.ts_cols = cols;
4120 ts.ts_lines = rows;
4121 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4122 {
4123 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4124 return OK;
4125 }
4126 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4127# endif
4128# endif
4129 return FAIL;
4130}
4131#endif
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133/*
4134 * Try to set the window size to Rows and Columns.
4135 */
4136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004137mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 if (*T_CWS)
4140 {
4141 /*
4142 * NOTE: if you get an error here that term_set_winsize() is
4143 * undefined, check the output of configure. It could probably not
4144 * find a ncurses, termcap or termlib library.
4145 */
4146 term_set_winsize((int)Rows, (int)Columns);
4147 out_flush();
4148 screen_start(); /* don't know where cursor is now */
4149 }
4150}
4151
4152#endif /* VMS */
4153
4154/*
4155 * Rows and/or Columns has changed.
4156 */
4157 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004158mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 /* Nothing to do. */
4161}
4162
Bram Moolenaar205b8862011-09-07 15:04:31 +02004163/*
4164 * Wait for process "child" to end.
4165 * Return "child" if it exited properly, <= 0 on error.
4166 */
4167 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004168wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004169{
4170 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004171 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004172
4173 while (wait_pid != child)
4174 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004175 /* When compiled with Python threads are probably used, in which case
4176 * wait() sometimes hangs for no obvious reason. Use waitpid()
4177 * instead and loop (like the GUI). Also needed for other interfaces,
4178 * they might call system(). */
4179# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004180 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004181# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004182 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004183# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004184 if (wait_pid == 0)
4185 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004186 /* Wait for 1 to 10 msec before trying again. */
4187 mch_delay(delay_msec, TRUE);
4188 if (++delay_msec > 10)
4189 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004190 continue;
4191 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004192 if (wait_pid <= 0
4193# ifdef ECHILD
4194 && errno == ECHILD
4195# endif
4196 )
4197 break;
4198 }
4199 return wait_pid;
4200}
4201
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004202#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004203/*
4204 * Set the environment for a child process.
4205 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004206 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004207set_child_environment(
4208 long rows,
4209 long columns,
4210 char *term,
4211 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004212{
4213# ifdef HAVE_SETENV
4214 char envbuf[50];
4215# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004216 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004217 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004218 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004219 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004220 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004221# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004222 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004223# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004224# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004225 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004226# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004227# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004228 long colors =
4229# ifdef FEAT_GUI
4230 gui.in_use ? 256*256*256 :
4231# endif
4232 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004234# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004235 setenv("TERM", term, 1);
4236 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004237 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004238 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004239 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004240 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004241 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004242 sprintf((char *)envbuf, "%ld", colors);
4243 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004244# ifdef FEAT_TERMINAL
4245 if (is_terminal)
4246 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004247 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004248 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4249 }
4250# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004251# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004252 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004253# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004254# else
4255 /*
4256 * Putenv does not copy the string, it has to remain valid.
4257 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004258 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004259 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004260 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4261 putenv(envbuf_Term);
4262 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004263 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004264 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4265 putenv(envbuf_Lines);
4266 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4267 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004268 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004269 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4270 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004271# ifdef FEAT_TERMINAL
4272 if (is_terminal)
4273 {
4274 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004275 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004276 putenv(envbuf_Version);
4277 }
4278# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004279# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004280 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4281 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4282 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004283# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004284# endif
4285}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004286
4287 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004288set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004289{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004290 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004291}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004292#endif
4293
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004294#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004295/*
4296 * Open a PTY, with FD for the master and slave side.
4297 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4298 * When successful both file descriptors are stored.
4299 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004300 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004301open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004302{
4303 char *tty_name;
4304
4305 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4306 if (*pty_master_fd >= 0)
4307 {
4308 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4309 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4310 * adding O_NOCTTY always works when defined. */
4311#ifdef O_NOCTTY
4312 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4313#else
4314 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4315#endif
4316 if (*pty_slave_fd < 0)
4317 {
4318 close(*pty_master_fd);
4319 *pty_master_fd = -1;
4320 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004321 else if (namep != NULL)
4322 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004323 }
4324}
4325#endif
4326
Bram Moolenaarfae42832017-08-01 22:24:26 +02004327/*
4328 * Send SIGINT to a child process if "c" is an interrupt character.
4329 */
4330 void
4331may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4332{
4333# ifdef SIGINT
4334 if (c == Ctrl_C || c == intr_char)
4335 {
4336# ifdef HAVE_SETSID
4337 kill(-pid, SIGINT);
4338# else
4339 kill(0, SIGINT);
4340# endif
4341 if (wpid > 0)
4342 kill(wpid, SIGINT);
4343 }
4344# endif
4345}
4346
Bram Moolenaar13568252018-03-16 20:46:58 +01004347#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4348
4349 static int
4350build_argv(
4351 char_u *cmd,
4352 char ***argvp,
4353 char_u **sh_tofree,
4354 char_u **shcf_tofree)
4355{
4356 char **argv = NULL;
4357 int argc;
4358
4359 *sh_tofree = vim_strsave(p_sh);
4360 if (*sh_tofree == NULL) /* out of memory */
4361 return FAIL;
4362
4363 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4364 return FAIL;
4365 *argvp = argv;
4366
4367 if (cmd != NULL)
4368 {
4369 char_u *s;
4370 char_u *p;
4371
4372 if (extra_shell_arg != NULL)
4373 argv[argc++] = (char *)extra_shell_arg;
4374
4375 /* Break 'shellcmdflag' into white separated parts. This doesn't
4376 * handle quoted strings, they are very unlikely to appear. */
4377 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4378 if (*shcf_tofree == NULL) /* out of memory */
4379 return FAIL;
4380 s = *shcf_tofree;
4381 p = p_shcf;
4382 while (*p != NUL)
4383 {
4384 argv[argc++] = (char *)s;
4385 while (*p && *p != ' ' && *p != TAB)
4386 *s++ = *p++;
4387 *s++ = NUL;
4388 p = skipwhite(p);
4389 }
4390
4391 argv[argc++] = (char *)cmd;
4392 }
4393 argv[argc] = NULL;
4394 return OK;
4395}
4396#endif
4397
4398#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4399/*
4400 * Use a terminal window to run a shell command in.
4401 */
4402 static int
4403mch_call_shell_terminal(
4404 char_u *cmd,
4405 int options UNUSED) /* SHELL_*, see vim.h */
4406{
4407 jobopt_T opt;
4408 char **argv = NULL;
4409 char_u *tofree1 = NULL;
4410 char_u *tofree2 = NULL;
4411 int retval = -1;
4412 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004413 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004414 aco_save_T aco;
4415 oparg_T oa; /* operator arguments */
4416
4417 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4418 goto theend;
4419
4420 init_job_options(&opt);
4421 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4422 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004423 if (buf == NULL)
4424 goto theend;
4425
4426 job = term_getjob(buf->b_term);
4427 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004428
4429 /* Find a window to make "buf" curbuf. */
4430 aucmd_prepbuf(&aco, buf);
4431
4432 clear_oparg(&oa);
4433 while (term_use_loop())
4434 {
4435 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4436 {
4437 /* If terminal_loop() returns OK we got a key that is handled
4438 * in Normal model. We don't do redrawing anyway. */
4439 if (terminal_loop(TRUE) == OK)
4440 normal_cmd(&oa, TRUE);
4441 }
4442 else
4443 normal_cmd(&oa, TRUE);
4444 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004445 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004446 ch_log(NULL, "system command finished");
4447
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004448 job_unref(job);
4449
Bram Moolenaar13568252018-03-16 20:46:58 +01004450 /* restore curwin/curbuf and a few other things */
4451 aucmd_restbuf(&aco);
4452
4453 wait_return(TRUE);
4454 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4455
4456theend:
4457 vim_free(argv);
4458 vim_free(tofree1);
4459 vim_free(tofree2);
4460 return retval;
4461}
4462#endif
4463
4464#ifdef USE_SYSTEM
4465/*
4466 * Use system() to start the shell: simple but slow.
4467 */
4468 static int
4469mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004470 char_u *cmd,
4471 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473#ifdef VMS
4474 char *ifn = NULL;
4475 char *ofn = NULL;
4476#endif
4477 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004478 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004479 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 out_flush();
4482
4483 if (options & SHELL_COOKED)
4484 settmode(TMODE_COOK); /* set to normal mode */
4485
Bram Moolenaar62b42182010-09-21 22:09:37 +02004486# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004487 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004488 loose_clipboard();
4489# endif
4490
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (cmd == NULL)
4492 x = system((char *)p_sh);
4493 else
4494 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004495# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (ofn = strchr((char *)cmd, '>'))
4497 *ofn++ = '\0';
4498 if (ifn = strchr((char *)cmd, '<'))
4499 {
4500 char *p;
4501
4502 *ifn++ = '\0';
4503 p = strchr(ifn,' '); /* chop off any trailing spaces */
4504 if (p)
4505 *p = '\0';
4506 }
4507 if (ofn)
4508 x = vms_sys((char *)cmd, ofn, ifn);
4509 else
4510 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004511# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 newcmd = lalloc(STRLEN(p_sh)
4513 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4514 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4515 if (newcmd == NULL)
4516 x = 0;
4517 else
4518 {
4519 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4520 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4521 (char *)p_shcf,
4522 (char *)cmd);
4523 x = system((char *)newcmd);
4524 vim_free(newcmd);
4525 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528# ifdef VMS
4529 x = vms_sys_status(x);
4530# endif
4531 if (emsg_silent)
4532 ;
4533 else if (x == 127)
4534 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 else if (x && !(options & SHELL_SILENT))
4536 {
4537 MSG_PUTS(_("\nshell returned "));
4538 msg_outnum((long)x);
4539 msg_putchar('\n');
4540 }
4541
4542 if (tmode == TMODE_RAW)
4543 settmode(TMODE_RAW); /* set to raw mode */
4544# ifdef FEAT_TITLE
4545 resettitle();
4546# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004547# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4548 restore_clipboard();
4549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar13568252018-03-16 20:46:58 +01004553#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaardf177f62005-02-22 08:39:57 +00004555# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4556 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004557# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar13568252018-03-16 20:46:58 +01004559/*
4560 * Don't use system(), use fork()/exec().
4561 */
4562 static int
4563mch_call_shell_fork(
4564 char_u *cmd,
4565 int options) /* SHELL_*, see vim.h */
4566{
4567 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 pid_t wait_pid = 0;
4571# ifdef HAVE_UNION_WAIT
4572 union wait status;
4573# else
4574 int status = -1;
4575# endif
4576 int retval = -1;
4577 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004578 char_u *tofree1 = NULL;
4579 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004582# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004584# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004585 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 int fd_fromshell[2];
4587 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004588 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
4590 out_flush();
4591 if (options & SHELL_COOKED)
4592 settmode(TMODE_COOK); /* set to normal mode */
4593
Bram Moolenaar13568252018-03-16 20:46:58 +01004594 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004595 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 * For the GUI, when writing the output into the buffer and when reading
4599 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4600 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004602 if ((options & (SHELL_READ|SHELL_WRITE))
4603# ifdef FEAT_GUI
4604 || (gui.in_use && show_shell_mess)
4605# endif
4606 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 /*
4610 * Try to open a master pty.
4611 * If this works, open the slave pty.
4612 * If the slave can't be opened, close the master pty.
4613 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004615 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 /*
4617 * If not opening a pty or it didn't work, try using pipes.
4618 */
4619 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 pipe_error = (pipe(fd_toshell) < 0);
4623 if (!pipe_error) /* pipe create OK */
4624 {
4625 pipe_error = (pipe(fd_fromshell) < 0);
4626 if (pipe_error) /* pipe create failed */
4627 {
4628 close(fd_toshell[0]);
4629 close(fd_toshell[1]);
4630 }
4631 }
4632 if (pipe_error)
4633 {
4634 MSG_PUTS(_("\nCannot create pipes\n"));
4635 out_flush();
4636 }
4637 }
4638 }
4639
4640 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004642 SIGSET_DECL(curset)
4643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644# ifdef __BEOS__
4645 beos_cleanup_read_thread();
4646# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004647
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004648 BLOCK_SIGNALS(&curset);
4649 pid = fork(); /* maybe we should use vfork() */
4650 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004652 UNBLOCK_SIGNALS(&curset);
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657 || (gui.in_use && show_shell_mess)
4658# endif
4659 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004661# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (pty_master_fd >= 0) /* close the pseudo tty */
4663 {
4664 close(pty_master_fd);
4665 close(pty_slave_fd);
4666 }
4667 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 close(fd_toshell[0]);
4671 close(fd_toshell[1]);
4672 close(fd_fromshell[0]);
4673 close(fd_fromshell[1]);
4674 }
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
4677 else if (pid == 0) /* child */
4678 {
4679 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004680 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaar819524702018-02-27 19:10:00 +01004682# ifdef FEAT_JOB_CHANNEL
4683 if (ch_log_active())
4684 /* close the log file in the child */
4685 ch_logfile((char_u *)"", (char_u *)"");
4686# endif
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (!show_shell_mess || (options & SHELL_EXPAND))
4689 {
4690 int fd;
4691
4692 /*
4693 * Don't want to show any message from the shell. Can't just
4694 * close stdout and stderr though, because some systems will
4695 * break if you try to write to them after that, so we must
4696 * use dup() to replace them with something else -- webb
4697 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4698 * waiting for input.
4699 */
4700 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4701 fclose(stdin);
4702 fclose(stdout);
4703 fclose(stderr);
4704
4705 /*
4706 * If any of these open()'s and dup()'s fail, we just continue
4707 * anyway. It's not fatal, and on most systems it will make
4708 * no difference at all. On a few it will cause the execvp()
4709 * to exit with a non-zero status even when the completion
4710 * could be done, which is nothing too serious. If the open()
4711 * or dup() failed we'd just do the same thing ourselves
4712 * anyway -- webb
4713 */
4714 if (fd >= 0)
4715 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004716 ignored = dup(fd); /* To replace stdin (fd 0) */
4717 ignored = dup(fd); /* To replace stdout (fd 1) */
4718 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 /* Don't need this now that we've duplicated it */
4721 close(fd);
4722 }
4723 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004724 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004726 || gui.in_use
4727# endif
4728 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
4730
Bram Moolenaardf177f62005-02-22 08:39:57 +00004731# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004732 /* Create our own process group, so that the child and all its
4733 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004734 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004735 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004736 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004737 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004738# if defined(SIGHUP)
4739 /* When doing "!xterm&" and 'shell' is bash: the shell
4740 * will exit and send SIGHUP to all processes in its
4741 * group, killing the just started process. Ignore SIGHUP
4742 * to avoid that. (suggested by Simon Schubert)
4743 */
4744 signal(SIGHUP, SIG_IGN);
4745# endif
4746 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004747# endif
4748# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004749 if (pty_slave_fd >= 0)
4750 {
4751 /* push stream discipline modules */
4752 if (options & SHELL_COOKED)
4753 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004755 /* Try to become controlling tty (probably doesn't work,
4756 * unless run by root) */
4757 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004759 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004760# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004761 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaara5792f52005-11-23 21:25:05 +00004763 /*
4764 * stderr is only redirected when using the GUI, so that a
4765 * program like gpg can still access the terminal to get a
4766 * passphrase using stderr.
4767 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004768# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (pty_master_fd >= 0)
4770 {
4771 close(pty_master_fd); /* close master side of pty */
4772
4773 /* set up stdin/stdout/stderr for the child */
4774 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004775 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004777 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004778 if (gui.in_use)
4779 {
4780 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004781 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 close(pty_slave_fd); /* has been dupped, close it now */
4785 }
4786 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 /* set up stdin for the child */
4790 close(fd_toshell[1]);
4791 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004792 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 close(fd_toshell[0]);
4794
4795 /* set up stdout for the child */
4796 close(fd_fromshell[0]);
4797 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004798 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 close(fd_fromshell[1]);
4800
Bram Moolenaara5792f52005-11-23 21:25:05 +00004801# ifdef FEAT_GUI
4802 if (gui.in_use)
4803 {
4804 /* set up stderr for the child */
4805 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004806 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004807 }
4808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
4813 * There is no type cast for the argv, because the type may be
4814 * different on different machines. This may cause a warning
4815 * message with strict compilers, don't worry about it.
4816 * Call _exit() instead of exit() to avoid closing the connection
4817 * to the X server (esp. with GTK, which uses atexit()).
4818 */
4819 execvp(argv[0], argv);
4820 _exit(EXEC_FAILED); /* exec failed, return failure code */
4821 }
4822 else /* parent */
4823 {
4824 /*
4825 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 */
4828 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004829 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004830 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004831# ifdef FEAT_JOB_CHANNEL
4832 ++dont_check_job_ended;
4833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 /*
4835 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836 * This is also used to pipe stdin/stdout to/from the external
4837 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004839 if ((options & (SHELL_READ|SHELL_WRITE))
4840# ifdef FEAT_GUI
4841 || (gui.in_use && show_shell_mess)
4842# endif
4843 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004847# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4851 int ta_len = 0; /* valid bytes in ta_buf[] */
4852 int len;
4853 int p_more_save;
4854 int old_State;
4855 int c;
4856 int toshell_fd;
4857 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004858 garray_T ga;
4859 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004860# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4861 struct timeval start_tv;
4862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaardf177f62005-02-22 08:39:57 +00004864# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (pty_master_fd >= 0)
4866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 fromshell_fd = pty_master_fd;
4868 toshell_fd = dup(pty_master_fd);
4869 }
4870 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 close(fd_toshell[0]);
4874 close(fd_fromshell[1]);
4875 toshell_fd = fd_toshell[1];
4876 fromshell_fd = fd_fromshell[0];
4877 }
4878
4879 /*
4880 * Write to the child if there are typed characters.
4881 * Read from the child if there are characters available.
4882 * Repeat the reading a few times if more characters are
4883 * available. Need to check for typed keys now and then, but
4884 * not too often (delays when no chars are available).
4885 * This loop is quit if no characters can be read from the pty
4886 * (WaitForChar detected special condition), or there are no
4887 * characters available and the child has exited.
4888 * Only check if the child has exited when there is no more
4889 * output. The child may exit before all the output has
4890 * been printed.
4891 *
4892 * Currently this busy loops!
4893 * This can probably dead-lock when the write blocks!
4894 */
4895 p_more_save = p_more;
4896 p_more = FALSE;
4897 old_State = State;
4898 State = EXTERNCMD; /* don't redraw at window resize */
4899
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004900 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 {
4902 /* Fork a process that will write the lines to the
4903 * external program. */
4904 if ((wpid = fork()) == -1)
4905 {
4906 MSG_PUTS(_("\nCannot fork\n"));
4907 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004908 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 {
4910 linenr_T lnum = curbuf->b_op_start.lnum;
4911 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004912 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 size_t l;
4914
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004915 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004916 for (;;)
4917 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004918 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004919 if (l == 0)
4920 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004921 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922 /* NL -> NUL translation */
4923 len = write(toshell_fd, "", (size_t)1);
4924 else
4925 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004926 char_u *s = vim_strchr(lp + written, NL);
4927
Bram Moolenaar89d40322006-08-29 15:30:07 +00004928 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004929 s == NULL ? l
4930 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004931 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004932 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004933 {
4934 /* Finished a line, add a NL, unless this line
4935 * should not have one. */
4936 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004937 || (!curbuf->b_p_bin
4938 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004939 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 && (lnum !=
4941 curbuf->b_ml.ml_line_count
4942 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004943 ignored = write(toshell_fd, "\n",
4944 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004945 ++lnum;
4946 if (lnum > curbuf->b_op_end.lnum)
4947 {
4948 /* finished all the lines, close pipe */
4949 close(toshell_fd);
4950 toshell_fd = -1;
4951 break;
4952 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004953 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004954 written = 0;
4955 }
4956 else if (len > 0)
4957 written += len;
4958 }
4959 _exit(0);
4960 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004961 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004962 {
4963 close(toshell_fd);
4964 toshell_fd = -1;
4965 }
4966 }
4967
4968 if (options & SHELL_READ)
4969 ga_init2(&ga, 1, BUFLEN);
4970
4971 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004972# ifdef ELAPSED_FUNC
4973 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 for (;;)
4976 {
4977 /*
4978 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004979 * if there are any.
4980 * Don't do this if we are expanding wild cards (would eat
4981 * typeahead).
4982 * Don't do this when filtering and terminal is in cooked
4983 * mode, the shell command will handle the I/O. Avoids
4984 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004985 * Don't get characters when the child has already
4986 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004987 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004988 * while (noread_cnt > 4), avoids that ":r !ls" eats
4989 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
4991 len = 0;
4992 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004993 && ((options &
4994 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4995 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004996# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004997 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004998# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004999 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005000 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005001 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005003 if (ta_len == 0)
5004 {
5005 /* Get extra characters when we don't have any.
5006 * Reset the counter and timer. */
5007 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005008# ifdef ELAPSED_FUNC
5009 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005010# endif
5011 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5012 }
5013 if (ta_len > 0 || len > 0)
5014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 /*
5016 * For pipes:
5017 * Check for CTRL-C: send interrupt signal to child.
5018 * Check for CTRL-D: EOF, close pipe to child.
5019 */
5020 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 /*
5023 * Send SIGINT to the child's group or all
5024 * processes in our group.
5025 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005026 may_send_sigint(ta_buf[ta_len], pid, wpid);
5027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (pty_master_fd < 0 && toshell_fd >= 0
5029 && ta_buf[ta_len] == Ctrl_D)
5030 {
5031 close(toshell_fd);
5032 toshell_fd = -1;
5033 }
5034 }
5035
5036 /* replace K_BS by <BS> and K_DEL by <DEL> */
5037 for (i = ta_len; i < ta_len + len; ++i)
5038 {
5039 if (ta_buf[i] == CSI && len - i > 2)
5040 {
5041 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5042 if (c == K_DEL || c == K_KDEL || c == K_BS)
5043 {
5044 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5045 (size_t)(len - i - 2));
5046 if (c == K_DEL || c == K_KDEL)
5047 ta_buf[i] = DEL;
5048 else
5049 ta_buf[i] = Ctrl_H;
5050 len -= 2;
5051 }
5052 }
5053 else if (ta_buf[i] == '\r')
5054 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005055# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005057 i += (*mb_ptr2len_len)(ta_buf + i,
5058 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061
5062 /*
5063 * For pipes: echo the typed characters.
5064 * For a pty this does not seem to work.
5065 */
5066 if (pty_master_fd < 0)
5067 {
5068 for (i = ta_len; i < ta_len + len; ++i)
5069 {
5070 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5071 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005072# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 else if (has_mbyte)
5074 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005075 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 msg_outtrans_len(ta_buf + i, l);
5078 i += l - 1;
5079 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 else
5082 msg_outtrans_len(ta_buf + i, 1);
5083 }
5084 windgoto(msg_row, msg_col);
5085 out_flush();
5086 }
5087
5088 ta_len += len;
5089
5090 /*
5091 * Write the characters to the child, unless EOF has
5092 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005093 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005094 * When writing buffer lines, drop the typed
5095 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005097 if (options & SHELL_WRITE)
5098 ta_len = 0;
5099 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5102 if (len > 0)
5103 {
5104 ta_len -= len;
5105 mch_memmove(ta_buf, ta_buf + len, ta_len);
5106 }
5107 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110
Bram Moolenaardf177f62005-02-22 08:39:57 +00005111 if (got_int)
5112 {
5113 /* CTRL-C sends a signal to the child, we ignore it
5114 * ourselves */
5115# ifdef HAVE_SETSID
5116 kill(-pid, SIGINT);
5117# else
5118 kill(0, SIGINT);
5119# endif
5120 if (wpid > 0)
5121 kill(wpid, SIGINT);
5122 got_int = FALSE;
5123 }
5124
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 /*
5126 * Check if the child has any characters to be printed.
5127 * Read them and write them to our window. Repeat this as
5128 * long as there is something to do, avoid the 10ms wait
5129 * for mch_inchar(), or sending typeahead characters to
5130 * the external process.
5131 * TODO: This should handle escape sequences, compatible
5132 * to some terminal (vt52?).
5133 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005134 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005135 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005137 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005138# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005140# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 );
5144 if (len <= 0) /* end of file or error */
5145 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005146
5147 noread_cnt = 0;
5148 if (options & SHELL_READ)
5149 {
5150 /* Do NUL -> NL translation, append NL separated
5151 * lines to the current buffer. */
5152 for (i = 0; i < len; ++i)
5153 {
5154 if (buffer[i] == NL)
5155 append_ga_line(&ga);
5156 else if (buffer[i] == NUL)
5157 ga_append(&ga, NL);
5158 else
5159 ga_append(&ga, buffer[i]);
5160 }
5161 }
5162# ifdef FEAT_MBYTE
5163 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005166 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
Bram Moolenaardf177f62005-02-22 08:39:57 +00005168 len += buffer_off;
5169 buffer[len] = NUL;
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 /* Check if the last character in buffer[] is
5172 * incomplete, keep these bytes for the next
5173 * round. */
5174 for (p = buffer; p < buffer + len; p += l)
5175 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005176 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (l == 0)
5178 l = 1; /* NUL byte? */
5179 else if (MB_BYTE2LEN(*p) != l)
5180 break;
5181 }
5182 if (p == buffer) /* no complete character */
5183 {
5184 /* avoid getting stuck at an illegal byte */
5185 if (len >= 12)
5186 ++p;
5187 else
5188 {
5189 buffer_off = len;
5190 continue;
5191 }
5192 }
5193 c = *p;
5194 *p = NUL;
5195 msg_puts(buffer);
5196 if (p < buffer + len)
5197 {
5198 *p = c;
5199 buffer_off = (buffer + len) - p;
5200 mch_memmove(buffer, p, buffer_off);
5201 continue;
5202 }
5203 buffer_off = 0;
5204 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005205# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 buffer[len] = NUL;
5209 msg_puts(buffer);
5210 }
5211
5212 windgoto(msg_row, msg_col);
5213 cursor_on();
5214 out_flush();
5215 if (got_int)
5216 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005217
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005218# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005219 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005220 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005221 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005222
5223 /* Avoid that we keep looping here without
5224 * checking for a CTRL-C for a long time. Don't
5225 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005226 if (msec > 2000)
5227 {
5228 noread_cnt = 5;
5229 break;
5230 }
5231 }
5232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005235 /* If we already detected the child has finished, continue
5236 * reading output for a short while. Some text may be
5237 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005238 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005239 {
5240 if (noread_cnt < 5)
5241 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005242 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005243 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /*
5246 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005247 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005249# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005250 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005251# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5255 || (wait_pid == pid && WIFEXITED(status)))
5256 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005257 /* Don't break the loop yet, try reading more
5258 * characters from "fromshell_fd" first. When using
5259 * pipes there might still be something to read and
5260 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005263 else
5264 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005265
Bram Moolenaar95a51352013-03-21 22:53:50 +01005266# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005267 /* Handle any X events, e.g. serving the clipboard. */
5268 clip_update();
5269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271finished:
5272 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005273 if (options & SHELL_READ)
5274 {
5275 if (ga.ga_len > 0)
5276 {
5277 append_ga_line(&ga);
5278 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005279 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005280 }
5281 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005282 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005283 ga_clear(&ga);
5284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 /*
5287 * Give all typeahead that wasn't used back to ui_inchar().
5288 */
5289 if (ta_len)
5290 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 State = old_State;
5292 if (toshell_fd >= 0)
5293 close(toshell_fd);
5294 close(fromshell_fd);
5295 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005296# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005297 else
5298 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005299 long delay_msec = 1;
5300
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005301 /*
5302 * Similar to the loop above, but only handle X events, no
5303 * I/O.
5304 */
5305 for (;;)
5306 {
5307 if (got_int)
5308 {
5309 /* CTRL-C sends a signal to the child, we ignore it
5310 * ourselves */
5311# ifdef HAVE_SETSID
5312 kill(-pid, SIGINT);
5313# else
5314 kill(0, SIGINT);
5315# endif
5316 got_int = FALSE;
5317 }
5318# ifdef __NeXT__
5319 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5320# else
5321 wait_pid = waitpid(pid, &status, WNOHANG);
5322# endif
5323 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5324 || (wait_pid == pid && WIFEXITED(status)))
5325 {
5326 wait_pid = pid;
5327 break;
5328 }
5329
5330 /* Handle any X events, e.g. serving the clipboard. */
5331 clip_update();
5332
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005333 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5334 * less time. */
5335 mch_delay(delay_msec, TRUE);
5336 if (++delay_msec > 10)
5337 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005338 }
5339 }
5340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 /*
5343 * Wait until our child has exited.
5344 * Ignore wait() returning pids of other children and returning
5345 * because of some signal like SIGWINCH.
5346 * Don't wait if wait_pid was already set above, indicating the
5347 * child already exited.
5348 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005349 if (wait_pid != pid)
5350 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaar624891f2010-10-13 16:22:09 +02005352# ifdef FEAT_GUI
5353 /* Close slave side of pty. Only do this after the child has
5354 * exited, otherwise the child may hang when it tries to write on
5355 * the pty. */
5356 if (pty_master_fd >= 0)
5357 close(pty_slave_fd);
5358# endif
5359
Bram Moolenaardf177f62005-02-22 08:39:57 +00005360 /* Make sure the child that writes to the external program is
5361 * dead. */
5362 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005363 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005364 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005365 wait4pid(wpid, NULL);
5366 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005367
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005368# ifdef FEAT_JOB_CHANNEL
5369 --dont_check_job_ended;
5370# endif
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 /*
5373 * Set to raw mode right now, otherwise a CTRL-C after
5374 * catch_signals() will kill Vim.
5375 */
5376 if (tmode == TMODE_RAW)
5377 settmode(TMODE_RAW);
5378 did_settmode = TRUE;
5379 set_signals();
5380
5381 if (WIFEXITED(status))
5382 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005383 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005385 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
5387 if (retval == EXEC_FAILED)
5388 {
5389 MSG_PUTS(_("\nCannot execute shell "));
5390 msg_outtrans(p_sh);
5391 msg_putchar('\n');
5392 }
5393 else if (!(options & SHELL_SILENT))
5394 {
5395 MSG_PUTS(_("\nshell returned "));
5396 msg_outnum((long)retval);
5397 msg_putchar('\n');
5398 }
5399 }
5400 }
5401 else
5402 MSG_PUTS(_("\nCommand terminated\n"));
5403 }
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406error:
5407 if (!did_settmode)
5408 if (tmode == TMODE_RAW)
5409 settmode(TMODE_RAW); /* set to raw mode */
5410# ifdef FEAT_TITLE
5411 resettitle();
5412# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005413 vim_free(argv);
5414 vim_free(tofree1);
5415 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005420
5421 int
5422mch_call_shell(
5423 char_u *cmd,
5424 int options) /* SHELL_*, see vim.h */
5425{
5426#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5427 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5428 return mch_call_shell_terminal(cmd, options);
5429#endif
5430#ifdef USE_SYSTEM
5431 return mch_call_shell_system(cmd, options);
5432#else
5433 return mch_call_shell_fork(cmd, options);
5434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435}
5436
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005437#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005438 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005439mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005440{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005441 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005442 int fd_in[2] = {-1, -1}; /* for stdin */
5443 int fd_out[2] = {-1, -1}; /* for stdout */
5444 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005445 int pty_master_fd = -1;
5446 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005447 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005448 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5449 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5450 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005451 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005452 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5453 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005454 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005455 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005456 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005457
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005458 if (use_out_for_err && use_null_for_out)
5459 use_null_for_err = TRUE;
5460
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005461 /* default is to fail */
5462 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005463
Bram Moolenaarb2412082017-08-20 18:09:14 +02005464 if (options->jo_pty
5465 && (!(use_file_for_in || use_null_for_in)
5466 || !(use_file_for_in || use_null_for_out)
5467 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005468 {
5469 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5470 if (job->jv_tty_out != NULL)
5471 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5472 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005473
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005474 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005475 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005476 if (use_file_for_in)
5477 {
5478 char_u *fname = options->jo_io_name[PART_IN];
5479
5480 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5481 if (fd_in[0] < 0)
5482 {
5483 EMSG2(_(e_notopen), fname);
5484 goto failed;
5485 }
5486 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005487 else
5488 /* When writing buffer lines to the input don't use the pty, so that
5489 * the pipe can be closed when all lines were written. */
5490 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5491 && pipe(fd_in) < 0)
5492 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005493
5494 if (use_file_for_out)
5495 {
5496 char_u *fname = options->jo_io_name[PART_OUT];
5497
5498 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5499 if (fd_out[1] < 0)
5500 {
5501 EMSG2(_(e_notopen), fname);
5502 goto failed;
5503 }
5504 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005505 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005506 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005507
5508 if (use_file_for_err)
5509 {
5510 char_u *fname = options->jo_io_name[PART_ERR];
5511
5512 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5513 if (fd_err[1] < 0)
5514 {
5515 EMSG2(_(e_notopen), fname);
5516 goto failed;
5517 }
5518 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005519 else if (!use_out_for_err && !use_null_for_err
5520 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005521 goto failed;
5522
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005523 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5524 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005525 if (options->jo_set & JO_CHANNEL)
5526 {
5527 channel = options->jo_channel;
5528 if (channel != NULL)
5529 ++channel->ch_refcount;
5530 }
5531 else
5532 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005533 if (channel == NULL)
5534 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005535 if (job->jv_tty_out != NULL)
5536 ch_log(channel, "using pty %s on fd %d",
5537 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005538 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005539
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005540 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005541 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005542 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005543 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005544 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005545 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005546 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005547 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005548 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005549 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005550 int null_fd = -1;
5551 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005552
Bram Moolenaar835dc632016-02-07 14:27:38 +01005553 /* child */
5554 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005555 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005556
Bram Moolenaar819524702018-02-27 19:10:00 +01005557# ifdef FEAT_JOB_CHANNEL
5558 if (ch_log_active())
5559 /* close the log file in the child */
5560 ch_logfile((char_u *)"", (char_u *)"");
5561# endif
5562
Bram Moolenaar835dc632016-02-07 14:27:38 +01005563# ifdef HAVE_SETSID
5564 /* Create our own process group, so that the child and all its
5565 * children can be kill()ed. Don't do this when using pipes,
5566 * because stdin is not a tty, we would lose /dev/tty. */
5567 (void)setsid();
5568# endif
5569
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005570# ifdef FEAT_TERMINAL
5571 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005572 {
5573 char *term = (char *)T_NAME;
5574
5575#ifdef FEAT_GUI
5576 if (term_is_gui(T_NAME))
5577 /* In the GUI 'term' is not what we want, use $TERM. */
5578 term = getenv("TERM");
5579#endif
5580 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5581 * back to "xterm". */
5582 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5583 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005584 set_child_environment(
5585 (long)options->jo_term_rows,
5586 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005587 term,
5588 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005589 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005590 else
5591# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005592 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005593
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005594 if (options->jo_env != NULL)
5595 {
5596 dict_T *dict = options->jo_env;
5597 hashitem_T *hi;
5598 int todo = (int)dict->dv_hashtab.ht_used;
5599
5600 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5601 if (!HASHITEM_EMPTY(hi))
5602 {
5603 typval_T *item = &dict_lookup(hi)->di_tv;
5604
5605 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5606 --todo;
5607 }
5608 }
5609
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005610 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005611 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005612 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005613 if (null_fd < 0)
5614 {
5615 perror("opening /dev/null failed");
5616 _exit(OPEN_NULL_FAILED);
5617 }
5618 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005619
Bram Moolenaar223896d2017-08-02 22:33:28 +02005620 if (pty_slave_fd >= 0)
5621 {
5622 /* push stream discipline modules */
5623 SetupSlavePTY(pty_slave_fd);
5624# ifdef TIOCSCTTY
5625 /* Try to become controlling tty (probably doesn't work,
5626 * unless run by root) */
5627 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5628# endif
5629 }
5630
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005631 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005632 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005633 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005634 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005635 else if (fd_in[0] < 0)
5636 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005637 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005638 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005639
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005640 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005641 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005642 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005643 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005644 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005645 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005646 }
5647 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005648 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005649 else if (fd_err[1] < 0)
5650 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005651 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005652 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005653
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005654 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005655 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005656 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005657 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005658 else if (fd_out[1] < 0)
5659 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005660 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005661 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005662
5663 if (fd_in[0] >= 0)
5664 close(fd_in[0]);
5665 if (fd_in[1] >= 0)
5666 close(fd_in[1]);
5667 if (fd_out[0] >= 0)
5668 close(fd_out[0]);
5669 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005670 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005671 if (fd_err[0] >= 0)
5672 close(fd_err[0]);
5673 if (fd_err[1] >= 0)
5674 close(fd_err[1]);
5675 if (pty_master_fd >= 0)
5676 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005677 close(pty_master_fd); /* not used in the child */
5678 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005679 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005680
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005681 if (null_fd >= 0)
5682 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005683
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005684 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5685 _exit(EXEC_FAILED);
5686
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687 /* See above for type of argv. */
5688 execvp(argv[0], argv);
5689
Bram Moolenaar4694a172016-04-21 14:05:23 +02005690 if (stderr_works)
5691 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005692# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005693 /* calling free_all_mem() here causes problems. Ignore valgrind
5694 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005695# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005696 _exit(EXEC_FAILED); /* exec failed, return failure code */
5697 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005698
5699 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005700 UNBLOCK_SIGNALS(&curset);
5701
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702 job->jv_pid = pid;
5703 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005704 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005705
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005706 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005707 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005708 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005709 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005710 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005711 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005712 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005713 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005714 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005715 if (channel != NULL)
5716 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005717 int in_fd = use_file_for_in || use_null_for_in
5718 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5719 int out_fd = use_file_for_out || use_null_for_out
5720 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005721 /* When using pty_master_fd only set it for stdout, do not duplicate it
5722 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005723 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005724 ? INVALID_FD
5725 : fd_err[0] >= 0
5726 ? fd_err[0]
5727 : (out_fd == pty_master_fd
5728 ? INVALID_FD
5729 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005730
5731 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005732 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005733 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005734 else
5735 {
5736 if (fd_in[1] >= 0)
5737 close(fd_in[1]);
5738 if (fd_out[0] >= 0)
5739 close(fd_out[0]);
5740 if (fd_err[0] >= 0)
5741 close(fd_err[0]);
5742 if (pty_master_fd >= 0)
5743 close(pty_master_fd);
5744 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005745
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005746 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005747 return;
5748
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005749failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005750 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005752 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005753 if (fd_in[1] >= 0)
5754 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005755 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005756 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005757 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005758 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005759 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005760 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005761 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005762 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005763 if (pty_master_fd >= 0)
5764 close(pty_master_fd);
5765 if (pty_slave_fd >= 0)
5766 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005767}
5768
5769 char *
5770mch_job_status(job_T *job)
5771{
5772# ifdef HAVE_UNION_WAIT
5773 union wait status;
5774# else
5775 int status = -1;
5776# endif
5777 pid_t wait_pid = 0;
5778
5779# ifdef __NeXT__
5780 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5781# else
5782 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5783# endif
5784 if (wait_pid == -1)
5785 {
5786 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005787 if (job->jv_status < JOB_ENDED)
5788 ch_log(job->jv_channel, "Job no longer exists: %s",
5789 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005790 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005791 }
5792 if (wait_pid == 0)
5793 return "run";
5794 if (WIFEXITED(status))
5795 {
5796 /* LINTED avoid "bitwise operation on signed value" */
5797 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005798 if (job->jv_status < JOB_ENDED)
5799 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005800 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005801 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005802 if (WIFSIGNALED(status))
5803 {
5804 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005805 if (job->jv_status < JOB_ENDED)
5806 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005807 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005808 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005809 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005810
5811return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005812 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005813 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005814 return "dead";
5815}
5816
5817 job_T *
5818mch_detect_ended_job(job_T *job_list)
5819{
5820# ifdef HAVE_UNION_WAIT
5821 union wait status;
5822# else
5823 int status = -1;
5824# endif
5825 pid_t wait_pid = 0;
5826 job_T *job;
5827
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005828# ifndef USE_SYSTEM
5829 /* Do not do this when waiting for a shell command to finish, we would get
5830 * the exit value here (and discard it), the exit value obtained there
5831 * would then be wrong. */
5832 if (dont_check_job_ended > 0)
5833 return NULL;
5834# endif
5835
Bram Moolenaar97792de2016-10-15 18:36:49 +02005836# ifdef __NeXT__
5837 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5838# else
5839 wait_pid = waitpid(-1, &status, WNOHANG);
5840# endif
5841 if (wait_pid <= 0)
5842 /* no process ended */
5843 return NULL;
5844 for (job = job_list; job != NULL; job = job->jv_next)
5845 {
5846 if (job->jv_pid == wait_pid)
5847 {
5848 if (WIFEXITED(status))
5849 /* LINTED avoid "bitwise operation on signed value" */
5850 job->jv_exitval = WEXITSTATUS(status);
5851 else if (WIFSIGNALED(status))
5852 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005853 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005854 {
5855 ch_log(job->jv_channel, "Job ended");
5856 job->jv_status = JOB_ENDED;
5857 }
5858 return job;
5859 }
5860 }
5861 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005862}
5863
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005864/*
5865 * Send a (deadly) signal to "job".
5866 * Return FAIL if "how" is not a valid name.
5867 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005869mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005870{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005871 int sig = -1;
5872 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005873
Bram Moolenaar923d9262016-02-25 20:56:01 +01005874 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005876 else if (STRCMP(how, "hup") == 0)
5877 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 else if (STRCMP(how, "quit") == 0)
5879 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005880 else if (STRCMP(how, "int") == 0)
5881 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005882 else if (STRCMP(how, "kill") == 0)
5883 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005884#ifdef SIGWINCH
5885 else if (STRCMP(how, "winch") == 0)
5886 sig = SIGWINCH;
5887#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005888 else if (isdigit(*how))
5889 sig = atoi((char *)how);
5890 else
5891 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005892
Bram Moolenaar72801402016-02-09 11:37:50 +01005893 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005894 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005895#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005896 if (job_pid == getpgid(job_pid))
5897 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005898#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005899
Bram Moolenaar61a66052017-07-22 18:39:00 +02005900 /* Never kill ourselves! */
5901 if (job_pid != 0)
5902 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005903
Bram Moolenaar835dc632016-02-07 14:27:38 +01005904 return OK;
5905}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005906
5907/*
5908 * Clear the data related to "job".
5909 */
5910 void
5911mch_clear_job(job_T *job)
5912{
5913 /* call waitpid because child process may become zombie */
5914# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005915 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005916# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005917 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005918# endif
5919}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005920#endif
5921
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005922#if defined(FEAT_TERMINAL) || defined(PROTO)
5923 int
5924mch_create_pty_channel(job_T *job, jobopt_T *options)
5925{
5926 int pty_master_fd = -1;
5927 int pty_slave_fd = -1;
5928 channel_T *channel;
5929
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005930 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5931 if (job->jv_tty_out != NULL)
5932 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005933 close(pty_slave_fd);
5934
5935 channel = add_channel();
5936 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005937 {
5938 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005939 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005940 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005941 if (job->jv_tty_out != NULL)
5942 ch_log(channel, "using pty %s on fd %d",
5943 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005944 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5945 channel->ch_keep_open = TRUE;
5946
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005947 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5948 * it only needs to be read once. */
5949 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005950 channel_set_job(channel, job, options);
5951 return OK;
5952}
5953#endif
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955/*
5956 * Check for CTRL-C typed by reading all available characters.
5957 * In cooked mode we should get SIGINT, no need to check.
5958 */
5959 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005960mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005962 if ((curr_tmode == TMODE_RAW || force)
5963 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 fill_input_buf(FALSE);
5965}
5966
5967/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005968 * Wait "msec" msec until a character is available from the mouse, keyboard,
5969 * from inbuf[].
5970 * "msec" == -1 will block forever.
5971 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005972 * When "ignore_input" is TRUE even check for pending input when input is
5973 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005974 * "interrupted" (if not NULL) is set to TRUE when no character is available
5975 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005976 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005977 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 */
5979 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005980WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005982#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005983 return ui_wait_for_chars_or_timer(
5984 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005985#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005986 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005987#endif
5988}
5989
5990/*
5991 * Wait "msec" msec until a character is available from the mouse or keyboard
5992 * or from inbuf[].
5993 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005994 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005995 * "interrupted" (if not NULL) is set to TRUE when no character is available
5996 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005997 * When a GUI is being used, this will never get called -- webb
5998 */
5999 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006000WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01006001{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#ifdef FEAT_MOUSE_GPM
6003 int gpm_process_wanted;
6004#endif
6005#ifdef FEAT_XCLIPBOARD
6006 int rest;
6007#endif
6008 int avail;
6009
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006010 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 return 1;
6012
6013#if defined(FEAT_MOUSE_DEC)
6014 /* May need to query the mouse position. */
6015 if (WantQueryMouse)
6016 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006017 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
6019 }
6020#endif
6021
6022 /*
6023 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6024 * events. This is a bit complicated, because they might both be defined.
6025 */
6026#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6027# ifdef FEAT_XCLIPBOARD
6028 rest = 0;
6029 if (do_xterm_trace())
6030 rest = msec;
6031# endif
6032 do
6033 {
6034# ifdef FEAT_XCLIPBOARD
6035 if (rest != 0)
6036 {
6037 msec = XT_TRACE_DELAY;
6038 if (rest >= 0 && rest < XT_TRACE_DELAY)
6039 msec = rest;
6040 if (rest >= 0)
6041 rest -= msec;
6042 }
6043# endif
6044# ifdef FEAT_MOUSE_GPM
6045 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006046 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006047 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006049 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050# endif
6051 if (!avail)
6052 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006053 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 return 1;
6055# ifdef FEAT_XCLIPBOARD
6056 if (rest == 0 || !do_xterm_trace())
6057# endif
6058 break;
6059 }
6060 }
6061 while (FALSE
6062# ifdef FEAT_MOUSE_GPM
6063 || (gpm_process_wanted && mch_gpm_process() == 0)
6064# endif
6065# ifdef FEAT_XCLIPBOARD
6066 || (!avail && rest != 0)
6067# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006068 )
6069 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006072 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
6074 return avail;
6075}
6076
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006077#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078/*
6079 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006080 * "msec" == 0 will check for characters once.
6081 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006084 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006085 * "interrupted" (if not NULL) is set to TRUE when no character is available
6086 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088#if defined(__BEOS__)
6089 int
6090#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006091 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006093RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006096 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006097#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 static int busy = FALSE;
6099
6100 /* May retry getting characters after an event was handled. */
6101# define MAY_LOOP
6102
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006103# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 /* Remember at what time we started, so that we know how much longer we
6105 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006106 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006107 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006109 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006110 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111# endif
6112
6113 /* Handle being called recursively. This may happen for the session
6114 * manager stuff, it may save the file, which does a breakcheck. */
6115 if (busy)
6116 return 0;
6117#endif
6118
6119#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006120 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121#endif
6122 {
6123#ifdef MAY_LOOP
6124 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006125# ifdef FEAT_MZSCHEME
6126 int mzquantum_used = FALSE;
6127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128#endif
6129#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006130 /* each channel may use in, out and err */
6131 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 int nfd;
6133# ifdef FEAT_XCLIPBOARD
6134 int xterm_idx = -1;
6135# endif
6136# ifdef FEAT_MOUSE_GPM
6137 int gpm_idx = -1;
6138# endif
6139# ifdef USE_XSMP
6140 int xsmp_idx = -1;
6141# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006142 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006144# ifdef FEAT_MZSCHEME
6145 mzvim_check_threads();
6146 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6147 {
6148 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6149 mzquantum_used = TRUE;
6150 }
6151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 fds[0].fd = fd;
6153 fds[0].events = POLLIN;
6154 nfd = 1;
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006157 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (xterm_Shell != (Widget)0)
6159 {
6160 xterm_idx = nfd;
6161 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6162 fds[nfd].events = POLLIN;
6163 nfd++;
6164 }
6165# endif
6166# ifdef FEAT_MOUSE_GPM
6167 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6168 {
6169 gpm_idx = nfd;
6170 fds[nfd].fd = gpm_fd;
6171 fds[nfd].events = POLLIN;
6172 nfd++;
6173 }
6174# endif
6175# ifdef USE_XSMP
6176 if (xsmp_icefd != -1)
6177 {
6178 xsmp_idx = nfd;
6179 fds[nfd].fd = xsmp_icefd;
6180 fds[nfd].events = POLLIN;
6181 nfd++;
6182 }
6183# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006184#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006185 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006186#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006187 if (interrupted != NULL)
6188 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006190 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006191
6192 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006193 if (result == 0 && interrupted != NULL && ret > 0)
6194 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006195
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006196# ifdef FEAT_MZSCHEME
6197 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006198 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006199 finished = FALSE;
6200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202# ifdef FEAT_XCLIPBOARD
6203 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6204 {
6205 xterm_update(); /* Maybe we should hand out clipboard */
6206 if (--ret == 0 && !input_available())
6207 /* Try again */
6208 finished = FALSE;
6209 }
6210# endif
6211# ifdef FEAT_MOUSE_GPM
6212 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6213 {
6214 *check_for_gpm = 1;
6215 }
6216# endif
6217# ifdef USE_XSMP
6218 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6219 {
6220 if (fds[xsmp_idx].revents & POLLIN)
6221 {
6222 busy = TRUE;
6223 xsmp_handle_requests();
6224 busy = FALSE;
6225 }
6226 else if (fds[xsmp_idx].revents & POLLHUP)
6227 {
6228 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006229 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 xsmp_close();
6231 }
6232 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006233 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
6235# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006236#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006237 /* also call when ret == 0, we may be polling a keep-open channel */
6238 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006239 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#else /* HAVE_SELECT */
6243
6244 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006245 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006246 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006248 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006250# ifdef FEAT_MZSCHEME
6251 mzvim_check_threads();
6252 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6253 {
6254 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6255 mzquantum_used = TRUE;
6256 }
6257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006259 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006261 tv.tv_sec = towait / 1000;
6262 tv.tv_usec = (towait % 1000) * (1000000/1000);
6263 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006265 else
6266 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267
6268 /*
6269 * Select on ready for reading and exceptional condition (end of file).
6270 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006271select_eintr:
6272 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006273 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 FD_ZERO(&efds);
6275 FD_SET(fd, &rfds);
6276# if !defined(__QNX__) && !defined(__CYGWIN32__)
6277 /* For QNX select() always returns 1 if this is set. Why? */
6278 FD_SET(fd, &efds);
6279# endif
6280 maxfd = fd;
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006283 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (xterm_Shell != (Widget)0)
6285 {
6286 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6287 if (maxfd < ConnectionNumber(xterm_dpy))
6288 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006289
6290 /* An event may have already been read but not handled. In
6291 * particulary, XFlush may cause this. */
6292 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294# endif
6295# ifdef FEAT_MOUSE_GPM
6296 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6297 {
6298 FD_SET(gpm_fd, &rfds);
6299 FD_SET(gpm_fd, &efds);
6300 if (maxfd < gpm_fd)
6301 maxfd = gpm_fd;
6302 }
6303# endif
6304# ifdef USE_XSMP
6305 if (xsmp_icefd != -1)
6306 {
6307 FD_SET(xsmp_icefd, &rfds);
6308 FD_SET(xsmp_icefd, &efds);
6309 if (maxfd < xsmp_icefd)
6310 maxfd = xsmp_icefd;
6311 }
6312# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006313# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006314 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006315# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006316 if (interrupted != NULL)
6317 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006319 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006320 result = ret > 0 && FD_ISSET(fd, &rfds);
6321 if (result)
6322 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006323 else if (interrupted != NULL && ret > 0)
6324 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006325
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006326# ifdef EINTR
6327 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006328 {
6329 /* Check whether window has been resized, EINTR may be caused by
6330 * SIGWINCH. */
6331 if (do_resize)
6332 handle_resize();
6333
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006334 /* Interrupted by a signal, need to try again. We ignore msec
6335 * here, because we do want to check even after a timeout if
6336 * characters are available. Needed for reading output of an
6337 * external command after the process has finished. */
6338 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006339 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006340# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006341# ifdef __TANDEM
6342 if (ret == -1 && errno == ENOTSUP)
6343 {
6344 FD_ZERO(&rfds);
6345 FD_ZERO(&efds);
6346 ret = 0;
6347 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006348# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006349# ifdef FEAT_MZSCHEME
6350 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006351 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006352 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353# endif
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355# ifdef FEAT_XCLIPBOARD
6356 if (ret > 0 && xterm_Shell != (Widget)0
6357 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6358 {
6359 xterm_update(); /* Maybe we should hand out clipboard */
6360 /* continue looping when we only got the X event and the input
6361 * buffer is empty */
6362 if (--ret == 0 && !input_available())
6363 {
6364 /* Try again */
6365 finished = FALSE;
6366 }
6367 }
6368# endif
6369# ifdef FEAT_MOUSE_GPM
6370 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6371 {
6372 if (FD_ISSET(gpm_fd, &efds))
6373 gpm_close();
6374 else if (FD_ISSET(gpm_fd, &rfds))
6375 *check_for_gpm = 1;
6376 }
6377# endif
6378# ifdef USE_XSMP
6379 if (ret > 0 && xsmp_icefd != -1)
6380 {
6381 if (FD_ISSET(xsmp_icefd, &efds))
6382 {
6383 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006384 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 xsmp_close();
6386 if (--ret == 0)
6387 finished = FALSE; /* keep going if event was only one */
6388 }
6389 else if (FD_ISSET(xsmp_icefd, &rfds))
6390 {
6391 busy = TRUE;
6392 xsmp_handle_requests();
6393 busy = FALSE;
6394 if (--ret == 0)
6395 finished = FALSE; /* keep going if event was only one */
6396 }
6397 }
6398# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006399#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006400 /* also call when ret == 0, we may be polling a keep-open channel */
6401 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006402 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405#endif /* HAVE_SELECT */
6406
6407#ifdef MAY_LOOP
6408 if (finished || msec == 0)
6409 break;
6410
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006411# ifdef FEAT_CLIENTSERVER
6412 if (server_waiting())
6413 break;
6414# endif
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 /* We're going to loop around again, find out for how long */
6417 if (msec > 0)
6418 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006419# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006421 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422# else
6423 /* Guess we got interrupted halfway. */
6424 msec = msec / 2;
6425# endif
6426 if (msec <= 0)
6427 break; /* waited long enough */
6428 }
6429#endif
6430 }
6431
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006432 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433}
6434
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006437 * Expand a path into all matching files and/or directories. Handles "*",
6438 * "?", "[a-z]", "**", etc.
6439 * "path" has backslashes before chars that are not to be expanded.
6440 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
6442 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006443mch_expandpath(
6444 garray_T *gap,
6445 char_u *path,
6446 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
Bram Moolenaar02743632005-07-25 20:42:36 +00006448 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449}
6450#endif
6451
6452/*
6453 * mch_expand_wildcards() - this code does wild-card pattern matching using
6454 * the shell
6455 *
6456 * return OK for success, FAIL for error (you may lose some memory) and put
6457 * an error message in *file.
6458 *
6459 * num_pat is number of input patterns
6460 * pat is array of pointers to input patterns
6461 * num_file is pointer to number of matched file names
6462 * file is pointer to array of pointers to matched file names
6463 */
6464
6465#ifndef SEEK_SET
6466# define SEEK_SET 0
6467#endif
6468#ifndef SEEK_END
6469# define SEEK_END 2
6470#endif
6471
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006472#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006475mch_expand_wildcards(
6476 int num_pat,
6477 char_u **pat,
6478 int *num_file,
6479 char_u ***file,
6480 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 int i;
6483 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006484 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 char_u *p;
6486 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaarc7247912008-01-13 12:54:11 +00006488 /*
6489 * This is the non-OS/2 implementation (really Unix).
6490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 int j;
6492 char_u *tempname;
6493 char_u *command;
6494 FILE *fd;
6495 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006496#define STYLE_ECHO 0 /* use "echo", the default */
6497#define STYLE_GLOB 1 /* use "glob", for csh */
6498#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6499#define STYLE_PRINT 3 /* use "print -N", for zsh */
6500#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6501 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 int shell_style = STYLE_ECHO;
6503 int check_spaces;
6504 static int did_find_nul = FALSE;
6505 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006506 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006507 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509 *num_file = 0; /* default: no files found */
6510 *file = NULL;
6511
6512 /*
6513 * If there are no wildcards, just copy the names to allocated memory.
6514 * Saves a lot of time, because we don't have to start a new shell.
6515 */
6516 if (!have_wildcard(num_pat, pat))
6517 return save_patterns(num_pat, pat, num_file, file);
6518
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006519# ifdef HAVE_SANDBOX
6520 /* Don't allow any shell command in the sandbox. */
6521 if (sandbox != 0 && check_secure())
6522 return FAIL;
6523# endif
6524
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 /*
6526 * Don't allow the use of backticks in secure and restricted mode.
6527 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006528 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 for (i = 0; i < num_pat; ++i)
6530 if (vim_strchr(pat[i], '`') != NULL
6531 && (check_restricted() || check_secure()))
6532 return FAIL;
6533
6534 /*
6535 * get a name for the temp file
6536 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006537 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
6539 EMSG(_(e_notmp));
6540 return FAIL;
6541 }
6542
6543 /*
6544 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006545 * file.
6546 * STYLE_BT: NL separated
6547 * If expanding `cmd` execute it directly.
6548 * STYLE_GLOB: NUL separated
6549 * If we use *csh, "glob" will work better than "echo".
6550 * STYLE_PRINT: NL or NUL separated
6551 * If we use *zsh, "print -N" will work better than "glob".
6552 * STYLE_VIMGLOB: NL separated
6553 * If we use *sh*, we define "vimglob()".
6554 * STYLE_ECHO: space separated.
6555 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 */
6557 if (num_pat == 1 && *pat[0] == '`'
6558 && (len = STRLEN(pat[0])) > 2
6559 && *(pat[0] + len - 1) == '`')
6560 shell_style = STYLE_BT;
6561 else if ((len = STRLEN(p_sh)) >= 3)
6562 {
6563 if (STRCMP(p_sh + len - 3, "csh") == 0)
6564 shell_style = STYLE_GLOB;
6565 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6566 shell_style = STYLE_PRINT;
6567 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006568 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6569 "sh") != NULL)
6570 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Bram Moolenaarc7247912008-01-13 12:54:11 +00006572 /* Compute the length of the command. We need 2 extra bytes: for the
6573 * optional '&' and for the NUL.
6574 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006576 if (shell_style == STYLE_VIMGLOB)
6577 len += STRLEN(sh_vimglob_func);
6578
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006579 for (i = 0; i < num_pat; ++i)
6580 {
6581 /* Count the length of the patterns in the same way as they are put in
6582 * "command" below. */
6583#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006585#else
6586 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006587 for (j = 0; pat[i][j] != NUL; ++j)
6588 {
6589 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6590 ++len; /* may add a backslash */
6591 ++len;
6592 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006593#endif
6594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 command = alloc(len);
6596 if (command == NULL)
6597 {
6598 /* out of memory */
6599 vim_free(tempname);
6600 return FAIL;
6601 }
6602
6603 /*
6604 * Build the shell command:
6605 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6606 * recognizes this).
6607 * - Add the shell command to print the expanded names.
6608 * - Add the temp file name.
6609 * - Add the file name patterns.
6610 */
6611 if (shell_style == STYLE_BT)
6612 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006613 /* change `command; command& ` to (command; command ) */
6614 STRCPY(command, "(");
6615 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006617 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006618 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 --p;
6620 if (*p == '&') /* remove trailing '&' */
6621 {
6622 ampersent = TRUE;
6623 *p = ' ';
6624 }
6625 STRCAT(command, ">");
6626 }
6627 else
6628 {
6629 if (flags & EW_NOTFOUND)
6630 STRCPY(command, "set nonomatch; ");
6631 else
6632 STRCPY(command, "unset nonomatch; ");
6633 if (shell_style == STYLE_GLOB)
6634 STRCAT(command, "glob >");
6635 else if (shell_style == STYLE_PRINT)
6636 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006637 else if (shell_style == STYLE_VIMGLOB)
6638 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 else
6640 STRCAT(command, "echo >");
6641 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (shell_style != STYLE_BT)
6646 for (i = 0; i < num_pat; ++i)
6647 {
6648 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006649 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006650 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651#ifdef USE_SYSTEM
6652 STRCAT(command, " \"");
6653 STRCAT(command, pat[i]);
6654 STRCAT(command, "\"");
6655#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006656 int intick = FALSE;
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 p = command + STRLEN(command);
6659 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006660 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006661 {
6662 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006663 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006664 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6665 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006666 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006667 * backslash inside backticks, before a special character
6668 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006669 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006670 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6671 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006672 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006673 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006674 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006675 else if (!intick
6676 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6677 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006678 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006679 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6680 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006681 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006682
6683 /* Copy one character. */
6684 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 *p = NUL;
6687#endif
6688 }
6689 if (flags & EW_SILENT)
6690 show_shell_mess = FALSE;
6691 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006692 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694 /*
6695 * Using zsh -G: If a pattern has no matches, it is just deleted from
6696 * the argument list, otherwise zsh gives an error message and doesn't
6697 * expand any other pattern.
6698 */
6699 if (shell_style == STYLE_PRINT)
6700 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6701
6702 /*
6703 * If we use -f then shell variables set in .cshrc won't get expanded.
6704 * vi can do it, so we will too, but it is only necessary if there is a "$"
6705 * in one of the patterns, otherwise we can still use the fast option.
6706 */
6707 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6708 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6709
6710 /*
6711 * execute the shell command
6712 */
6713 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6714
6715 /* When running in the background, give it some time to create the temp
6716 * file, but don't wait for it to finish. */
6717 if (ampersent)
6718 mch_delay(10L, TRUE);
6719
6720 extra_shell_arg = NULL; /* cleanup */
6721 show_shell_mess = TRUE;
6722 vim_free(command);
6723
Bram Moolenaarc7247912008-01-13 12:54:11 +00006724 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
6726 mch_remove(tempname);
6727 vim_free(tempname);
6728 /*
6729 * With interactive completion, the error message is not printed.
6730 * However with USE_SYSTEM, I don't know how to turn off error messages
6731 * from the shell, so screen may still get messed up -- webb.
6732 */
6733#ifndef USE_SYSTEM
6734 if (!(flags & EW_SILENT))
6735#endif
6736 {
6737 redraw_later_clear(); /* probably messed up screen */
6738 msg_putchar('\n'); /* clear bottom line quickly */
6739 cmdline_row = Rows - 1; /* continue on last line */
6740#ifdef USE_SYSTEM
6741 if (!(flags & EW_SILENT))
6742#endif
6743 {
6744 MSG(_(e_wildexpand));
6745 msg_start(); /* don't overwrite this message */
6746 }
6747 }
6748 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6749 * EW_NOTFOUND is given */
6750 if (shell_style == STYLE_BT)
6751 return FAIL;
6752 goto notfound;
6753 }
6754
6755 /*
6756 * read the names from the file into memory
6757 */
6758 fd = fopen((char *)tempname, READBIN);
6759 if (fd == NULL)
6760 {
6761 /* Something went wrong, perhaps a file name with a special char. */
6762 if (!(flags & EW_SILENT))
6763 {
6764 MSG(_(e_wildexpand));
6765 msg_start(); /* don't overwrite this message */
6766 }
6767 vim_free(tempname);
6768 goto notfound;
6769 }
6770 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006771 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006773 if (llen < 0)
6774 /* just in case ftell() would fail */
6775 buffer = NULL;
6776 else
6777 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (buffer == NULL)
6779 {
6780 /* out of memory */
6781 mch_remove(tempname);
6782 vim_free(tempname);
6783 fclose(fd);
6784 return FAIL;
6785 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006786 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 i = fread((char *)buffer, 1, len, fd);
6788 fclose(fd);
6789 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006790 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 /* unexpected read error */
6793 EMSG2(_(e_notread), tempname);
6794 vim_free(tempname);
6795 vim_free(buffer);
6796 return FAIL;
6797 }
6798 vim_free(tempname);
6799
Bram Moolenaarc7247912008-01-13 12:54:11 +00006800# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6802 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006803 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6805 *p++ = buffer[i];
6806 len = p - buffer;
6807# endif
6808
6809
6810 /* file names are separated with Space */
6811 if (shell_style == STYLE_ECHO)
6812 {
6813 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6814 p = buffer;
6815 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6816 {
6817 while (*p != ' ' && *p != '\n')
6818 ++p;
6819 p = skipwhite(p); /* skip to next entry */
6820 }
6821 }
6822 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006823 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {
6825 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6826 p = buffer;
6827 for (i = 0; *p != NUL; ++i) /* count number of entries */
6828 {
6829 while (*p != '\n' && *p != NUL)
6830 ++p;
6831 if (*p != NUL)
6832 ++p;
6833 p = skipwhite(p); /* skip leading white space */
6834 }
6835 }
6836 /* file names are separated with NUL */
6837 else
6838 {
6839 /*
6840 * Some versions of zsh use spaces instead of NULs to separate
6841 * results. Only do this when there is no NUL before the end of the
6842 * buffer, otherwise we would never be able to use file names with
6843 * embedded spaces when zsh does use NULs.
6844 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6845 * don't check for spaces again.
6846 */
6847 check_spaces = FALSE;
6848 if (shell_style == STYLE_PRINT && !did_find_nul)
6849 {
6850 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006851 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006852 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 did_find_nul = TRUE;
6854 else
6855 check_spaces = TRUE;
6856 }
6857
6858 /*
6859 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6860 * already is one, for STYLE_GLOB it needs to be added.
6861 */
6862 if (len && buffer[len - 1] == NUL)
6863 --len;
6864 else
6865 buffer[len] = NUL;
6866 i = 0;
6867 for (p = buffer; p < buffer + len; ++p)
6868 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6869 {
6870 ++i;
6871 *p = NUL;
6872 }
6873 if (len)
6874 ++i; /* count last entry */
6875 }
6876 if (i == 0)
6877 {
6878 /*
6879 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6880 * /bin/sh will happily expand it to nothing rather than returning an
6881 * error; and hey, it's good to check anyway -- webb.
6882 */
6883 vim_free(buffer);
6884 goto notfound;
6885 }
6886 *num_file = i;
6887 *file = (char_u **)alloc(sizeof(char_u *) * i);
6888 if (*file == NULL)
6889 {
6890 /* out of memory */
6891 vim_free(buffer);
6892 return FAIL;
6893 }
6894
6895 /*
6896 * Isolate the individual file names.
6897 */
6898 p = buffer;
6899 for (i = 0; i < *num_file; ++i)
6900 {
6901 (*file)[i] = p;
6902 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006903 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6904 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006906 while (!(shell_style == STYLE_ECHO && *p == ' ')
6907 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 ++p;
6909 if (p == buffer + len) /* last entry */
6910 *p = NUL;
6911 else
6912 {
6913 *p++ = NUL;
6914 p = skipwhite(p); /* skip to next entry */
6915 }
6916 }
6917 else /* NUL separates */
6918 {
6919 while (*p && p < buffer + len) /* skip entry */
6920 ++p;
6921 ++p; /* skip NUL */
6922 }
6923 }
6924
6925 /*
6926 * Move the file names to allocated memory.
6927 */
6928 for (j = 0, i = 0; i < *num_file; ++i)
6929 {
6930 /* Require the files to exist. Helps when using /bin/sh */
6931 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6932 continue;
6933
6934 /* check if this entry should be included */
6935 dir = (mch_isdir((*file)[i]));
6936 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6937 continue;
6938
Bram Moolenaara2031822006-03-07 22:29:51 +00006939 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006940 if (!dir && (flags & EW_EXEC)
6941 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006942 continue;
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6945 if (p)
6946 {
6947 STRCPY(p, (*file)[i]);
6948 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006949 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 (*file)[j++] = p;
6951 }
6952 }
6953 vim_free(buffer);
6954 *num_file = j;
6955
6956 if (*num_file == 0) /* rejected all entries */
6957 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006958 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 goto notfound;
6960 }
6961
6962 return OK;
6963
6964notfound:
6965 if (flags & EW_NOTFOUND)
6966 return save_patterns(num_pat, pat, num_file, file);
6967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968}
6969
6970#endif /* VMS */
6971
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006973save_patterns(
6974 int num_pat,
6975 char_u **pat,
6976 int *num_file,
6977 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006980 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6983 if (*file == NULL)
6984 return FAIL;
6985 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006986 {
6987 s = vim_strsave(pat[i]);
6988 if (s != NULL)
6989 /* Be compatible with expand_filename(): halve the number of
6990 * backslashes. */
6991 backslash_halve(s);
6992 (*file)[i] = s;
6993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 *num_file = num_pat;
6995 return OK;
6996}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998/*
6999 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
7000 * expand.
7001 */
7002 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007003mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007005 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (*p == '\\' && p[1] != NUL)
7008 ++p;
7009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (vim_strchr((char_u *)
7011#ifdef VMS
7012 "*?%"
7013#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#endif
7016 , *p) != NULL)
7017 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019 return FALSE;
7020}
7021
7022/*
7023 * Return TRUE if the string "p" contains a wildcard.
7024 * Don't recognize '~' at the end as a wildcard.
7025 */
7026 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007027mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007029 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (*p == '\\' && p[1] != NUL)
7032 ++p;
7033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (vim_strchr((char_u *)
7035#ifdef VMS
7036 "*?%$"
7037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039#endif
7040 , *p) != NULL
7041 || (*p == '~' && p[1] != NUL))
7042 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 return FALSE;
7045}
7046
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007048have_wildcard(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 (mch_has_wildcard(file[i]))
7054 return 1;
7055 return 0;
7056}
7057
7058 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007059have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 int i;
7062
7063 for (i = 0; i < num; i++)
7064 if (vim_strchr(file[i], '$') != NULL)
7065 return TRUE;
7066 return FALSE;
7067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007069#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070/*
7071 * Scaled-down version of rename(), which is missing in Xenix.
7072 * This version can only move regular files and will fail if the
7073 * destination exists.
7074 */
7075 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007076mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 struct stat st;
7079
7080 if (stat(dest, &st) >= 0) /* fail if destination exists */
7081 return -1;
7082 if (link(src, dest) != 0) /* link file to new name */
7083 return -1;
7084 if (mch_remove(src) == 0) /* delete link to old name */
7085 return 0;
7086 return -1;
7087}
7088#endif /* !HAVE_RENAME */
7089
7090#ifdef FEAT_MOUSE_GPM
7091/*
7092 * Initializes connection with gpm (if it isn't already opened)
7093 * Return 1 if succeeded (or connection already opened), 0 if failed
7094 */
7095 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007096gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
7098 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7099
7100 if (!gpm_flag)
7101 {
7102 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7103 gpm_connect.defaultMask = ~GPM_HARD;
7104 /* Default handling for mouse move*/
7105 gpm_connect.minMod = 0; /* Handle any modifier keys */
7106 gpm_connect.maxMod = 0xffff;
7107 if (Gpm_Open(&gpm_connect, 0) > 0)
7108 {
7109 /* gpm library tries to handling TSTP causes
7110 * problems. Anyways, we close connection to Gpm whenever
7111 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007112 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007114# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 return 1; /* succeed */
7118 }
7119 if (gpm_fd == -2)
7120 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7121 return 0;
7122 }
7123 return 1; /* already open */
7124}
7125
7126/*
7127 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 */
7129 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007130gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 if (gpm_flag && gpm_fd >= 0) /* if Open */
7133 Gpm_Close();
7134}
7135
7136/* Reads gpm event and adds special keys to input buf. Returns length of
7137 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007138 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 */
7140 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007141mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 int button;
7144 static Gpm_Event gpm_event;
7145 char_u string[6];
7146 int_u vim_modifiers;
7147 int row,col;
7148 unsigned char buttons_mask;
7149 unsigned char gpm_modifiers;
7150 static unsigned char old_buttons = 0;
7151
7152 Gpm_GetEvent(&gpm_event);
7153
7154#ifdef FEAT_GUI
7155 /* Don't put events in the input queue now. */
7156 if (hold_gui_events)
7157 return 0;
7158#endif
7159
7160 row = gpm_event.y - 1;
7161 col = gpm_event.x - 1;
7162
7163 string[0] = ESC; /* Our termcode */
7164 string[1] = 'M';
7165 string[2] = 'G';
7166 switch (GPM_BARE_EVENTS(gpm_event.type))
7167 {
7168 case GPM_DRAG:
7169 string[3] = MOUSE_DRAG;
7170 break;
7171 case GPM_DOWN:
7172 buttons_mask = gpm_event.buttons & ~old_buttons;
7173 old_buttons = gpm_event.buttons;
7174 switch (buttons_mask)
7175 {
7176 case GPM_B_LEFT:
7177 button = MOUSE_LEFT;
7178 break;
7179 case GPM_B_MIDDLE:
7180 button = MOUSE_MIDDLE;
7181 break;
7182 case GPM_B_RIGHT:
7183 button = MOUSE_RIGHT;
7184 break;
7185 default:
7186 return 0;
7187 /*Don't know what to do. Can more than one button be
7188 * reported in one event? */
7189 }
7190 string[3] = (char_u)(button | 0x20);
7191 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7192 break;
7193 case GPM_UP:
7194 string[3] = MOUSE_RELEASE;
7195 old_buttons &= ~gpm_event.buttons;
7196 break;
7197 default:
7198 return 0;
7199 }
7200 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7201 gpm_modifiers = gpm_event.modifiers;
7202 vim_modifiers = 0x0;
7203 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7204 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7205 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7206 */
7207 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7208 vim_modifiers |= MOUSE_SHIFT;
7209
7210 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7211 vim_modifiers |= MOUSE_CTRL;
7212 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7213 vim_modifiers |= MOUSE_ALT;
7214 string[3] |= vim_modifiers;
7215 string[4] = (char_u)(col + ' ' + 1);
7216 string[5] = (char_u)(row + ' ' + 1);
7217 add_to_input_buf(string, 6);
7218 return 6;
7219}
7220#endif /* FEAT_MOUSE_GPM */
7221
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007222#ifdef FEAT_SYSMOUSE
7223/*
7224 * Initialize connection with sysmouse.
7225 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7226 * output, any sysmouse output than will be processed via sig_sysmouse().
7227 * Return OK if succeeded, FAIL if failed.
7228 */
7229 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007230sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007231{
7232 struct mouse_info mouse;
7233
7234 mouse.operation = MOUSE_MODE;
7235 mouse.u.mode.mode = 0;
7236 mouse.u.mode.signal = SIGUSR2;
7237 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7238 {
7239 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7240 mouse.operation = MOUSE_SHOW;
7241 ioctl(1, CONS_MOUSECTL, &mouse);
7242 return OK;
7243 }
7244 return FAIL;
7245}
7246
7247/*
7248 * Stop processing SIGUSR2 signals, and also make sure that
7249 * virtual console do not send us any sysmouse related signal.
7250 */
7251 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007252sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007253{
7254 struct mouse_info mouse;
7255
7256 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7257 mouse.operation = MOUSE_MODE;
7258 mouse.u.mode.mode = 0;
7259 mouse.u.mode.signal = 0;
7260 ioctl(1, CONS_MOUSECTL, &mouse);
7261}
7262
7263/*
7264 * Gets info from sysmouse and adds special keys to input buf.
7265 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007266 static RETSIGTYPE
7267sig_sysmouse SIGDEFARG(sigarg)
7268{
7269 struct mouse_info mouse;
7270 struct video_info video;
7271 char_u string[6];
7272 int row, col;
7273 int button;
7274 int buttons;
7275 static int oldbuttons = 0;
7276
7277#ifdef FEAT_GUI
7278 /* Don't put events in the input queue now. */
7279 if (hold_gui_events)
7280 return;
7281#endif
7282
7283 mouse.operation = MOUSE_GETINFO;
7284 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7285 && ioctl(1, FBIO_MODEINFO, &video) != -1
7286 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7287 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7288 {
7289 row = mouse.u.data.y / video.vi_cheight;
7290 col = mouse.u.data.x / video.vi_cwidth;
7291 buttons = mouse.u.data.buttons;
7292 string[0] = ESC; /* Our termcode */
7293 string[1] = 'M';
7294 string[2] = 'S';
7295 if (oldbuttons == buttons && buttons != 0)
7296 {
7297 button = MOUSE_DRAG;
7298 }
7299 else
7300 {
7301 switch (buttons)
7302 {
7303 case 0:
7304 button = MOUSE_RELEASE;
7305 break;
7306 case 1:
7307 button = MOUSE_LEFT;
7308 break;
7309 case 2:
7310 button = MOUSE_MIDDLE;
7311 break;
7312 case 4:
7313 button = MOUSE_RIGHT;
7314 break;
7315 default:
7316 return;
7317 }
7318 oldbuttons = buttons;
7319 }
7320 string[3] = (char_u)(button);
7321 string[4] = (char_u)(col + ' ' + 1);
7322 string[5] = (char_u)(row + ' ' + 1);
7323 add_to_input_buf(string, 6);
7324 }
7325 return;
7326}
7327#endif /* FEAT_SYSMOUSE */
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007330typedef char_u * (*STRPROCSTR)(char_u *);
7331typedef char_u * (*INTPROCSTR)(int);
7332typedef int (*STRPROCINT)(char_u *);
7333typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
7335/*
7336 * Call a DLL routine which takes either a string or int param
7337 * and returns an allocated string.
7338 */
7339 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007340mch_libcall(
7341 char_u *libname,
7342 char_u *funcname,
7343 char_u *argstring, /* NULL when using a argint */
7344 int argint,
7345 char_u **string_result,/* NULL when using number_result */
7346 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348# if defined(USE_DLOPEN)
7349 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007350 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351# else
7352 shl_t hinstLib;
7353# endif
7354 STRPROCSTR ProcAdd;
7355 INTPROCSTR ProcAddI;
7356 char_u *retval_str = NULL;
7357 int retval_int = 0;
7358 int success = FALSE;
7359
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007360 /*
7361 * Get a handle to the DLL module.
7362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007364 /* First clear any error, it's not cleared by the dlopen() call. */
7365 (void)dlerror();
7366
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 hinstLib = dlopen((char *)libname, RTLD_LAZY
7368# ifdef RTLD_LOCAL
7369 | RTLD_LOCAL
7370# endif
7371 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007372 if (hinstLib == NULL)
7373 {
7374 /* "dlerr" must be used before dlclose() */
7375 dlerr = (char *)dlerror();
7376 if (dlerr != NULL)
7377 EMSG2(_("dlerror = \"%s\""), dlerr);
7378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379# else
7380 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7381# endif
7382
7383 /* If the handle is valid, try to get the function address. */
7384 if (hinstLib != NULL)
7385 {
7386# ifdef HAVE_SETJMP_H
7387 /*
7388 * Catch a crash when calling the library function. For example when
7389 * using a number where a string pointer is expected.
7390 */
7391 mch_startjmp();
7392 if (SETJMP(lc_jump_env) != 0)
7393 {
7394 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007395# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007396 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 mch_didjmp();
7399 }
7400 else
7401# endif
7402 {
7403 retval_str = NULL;
7404 retval_int = 0;
7405
7406 if (argstring != NULL)
7407 {
7408# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007409 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007410 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411# else
7412 if (shl_findsym(&hinstLib, (const char *)funcname,
7413 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7414 ProcAdd = NULL;
7415# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007416 if ((success = (ProcAdd != NULL
7417# if defined(USE_DLOPEN)
7418 && dlerr == NULL
7419# endif
7420 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {
7422 if (string_result == NULL)
7423 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7424 else
7425 retval_str = (ProcAdd)(argstring);
7426 }
7427 }
7428 else
7429 {
7430# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007431 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007432 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433# else
7434 if (shl_findsym(&hinstLib, (const char *)funcname,
7435 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7436 ProcAddI = NULL;
7437# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007438 if ((success = (ProcAddI != NULL
7439# if defined(USE_DLOPEN)
7440 && dlerr == NULL
7441# endif
7442 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
7444 if (string_result == NULL)
7445 retval_int = ((INTPROCINT)ProcAddI)(argint);
7446 else
7447 retval_str = (ProcAddI)(argint);
7448 }
7449 }
7450
7451 /* Save the string before we free the library. */
7452 /* Assume that a "1" or "-1" result is an illegal pointer. */
7453 if (string_result == NULL)
7454 *number_result = retval_int;
7455 else if (retval_str != NULL
7456 && retval_str != (char_u *)1
7457 && retval_str != (char_u *)-1)
7458 *string_result = vim_strsave(retval_str);
7459 }
7460
7461# ifdef HAVE_SETJMP_H
7462 mch_endjmp();
7463# ifdef SIGHASARG
7464 if (lc_signal != 0)
7465 {
7466 int i;
7467
7468 /* try to find the name of this signal */
7469 for (i = 0; signal_info[i].sig != -1; i++)
7470 if (lc_signal == signal_info[i].sig)
7471 break;
7472 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7473 }
7474# endif
7475# endif
7476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007478 /* "dlerr" must be used before dlclose() */
7479 if (dlerr != NULL)
7480 EMSG2(_("dlerror = \"%s\""), dlerr);
7481
7482 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 (void)dlclose(hinstLib);
7484# else
7485 (void)shl_unload(hinstLib);
7486# endif
7487 }
7488
7489 if (!success)
7490 {
7491 EMSG2(_(e_libcall), funcname);
7492 return FAIL;
7493 }
7494
7495 return OK;
7496}
7497#endif
7498
7499#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7500static int xterm_trace = -1; /* default: disabled */
7501static int xterm_button;
7502
7503/*
7504 * Setup a dummy window for X selections in a terminal.
7505 */
7506 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007507setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
7509 int z = 0;
7510 char *strp = "";
7511 Widget AppShell;
7512
7513 if (!x_connect_to_server())
7514 return;
7515
7516 open_app_context();
7517 if (app_context != NULL && xterm_Shell == (Widget)0)
7518 {
7519 int (*oldhandler)();
7520#if defined(HAVE_SETJMP_H)
7521 int (*oldIOhandler)();
7522#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007523# ifdef ELAPSED_FUNC
7524 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007527 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528# endif
7529
7530 /* Ignore X errors while opening the display */
7531 oldhandler = XSetErrorHandler(x_error_check);
7532
7533#if defined(HAVE_SETJMP_H)
7534 /* Ignore X IO errors while opening the display */
7535 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7536 mch_startjmp();
7537 if (SETJMP(lc_jump_env) != 0)
7538 {
7539 mch_didjmp();
7540 xterm_dpy = NULL;
7541 }
7542 else
7543#endif
7544 {
7545 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7546 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7547#if defined(HAVE_SETJMP_H)
7548 mch_endjmp();
7549#endif
7550 }
7551
7552#if defined(HAVE_SETJMP_H)
7553 /* Now handle X IO errors normally. */
7554 (void)XSetIOErrorHandler(oldIOhandler);
7555#endif
7556 /* Now handle X errors normally. */
7557 (void)XSetErrorHandler(oldhandler);
7558
7559 if (xterm_dpy == NULL)
7560 {
7561 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007562 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 return;
7564 }
7565
7566 /* Catch terminating error of the X server connection. */
7567 (void)XSetIOErrorHandler(x_IOerror_handler);
7568
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007569# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007571 {
7572 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007573 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007574 verbose_leave();
7575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576# endif
7577
7578 /* Create a Shell to make converters work. */
7579 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7580 applicationShellWidgetClass, xterm_dpy,
7581 NULL);
7582 if (AppShell == (Widget)0)
7583 return;
7584 xterm_Shell = XtVaCreatePopupShell("VIM",
7585 topLevelShellWidgetClass, AppShell,
7586 XtNmappedWhenManaged, 0,
7587 XtNwidth, 1,
7588 XtNheight, 1,
7589 NULL);
7590 if (xterm_Shell == (Widget)0)
7591 return;
7592
7593 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007594 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (x11_display == NULL)
7596 x11_display = xterm_dpy;
7597
7598 XtRealizeWidget(xterm_Shell);
7599 XSync(xterm_dpy, False);
7600 xterm_update();
7601 }
7602 if (xterm_Shell != (Widget)0)
7603 {
7604 clip_init(TRUE);
7605 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7606 x11_window = (Window)atol(strp);
7607 /* Check if $WINDOWID is valid. */
7608 if (test_x11_window(xterm_dpy) == FAIL)
7609 x11_window = 0;
7610 if (x11_window != 0)
7611 xterm_trace = 0;
7612 }
7613}
7614
7615 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007616start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7619 return;
7620 xterm_trace = 1;
7621 xterm_button = button;
7622 do_xterm_trace();
7623}
7624
7625
7626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007627stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
7629 if (xterm_trace < 0)
7630 return;
7631 xterm_trace = 0;
7632}
7633
7634/*
7635 * Query the xterm pointer and generate mouse termcodes if necessary
7636 * return TRUE if dragging is active, else FALSE
7637 */
7638 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007639do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 Window root, child;
7642 int root_x, root_y;
7643 int win_x, win_y;
7644 int row, col;
7645 int_u mask_return;
7646 char_u buf[50];
7647 char_u *strp;
7648 long got_hints;
7649 static char_u *mouse_code;
7650 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7651 static int prev_row = 0, prev_col = 0;
7652 static XSizeHints xterm_hints;
7653
7654 if (xterm_trace <= 0)
7655 return FALSE;
7656
7657 if (xterm_trace == 1)
7658 {
7659 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007660 * have changed recently. */
7661 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7662 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 || xterm_hints.width_inc <= 1
7664 || xterm_hints.height_inc <= 1)
7665 {
7666 xterm_trace = -1; /* Not enough data -- disable tracing */
7667 return FALSE;
7668 }
7669
7670 /* Rely on the same mouse code for the duration of this */
7671 mouse_code = find_termcode(mouse_name);
7672 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007673 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 xterm_trace = 2;
7675
7676 /* Find the offset of the chars, there might be a scrollbar on the
7677 * left of the window and/or a menu on the top (eterm etc.) */
7678 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7679 &win_x, &win_y, &mask_return);
7680 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7681 - (xterm_hints.height_inc / 2);
7682 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7683 xterm_hints.y = 2;
7684 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7685 - (xterm_hints.width_inc / 2);
7686 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7687 xterm_hints.x = 2;
7688 return TRUE;
7689 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007690 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
7692 xterm_trace = 0;
7693 return FALSE;
7694 }
7695
7696 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7697 &win_x, &win_y, &mask_return);
7698
7699 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7700 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7701 if (row == prev_row && col == prev_col)
7702 return TRUE;
7703
7704 STRCPY(buf, mouse_code);
7705 strp = buf + STRLEN(buf);
7706 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7707 *strp++ = (char_u)(col + ' ' + 1);
7708 *strp++ = (char_u)(row + ' ' + 1);
7709 *strp = 0;
7710 add_to_input_buf(buf, STRLEN(buf));
7711
7712 prev_row = row;
7713 prev_col = col;
7714 return TRUE;
7715}
7716
7717# if defined(FEAT_GUI) || defined(PROTO)
7718/*
7719 * Destroy the display, window and app_context. Required for GTK.
7720 */
7721 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007722clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
7724 if (xterm_Shell != (Widget)0)
7725 {
7726 XtDestroyWidget(xterm_Shell);
7727 xterm_Shell = (Widget)0;
7728 }
7729 if (xterm_dpy != NULL)
7730 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007731# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 /* Lesstif and Solaris crash here, lose some memory */
7733 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (x11_display == xterm_dpy)
7736 x11_display = NULL;
7737 xterm_dpy = NULL;
7738 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007739# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (app_context != (XtAppContext)NULL)
7741 {
7742 /* Lesstif and Solaris crash here, lose some memory */
7743 XtDestroyApplicationContext(app_context);
7744 app_context = (XtAppContext)NULL;
7745 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747}
7748# endif
7749
7750/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007751 * Catch up with GUI or X events.
7752 */
7753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007754clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007755{
7756# ifdef FEAT_GUI
7757 if (gui.in_use)
7758 gui_mch_update();
7759 else
7760# endif
7761 if (xterm_Shell != (Widget)0)
7762 xterm_update();
7763}
7764
7765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 * Catch up with any queued X events. This may put keyboard input into the
7767 * input buffer, call resize call-backs, trigger timers etc. If there is
7768 * nothing in the X event queue (& no timers pending), then we return
7769 * immediately.
7770 */
7771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 XEvent event;
7775
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007776 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007778 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007780 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007781 break;
7782
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007783 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007784 {
7785 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007786 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007787#ifdef FEAT_CLIENTSERVER
7788 {
7789 XPropertyEvent *e = (XPropertyEvent *)&event;
7790
7791 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007793 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007796 XtDispatchEvent(&event);
7797 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007798 else
7799 {
7800 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007801 XtAppProcessEvent(app_context, mask);
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804}
7805
7806 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007807clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809 if (xterm_Shell != (Widget)0)
7810 return clip_x11_own_selection(xterm_Shell, cbd);
7811 return FAIL;
7812}
7813
7814 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007815clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 if (xterm_Shell != (Widget)0)
7818 clip_x11_lose_selection(xterm_Shell, cbd);
7819}
7820
7821 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007822clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 if (xterm_Shell != (Widget)0)
7825 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7826}
7827
7828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007829clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831 clip_x11_set_selection(cbd);
7832}
7833#endif
7834
7835
7836#if defined(USE_XSMP) || defined(PROTO)
7837/*
7838 * Code for X Session Management Protocol.
7839 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007840static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7841static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7842static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7843static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7844static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846
7847# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007848static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
7850/*
7851 * This is our chance to ask the user if they want to save,
7852 * or abort the logout
7853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007855xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 cmdmod_T save_cmdmod;
7858 int cancel_shutdown = False;
7859
7860 save_cmdmod = cmdmod;
7861 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007862 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 /* Mustn't logout */
7864 cancel_shutdown = True;
7865 cmdmod = save_cmdmod;
7866 setcursor(); /* position cursor */
7867 out_flush();
7868
7869 /* Done interaction */
7870 SmcInteractDone(smc_conn, cancel_shutdown);
7871
7872 /* Finish off
7873 * Only end save-yourself here if we're not cancelling shutdown;
7874 * we'll get a cancelled callback later in which we'll end it.
7875 * Hopefully get around glitchy SMs (like GNOME-1)
7876 */
7877 if (!cancel_shutdown)
7878 {
7879 xsmp.save_yourself = False;
7880 SmcSaveYourselfDone(smc_conn, True);
7881 }
7882}
7883# endif
7884
7885/*
7886 * Callback that starts save-yourself.
7887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007889xsmp_handle_save_yourself(
7890 SmcConn smc_conn,
7891 SmPointer client_data UNUSED,
7892 int save_type UNUSED,
7893 Bool shutdown,
7894 int interact_style UNUSED,
7895 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 /* Handle already being in saveyourself */
7898 if (xsmp.save_yourself)
7899 SmcSaveYourselfDone(smc_conn, True);
7900 xsmp.save_yourself = True;
7901 xsmp.shutdown = shutdown;
7902
7903 /* First up, preserve all files */
7904 out_flush();
7905 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7906
7907 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007908 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
7910# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7911 /* Now see if we can ask about unsaved files */
7912 if (shutdown && !fast && gui.in_use)
7913 /* Need to interact with user, but need SM's permission */
7914 SmcInteractRequest(smc_conn, SmDialogError,
7915 xsmp_handle_interaction, client_data);
7916 else
7917# endif
7918 {
7919 /* Can stop the cycle here */
7920 SmcSaveYourselfDone(smc_conn, True);
7921 xsmp.save_yourself = False;
7922 }
7923}
7924
7925
7926/*
7927 * Callback to warn us of imminent death.
7928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007930xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 xsmp_close();
7933
7934 /* quit quickly leaving swapfiles for modified buffers behind */
7935 getout_preserve_modified(0);
7936}
7937
7938
7939/*
7940 * Callback to tell us that save-yourself has completed.
7941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007943xsmp_save_complete(
7944 SmcConn smc_conn UNUSED,
7945 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947 xsmp.save_yourself = False;
7948}
7949
7950
7951/*
7952 * Callback to tell us that an instigated shutdown was cancelled
7953 * (maybe even by us)
7954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007956xsmp_shutdown_cancelled(
7957 SmcConn smc_conn,
7958 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 if (xsmp.save_yourself)
7961 SmcSaveYourselfDone(smc_conn, True);
7962 xsmp.save_yourself = False;
7963 xsmp.shutdown = False;
7964}
7965
7966
7967/*
7968 * Callback to tell us that a new ICE connection has been established.
7969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007971xsmp_ice_connection(
7972 IceConn iceConn,
7973 IcePointer clientData UNUSED,
7974 Bool opening,
7975 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 /* Intercept creation of ICE connection fd */
7978 if (opening)
7979 {
7980 xsmp_icefd = IceConnectionNumber(iceConn);
7981 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7982 }
7983}
7984
7985
7986/* Handle any ICE processing that's required; return FAIL if SM lost */
7987 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007988xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 Bool rep;
7991
7992 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7993 == IceProcessMessagesIOError)
7994 {
7995 /* Lost ICE */
7996 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007997 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 xsmp_close();
7999 return FAIL;
8000 }
8001 else
8002 return OK;
8003}
8004
8005static int dummy;
8006
8007/* Set up X Session Management Protocol */
8008 void
8009xsmp_init(void)
8010{
8011 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 SmcCallbacks smcallbacks;
8013#if 0
8014 SmPropValue smname;
8015 SmProp smnameprop;
8016 SmProp *smprops[1];
8017#endif
8018
8019 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008020 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 xsmp.save_yourself = xsmp.shutdown = False;
8023
8024 /* Set up SM callbacks - must have all, even if they're not used */
8025 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8026 smcallbacks.save_yourself.client_data = NULL;
8027 smcallbacks.die.callback = xsmp_die;
8028 smcallbacks.die.client_data = NULL;
8029 smcallbacks.save_complete.callback = xsmp_save_complete;
8030 smcallbacks.save_complete.client_data = NULL;
8031 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8032 smcallbacks.shutdown_cancelled.client_data = NULL;
8033
8034 /* Set up a watch on ICE connection creations. The "dummy" argument is
8035 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8036 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8037 {
8038 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008039 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 return;
8041 }
8042
8043 /* Create an SM connection */
8044 xsmp.smcconn = SmcOpenConnection(
8045 NULL,
8046 NULL,
8047 SmProtoMajor,
8048 SmProtoMinor,
8049 SmcSaveYourselfProcMask | SmcDieProcMask
8050 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8051 &smcallbacks,
8052 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008053 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 sizeof(errorstring),
8055 errorstring);
8056 if (xsmp.smcconn == NULL)
8057 {
8058 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008059
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008061 {
8062 vim_snprintf(errorreport, sizeof(errorreport),
8063 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8064 verb_msg((char_u *)errorreport);
8065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 return;
8067 }
8068 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8069
8070#if 0
8071 /* ID ourselves */
8072 smname.value = "vim";
8073 smname.length = 3;
8074 smnameprop.name = "SmProgram";
8075 smnameprop.type = "SmARRAY8";
8076 smnameprop.num_vals = 1;
8077 smnameprop.vals = &smname;
8078
8079 smprops[0] = &smnameprop;
8080 SmcSetProperties(xsmp.smcconn, 1, smprops);
8081#endif
8082}
8083
8084
8085/* Shut down XSMP comms. */
8086 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008087xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 if (xsmp_icefd != -1)
8090 {
8091 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008092 if (xsmp.clientid != NULL)
8093 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008094 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 xsmp_icefd = -1;
8096 }
8097}
8098#endif /* USE_XSMP */
8099
8100
8101#ifdef EBCDIC
8102/* Translate character to its CTRL- value */
8103char CtrlTable[] =
8104{
8105/* 00 - 5E */
8106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8112/* ^ */ 0x1E,
8113/* - */ 0x1F,
8114/* 61 - 6C */
8115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8116/* _ */ 0x1F,
8117/* 6E - 80 */
8118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8119/* a */ 0x01,
8120/* b */ 0x02,
8121/* c */ 0x03,
8122/* d */ 0x37,
8123/* e */ 0x2D,
8124/* f */ 0x2E,
8125/* g */ 0x2F,
8126/* h */ 0x16,
8127/* i */ 0x05,
8128/* 8A - 90 */
8129 0, 0, 0, 0, 0, 0, 0,
8130/* j */ 0x15,
8131/* k */ 0x0B,
8132/* l */ 0x0C,
8133/* m */ 0x0D,
8134/* n */ 0x0E,
8135/* o */ 0x0F,
8136/* p */ 0x10,
8137/* q */ 0x11,
8138/* r */ 0x12,
8139/* 9A - A1 */
8140 0, 0, 0, 0, 0, 0, 0, 0,
8141/* s */ 0x13,
8142/* t */ 0x3C,
8143/* u */ 0x3D,
8144/* v */ 0x32,
8145/* w */ 0x26,
8146/* x */ 0x18,
8147/* y */ 0x19,
8148/* z */ 0x3F,
8149/* AA - AC */
8150 0, 0, 0,
8151/* [ */ 0x27,
8152/* AE - BC */
8153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8154/* ] */ 0x1D,
8155/* BE - C0 */ 0, 0, 0,
8156/* A */ 0x01,
8157/* B */ 0x02,
8158/* C */ 0x03,
8159/* D */ 0x37,
8160/* E */ 0x2D,
8161/* F */ 0x2E,
8162/* G */ 0x2F,
8163/* H */ 0x16,
8164/* I */ 0x05,
8165/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8166/* J */ 0x15,
8167/* K */ 0x0B,
8168/* L */ 0x0C,
8169/* M */ 0x0D,
8170/* N */ 0x0E,
8171/* O */ 0x0F,
8172/* P */ 0x10,
8173/* Q */ 0x11,
8174/* R */ 0x12,
8175/* DA - DF */ 0, 0, 0, 0, 0, 0,
8176/* \ */ 0x1C,
8177/* E1 */ 0,
8178/* S */ 0x13,
8179/* T */ 0x3C,
8180/* U */ 0x3D,
8181/* V */ 0x32,
8182/* W */ 0x26,
8183/* X */ 0x18,
8184/* Y */ 0x19,
8185/* Z */ 0x3F,
8186/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8188};
8189
8190char MetaCharTable[]=
8191{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8192 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8193 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8194 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8195 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8196};
8197
8198
8199/* TODO: Use characters NOT numbers!!! */
8200char CtrlCharTable[]=
8201{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8202 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8203 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8204 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8205 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8206};
8207
8208
8209#endif