blob: 99662218b641949746f65c17b7c3ee9ce1548aec [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
420 parse_queued_messages();
Bram Moolenaard85f2712017-07-28 21:51:57 +0200421 /* If input was put directly in typeahead buffer bail out here. */
422 if (typebuf_changed(tb_change_cnt))
423 return 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200424#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200425 if (wtime < 0 && did_start_blocking)
426 /* blocking and already waited for p_ut */
427 wait_time = -1;
428 else
429 {
430 if (wtime >= 0)
431 wait_time = wtime;
432 else
433 /* going to block after p_ut */
434 wait_time = p_ut;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +0100435#ifdef ELAPSED_FUNC
436 elapsed_time = ELAPSED_FUNC(start_tv);
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200437#endif
438 wait_time -= elapsed_time;
439 if (wait_time < 0)
440 {
441 if (wtime >= 0)
442 /* no character available within "wtime" */
443 return 0;
444
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +0200445 else
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200446 {
447 /* no character available within 'updatetime' */
448 did_start_blocking = TRUE;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200449 if (trigger_cursorhold() && maxlen >= 3
450 && !typebuf_changed(tb_change_cnt))
451 {
452 buf[0] = K_SPECIAL;
453 buf[1] = KS_EXTRA;
454 buf[2] = (int)KE_CURSORHOLD;
455 return 3;
456 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200457 /*
458 * If there is no character available within 'updatetime'
459 * seconds flush all the swap files to disk.
460 * Also done when interrupted by SIGWINCH.
461 */
462 before_blocking();
463 continue;
464 }
465 }
466 }
467
468#ifdef FEAT_JOB_CHANNEL
469 /* Checking if a job ended requires polling. Do this every 100 msec. */
470 if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
471 wait_time = 100L;
Bram Moolenaar8a8199e2016-11-26 15:13:33 +0100472 /* If there is readahead then parse_queued_messages() timed out and we
473 * should call it again soon. */
474 if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
475 wait_time = 10L;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200476#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100477#ifdef FEAT_BEVAL_GUI
Bram Moolenaar59716a22017-03-01 20:32:44 +0100478 if (p_beval && wait_time > 100L)
479 /* The 'balloonexpr' may indirectly invoke a callback while waiting
480 * for a character, need to check often. */
481 wait_time = 100L;
482#endif
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 /*
Bram Moolenaar48bae372010-07-29 23:12:15 +0200485 * We want to be interrupted by the winch signal
486 * or by an event on the monitored file descriptors.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200488 if (WaitForChar(wait_time, &interrupted, FALSE))
Bram Moolenaar67c53842010-05-22 18:28:27 +0200489 {
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200490 /* If input was put directly in typeahead buffer bail out here. */
491 if (typebuf_changed(tb_change_cnt))
492 return 0;
493
494 /*
495 * For some terminals we only get one character at a time.
496 * We want the get all available characters, so we could keep on
497 * trying until none is available
498 * For some other terminals this is quite slow, that's why we don't
499 * do it.
500 */
501 len = read_from_input_buf(buf, (long)maxlen);
502 if (len > 0)
503 return len;
504 continue;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200507 /* no character available */
508#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
509 /* estimate the elapsed time */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100510 elapsed_time += wait_time;
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200513 if (do_resize /* interrupted by SIGWINCH signal */
514#ifdef FEAT_CLIENTSERVER
515 || server_waiting()
516#endif
517#ifdef MESSAGE_QUEUE
518 || interrupted
519#endif
520 || wait_time > 0
Bram Moolenaar1572e302017-03-25 20:16:28 +0100521 || (wtime < 0 && !did_start_blocking))
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200522 continue;
523
524 /* no character available or interrupted */
525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200527 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528}
529
530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100531handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533 do_resize = FALSE;
534 shell_resized();
535}
536
537/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200538 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
540 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100541mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200543 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544}
545
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200546#if defined(FEAT_TERMINAL) || defined(PROTO)
547/*
548 * Check for any pending input or messages.
549 */
550 int
551mch_check_messages(void)
552{
553 return WaitForChar(0L, NULL, TRUE);
554}
555#endif
556
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
558# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000559# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560# endif
561# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
562# include <sys/sysctl.h>
563# endif
564# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
565# include <sys/sysinfo.h>
566# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100567# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100568# include <mach/mach_host.h>
569# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000573 * Return total amount of memory available in Kbyte.
574 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100577mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 long_u mem = 0;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000580 long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar362dc332018-03-05 21:59:37 +0100582# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100583 {
584 /* Mac (Darwin) way of getting the amount of RAM available */
585 mach_port_t host = mach_host_self();
586 kern_return_t kret;
587# ifdef HOST_VM_INFO64
588 struct vm_statistics64 vm_stat;
589 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar988615f2018-02-27 17:58:20 +0100591 kret = host_statistics64(host, HOST_VM_INFO64,
592 (host_info64_t)&vm_stat, &count);
593# else
594 struct vm_statistics vm_stat;
595 natural_t count = HOST_VM_INFO_COUNT;
596
597 kret = host_statistics(host, HOST_VM_INFO,
598 (host_info_t)&vm_stat, &count);
599# endif
600 if (kret == KERN_SUCCESS)
601 /* get the amount of user memory by summing each usage */
602 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
603 + vm_stat.inactive_count
604# ifdef MAC_OS_X_VERSION_10_9
605 + vm_stat.compressor_page_count
606# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100607 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100608 mach_port_deallocate(mach_task_self(), host);
609 }
610# endif
611
612# ifdef HAVE_SYSCTL
613 if (mem == 0)
614 {
615 /* BSD way of getting the amount of RAM available. */
616 int mib[2];
617 size_t len = sizeof(long_u);
618# ifdef HW_USERMEM64
619 long_u physmem;
620# else
621 /* sysctl() may return 32 bit or 64 bit, accept both */
622 union {
623 int_u u32;
624 long_u u64;
625 } physmem;
626# endif
627
628 mib[0] = CTL_HW;
629# ifdef HW_USERMEM64
630 mib[1] = HW_USERMEM64;
631# else
632 mib[1] = HW_USERMEM;
633# endif
634 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
635 {
636# ifdef HW_USERMEM64
637 mem = (long_u)physmem;
638# else
639 if (len == sizeof(physmem.u64))
640 mem = (long_u)physmem.u64;
641 else
642 mem = (long_u)physmem.u32;
643# endif
644 }
645 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (mem == 0)
650 {
651 struct sysinfo sinfo;
652
653 /* Linux way of getting amount of RAM available */
654 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000655 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200656# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar914572a2007-05-01 11:37:47 +0000657 /* avoid overflow as much as possible */
658 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
659 {
660 sinfo.mem_unit = sinfo.mem_unit >> 1;
661 --shiftright;
662 }
663 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200664# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200666# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200671# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (mem == 0)
673 {
674 long pagesize, pagecount;
675
676 /* Solaris way of getting amount of RAM available */
677 pagesize = sysconf(_SC_PAGESIZE);
678 pagecount = sysconf(_SC_PHYS_PAGES);
679 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000680 {
681 /* avoid overflow as much as possible */
682 while (shiftright > 0 && (pagesize & 1) == 0)
683 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000684 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000685 --shiftright;
686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* Return the minimum of the physical memory and the user limit, because
693 * using more than the user limit may cause Vim to be terminated. */
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200694# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
696 struct rlimit rlp;
697
698 if (getrlimit(RLIMIT_DATA, &rlp) == 0
699 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200700# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200702# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000703 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000705 {
706 mem = (long_u)rlp.rlim_cur;
707 shiftright = 10;
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000713 return mem >> shiftright;
714 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715}
716#endif
717
718 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100719mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000722#ifdef FEAT_MZSCHEME
723 long total = msec; /* remember original value */
724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
726 if (ignoreinput)
727 {
728 /* Go to cooked mode without echo, to allow SIGINT interrupting us
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000729 * here. But we don't want QUIT to kill us (CTRL-\ used in a
730 * shell may produce SIGQUIT). */
731 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 old_tmode = curr_tmode;
733 if (curr_tmode == TMODE_RAW)
734 settmode(TMODE_SLEEP);
735
736 /*
737 * Everybody sleeps in a different way...
738 * Prefer nanosleep(), some versions of usleep() can only sleep up to
739 * one second.
740 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000741#ifdef FEAT_MZSCHEME
742 do
743 {
744 /* if total is large enough, wait by portions in p_mzq */
745 if (total > p_mzq)
746 msec = p_mzq;
747 else
748 msec = total;
749 total -= msec;
750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#ifdef HAVE_NANOSLEEP
752 {
753 struct timespec ts;
754
755 ts.tv_sec = msec / 1000;
756 ts.tv_nsec = (msec % 1000) * 1000000;
757 (void)nanosleep(&ts, NULL);
758 }
759#else
760# ifdef HAVE_USLEEP
761 while (msec >= 1000)
762 {
763 usleep((unsigned int)(999 * 1000));
764 msec -= 999;
765 }
766 usleep((unsigned int)(msec * 1000));
767# else
768# ifndef HAVE_SELECT
769 poll(NULL, 0, (int)msec);
770# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 struct timeval tv;
773
774 tv.tv_sec = msec / 1000;
775 tv.tv_usec = (msec % 1000) * 1000;
776 /*
777 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
778 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
779 */
780 select(0, NULL, NULL, NULL, &tv);
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782# endif /* HAVE_SELECT */
783# endif /* HAVE_NANOSLEEP */
784#endif /* HAVE_USLEEP */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000785#ifdef FEAT_MZSCHEME
786 }
787 while (total > 0);
788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000791 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200794 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000797#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
799# define HAVE_CHECK_STACK_GROWTH
800/*
801 * Support for checking for an almost-out-of-stack-space situation.
802 */
803
804/*
805 * Return a pointer to an item on the stack. Used to find out if the stack
806 * grows up or down.
807 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100808static void check_stack_growth(char *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809static int stack_grows_downwards;
810
811/*
812 * Find out if the stack grows upwards or downwards.
813 * "p" points to a variable on the stack of the caller.
814 */
815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100816check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 int i;
819
820 stack_grows_downwards = (p > (char *)&i);
821}
822#endif
823
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000824#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825static char *stack_limit = NULL;
826
827#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
828# include <pthread.h>
829# include <pthread_np.h>
830#endif
831
832/*
833 * Find out until how var the stack can grow without getting into trouble.
834 * Called when starting up and when switching to the signal stack in
835 * deathtrap().
836 */
837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100838get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 struct rlimit rlp;
841 int i;
842 long lim;
843
844 /* Set the stack limit to 15/16 of the allowable size. Skip this when the
845 * limit doesn't fit in a long (rlim_cur might be "long long"). */
846 if (getrlimit(RLIMIT_STACK, &rlp) == 0
847 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
848# ifdef RLIM_INFINITY
849 && rlp.rlim_cur != RLIM_INFINITY
850# endif
851 )
852 {
853 lim = (long)rlp.rlim_cur;
854#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
855 {
856 pthread_attr_t attr;
857 size_t size;
858
859 /* On FreeBSD the initial thread always has a fixed stack size, no
860 * matter what the limits are set to. Normally it's 1 Mbyte. */
861 pthread_attr_init(&attr);
862 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
863 {
864 pthread_attr_getstacksize(&attr, &size);
865 if (lim > (long)size)
866 lim = (long)size;
867 }
868 pthread_attr_destroy(&attr);
869 }
870#endif
871 if (stack_grows_downwards)
872 {
873 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
874 if (stack_limit >= (char *)&i)
875 /* overflow, set to 1/16 of current stack position */
876 stack_limit = (char *)((long)&i / 16L);
877 }
878 else
879 {
880 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
881 if (stack_limit <= (char *)&i)
882 stack_limit = NULL; /* overflow */
883 }
884 }
885}
886
887/*
888 * Return FAIL when running out of stack space.
889 * "p" must point to any variable local to the caller that's on the stack.
890 */
891 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100892mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 if (stack_limit != NULL)
895 {
896 if (stack_grows_downwards)
897 {
898 if (p < stack_limit)
899 return FAIL;
900 }
901 else if (p > stack_limit)
902 return FAIL;
903 }
904 return OK;
905}
906#endif
907
908#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
909/*
910 * Support for using the signal stack.
911 * This helps when we run out of stack space, which causes a SIGSEGV. The
912 * signal handler then must run on another stack, since the normal stack is
913 * completely full.
914 */
915
916#ifndef SIGSTKSZ
917# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
918#endif
919
920# ifdef HAVE_SIGALTSTACK
921static stack_t sigstk; /* for sigaltstack() */
922# else
923static struct sigstack sigstk; /* for sigstack() */
924# endif
925
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100926static void init_signal_stack(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927static char *signal_stack;
928
929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100930init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 if (signal_stack != NULL)
933 {
934# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935# ifdef HAVE_SS_BASE
936 sigstk.ss_base = signal_stack;
937# else
938 sigstk.ss_sp = signal_stack;
939# endif
940 sigstk.ss_size = SIGSTKSZ;
941 sigstk.ss_flags = 0;
942 (void)sigaltstack(&sigstk, NULL);
943# else
944 sigstk.ss_sp = signal_stack;
945 if (stack_grows_downwards)
946 sigstk.ss_sp += SIGSTKSZ - 1;
947 sigstk.ss_onstack = 0;
948 (void)sigstack(&sigstk, NULL);
949# endif
950 }
951}
952#endif
953
954/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000955 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * will barf when the second argument to signal() is ``wrong''.
957 * Let me try it with a few tricky defines from my own osdef.h (jw).
958 */
959#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 static RETSIGTYPE
961sig_winch SIGDEFARG(sigarg)
962{
963 /* this is not required on all systems, but it doesn't hurt anybody */
964 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
965 do_resize = TRUE;
966 SIGRETURN;
967}
968#endif
969
970#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 static RETSIGTYPE
972catch_sigint SIGDEFARG(sigarg)
973{
974 /* this is not required on all systems, but it doesn't hurt anybody */
975 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
976 got_int = TRUE;
977 SIGRETURN;
978}
979#endif
980
981#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 static RETSIGTYPE
983catch_sigpwr SIGDEFARG(sigarg)
984{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000985 /* this is not required on all systems, but it doesn't hurt anybody */
986 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 /*
988 * I'm not sure we get the SIGPWR signal when the system is really going
989 * down or when the batteries are almost empty. Just preserve the swap
990 * files and don't exit, that can't do any harm.
991 */
992 ml_sync_all(FALSE, FALSE);
993 SIGRETURN;
994}
995#endif
996
997#ifdef SET_SIG_ALARM
998/*
999 * signal function for alarm().
1000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 static RETSIGTYPE
1002sig_alarm SIGDEFARG(sigarg)
1003{
1004 /* doesn't do anything, just to break a system call */
1005 sig_alarm_called = TRUE;
1006 SIGRETURN;
1007}
1008#endif
1009
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001010#if (defined(HAVE_SETJMP_H) \
1011 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
1012 || defined(FEAT_LIBCALL))) \
1013 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/*
1015 * A simplistic version of setjmp() that only allows one level of using.
1016 * Don't call twice before calling mch_endjmp()!.
1017 * Usage:
1018 * mch_startjmp();
1019 * if (SETJMP(lc_jump_env) != 0)
1020 * {
1021 * mch_didjmp();
1022 * EMSG("crash!");
1023 * }
1024 * else
1025 * {
1026 * do_the_work;
1027 * mch_endjmp();
1028 * }
1029 * Note: Can't move SETJMP() here, because a function calling setjmp() must
1030 * not return before the saved environment is used.
1031 * Returns OK for normal return, FAIL when the protected code caused a
1032 * problem and LONGJMP() was used.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037#ifdef SIGHASARG
1038 lc_signal = 0;
1039#endif
1040 lc_active = TRUE;
1041}
1042
1043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001044mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 lc_active = FALSE;
1047}
1048
1049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001050mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
1053 /* On FreeBSD the signal stack has to be reset after using siglongjmp(),
1054 * otherwise catching the signal only works once. */
1055 init_signal_stack();
1056# endif
1057}
1058#endif
1059
1060/*
1061 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001062 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001064 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
1065 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 static RETSIGTYPE
1068deathtrap SIGDEFARG(sigarg)
1069{
1070 static int entered = 0; /* count the number of times we got here.
1071 Note: when memory has been corrupted
1072 this may get an arbitrary value! */
1073#ifdef SIGHASARG
1074 int i;
1075#endif
1076
1077#if defined(HAVE_SETJMP_H)
1078 /*
1079 * Catch a crash in protected code.
1080 * Restores the environment saved in lc_jump_env, which looks like
1081 * SETJMP() returns 1.
1082 */
1083 if (lc_active)
1084 {
1085# if defined(SIGHASARG)
1086 lc_signal = sigarg;
1087# endif
1088 lc_active = FALSE; /* don't jump again */
1089 LONGJMP(lc_jump_env, 1);
1090 /* NOTREACHED */
1091 }
1092#endif
1093
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001094#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +00001095# ifdef SIGQUIT
1096 /* While in mch_delay() we go to cooked mode to allow a CTRL-C to
1097 * interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
1098 * pressing CTRL-\, but we don't want Vim to exit then. */
1099 if (in_mch_delay && sigarg == SIGQUIT)
1100 SIGRETURN;
1101# endif
1102
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001103 /* When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
1104 * here. This avoids that a non-reentrant function is interrupted, e.g.,
1105 * free(). Calling free() again may then cause a crash. */
1106 if (entered == 0
1107 && (0
1108# ifdef SIGHUP
1109 || sigarg == SIGHUP
1110# endif
1111# ifdef SIGQUIT
1112 || sigarg == SIGQUIT
1113# endif
1114# ifdef SIGTERM
1115 || sigarg == SIGTERM
1116# endif
1117# ifdef SIGPWR
1118 || sigarg == SIGPWR
1119# endif
1120# ifdef SIGUSR1
1121 || sigarg == SIGUSR1
1122# endif
1123# ifdef SIGUSR2
1124 || sigarg == SIGUSR2
1125# endif
1126 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001127 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001128 SIGRETURN;
1129#endif
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /* Remember how often we have been called. */
1132 ++entered;
1133
Bram Moolenaare429e702016-06-10 19:49:14 +02001134 /* Executing autocommands is likely to use more stack space than we have
1135 * available in the signal stack. */
1136 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_EVAL
1139 /* Set the v:dying variable. */
1140 set_vim_var_nr(VV_DYING, (long)entered);
1141#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001142 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001144#ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /* Since we are now using the signal stack, need to reset the stack
1146 * limit. Otherwise using a regexp will fail. */
1147 get_stack_limit();
1148#endif
1149
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001150#if 0
1151 /* This is for opening gdb the moment Vim crashes.
1152 * You need to manually adjust the file name and Vim executable name.
1153 * Suggested by SungHyun Nam. */
1154 {
1155# define VI_GDB_FILE "/tmp/vimgdb"
1156# define VIM_NAME "/usr/bin/vim"
1157 FILE *fp = fopen(VI_GDB_FILE, "w");
1158 if (fp)
1159 {
1160 fprintf(fp,
1161 "file %s\n"
1162 "attach %d\n"
1163 "set height 1000\n"
1164 "bt full\n"
1165 , VIM_NAME, getpid());
1166 fclose(fp);
1167 system("xterm -e gdb -x "VI_GDB_FILE);
1168 unlink(VI_GDB_FILE);
1169 }
1170 }
1171#endif
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#ifdef SIGHASARG
1174 /* try to find the name of this signal */
1175 for (i = 0; signal_info[i].sig != -1; i++)
1176 if (sigarg == signal_info[i].sig)
1177 break;
1178 deadly_signal = sigarg;
1179#endif
1180
1181 full_screen = FALSE; /* don't write message to the GUI, it might be
1182 * part of the problem... */
1183 /*
1184 * If something goes wrong after entering here, we may get here again.
1185 * When this happens, give a message and try to exit nicely (resetting the
1186 * terminal mode, etc.)
1187 * When this happens twice, just exit, don't even try to give a message,
1188 * stack may be corrupt or something weird.
1189 * When this still happens again (or memory was corrupted in such a way
1190 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1191 */
1192 if (entered >= 3)
1193 {
1194 reset_signals(); /* don't catch any signals anymore */
1195 may_core_dump();
1196 if (entered >= 4)
1197 _exit(8);
1198 exit(7);
1199 }
1200 if (entered == 2)
1201 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001202 /* No translation, it may call malloc(). */
1203 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 out_flush();
1205 getout(1);
1206 }
1207
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001208 /* No translation, it may call malloc(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#ifdef SIGHASARG
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001210 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 signal_info[i].name);
1212#else
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001213 sprintf((char *)IObuff, "Vim: Caught deadly signal\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001215
1216 /* Preserve files and exit. This sets the really_exiting flag to prevent
1217 * calling free(). */
1218 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaare429e702016-06-10 19:49:14 +02001220 /* NOTREACHED */
1221
Bram Moolenaar009b2592004-10-24 19:18:58 +00001222#ifdef NBDEBUG
1223 reset_signals();
1224 may_core_dump();
1225 abort();
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 SIGRETURN;
1229}
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001232 * Invoked after receiving SIGCONT. We don't know what happened while
1233 * sleeping, deal with part of that.
1234 */
1235 static void
1236after_sigcont(void)
1237{
1238# ifdef FEAT_TITLE
1239 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1240 // again later.
1241 oldtitle_outdated = TRUE;
1242# endif
1243 settmode(TMODE_RAW);
1244 need_check_timestamps = TRUE;
1245 did_check_timestamps = FALSE;
1246}
1247
1248#if defined(SIGCONT)
1249static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001250static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001251
1252/*
1253 * With multi-threading, suspending might not work immediately. Catch the
1254 * SIGCONT signal, which will be used as an indication whether the suspending
1255 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001256 *
1257 * On Linux, signal is not always handled immediately either.
1258 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001259 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001260 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001261 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001263static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001264static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266/*
1267 * signal handler for SIGCONT
1268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 static RETSIGTYPE
1270sigcont_handler SIGDEFARG(sigarg)
1271{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001272 if (in_mch_suspend)
1273 {
1274 sigcont_received = TRUE;
1275 }
1276 else
1277 {
1278 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1279 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1280 // back to a sane mode. We should redraw, but we can't really do that
1281 // in a signal handler, do a redraw later.
1282 after_sigcont();
1283 redraw_later(CLEAR);
1284 cursor_on_force();
1285 out_flush();
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 SIGRETURN;
1289}
1290#endif
1291
Bram Moolenaar62b42182010-09-21 22:09:37 +02001292# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001293static void loose_clipboard(void);
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001294# ifdef USE_SYSTEM
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001295static void save_clipboard(void);
1296static void restore_clipboard(void);
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001297
1298static void *clip_star_save = NULL;
1299static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001300# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001301
1302/*
1303 * Called when Vim is going to sleep or execute a shell command.
1304 * We can't respond to requests for the X selections. Lose them, otherwise
1305 * other applications will hang. But first copy the text to cut buffer 0.
1306 */
1307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001308loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001309{
1310 if (clip_star.owned || clip_plus.owned)
1311 {
1312 x11_export_final_selection();
1313 if (clip_star.owned)
1314 clip_lose_selection(&clip_star);
1315 if (clip_plus.owned)
1316 clip_lose_selection(&clip_plus);
1317 if (x11_display != NULL)
1318 XFlush(x11_display);
1319 }
1320}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001321
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001322# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001323/*
1324 * Save clipboard text to restore later.
1325 */
1326 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001327save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001328{
1329 if (clip_star.owned)
1330 clip_star_save = get_register('*', TRUE);
1331 if (clip_plus.owned)
1332 clip_plus_save = get_register('+', TRUE);
1333}
1334
1335/*
1336 * Restore clipboard text if no one own the X selection.
1337 */
1338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001339restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001340{
1341 if (clip_star_save != NULL)
1342 {
1343 if (!clip_gen_owner_exists(&clip_star))
1344 put_register('*', clip_star_save);
1345 else
1346 free_register(clip_star_save);
1347 clip_star_save = NULL;
1348 }
1349 if (clip_plus_save != NULL)
1350 {
1351 if (!clip_gen_owner_exists(&clip_plus))
1352 put_register('+', clip_plus_save);
1353 else
1354 free_register(clip_plus_save);
1355 clip_plus_save = NULL;
1356 }
1357}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001358# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001359#endif
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361/*
1362 * If the machine has job control, use it to suspend the program,
1363 * otherwise fake it by starting a new shell.
1364 */
1365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001366mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
1368 /* BeOS does have SIGTSTP, but it doesn't work. */
1369#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001370 in_mch_suspend = TRUE;
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 out_flush(); /* needed to make cursor visible on some systems */
1373 settmode(TMODE_COOK);
1374 out_flush(); /* needed to disable mouse on some systems */
1375
1376# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001377 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001379# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 sigcont_received = FALSE;
1381# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 kill(0, SIGTSTP); /* send ourselves a STOP signal */
Bram Moolenaar2e310482018-08-21 13:09:10 +02001384
1385# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001386 /*
1387 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001388 * immediately, but somehow not all the time, probably because it's handled
1389 * in another thread. Do not call pause() because there would be race
1390 * condition which would hang Vim if signal happened in between the test of
1391 * sigcont_received and the call to pause(). If signal is not yet received,
1392 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1393 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001394 */
1395 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001396 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001397
Bram Moolenaar262735e2009-07-14 10:20:22 +00001398 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001399 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001402 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaar2e310482018-08-21 13:09:10 +02001404 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#else
1406 suspend_shell();
1407#endif
1408}
1409
1410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001411mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 Columns = 80;
1414 Rows = 24;
1415
1416 out_flush();
1417 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001418
Bram Moolenaar56718732006-03-15 22:53:57 +00001419#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001420 mac_conv_init();
1421#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001422#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1423 win_clip_init();
1424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425}
1426
1427 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001428set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430#if defined(SIGWINCH)
1431 /*
1432 * WINDOW CHANGE signal is handled with sig_winch().
1433 */
1434 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1435#endif
1436
1437 /*
1438 * We want the STOP signal to work, to make mch_suspend() work.
1439 * For "rvim" the STOP signal is ignored.
1440 */
1441#ifdef SIGTSTP
1442 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
1443#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001444#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 signal(SIGCONT, sigcont_handler);
1446#endif
1447
1448 /*
1449 * We want to ignore breaking of PIPEs.
1450 */
1451#ifdef SIGPIPE
1452 signal(SIGPIPE, SIG_IGN);
1453#endif
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001456 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#endif
1458
1459 /*
1460 * Ignore alarm signals (Perl's alarm() generates it).
1461 */
1462#ifdef SIGALRM
1463 signal(SIGALRM, SIG_IGN);
1464#endif
1465
1466 /*
1467 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1468 * work will be lost.
1469 */
1470#ifdef SIGPWR
1471 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1472#endif
1473
1474 /*
1475 * Arrange for other signals to gracefully shutdown Vim.
1476 */
1477 catch_signals(deathtrap, SIG_ERR);
1478
1479#if defined(FEAT_GUI) && defined(SIGHUP)
1480 /*
1481 * When the GUI is running, ignore the hangup signal.
1482 */
1483 if (gui.in_use)
1484 signal(SIGHUP, SIG_IGN);
1485#endif
1486}
1487
Bram Moolenaardf177f62005-02-22 08:39:57 +00001488#if defined(SIGINT) || defined(PROTO)
1489/*
1490 * Catch CTRL-C (only works while in Cooked mode).
1491 */
1492 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001493catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001494{
1495 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1496}
1497#endif
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001500reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001503#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 /* SIGCONT isn't in the list, because its default action is ignore */
1505 signal(SIGCONT, SIG_DFL);
1506#endif
1507}
1508
1509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001510catch_signals(
1511 RETSIGTYPE (*func_deadly)(),
1512 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 int i;
1515
1516 for (i = 0; signal_info[i].sig != -1; i++)
1517 if (signal_info[i].deadly)
1518 {
1519#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1520 struct sigaction sa;
1521
1522 /* Setup to use the alternate stack for the signal function. */
1523 sa.sa_handler = func_deadly;
1524 sigemptyset(&sa.sa_mask);
1525# if defined(__linux__) && defined(_REENTRANT)
1526 /* On Linux, with glibc compiled for kernel 2.2, there is a bug in
1527 * thread handling in combination with using the alternate stack:
1528 * pthread library functions try to use the stack pointer to
1529 * identify the current thread, causing a SEGV signal, which
1530 * recursively calls deathtrap() and hangs. */
1531 sa.sa_flags = 0;
1532# else
1533 sa.sa_flags = SA_ONSTACK;
1534# endif
1535 sigaction(signal_info[i].sig, &sa, NULL);
1536#else
1537# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1538 struct sigvec sv;
1539
1540 /* Setup to use the alternate stack for the signal function. */
1541 sv.sv_handler = func_deadly;
1542 sv.sv_mask = 0;
1543 sv.sv_flags = SV_ONSTACK;
1544 sigvec(signal_info[i].sig, &sv, NULL);
1545# else
1546 signal(signal_info[i].sig, func_deadly);
1547# endif
1548#endif
1549 }
1550 else if (func_other != SIG_ERR)
1551 signal(signal_info[i].sig, func_other);
1552}
1553
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001554#ifdef HAVE_SIGPROCMASK
1555 static void
1556block_signals(sigset_t *set)
1557{
1558 sigset_t newset;
1559 int i;
1560
1561 sigemptyset(&newset);
1562
1563 for (i = 0; signal_info[i].sig != -1; i++)
1564 sigaddset(&newset, signal_info[i].sig);
1565
Bram Moolenaar2e310482018-08-21 13:09:10 +02001566# if defined(SIGCONT)
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001567 /* SIGCONT isn't in the list, because its default action is ignore */
1568 sigaddset(&newset, SIGCONT);
1569# endif
1570
1571 sigprocmask(SIG_BLOCK, &newset, set);
1572}
1573
1574 static void
1575unblock_signals(sigset_t *set)
1576{
1577 sigprocmask(SIG_SETMASK, set, NULL);
1578}
1579#endif
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001582 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001583 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1584 * return TRUE
1585 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1586 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001587 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001588 * Returns TRUE when Vim should exit.
1589 */
1590 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001592{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001593 static int got_signal = 0;
1594 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001595
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001596 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001597 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001598 case SIGNAL_BLOCK: blocked = TRUE;
1599 break;
1600
1601 case SIGNAL_UNBLOCK: blocked = FALSE;
1602 if (got_signal != 0)
1603 {
1604 kill(getpid(), got_signal);
1605 got_signal = 0;
1606 }
1607 break;
1608
1609 default: if (!blocked)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001610 return TRUE; /* exit! */
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001611 got_signal = sig;
1612#ifdef SIGPWR
1613 if (sig != SIGPWR)
1614#endif
1615 got_int = TRUE; /* break any loops */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001616 break;
1617 }
1618 return FALSE;
1619}
1620
1621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 * Check_win checks whether we have an interactive stdout.
1623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001625mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (isatty(1))
1628 return OK;
1629 return FAIL;
1630}
1631
1632/*
1633 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1634 */
1635 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001636mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 if (isatty(read_cmd_fd))
1639 return TRUE;
1640 return FALSE;
1641}
1642
1643#ifdef FEAT_X11
1644
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001645# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1647
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648/*
1649 * Give a message about the elapsed time for opening the X window.
1650 */
1651 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001652xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001654 smsg((char_u *)_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655}
1656# endif
1657#endif
1658
1659#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1660/*
1661 * A few functions shared by X11 title and clipboard code.
1662 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001663static int x_error_handler(Display *dpy, XErrorEvent *error_event);
1664static int x_error_check(Display *dpy, XErrorEvent *error_event);
1665static int x_connect_to_server(void);
1666static int test_x11_window(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668static int got_x_error = FALSE;
1669
1670/*
1671 * X Error handler, otherwise X just exits! (very rude) -- webb
1672 */
1673 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001674x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001676 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 STRCAT(IObuff, _("\nVim: Got X error\n"));
1678
1679 /* We cannot print a message and continue, because no X calls are allowed
1680 * here (causes my system to hang). Silently continuing might be an
1681 * alternative... */
1682 preserve_exit(); /* preserve files and exit */
1683
1684 return 0; /* NOTREACHED */
1685}
1686
1687/*
1688 * Another X Error handler, just used to check for errors.
1689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001691x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 got_x_error = TRUE;
1694 return 0;
1695}
1696
1697#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1698# if defined(HAVE_SETJMP_H)
1699/*
1700 * An X IO Error handler, used to catch error while opening the display.
1701 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001702static int x_IOerror_check(Display *dpy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001705x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 /* This function should not return, it causes exit(). Longjump instead. */
1708 LONGJMP(lc_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001709# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001710 return 0; /* avoid the compiler complains about missing return value */
1711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713# endif
1714
1715/*
1716 * An X IO Error handler, used to catch terminal errors.
1717 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001718static int x_IOerror_handler(Display *dpy);
1719static void may_restore_clipboard(void);
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001720static int xterm_dpy_was_reset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001723x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 xterm_dpy = NULL;
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001726 xterm_dpy_was_reset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 x11_window = 0;
1728 x11_display = NULL;
1729 xterm_Shell = (Widget)0;
1730
1731 /* This function should not return, it causes exit(). Longjump instead. */
1732 LONGJMP(x_jump_env, 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02001733# if defined(VMS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001734 return 0; /* avoid the compiler complains about missing return value */
1735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001737
1738/*
1739 * If the X11 connection was lost try to restore it.
1740 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001741 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001742 */
1743 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001744may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001745{
1746 if (xterm_dpy_was_reset)
1747 {
1748 xterm_dpy_was_reset = FALSE;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001749
1750# ifndef LESSTIF_VERSION
1751 /* This has been reported to avoid Vim getting stuck. */
1752 if (app_context != (XtAppContext)NULL)
1753 {
1754 XtDestroyApplicationContext(app_context);
1755 app_context = (XtAppContext)NULL;
1756 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
1757 }
1758# endif
1759
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001760 setup_term_clip();
1761 get_x11_title(FALSE);
1762 }
1763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
1765
1766/*
1767 * Return TRUE when connection to the X server is desired.
1768 */
1769 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001770x_connect_to_server(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#if defined(FEAT_CLIENTSERVER)
1773 if (x_force_connect)
1774 return TRUE;
1775#endif
1776 if (x_no_connect)
1777 return FALSE;
1778
1779 /* Check for a match with "exclude:" from 'clipboard'. */
1780 if (clip_exclude_prog != NULL)
1781 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001782 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 return FALSE;
1784 }
1785 return TRUE;
1786}
1787
1788/*
1789 * Test if "dpy" and x11_window are valid by getting the window title.
1790 * I don't actually want it yet, so there may be a simpler call to use, but
1791 * this will cause the error handler x_error_check() to be called if anything
1792 * is wrong, such as the window pointer being invalid (as can happen when the
1793 * user changes his DISPLAY, but not his WINDOWID) -- webb
1794 */
1795 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001796test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 int (*old_handler)();
1799 XTextProperty text_prop;
1800
1801 old_handler = XSetErrorHandler(x_error_check);
1802 got_x_error = FALSE;
1803 if (XGetWMName(dpy, x11_window, &text_prop))
1804 XFree((void *)text_prop.value);
1805 XSync(dpy, False);
1806 (void)XSetErrorHandler(old_handler);
1807
1808 if (p_verbose > 0 && got_x_error)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001809 verb_msg((char_u *)_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 return (got_x_error ? FAIL : OK);
1812}
1813#endif
1814
1815#ifdef FEAT_TITLE
1816
1817#ifdef FEAT_X11
1818
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001819static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821/*
1822 * try to get x11 window and display
1823 *
1824 * return FAIL for failure, OK otherwise
1825 */
1826 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001827get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 char *winid;
1830 static int result = -1;
1831#define XD_NONE 0 /* x11_display not set here */
1832#define XD_HERE 1 /* x11_display opened here */
1833#define XD_GUI 2 /* x11_display used from gui.dpy */
1834#define XD_XTERM 3 /* x11_display used from xterm_dpy */
1835 static int x11_display_from = XD_NONE;
1836 static int did_set_error_handler = FALSE;
1837
1838 if (!did_set_error_handler)
1839 {
1840 /* X just exits if it finds an error otherwise! */
1841 (void)XSetErrorHandler(x_error_handler);
1842 did_set_error_handler = TRUE;
1843 }
1844
Bram Moolenaar9372a112005-12-06 19:59:18 +00001845#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (gui.in_use)
1847 {
1848 /*
1849 * If the X11 display was opened here before, for the window where Vim
1850 * was started, close that one now to avoid a memory leak.
1851 */
1852 if (x11_display_from == XD_HERE && x11_display != NULL)
1853 {
1854 XCloseDisplay(x11_display);
1855 x11_display_from = XD_NONE;
1856 }
1857 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1858 {
1859 x11_display_from = XD_GUI;
1860 return OK;
1861 }
1862 x11_display = NULL;
1863 return FAIL;
1864 }
1865 else if (x11_display_from == XD_GUI)
1866 {
1867 /* GUI must have stopped somehow, clear x11_display */
1868 x11_window = 0;
1869 x11_display = NULL;
1870 x11_display_from = XD_NONE;
1871 }
1872#endif
1873
1874 /* When started with the "-X" argument, don't try connecting. */
1875 if (!x_connect_to_server())
1876 return FAIL;
1877
1878 /*
1879 * If WINDOWID not set, should try another method to find out
1880 * what the current window number is. The only code I know for
1881 * this is very complicated.
1882 * We assume that zero is invalid for WINDOWID.
1883 */
1884 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1885 x11_window = (Window)atol(winid);
1886
1887#ifdef FEAT_XCLIPBOARD
1888 if (xterm_dpy != NULL && x11_window != 0)
1889 {
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001890 /* We may have checked it already, but Gnome terminal can move us to
1891 * another window, so we need to check every time. */
1892 if (x11_display_from != XD_XTERM)
1893 {
1894 /*
1895 * If the X11 display was opened here before, for the window where
1896 * Vim was started, close that one now to avoid a memory leak.
1897 */
1898 if (x11_display_from == XD_HERE && x11_display != NULL)
1899 XCloseDisplay(x11_display);
1900 x11_display = xterm_dpy;
1901 x11_display_from = XD_XTERM;
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (test_x11_window(x11_display) == FAIL)
1904 {
1905 /* probably bad $WINDOWID */
1906 x11_window = 0;
1907 x11_display = NULL;
1908 x11_display_from = XD_NONE;
1909 return FAIL;
1910 }
1911 return OK;
1912 }
1913#endif
1914
1915 if (x11_window == 0 || x11_display == NULL)
1916 result = -1;
1917
1918 if (result != -1) /* Have already been here and set this */
1919 return result; /* Don't do all these X calls again */
1920
1921 if (x11_window != 0 && x11_display == NULL)
1922 {
1923#ifdef SET_SIG_ALARM
1924 RETSIGTYPE (*sig_save)();
1925#endif
1926#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
1927 struct timeval start_tv;
1928
1929 if (p_verbose > 0)
1930 gettimeofday(&start_tv, NULL);
1931#endif
1932
1933#ifdef SET_SIG_ALARM
1934 /*
1935 * Opening the Display may hang if the DISPLAY setting is wrong, or
1936 * the network connection is bad. Set an alarm timer to get out.
1937 */
1938 sig_alarm_called = FALSE;
1939 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1940 (RETSIGTYPE (*)())sig_alarm);
1941 alarm(2);
1942#endif
1943 x11_display = XOpenDisplay(NULL);
1944
1945#ifdef SET_SIG_ALARM
1946 alarm(0);
1947 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1948 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001949 verb_msg((char_u *)_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#endif
1951 if (x11_display != NULL)
1952 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001953# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001955 {
1956 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001957 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001958 verbose_leave();
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960# endif
1961 if (test_x11_window(x11_display) == FAIL)
1962 {
1963 /* Maybe window id is bad */
1964 x11_window = 0;
1965 XCloseDisplay(x11_display);
1966 x11_display = NULL;
1967 }
1968 else
1969 x11_display_from = XD_HERE;
1970 }
1971 }
1972 if (x11_window == 0 || x11_display == NULL)
1973 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001974
1975# ifdef FEAT_EVAL
1976 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1977# endif
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 return (result = OK);
1980}
1981
1982/*
1983 * Determine original x11 Window Title
1984 */
1985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001986get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001988 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989}
1990
1991/*
1992 * Determine original x11 Window icon
1993 */
1994 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001995get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int retval = FALSE;
1998
1999 retval = get_x11_thing(FALSE, test_only);
2000
2001 /* could not get old icon, use terminal name */
2002 if (oldicon == NULL && !test_only)
2003 {
2004 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002005 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002007 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
2010 return retval;
2011}
2012
2013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002014get_x11_thing(
2015 int get_title, /* get title string */
2016 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 XTextProperty text_prop;
2019 int retval = FALSE;
2020 Status status;
2021
2022 if (get_x11_windis() == OK)
2023 {
2024 /* Get window/icon name if any */
2025 if (get_title)
2026 status = XGetWMName(x11_display, x11_window, &text_prop);
2027 else
2028 status = XGetWMIconName(x11_display, x11_window, &text_prop);
2029
2030 /*
2031 * If terminal is xterm, then x11_window may be a child window of the
2032 * outer xterm window that actually contains the window/icon name, so
2033 * keep traversing up the tree until a window with a title/icon is
2034 * found.
2035 */
2036 /* Previously this was only done for xterm and alikes. I don't see a
2037 * reason why it would fail for other terminal emulators.
2038 * if (term_is_xterm) */
2039 {
2040 Window root;
2041 Window parent;
2042 Window win = x11_window;
2043 Window *children;
2044 unsigned int num_children;
2045
2046 while (!status || text_prop.value == NULL)
2047 {
2048 if (!XQueryTree(x11_display, win, &root, &parent, &children,
2049 &num_children))
2050 break;
2051 if (children)
2052 XFree((void *)children);
2053 if (parent == root || parent == 0)
2054 break;
2055
2056 win = parent;
2057 if (get_title)
2058 status = XGetWMName(x11_display, win, &text_prop);
2059 else
2060 status = XGetWMIconName(x11_display, win, &text_prop);
2061 }
2062 }
2063 if (status && text_prop.value != NULL)
2064 {
2065 retval = TRUE;
2066 if (!test_only)
2067 {
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002068#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
2069 if (text_prop.encoding == XA_STRING
2070# ifdef FEAT_MBYTE
2071 && !has_mbyte
2072# endif
2073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075#endif
2076 if (get_title)
2077 oldtitle = vim_strsave((char_u *)text_prop.value);
2078 else
2079 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002080#if defined(FEAT_XFONTSET) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 else
2083 {
2084 char **cl;
2085 Status transform_status;
2086 int n = 0;
2087
2088 transform_status = XmbTextPropertyToTextList(x11_display,
2089 &text_prop,
2090 &cl, &n);
2091 if (transform_status >= Success && n > 0 && cl[0])
2092 {
2093 if (get_title)
2094 oldtitle = vim_strsave((char_u *) cl[0]);
2095 else
2096 oldicon = vim_strsave((char_u *) cl[0]);
2097 XFreeStringList(cl);
2098 }
2099 else
2100 {
2101 if (get_title)
2102 oldtitle = vim_strsave((char_u *)text_prop.value);
2103 else
2104 oldicon = vim_strsave((char_u *)text_prop.value);
2105 }
2106 }
2107#endif
2108 }
2109 XFree((void *)text_prop.value);
2110 }
2111 }
2112 return retval;
2113}
2114
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002115/* Xutf8 functions are not avaialble on older systems. Note that on some
2116 * systems X_HAVE_UTF8_STRING may be defined in a header file but
2117 * Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2118 * that and defines HAVE_XUTF8SETWMPROPERTIES. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002120# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121# define USE_UTF8_STRING
2122# endif
2123#endif
2124
2125/*
2126 * Set x11 Window Title
2127 *
2128 * get_x11_windis() must be called before this and have returned OK
2129 */
2130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002131set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 /* XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2134 * when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2135 * supported everywhere and STRING doesn't work for multi-byte titles.
2136 */
2137#ifdef USE_UTF8_STRING
2138 if (enc_utf8)
2139 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2140 NULL, NULL, 0, NULL, NULL, NULL);
2141 else
2142#endif
2143 {
2144#if XtSpecificationRelease >= 4
2145# ifdef FEAT_XFONTSET
2146 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2147 NULL, NULL, 0, NULL, NULL, NULL);
2148# else
2149 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002150 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 /* directly from example 3-18 "basicwin" of Xlib Programming Manual */
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002153 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 XSetWMProperties(x11_display, x11_window, &text_prop,
2155 NULL, NULL, 0, NULL, NULL, NULL);
2156# endif
2157#else
2158 XStoreName(x11_display, x11_window, (char *)title);
2159#endif
2160 }
2161 XFlush(x11_display);
2162}
2163
2164/*
2165 * Set x11 Window icon
2166 *
2167 * get_x11_windis() must be called before this and have returned OK
2168 */
2169 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002170set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 /* See above for comments about using X*SetWMProperties(). */
2173#ifdef USE_UTF8_STRING
2174 if (enc_utf8)
2175 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2176 NULL, 0, NULL, NULL, NULL);
2177 else
2178#endif
2179 {
2180#if XtSpecificationRelease >= 4
2181# ifdef FEAT_XFONTSET
2182 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2183 NULL, 0, NULL, NULL, NULL);
2184# else
2185 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002186 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002188 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2190 NULL, 0, NULL, NULL, NULL);
2191# endif
2192#else
2193 XSetIconName(x11_display, x11_window, (char *)icon);
2194#endif
2195 }
2196 XFlush(x11_display);
2197}
2198
2199#else /* FEAT_X11 */
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002202get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 return FALSE;
2205}
2206
2207 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002208get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 if (!test_only)
2211 {
2212 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002213 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002215 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 return FALSE;
2218}
2219
2220#endif /* FEAT_X11 */
2221
2222 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002223mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 return get_x11_title(TRUE);
2226}
2227
2228 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002229mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 return get_x11_icon(TRUE);
2232}
2233
2234/*
2235 * Set the window title and icon.
2236 */
2237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002238mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 int type = 0;
2241 static int recursive = 0;
2242
2243 if (T_NAME == NULL) /* no terminal name (yet) */
2244 return;
2245 if (title == NULL && icon == NULL) /* nothing to do */
2246 return;
2247
2248 /* When one of the X11 functions causes a deadly signal, we get here again
2249 * recursively. Avoid hanging then (something is probably locked). */
2250 if (recursive)
2251 return;
2252 ++recursive;
2253
2254 /*
2255 * if the window ID and the display is known, we may use X11 calls
2256 */
2257#ifdef FEAT_X11
2258 if (get_x11_windis() == OK)
2259 type = 1;
2260#else
2261# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
2262 if (gui.in_use)
2263 type = 1;
2264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265#endif
2266
2267 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002268 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * than x11 calls, because the x11 calls don't always work
2270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if ((type || *T_TS != NUL) && title != NULL)
2272 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002273 if (oldtitle_outdated)
2274 {
2275 oldtitle_outdated = FALSE;
2276 VIM_CLEAR(oldtitle);
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (oldtitle == NULL
2279#ifdef FEAT_GUI
2280 && !gui.in_use
2281#endif
2282 ) /* first call but not in GUI, save title */
2283 (void)get_x11_title(FALSE);
2284
2285 if (*T_TS != NUL) /* it's OK if t_fs is empty */
2286 term_settitle(title);
2287#ifdef FEAT_X11
2288 else
2289# ifdef FEAT_GUI_GTK
2290 if (!gui.in_use) /* don't do this if GTK+ is running */
2291# endif
2292 set_x11_title(title); /* x11 */
2293#endif
Bram Moolenaar2fa15e62005-01-04 21:23:48 +00002294#if defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2296 else
2297 gui_mch_settitle(title, icon);
2298#endif
2299 did_set_title = TRUE;
2300 }
2301
2302 if ((type || *T_CIS != NUL) && icon != NULL)
2303 {
2304 if (oldicon == NULL
2305#ifdef FEAT_GUI
2306 && !gui.in_use
2307#endif
2308 ) /* first call, save icon */
2309 get_x11_icon(FALSE);
2310
2311 if (*T_CIS != NUL)
2312 {
2313 out_str(T_CIS); /* set icon start */
2314 out_str_nf(icon);
2315 out_str(T_CIE); /* set icon end */
2316 out_flush();
2317 }
2318#ifdef FEAT_X11
2319 else
2320# ifdef FEAT_GUI_GTK
2321 if (!gui.in_use) /* don't do this if GTK+ is running */
2322# endif
2323 set_x11_icon(icon); /* x11 */
2324#endif
2325 did_set_icon = TRUE;
2326 }
2327 --recursive;
2328}
2329
2330/*
2331 * Restore the window/icon title.
2332 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002333 * SAVE_RESTORE_TITLE only restore title
2334 * SAVE_RESTORE_ICON only restore icon
2335 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 */
2337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002338mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 /* only restore the title or icon when it has been set */
Bram Moolenaar40385db2018-08-07 22:31:44 +02002341 mch_settitle(((which & SAVE_RESTORE_TITLE) && did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002343 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2344
2345 // pop and push from/to the stack
2346 term_pop_title(which);
2347 term_push_title(which);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348}
2349
2350#endif /* FEAT_TITLE */
2351
2352/*
2353 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002354 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
2356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002357vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 if (name == NULL)
2360 return FALSE;
2361 return (STRNICMP(name, "xterm", 5) == 0
2362 || STRNICMP(name, "nxterm", 6) == 0
2363 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002364 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002366 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 || STRCMP(name, "builtin_xterm") == 0);
2368}
2369
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002370#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2371/*
2372 * Return TRUE if "name" appears to be that of a terminal
2373 * known to support the xterm-style mouse protocol.
2374 * Relies on term_is_xterm having been set to its correct value.
2375 */
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002378{
2379 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002380 && (term_is_xterm
2381 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002382 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002383 || STRICMP(name, "st") == 0
2384 || STRNICMP(name, "st-", 3) == 0
2385 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002386}
2387#endif
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
2390/*
2391 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2392 * Return 1 for "xterm".
2393 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002394 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002395 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002398use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002400 if (ttym_flags == TTYM_SGR)
2401 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002402 if (ttym_flags == TTYM_URXVT)
2403 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (ttym_flags == TTYM_XTERM2)
2405 return 2;
2406 if (ttym_flags == TTYM_XTERM)
2407 return 1;
2408 return 0;
2409}
2410#endif
2411
2412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002413vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414{
2415 if (name == NULL)
2416 return FALSE;
2417 return (STRNICMP(name, "iris-ansi", 9) == 0
2418 || STRCMP(name, "builtin_iris-ansi") == 0);
2419}
2420
2421 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002422vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 if (name == NULL)
2425 return FALSE; /* actually all ANSI comp. terminals should be here */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002426 /* catch VT100 - VT5xx */
2427 return ((STRNICMP(name, "vt", 2) == 0
2428 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 || STRCMP(name, "builtin_vt320") == 0);
2430}
2431
2432/*
2433 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2434 * This should include all windowed terminal emulators.
2435 */
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 if (name == NULL)
2440 return FALSE;
2441 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2442 return TRUE;
2443 return ( STRNICMP(name, "hpterm", 6) == 0
2444 || STRNICMP(name, "sun-cmd", 7) == 0
2445 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002446 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 || STRNICMP(name, "dtterm", 6) == 0);
2448}
2449
2450/*
2451 * Insert user name in s[len].
2452 * Return OK if a name found.
2453 */
2454 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002455mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002458 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return OK;
2460#else
2461 return mch_get_uname(getuid(), s, len);
2462#endif
2463}
2464
2465/*
2466 * Insert user name for "uid" in s[len].
2467 * Return OK if a name found.
2468 */
2469 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002470mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2473 struct passwd *pw;
2474
2475 if ((pw = getpwuid(uid)) != NULL
2476 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2477 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002478 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return OK;
2480 }
2481#endif
2482 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
2483 return FAIL; /* a number is not a name */
2484}
2485
2486/*
2487 * Insert host name is s[len].
2488 */
2489
2490#ifdef HAVE_SYS_UTSNAME_H
2491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002492mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 struct utsname vutsname;
2495
2496 if (uname(&vutsname) < 0)
2497 *s = NUL;
2498 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002499 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501#else /* HAVE_SYS_UTSNAME_H */
2502
2503# ifdef HAVE_SYS_SYSTEMINFO_H
2504# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2505# endif
2506
2507 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002508mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510# ifdef VAXC
2511 vaxc$gethostname((char *)s, len);
2512# else
2513 gethostname((char *)s, len);
2514# endif
2515 s[len - 1] = NUL; /* make sure it's terminated */
2516}
2517#endif /* HAVE_SYS_UTSNAME_H */
2518
2519/*
2520 * return process ID
2521 */
2522 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002523mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 return (long)getpid();
2526}
2527
2528#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002529static char *strerror(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002532strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534 extern int sys_nerr;
2535 extern char *sys_errlist[];
2536 static char er[20];
2537
2538 if (err > 0 && err < sys_nerr)
2539 return (sys_errlist[err]);
2540 sprintf(er, "Error %d", err);
2541 return er;
2542}
2543#endif
2544
2545/*
2546 * Get name of current directory into buffer 'buf' of length 'len' bytes.
2547 * Return OK for success, FAIL for failure.
2548 */
2549 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002550mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552#if defined(USE_GETCWD)
2553 if (getcwd((char *)buf, len) == NULL)
2554 {
2555 STRCPY(buf, strerror(errno));
2556 return FAIL;
2557 }
2558 return OK;
2559#else
2560 return (getwd((char *)buf) != NULL ? OK : FAIL);
2561#endif
2562}
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002565 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 *
2567 * return FAIL for failure, OK for success
2568 */
2569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002570mch_FullName(
2571 char_u *fname,
2572 char_u *buf,
2573 int len,
2574 int force) /* also expand when already absolute path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002577#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 int fd = -1;
2579 static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 char_u olddir[MAXPATHL];
2582 char_u *p;
2583 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002584#ifdef __CYGWIN__
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002585 char_u posix_fname[MAXPATHL]; /* Cygwin docs mention MAX_PATH, but
2586 it's not always defined */
Bram Moolenaarbf820722008-06-21 11:12:49 +00002587#endif
2588
Bram Moolenaar38323e42007-03-06 19:22:53 +00002589#ifdef VMS
2590 fname = vms_fixfilename(fname);
2591#endif
2592
Bram Moolenaara2442432007-04-26 14:26:37 +00002593#ifdef __CYGWIN__
2594 /*
2595 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2596 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002597# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar06b07342015-12-31 22:26:28 +01002598 /* Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2599 * a forward slash. */
2600 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2601 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002602# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002603 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002604# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002605 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002606#endif
2607
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002608 /* Expand it if forced or not an absolute path.
2609 * Do not do it for "/file", the result is always "/". */
2610 if ((force || !mch_isFullName(fname))
2611 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
2614 * If the file name has a path, change to that directory for a moment,
2615 * and then do the getwd() (and get back to where we were).
2616 * This will get the correct path name with "../" things.
2617 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002618 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002620#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 /*
2622 * Use fchdir() if possible, it's said to be faster and more
2623 * reliable. But on SunOS 4 it might not work. Check this by
2624 * doing a fchdir() right now.
2625 */
2626 if (!dont_fchdir)
2627 {
2628 fd = open(".", O_RDONLY | O_EXTRA, 0);
2629 if (fd >= 0 && fchdir(fd) < 0)
2630 {
2631 close(fd);
2632 fd = -1;
2633 dont_fchdir = TRUE; /* don't try again */
2634 }
2635 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 /* Only change directory when we are sure we can return to where
2639 * we are now. After doing "su" chdir(".") might not work. */
2640 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002641#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 (mch_dirname(olddir, MAXPATHL) == FAIL
2645 || mch_chdir((char *)olddir) != 0))
2646 {
2647 p = NULL; /* can't get current dir: don't chdir */
2648 retval = FAIL;
2649 }
2650 else
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 /* The directory is copied into buf[], to be able to remove
2653 * the file name without changing it (could be a string in
2654 * read-only memory) */
2655 if (p - fname >= len)
2656 retval = FAIL;
2657 else
2658 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002659 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (mch_chdir((char *)buf))
2661 retval = FAIL;
2662 else
2663 fname = p + 1;
2664 *buf = NUL;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 }
2668 if (mch_dirname(buf, len) == FAIL)
2669 {
2670 retval = FAIL;
2671 *buf = NUL;
2672 }
2673 if (p != NULL)
2674 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002675#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (fd >= 0)
2677 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002678 if (p_verbose >= 5)
2679 {
2680 verbose_enter();
2681 MSG("fchdir() to previous dir");
2682 verbose_leave();
2683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 l = fchdir(fd);
2685 close(fd);
2686 }
2687 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 l = mch_chdir((char *)olddir);
2690 if (l != 0)
2691 EMSG(_(e_prev_dir));
2692 }
2693
2694 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002695 if (l >= len - 1)
2696 retval = FAIL; /* no space for trailing "/" */
Bram Moolenaar38323e42007-03-06 19:22:53 +00002697#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002698 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002700 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 /* Catch file names which are too long. */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002705 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
2707
2708 /* Do not append ".", "/dir/." is equal to "/dir". */
2709 if (STRCMP(fname, ".") != 0)
2710 STRCAT(buf, fname);
2711
2712 return OK;
2713}
2714
2715/*
2716 * Return TRUE if "fname" does not depend on the current directory.
2717 */
2718 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002719mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002721#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 return ( fname[0] == '/' || fname[0] == '.' ||
2723 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2724 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2725 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002726#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif
2729}
2730
Bram Moolenaar24552be2005-12-10 20:17:30 +00002731#if defined(USE_FNAME_CASE) || defined(PROTO)
2732/*
2733 * Set the case of the file name, if it already exists. This will cause the
2734 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002735 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002736 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002738fname_case(
2739 char_u *name,
2740 int len UNUSED) /* buffer size, only used when name gets longer */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002741{
2742 struct stat st;
2743 char_u *slash, *tail;
2744 DIR *dirp;
2745 struct dirent *dp;
2746
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002747 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002748 {
2749 /* Open the directory where the file is located. */
2750 slash = vim_strrchr(name, '/');
2751 if (slash == NULL)
2752 {
2753 dirp = opendir(".");
2754 tail = name;
2755 }
2756 else
2757 {
2758 *slash = NUL;
2759 dirp = opendir((char *)name);
2760 *slash = '/';
2761 tail = slash + 1;
2762 }
2763
2764 if (dirp != NULL)
2765 {
2766 while ((dp = readdir(dirp)) != NULL)
2767 {
2768 /* Only accept names that differ in case and are the same byte
2769 * length. TODO: accept different length name. */
2770 if (STRICMP(tail, dp->d_name) == 0
2771 && STRLEN(tail) == STRLEN(dp->d_name))
2772 {
2773 char_u newname[MAXPATHL + 1];
2774 struct stat st2;
2775
2776 /* Verify the inode is equal. */
2777 vim_strncpy(newname, name, MAXPATHL);
2778 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2779 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002780 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002781 && st.st_ino == st2.st_ino
2782 && st.st_dev == st2.st_dev)
2783 {
2784 STRCPY(tail, dp->d_name);
2785 break;
2786 }
2787 }
2788 }
2789
2790 closedir(dirp);
2791 }
2792 }
2793}
2794#endif
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796/*
2797 * Get file permissions for 'name'.
2798 * Returns -1 when it doesn't exist.
2799 */
2800 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002801mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 struct stat statb;
2804
2805 /* Keep the #ifdef outside of stat(), it may be a macro. */
2806#ifdef VMS
2807 if (stat((char *)vms_fixfilename(name), &statb))
2808#else
2809 if (stat((char *)name, &statb))
2810#endif
2811 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002812#ifdef __INTERIX
2813 /* The top bit makes the value negative, which means the file doesn't
2814 * exist. Remove the bit, we don't use it. */
2815 return statb.st_mode & ~S_ADDACE;
2816#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819}
2820
2821/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002822 * Set file permission for "name" to "perm".
2823 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 */
2825 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002826mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 return (chmod((char *)
2829#ifdef VMS
2830 vms_fixfilename(name),
2831#else
2832 name,
2833#endif
2834 (mode_t)perm) == 0 ? OK : FAIL);
2835}
2836
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002837#if defined(HAVE_FCHMOD) || defined(PROTO)
2838/*
2839 * Set file permission for open file "fd" to "perm".
2840 * Return FAIL for failure, OK otherwise.
2841 */
2842 int
2843mch_fsetperm(int fd, long perm)
2844{
2845 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2846}
2847#endif
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#if defined(HAVE_ACL) || defined(PROTO)
2850# ifdef HAVE_SYS_ACL_H
2851# include <sys/acl.h>
2852# endif
2853# ifdef HAVE_SYS_ACCESS_H
2854# include <sys/access.h>
2855# endif
2856
2857# ifdef HAVE_SOLARIS_ACL
2858typedef struct vim_acl_solaris_T {
2859 int acl_cnt;
2860 aclent_t *acl_entry;
2861} vim_acl_solaris_T;
2862# endif
2863
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002864#if defined(HAVE_SELINUX) || defined(PROTO)
2865/*
2866 * Copy security info from "from_file" to "to_file".
2867 */
2868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002869mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002870{
2871 if (from_file == NULL)
2872 return;
2873
2874 if (selinux_enabled == -1)
2875 selinux_enabled = is_selinux_enabled();
2876
2877 if (selinux_enabled > 0)
2878 {
2879 security_context_t from_context = NULL;
2880 security_context_t to_context = NULL;
2881
2882 if (getfilecon((char *)from_file, &from_context) < 0)
2883 {
2884 /* If the filesystem doesn't support extended attributes,
2885 the original had no special security context and the
2886 target cannot have one either. */
2887 if (errno == EOPNOTSUPP)
2888 return;
2889
2890 MSG_PUTS(_("\nCould not get security context for "));
2891 msg_outtrans(from_file);
2892 msg_putchar('\n');
2893 return;
2894 }
2895 if (getfilecon((char *)to_file, &to_context) < 0)
2896 {
2897 MSG_PUTS(_("\nCould not get security context for "));
2898 msg_outtrans(to_file);
2899 msg_putchar('\n');
2900 freecon (from_context);
2901 return ;
2902 }
2903 if (strcmp(from_context, to_context) != 0)
2904 {
2905 if (setfilecon((char *)to_file, from_context) < 0)
2906 {
2907 MSG_PUTS(_("\nCould not set security context for "));
2908 msg_outtrans(to_file);
2909 msg_putchar('\n');
2910 }
2911 }
2912 freecon(to_context);
2913 freecon(from_context);
2914 }
2915}
2916#endif /* HAVE_SELINUX */
2917
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002918#if defined(HAVE_SMACK) && !defined(PROTO)
2919/*
2920 * Copy security info from "from_file" to "to_file".
2921 */
2922 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002923mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002924{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002925 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002926 {
2927 XATTR_NAME_SMACK,
2928 XATTR_NAME_SMACKEXEC,
2929 XATTR_NAME_SMACKMMAP
2930 };
2931
2932 char buffer[SMACK_LABEL_LEN];
2933 const char *name;
2934 int index;
2935 int ret;
2936 ssize_t size;
2937
2938 if (from_file == NULL)
2939 return;
2940
2941 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2942 / sizeof(smack_copied_attributes)[0]) ; index++)
2943 {
2944 /* get the name of the attribute to copy */
2945 name = smack_copied_attributes[index];
2946
2947 /* get the value of the attribute in buffer */
2948 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2949 if (size >= 0)
2950 {
2951 /* copy the attribute value of buffer */
2952 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2953 if (ret < 0)
2954 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002955 vim_snprintf((char *)IObuff, IOSIZE,
2956 _("Could not set security context %s for %s"),
2957 name, to_file);
2958 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002959 msg_putchar('\n');
2960 }
2961 }
2962 else
2963 {
2964 /* what reason of not having the attribute value? */
2965 switch (errno)
2966 {
2967 case ENOTSUP:
2968 /* extended attributes aren't supported or enabled */
2969 /* should a message be echoed? not sure... */
2970 return; /* leave because it isn't usefull to continue */
2971
2972 case ERANGE:
2973 default:
2974 /* no enough size OR unexpected error */
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002975 vim_snprintf((char *)IObuff, IOSIZE,
2976 _("Could not get security context %s for %s. Removing it!"),
2977 name, from_file);
2978 msg_puts(IObuff);
2979 msg_putchar('\n');
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002980 /* FALLTHROUGH to remove the attribute */
2981
2982 case ENODATA:
2983 /* no attribute of this name */
2984 ret = removexattr((char*)to_file, name);
Bram Moolenaar57a728d2014-04-02 23:09:26 +02002985 /* Silently ignore errors, apparently this happens when
2986 * smack is not actually being used. */
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002987 break;
2988 }
2989 }
2990 }
2991}
2992#endif /* HAVE_SMACK */
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
2995 * Return a pointer to the ACL of file "fname" in allocated memory.
2996 * Return NULL if the ACL is not available for whatever reason.
2997 */
2998 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002999mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 vim_acl_T ret = NULL;
3002#ifdef HAVE_POSIX_ACL
3003 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
3004#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003005#ifdef HAVE_SOLARIS_ZFS_ACL
3006 acl_t *aclent;
3007
3008 if (acl_get((char *)fname, 0, &aclent) < 0)
3009 return NULL;
3010 ret = (vim_acl_T)aclent;
3011#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#ifdef HAVE_SOLARIS_ACL
3013 vim_acl_solaris_T *aclent;
3014
3015 aclent = malloc(sizeof(vim_acl_solaris_T));
3016 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
3017 {
3018 free(aclent);
3019 return NULL;
3020 }
3021 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
3022 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
3023 {
3024 free(aclent->acl_entry);
3025 free(aclent);
3026 return NULL;
3027 }
3028 ret = (vim_acl_T)aclent;
3029#else
3030#if defined(HAVE_AIX_ACL)
3031 int aclsize;
3032 struct acl *aclent;
3033
3034 aclsize = sizeof(struct acl);
3035 aclent = malloc(aclsize);
3036 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3037 {
3038 if (errno == ENOSPC)
3039 {
3040 aclsize = aclent->acl_len;
3041 aclent = realloc(aclent, aclsize);
3042 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
3043 {
3044 free(aclent);
3045 return NULL;
3046 }
3047 }
3048 else
3049 {
3050 free(aclent);
3051 return NULL;
3052 }
3053 }
3054 ret = (vim_acl_T)aclent;
3055#endif /* HAVE_AIX_ACL */
3056#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003057#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#endif /* HAVE_POSIX_ACL */
3059 return ret;
3060}
3061
3062/*
3063 * Set the ACL of file "fname" to "acl" (unless it's NULL).
3064 */
3065 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003066mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 if (aclent == NULL)
3069 return;
3070#ifdef HAVE_POSIX_ACL
3071 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3072#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003073#ifdef HAVE_SOLARIS_ZFS_ACL
3074 acl_set((char *)fname, (acl_t *)aclent);
3075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#ifdef HAVE_SOLARIS_ACL
3077 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3078 ((vim_acl_solaris_T *)aclent)->acl_entry);
3079#else
3080#ifdef HAVE_AIX_ACL
3081 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
3082#endif /* HAVE_AIX_ACL */
3083#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003084#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#endif /* HAVE_POSIX_ACL */
3086}
3087
3088 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003089mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 if (aclent == NULL)
3092 return;
3093#ifdef HAVE_POSIX_ACL
3094 acl_free((acl_t)aclent);
3095#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003096#ifdef HAVE_SOLARIS_ZFS_ACL
3097 acl_free((acl_t *)aclent);
3098#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#ifdef HAVE_SOLARIS_ACL
3100 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3101 free(aclent);
3102#else
3103#ifdef HAVE_AIX_ACL
3104 free(aclent);
3105#endif /* HAVE_AIX_ACL */
3106#endif /* HAVE_SOLARIS_ACL */
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003107#endif /* HAVE_SOLARIS_ZFS_ACL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif /* HAVE_POSIX_ACL */
3109}
3110#endif
3111
3112/*
3113 * Set hidden flag for "name".
3114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 /* can't hide a file */
3119}
3120
3121/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003122 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * return FALSE if "name" is not a directory
3124 * return FALSE for error
3125 */
3126 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003127mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128{
3129 struct stat statb;
3130
3131 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3132 return FALSE;
3133 if (stat((char *)name, &statb))
3134 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136}
3137
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003138/*
3139 * return TRUE if "name" is a directory, NOT a symlink to a directory
3140 * return FALSE if "name" is not a directory
3141 * return FALSE for error
3142 */
3143 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003144mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003145{
3146 struct stat statb;
3147
3148 if (*name == NUL) /* Some stat()s don't flag "" as an error. */
3149 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003150 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003151 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003152 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003153}
3154
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003155static int executable_file(char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157/*
3158 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3159 */
3160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003161executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162{
3163 struct stat st;
3164
3165 if (stat((char *)name, &st))
3166 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003167#ifdef VMS
3168 /* Like on Unix system file can have executable rights but not necessarily
3169 * be an executable, but on Unix is not a default for an ordianry file to
3170 * have an executable flag - on VMS it is in most cases.
3171 * Therefore, this check does not have any sense - let keep us to the
3172 * conventions instead:
3173 * *.COM and *.EXE files are the executables - the rest are not. This is
3174 * not ideal but better then it was.
3175 */
3176 int vms_executable = 0;
3177 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3178 {
3179 if (strstr(vms_tolower((char*)name),".exe") != NULL
3180 || strstr(vms_tolower((char*)name),".com")!= NULL)
3181 vms_executable = 1;
3182 }
3183 return vms_executable;
3184#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187}
3188
3189/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003190 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003191 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * Return -1 if unknown.
3193 */
3194 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003195mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 char_u *buf;
3198 char_u *p, *e;
3199 int retval;
3200
Bram Moolenaarb5971142015-03-21 17:32:19 +01003201 /* When "use_path" is false and if it's an absolute or relative path don't
3202 * need to use $PATH. */
3203 if (!use_path || mch_isFullName(name) || (name[0] == '.'
3204 && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))))
Bram Moolenaar206f0112014-03-12 16:51:55 +01003205 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01003206 /* There must be a path separator, files in the current directory
3207 * can't be executed. */
3208 if (gettail(name) != name && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003209 {
3210 if (path != NULL)
3211 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003212 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003213 *path = FullName_save(name, TRUE);
3214 else
3215 *path = vim_strsave(name);
3216 }
3217 return TRUE;
3218 }
3219 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 p = (char_u *)getenv("PATH");
3223 if (p == NULL || *p == NUL)
3224 return -1;
3225 buf = alloc((unsigned)(STRLEN(name) + STRLEN(p) + 2));
3226 if (buf == NULL)
3227 return -1;
3228
3229 /*
3230 * Walk through all entries in $PATH to check if "name" exists there and
3231 * is an executable file.
3232 */
3233 for (;;)
3234 {
3235 e = (char_u *)strchr((char *)p, ':');
3236 if (e == NULL)
3237 e = p + STRLEN(p);
3238 if (e - p <= 1) /* empty entry means current dir */
3239 STRCPY(buf, "./");
3240 else
3241 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003242 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 add_pathsep(buf);
3244 }
3245 STRCAT(buf, name);
3246 retval = executable_file(buf);
3247 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003248 {
3249 if (path != NULL)
3250 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003251 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003252 *path = FullName_save(buf, TRUE);
3253 else
3254 *path = vim_strsave(buf);
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 if (*e != ':')
3260 break;
3261 p = e + 1;
3262 }
3263
3264 vim_free(buf);
3265 return retval;
3266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268/*
3269 * Check what "name" is:
3270 * NODE_NORMAL: file or directory (or doesn't exist)
3271 * NODE_WRITABLE: writable device, socket, fifo, etc.
3272 * NODE_OTHER: non-writable things
3273 */
3274 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003275mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 struct stat st;
3278
3279 if (stat((char *)name, &st))
3280 return NODE_NORMAL;
3281 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3282 return NODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (S_ISBLK(st.st_mode)) /* block device isn't writable */
3284 return NODE_OTHER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /* Everything else is writable? */
3286 return NODE_WRITABLE;
3287}
3288
3289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003290mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292#ifdef HAVE_CHECK_STACK_GROWTH
3293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 check_stack_growth((char *)&i);
3296
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003297# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 get_stack_limit();
3299# endif
3300
3301#endif
3302
3303 /*
3304 * Setup an alternative stack for signals. Helps to catch signals when
3305 * running out of stack space.
3306 * Use of sigaltstack() is preferred, it's more portable.
3307 * Ignore any errors.
3308 */
3309#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaar5a221812008-11-12 12:08:45 +00003310 signal_stack = (char *)alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 init_signal_stack();
3312#endif
3313}
3314
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003315#if defined(EXITFREE) || defined(PROTO)
3316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003317mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003318{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3320 if (clip_star.owned)
3321 clip_lose_selection(&clip_star);
3322 if (clip_plus.owned)
3323 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003324# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003325# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003326 if (xterm_Shell != (Widget)0)
3327 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003328# ifndef LESSTIF_VERSION
3329 /* Lesstif crashes here, lose some memory */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003330 if (xterm_dpy != NULL)
3331 XtCloseDisplay(xterm_dpy);
3332 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003333 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003334 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003335# ifdef FEAT_X11
3336 x11_display = NULL; /* freed by XtDestroyApplicationContext() */
3337# endif
3338 }
3339# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003340# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003341# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003342 if (x11_display != NULL
3343# ifdef FEAT_XCLIPBOARD
3344 && x11_display != xterm_dpy
3345# endif
3346 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003347 XCloseDisplay(x11_display);
3348# endif
3349# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003350 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003351# endif
3352# ifdef FEAT_TITLE
3353 vim_free(oldtitle);
3354 vim_free(oldicon);
3355# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003356}
3357#endif
3358
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003359static void exit_scroll(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361/*
3362 * Output a newline when exiting.
3363 * Make sure the newline goes to the same stream as the text.
3364 */
3365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003366exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003368 if (silent_mode)
3369 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (newline_on_exit || msg_didout)
3371 {
3372 if (msg_use_printf())
3373 {
3374 if (info_message)
3375 mch_msg("\n");
3376 else
3377 mch_errmsg("\r\n");
3378 }
3379 else
3380 out_char('\n');
3381 }
3382 else
3383 {
3384 restore_cterm_colors(); /* get original colors back */
3385 msg_clr_eos_force(); /* clear the rest of the display */
3386 windgoto((int)Rows - 1, 0); /* may have moved the cursor */
3387 }
3388}
3389
3390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003391mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 exiting = TRUE;
3394
3395#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3396 x11_export_final_selection();
3397#endif
3398
3399#ifdef FEAT_GUI
3400 if (!gui.in_use)
3401#endif
3402 {
3403 settmode(TMODE_COOK);
3404#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003405 // restore xterm title and icon name
3406 mch_restore_title(SAVE_RESTORE_BOTH);
3407 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#endif
3409 /*
3410 * When t_ti is not empty but it doesn't cause swapping terminal
3411 * pages, need to output a newline when msg_didout is set. But when
3412 * t_ti does swap pages it should not go to the shell page. Do this
3413 * before stoptermcap().
3414 */
3415 if (swapping_screen() && !newline_on_exit)
3416 exit_scroll();
3417
3418 /* Stop termcap: May need to check for T_CRV response, which
3419 * requires RAW mode. */
3420 stoptermcap();
3421
3422 /*
3423 * A newline is only required after a message in the alternate screen.
3424 * This is set to TRUE by wait_return().
3425 */
3426 if (!swapping_screen() || newline_on_exit)
3427 exit_scroll();
3428
3429 /* Cursor may have been switched off without calling starttermcap()
3430 * when doing "vim -u vimrc" and vimrc contains ":q". */
3431 if (full_screen)
3432 cursor_on();
3433 }
3434 out_flush();
3435 ml_close_all(TRUE); /* remove all memfiles */
3436 may_core_dump();
3437#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 gui_exit(r);
3440#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003441
Bram Moolenaar56718732006-03-15 22:53:57 +00003442#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003443 mac_conv_cleanup();
3444#endif
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#ifdef __QNX__
3447 /* A core dump won't be created if the signal handler
3448 * doesn't return, so we can't call exit() */
3449 if (deadly_signal != 0)
3450 return;
3451#endif
3452
Bram Moolenaar009b2592004-10-24 19:18:58 +00003453#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003454 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003455#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003456
3457#ifdef EXITFREE
3458 free_all_mem();
3459#endif
3460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 exit(r);
3462}
3463
3464 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003465may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 if (deadly_signal != 0)
3468 {
3469 signal(deadly_signal, SIG_DFL);
3470 kill(getpid(), deadly_signal); /* Die using the signal we caught */
3471 }
3472}
3473
3474#ifndef VMS
3475
3476 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003477mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 static int first = TRUE;
3480
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003481#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482# ifdef HAVE_TERMIOS_H
3483 static struct termios told;
3484 struct termios tnew;
3485# else
3486 static struct termio told;
3487 struct termio tnew;
3488# endif
3489
3490 if (first)
3491 {
3492 first = FALSE;
3493# if defined(HAVE_TERMIOS_H)
3494 tcgetattr(read_cmd_fd, &told);
3495# else
3496 ioctl(read_cmd_fd, TCGETA, &told);
3497# endif
3498 }
3499
3500 tnew = told;
3501 if (tmode == TMODE_RAW)
3502 {
3503 /*
3504 * ~ICRNL enables typing ^V^M
3505 */
3506 tnew.c_iflag &= ~ICRNL;
3507 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3508# if defined(IEXTEN) && !defined(__MINT__)
3509 | IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
3510 /* but it breaks function keys on MINT */
3511# endif
3512 );
3513# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
3514 tnew.c_oflag &= ~ONLCR;
3515# endif
3516 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3517 tnew.c_cc[VTIME] = 0; /* don't wait */
3518 }
3519 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003520 {
3521 /* Also reset ICANON here, otherwise on Solaris select() won't see
3522 * typeahead characters. */
3523 tnew.c_lflag &= ~(ICANON | ECHO);
3524 tnew.c_cc[VMIN] = 1; /* return after 1 char */
3525 tnew.c_cc[VTIME] = 0; /* don't wait */
3526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528# if defined(HAVE_TERMIOS_H)
3529 {
3530 int n = 10;
3531
3532 /* A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3533 * few times. */
3534 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3535 && errno == EINTR && n > 0)
3536 --n;
3537 }
3538# else
3539 ioctl(read_cmd_fd, TCSETA, &tnew);
3540# endif
3541
3542#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * for "old" tty systems
3545 */
3546# ifndef TIOCSETN
3547# define TIOCSETN TIOCSETP /* for hpux 9.0 */
3548# endif
3549 static struct sgttyb ttybold;
3550 struct sgttyb ttybnew;
3551
3552 if (first)
3553 {
3554 first = FALSE;
3555 ioctl(read_cmd_fd, TIOCGETP, &ttybold);
3556 }
3557
3558 ttybnew = ttybold;
3559 if (tmode == TMODE_RAW)
3560 {
3561 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3562 ttybnew.sg_flags |= RAW;
3563 }
3564 else if (tmode == TMODE_SLEEP)
3565 ttybnew.sg_flags &= ~(ECHO);
3566 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3567#endif
3568 curr_tmode = tmode;
3569}
3570
3571/*
3572 * Try to get the code for "t_kb" from the stty setting
3573 *
3574 * Even if termcap claims a backspace key, the user's setting *should*
3575 * prevail. stty knows more about reality than termcap does, and if
3576 * somebody's usual erase key is DEL (which, for most BSD users, it will
3577 * be), they're going to get really annoyed if their erase key starts
3578 * doing forward deletes for no reason. (Eric Fischer)
3579 */
3580 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003581get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003583 ttyinfo_T info;
3584 char_u buf[2];
3585 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003587 if (get_tty_info(read_cmd_fd, &info) == OK)
3588 {
3589 intr_char = info.interrupt;
3590 buf[0] = info.backspace;
3591 buf[1] = NUL;
3592 add_termcode((char_u *)"kb", buf, FALSE);
3593
3594 /* If <BS> and <DEL> are now the same, redefine <DEL>. */
3595 p = find_termcode((char_u *)"kD");
3596 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3597 do_fixdel(NULL);
3598 }
3599}
3600
3601/*
3602 * Obtain the characters that Backspace and Enter produce on "fd".
3603 * Returns OK or FAIL.
3604 */
3605 int
3606get_tty_info(int fd, ttyinfo_T *info)
3607{
3608#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609# ifdef HAVE_TERMIOS_H
3610 struct termios keys;
3611# else
3612 struct termio keys;
3613# endif
3614
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003615 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616# if defined(HAVE_TERMIOS_H)
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003617 tcgetattr(fd, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618# else
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003619 ioctl(fd, TCGETA, &keys) != -1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620# endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003621 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003623 info->backspace = keys.c_cc[VERASE];
3624 info->interrupt = keys.c_cc[VINTR];
3625 if (keys.c_iflag & ICRNL)
3626 info->enter = NL;
3627 else
3628 info->enter = CAR;
3629 if (keys.c_oflag & ONLCR)
3630 info->nl_does_cr = TRUE;
3631 else
3632 info->nl_does_cr = FALSE;
3633 return OK;
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#else
3636 /* for "old" tty systems */
3637 struct sgttyb keys;
3638
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003639 if (ioctl(fd, TIOCGETP, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003641 info->backspace = keys.sg_erase;
3642 info->interrupt = keys.sg_kill;
3643 info->enter = CAR;
3644 info->nl_does_cr = TRUE;
3645 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003648 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649}
3650
3651#endif /* VMS */
3652
3653#if defined(FEAT_MOUSE_TTY) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003654static int mouse_ison = FALSE;
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * Set mouse clicks on or off.
3658 */
3659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003660mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003662# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003663 static int bevalterm_ison = FALSE;
3664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 int xterm_mouse_vers;
3666
Bram Moolenaara06afc72018-08-27 23:24:16 +02003667# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
3668 if (!on)
3669 // Make sure not tracing mouse movements. Important when a button-down
3670 // was received but no release yet.
3671 stop_xterm_trace();
3672# endif
3673
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003674 if (on == mouse_ison
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003675# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003676 && p_bevalterm == bevalterm_ison
3677# endif
3678 )
3679 /* return quickly if nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return;
3681
3682 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003683
3684# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003685 if (ttym_flags == TTYM_URXVT)
3686 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003687 out_str_nf((char_u *)
3688 (on
3689 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3690 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003691 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003692 }
3693# endif
3694
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003695# ifdef FEAT_MOUSE_SGR
3696 if (ttym_flags == TTYM_SGR)
3697 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003698 /* SGR mode supports columns above 223 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003699 out_str_nf((char_u *)
3700 (on
3701 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3702 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003703 mouse_ison = on;
3704 }
3705# endif
3706
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003707# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003708 if (bevalterm_ison != (p_bevalterm && on))
3709 {
3710 bevalterm_ison = (p_bevalterm && on);
3711 if (xterm_mouse_vers > 1 && !bevalterm_ison)
3712 /* disable mouse movement events, enabling is below */
3713 out_str_nf((char_u *)
3714 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003715 }
3716# endif
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (xterm_mouse_vers > 0)
3719 {
3720 if (on) /* enable mouse events, use mouse tracking if available */
3721 out_str_nf((char_u *)
3722 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003723 ? (
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003724# ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003725 bevalterm_ison
3726 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
3727# endif
3728 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
3730 else /* disable mouse events, could probably always send the same */
3731 out_str_nf((char_u *)
3732 (xterm_mouse_vers > 1
3733 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3734 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003735 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
3738# ifdef FEAT_MOUSE_DEC
3739 else if (ttym_flags == TTYM_DEC)
3740 {
3741 if (on) /* enable mouse events */
3742 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
3743 else /* disable mouse events */
3744 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003745 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747# endif
3748
3749# ifdef FEAT_MOUSE_GPM
3750 else
3751 {
3752 if (on)
3753 {
3754 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003755 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
3759 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763# endif
3764
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003765# ifdef FEAT_SYSMOUSE
3766 else
3767 {
3768 if (on)
3769 {
3770 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003771 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003772 }
3773 else
3774 {
3775 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003776 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003777 }
3778 }
3779# endif
3780
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781# ifdef FEAT_MOUSE_JSB
3782 else
3783 {
3784 if (on)
3785 {
3786 /* D - Enable Mouse up/down messages
3787 * L - Enable Left Button Reporting
3788 * M - Enable Middle Button Reporting
3789 * R - Enable Right Button Reporting
3790 * K - Enable SHIFT and CTRL key Reporting
3791 * + - Enable Advanced messaging of mouse moves and up/down messages
3792 * Q - Quiet No Ack
3793 * # - Numeric value of mouse pointer required
3794 * 0 = Multiview 2000 cursor, used as standard
3795 * 1 = Windows Arrow
3796 * 2 = Windows I Beam
3797 * 3 = Windows Hour Glass
3798 * 4 = Windows Cross Hair
3799 * 5 = Windows UP Arrow
3800 */
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003801# ifdef JSBTERM_MOUSE_NONADVANCED
3802 /* Disables full feedback of pointer movements */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3804 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003805# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3807 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaarf8de1612013-04-15 15:32:25 +02003808# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003809 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811 else
3812 {
3813 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3814 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003815 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
3818# endif
3819# ifdef FEAT_MOUSE_PTERM
3820 else
3821 {
3822 /* 1 = button press, 6 = release, 7 = drag, 1h...9l = right button */
3823 if (on)
3824 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3825 else
3826 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003827 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829# endif
3830}
3831
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003832#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003833/*
3834 * Called when 'balloonevalterm' changed.
3835 */
3836 void
3837mch_bevalterm_changed(void)
3838{
3839 mch_setmouse(mouse_ison);
3840}
3841#endif
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843/*
3844 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3845 */
3846 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003847check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849# ifdef FEAT_MOUSE_XTERM
3850 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003851# ifdef FEAT_MOUSE_URXVT
3852 && use_xterm_mouse() != 3
3853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854# ifdef FEAT_GUI
3855 && !gui.in_use
3856# endif
3857 )
3858 {
3859 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003860 ? IF_EB("\233M", CSI_STR "M")
3861 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (*p_mouse != NUL)
3863 {
3864 /* force mouse off and maybe on to send possibly new mouse
3865 * activation sequence to the xterm, with(out) drag tracing. */
3866 mch_setmouse(FALSE);
3867 setmouse();
3868 }
3869 }
3870 else
3871 del_mouse_termcode(KS_MOUSE);
3872# endif
3873
3874# ifdef FEAT_MOUSE_GPM
3875 if (!use_xterm_mouse()
3876# ifdef FEAT_GUI
3877 && !gui.in_use
3878# endif
3879 )
3880 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3881# endif
3882
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003883# ifdef FEAT_SYSMOUSE
3884 if (!use_xterm_mouse()
3885# ifdef FEAT_GUI
3886 && !gui.in_use
3887# endif
3888 )
3889 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3890# endif
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892# ifdef FEAT_MOUSE_JSB
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003893 /* Conflicts with xterm mouse: "\033[" and "\033[M" ??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (!use_xterm_mouse()
3895# ifdef FEAT_GUI
3896 && !gui.in_use
3897# endif
3898 )
3899 set_mouse_termcode(KS_JSBTERM_MOUSE,
3900 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3901 else
3902 del_mouse_termcode(KS_JSBTERM_MOUSE);
3903# endif
3904
3905# ifdef FEAT_MOUSE_NET
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003906 /* There is no conflict, but one may type "ESC }" from Insert mode. Don't
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 * define it in the GUI or when using an xterm. */
3908 if (!use_xterm_mouse()
3909# ifdef FEAT_GUI
3910 && !gui.in_use
3911# endif
3912 )
3913 set_mouse_termcode(KS_NETTERM_MOUSE,
3914 (char_u *)IF_EB("\033}", ESC_STR "}"));
3915 else
3916 del_mouse_termcode(KS_NETTERM_MOUSE);
3917# endif
3918
3919# ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003920 /* Conflicts with xterm mouse: "\033[" and "\033[M" */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003921 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922# ifdef FEAT_GUI
3923 && !gui.in_use
3924# endif
3925 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003926 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3927 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 else
3929 del_mouse_termcode(KS_DEC_MOUSE);
3930# endif
3931# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003932 /* same conflict as the dec mouse */
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003933 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934# ifdef FEAT_GUI
3935 && !gui.in_use
3936# endif
3937 )
3938 set_mouse_termcode(KS_PTERM_MOUSE,
3939 (char_u *) IF_EB("\033[", ESC_STR "["));
3940 else
3941 del_mouse_termcode(KS_PTERM_MOUSE);
3942# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003943# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003944 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003945# ifdef FEAT_GUI
3946 && !gui.in_use
3947# endif
3948 )
3949 {
3950 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003951 ? IF_EB("\233*M", CSI_STR "*M")
3952 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003953
3954 if (*p_mouse != NUL)
3955 {
3956 mch_setmouse(FALSE);
3957 setmouse();
3958 }
3959 }
3960 else
3961 del_mouse_termcode(KS_URXVT_MOUSE);
3962# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003963# ifdef FEAT_MOUSE_SGR
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003964 if (use_xterm_mouse() == 4
3965# ifdef FEAT_GUI
3966 && !gui.in_use
3967# endif
3968 )
3969 {
3970 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003971 ? IF_EB("\233<*M", CSI_STR "<*M")
3972 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3973
3974 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3975 ? IF_EB("\233<*m", CSI_STR "<*m")
3976 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003977
3978 if (*p_mouse != NUL)
3979 {
3980 mch_setmouse(FALSE);
3981 setmouse();
3982 }
3983 }
3984 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003985 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003986 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003987 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3988 }
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
3991#endif
3992
3993/*
3994 * set screen mode, always fails.
3995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003997mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 EMSG(_(e_screenmode));
4000 return FAIL;
4001}
4002
4003#ifndef VMS
4004
4005/*
4006 * Try to get the current window size:
4007 * 1. with an ioctl(), most accurate method
4008 * 2. from the environment variables LINES and COLUMNS
4009 * 3. from the termcap
4010 * 4. keep using the old values
4011 * Return OK when size could be determined, FAIL otherwise.
4012 */
4013 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004014mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 long rows = 0;
4017 long columns = 0;
4018 char_u *p;
4019
4020 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * 1. try using an ioctl. It is the most accurate method.
4022 *
4023 * Try using TIOCGWINSZ first, some systems that have it also define
4024 * TIOCGSIZE but don't have a struct ttysize.
4025 */
4026# ifdef TIOCGWINSZ
4027 {
4028 struct winsize ws;
4029 int fd = 1;
4030
4031 /* When stdout is not a tty, use stdin for the ioctl(). */
4032 if (!isatty(fd) && isatty(read_cmd_fd))
4033 fd = read_cmd_fd;
4034 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4035 {
4036 columns = ws.ws_col;
4037 rows = ws.ws_row;
4038 }
4039 }
4040# else /* TIOCGWINSZ */
4041# ifdef TIOCGSIZE
4042 {
4043 struct ttysize ts;
4044 int fd = 1;
4045
4046 /* When stdout is not a tty, use stdin for the ioctl(). */
4047 if (!isatty(fd) && isatty(read_cmd_fd))
4048 fd = read_cmd_fd;
4049 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4050 {
4051 columns = ts.ts_cols;
4052 rows = ts.ts_lines;
4053 }
4054 }
4055# endif /* TIOCGSIZE */
4056# endif /* TIOCGWINSZ */
4057
4058 /*
4059 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004060 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4061 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004063 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
4065 if ((p = (char_u *)getenv("LINES")))
4066 rows = atoi((char *)p);
4067 if ((p = (char_u *)getenv("COLUMNS")))
4068 columns = atoi((char *)p);
4069 }
4070
4071#ifdef HAVE_TGETENT
4072 /*
4073 * 3. try reading "co" and "li" entries from termcap
4074 */
4075 if (columns == 0 || rows == 0)
4076 getlinecol(&columns, &rows);
4077#endif
4078
4079 /*
4080 * 4. If everything fails, use the old values
4081 */
4082 if (columns <= 0 || rows <= 0)
4083 return FAIL;
4084
4085 Rows = rows;
4086 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004087 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return OK;
4089}
4090
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004091#if defined(FEAT_TERMINAL) || defined(PROTO)
4092/*
4093 * Report the windows size "rows" and "cols" to tty "fd".
4094 */
4095 int
4096mch_report_winsize(int fd, int rows, int cols)
4097{
4098# ifdef TIOCSWINSZ
4099 struct winsize ws;
4100
4101 ws.ws_col = cols;
4102 ws.ws_row = rows;
4103 ws.ws_xpixel = cols * 5;
4104 ws.ws_ypixel = rows * 10;
4105 if (ioctl(fd, TIOCSWINSZ, &ws) == 0)
4106 {
4107 ch_log(NULL, "ioctl(TIOCSWINSZ) success");
4108 return OK;
4109 }
4110 ch_log(NULL, "ioctl(TIOCSWINSZ) failed");
4111# else
4112# ifdef TIOCSSIZE
4113 struct ttysize ts;
4114
4115 ts.ts_cols = cols;
4116 ts.ts_lines = rows;
4117 if (ioctl(fd, TIOCSSIZE, &ws) == 0)
4118 {
4119 ch_log(NULL, "ioctl(TIOCSSIZE) success");
4120 return OK;
4121 }
4122 ch_log(NULL, "ioctl(TIOCSSIZE) failed");
4123# endif
4124# endif
4125 return FAIL;
4126}
4127#endif
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129/*
4130 * Try to set the window size to Rows and Columns.
4131 */
4132 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004133mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 if (*T_CWS)
4136 {
4137 /*
4138 * NOTE: if you get an error here that term_set_winsize() is
4139 * undefined, check the output of configure. It could probably not
4140 * find a ncurses, termcap or termlib library.
4141 */
4142 term_set_winsize((int)Rows, (int)Columns);
4143 out_flush();
4144 screen_start(); /* don't know where cursor is now */
4145 }
4146}
4147
4148#endif /* VMS */
4149
4150/*
4151 * Rows and/or Columns has changed.
4152 */
4153 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004154mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 /* Nothing to do. */
4157}
4158
Bram Moolenaar205b8862011-09-07 15:04:31 +02004159/*
4160 * Wait for process "child" to end.
4161 * Return "child" if it exited properly, <= 0 on error.
4162 */
4163 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004164wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004165{
4166 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004167 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004168
4169 while (wait_pid != child)
4170 {
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004171 /* When compiled with Python threads are probably used, in which case
4172 * wait() sometimes hangs for no obvious reason. Use waitpid()
4173 * instead and loop (like the GUI). Also needed for other interfaces,
4174 * they might call system(). */
4175# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004176 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004177# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004178 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004179# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004180 if (wait_pid == 0)
4181 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004182 /* Wait for 1 to 10 msec before trying again. */
4183 mch_delay(delay_msec, TRUE);
4184 if (++delay_msec > 10)
4185 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004186 continue;
4187 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004188 if (wait_pid <= 0
4189# ifdef ECHILD
4190 && errno == ECHILD
4191# endif
4192 )
4193 break;
4194 }
4195 return wait_pid;
4196}
4197
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004198#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004199/*
4200 * Set the environment for a child process.
4201 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004203set_child_environment(
4204 long rows,
4205 long columns,
4206 char *term,
4207 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004208{
4209# ifdef HAVE_SETENV
4210 char envbuf[50];
4211# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004212 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004213 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004214 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004215 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004216 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004217# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004218 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004219# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004220# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004221 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004222# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004223# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004224 long colors =
4225# ifdef FEAT_GUI
4226 gui.in_use ? 256*256*256 :
4227# endif
4228 t_colors;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004229
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004230# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004231 setenv("TERM", term, 1);
4232 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004234 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004235 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004236 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004237 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004238 sprintf((char *)envbuf, "%ld", colors);
4239 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004240# ifdef FEAT_TERMINAL
4241 if (is_terminal)
4242 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004243 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004244 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4245 }
4246# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004247# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004248 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004249# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004250# else
4251 /*
4252 * Putenv does not copy the string, it has to remain valid.
4253 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004254 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004255 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004256 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4257 putenv(envbuf_Term);
4258 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004259 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004260 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4261 putenv(envbuf_Lines);
4262 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4263 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004264 putenv(envbuf_Columns);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004265 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", colors);
4266 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004267# ifdef FEAT_TERMINAL
4268 if (is_terminal)
4269 {
4270 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004271 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004272 putenv(envbuf_Version);
4273 }
4274# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004275# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004276 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4277 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4278 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004279# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004280# endif
4281}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004282
4283 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004284set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004285{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004286 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004287}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004288#endif
4289
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004290#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004291/*
4292 * Open a PTY, with FD for the master and slave side.
4293 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
4294 * When successful both file descriptors are stored.
4295 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004296 static void
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004297open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **namep)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004298{
4299 char *tty_name;
4300
4301 *pty_master_fd = OpenPTY(&tty_name); /* open pty */
4302 if (*pty_master_fd >= 0)
4303 {
4304 /* Leaving out O_NOCTTY may lead to waitpid() always returning
4305 * 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4306 * adding O_NOCTTY always works when defined. */
4307#ifdef O_NOCTTY
4308 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4309#else
4310 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4311#endif
4312 if (*pty_slave_fd < 0)
4313 {
4314 close(*pty_master_fd);
4315 *pty_master_fd = -1;
4316 }
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004317 else if (namep != NULL)
4318 *namep = vim_strsave((char_u *)tty_name);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004319 }
4320}
4321#endif
4322
Bram Moolenaarfae42832017-08-01 22:24:26 +02004323/*
4324 * Send SIGINT to a child process if "c" is an interrupt character.
4325 */
4326 void
4327may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4328{
4329# ifdef SIGINT
4330 if (c == Ctrl_C || c == intr_char)
4331 {
4332# ifdef HAVE_SETSID
4333 kill(-pid, SIGINT);
4334# else
4335 kill(0, SIGINT);
4336# endif
4337 if (wpid > 0)
4338 kill(wpid, SIGINT);
4339 }
4340# endif
4341}
4342
Bram Moolenaar13568252018-03-16 20:46:58 +01004343#if !defined(USE_SYSTEM) || (defined(FEAT_GUI) && defined(FEAT_TERMINAL))
4344
4345 static int
4346build_argv(
4347 char_u *cmd,
4348 char ***argvp,
4349 char_u **sh_tofree,
4350 char_u **shcf_tofree)
4351{
4352 char **argv = NULL;
4353 int argc;
4354
4355 *sh_tofree = vim_strsave(p_sh);
4356 if (*sh_tofree == NULL) /* out of memory */
4357 return FAIL;
4358
4359 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4360 return FAIL;
4361 *argvp = argv;
4362
4363 if (cmd != NULL)
4364 {
4365 char_u *s;
4366 char_u *p;
4367
4368 if (extra_shell_arg != NULL)
4369 argv[argc++] = (char *)extra_shell_arg;
4370
4371 /* Break 'shellcmdflag' into white separated parts. This doesn't
4372 * handle quoted strings, they are very unlikely to appear. */
4373 *shcf_tofree = alloc((unsigned)STRLEN(p_shcf) + 1);
4374 if (*shcf_tofree == NULL) /* out of memory */
4375 return FAIL;
4376 s = *shcf_tofree;
4377 p = p_shcf;
4378 while (*p != NUL)
4379 {
4380 argv[argc++] = (char *)s;
4381 while (*p && *p != ' ' && *p != TAB)
4382 *s++ = *p++;
4383 *s++ = NUL;
4384 p = skipwhite(p);
4385 }
4386
4387 argv[argc++] = (char *)cmd;
4388 }
4389 argv[argc] = NULL;
4390 return OK;
4391}
4392#endif
4393
4394#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4395/*
4396 * Use a terminal window to run a shell command in.
4397 */
4398 static int
4399mch_call_shell_terminal(
4400 char_u *cmd,
4401 int options UNUSED) /* SHELL_*, see vim.h */
4402{
4403 jobopt_T opt;
4404 char **argv = NULL;
4405 char_u *tofree1 = NULL;
4406 char_u *tofree2 = NULL;
4407 int retval = -1;
4408 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004409 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004410 aco_save_T aco;
4411 oparg_T oa; /* operator arguments */
4412
4413 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
4414 goto theend;
4415
4416 init_job_options(&opt);
4417 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4418 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004419 if (buf == NULL)
4420 goto theend;
4421
4422 job = term_getjob(buf->b_term);
4423 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004424
4425 /* Find a window to make "buf" curbuf. */
4426 aucmd_prepbuf(&aco, buf);
4427
4428 clear_oparg(&oa);
4429 while (term_use_loop())
4430 {
4431 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4432 {
4433 /* If terminal_loop() returns OK we got a key that is handled
4434 * in Normal model. We don't do redrawing anyway. */
4435 if (terminal_loop(TRUE) == OK)
4436 normal_cmd(&oa, TRUE);
4437 }
4438 else
4439 normal_cmd(&oa, TRUE);
4440 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004441 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004442 ch_log(NULL, "system command finished");
4443
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004444 job_unref(job);
4445
Bram Moolenaar13568252018-03-16 20:46:58 +01004446 /* restore curwin/curbuf and a few other things */
4447 aucmd_restbuf(&aco);
4448
4449 wait_return(TRUE);
4450 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4451
4452theend:
4453 vim_free(argv);
4454 vim_free(tofree1);
4455 vim_free(tofree2);
4456 return retval;
4457}
4458#endif
4459
4460#ifdef USE_SYSTEM
4461/*
4462 * Use system() to start the shell: simple but slow.
4463 */
4464 static int
4465mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004466 char_u *cmd,
4467 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469#ifdef VMS
4470 char *ifn = NULL;
4471 char *ofn = NULL;
4472#endif
4473 int tmode = cur_tmode;
Bram Moolenaarb2b050a2016-07-16 21:52:46 +02004474 char_u *newcmd; /* only needed for unix */
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004475 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 out_flush();
4478
4479 if (options & SHELL_COOKED)
4480 settmode(TMODE_COOK); /* set to normal mode */
4481
Bram Moolenaar62b42182010-09-21 22:09:37 +02004482# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004483 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004484 loose_clipboard();
4485# endif
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (cmd == NULL)
4488 x = system((char *)p_sh);
4489 else
4490 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004491# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (ofn = strchr((char *)cmd, '>'))
4493 *ofn++ = '\0';
4494 if (ifn = strchr((char *)cmd, '<'))
4495 {
4496 char *p;
4497
4498 *ifn++ = '\0';
4499 p = strchr(ifn,' '); /* chop off any trailing spaces */
4500 if (p)
4501 *p = '\0';
4502 }
4503 if (ofn)
4504 x = vms_sys((char *)cmd, ofn, ifn);
4505 else
4506 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004507# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 newcmd = lalloc(STRLEN(p_sh)
4509 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
4510 + STRLEN(p_shcf) + STRLEN(cmd) + 4, TRUE);
4511 if (newcmd == NULL)
4512 x = 0;
4513 else
4514 {
4515 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4516 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4517 (char *)p_shcf,
4518 (char *)cmd);
4519 x = system((char *)newcmd);
4520 vim_free(newcmd);
4521 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524# ifdef VMS
4525 x = vms_sys_status(x);
4526# endif
4527 if (emsg_silent)
4528 ;
4529 else if (x == 127)
4530 MSG_PUTS(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 else if (x && !(options & SHELL_SILENT))
4532 {
4533 MSG_PUTS(_("\nshell returned "));
4534 msg_outnum((long)x);
4535 msg_putchar('\n');
4536 }
4537
4538 if (tmode == TMODE_RAW)
4539 settmode(TMODE_RAW); /* set to raw mode */
4540# ifdef FEAT_TITLE
4541 resettitle();
4542# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004543# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4544 restore_clipboard();
4545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
Bram Moolenaar13568252018-03-16 20:46:58 +01004549#else /* USE_SYSTEM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaardf177f62005-02-22 08:39:57 +00004551# define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
4552 127, some shells use that already */
Bram Moolenaarb109bb42017-08-21 21:07:29 +02004553# define OPEN_NULL_FAILED 123 /* Exit code if /dev/null can't be opened */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar13568252018-03-16 20:46:58 +01004555/*
4556 * Don't use system(), use fork()/exec().
4557 */
4558 static int
4559mch_call_shell_fork(
4560 char_u *cmd,
4561 int options) /* SHELL_*, see vim.h */
4562{
4563 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 pid_t wait_pid = 0;
4567# ifdef HAVE_UNION_WAIT
4568 union wait status;
4569# else
4570 int status = -1;
4571# endif
4572 int retval = -1;
4573 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004574 char_u *tofree1 = NULL;
4575 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 int pty_master_fd = -1; /* for pty's */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004578# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004580# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004581 int fd_toshell[2]; /* for pipes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 int fd_fromshell[2];
4583 int pipe_error = FALSE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004584 int did_settmode = FALSE; /* settmode(TMODE_RAW) called */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 out_flush();
4587 if (options & SHELL_COOKED)
4588 settmode(TMODE_COOK); /* set to normal mode */
4589
Bram Moolenaar13568252018-03-16 20:46:58 +01004590 if (build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004591 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594 * For the GUI, when writing the output into the buffer and when reading
4595 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4596 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 if ((options & (SHELL_READ|SHELL_WRITE))
4599# ifdef FEAT_GUI
4600 || (gui.in_use && show_shell_mess)
4601# endif
4602 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004604# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 /*
4606 * Try to open a master pty.
4607 * If this works, open the slave pty.
4608 * If the slave can't be opened, close the master pty.
4609 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004610 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004611 open_pty(&pty_master_fd, &pty_slave_fd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 /*
4613 * If not opening a pty or it didn't work, try using pipes.
4614 */
4615 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
4618 pipe_error = (pipe(fd_toshell) < 0);
4619 if (!pipe_error) /* pipe create OK */
4620 {
4621 pipe_error = (pipe(fd_fromshell) < 0);
4622 if (pipe_error) /* pipe create failed */
4623 {
4624 close(fd_toshell[0]);
4625 close(fd_toshell[1]);
4626 }
4627 }
4628 if (pipe_error)
4629 {
4630 MSG_PUTS(_("\nCannot create pipes\n"));
4631 out_flush();
4632 }
4633 }
4634 }
4635
4636 if (!pipe_error) /* pty or pipe opened or not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004638 SIGSET_DECL(curset)
4639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640# ifdef __BEOS__
4641 beos_cleanup_read_thread();
4642# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004643
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004644 BLOCK_SIGNALS(&curset);
4645 pid = fork(); /* maybe we should use vfork() */
4646 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004648 UNBLOCK_SIGNALS(&curset);
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 MSG_PUTS(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004651 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004653 || (gui.in_use && show_shell_mess)
4654# endif
4655 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004657# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (pty_master_fd >= 0) /* close the pseudo tty */
4659 {
4660 close(pty_master_fd);
4661 close(pty_slave_fd);
4662 }
4663 else /* close the pipes */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
4666 close(fd_toshell[0]);
4667 close(fd_toshell[1]);
4668 close(fd_fromshell[0]);
4669 close(fd_fromshell[1]);
4670 }
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 else if (pid == 0) /* child */
4674 {
4675 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004676 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
Bram Moolenaar819524702018-02-27 19:10:00 +01004678# ifdef FEAT_JOB_CHANNEL
4679 if (ch_log_active())
4680 /* close the log file in the child */
4681 ch_logfile((char_u *)"", (char_u *)"");
4682# endif
4683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (!show_shell_mess || (options & SHELL_EXPAND))
4685 {
4686 int fd;
4687
4688 /*
4689 * Don't want to show any message from the shell. Can't just
4690 * close stdout and stderr though, because some systems will
4691 * break if you try to write to them after that, so we must
4692 * use dup() to replace them with something else -- webb
4693 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4694 * waiting for input.
4695 */
4696 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4697 fclose(stdin);
4698 fclose(stdout);
4699 fclose(stderr);
4700
4701 /*
4702 * If any of these open()'s and dup()'s fail, we just continue
4703 * anyway. It's not fatal, and on most systems it will make
4704 * no difference at all. On a few it will cause the execvp()
4705 * to exit with a non-zero status even when the completion
4706 * could be done, which is nothing too serious. If the open()
4707 * or dup() failed we'd just do the same thing ourselves
4708 * anyway -- webb
4709 */
4710 if (fd >= 0)
4711 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004712 ignored = dup(fd); /* To replace stdin (fd 0) */
4713 ignored = dup(fd); /* To replace stdout (fd 1) */
4714 ignored = dup(fd); /* To replace stderr (fd 2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 /* Don't need this now that we've duplicated it */
4717 close(fd);
4718 }
4719 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004722 || gui.in_use
4723# endif
4724 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# ifdef HAVE_SETSID
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004728 /* Create our own process group, so that the child and all its
4729 * children can be kill()ed. Don't do this when using pipes,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004730 * because stdin is not a tty, we would lose /dev/tty. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004731 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004732 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004733 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004734# if defined(SIGHUP)
4735 /* When doing "!xterm&" and 'shell' is bash: the shell
4736 * will exit and send SIGHUP to all processes in its
4737 * group, killing the just started process. Ignore SIGHUP
4738 * to avoid that. (suggested by Simon Schubert)
4739 */
4740 signal(SIGHUP, SIG_IGN);
4741# endif
4742 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004743# endif
4744# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004745 if (pty_slave_fd >= 0)
4746 {
4747 /* push stream discipline modules */
4748 if (options & SHELL_COOKED)
4749 SetupSlavePTY(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750# ifdef TIOCSCTTY
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004751 /* Try to become controlling tty (probably doesn't work,
4752 * unless run by root) */
4753 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004755 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004756# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004757 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaara5792f52005-11-23 21:25:05 +00004759 /*
4760 * stderr is only redirected when using the GUI, so that a
4761 * program like gpg can still access the terminal to get a
4762 * passphrase using stderr.
4763 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (pty_master_fd >= 0)
4766 {
4767 close(pty_master_fd); /* close master side of pty */
4768
4769 /* set up stdin/stdout/stderr for the child */
4770 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004771 ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004773 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004774 if (gui.in_use)
4775 {
4776 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004777 ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 close(pty_slave_fd); /* has been dupped, close it now */
4781 }
4782 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 /* set up stdin for the child */
4786 close(fd_toshell[1]);
4787 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004788 ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 close(fd_toshell[0]);
4790
4791 /* set up stdout for the child */
4792 close(fd_fromshell[0]);
4793 close(1);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004794 ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 close(fd_fromshell[1]);
4796
Bram Moolenaara5792f52005-11-23 21:25:05 +00004797# ifdef FEAT_GUI
4798 if (gui.in_use)
4799 {
4800 /* set up stderr for the child */
4801 close(2);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004802 ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004803 }
4804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
4806 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /*
4809 * There is no type cast for the argv, because the type may be
4810 * different on different machines. This may cause a warning
4811 * message with strict compilers, don't worry about it.
4812 * Call _exit() instead of exit() to avoid closing the connection
4813 * to the X server (esp. with GTK, which uses atexit()).
4814 */
4815 execvp(argv[0], argv);
4816 _exit(EXEC_FAILED); /* exec failed, return failure code */
4817 }
4818 else /* parent */
4819 {
4820 /*
4821 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004822 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
4824 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004825 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004826 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004827# ifdef FEAT_JOB_CHANNEL
4828 ++dont_check_job_ended;
4829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 /*
4831 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832 * This is also used to pipe stdin/stdout to/from the external
4833 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004835 if ((options & (SHELL_READ|SHELL_WRITE))
4836# ifdef FEAT_GUI
4837 || (gui.in_use && show_shell_mess)
4838# endif
4839 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004841# define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 char_u buffer[BUFLEN + 1];
Bram Moolenaardf177f62005-02-22 08:39:57 +00004843# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 int buffer_off = 0; /* valid bytes in buffer[] */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 char_u ta_buf[BUFLEN + 1]; /* TypeAHead */
4847 int ta_len = 0; /* valid bytes in ta_buf[] */
4848 int len;
4849 int p_more_save;
4850 int old_State;
4851 int c;
4852 int toshell_fd;
4853 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004854 garray_T ga;
4855 int noread_cnt;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004856# if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
4857 struct timeval start_tv;
4858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaardf177f62005-02-22 08:39:57 +00004860# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (pty_master_fd >= 0)
4862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 fromshell_fd = pty_master_fd;
4864 toshell_fd = dup(pty_master_fd);
4865 }
4866 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 close(fd_toshell[0]);
4870 close(fd_fromshell[1]);
4871 toshell_fd = fd_toshell[1];
4872 fromshell_fd = fd_fromshell[0];
4873 }
4874
4875 /*
4876 * Write to the child if there are typed characters.
4877 * Read from the child if there are characters available.
4878 * Repeat the reading a few times if more characters are
4879 * available. Need to check for typed keys now and then, but
4880 * not too often (delays when no chars are available).
4881 * This loop is quit if no characters can be read from the pty
4882 * (WaitForChar detected special condition), or there are no
4883 * characters available and the child has exited.
4884 * Only check if the child has exited when there is no more
4885 * output. The child may exit before all the output has
4886 * been printed.
4887 *
4888 * Currently this busy loops!
4889 * This can probably dead-lock when the write blocks!
4890 */
4891 p_more_save = p_more;
4892 p_more = FALSE;
4893 old_State = State;
4894 State = EXTERNCMD; /* don't redraw at window resize */
4895
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004896 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897 {
4898 /* Fork a process that will write the lines to the
4899 * external program. */
4900 if ((wpid = fork()) == -1)
4901 {
4902 MSG_PUTS(_("\nCannot fork\n"));
4903 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004904 else if (wpid == 0) /* child */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004905 {
4906 linenr_T lnum = curbuf->b_op_start.lnum;
4907 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004908 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 size_t l;
4910
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004911 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004912 for (;;)
4913 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004914 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004915 if (l == 0)
4916 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004917 else if (lp[written] == NL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 /* NL -> NUL translation */
4919 len = write(toshell_fd, "", (size_t)1);
4920 else
4921 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004922 char_u *s = vim_strchr(lp + written, NL);
4923
Bram Moolenaar89d40322006-08-29 15:30:07 +00004924 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004925 s == NULL ? l
4926 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004928 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004929 {
4930 /* Finished a line, add a NL, unless this line
4931 * should not have one. */
4932 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004933 || (!curbuf->b_p_bin
4934 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004935 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004936 && (lnum !=
4937 curbuf->b_ml.ml_line_count
4938 || curbuf->b_p_eol)))
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004939 ignored = write(toshell_fd, "\n",
4940 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004941 ++lnum;
4942 if (lnum > curbuf->b_op_end.lnum)
4943 {
4944 /* finished all the lines, close pipe */
4945 close(toshell_fd);
4946 toshell_fd = -1;
4947 break;
4948 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004949 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004950 written = 0;
4951 }
4952 else if (len > 0)
4953 written += len;
4954 }
4955 _exit(0);
4956 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004957 else /* parent */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004958 {
4959 close(toshell_fd);
4960 toshell_fd = -1;
4961 }
4962 }
4963
4964 if (options & SHELL_READ)
4965 ga_init2(&ga, 1, BUFLEN);
4966
4967 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004968# ifdef ELAPSED_FUNC
4969 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 for (;;)
4972 {
4973 /*
4974 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004975 * if there are any.
4976 * Don't do this if we are expanding wild cards (would eat
4977 * typeahead).
4978 * Don't do this when filtering and terminal is in cooked
4979 * mode, the shell command will handle the I/O. Avoids
4980 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004981 * Don't get characters when the child has already
4982 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004983 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004984 * while (noread_cnt > 4), avoids that ":r !ls" eats
4985 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
4987 len = 0;
4988 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004989 && ((options &
4990 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4991 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004992# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004993 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004994# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004995 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004996 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004997 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004999 if (ta_len == 0)
5000 {
5001 /* Get extra characters when we don't have any.
5002 * Reset the counter and timer. */
5003 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005004# ifdef ELAPSED_FUNC
5005 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005006# endif
5007 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
5008 }
5009 if (ta_len > 0 || len > 0)
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /*
5012 * For pipes:
5013 * Check for CTRL-C: send interrupt signal to child.
5014 * Check for CTRL-D: EOF, close pipe to child.
5015 */
5016 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /*
5019 * Send SIGINT to the child's group or all
5020 * processes in our group.
5021 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005022 may_send_sigint(ta_buf[ta_len], pid, wpid);
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (pty_master_fd < 0 && toshell_fd >= 0
5025 && ta_buf[ta_len] == Ctrl_D)
5026 {
5027 close(toshell_fd);
5028 toshell_fd = -1;
5029 }
5030 }
5031
5032 /* replace K_BS by <BS> and K_DEL by <DEL> */
5033 for (i = ta_len; i < ta_len + len; ++i)
5034 {
5035 if (ta_buf[i] == CSI && len - i > 2)
5036 {
5037 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
5038 if (c == K_DEL || c == K_KDEL || c == K_BS)
5039 {
5040 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
5041 (size_t)(len - i - 2));
5042 if (c == K_DEL || c == K_KDEL)
5043 ta_buf[i] = DEL;
5044 else
5045 ta_buf[i] = Ctrl_H;
5046 len -= 2;
5047 }
5048 }
5049 else if (ta_buf[i] == '\r')
5050 ta_buf[i] = '\n';
Bram Moolenaardf177f62005-02-22 08:39:57 +00005051# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (has_mbyte)
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00005053 i += (*mb_ptr2len_len)(ta_buf + i,
5054 ta_len + len - i) - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057
5058 /*
5059 * For pipes: echo the typed characters.
5060 * For a pty this does not seem to work.
5061 */
5062 if (pty_master_fd < 0)
5063 {
5064 for (i = ta_len; i < ta_len + len; ++i)
5065 {
5066 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5067 msg_putchar(ta_buf[i]);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005068# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 else if (has_mbyte)
5070 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005071 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 msg_outtrans_len(ta_buf + i, l);
5074 i += l - 1;
5075 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 else
5078 msg_outtrans_len(ta_buf + i, 1);
5079 }
5080 windgoto(msg_row, msg_col);
5081 out_flush();
5082 }
5083
5084 ta_len += len;
5085
5086 /*
5087 * Write the characters to the child, unless EOF has
5088 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005089 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005090 * When writing buffer lines, drop the typed
5091 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005093 if (options & SHELL_WRITE)
5094 ta_len = 0;
5095 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5098 if (len > 0)
5099 {
5100 ta_len -= len;
5101 mch_memmove(ta_buf, ta_buf + len, ta_len);
5102 }
5103 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106
Bram Moolenaardf177f62005-02-22 08:39:57 +00005107 if (got_int)
5108 {
5109 /* CTRL-C sends a signal to the child, we ignore it
5110 * ourselves */
5111# ifdef HAVE_SETSID
5112 kill(-pid, SIGINT);
5113# else
5114 kill(0, SIGINT);
5115# endif
5116 if (wpid > 0)
5117 kill(wpid, SIGINT);
5118 got_int = FALSE;
5119 }
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 /*
5122 * Check if the child has any characters to be printed.
5123 * Read them and write them to our window. Repeat this as
5124 * long as there is something to do, avoid the 10ms wait
5125 * for mch_inchar(), or sending typeahead characters to
5126 * the external process.
5127 * TODO: This should handle escape sequences, compatible
5128 * to some terminal (vt52?).
5129 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005130 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005131 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005133 len = read_eintr(fromshell_fd, buffer
Bram Moolenaardf177f62005-02-22 08:39:57 +00005134# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaardf177f62005-02-22 08:39:57 +00005136# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 , (size_t)BUFLEN
Bram Moolenaardf177f62005-02-22 08:39:57 +00005138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 );
5140 if (len <= 0) /* end of file or error */
5141 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005142
5143 noread_cnt = 0;
5144 if (options & SHELL_READ)
5145 {
5146 /* Do NUL -> NL translation, append NL separated
5147 * lines to the current buffer. */
5148 for (i = 0; i < len; ++i)
5149 {
5150 if (buffer[i] == NL)
5151 append_ga_line(&ga);
5152 else if (buffer[i] == NUL)
5153 ga_append(&ga, NL);
5154 else
5155 ga_append(&ga, buffer[i]);
5156 }
5157 }
5158# ifdef FEAT_MBYTE
5159 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005162 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
Bram Moolenaardf177f62005-02-22 08:39:57 +00005164 len += buffer_off;
5165 buffer[len] = NUL;
5166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 /* Check if the last character in buffer[] is
5168 * incomplete, keep these bytes for the next
5169 * round. */
5170 for (p = buffer; p < buffer + len; p += l)
5171 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005172 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (l == 0)
5174 l = 1; /* NUL byte? */
5175 else if (MB_BYTE2LEN(*p) != l)
5176 break;
5177 }
5178 if (p == buffer) /* no complete character */
5179 {
5180 /* avoid getting stuck at an illegal byte */
5181 if (len >= 12)
5182 ++p;
5183 else
5184 {
5185 buffer_off = len;
5186 continue;
5187 }
5188 }
5189 c = *p;
5190 *p = NUL;
5191 msg_puts(buffer);
5192 if (p < buffer + len)
5193 {
5194 *p = c;
5195 buffer_off = (buffer + len) - p;
5196 mch_memmove(buffer, p, buffer_off);
5197 continue;
5198 }
5199 buffer_off = 0;
5200 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005201# endif /* FEAT_MBYTE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 buffer[len] = NUL;
5205 msg_puts(buffer);
5206 }
5207
5208 windgoto(msg_row, msg_col);
5209 cursor_on();
5210 out_flush();
5211 if (got_int)
5212 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005213
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005214# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005215 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005216 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005217 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005218
5219 /* Avoid that we keep looping here without
5220 * checking for a CTRL-C for a long time. Don't
5221 * break out too often to avoid losing typeahead. */
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005222 if (msec > 2000)
5223 {
5224 noread_cnt = 5;
5225 break;
5226 }
5227 }
5228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005231 /* If we already detected the child has finished, continue
5232 * reading output for a short while. Some text may be
5233 * buffered. */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005234 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005235 {
5236 if (noread_cnt < 5)
5237 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005238 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005239 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 /*
5242 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005243 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005245# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005246 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005247# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5251 || (wait_pid == pid && WIFEXITED(status)))
5252 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005253 /* Don't break the loop yet, try reading more
5254 * characters from "fromshell_fd" first. When using
5255 * pipes there might still be something to read and
5256 * then we'll break the loop at the "break" above. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005259 else
5260 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005261
Bram Moolenaar95a51352013-03-21 22:53:50 +01005262# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005263 /* Handle any X events, e.g. serving the clipboard. */
5264 clip_update();
5265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267finished:
5268 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005269 if (options & SHELL_READ)
5270 {
5271 if (ga.ga_len > 0)
5272 {
5273 append_ga_line(&ga);
5274 /* remember that the NL was missing */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005275 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005276 }
5277 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005278 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005279 ga_clear(&ga);
5280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 /*
5283 * Give all typeahead that wasn't used back to ui_inchar().
5284 */
5285 if (ta_len)
5286 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 State = old_State;
5288 if (toshell_fd >= 0)
5289 close(toshell_fd);
5290 close(fromshell_fd);
5291 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005292# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005293 else
5294 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005295 long delay_msec = 1;
5296
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005297 /*
5298 * Similar to the loop above, but only handle X events, no
5299 * I/O.
5300 */
5301 for (;;)
5302 {
5303 if (got_int)
5304 {
5305 /* CTRL-C sends a signal to the child, we ignore it
5306 * ourselves */
5307# ifdef HAVE_SETSID
5308 kill(-pid, SIGINT);
5309# else
5310 kill(0, SIGINT);
5311# endif
5312 got_int = FALSE;
5313 }
5314# ifdef __NeXT__
5315 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5316# else
5317 wait_pid = waitpid(pid, &status, WNOHANG);
5318# endif
5319 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5320 || (wait_pid == pid && WIFEXITED(status)))
5321 {
5322 wait_pid = pid;
5323 break;
5324 }
5325
5326 /* Handle any X events, e.g. serving the clipboard. */
5327 clip_update();
5328
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005329 /* Wait for 1 to 10 msec. 1 is faster but gives the child
5330 * less time. */
5331 mch_delay(delay_msec, TRUE);
5332 if (++delay_msec > 10)
5333 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005334 }
5335 }
5336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
5338 /*
5339 * Wait until our child has exited.
5340 * Ignore wait() returning pids of other children and returning
5341 * because of some signal like SIGWINCH.
5342 * Don't wait if wait_pid was already set above, indicating the
5343 * child already exited.
5344 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005345 if (wait_pid != pid)
5346 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
Bram Moolenaar624891f2010-10-13 16:22:09 +02005348# ifdef FEAT_GUI
5349 /* Close slave side of pty. Only do this after the child has
5350 * exited, otherwise the child may hang when it tries to write on
5351 * the pty. */
5352 if (pty_master_fd >= 0)
5353 close(pty_slave_fd);
5354# endif
5355
Bram Moolenaardf177f62005-02-22 08:39:57 +00005356 /* Make sure the child that writes to the external program is
5357 * dead. */
5358 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005359 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005360 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005361 wait4pid(wpid, NULL);
5362 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005363
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005364# ifdef FEAT_JOB_CHANNEL
5365 --dont_check_job_ended;
5366# endif
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 /*
5369 * Set to raw mode right now, otherwise a CTRL-C after
5370 * catch_signals() will kill Vim.
5371 */
5372 if (tmode == TMODE_RAW)
5373 settmode(TMODE_RAW);
5374 did_settmode = TRUE;
5375 set_signals();
5376
5377 if (WIFEXITED(status))
5378 {
Bram Moolenaar9d75c832005-01-25 21:57:23 +00005379 /* LINTED avoid "bitwise operation on signed value" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005381 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 if (retval == EXEC_FAILED)
5384 {
5385 MSG_PUTS(_("\nCannot execute shell "));
5386 msg_outtrans(p_sh);
5387 msg_putchar('\n');
5388 }
5389 else if (!(options & SHELL_SILENT))
5390 {
5391 MSG_PUTS(_("\nshell returned "));
5392 msg_outnum((long)retval);
5393 msg_putchar('\n');
5394 }
5395 }
5396 }
5397 else
5398 MSG_PUTS(_("\nCommand terminated\n"));
5399 }
5400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402error:
5403 if (!did_settmode)
5404 if (tmode == TMODE_RAW)
5405 settmode(TMODE_RAW); /* set to raw mode */
5406# ifdef FEAT_TITLE
5407 resettitle();
5408# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005409 vim_free(argv);
5410 vim_free(tofree1);
5411 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005414}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#endif /* USE_SYSTEM */
Bram Moolenaar13568252018-03-16 20:46:58 +01005416
5417 int
5418mch_call_shell(
5419 char_u *cmd,
5420 int options) /* SHELL_*, see vim.h */
5421{
5422#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5423 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5424 return mch_call_shell_terminal(cmd, options);
5425#endif
5426#ifdef USE_SYSTEM
5427 return mch_call_shell_system(cmd, options);
5428#else
5429 return mch_call_shell_fork(cmd, options);
5430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431}
5432
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005433#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005434 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005435mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005436{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005437 pid_t pid;
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005438 int fd_in[2] = {-1, -1}; /* for stdin */
5439 int fd_out[2] = {-1, -1}; /* for stdout */
5440 int fd_err[2] = {-1, -1}; /* for stderr */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005441 int pty_master_fd = -1;
5442 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005443 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005444 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5445 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5446 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005447 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005448 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5449 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005450 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005451 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005452 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005453
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005454 if (use_out_for_err && use_null_for_out)
5455 use_null_for_err = TRUE;
5456
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005457 /* default is to fail */
5458 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005459
Bram Moolenaarb2412082017-08-20 18:09:14 +02005460 if (options->jo_pty
5461 && (!(use_file_for_in || use_null_for_in)
5462 || !(use_file_for_in || use_null_for_out)
5463 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005464 {
5465 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5466 if (job->jv_tty_out != NULL)
5467 job->jv_tty_in = vim_strsave(job->jv_tty_out);
5468 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005469
Bram Moolenaar12dcf022016-02-15 23:09:04 +01005470 /* TODO: without the channel feature connect the child to /dev/null? */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005471 /* Open pipes for stdin, stdout, stderr. */
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005472 if (use_file_for_in)
5473 {
5474 char_u *fname = options->jo_io_name[PART_IN];
5475
5476 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5477 if (fd_in[0] < 0)
5478 {
5479 EMSG2(_(e_notopen), fname);
5480 goto failed;
5481 }
5482 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005483 else
5484 /* When writing buffer lines to the input don't use the pty, so that
5485 * the pipe can be closed when all lines were written. */
5486 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5487 && pipe(fd_in) < 0)
5488 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005489
5490 if (use_file_for_out)
5491 {
5492 char_u *fname = options->jo_io_name[PART_OUT];
5493
5494 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5495 if (fd_out[1] < 0)
5496 {
5497 EMSG2(_(e_notopen), fname);
5498 goto failed;
5499 }
5500 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005501 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005502 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005503
5504 if (use_file_for_err)
5505 {
5506 char_u *fname = options->jo_io_name[PART_ERR];
5507
5508 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5509 if (fd_err[1] < 0)
5510 {
5511 EMSG2(_(e_notopen), fname);
5512 goto failed;
5513 }
5514 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005515 else if (!use_out_for_err && !use_null_for_err
5516 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005517 goto failed;
5518
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005519 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5520 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005521 if (options->jo_set & JO_CHANNEL)
5522 {
5523 channel = options->jo_channel;
5524 if (channel != NULL)
5525 ++channel->ch_refcount;
5526 }
5527 else
5528 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005529 if (channel == NULL)
5530 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005531 if (job->jv_tty_out != NULL)
5532 ch_log(channel, "using pty %s on fd %d",
5533 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005534 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005535
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005536 BLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005537 pid = fork(); /* maybe we should use vfork() */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005538 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005540 /* failed to fork */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005541 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005542 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005543 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005544 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005546 int null_fd = -1;
5547 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005548
Bram Moolenaar835dc632016-02-07 14:27:38 +01005549 /* child */
5550 reset_signals(); /* handle signals normally */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005551 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005552
Bram Moolenaar819524702018-02-27 19:10:00 +01005553# ifdef FEAT_JOB_CHANNEL
5554 if (ch_log_active())
5555 /* close the log file in the child */
5556 ch_logfile((char_u *)"", (char_u *)"");
5557# endif
5558
Bram Moolenaar835dc632016-02-07 14:27:38 +01005559# ifdef HAVE_SETSID
5560 /* Create our own process group, so that the child and all its
5561 * children can be kill()ed. Don't do this when using pipes,
5562 * because stdin is not a tty, we would lose /dev/tty. */
5563 (void)setsid();
5564# endif
5565
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005566# ifdef FEAT_TERMINAL
5567 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005568 {
5569 char *term = (char *)T_NAME;
5570
5571#ifdef FEAT_GUI
5572 if (term_is_gui(T_NAME))
5573 /* In the GUI 'term' is not what we want, use $TERM. */
5574 term = getenv("TERM");
5575#endif
5576 /* Use 'term' or $TERM if it starts with "xterm", otherwise fall
5577 * back to "xterm". */
5578 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
5579 term = "xterm";
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005580 set_child_environment(
5581 (long)options->jo_term_rows,
5582 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005583 term,
5584 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005585 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005586 else
5587# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005588 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005589
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005590 if (options->jo_env != NULL)
5591 {
5592 dict_T *dict = options->jo_env;
5593 hashitem_T *hi;
5594 int todo = (int)dict->dv_hashtab.ht_used;
5595
5596 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5597 if (!HASHITEM_EMPTY(hi))
5598 {
5599 typval_T *item = &dict_lookup(hi)->di_tv;
5600
5601 vim_setenv((char_u*)hi->hi_key, get_tv_string(item));
5602 --todo;
5603 }
5604 }
5605
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005606 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005607 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005608 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005609 if (null_fd < 0)
5610 {
5611 perror("opening /dev/null failed");
5612 _exit(OPEN_NULL_FAILED);
5613 }
5614 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005615
Bram Moolenaar223896d2017-08-02 22:33:28 +02005616 if (pty_slave_fd >= 0)
5617 {
5618 /* push stream discipline modules */
5619 SetupSlavePTY(pty_slave_fd);
5620# ifdef TIOCSCTTY
5621 /* Try to become controlling tty (probably doesn't work,
5622 * unless run by root) */
5623 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5624# endif
5625 }
5626
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005627 /* set up stdin for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005628 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005629 if (use_null_for_in && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005630 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005631 else if (fd_in[0] < 0)
5632 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005633 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005634 ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005635
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005636 /* set up stderr for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005637 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005638 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005639 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005640 ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005641 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005642 }
5643 else if (use_out_for_err)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005644 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005645 else if (fd_err[1] < 0)
5646 ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005647 else
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005648 ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005649
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005650 /* set up stdout for the child */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005651 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005652 if (use_null_for_out && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005653 ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005654 else if (fd_out[1] < 0)
5655 ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005656 else
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005657 ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005658
5659 if (fd_in[0] >= 0)
5660 close(fd_in[0]);
5661 if (fd_in[1] >= 0)
5662 close(fd_in[1]);
5663 if (fd_out[0] >= 0)
5664 close(fd_out[0]);
5665 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005666 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005667 if (fd_err[0] >= 0)
5668 close(fd_err[0]);
5669 if (fd_err[1] >= 0)
5670 close(fd_err[1]);
5671 if (pty_master_fd >= 0)
5672 {
Bram Moolenaar223896d2017-08-02 22:33:28 +02005673 close(pty_master_fd); /* not used in the child */
5674 close(pty_slave_fd); /* was duped above */
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005675 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005676
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005677 if (null_fd >= 0)
5678 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005679
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005680 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5681 _exit(EXEC_FAILED);
5682
Bram Moolenaar835dc632016-02-07 14:27:38 +01005683 /* See above for type of argv. */
5684 execvp(argv[0], argv);
5685
Bram Moolenaar4694a172016-04-21 14:05:23 +02005686 if (stderr_works)
5687 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005688# ifdef EXITFREE
Bram Moolenaarcda77642016-06-04 13:32:35 +02005689 /* calling free_all_mem() here causes problems. Ignore valgrind
5690 * reporting possibly leaked memory. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005691# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005692 _exit(EXEC_FAILED); /* exec failed, return failure code */
5693 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005694
5695 /* parent */
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005696 UNBLOCK_SIGNALS(&curset);
5697
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005698 job->jv_pid = pid;
5699 job->jv_status = JOB_STARTED;
Bram Moolenaarde279892016-03-11 22:19:44 +01005700 job->jv_channel = channel; /* ch_refcount was set above */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005701
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005702 if (pty_master_fd >= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005703 close(pty_slave_fd); /* not used in the parent */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005704 /* close child stdin, stdout and stderr */
Bram Moolenaar819524702018-02-27 19:10:00 +01005705 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005706 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005707 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005708 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005709 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005710 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005711 if (channel != NULL)
5712 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005713 int in_fd = use_file_for_in || use_null_for_in
5714 ? INVALID_FD : fd_in[1] < 0 ? pty_master_fd : fd_in[1];
5715 int out_fd = use_file_for_out || use_null_for_out
5716 ? INVALID_FD : fd_out[0] < 0 ? pty_master_fd : fd_out[0];
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005717 /* When using pty_master_fd only set it for stdout, do not duplicate it
5718 * for stderr, it only needs to be read once. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005719 int err_fd = use_out_for_err || use_file_for_err || use_null_for_err
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02005720 ? INVALID_FD
5721 : fd_err[0] >= 0
5722 ? fd_err[0]
5723 : (out_fd == pty_master_fd
5724 ? INVALID_FD
5725 : pty_master_fd);
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005726
5727 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005728 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005729 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005730 else
5731 {
5732 if (fd_in[1] >= 0)
5733 close(fd_in[1]);
5734 if (fd_out[0] >= 0)
5735 close(fd_out[0]);
5736 if (fd_err[0] >= 0)
5737 close(fd_err[0]);
5738 if (pty_master_fd >= 0)
5739 close(pty_master_fd);
5740 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005741
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005742 /* success! */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005743 return;
5744
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005745failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005746 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005747 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005748 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005749 if (fd_in[1] >= 0)
5750 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005751 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005752 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005753 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005754 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005755 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005756 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005757 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005758 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005759 if (pty_master_fd >= 0)
5760 close(pty_master_fd);
5761 if (pty_slave_fd >= 0)
5762 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005763}
5764
5765 char *
5766mch_job_status(job_T *job)
5767{
5768# ifdef HAVE_UNION_WAIT
5769 union wait status;
5770# else
5771 int status = -1;
5772# endif
5773 pid_t wait_pid = 0;
5774
5775# ifdef __NeXT__
5776 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5777# else
5778 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5779# endif
5780 if (wait_pid == -1)
5781 {
5782 /* process must have exited */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005783 if (job->jv_status < JOB_ENDED)
5784 ch_log(job->jv_channel, "Job no longer exists: %s",
5785 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005786 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005787 }
5788 if (wait_pid == 0)
5789 return "run";
5790 if (WIFEXITED(status))
5791 {
5792 /* LINTED avoid "bitwise operation on signed value" */
5793 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005794 if (job->jv_status < JOB_ENDED)
5795 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005796 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005797 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005798 if (WIFSIGNALED(status))
5799 {
5800 job->jv_exitval = -1;
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005801 if (job->jv_status < JOB_ENDED)
5802 ch_log(job->jv_channel, "Job terminated by a signal");
Bram Moolenaar97792de2016-10-15 18:36:49 +02005803 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005804 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005805 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005806
5807return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005808 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005809 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005810 return "dead";
5811}
5812
5813 job_T *
5814mch_detect_ended_job(job_T *job_list)
5815{
5816# ifdef HAVE_UNION_WAIT
5817 union wait status;
5818# else
5819 int status = -1;
5820# endif
5821 pid_t wait_pid = 0;
5822 job_T *job;
5823
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005824# ifndef USE_SYSTEM
5825 /* Do not do this when waiting for a shell command to finish, we would get
5826 * the exit value here (and discard it), the exit value obtained there
5827 * would then be wrong. */
5828 if (dont_check_job_ended > 0)
5829 return NULL;
5830# endif
5831
Bram Moolenaar97792de2016-10-15 18:36:49 +02005832# ifdef __NeXT__
5833 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5834# else
5835 wait_pid = waitpid(-1, &status, WNOHANG);
5836# endif
5837 if (wait_pid <= 0)
5838 /* no process ended */
5839 return NULL;
5840 for (job = job_list; job != NULL; job = job->jv_next)
5841 {
5842 if (job->jv_pid == wait_pid)
5843 {
5844 if (WIFEXITED(status))
5845 /* LINTED avoid "bitwise operation on signed value" */
5846 job->jv_exitval = WEXITSTATUS(status);
5847 else if (WIFSIGNALED(status))
5848 job->jv_exitval = -1;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005849 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005850 {
5851 ch_log(job->jv_channel, "Job ended");
5852 job->jv_status = JOB_ENDED;
5853 }
5854 return job;
5855 }
5856 }
5857 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005858}
5859
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005860/*
5861 * Send a (deadly) signal to "job".
5862 * Return FAIL if "how" is not a valid name.
5863 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005864 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005865mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005867 int sig = -1;
5868 pid_t job_pid;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005869
Bram Moolenaar923d9262016-02-25 20:56:01 +01005870 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005871 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005872 else if (STRCMP(how, "hup") == 0)
5873 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005874 else if (STRCMP(how, "quit") == 0)
5875 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005876 else if (STRCMP(how, "int") == 0)
5877 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005878 else if (STRCMP(how, "kill") == 0)
5879 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005880#ifdef SIGWINCH
5881 else if (STRCMP(how, "winch") == 0)
5882 sig = SIGWINCH;
5883#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005884 else if (isdigit(*how))
5885 sig = atoi((char *)how);
5886 else
5887 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888
Bram Moolenaar72801402016-02-09 11:37:50 +01005889 /* TODO: have an option to only kill the process, not the group? */
Bram Moolenaar76467df2016-02-12 19:30:26 +01005890 job_pid = job->jv_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005891#ifdef HAVE_GETPGID
Bram Moolenaar76467df2016-02-12 19:30:26 +01005892 if (job_pid == getpgid(job_pid))
5893 job_pid = -job_pid;
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01005894#endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01005895
Bram Moolenaar61a66052017-07-22 18:39:00 +02005896 /* Never kill ourselves! */
5897 if (job_pid != 0)
5898 kill(job_pid, sig);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005899
Bram Moolenaar835dc632016-02-07 14:27:38 +01005900 return OK;
5901}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005902
5903/*
5904 * Clear the data related to "job".
5905 */
5906 void
5907mch_clear_job(job_T *job)
5908{
5909 /* call waitpid because child process may become zombie */
5910# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005911 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005912# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005913 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005914# endif
5915}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005916#endif
5917
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005918#if defined(FEAT_TERMINAL) || defined(PROTO)
5919 int
5920mch_create_pty_channel(job_T *job, jobopt_T *options)
5921{
5922 int pty_master_fd = -1;
5923 int pty_slave_fd = -1;
5924 channel_T *channel;
5925
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005926 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out);
5927 if (job->jv_tty_out != NULL)
5928 job->jv_tty_in = vim_strsave(job->jv_tty_out);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005929 close(pty_slave_fd);
5930
5931 channel = add_channel();
5932 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005933 {
5934 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005935 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005936 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005937 if (job->jv_tty_out != NULL)
5938 ch_log(channel, "using pty %s on fd %d",
5939 job->jv_tty_out, pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005940 job->jv_channel = channel; /* ch_refcount was set by add_channel() */
5941 channel->ch_keep_open = TRUE;
5942
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005943 /* Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5944 * it only needs to be read once. */
5945 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005946 channel_set_job(channel, job, options);
5947 return OK;
5948}
5949#endif
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951/*
5952 * Check for CTRL-C typed by reading all available characters.
5953 * In cooked mode we should get SIGINT, no need to check.
5954 */
5955 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005956mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005958 if ((curr_tmode == TMODE_RAW || force)
5959 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 fill_input_buf(FALSE);
5961}
5962
5963/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005964 * Wait "msec" msec until a character is available from the mouse, keyboard,
5965 * from inbuf[].
5966 * "msec" == -1 will block forever.
5967 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005968 * When "ignore_input" is TRUE even check for pending input when input is
5969 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005970 * "interrupted" (if not NULL) is set to TRUE when no character is available
5971 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005972 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005973 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 */
5975 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005976WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005978#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005979 return ui_wait_for_chars_or_timer(
5980 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005981#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005982 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005983#endif
5984}
5985
5986/*
5987 * Wait "msec" msec until a character is available from the mouse or keyboard
5988 * or from inbuf[].
5989 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005990 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005991 * "interrupted" (if not NULL) is set to TRUE when no character is available
5992 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005993 * When a GUI is being used, this will never get called -- webb
5994 */
5995 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005996WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005997{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998#ifdef FEAT_MOUSE_GPM
5999 int gpm_process_wanted;
6000#endif
6001#ifdef FEAT_XCLIPBOARD
6002 int rest;
6003#endif
6004 int avail;
6005
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006006 if (!ignore_input && input_available()) /* something in inbuf[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 return 1;
6008
6009#if defined(FEAT_MOUSE_DEC)
6010 /* May need to query the mouse position. */
6011 if (WantQueryMouse)
6012 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00006013 WantQueryMouse = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
6015 }
6016#endif
6017
6018 /*
6019 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6020 * events. This is a bit complicated, because they might both be defined.
6021 */
6022#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6023# ifdef FEAT_XCLIPBOARD
6024 rest = 0;
6025 if (do_xterm_trace())
6026 rest = msec;
6027# endif
6028 do
6029 {
6030# ifdef FEAT_XCLIPBOARD
6031 if (rest != 0)
6032 {
6033 msec = XT_TRACE_DELAY;
6034 if (rest >= 0 && rest < XT_TRACE_DELAY)
6035 msec = rest;
6036 if (rest >= 0)
6037 rest -= msec;
6038 }
6039# endif
6040# ifdef FEAT_MOUSE_GPM
6041 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006042 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006043 &gpm_process_wanted, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006045 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046# endif
6047 if (!avail)
6048 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006049 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 return 1;
6051# ifdef FEAT_XCLIPBOARD
6052 if (rest == 0 || !do_xterm_trace())
6053# endif
6054 break;
6055 }
6056 }
6057 while (FALSE
6058# ifdef FEAT_MOUSE_GPM
6059 || (gpm_process_wanted && mch_gpm_process() == 0)
6060# endif
6061# ifdef FEAT_XCLIPBOARD
6062 || (!avail && rest != 0)
6063# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006064 )
6065 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006068 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069#endif
6070 return avail;
6071}
6072
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006073#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074/*
6075 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006076 * "msec" == 0 will check for characters once.
6077 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006080 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006081 * "interrupted" (if not NULL) is set to TRUE when no character is available
6082 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084#if defined(__BEOS__)
6085 int
6086#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006087 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006089RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006092 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006093#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 static int busy = FALSE;
6095
6096 /* May retry getting characters after an event was handled. */
6097# define MAY_LOOP
6098
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006099# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /* Remember at what time we started, so that we know how much longer we
6101 * should wait after being interrupted. */
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006102 long start_msec = msec;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006103 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006105 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006106 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107# endif
6108
6109 /* Handle being called recursively. This may happen for the session
6110 * manager stuff, it may save the file, which does a breakcheck. */
6111 if (busy)
6112 return 0;
6113#endif
6114
6115#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006116 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#endif
6118 {
6119#ifdef MAY_LOOP
6120 int finished = TRUE; /* default is to 'loop' just once */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006121# ifdef FEAT_MZSCHEME
6122 int mzquantum_used = FALSE;
6123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#endif
6125#ifndef HAVE_SELECT
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006126 /* each channel may use in, out and err */
6127 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 int nfd;
6129# ifdef FEAT_XCLIPBOARD
6130 int xterm_idx = -1;
6131# endif
6132# ifdef FEAT_MOUSE_GPM
6133 int gpm_idx = -1;
6134# endif
6135# ifdef USE_XSMP
6136 int xsmp_idx = -1;
6137# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006138 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006140# ifdef FEAT_MZSCHEME
6141 mzvim_check_threads();
6142 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6143 {
6144 towait = (int)p_mzq; /* don't wait longer than 'mzquantum' */
6145 mzquantum_used = TRUE;
6146 }
6147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 fds[0].fd = fd;
6149 fds[0].events = POLLIN;
6150 nfd = 1;
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006153 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (xterm_Shell != (Widget)0)
6155 {
6156 xterm_idx = nfd;
6157 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6158 fds[nfd].events = POLLIN;
6159 nfd++;
6160 }
6161# endif
6162# ifdef FEAT_MOUSE_GPM
6163 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6164 {
6165 gpm_idx = nfd;
6166 fds[nfd].fd = gpm_fd;
6167 fds[nfd].events = POLLIN;
6168 nfd++;
6169 }
6170# endif
6171# ifdef USE_XSMP
6172 if (xsmp_icefd != -1)
6173 {
6174 xsmp_idx = nfd;
6175 fds[nfd].fd = xsmp_icefd;
6176 fds[nfd].events = POLLIN;
6177 nfd++;
6178 }
6179# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006180#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006181 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006182#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006183 if (interrupted != NULL)
6184 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006186 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006187
6188 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006189 if (result == 0 && interrupted != NULL && ret > 0)
6190 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006191
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006192# ifdef FEAT_MZSCHEME
6193 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006194 /* MzThreads scheduling is required and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006195 finished = FALSE;
6196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198# ifdef FEAT_XCLIPBOARD
6199 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6200 {
6201 xterm_update(); /* Maybe we should hand out clipboard */
6202 if (--ret == 0 && !input_available())
6203 /* Try again */
6204 finished = FALSE;
6205 }
6206# endif
6207# ifdef FEAT_MOUSE_GPM
6208 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
6209 {
6210 *check_for_gpm = 1;
6211 }
6212# endif
6213# ifdef USE_XSMP
6214 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6215 {
6216 if (fds[xsmp_idx].revents & POLLIN)
6217 {
6218 busy = TRUE;
6219 xsmp_handle_requests();
6220 busy = FALSE;
6221 }
6222 else if (fds[xsmp_idx].revents & POLLHUP)
6223 {
6224 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006225 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 xsmp_close();
6227 }
6228 if (--ret == 0)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006229 finished = FALSE; /* Try again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006233 /* also call when ret == 0, we may be polling a keep-open channel */
6234 if (ret >= 0)
Bram Moolenaare0874f82016-01-24 20:36:41 +01006235 ret = channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238#else /* HAVE_SELECT */
6239
6240 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006241 struct timeval *tvp;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006242 fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006244 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006246# ifdef FEAT_MZSCHEME
6247 mzvim_check_threads();
6248 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6249 {
6250 towait = p_mzq; /* don't wait longer than 'mzquantum' */
6251 mzquantum_used = TRUE;
6252 }
6253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006255 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006257 tv.tv_sec = towait / 1000;
6258 tv.tv_usec = (towait % 1000) * (1000000/1000);
6259 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006261 else
6262 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 /*
6265 * Select on ready for reading and exceptional condition (end of file).
6266 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006267select_eintr:
6268 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006269 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 FD_ZERO(&efds);
6271 FD_SET(fd, &rfds);
6272# if !defined(__QNX__) && !defined(__CYGWIN32__)
6273 /* For QNX select() always returns 1 if this is set. Why? */
6274 FD_SET(fd, &efds);
6275# endif
6276 maxfd = fd;
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006279 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (xterm_Shell != (Widget)0)
6281 {
6282 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6283 if (maxfd < ConnectionNumber(xterm_dpy))
6284 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006285
6286 /* An event may have already been read but not handled. In
6287 * particulary, XFlush may cause this. */
6288 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290# endif
6291# ifdef FEAT_MOUSE_GPM
6292 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6293 {
6294 FD_SET(gpm_fd, &rfds);
6295 FD_SET(gpm_fd, &efds);
6296 if (maxfd < gpm_fd)
6297 maxfd = gpm_fd;
6298 }
6299# endif
6300# ifdef USE_XSMP
6301 if (xsmp_icefd != -1)
6302 {
6303 FD_SET(xsmp_icefd, &rfds);
6304 FD_SET(xsmp_icefd, &efds);
6305 if (maxfd < xsmp_icefd)
6306 maxfd = xsmp_icefd;
6307 }
6308# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006309# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006310 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006311# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006312 if (interrupted != NULL)
6313 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006315 ret = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006316 result = ret > 0 && FD_ISSET(fd, &rfds);
6317 if (result)
6318 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006319 else if (interrupted != NULL && ret > 0)
6320 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006321
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006322# ifdef EINTR
6323 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006324 {
6325 /* Check whether window has been resized, EINTR may be caused by
6326 * SIGWINCH. */
6327 if (do_resize)
6328 handle_resize();
6329
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006330 /* Interrupted by a signal, need to try again. We ignore msec
6331 * here, because we do want to check even after a timeout if
6332 * characters are available. Needed for reading output of an
6333 * external command after the process has finished. */
6334 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006335 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006336# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006337# ifdef __TANDEM
6338 if (ret == -1 && errno == ENOTSUP)
6339 {
6340 FD_ZERO(&rfds);
6341 FD_ZERO(&efds);
6342 ret = 0;
6343 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006344# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006345# ifdef FEAT_MZSCHEME
6346 if (ret == 0 && mzquantum_used)
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006347 /* loop if MzThreads must be scheduled and timeout occurred */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006348 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349# endif
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351# ifdef FEAT_XCLIPBOARD
6352 if (ret > 0 && xterm_Shell != (Widget)0
6353 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6354 {
6355 xterm_update(); /* Maybe we should hand out clipboard */
6356 /* continue looping when we only got the X event and the input
6357 * buffer is empty */
6358 if (--ret == 0 && !input_available())
6359 {
6360 /* Try again */
6361 finished = FALSE;
6362 }
6363 }
6364# endif
6365# ifdef FEAT_MOUSE_GPM
6366 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6367 {
6368 if (FD_ISSET(gpm_fd, &efds))
6369 gpm_close();
6370 else if (FD_ISSET(gpm_fd, &rfds))
6371 *check_for_gpm = 1;
6372 }
6373# endif
6374# ifdef USE_XSMP
6375 if (ret > 0 && xsmp_icefd != -1)
6376 {
6377 if (FD_ISSET(xsmp_icefd, &efds))
6378 {
6379 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00006380 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 xsmp_close();
6382 if (--ret == 0)
6383 finished = FALSE; /* keep going if event was only one */
6384 }
6385 else if (FD_ISSET(xsmp_icefd, &rfds))
6386 {
6387 busy = TRUE;
6388 xsmp_handle_requests();
6389 busy = FALSE;
6390 if (--ret == 0)
6391 finished = FALSE; /* keep going if event was only one */
6392 }
6393 }
6394# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006395#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006396 /* also call when ret == 0, we may be polling a keep-open channel */
6397 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006398 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401#endif /* HAVE_SELECT */
6402
6403#ifdef MAY_LOOP
6404 if (finished || msec == 0)
6405 break;
6406
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006407# ifdef FEAT_CLIENTSERVER
6408 if (server_waiting())
6409 break;
6410# endif
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 /* We're going to loop around again, find out for how long */
6413 if (msec > 0)
6414 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006415# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 /* Compute remaining wait time. */
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006417 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418# else
6419 /* Guess we got interrupted halfway. */
6420 msec = msec / 2;
6421# endif
6422 if (msec <= 0)
6423 break; /* waited long enough */
6424 }
6425#endif
6426 }
6427
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006428 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429}
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431#ifndef NO_EXPANDPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006433 * Expand a path into all matching files and/or directories. Handles "*",
6434 * "?", "[a-z]", "**", etc.
6435 * "path" has backslashes before chars that are not to be expanded.
6436 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
6438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006439mch_expandpath(
6440 garray_T *gap,
6441 char_u *path,
6442 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaar02743632005-07-25 20:42:36 +00006444 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445}
6446#endif
6447
6448/*
6449 * mch_expand_wildcards() - this code does wild-card pattern matching using
6450 * the shell
6451 *
6452 * return OK for success, FAIL for error (you may lose some memory) and put
6453 * an error message in *file.
6454 *
6455 * num_pat is number of input patterns
6456 * pat is array of pointers to input patterns
6457 * num_file is pointer to number of matched file names
6458 * file is pointer to array of pointers to matched file names
6459 */
6460
6461#ifndef SEEK_SET
6462# define SEEK_SET 0
6463#endif
6464#ifndef SEEK_END
6465# define SEEK_END 2
6466#endif
6467
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006468#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006471mch_expand_wildcards(
6472 int num_pat,
6473 char_u **pat,
6474 int *num_file,
6475 char_u ***file,
6476 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 int i;
6479 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006480 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 char_u *p;
6482 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaarc7247912008-01-13 12:54:11 +00006484 /*
6485 * This is the non-OS/2 implementation (really Unix).
6486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 int j;
6488 char_u *tempname;
6489 char_u *command;
6490 FILE *fd;
6491 char_u *buffer;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006492#define STYLE_ECHO 0 /* use "echo", the default */
6493#define STYLE_GLOB 1 /* use "glob", for csh */
6494#define STYLE_VIMGLOB 2 /* use "vimglob", for Posix sh */
6495#define STYLE_PRINT 3 /* use "print -N", for zsh */
6496#define STYLE_BT 4 /* `cmd` expansion, execute the pattern
6497 * directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 int shell_style = STYLE_ECHO;
6499 int check_spaces;
6500 static int did_find_nul = FALSE;
6501 int ampersent = FALSE;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006502 /* vimglob() function to define for Posix shell */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006503 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
6505 *num_file = 0; /* default: no files found */
6506 *file = NULL;
6507
6508 /*
6509 * If there are no wildcards, just copy the names to allocated memory.
6510 * Saves a lot of time, because we don't have to start a new shell.
6511 */
6512 if (!have_wildcard(num_pat, pat))
6513 return save_patterns(num_pat, pat, num_file, file);
6514
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006515# ifdef HAVE_SANDBOX
6516 /* Don't allow any shell command in the sandbox. */
6517 if (sandbox != 0 && check_secure())
6518 return FAIL;
6519# endif
6520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 /*
6522 * Don't allow the use of backticks in secure and restricted mode.
6523 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006524 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 for (i = 0; i < num_pat; ++i)
6526 if (vim_strchr(pat[i], '`') != NULL
6527 && (check_restricted() || check_secure()))
6528 return FAIL;
6529
6530 /*
6531 * get a name for the temp file
6532 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006533 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
6535 EMSG(_(e_notmp));
6536 return FAIL;
6537 }
6538
6539 /*
6540 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006541 * file.
6542 * STYLE_BT: NL separated
6543 * If expanding `cmd` execute it directly.
6544 * STYLE_GLOB: NUL separated
6545 * If we use *csh, "glob" will work better than "echo".
6546 * STYLE_PRINT: NL or NUL separated
6547 * If we use *zsh, "print -N" will work better than "glob".
6548 * STYLE_VIMGLOB: NL separated
6549 * If we use *sh*, we define "vimglob()".
6550 * STYLE_ECHO: space separated.
6551 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 */
6553 if (num_pat == 1 && *pat[0] == '`'
6554 && (len = STRLEN(pat[0])) > 2
6555 && *(pat[0] + len - 1) == '`')
6556 shell_style = STYLE_BT;
6557 else if ((len = STRLEN(p_sh)) >= 3)
6558 {
6559 if (STRCMP(p_sh + len - 3, "csh") == 0)
6560 shell_style = STYLE_GLOB;
6561 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6562 shell_style = STYLE_PRINT;
6563 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006564 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6565 "sh") != NULL)
6566 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
Bram Moolenaarc7247912008-01-13 12:54:11 +00006568 /* Compute the length of the command. We need 2 extra bytes: for the
6569 * optional '&' and for the NUL.
6570 * Worst case: "unset nonomatch; print -N >" plus two is 29 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006572 if (shell_style == STYLE_VIMGLOB)
6573 len += STRLEN(sh_vimglob_func);
6574
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006575 for (i = 0; i < num_pat; ++i)
6576 {
6577 /* Count the length of the patterns in the same way as they are put in
6578 * "command" below. */
6579#ifdef USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 len += STRLEN(pat[i]) + 3; /* add space and two quotes */
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006581#else
6582 ++len; /* add space */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006583 for (j = 0; pat[i][j] != NUL; ++j)
6584 {
6585 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
6586 ++len; /* may add a backslash */
6587 ++len;
6588 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006589#endif
6590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 command = alloc(len);
6592 if (command == NULL)
6593 {
6594 /* out of memory */
6595 vim_free(tempname);
6596 return FAIL;
6597 }
6598
6599 /*
6600 * Build the shell command:
6601 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6602 * recognizes this).
6603 * - Add the shell command to print the expanded names.
6604 * - Add the temp file name.
6605 * - Add the file name patterns.
6606 */
6607 if (shell_style == STYLE_BT)
6608 {
Bram Moolenaar316059c2006-01-14 21:18:42 +00006609 /* change `command; command& ` to (command; command ) */
6610 STRCPY(command, "(");
6611 STRCAT(command, pat[0] + 1); /* exclude first backtick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 p = command + STRLEN(command) - 1;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006613 *p-- = ')'; /* remove last backtick */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006614 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 --p;
6616 if (*p == '&') /* remove trailing '&' */
6617 {
6618 ampersent = TRUE;
6619 *p = ' ';
6620 }
6621 STRCAT(command, ">");
6622 }
6623 else
6624 {
6625 if (flags & EW_NOTFOUND)
6626 STRCPY(command, "set nonomatch; ");
6627 else
6628 STRCPY(command, "unset nonomatch; ");
6629 if (shell_style == STYLE_GLOB)
6630 STRCAT(command, "glob >");
6631 else if (shell_style == STYLE_PRINT)
6632 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006633 else if (shell_style == STYLE_VIMGLOB)
6634 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 else
6636 STRCAT(command, "echo >");
6637 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006638
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (shell_style != STYLE_BT)
6642 for (i = 0; i < num_pat; ++i)
6643 {
6644 /* When using system() always add extra quotes, because the shell
Bram Moolenaar316059c2006-01-14 21:18:42 +00006645 * is started twice. Otherwise put a backslash before special
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00006646 * characters, except inside ``. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647#ifdef USE_SYSTEM
6648 STRCAT(command, " \"");
6649 STRCAT(command, pat[i]);
6650 STRCAT(command, "\"");
6651#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006652 int intick = FALSE;
6653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 p = command + STRLEN(command);
6655 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006656 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006657 {
6658 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006659 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006660 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6661 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006662 /* Remove a backslash, take char literally. But keep
Bram Moolenaar49315f62006-02-04 00:54:59 +00006663 * backslash inside backticks, before a special character
6664 * and before a backtick. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006665 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006666 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6667 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006668 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006669 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006670 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006671 else if (!intick
6672 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6673 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar316059c2006-01-14 21:18:42 +00006674 /* Put a backslash before a special character, but not
Bram Moolenaare4df1642014-08-29 12:58:44 +02006675 * when inside ``. And not for $var when EW_KEEPDOLLAR is
6676 * set. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00006677 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006678
6679 /* Copy one character. */
6680 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 *p = NUL;
6683#endif
6684 }
6685 if (flags & EW_SILENT)
6686 show_shell_mess = FALSE;
6687 if (ampersent)
Bram Moolenaarc7247912008-01-13 12:54:11 +00006688 STRCAT(command, "&"); /* put the '&' after the redirection */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
6690 /*
6691 * Using zsh -G: If a pattern has no matches, it is just deleted from
6692 * the argument list, otherwise zsh gives an error message and doesn't
6693 * expand any other pattern.
6694 */
6695 if (shell_style == STYLE_PRINT)
6696 extra_shell_arg = (char_u *)"-G"; /* Use zsh NULL_GLOB option */
6697
6698 /*
6699 * If we use -f then shell variables set in .cshrc won't get expanded.
6700 * vi can do it, so we will too, but it is only necessary if there is a "$"
6701 * in one of the patterns, otherwise we can still use the fast option.
6702 */
6703 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
6704 extra_shell_arg = (char_u *)"-f"; /* Use csh fast option */
6705
6706 /*
6707 * execute the shell command
6708 */
6709 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6710
6711 /* When running in the background, give it some time to create the temp
6712 * file, but don't wait for it to finish. */
6713 if (ampersent)
6714 mch_delay(10L, TRUE);
6715
6716 extra_shell_arg = NULL; /* cleanup */
6717 show_shell_mess = TRUE;
6718 vim_free(command);
6719
Bram Moolenaarc7247912008-01-13 12:54:11 +00006720 if (i != 0) /* mch_call_shell() failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {
6722 mch_remove(tempname);
6723 vim_free(tempname);
6724 /*
6725 * With interactive completion, the error message is not printed.
6726 * However with USE_SYSTEM, I don't know how to turn off error messages
6727 * from the shell, so screen may still get messed up -- webb.
6728 */
6729#ifndef USE_SYSTEM
6730 if (!(flags & EW_SILENT))
6731#endif
6732 {
6733 redraw_later_clear(); /* probably messed up screen */
6734 msg_putchar('\n'); /* clear bottom line quickly */
6735 cmdline_row = Rows - 1; /* continue on last line */
6736#ifdef USE_SYSTEM
6737 if (!(flags & EW_SILENT))
6738#endif
6739 {
6740 MSG(_(e_wildexpand));
6741 msg_start(); /* don't overwrite this message */
6742 }
6743 }
6744 /* If a `cmd` expansion failed, don't list `cmd` as a match, even when
6745 * EW_NOTFOUND is given */
6746 if (shell_style == STYLE_BT)
6747 return FAIL;
6748 goto notfound;
6749 }
6750
6751 /*
6752 * read the names from the file into memory
6753 */
6754 fd = fopen((char *)tempname, READBIN);
6755 if (fd == NULL)
6756 {
6757 /* Something went wrong, perhaps a file name with a special char. */
6758 if (!(flags & EW_SILENT))
6759 {
6760 MSG(_(e_wildexpand));
6761 msg_start(); /* don't overwrite this message */
6762 }
6763 vim_free(tempname);
6764 goto notfound;
6765 }
6766 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006767 llen = ftell(fd); /* get size of temp file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006769 if (llen < 0)
6770 /* just in case ftell() would fail */
6771 buffer = NULL;
6772 else
6773 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (buffer == NULL)
6775 {
6776 /* out of memory */
6777 mch_remove(tempname);
6778 vim_free(tempname);
6779 fclose(fd);
6780 return FAIL;
6781 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006782 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 i = fread((char *)buffer, 1, len, fd);
6784 fclose(fd);
6785 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006786 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {
6788 /* unexpected read error */
6789 EMSG2(_(e_notread), tempname);
6790 vim_free(tempname);
6791 vim_free(buffer);
6792 return FAIL;
6793 }
6794 vim_free(tempname);
6795
Bram Moolenaarc7247912008-01-13 12:54:11 +00006796# if defined(__CYGWIN__) || defined(__CYGWIN32__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* Translate <CR><NL> into <NL>. Caution, buffer may contain NUL. */
6798 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006799 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6801 *p++ = buffer[i];
6802 len = p - buffer;
6803# endif
6804
6805
6806 /* file names are separated with Space */
6807 if (shell_style == STYLE_ECHO)
6808 {
6809 buffer[len] = '\n'; /* make sure the buffer ends in NL */
6810 p = buffer;
6811 for (i = 0; *p != '\n'; ++i) /* count number of entries */
6812 {
6813 while (*p != ' ' && *p != '\n')
6814 ++p;
6815 p = skipwhite(p); /* skip to next entry */
6816 }
6817 }
6818 /* file names are separated with NL */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006819 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {
6821 buffer[len] = NUL; /* make sure the buffer ends in NUL */
6822 p = buffer;
6823 for (i = 0; *p != NUL; ++i) /* count number of entries */
6824 {
6825 while (*p != '\n' && *p != NUL)
6826 ++p;
6827 if (*p != NUL)
6828 ++p;
6829 p = skipwhite(p); /* skip leading white space */
6830 }
6831 }
6832 /* file names are separated with NUL */
6833 else
6834 {
6835 /*
6836 * Some versions of zsh use spaces instead of NULs to separate
6837 * results. Only do this when there is no NUL before the end of the
6838 * buffer, otherwise we would never be able to use file names with
6839 * embedded spaces when zsh does use NULs.
6840 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6841 * don't check for spaces again.
6842 */
6843 check_spaces = FALSE;
6844 if (shell_style == STYLE_PRINT && !did_find_nul)
6845 {
6846 /* If there is a NUL, set did_find_nul, else set check_spaces */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006847 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006848 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 did_find_nul = TRUE;
6850 else
6851 check_spaces = TRUE;
6852 }
6853
6854 /*
6855 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6856 * already is one, for STYLE_GLOB it needs to be added.
6857 */
6858 if (len && buffer[len - 1] == NUL)
6859 --len;
6860 else
6861 buffer[len] = NUL;
6862 i = 0;
6863 for (p = buffer; p < buffer + len; ++p)
6864 if (*p == NUL || (*p == ' ' && check_spaces)) /* count entry */
6865 {
6866 ++i;
6867 *p = NUL;
6868 }
6869 if (len)
6870 ++i; /* count last entry */
6871 }
6872 if (i == 0)
6873 {
6874 /*
6875 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6876 * /bin/sh will happily expand it to nothing rather than returning an
6877 * error; and hey, it's good to check anyway -- webb.
6878 */
6879 vim_free(buffer);
6880 goto notfound;
6881 }
6882 *num_file = i;
6883 *file = (char_u **)alloc(sizeof(char_u *) * i);
6884 if (*file == NULL)
6885 {
6886 /* out of memory */
6887 vim_free(buffer);
6888 return FAIL;
6889 }
6890
6891 /*
6892 * Isolate the individual file names.
6893 */
6894 p = buffer;
6895 for (i = 0; i < *num_file; ++i)
6896 {
6897 (*file)[i] = p;
6898 /* Space or NL separates */
Bram Moolenaarc7247912008-01-13 12:54:11 +00006899 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6900 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006902 while (!(shell_style == STYLE_ECHO && *p == ' ')
6903 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 ++p;
6905 if (p == buffer + len) /* last entry */
6906 *p = NUL;
6907 else
6908 {
6909 *p++ = NUL;
6910 p = skipwhite(p); /* skip to next entry */
6911 }
6912 }
6913 else /* NUL separates */
6914 {
6915 while (*p && p < buffer + len) /* skip entry */
6916 ++p;
6917 ++p; /* skip NUL */
6918 }
6919 }
6920
6921 /*
6922 * Move the file names to allocated memory.
6923 */
6924 for (j = 0, i = 0; i < *num_file; ++i)
6925 {
6926 /* Require the files to exist. Helps when using /bin/sh */
6927 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6928 continue;
6929
6930 /* check if this entry should be included */
6931 dir = (mch_isdir((*file)[i]));
6932 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6933 continue;
6934
Bram Moolenaara2031822006-03-07 22:29:51 +00006935 /* Skip files that are not executable if we check for that. */
Bram Moolenaarb5971142015-03-21 17:32:19 +01006936 if (!dir && (flags & EW_EXEC)
6937 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006938 continue;
6939
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
6941 if (p)
6942 {
6943 STRCPY(p, (*file)[i]);
6944 if (dir)
Bram Moolenaarb2389092008-01-03 17:56:04 +00006945 add_pathsep(p); /* add '/' to a directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 (*file)[j++] = p;
6947 }
6948 }
6949 vim_free(buffer);
6950 *num_file = j;
6951
6952 if (*num_file == 0) /* rejected all entries */
6953 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006954 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 goto notfound;
6956 }
6957
6958 return OK;
6959
6960notfound:
6961 if (flags & EW_NOTFOUND)
6962 return save_patterns(num_pat, pat, num_file, file);
6963 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
6966#endif /* VMS */
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006969save_patterns(
6970 int num_pat,
6971 char_u **pat,
6972 int *num_file,
6973 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006976 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 *file = (char_u **)alloc(num_pat * sizeof(char_u *));
6979 if (*file == NULL)
6980 return FAIL;
6981 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006982 {
6983 s = vim_strsave(pat[i]);
6984 if (s != NULL)
6985 /* Be compatible with expand_filename(): halve the number of
6986 * backslashes. */
6987 backslash_halve(s);
6988 (*file)[i] = s;
6989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 *num_file = num_pat;
6991 return OK;
6992}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994/*
6995 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6996 * expand.
6997 */
6998 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006999mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007001 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (*p == '\\' && p[1] != NUL)
7004 ++p;
7005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (vim_strchr((char_u *)
7007#ifdef VMS
7008 "*?%"
7009#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#endif
7012 , *p) != NULL)
7013 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015 return FALSE;
7016}
7017
7018/*
7019 * Return TRUE if the string "p" contains a wildcard.
7020 * Don't recognize '~' at the end as a wildcard.
7021 */
7022 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007023mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007025 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (*p == '\\' && p[1] != NUL)
7028 ++p;
7029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (vim_strchr((char_u *)
7031#ifdef VMS
7032 "*?%$"
7033#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035#endif
7036 , *p) != NULL
7037 || (*p == '~' && p[1] != NUL))
7038 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040 return FALSE;
7041}
7042
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007044have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 int i;
7047
7048 for (i = 0; i < num; i++)
7049 if (mch_has_wildcard(file[i]))
7050 return 1;
7051 return 0;
7052}
7053
7054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007055have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 int i;
7058
7059 for (i = 0; i < num; i++)
7060 if (vim_strchr(file[i], '$') != NULL)
7061 return TRUE;
7062 return FALSE;
7063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007065#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066/*
7067 * Scaled-down version of rename(), which is missing in Xenix.
7068 * This version can only move regular files and will fail if the
7069 * destination exists.
7070 */
7071 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007072mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 struct stat st;
7075
7076 if (stat(dest, &st) >= 0) /* fail if destination exists */
7077 return -1;
7078 if (link(src, dest) != 0) /* link file to new name */
7079 return -1;
7080 if (mch_remove(src) == 0) /* delete link to old name */
7081 return 0;
7082 return -1;
7083}
7084#endif /* !HAVE_RENAME */
7085
7086#ifdef FEAT_MOUSE_GPM
7087/*
7088 * Initializes connection with gpm (if it isn't already opened)
7089 * Return 1 if succeeded (or connection already opened), 0 if failed
7090 */
7091 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007092gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094 static Gpm_Connect gpm_connect; /* Must it be kept till closing ? */
7095
7096 if (!gpm_flag)
7097 {
7098 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7099 gpm_connect.defaultMask = ~GPM_HARD;
7100 /* Default handling for mouse move*/
7101 gpm_connect.minMod = 0; /* Handle any modifier keys */
7102 gpm_connect.maxMod = 0xffff;
7103 if (Gpm_Open(&gpm_connect, 0) > 0)
7104 {
7105 /* gpm library tries to handling TSTP causes
7106 * problems. Anyways, we close connection to Gpm whenever
7107 * we are going to suspend or starting an external process
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00007108 * so we shouldn't have problem with this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 */
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007110# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 return 1; /* succeed */
7114 }
7115 if (gpm_fd == -2)
7116 Gpm_Close(); /* We don't want to talk to xterm via gpm */
7117 return 0;
7118 }
7119 return 1; /* already open */
7120}
7121
7122/*
7123 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 */
7125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007126gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128 if (gpm_flag && gpm_fd >= 0) /* if Open */
7129 Gpm_Close();
7130}
7131
7132/* Reads gpm event and adds special keys to input buf. Returns length of
7133 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007134 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 */
7136 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007137mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 int button;
7140 static Gpm_Event gpm_event;
7141 char_u string[6];
7142 int_u vim_modifiers;
7143 int row,col;
7144 unsigned char buttons_mask;
7145 unsigned char gpm_modifiers;
7146 static unsigned char old_buttons = 0;
7147
7148 Gpm_GetEvent(&gpm_event);
7149
7150#ifdef FEAT_GUI
7151 /* Don't put events in the input queue now. */
7152 if (hold_gui_events)
7153 return 0;
7154#endif
7155
7156 row = gpm_event.y - 1;
7157 col = gpm_event.x - 1;
7158
7159 string[0] = ESC; /* Our termcode */
7160 string[1] = 'M';
7161 string[2] = 'G';
7162 switch (GPM_BARE_EVENTS(gpm_event.type))
7163 {
7164 case GPM_DRAG:
7165 string[3] = MOUSE_DRAG;
7166 break;
7167 case GPM_DOWN:
7168 buttons_mask = gpm_event.buttons & ~old_buttons;
7169 old_buttons = gpm_event.buttons;
7170 switch (buttons_mask)
7171 {
7172 case GPM_B_LEFT:
7173 button = MOUSE_LEFT;
7174 break;
7175 case GPM_B_MIDDLE:
7176 button = MOUSE_MIDDLE;
7177 break;
7178 case GPM_B_RIGHT:
7179 button = MOUSE_RIGHT;
7180 break;
7181 default:
7182 return 0;
7183 /*Don't know what to do. Can more than one button be
7184 * reported in one event? */
7185 }
7186 string[3] = (char_u)(button | 0x20);
7187 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7188 break;
7189 case GPM_UP:
7190 string[3] = MOUSE_RELEASE;
7191 old_buttons &= ~gpm_event.buttons;
7192 break;
7193 default:
7194 return 0;
7195 }
7196 /*This code is based on gui_x11_mouse_cb in gui_x11.c */
7197 gpm_modifiers = gpm_event.modifiers;
7198 vim_modifiers = 0x0;
7199 /* I ignore capslock stats. Aren't we all just hate capslock mixing with
7200 * Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7201 * K_CAPSSHIFT is defined 8, so it probably isn't even reported
7202 */
7203 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7204 vim_modifiers |= MOUSE_SHIFT;
7205
7206 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7207 vim_modifiers |= MOUSE_CTRL;
7208 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7209 vim_modifiers |= MOUSE_ALT;
7210 string[3] |= vim_modifiers;
7211 string[4] = (char_u)(col + ' ' + 1);
7212 string[5] = (char_u)(row + ' ' + 1);
7213 add_to_input_buf(string, 6);
7214 return 6;
7215}
7216#endif /* FEAT_MOUSE_GPM */
7217
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007218#ifdef FEAT_SYSMOUSE
7219/*
7220 * Initialize connection with sysmouse.
7221 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7222 * output, any sysmouse output than will be processed via sig_sysmouse().
7223 * Return OK if succeeded, FAIL if failed.
7224 */
7225 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007226sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007227{
7228 struct mouse_info mouse;
7229
7230 mouse.operation = MOUSE_MODE;
7231 mouse.u.mode.mode = 0;
7232 mouse.u.mode.signal = SIGUSR2;
7233 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7234 {
7235 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7236 mouse.operation = MOUSE_SHOW;
7237 ioctl(1, CONS_MOUSECTL, &mouse);
7238 return OK;
7239 }
7240 return FAIL;
7241}
7242
7243/*
7244 * Stop processing SIGUSR2 signals, and also make sure that
7245 * virtual console do not send us any sysmouse related signal.
7246 */
7247 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007248sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007249{
7250 struct mouse_info mouse;
7251
7252 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7253 mouse.operation = MOUSE_MODE;
7254 mouse.u.mode.mode = 0;
7255 mouse.u.mode.signal = 0;
7256 ioctl(1, CONS_MOUSECTL, &mouse);
7257}
7258
7259/*
7260 * Gets info from sysmouse and adds special keys to input buf.
7261 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007262 static RETSIGTYPE
7263sig_sysmouse SIGDEFARG(sigarg)
7264{
7265 struct mouse_info mouse;
7266 struct video_info video;
7267 char_u string[6];
7268 int row, col;
7269 int button;
7270 int buttons;
7271 static int oldbuttons = 0;
7272
7273#ifdef FEAT_GUI
7274 /* Don't put events in the input queue now. */
7275 if (hold_gui_events)
7276 return;
7277#endif
7278
7279 mouse.operation = MOUSE_GETINFO;
7280 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7281 && ioctl(1, FBIO_MODEINFO, &video) != -1
7282 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7283 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7284 {
7285 row = mouse.u.data.y / video.vi_cheight;
7286 col = mouse.u.data.x / video.vi_cwidth;
7287 buttons = mouse.u.data.buttons;
7288 string[0] = ESC; /* Our termcode */
7289 string[1] = 'M';
7290 string[2] = 'S';
7291 if (oldbuttons == buttons && buttons != 0)
7292 {
7293 button = MOUSE_DRAG;
7294 }
7295 else
7296 {
7297 switch (buttons)
7298 {
7299 case 0:
7300 button = MOUSE_RELEASE;
7301 break;
7302 case 1:
7303 button = MOUSE_LEFT;
7304 break;
7305 case 2:
7306 button = MOUSE_MIDDLE;
7307 break;
7308 case 4:
7309 button = MOUSE_RIGHT;
7310 break;
7311 default:
7312 return;
7313 }
7314 oldbuttons = buttons;
7315 }
7316 string[3] = (char_u)(button);
7317 string[4] = (char_u)(col + ' ' + 1);
7318 string[5] = (char_u)(row + ' ' + 1);
7319 add_to_input_buf(string, 6);
7320 }
7321 return;
7322}
7323#endif /* FEAT_SYSMOUSE */
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007326typedef char_u * (*STRPROCSTR)(char_u *);
7327typedef char_u * (*INTPROCSTR)(int);
7328typedef int (*STRPROCINT)(char_u *);
7329typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
7331/*
7332 * Call a DLL routine which takes either a string or int param
7333 * and returns an allocated string.
7334 */
7335 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007336mch_libcall(
7337 char_u *libname,
7338 char_u *funcname,
7339 char_u *argstring, /* NULL when using a argint */
7340 int argint,
7341 char_u **string_result,/* NULL when using number_result */
7342 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344# if defined(USE_DLOPEN)
7345 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007346 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347# else
7348 shl_t hinstLib;
7349# endif
7350 STRPROCSTR ProcAdd;
7351 INTPROCSTR ProcAddI;
7352 char_u *retval_str = NULL;
7353 int retval_int = 0;
7354 int success = FALSE;
7355
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007356 /*
7357 * Get a handle to the DLL module.
7358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359# if defined(USE_DLOPEN)
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007360 /* First clear any error, it's not cleared by the dlopen() call. */
7361 (void)dlerror();
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 hinstLib = dlopen((char *)libname, RTLD_LAZY
7364# ifdef RTLD_LOCAL
7365 | RTLD_LOCAL
7366# endif
7367 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007368 if (hinstLib == NULL)
7369 {
7370 /* "dlerr" must be used before dlclose() */
7371 dlerr = (char *)dlerror();
7372 if (dlerr != NULL)
7373 EMSG2(_("dlerror = \"%s\""), dlerr);
7374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375# else
7376 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7377# endif
7378
7379 /* If the handle is valid, try to get the function address. */
7380 if (hinstLib != NULL)
7381 {
7382# ifdef HAVE_SETJMP_H
7383 /*
7384 * Catch a crash when calling the library function. For example when
7385 * using a number where a string pointer is expected.
7386 */
7387 mch_startjmp();
7388 if (SETJMP(lc_jump_env) != 0)
7389 {
7390 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007391# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007392 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 mch_didjmp();
7395 }
7396 else
7397# endif
7398 {
7399 retval_str = NULL;
7400 retval_int = 0;
7401
7402 if (argstring != NULL)
7403 {
7404# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007405 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007406 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407# else
7408 if (shl_findsym(&hinstLib, (const char *)funcname,
7409 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7410 ProcAdd = NULL;
7411# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007412 if ((success = (ProcAdd != NULL
7413# if defined(USE_DLOPEN)
7414 && dlerr == NULL
7415# endif
7416 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
7418 if (string_result == NULL)
7419 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7420 else
7421 retval_str = (ProcAdd)(argstring);
7422 }
7423 }
7424 else
7425 {
7426# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007427 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007428 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429# else
7430 if (shl_findsym(&hinstLib, (const char *)funcname,
7431 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7432 ProcAddI = NULL;
7433# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007434 if ((success = (ProcAddI != NULL
7435# if defined(USE_DLOPEN)
7436 && dlerr == NULL
7437# endif
7438 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
7440 if (string_result == NULL)
7441 retval_int = ((INTPROCINT)ProcAddI)(argint);
7442 else
7443 retval_str = (ProcAddI)(argint);
7444 }
7445 }
7446
7447 /* Save the string before we free the library. */
7448 /* Assume that a "1" or "-1" result is an illegal pointer. */
7449 if (string_result == NULL)
7450 *number_result = retval_int;
7451 else if (retval_str != NULL
7452 && retval_str != (char_u *)1
7453 && retval_str != (char_u *)-1)
7454 *string_result = vim_strsave(retval_str);
7455 }
7456
7457# ifdef HAVE_SETJMP_H
7458 mch_endjmp();
7459# ifdef SIGHASARG
7460 if (lc_signal != 0)
7461 {
7462 int i;
7463
7464 /* try to find the name of this signal */
7465 for (i = 0; signal_info[i].sig != -1; i++)
7466 if (lc_signal == signal_info[i].sig)
7467 break;
7468 EMSG2("E368: got SIG%s in libcall()", signal_info[i].name);
7469 }
7470# endif
7471# endif
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007474 /* "dlerr" must be used before dlclose() */
7475 if (dlerr != NULL)
7476 EMSG2(_("dlerror = \"%s\""), dlerr);
7477
7478 /* Free the DLL module. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 (void)dlclose(hinstLib);
7480# else
7481 (void)shl_unload(hinstLib);
7482# endif
7483 }
7484
7485 if (!success)
7486 {
7487 EMSG2(_(e_libcall), funcname);
7488 return FAIL;
7489 }
7490
7491 return OK;
7492}
7493#endif
7494
7495#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
7496static int xterm_trace = -1; /* default: disabled */
7497static int xterm_button;
7498
7499/*
7500 * Setup a dummy window for X selections in a terminal.
7501 */
7502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007503setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
7505 int z = 0;
7506 char *strp = "";
7507 Widget AppShell;
7508
7509 if (!x_connect_to_server())
7510 return;
7511
7512 open_app_context();
7513 if (app_context != NULL && xterm_Shell == (Widget)0)
7514 {
7515 int (*oldhandler)();
7516#if defined(HAVE_SETJMP_H)
7517 int (*oldIOhandler)();
7518#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007519# ifdef ELAPSED_FUNC
7520 ELAPSED_TYPE start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
7522 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007523 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524# endif
7525
7526 /* Ignore X errors while opening the display */
7527 oldhandler = XSetErrorHandler(x_error_check);
7528
7529#if defined(HAVE_SETJMP_H)
7530 /* Ignore X IO errors while opening the display */
7531 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7532 mch_startjmp();
7533 if (SETJMP(lc_jump_env) != 0)
7534 {
7535 mch_didjmp();
7536 xterm_dpy = NULL;
7537 }
7538 else
7539#endif
7540 {
7541 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7542 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
7543#if defined(HAVE_SETJMP_H)
7544 mch_endjmp();
7545#endif
7546 }
7547
7548#if defined(HAVE_SETJMP_H)
7549 /* Now handle X IO errors normally. */
7550 (void)XSetIOErrorHandler(oldIOhandler);
7551#endif
7552 /* Now handle X errors normally. */
7553 (void)XSetErrorHandler(oldhandler);
7554
7555 if (xterm_dpy == NULL)
7556 {
7557 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007558 verb_msg((char_u *)_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 return;
7560 }
7561
7562 /* Catch terminating error of the X server connection. */
7563 (void)XSetIOErrorHandler(x_IOerror_handler);
7564
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007565# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007567 {
7568 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007569 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007570 verbose_leave();
7571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572# endif
7573
7574 /* Create a Shell to make converters work. */
7575 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7576 applicationShellWidgetClass, xterm_dpy,
7577 NULL);
7578 if (AppShell == (Widget)0)
7579 return;
7580 xterm_Shell = XtVaCreatePopupShell("VIM",
7581 topLevelShellWidgetClass, AppShell,
7582 XtNmappedWhenManaged, 0,
7583 XtNwidth, 1,
7584 XtNheight, 1,
7585 NULL);
7586 if (xterm_Shell == (Widget)0)
7587 return;
7588
7589 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007590 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (x11_display == NULL)
7592 x11_display = xterm_dpy;
7593
7594 XtRealizeWidget(xterm_Shell);
7595 XSync(xterm_dpy, False);
7596 xterm_update();
7597 }
7598 if (xterm_Shell != (Widget)0)
7599 {
7600 clip_init(TRUE);
7601 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7602 x11_window = (Window)atol(strp);
7603 /* Check if $WINDOWID is valid. */
7604 if (test_x11_window(xterm_dpy) == FAIL)
7605 x11_window = 0;
7606 if (x11_window != 0)
7607 xterm_trace = 0;
7608 }
7609}
7610
7611 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007612start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7615 return;
7616 xterm_trace = 1;
7617 xterm_button = button;
7618 do_xterm_trace();
7619}
7620
7621
7622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007623stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
7625 if (xterm_trace < 0)
7626 return;
7627 xterm_trace = 0;
7628}
7629
7630/*
7631 * Query the xterm pointer and generate mouse termcodes if necessary
7632 * return TRUE if dragging is active, else FALSE
7633 */
7634 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007635do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 Window root, child;
7638 int root_x, root_y;
7639 int win_x, win_y;
7640 int row, col;
7641 int_u mask_return;
7642 char_u buf[50];
7643 char_u *strp;
7644 long got_hints;
7645 static char_u *mouse_code;
7646 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7647 static int prev_row = 0, prev_col = 0;
7648 static XSizeHints xterm_hints;
7649
7650 if (xterm_trace <= 0)
7651 return FALSE;
7652
7653 if (xterm_trace == 1)
7654 {
7655 /* Get the hints just before tracking starts. The font size might
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007656 * have changed recently. */
7657 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7658 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 || xterm_hints.width_inc <= 1
7660 || xterm_hints.height_inc <= 1)
7661 {
7662 xterm_trace = -1; /* Not enough data -- disable tracing */
7663 return FALSE;
7664 }
7665
7666 /* Rely on the same mouse code for the duration of this */
7667 mouse_code = find_termcode(mouse_name);
7668 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007669 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 xterm_trace = 2;
7671
7672 /* Find the offset of the chars, there might be a scrollbar on the
7673 * left of the window and/or a menu on the top (eterm etc.) */
7674 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7675 &win_x, &win_y, &mask_return);
7676 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7677 - (xterm_hints.height_inc / 2);
7678 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7679 xterm_hints.y = 2;
7680 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7681 - (xterm_hints.width_inc / 2);
7682 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7683 xterm_hints.x = 2;
7684 return TRUE;
7685 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007686 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 xterm_trace = 0;
7689 return FALSE;
7690 }
7691
7692 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7693 &win_x, &win_y, &mask_return);
7694
7695 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7696 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7697 if (row == prev_row && col == prev_col)
7698 return TRUE;
7699
7700 STRCPY(buf, mouse_code);
7701 strp = buf + STRLEN(buf);
7702 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7703 *strp++ = (char_u)(col + ' ' + 1);
7704 *strp++ = (char_u)(row + ' ' + 1);
7705 *strp = 0;
7706 add_to_input_buf(buf, STRLEN(buf));
7707
7708 prev_row = row;
7709 prev_col = col;
7710 return TRUE;
7711}
7712
7713# if defined(FEAT_GUI) || defined(PROTO)
7714/*
7715 * Destroy the display, window and app_context. Required for GTK.
7716 */
7717 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007718clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 if (xterm_Shell != (Widget)0)
7721 {
7722 XtDestroyWidget(xterm_Shell);
7723 xterm_Shell = (Widget)0;
7724 }
7725 if (xterm_dpy != NULL)
7726 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007727# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 /* Lesstif and Solaris crash here, lose some memory */
7729 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 if (x11_display == xterm_dpy)
7732 x11_display = NULL;
7733 xterm_dpy = NULL;
7734 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007735# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 if (app_context != (XtAppContext)NULL)
7737 {
7738 /* Lesstif and Solaris crash here, lose some memory */
7739 XtDestroyApplicationContext(app_context);
7740 app_context = (XtAppContext)NULL;
7741 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744# endif
7745
7746/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007747 * Catch up with GUI or X events.
7748 */
7749 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007750clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007751{
7752# ifdef FEAT_GUI
7753 if (gui.in_use)
7754 gui_mch_update();
7755 else
7756# endif
7757 if (xterm_Shell != (Widget)0)
7758 xterm_update();
7759}
7760
7761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 * Catch up with any queued X events. This may put keyboard input into the
7763 * input buffer, call resize call-backs, trigger timers etc. If there is
7764 * nothing in the X event queue (& no timers pending), then we return
7765 * immediately.
7766 */
7767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007768xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770 XEvent event;
7771
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007772 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007774 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007776 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007777 break;
7778
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007779 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007780 {
7781 /* There is an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007782 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007783#ifdef FEAT_CLIENTSERVER
7784 {
7785 XPropertyEvent *e = (XPropertyEvent *)&event;
7786
7787 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007789 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007792 XtDispatchEvent(&event);
7793 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007794 else
7795 {
7796 /* There is something else than an event to process. */
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007797 XtAppProcessEvent(app_context, mask);
7798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800}
7801
7802 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007803clip_xterm_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 if (xterm_Shell != (Widget)0)
7806 return clip_x11_own_selection(xterm_Shell, cbd);
7807 return FAIL;
7808}
7809
7810 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007811clip_xterm_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 if (xterm_Shell != (Widget)0)
7814 clip_x11_lose_selection(xterm_Shell, cbd);
7815}
7816
7817 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007818clip_xterm_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 if (xterm_Shell != (Widget)0)
7821 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7822}
7823
7824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007825clip_xterm_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 clip_x11_set_selection(cbd);
7828}
7829#endif
7830
7831
7832#if defined(USE_XSMP) || defined(PROTO)
7833/*
7834 * Code for X Session Management Protocol.
7835 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007836static void xsmp_handle_save_yourself(SmcConn smc_conn, SmPointer client_data, int save_type, Bool shutdown, int interact_style, Bool fast);
7837static void xsmp_die(SmcConn smc_conn, SmPointer client_data);
7838static void xsmp_save_complete(SmcConn smc_conn, SmPointer client_data);
7839static void xsmp_shutdown_cancelled(SmcConn smc_conn, SmPointer client_data);
7840static void xsmp_ice_connection(IceConn iceConn, IcePointer clientData, Bool opening, IcePointer *watchData);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841
7842
7843# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007844static void xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846/*
7847 * This is our chance to ask the user if they want to save,
7848 * or abort the logout
7849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007851xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 cmdmod_T save_cmdmod;
7854 int cancel_shutdown = False;
7855
7856 save_cmdmod = cmdmod;
7857 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007858 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 /* Mustn't logout */
7860 cancel_shutdown = True;
7861 cmdmod = save_cmdmod;
7862 setcursor(); /* position cursor */
7863 out_flush();
7864
7865 /* Done interaction */
7866 SmcInteractDone(smc_conn, cancel_shutdown);
7867
7868 /* Finish off
7869 * Only end save-yourself here if we're not cancelling shutdown;
7870 * we'll get a cancelled callback later in which we'll end it.
7871 * Hopefully get around glitchy SMs (like GNOME-1)
7872 */
7873 if (!cancel_shutdown)
7874 {
7875 xsmp.save_yourself = False;
7876 SmcSaveYourselfDone(smc_conn, True);
7877 }
7878}
7879# endif
7880
7881/*
7882 * Callback that starts save-yourself.
7883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007885xsmp_handle_save_yourself(
7886 SmcConn smc_conn,
7887 SmPointer client_data UNUSED,
7888 int save_type UNUSED,
7889 Bool shutdown,
7890 int interact_style UNUSED,
7891 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 /* Handle already being in saveyourself */
7894 if (xsmp.save_yourself)
7895 SmcSaveYourselfDone(smc_conn, True);
7896 xsmp.save_yourself = True;
7897 xsmp.shutdown = shutdown;
7898
7899 /* First up, preserve all files */
7900 out_flush();
7901 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7902
7903 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007904 verb_msg((char_u *)_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
7906# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
7907 /* Now see if we can ask about unsaved files */
7908 if (shutdown && !fast && gui.in_use)
7909 /* Need to interact with user, but need SM's permission */
7910 SmcInteractRequest(smc_conn, SmDialogError,
7911 xsmp_handle_interaction, client_data);
7912 else
7913# endif
7914 {
7915 /* Can stop the cycle here */
7916 SmcSaveYourselfDone(smc_conn, True);
7917 xsmp.save_yourself = False;
7918 }
7919}
7920
7921
7922/*
7923 * Callback to warn us of imminent death.
7924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007926xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 xsmp_close();
7929
7930 /* quit quickly leaving swapfiles for modified buffers behind */
7931 getout_preserve_modified(0);
7932}
7933
7934
7935/*
7936 * Callback to tell us that save-yourself has completed.
7937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007939xsmp_save_complete(
7940 SmcConn smc_conn UNUSED,
7941 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 xsmp.save_yourself = False;
7944}
7945
7946
7947/*
7948 * Callback to tell us that an instigated shutdown was cancelled
7949 * (maybe even by us)
7950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007952xsmp_shutdown_cancelled(
7953 SmcConn smc_conn,
7954 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 if (xsmp.save_yourself)
7957 SmcSaveYourselfDone(smc_conn, True);
7958 xsmp.save_yourself = False;
7959 xsmp.shutdown = False;
7960}
7961
7962
7963/*
7964 * Callback to tell us that a new ICE connection has been established.
7965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007967xsmp_ice_connection(
7968 IceConn iceConn,
7969 IcePointer clientData UNUSED,
7970 Bool opening,
7971 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 /* Intercept creation of ICE connection fd */
7974 if (opening)
7975 {
7976 xsmp_icefd = IceConnectionNumber(iceConn);
7977 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7978 }
7979}
7980
7981
7982/* Handle any ICE processing that's required; return FAIL if SM lost */
7983 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007984xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 Bool rep;
7987
7988 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7989 == IceProcessMessagesIOError)
7990 {
7991 /* Lost ICE */
7992 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007993 verb_msg((char_u *)_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 xsmp_close();
7995 return FAIL;
7996 }
7997 else
7998 return OK;
7999}
8000
8001static int dummy;
8002
8003/* Set up X Session Management Protocol */
8004 void
8005xsmp_init(void)
8006{
8007 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 SmcCallbacks smcallbacks;
8009#if 0
8010 SmPropValue smname;
8011 SmProp smnameprop;
8012 SmProp *smprops[1];
8013#endif
8014
8015 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008016 verb_msg((char_u *)_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 xsmp.save_yourself = xsmp.shutdown = False;
8019
8020 /* Set up SM callbacks - must have all, even if they're not used */
8021 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8022 smcallbacks.save_yourself.client_data = NULL;
8023 smcallbacks.die.callback = xsmp_die;
8024 smcallbacks.die.client_data = NULL;
8025 smcallbacks.save_complete.callback = xsmp_save_complete;
8026 smcallbacks.save_complete.client_data = NULL;
8027 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8028 smcallbacks.shutdown_cancelled.client_data = NULL;
8029
8030 /* Set up a watch on ICE connection creations. The "dummy" argument is
8031 * apparently required for FreeBSD (we get a BUS error when using NULL). */
8032 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8033 {
8034 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008035 verb_msg((char_u *)_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 return;
8037 }
8038
8039 /* Create an SM connection */
8040 xsmp.smcconn = SmcOpenConnection(
8041 NULL,
8042 NULL,
8043 SmProtoMajor,
8044 SmProtoMinor,
8045 SmcSaveYourselfProcMask | SmcDieProcMask
8046 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8047 &smcallbacks,
8048 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008049 &xsmp.clientid,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 sizeof(errorstring),
8051 errorstring);
8052 if (xsmp.smcconn == NULL)
8053 {
8054 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008055
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008057 {
8058 vim_snprintf(errorreport, sizeof(errorreport),
8059 _("XSMP SmcOpenConnection failed: %s"), errorstring);
8060 verb_msg((char_u *)errorreport);
8061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 return;
8063 }
8064 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8065
8066#if 0
8067 /* ID ourselves */
8068 smname.value = "vim";
8069 smname.length = 3;
8070 smnameprop.name = "SmProgram";
8071 smnameprop.type = "SmARRAY8";
8072 smnameprop.num_vals = 1;
8073 smnameprop.vals = &smname;
8074
8075 smprops[0] = &smnameprop;
8076 SmcSetProperties(xsmp.smcconn, 1, smprops);
8077#endif
8078}
8079
8080
8081/* Shut down XSMP comms. */
8082 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 if (xsmp_icefd != -1)
8086 {
8087 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008088 if (xsmp.clientid != NULL)
8089 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008090 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 xsmp_icefd = -1;
8092 }
8093}
8094#endif /* USE_XSMP */
8095
8096
8097#ifdef EBCDIC
8098/* Translate character to its CTRL- value */
8099char CtrlTable[] =
8100{
8101/* 00 - 5E */
8102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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,
8108/* ^ */ 0x1E,
8109/* - */ 0x1F,
8110/* 61 - 6C */
8111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8112/* _ */ 0x1F,
8113/* 6E - 80 */
8114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8115/* a */ 0x01,
8116/* b */ 0x02,
8117/* c */ 0x03,
8118/* d */ 0x37,
8119/* e */ 0x2D,
8120/* f */ 0x2E,
8121/* g */ 0x2F,
8122/* h */ 0x16,
8123/* i */ 0x05,
8124/* 8A - 90 */
8125 0, 0, 0, 0, 0, 0, 0,
8126/* j */ 0x15,
8127/* k */ 0x0B,
8128/* l */ 0x0C,
8129/* m */ 0x0D,
8130/* n */ 0x0E,
8131/* o */ 0x0F,
8132/* p */ 0x10,
8133/* q */ 0x11,
8134/* r */ 0x12,
8135/* 9A - A1 */
8136 0, 0, 0, 0, 0, 0, 0, 0,
8137/* s */ 0x13,
8138/* t */ 0x3C,
8139/* u */ 0x3D,
8140/* v */ 0x32,
8141/* w */ 0x26,
8142/* x */ 0x18,
8143/* y */ 0x19,
8144/* z */ 0x3F,
8145/* AA - AC */
8146 0, 0, 0,
8147/* [ */ 0x27,
8148/* AE - BC */
8149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8150/* ] */ 0x1D,
8151/* BE - C0 */ 0, 0, 0,
8152/* A */ 0x01,
8153/* B */ 0x02,
8154/* C */ 0x03,
8155/* D */ 0x37,
8156/* E */ 0x2D,
8157/* F */ 0x2E,
8158/* G */ 0x2F,
8159/* H */ 0x16,
8160/* I */ 0x05,
8161/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8162/* J */ 0x15,
8163/* K */ 0x0B,
8164/* L */ 0x0C,
8165/* M */ 0x0D,
8166/* N */ 0x0E,
8167/* O */ 0x0F,
8168/* P */ 0x10,
8169/* Q */ 0x11,
8170/* R */ 0x12,
8171/* DA - DF */ 0, 0, 0, 0, 0, 0,
8172/* \ */ 0x1C,
8173/* E1 */ 0,
8174/* S */ 0x13,
8175/* T */ 0x3C,
8176/* U */ 0x3D,
8177/* V */ 0x32,
8178/* W */ 0x26,
8179/* X */ 0x18,
8180/* Y */ 0x19,
8181/* Z */ 0x3F,
8182/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8184};
8185
8186char MetaCharTable[]=
8187{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8188 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8189 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8190 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8191 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8192};
8193
8194
8195/* TODO: Use characters NOT numbers!!! */
8196char CtrlCharTable[]=
8197{/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
8198 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8199 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8200 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8201 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8202};
8203
8204
8205#endif