blob: 9162f5bf8a0810eb2bcb6cfd610625dc8ad694ad [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * OS/2 port by Paul Slootman
5 * VMS merge by Zoltan Arpadffy
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
12/*
13 * os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
14 * Also for OS/2, using the excellent EMX package!!!
15 * Also for BeOS and Atari MiNT.
16 *
17 * A lot of this file was originally written by Juergen Weigert and later
18 * changed beyond recognition.
19 */
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#include "vim.h"
22
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023#ifdef FEAT_MZSCHEME
24# include "if_mzsch.h"
25#endif
26
Bram Moolenaar0f873732019-12-05 20:28:46 +010027#include "os_unixx.h" // unix includes for os_unix.c only
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29#ifdef USE_XSMP
30# include <X11/SM/SMlib.h>
31#endif
32
Bram Moolenaar588ebeb2008-05-07 17:09:24 +000033#ifdef HAVE_SELINUX
34# include <selinux/selinux.h>
35static int selinux_enabled = -1;
36#endif
37
Bram Moolenaar5bd32f42014-04-02 14:05:38 +020038#ifdef HAVE_SMACK
39# include <attr/xattr.h>
40# include <linux/xattr.h>
41# ifndef SMACK_LABEL_LEN
42# define SMACK_LABEL_LEN 1024
43# endif
44#endif
45
Bram Moolenaar643b6142018-09-12 20:29:09 +020046#ifdef __BEOS__
Bram Moolenaar311d9822007-02-27 15:48:28 +000047# undef select
Bram Moolenaar643b6142018-09-12 20:29:09 +020048# define select beos_select
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
Bram Moolenaara2442432007-04-26 14:26:37 +000051#ifdef __CYGWIN__
Bram Moolenaar4f974752019-02-17 17:44:42 +010052# ifndef MSWIN
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000053# include <cygwin/version.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010054# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
55 // for cygwin_conv_path()
Bram Moolenaar693e40c2013-02-26 14:56:42 +010056# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
57# define WIN32_LEAN_AND_MEAN
58# include <windows.h>
59# include "winclip.pro"
60# endif
Bram Moolenaara2442432007-04-26 14:26:37 +000061# endif
62#endif
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_MOUSE_GPM
65# include <gpm.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010066// <linux/keyboard.h> contains defines conflicting with "keymap.h",
67// I just copied relevant defines here. A cleaner solution would be to put gpm
68// code into separate file and include there linux/keyboard.h
69// #include <linux/keyboard.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000070# define KG_SHIFT 0
71# define KG_CTRL 2
72# define KG_ALT 3
73# define KG_ALTGR 1
74# define KG_SHIFTL 4
75# define KG_SHIFTR 5
76# define KG_CTRLL 6
77# define KG_CTRLR 7
78# define KG_CAPSSHIFT 8
79
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void gpm_close(void);
81static int gpm_open(void);
82static int mch_gpm_process(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083#endif
84
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000085#ifdef FEAT_SYSMOUSE
86# include <sys/consio.h>
87# include <sys/fbio.h>
88
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010089static int sysmouse_open(void);
90static void sysmouse_close(void);
Bram Moolenaard99df422016-01-29 23:20:40 +010091static RETSIGTYPE sig_sysmouse SIGPROTOARG;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000092#endif
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * end of autoconf section. To be extended...
96 */
97
Bram Moolenaar0f873732019-12-05 20:28:46 +010098// Are the following #ifdefs still required? And why? Is that for X11?
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
101# ifdef SIGWINCH
102# undef SIGWINCH
103# endif
104# ifdef TIOCGWINSZ
105# undef TIOCGWINSZ
106# endif
107#endif
108
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# define SIGWINCH SIGWINDOW
111#endif
112
113#ifdef FEAT_X11
114# include <X11/Xlib.h>
115# include <X11/Xutil.h>
116# include <X11/Xatom.h>
117# ifdef FEAT_XCLIPBOARD
118# include <X11/Intrinsic.h>
119# include <X11/Shell.h>
120# include <X11/StringDefs.h>
121static Widget xterm_Shell = (Widget)0;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static void clip_update(void);
123static void xterm_update(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# endif
125
126# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
127Window x11_window = 0;
128# endif
129Display *x11_display = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
131
Bram Moolenaar5c3128e2020-05-11 20:54:42 +0200132static int ignore_sigtstp = FALSE;
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#ifdef FEAT_TITLE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int get_x11_title(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137static char_u *oldtitle = NULL;
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200138static volatile sig_atomic_t oldtitle_outdated = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +0200139static int unix_did_set_title = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static char_u *oldicon = NULL;
141static int did_set_icon = FALSE;
142#endif
143
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void may_core_dump(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar205b8862011-09-07 15:04:31 +0200146#ifdef HAVE_UNION_WAIT
147typedef union wait waitstatus;
148#else
149typedef int waitstatus;
150#endif
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200151static int WaitForChar(long msec, int *interrupted, int ignore_input);
152static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
Bram Moolenaar4ffa0702013-12-11 17:12:37 +0100153#if defined(__BEOS__) || defined(VMS)
Bram Moolenaarcda77642016-06-04 13:32:35 +0200154int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#else
Bram Moolenaarcda77642016-06-04 13:32:35 +0200156static int RealWaitForChar(int, long, int *, int *interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158
159#ifdef FEAT_XCLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int do_xterm_trace(void);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100161# define XT_TRACE_DELAY 50 // delay for xterm tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void handle_resize(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
166#if defined(SIGWINCH)
Bram Moolenaard99df422016-01-29 23:20:40 +0100167static RETSIGTYPE sig_winch SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#if defined(SIGINT)
Bram Moolenaard99df422016-01-29 23:20:40 +0100170static RETSIGTYPE catch_sigint SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#if defined(SIGPWR)
Bram Moolenaard99df422016-01-29 23:20:40 +0100173static RETSIGTYPE catch_sigpwr SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175#if defined(SIGALRM) && defined(FEAT_X11) \
176 && defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
177# define SET_SIG_ALARM
Bram Moolenaard99df422016-01-29 23:20:40 +0100178static RETSIGTYPE sig_alarm SIGPROTOARG;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100179// volatile because it is used in signal handler sig_alarm().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200180static volatile sig_atomic_t sig_alarm_called;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
Bram Moolenaard99df422016-01-29 23:20:40 +0100182static RETSIGTYPE deathtrap SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static void catch_int_signal(void);
185static void set_signals(void);
186static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +0200187#ifdef HAVE_SIGPROCMASK
188# define SIGSET_DECL(set) sigset_t set;
189# define BLOCK_SIGNALS(set) block_signals(set)
190# define UNBLOCK_SIGNALS(set) unblock_signals(set)
191#else
192# define SIGSET_DECL(set)
193# define BLOCK_SIGNALS(set) do { /**/ } while (0)
194# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
195#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100196static int have_wildcard(int, char_u **);
197static int have_dollars(int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100199static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201#ifndef SIG_ERR
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000202# define SIG_ERR ((RETSIGTYPE (*)())-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#endif
204
Bram Moolenaar0f873732019-12-05 20:28:46 +0100205// volatile because it is used in signal handler sig_winch().
Bram Moolenaar8e82c052018-08-21 19:47:48 +0200206static volatile sig_atomic_t do_resize = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static char_u *extra_shell_arg = NULL;
208static int show_shell_mess = TRUE;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100209// volatile because it is used in signal handler deathtrap().
210static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
211// volatile because it is used in signal handler deathtrap().
212static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +0100214#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
215static int dont_check_job_ended = 0;
216#endif
217
Bram Moolenaar0f873732019-12-05 20:28:46 +0100218static int curr_tmode = TMODE_COOK; // contains current terminal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220#ifdef USE_XSMP
221typedef struct
222{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100223 SmcConn smcconn; // The SM connection ID
224 IceConn iceconn; // The ICE connection ID
225 char *clientid; // The client ID for the current smc session
226 Bool save_yourself; // If we're in the middle of a save_yourself
227 Bool shutdown; // If we're in shutdown mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228} xsmp_config_T;
229
230static xsmp_config_T xsmp;
231#endif
232
233#ifdef SYS_SIGLIST_DECLARED
234/*
235 * I have seen
236 * extern char *_sys_siglist[NSIG];
237 * on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
238 * that describe the signals. That is nearly what we want here. But
239 * autoconf does only check for sys_siglist (without the underscore), I
240 * do not want to change everything today.... jw.
Bram Moolenaar3f7d0902016-11-12 21:13:42 +0100241 * This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 */
243#endif
244
245static struct signalinfo
246{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100247 int sig; // Signal number, eg. SIGSEGV etc
248 char *name; // Signal name (not char_u!).
249 char deadly; // Catch as a deadly signal?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250} signal_info[] =
251{
252#ifdef SIGHUP
253 {SIGHUP, "HUP", TRUE},
254#endif
255#ifdef SIGQUIT
256 {SIGQUIT, "QUIT", TRUE},
257#endif
258#ifdef SIGILL
259 {SIGILL, "ILL", TRUE},
260#endif
261#ifdef SIGTRAP
262 {SIGTRAP, "TRAP", TRUE},
263#endif
264#ifdef SIGABRT
265 {SIGABRT, "ABRT", TRUE},
266#endif
267#ifdef SIGEMT
268 {SIGEMT, "EMT", TRUE},
269#endif
270#ifdef SIGFPE
271 {SIGFPE, "FPE", TRUE},
272#endif
273#ifdef SIGBUS
274 {SIGBUS, "BUS", TRUE},
275#endif
Bram Moolenaar75676462013-01-30 14:55:42 +0100276#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100277 // MzScheme uses SEGV in its garbage collector
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 {SIGSEGV, "SEGV", TRUE},
279#endif
280#ifdef SIGSYS
281 {SIGSYS, "SYS", TRUE},
282#endif
283#ifdef SIGALRM
Bram Moolenaar0f873732019-12-05 20:28:46 +0100284 {SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
286#ifdef SIGTERM
287 {SIGTERM, "TERM", TRUE},
288#endif
Bram Moolenaarb292a2a2011-02-09 18:47:40 +0100289#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 {SIGVTALRM, "VTALRM", TRUE},
291#endif
Bram Moolenaar02f07e02008-03-12 12:17:28 +0000292#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100293 // MzScheme uses SIGPROF for its own needs; On Linux with profiling
294 // this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {SIGPROF, "PROF", TRUE},
296#endif
297#ifdef SIGXCPU
298 {SIGXCPU, "XCPU", TRUE},
299#endif
300#ifdef SIGXFSZ
301 {SIGXFSZ, "XFSZ", TRUE},
302#endif
303#ifdef SIGUSR1
304 {SIGUSR1, "USR1", TRUE},
305#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000306#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100307 // Used for sysmouse handling
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 {SIGUSR2, "USR2", TRUE},
309#endif
310#ifdef SIGINT
311 {SIGINT, "INT", FALSE},
312#endif
313#ifdef SIGWINCH
314 {SIGWINCH, "WINCH", FALSE},
315#endif
316#ifdef SIGTSTP
317 {SIGTSTP, "TSTP", FALSE},
318#endif
319#ifdef SIGPIPE
320 {SIGPIPE, "PIPE", FALSE},
321#endif
322 {-1, "Unknown!", FALSE}
323};
324
Bram Moolenaar25724922009-07-14 15:38:41 +0000325 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100326mch_chdir(char *path)
Bram Moolenaar25724922009-07-14 15:38:41 +0000327{
328 if (p_verbose >= 5)
329 {
330 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100331 smsg("chdir(%s)", path);
Bram Moolenaar25724922009-07-14 15:38:41 +0000332 verbose_leave();
333 }
334# ifdef VMS
335 return chdir(vms_fixfilename(path));
336# else
337 return chdir(path);
338# endif
339}
340
Bram Moolenaar0f873732019-12-05 20:28:46 +0100341// Why is NeXT excluded here (and not in os_unixx.h)?
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100342#if defined(ECHOE) && defined(ICANON) \
343 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
344 && !defined(__NeXT__)
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200345# define NEW_TTY_SYSTEM
346#endif
347
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000348/*
Bram Moolenaard23a8232018-02-10 18:45:26 +0100349 * Write s[len] to the screen (stdout).
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100352mch_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353{
Bram Moolenaar42335f52018-09-13 15:33:43 +0200354 vim_ignored = (int)write(1, (char *)s, len);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100355 if (p_wd) // Unix is too fast, slow down a bit more
Bram Moolenaar8fdd7212016-03-26 19:41:48 +0100356 RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357}
358
359/*
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100360 * Function passed to inchar_loop() to handle window resizing.
361 * If "check_only" is TRUE: Return whether there was a resize.
362 * If "check_only" is FALSE: Deal with the window resized.
363 */
364 static int
365resize_func(int check_only)
366{
367 if (check_only)
368 return do_resize;
369 while (do_resize)
370 handle_resize();
371 return FALSE;
372}
373
374/*
Bram Moolenaarc2a27c32007-12-01 16:19:33 +0000375 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 * Get a characters from the keyboard.
377 * Return the number of characters that are available.
378 * If wtime == 0 do not wait for characters.
379 * If wtime == n wait a short time for characters.
380 * If wtime == -1 wait forever for characters.
381 */
382 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100383mch_inchar(
384 char_u *buf,
385 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100386 long wtime, // don't use "time", MIPS cannot handle it
Bram Moolenaar05540972016-01-30 20:31:25 +0100387 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388{
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100389 return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
390 WaitForChar, resize_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391}
392
393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100394handle_resize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
396 do_resize = FALSE;
397 shell_resized();
398}
399
400/*
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200401 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
403 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100404mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200406 return WaitForChar(0L, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407}
408
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200409#if defined(FEAT_TERMINAL) || defined(PROTO)
410/*
411 * Check for any pending input or messages.
412 */
413 int
414mch_check_messages(void)
415{
416 return WaitForChar(0L, NULL, TRUE);
417}
418#endif
419
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
421# ifdef HAVE_SYS_RESOURCE_H
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +0000422# include <sys/resource.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423# endif
424# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
425# include <sys/sysctl.h>
426# endif
427# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
428# include <sys/sysinfo.h>
429# endif
Bram Moolenaar362dc332018-03-05 21:59:37 +0100430# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100431# include <mach/mach_host.h>
432# include <mach/mach_port.h>
Bram Moolenaar988615f2018-02-27 17:58:20 +0100433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435/*
Bram Moolenaar914572a2007-05-01 11:37:47 +0000436 * Return total amount of memory available in Kbyte.
437 * Doesn't change when memory has been allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100440mch_total_mem(int special UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 long_u mem = 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +0100443 long_u shiftright = 10; // how much to shift "mem" right for Kbyte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
Bram Moolenaar362dc332018-03-05 21:59:37 +0100445# ifdef MACOS_X
Bram Moolenaar988615f2018-02-27 17:58:20 +0100446 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100447 // Mac (Darwin) way of getting the amount of RAM available
Bram Moolenaar988615f2018-02-27 17:58:20 +0100448 mach_port_t host = mach_host_self();
449 kern_return_t kret;
450# ifdef HOST_VM_INFO64
451 struct vm_statistics64 vm_stat;
452 natural_t count = HOST_VM_INFO64_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
Bram Moolenaar988615f2018-02-27 17:58:20 +0100454 kret = host_statistics64(host, HOST_VM_INFO64,
455 (host_info64_t)&vm_stat, &count);
456# else
457 struct vm_statistics vm_stat;
458 natural_t count = HOST_VM_INFO_COUNT;
459
460 kret = host_statistics(host, HOST_VM_INFO,
461 (host_info_t)&vm_stat, &count);
462# endif
463 if (kret == KERN_SUCCESS)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100464 // get the amount of user memory by summing each usage
Bram Moolenaar988615f2018-02-27 17:58:20 +0100465 mem = (long_u)(vm_stat.free_count + vm_stat.active_count
466 + vm_stat.inactive_count
467# ifdef MAC_OS_X_VERSION_10_9
468 + vm_stat.compressor_page_count
469# endif
Bram Moolenaar62b7f6a2018-03-22 21:44:07 +0100470 ) * sysconf(_SC_PAGESIZE);
Bram Moolenaar988615f2018-02-27 17:58:20 +0100471 mach_port_deallocate(mach_task_self(), host);
472 }
473# endif
474
475# ifdef HAVE_SYSCTL
476 if (mem == 0)
477 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100478 // BSD way of getting the amount of RAM available.
Bram Moolenaar988615f2018-02-27 17:58:20 +0100479 int mib[2];
480 size_t len = sizeof(long_u);
481# ifdef HW_USERMEM64
482 long_u physmem;
483# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // sysctl() may return 32 bit or 64 bit, accept both
Bram Moolenaar988615f2018-02-27 17:58:20 +0100485 union {
486 int_u u32;
487 long_u u64;
488 } physmem;
489# endif
490
491 mib[0] = CTL_HW;
492# ifdef HW_USERMEM64
493 mib[1] = HW_USERMEM64;
494# else
495 mib[1] = HW_USERMEM;
496# endif
497 if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
498 {
499# ifdef HW_USERMEM64
500 mem = (long_u)physmem;
501# else
502 if (len == sizeof(physmem.u64))
503 mem = (long_u)physmem.u64;
504 else
505 mem = (long_u)physmem.u32;
506# endif
507 }
508 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200511# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (mem == 0)
513 {
514 struct sysinfo sinfo;
515
Bram Moolenaar0f873732019-12-05 20:28:46 +0100516 // Linux way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (sysinfo(&sinfo) == 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000518 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200519# ifdef HAVE_SYSINFO_MEM_UNIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100520 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000521 while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
522 {
523 sinfo.mem_unit = sinfo.mem_unit >> 1;
524 --shiftright;
525 }
526 mem = sinfo.totalram * sinfo.mem_unit;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200527# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 mem = sinfo.totalram;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200529# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200534# ifdef HAVE_SYSCONF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (mem == 0)
536 {
537 long pagesize, pagecount;
538
Bram Moolenaar0f873732019-12-05 20:28:46 +0100539 // Solaris way of getting amount of RAM available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 pagesize = sysconf(_SC_PAGESIZE);
541 pagecount = sysconf(_SC_PHYS_PAGES);
542 if (pagesize > 0 && pagecount > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000543 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100544 // avoid overflow as much as possible
Bram Moolenaar914572a2007-05-01 11:37:47 +0000545 while (shiftright > 0 && (pagesize & 1) == 0)
546 {
Bram Moolenaar3d27a452007-05-10 17:44:18 +0000547 pagesize = (long_u)pagesize >> 1;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000548 --shiftright;
549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 mem = (long_u)pagesize * pagecount;
Bram Moolenaar914572a2007-05-01 11:37:47 +0000551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar0f873732019-12-05 20:28:46 +0100555 // Return the minimum of the physical memory and the user limit, because
556 // using more than the user limit may cause Vim to be terminated.
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200557# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {
559 struct rlimit rlp;
560
561 if (getrlimit(RLIMIT_DATA, &rlp) == 0
562 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200563# ifdef RLIM_INFINITY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 && rlp.rlim_cur != RLIM_INFINITY
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200565# endif
Bram Moolenaar914572a2007-05-01 11:37:47 +0000566 && ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 )
Bram Moolenaar914572a2007-05-01 11:37:47 +0000568 {
569 mem = (long_u)rlp.rlim_cur;
570 shiftright = 10;
571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 if (mem > 0)
Bram Moolenaar914572a2007-05-01 11:37:47 +0000576 return mem >> shiftright;
577 return (long_u)0x1fffff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578}
579#endif
580
581 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100582mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 int old_tmode;
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000585#ifdef FEAT_MZSCHEME
Bram Moolenaar0f873732019-12-05 20:28:46 +0100586 long total = msec; // remember original value
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 if (ignoreinput)
590 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100591 // Go to cooked mode without echo, to allow SIGINT interrupting us
592 // here. But we don't want QUIT to kill us (CTRL-\ used in a
593 // shell may produce SIGQUIT).
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000594 in_mch_delay = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 old_tmode = curr_tmode;
596 if (curr_tmode == TMODE_RAW)
597 settmode(TMODE_SLEEP);
598
599 /*
600 * Everybody sleeps in a different way...
601 * Prefer nanosleep(), some versions of usleep() can only sleep up to
602 * one second.
603 */
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000604#ifdef FEAT_MZSCHEME
605 do
606 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100607 // if total is large enough, wait by portions in p_mzq
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000608 if (total > p_mzq)
609 msec = p_mzq;
610 else
611 msec = total;
612 total -= msec;
613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef HAVE_NANOSLEEP
615 {
616 struct timespec ts;
617
618 ts.tv_sec = msec / 1000;
619 ts.tv_nsec = (msec % 1000) * 1000000;
620 (void)nanosleep(&ts, NULL);
621 }
622#else
623# ifdef HAVE_USLEEP
624 while (msec >= 1000)
625 {
626 usleep((unsigned int)(999 * 1000));
627 msec -= 999;
628 }
629 usleep((unsigned int)(msec * 1000));
630# else
631# ifndef HAVE_SELECT
632 poll(NULL, 0, (int)msec);
633# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
635 struct timeval tv;
636
637 tv.tv_sec = msec / 1000;
638 tv.tv_usec = (msec % 1000) * 1000;
639 /*
640 * NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
641 * a patch from Sun to fix this. Reported by Gunnar Pedersen.
642 */
643 select(0, NULL, NULL, NULL, &tv);
644 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100645# endif // HAVE_SELECT
646# endif // HAVE_NANOSLEEP
647#endif // HAVE_USLEEP
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000648#ifdef FEAT_MZSCHEME
649 }
650 while (total > 0);
651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 settmode(old_tmode);
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000654 in_mch_delay = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200657 WaitForChar(msec, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658}
659
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000660#if defined(HAVE_STACK_LIMIT) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 || (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
662# define HAVE_CHECK_STACK_GROWTH
663/*
664 * Support for checking for an almost-out-of-stack-space situation.
665 */
666
667/*
668 * Return a pointer to an item on the stack. Used to find out if the stack
669 * grows up or down.
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671static int stack_grows_downwards;
672
673/*
674 * Find out if the stack grows upwards or downwards.
675 * "p" points to a variable on the stack of the caller.
676 */
677 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100678check_stack_growth(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
680 int i;
681
682 stack_grows_downwards = (p > (char *)&i);
683}
684#endif
685
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000686#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687static char *stack_limit = NULL;
688
689#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
690# include <pthread.h>
691# include <pthread_np.h>
692#endif
693
694/*
695 * Find out until how var the stack can grow without getting into trouble.
696 * Called when starting up and when switching to the signal stack in
697 * deathtrap().
698 */
699 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100700get_stack_limit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 struct rlimit rlp;
703 int i;
704 long lim;
705
Bram Moolenaar0f873732019-12-05 20:28:46 +0100706 // Set the stack limit to 15/16 of the allowable size. Skip this when the
707 // limit doesn't fit in a long (rlim_cur might be "long long").
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (getrlimit(RLIMIT_STACK, &rlp) == 0
709 && rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
710# ifdef RLIM_INFINITY
711 && rlp.rlim_cur != RLIM_INFINITY
712# endif
713 )
714 {
715 lim = (long)rlp.rlim_cur;
716#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
717 {
718 pthread_attr_t attr;
719 size_t size;
720
Bram Moolenaar0f873732019-12-05 20:28:46 +0100721 // On FreeBSD the initial thread always has a fixed stack size, no
722 // matter what the limits are set to. Normally it's 1 Mbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 pthread_attr_init(&attr);
724 if (pthread_attr_get_np(pthread_self(), &attr) == 0)
725 {
726 pthread_attr_getstacksize(&attr, &size);
727 if (lim > (long)size)
728 lim = (long)size;
729 }
730 pthread_attr_destroy(&attr);
731 }
732#endif
733 if (stack_grows_downwards)
734 {
735 stack_limit = (char *)((long)&i - (lim / 16L * 15L));
736 if (stack_limit >= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100737 // overflow, set to 1/16 of current stack position
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 stack_limit = (char *)((long)&i / 16L);
739 }
740 else
741 {
742 stack_limit = (char *)((long)&i + (lim / 16L * 15L));
743 if (stack_limit <= (char *)&i)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100744 stack_limit = NULL; // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746 }
747}
748
749/*
750 * Return FAIL when running out of stack space.
751 * "p" must point to any variable local to the caller that's on the stack.
752 */
753 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100754mch_stackcheck(char *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
756 if (stack_limit != NULL)
757 {
758 if (stack_grows_downwards)
759 {
760 if (p < stack_limit)
761 return FAIL;
762 }
763 else if (p > stack_limit)
764 return FAIL;
765 }
766 return OK;
767}
768#endif
769
770#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
771/*
772 * Support for using the signal stack.
773 * This helps when we run out of stack space, which causes a SIGSEGV. The
774 * signal handler then must run on another stack, since the normal stack is
775 * completely full.
776 */
777
778#ifndef SIGSTKSZ
Bram Moolenaar0f873732019-12-05 20:28:46 +0100779# define SIGSTKSZ 8000 // just a guess of how much stack is needed...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781
782# ifdef HAVE_SIGALTSTACK
Bram Moolenaar0f873732019-12-05 20:28:46 +0100783static stack_t sigstk; // for sigaltstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100785static struct sigstack sigstk; // for sigstack()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# endif
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788static char *signal_stack;
789
790 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100791init_signal_stack(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 if (signal_stack != NULL)
794 {
795# ifdef HAVE_SIGALTSTACK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796# ifdef HAVE_SS_BASE
797 sigstk.ss_base = signal_stack;
798# else
799 sigstk.ss_sp = signal_stack;
800# endif
801 sigstk.ss_size = SIGSTKSZ;
802 sigstk.ss_flags = 0;
803 (void)sigaltstack(&sigstk, NULL);
804# else
805 sigstk.ss_sp = signal_stack;
806 if (stack_grows_downwards)
807 sigstk.ss_sp += SIGSTKSZ - 1;
808 sigstk.ss_onstack = 0;
809 (void)sigstack(&sigstk, NULL);
810# endif
811 }
812}
813#endif
814
815/*
Bram Moolenaar76243bd2009-03-02 01:47:02 +0000816 * We need correct prototypes for a signal function, otherwise mean compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 * will barf when the second argument to signal() is ``wrong''.
818 * Let me try it with a few tricky defines from my own osdef.h (jw).
819 */
820#if defined(SIGWINCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 static RETSIGTYPE
822sig_winch SIGDEFARG(sigarg)
823{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100824 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
826 do_resize = TRUE;
827 SIGRETURN;
828}
829#endif
830
831#if defined(SIGINT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 static RETSIGTYPE
833catch_sigint SIGDEFARG(sigarg)
834{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100835 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
837 got_int = TRUE;
838 SIGRETURN;
839}
840#endif
841
842#if defined(SIGPWR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 static RETSIGTYPE
844catch_sigpwr SIGDEFARG(sigarg)
845{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100846 // this is not required on all systems, but it doesn't hurt anybody
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000847 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 /*
849 * I'm not sure we get the SIGPWR signal when the system is really going
850 * down or when the batteries are almost empty. Just preserve the swap
851 * files and don't exit, that can't do any harm.
852 */
853 ml_sync_all(FALSE, FALSE);
854 SIGRETURN;
855}
856#endif
857
858#ifdef SET_SIG_ALARM
859/*
860 * signal function for alarm().
861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 static RETSIGTYPE
863sig_alarm SIGDEFARG(sigarg)
864{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100865 // doesn't do anything, just to break a system call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 sig_alarm_called = TRUE;
867 SIGRETURN;
868}
869#endif
870
Bram Moolenaaredce7422019-01-20 18:39:30 +0100871#if (defined(HAVE_SETJMP_H) \
872 && ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
873 || defined(FEAT_LIBCALL))) \
874 || defined(PROTO)
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100875# define USING_SETJMP 1
Bram Moolenaaredce7422019-01-20 18:39:30 +0100876
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100877// argument to SETJMP()
878static JMP_BUF lc_jump_env;
879
880# ifdef SIGHASARG
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100881// Caught signal number, 0 when no signal was caught; used for mch_libcall().
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100882// Volatile because it is used in signal handlers.
883static volatile sig_atomic_t lc_signal;
884# endif
885
886// TRUE when lc_jump_env is valid.
887// Volatile because it is used in signal handler deathtrap().
888static volatile sig_atomic_t lc_active INIT(= FALSE);
889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890/*
891 * A simplistic version of setjmp() that only allows one level of using.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100892 * Used to protect areas where we could crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 * Don't call twice before calling mch_endjmp()!.
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100894 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 * Usage:
896 * mch_startjmp();
897 * if (SETJMP(lc_jump_env) != 0)
898 * {
899 * mch_didjmp();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100900 * emsg("crash!");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * }
902 * else
903 * {
904 * do_the_work;
905 * mch_endjmp();
906 * }
907 * Note: Can't move SETJMP() here, because a function calling setjmp() must
908 * not return before the saved environment is used.
909 * Returns OK for normal return, FAIL when the protected code caused a
910 * problem and LONGJMP() was used.
911 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100913mch_startjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100915# ifdef SIGHASARG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 lc_signal = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 lc_active = TRUE;
919}
920
Bram Moolenaar113e1072019-01-20 15:30:40 +0100921 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100922mch_endjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 lc_active = FALSE;
925}
926
Bram Moolenaar113e1072019-01-20 15:30:40 +0100927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100928mch_didjmp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarb7a7e032018-12-28 17:01:59 +0100931 // On FreeBSD the signal stack has to be reset after using siglongjmp(),
932 // otherwise catching the signal only works once.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 init_signal_stack();
934# endif
935}
936#endif
937
938/*
939 * This function handles deadly signals.
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200940 * It tries to preserve any swap files and exit properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * (partly from Elvis).
Bram Moolenaarbec9c202013-09-05 21:41:39 +0200942 * NOTE: Avoid unsafe functions, such as allocating memory, they can result in
943 * a deadlock.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 */
945 static RETSIGTYPE
946deathtrap SIGDEFARG(sigarg)
947{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100948 static int entered = 0; // count the number of times we got here.
949 // Note: when memory has been corrupted
950 // this may get an arbitrary value!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951#ifdef SIGHASARG
952 int i;
953#endif
954
Bram Moolenaarb2148f52019-01-20 23:43:57 +0100955#if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 /*
957 * Catch a crash in protected code.
958 * Restores the environment saved in lc_jump_env, which looks like
959 * SETJMP() returns 1.
960 */
961 if (lc_active)
962 {
963# if defined(SIGHASARG)
964 lc_signal = sigarg;
965# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100966 lc_active = FALSE; // don't jump again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 LONGJMP(lc_jump_env, 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100968 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970#endif
971
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000972#ifdef SIGHASARG
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000973# ifdef SIGQUIT
Bram Moolenaar0f873732019-12-05 20:28:46 +0100974 // While in mch_delay() we go to cooked mode to allow a CTRL-C to
975 // interrupt us. But in cooked mode we may also get SIGQUIT, e.g., when
976 // pressing CTRL-\, but we don't want Vim to exit then.
Bram Moolenaarae0f2ca2008-02-10 21:25:55 +0000977 if (in_mch_delay && sigarg == SIGQUIT)
978 SIGRETURN;
979# endif
980
Bram Moolenaar0f873732019-12-05 20:28:46 +0100981 // When SIGHUP, SIGQUIT, etc. are blocked: postpone the effect and return
982 // here. This avoids that a non-reentrant function is interrupted, e.g.,
983 // free(). Calling free() again may then cause a crash.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000984 if (entered == 0
985 && (0
986# ifdef SIGHUP
987 || sigarg == SIGHUP
988# endif
989# ifdef SIGQUIT
990 || sigarg == SIGQUIT
991# endif
992# ifdef SIGTERM
993 || sigarg == SIGTERM
994# endif
995# ifdef SIGPWR
996 || sigarg == SIGPWR
997# endif
998# ifdef SIGUSR1
999 || sigarg == SIGUSR1
1000# endif
1001# ifdef SIGUSR2
1002 || sigarg == SIGUSR2
1003# endif
1004 )
Bram Moolenaar1f28b072005-07-12 22:42:41 +00001005 && !vim_handle_signal(sigarg))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001006 SIGRETURN;
1007#endif
1008
Bram Moolenaar0f873732019-12-05 20:28:46 +01001009 // Remember how often we have been called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 ++entered;
1011
Bram Moolenaar0f873732019-12-05 20:28:46 +01001012 // Executing autocommands is likely to use more stack space than we have
1013 // available in the signal stack.
Bram Moolenaare429e702016-06-10 19:49:14 +02001014 block_autocmds();
Bram Moolenaare429e702016-06-10 19:49:14 +02001015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01001017 // Set the v:dying variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 set_vim_var_nr(VV_DYING, (long)entered);
1019#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001020 v_dying = entered;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001022#ifdef HAVE_STACK_LIMIT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001023 // Since we are now using the signal stack, need to reset the stack
1024 // limit. Otherwise using a regexp will fail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 get_stack_limit();
1026#endif
1027
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001028#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01001029 // This is for opening gdb the moment Vim crashes.
1030 // You need to manually adjust the file name and Vim executable name.
1031 // Suggested by SungHyun Nam.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001032 {
1033# define VI_GDB_FILE "/tmp/vimgdb"
1034# define VIM_NAME "/usr/bin/vim"
1035 FILE *fp = fopen(VI_GDB_FILE, "w");
1036 if (fp)
1037 {
1038 fprintf(fp,
1039 "file %s\n"
1040 "attach %d\n"
1041 "set height 1000\n"
1042 "bt full\n"
1043 , VIM_NAME, getpid());
1044 fclose(fp);
1045 system("xterm -e gdb -x "VI_GDB_FILE);
1046 unlink(VI_GDB_FILE);
1047 }
1048 }
1049#endif
1050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051#ifdef SIGHASARG
Bram Moolenaar0f873732019-12-05 20:28:46 +01001052 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 for (i = 0; signal_info[i].sig != -1; i++)
1054 if (sigarg == signal_info[i].sig)
1055 break;
1056 deadly_signal = sigarg;
1057#endif
1058
Bram Moolenaar0f873732019-12-05 20:28:46 +01001059 full_screen = FALSE; // don't write message to the GUI, it might be
1060 // part of the problem...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 /*
1062 * If something goes wrong after entering here, we may get here again.
1063 * When this happens, give a message and try to exit nicely (resetting the
1064 * terminal mode, etc.)
1065 * When this happens twice, just exit, don't even try to give a message,
1066 * stack may be corrupt or something weird.
1067 * When this still happens again (or memory was corrupted in such a way
1068 * that "entered" was clobbered) use _exit(), don't try freeing resources.
1069 */
1070 if (entered >= 3)
1071 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001072 reset_signals(); // don't catch any signals anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 may_core_dump();
1074 if (entered >= 4)
1075 _exit(8);
1076 exit(7);
1077 }
1078 if (entered == 2)
1079 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001080 // No translation, it may call malloc().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001081 OUT_STR("Vim: Double signal, exiting\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 out_flush();
1083 getout(1);
1084 }
1085
Bram Moolenaar0f873732019-12-05 20:28:46 +01001086 // No translation, it may call malloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef SIGHASARG
Bram Moolenaar69212b12020-05-10 14:14:03 +02001088 sprintf((char *)IObuff, "Vim: Caught deadly signal %s\r\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 signal_info[i].name);
1090#else
Bram Moolenaar69212b12020-05-10 14:14:03 +02001091 sprintf((char *)IObuff, "Vim: Caught deadly signal\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001093
Bram Moolenaar0f873732019-12-05 20:28:46 +01001094 // Preserve files and exit. This sets the really_exiting flag to prevent
1095 // calling free().
Bram Moolenaarbec9c202013-09-05 21:41:39 +02001096 preserve_exit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaar0f873732019-12-05 20:28:46 +01001098 // NOTREACHED
Bram Moolenaare429e702016-06-10 19:49:14 +02001099
Bram Moolenaar009b2592004-10-24 19:18:58 +00001100#ifdef NBDEBUG
1101 reset_signals();
1102 may_core_dump();
1103 abort();
1104#endif
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 SIGRETURN;
1107}
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02001110 * Invoked after receiving SIGCONT. We don't know what happened while
1111 * sleeping, deal with part of that.
1112 */
1113 static void
1114after_sigcont(void)
1115{
1116# ifdef FEAT_TITLE
1117 // Don't change "oldtitle" in a signal handler, set a flag to obtain it
1118 // again later.
1119 oldtitle_outdated = TRUE;
1120# endif
1121 settmode(TMODE_RAW);
1122 need_check_timestamps = TRUE;
1123 did_check_timestamps = FALSE;
1124}
1125
1126#if defined(SIGCONT)
1127static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001128static volatile sig_atomic_t in_mch_suspend = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001129
1130/*
1131 * With multi-threading, suspending might not work immediately. Catch the
1132 * SIGCONT signal, which will be used as an indication whether the suspending
1133 * has been done or not.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001134 *
1135 * On Linux, signal is not always handled immediately either.
1136 * See https://bugs.launchpad.net/bugs/291373
Bram Moolenaar2e310482018-08-21 13:09:10 +02001137 * Probably because the signal is handled in another thread.
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001138 *
Bram Moolenaarb292a2a2011-02-09 18:47:40 +01001139 * volatile because it is used in signal handler sigcont_handler().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 */
Bram Moolenaar8e82c052018-08-21 19:47:48 +02001141static volatile sig_atomic_t sigcont_received;
Bram Moolenaarf1883472018-08-20 21:58:57 +02001142static RETSIGTYPE sigcont_handler SIGPROTOARG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144/*
1145 * signal handler for SIGCONT
1146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 static RETSIGTYPE
1148sigcont_handler SIGDEFARG(sigarg)
1149{
Bram Moolenaar2e310482018-08-21 13:09:10 +02001150 if (in_mch_suspend)
1151 {
1152 sigcont_received = TRUE;
1153 }
1154 else
1155 {
1156 // We didn't suspend ourselves, assume we were stopped by a SIGSTOP
1157 // signal (which can't be intercepted) and get a SIGCONT. Need to get
1158 // back to a sane mode. We should redraw, but we can't really do that
1159 // in a signal handler, do a redraw later.
1160 after_sigcont();
1161 redraw_later(CLEAR);
1162 cursor_on_force();
1163 out_flush();
1164 }
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 SIGRETURN;
1167}
1168#endif
1169
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02001170#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001171# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001172static void *clip_star_save = NULL;
1173static void *clip_plus_save = NULL;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001174# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001175
1176/*
1177 * Called when Vim is going to sleep or execute a shell command.
1178 * We can't respond to requests for the X selections. Lose them, otherwise
1179 * other applications will hang. But first copy the text to cut buffer 0.
1180 */
1181 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001182loose_clipboard(void)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001183{
1184 if (clip_star.owned || clip_plus.owned)
1185 {
1186 x11_export_final_selection();
1187 if (clip_star.owned)
1188 clip_lose_selection(&clip_star);
1189 if (clip_plus.owned)
1190 clip_lose_selection(&clip_plus);
1191 if (x11_display != NULL)
1192 XFlush(x11_display);
1193 }
1194}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001195
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001196# ifdef USE_SYSTEM
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001197/*
1198 * Save clipboard text to restore later.
1199 */
1200 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001201save_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001202{
1203 if (clip_star.owned)
1204 clip_star_save = get_register('*', TRUE);
1205 if (clip_plus.owned)
1206 clip_plus_save = get_register('+', TRUE);
1207}
1208
1209/*
1210 * Restore clipboard text if no one own the X selection.
1211 */
1212 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001213restore_clipboard(void)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001214{
1215 if (clip_star_save != NULL)
1216 {
1217 if (!clip_gen_owner_exists(&clip_star))
1218 put_register('*', clip_star_save);
1219 else
1220 free_register(clip_star_save);
1221 clip_star_save = NULL;
1222 }
1223 if (clip_plus_save != NULL)
1224 {
1225 if (!clip_gen_owner_exists(&clip_plus))
1226 put_register('+', clip_plus_save);
1227 else
1228 free_register(clip_plus_save);
1229 clip_plus_save = NULL;
1230 }
1231}
Bram Moolenaar090cfc12013-03-19 12:35:42 +01001232# endif
Bram Moolenaar62b42182010-09-21 22:09:37 +02001233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
1236 * If the machine has job control, use it to suspend the program,
1237 * otherwise fake it by starting a new shell.
1238 */
1239 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001240mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001242 if (ignore_sigtstp)
1243 return;
1244
Bram Moolenaar0f873732019-12-05 20:28:46 +01001245 // BeOS does have SIGTSTP, but it doesn't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#if defined(SIGTSTP) && !defined(__BEOS__)
Bram Moolenaar2e310482018-08-21 13:09:10 +02001247 in_mch_suspend = TRUE;
1248
Bram Moolenaar0f873732019-12-05 20:28:46 +01001249 out_flush(); // needed to make cursor visible on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 settmode(TMODE_COOK);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001251 out_flush(); // needed to disable mouse on some systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar62b42182010-09-21 22:09:37 +02001254 loose_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001256# if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 sigcont_received = FALSE;
1258# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001259
Bram Moolenaar0f873732019-12-05 20:28:46 +01001260 kill(0, SIGTSTP); // send ourselves a STOP signal
Bram Moolenaar2e310482018-08-21 13:09:10 +02001261
1262# if defined(SIGCONT)
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001263 /*
1264 * Wait for the SIGCONT signal to be handled. It generally happens
Bram Moolenaar2e310482018-08-21 13:09:10 +02001265 * immediately, but somehow not all the time, probably because it's handled
1266 * in another thread. Do not call pause() because there would be race
1267 * condition which would hang Vim if signal happened in between the test of
1268 * sigcont_received and the call to pause(). If signal is not yet received,
1269 * sleep 0, 1, 2, 3 ms. Don't bother waiting further if signal is not
1270 * received after 1+2+3 ms (not expected to happen).
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001271 */
1272 {
Bram Moolenaar262735e2009-07-14 10:20:22 +00001273 long wait_time;
Bram Moolenaar2e310482018-08-21 13:09:10 +02001274
Bram Moolenaar262735e2009-07-14 10:20:22 +00001275 for (wait_time = 0; !sigcont_received && wait_time <= 3L; wait_time++)
Bram Moolenaar262735e2009-07-14 10:20:22 +00001276 mch_delay(wait_time, FALSE);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00001277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278# endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001279 in_mch_suspend = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
Bram Moolenaar2e310482018-08-21 13:09:10 +02001281 after_sigcont();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#else
1283 suspend_shell();
1284#endif
1285}
1286
1287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001288mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 Columns = 80;
1291 Rows = 24;
1292
1293 out_flush();
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001294
1295#ifdef SIGTSTP
1296 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1297 // that indicates the shell (or program) that launched us does not support
1298 // tty job control and thus we should ignore that signal. If invoked as a
1299 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1300 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR);
1301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 set_signals();
Bram Moolenaardf177f62005-02-22 08:39:57 +00001303
Bram Moolenaar56718732006-03-15 22:53:57 +00001304#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001305 mac_conv_init();
1306#endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001307#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
1308 win_clip_init();
1309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310}
1311
1312 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001313set_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315#if defined(SIGWINCH)
1316 /*
1317 * WINDOW CHANGE signal is handled with sig_winch().
1318 */
1319 signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#ifdef SIGTSTP
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001323 // See mch_init() for the conditions under which we ignore SIGTSTP.
1324 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#endif
Bram Moolenaar2e310482018-08-21 13:09:10 +02001326#if defined(SIGCONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 signal(SIGCONT, sigcont_handler);
1328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 /*
1330 * We want to ignore breaking of PIPEs.
1331 */
1332#ifdef SIGPIPE
1333 signal(SIGPIPE, SIG_IGN);
1334#endif
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#ifdef SIGINT
Bram Moolenaardf177f62005-02-22 08:39:57 +00001337 catch_int_signal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
1339
1340 /*
1341 * Ignore alarm signals (Perl's alarm() generates it).
1342 */
1343#ifdef SIGALRM
1344 signal(SIGALRM, SIG_IGN);
1345#endif
1346
1347 /*
1348 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1349 * work will be lost.
1350 */
1351#ifdef SIGPWR
1352 signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
1353#endif
1354
1355 /*
1356 * Arrange for other signals to gracefully shutdown Vim.
1357 */
1358 catch_signals(deathtrap, SIG_ERR);
1359
1360#if defined(FEAT_GUI) && defined(SIGHUP)
1361 /*
1362 * When the GUI is running, ignore the hangup signal.
1363 */
1364 if (gui.in_use)
1365 signal(SIGHUP, SIG_IGN);
1366#endif
1367}
1368
Bram Moolenaardf177f62005-02-22 08:39:57 +00001369#if defined(SIGINT) || defined(PROTO)
1370/*
1371 * Catch CTRL-C (only works while in Cooked mode).
1372 */
1373 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001374catch_int_signal(void)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001375{
1376 signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
1377}
1378#endif
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001381reset_signals(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 catch_signals(SIG_DFL, SIG_DFL);
Bram Moolenaar2e310482018-08-21 13:09:10 +02001384#if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001385 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 signal(SIGCONT, SIG_DFL);
1387#endif
1388}
1389
1390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001391catch_signals(
1392 RETSIGTYPE (*func_deadly)(),
1393 RETSIGTYPE (*func_other)())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 int i;
1396
1397 for (i = 0; signal_info[i].sig != -1; i++)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (signal_info[i].deadly)
1400 {
1401#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
1402 struct sigaction sa;
1403
Bram Moolenaar0f873732019-12-05 20:28:46 +01001404 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 sa.sa_handler = func_deadly;
1406 sigemptyset(&sa.sa_mask);
1407# if defined(__linux__) && defined(_REENTRANT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001408 // On Linux, with glibc compiled for kernel 2.2, there is a bug in
1409 // thread handling in combination with using the alternate stack:
1410 // pthread library functions try to use the stack pointer to
1411 // identify the current thread, causing a SEGV signal, which
1412 // recursively calls deathtrap() and hangs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 sa.sa_flags = 0;
1414# else
1415 sa.sa_flags = SA_ONSTACK;
1416# endif
1417 sigaction(signal_info[i].sig, &sa, NULL);
1418#else
1419# if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGVEC)
1420 struct sigvec sv;
1421
Bram Moolenaar0f873732019-12-05 20:28:46 +01001422 // Setup to use the alternate stack for the signal function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 sv.sv_handler = func_deadly;
1424 sv.sv_mask = 0;
1425 sv.sv_flags = SV_ONSTACK;
1426 sigvec(signal_info[i].sig, &sv, NULL);
1427# else
1428 signal(signal_info[i].sig, func_deadly);
1429# endif
1430#endif
1431 }
1432 else if (func_other != SIG_ERR)
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001433 {
1434 // Deal with non-deadly signals.
1435#ifdef SIGTSTP
1436 signal(signal_info[i].sig,
1437 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1438 ? SIG_IGN : func_other);
1439#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 signal(signal_info[i].sig, func_other);
Bram Moolenaar5c3128e2020-05-11 20:54:42 +02001441#endif
1442 }
1443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444}
1445
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001446#ifdef HAVE_SIGPROCMASK
1447 static void
1448block_signals(sigset_t *set)
1449{
1450 sigset_t newset;
1451 int i;
1452
1453 sigemptyset(&newset);
1454
1455 for (i = 0; signal_info[i].sig != -1; i++)
1456 sigaddset(&newset, signal_info[i].sig);
1457
Bram Moolenaar2e310482018-08-21 13:09:10 +02001458# if defined(SIGCONT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001459 // SIGCONT isn't in the list, because its default action is ignore
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001460 sigaddset(&newset, SIGCONT);
1461# endif
1462
1463 sigprocmask(SIG_BLOCK, &newset, set);
1464}
1465
1466 static void
1467unblock_signals(sigset_t *set)
1468{
1469 sigprocmask(SIG_SETMASK, set, NULL);
1470}
1471#endif
1472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001474 * Handling of SIGHUP, SIGQUIT and SIGTERM:
Bram Moolenaar9e1d2832007-05-06 12:51:41 +00001475 * "when" == a signal: when busy, postpone and return FALSE, otherwise
1476 * return TRUE
1477 * "when" == SIGNAL_BLOCK: Going to be busy, block signals
1478 * "when" == SIGNAL_UNBLOCK: Going to wait, unblock signals, use postponed
Bram Moolenaar67c53842010-05-22 18:28:27 +02001479 * signal
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001480 * Returns TRUE when Vim should exit.
1481 */
1482 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001483vim_handle_signal(int sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001484{
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001485 static int got_signal = 0;
1486 static int blocked = TRUE;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001487
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001488 switch (sig)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001489 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001490 case SIGNAL_BLOCK: blocked = TRUE;
1491 break;
1492
1493 case SIGNAL_UNBLOCK: blocked = FALSE;
1494 if (got_signal != 0)
1495 {
1496 kill(getpid(), got_signal);
1497 got_signal = 0;
1498 }
1499 break;
1500
1501 default: if (!blocked)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001502 return TRUE; // exit!
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001503 got_signal = sig;
1504#ifdef SIGPWR
1505 if (sig != SIGPWR)
1506#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001507 got_int = TRUE; // break any loops
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001508 break;
1509 }
1510 return FALSE;
1511}
1512
1513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * Check_win checks whether we have an interactive stdout.
1515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001517mch_check_win(int argc UNUSED, char **argv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (isatty(1))
1520 return OK;
1521 return FAIL;
1522}
1523
1524/*
1525 * Return TRUE if the input comes from a terminal, FALSE otherwise.
1526 */
1527 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001528mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 if (isatty(read_cmd_fd))
1531 return TRUE;
1532 return FALSE;
1533}
1534
1535#ifdef FEAT_X11
1536
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001537# if defined(ELAPSED_TIMEVAL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 && (defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE))
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
1541 * Give a message about the elapsed time for opening the X window.
1542 */
1543 static void
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001544xopen_message(long elapsed_msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001546 smsg(_("Opening the X display took %ld msec"), elapsed_msec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547}
1548# endif
1549#endif
1550
1551#if defined(FEAT_X11) && (defined(FEAT_TITLE) || defined(FEAT_XCLIPBOARD))
1552/*
1553 * A few functions shared by X11 title and clipboard code.
1554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
1556static int got_x_error = FALSE;
1557
1558/*
1559 * X Error handler, otherwise X just exits! (very rude) -- webb
1560 */
1561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001562x_error_handler(Display *dpy, XErrorEvent *error_event)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar843ee412004-06-30 16:16:41 +00001564 XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 STRCAT(IObuff, _("\nVim: Got X error\n"));
1566
Bram Moolenaarb1062eb2020-05-09 16:11:33 +02001567 // In the GUI we cannot print a message and continue, because no X calls
1568 // are allowed here (causes my system to hang). Silently continuing seems
1569 // like the best alternative. Do preserve files, in case we crash.
1570 ml_sync_all(FALSE, FALSE);
1571
1572#ifdef FEAT_GUI
1573 if (!gui.in_use)
1574#endif
1575 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar0f873732019-12-05 20:28:46 +01001577 return 0; // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578}
1579
1580/*
1581 * Another X Error handler, just used to check for errors.
1582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001584x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 got_x_error = TRUE;
1587 return 0;
1588}
1589
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001590/*
1591 * Return TRUE when connection to the X server is desired.
1592 */
1593 static int
1594x_connect_to_server(void)
1595{
1596 // No point in connecting if we are exiting or dying.
1597 if (exiting || v_dying)
1598 return FALSE;
1599
1600#if defined(FEAT_CLIENTSERVER)
1601 if (x_force_connect)
1602 return TRUE;
1603#endif
1604 if (x_no_connect)
1605 return FALSE;
1606
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001607 // Check for a match with "exclude:" from 'clipboard'.
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001608 if (clip_exclude_prog != NULL)
1609 {
Bram Moolenaara8bfa172018-12-29 22:28:46 +01001610 // Just in case we get called recursively, return FALSE. This could
1611 // happen if vpeekc() is used while executing the prog and it causes a
1612 // related callback to be invoked.
1613 if (regprog_in_use(clip_exclude_prog))
1614 return FALSE;
1615
Bram Moolenaarc0c75492018-12-29 11:03:23 +01001616 if (vim_regexec_prog(&clip_exclude_prog, FALSE, T_NAME, (colnr_T)0))
1617 return FALSE;
1618 }
1619 return TRUE;
1620}
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaarb2148f52019-01-20 23:43:57 +01001623# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
1625 * An X IO Error handler, used to catch error while opening the display.
1626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001628x_IOerror_check(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001630 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 LONGJMP(lc_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001632# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001633 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635}
1636# endif
1637
1638/*
1639 * An X IO Error handler, used to catch terminal errors.
1640 */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001641static int xterm_dpy_retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001644x_IOerror_handler(Display *dpy UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 xterm_dpy = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001647 xterm_dpy_retry_count = 5; // Try reconnecting five times
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 x11_window = 0;
1649 x11_display = NULL;
1650 xterm_Shell = (Widget)0;
1651
Bram Moolenaar0f873732019-12-05 20:28:46 +01001652 // This function should not return, it causes exit(). Longjump instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 LONGJMP(x_jump_env, 1);
Bram Moolenaar1eed5322019-02-26 17:03:54 +01001654# if defined(VMS) || defined(__CYGWIN__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001655 return 0; // avoid the compiler complains about missing return value
Bram Moolenaarb4990bf2010-02-11 18:19:38 +01001656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657}
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001658
1659/*
1660 * If the X11 connection was lost try to restore it.
1661 * Helps when the X11 server was stopped and restarted while Vim was inactive
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01001662 * (e.g. through tmux).
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001663 */
1664 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001665may_restore_clipboard(void)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001666{
Bram Moolenaar01e51e52018-12-29 13:09:46 +01001667 // No point in restoring the connecting if we are exiting or dying.
1668 if (!exiting && !v_dying && xterm_dpy_retry_count > 0)
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001669 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001670 --xterm_dpy_retry_count;
Bram Moolenaar527a6782014-12-17 17:59:31 +01001671
1672# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01001673 // This has been reported to avoid Vim getting stuck.
Bram Moolenaar527a6782014-12-17 17:59:31 +01001674 if (app_context != (XtAppContext)NULL)
1675 {
1676 XtDestroyApplicationContext(app_context);
1677 app_context = (XtAppContext)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001678 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaar527a6782014-12-17 17:59:31 +01001679 }
1680# endif
1681
Bram Moolenaarb1e26502014-11-19 18:48:46 +01001682 setup_term_clip();
1683 get_x11_title(FALSE);
1684 }
1685}
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001686
1687 void
1688ex_xrestore(exarg_T *eap)
1689{
1690 if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1691 {
1692 if (xterm_display_allocated)
1693 vim_free(xterm_display);
1694 xterm_display = (char *)vim_strsave(eap->arg);
1695 xterm_display_allocated = TRUE;
1696 }
1697 smsg(_("restoring display %s"), xterm_display == NULL
Bram Moolenaar0c5c3fa2019-11-30 22:38:16 +01001698 ? (char *)mch_getenv((char_u *)"DISPLAY") : xterm_display);
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001699
1700 clear_xterm_clip();
1701 x11_window = 0;
1702 xterm_dpy_retry_count = 5; // Try reconnecting five times
1703 may_restore_clipboard();
1704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
1706
1707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 * Test if "dpy" and x11_window are valid by getting the window title.
1709 * I don't actually want it yet, so there may be a simpler call to use, but
1710 * this will cause the error handler x_error_check() to be called if anything
1711 * is wrong, such as the window pointer being invalid (as can happen when the
1712 * user changes his DISPLAY, but not his WINDOWID) -- webb
1713 */
1714 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001715test_x11_window(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 int (*old_handler)();
1718 XTextProperty text_prop;
1719
1720 old_handler = XSetErrorHandler(x_error_check);
1721 got_x_error = FALSE;
1722 if (XGetWMName(dpy, x11_window, &text_prop))
1723 XFree((void *)text_prop.value);
1724 XSync(dpy, False);
1725 (void)XSetErrorHandler(old_handler);
1726
1727 if (p_verbose > 0 && got_x_error)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001728 verb_msg(_("Testing the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 return (got_x_error ? FAIL : OK);
1731}
1732#endif
1733
1734#ifdef FEAT_TITLE
1735
1736#ifdef FEAT_X11
1737
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001738static int get_x11_thing(int get_title, int test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740/*
1741 * try to get x11 window and display
1742 *
1743 * return FAIL for failure, OK otherwise
1744 */
1745 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001746get_x11_windis(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 char *winid;
1749 static int result = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001750#define XD_NONE 0 // x11_display not set here
1751#define XD_HERE 1 // x11_display opened here
1752#define XD_GUI 2 // x11_display used from gui.dpy
1753#define XD_XTERM 3 // x11_display used from xterm_dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 static int x11_display_from = XD_NONE;
1755 static int did_set_error_handler = FALSE;
1756
1757 if (!did_set_error_handler)
1758 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001759 // X just exits if it finds an error otherwise!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 (void)XSetErrorHandler(x_error_handler);
1761 did_set_error_handler = TRUE;
1762 }
1763
Bram Moolenaar9372a112005-12-06 19:59:18 +00001764#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (gui.in_use)
1766 {
1767 /*
1768 * If the X11 display was opened here before, for the window where Vim
1769 * was started, close that one now to avoid a memory leak.
1770 */
1771 if (x11_display_from == XD_HERE && x11_display != NULL)
1772 {
1773 XCloseDisplay(x11_display);
1774 x11_display_from = XD_NONE;
1775 }
1776 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
1777 {
1778 x11_display_from = XD_GUI;
1779 return OK;
1780 }
1781 x11_display = NULL;
1782 return FAIL;
1783 }
1784 else if (x11_display_from == XD_GUI)
1785 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001786 // GUI must have stopped somehow, clear x11_display
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 x11_window = 0;
1788 x11_display = NULL;
1789 x11_display_from = XD_NONE;
1790 }
1791#endif
1792
Bram Moolenaar0f873732019-12-05 20:28:46 +01001793 // When started with the "-X" argument, don't try connecting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (!x_connect_to_server())
1795 return FAIL;
1796
1797 /*
1798 * If WINDOWID not set, should try another method to find out
1799 * what the current window number is. The only code I know for
1800 * this is very complicated.
1801 * We assume that zero is invalid for WINDOWID.
1802 */
1803 if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
1804 x11_window = (Window)atol(winid);
1805
1806#ifdef FEAT_XCLIPBOARD
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02001807 if (xterm_dpy == x11_display)
1808 // x11_display may have been set to xterm_dpy elsewhere
1809 x11_display_from = XD_XTERM;
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (xterm_dpy != NULL && x11_window != 0)
1812 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001813 // We may have checked it already, but Gnome terminal can move us to
1814 // another window, so we need to check every time.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00001815 if (x11_display_from != XD_XTERM)
1816 {
1817 /*
1818 * If the X11 display was opened here before, for the window where
1819 * Vim was started, close that one now to avoid a memory leak.
1820 */
1821 if (x11_display_from == XD_HERE && x11_display != NULL)
1822 XCloseDisplay(x11_display);
1823 x11_display = xterm_dpy;
1824 x11_display_from = XD_XTERM;
1825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (test_x11_window(x11_display) == FAIL)
1827 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001828 // probably bad $WINDOWID
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 x11_window = 0;
1830 x11_display = NULL;
1831 x11_display_from = XD_NONE;
1832 return FAIL;
1833 }
1834 return OK;
1835 }
1836#endif
1837
1838 if (x11_window == 0 || x11_display == NULL)
1839 result = -1;
1840
Bram Moolenaar0f873732019-12-05 20:28:46 +01001841 if (result != -1) // Have already been here and set this
1842 return result; // Don't do all these X calls again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844 if (x11_window != 0 && x11_display == NULL)
1845 {
1846#ifdef SET_SIG_ALARM
1847 RETSIGTYPE (*sig_save)();
1848#endif
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001849#ifdef ELAPSED_FUNC
1850 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 if (p_verbose > 0)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001853 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#endif
1855
1856#ifdef SET_SIG_ALARM
1857 /*
1858 * Opening the Display may hang if the DISPLAY setting is wrong, or
1859 * the network connection is bad. Set an alarm timer to get out.
1860 */
1861 sig_alarm_called = FALSE;
1862 sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
1863 (RETSIGTYPE (*)())sig_alarm);
1864 alarm(2);
1865#endif
1866 x11_display = XOpenDisplay(NULL);
1867
1868#ifdef SET_SIG_ALARM
1869 alarm(0);
1870 signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
1871 if (p_verbose > 0 && sig_alarm_called)
Bram Moolenaar563bbea2019-01-22 21:45:40 +01001872 verb_msg(_("Opening the X display timed out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873#endif
1874 if (x11_display != NULL)
1875 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001876# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001878 {
1879 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01001880 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00001881 verbose_leave();
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883# endif
1884 if (test_x11_window(x11_display) == FAIL)
1885 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001886 // Maybe window id is bad
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 x11_window = 0;
1888 XCloseDisplay(x11_display);
1889 x11_display = NULL;
1890 }
1891 else
1892 x11_display_from = XD_HERE;
1893 }
1894 }
1895 if (x11_window == 0 || x11_display == NULL)
1896 return (result = FAIL);
Bram Moolenaar727c8762010-10-20 19:17:48 +02001897
1898# ifdef FEAT_EVAL
1899 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
1900# endif
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 return (result = OK);
1903}
1904
1905/*
1906 * Determine original x11 Window Title
1907 */
1908 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001909get_x11_title(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
Bram Moolenaar47136d72004-10-12 20:02:24 +00001911 return get_x11_thing(TRUE, test_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912}
1913
1914/*
1915 * Determine original x11 Window icon
1916 */
1917 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001918get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int retval = FALSE;
1921
1922 retval = get_x11_thing(FALSE, test_only);
1923
Bram Moolenaar0f873732019-12-05 20:28:46 +01001924 // could not get old icon, use terminal name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (oldicon == NULL && !test_only)
1926 {
1927 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001928 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00001930 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932
1933 return retval;
1934}
1935
1936 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001937get_x11_thing(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001938 int get_title, // get title string
Bram Moolenaar05540972016-01-30 20:31:25 +01001939 int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
1941 XTextProperty text_prop;
1942 int retval = FALSE;
1943 Status status;
1944
1945 if (get_x11_windis() == OK)
1946 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001947 // Get window/icon name if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (get_title)
1949 status = XGetWMName(x11_display, x11_window, &text_prop);
1950 else
1951 status = XGetWMIconName(x11_display, x11_window, &text_prop);
1952
1953 /*
1954 * If terminal is xterm, then x11_window may be a child window of the
1955 * outer xterm window that actually contains the window/icon name, so
1956 * keep traversing up the tree until a window with a title/icon is
1957 * found.
1958 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001959 // Previously this was only done for xterm and alike. I don't see a
Bram Moolenaar0f873732019-12-05 20:28:46 +01001960 // reason why it would fail for other terminal emulators.
1961 // if (term_is_xterm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 Window root;
1964 Window parent;
1965 Window win = x11_window;
1966 Window *children;
1967 unsigned int num_children;
1968
1969 while (!status || text_prop.value == NULL)
1970 {
1971 if (!XQueryTree(x11_display, win, &root, &parent, &children,
1972 &num_children))
1973 break;
1974 if (children)
1975 XFree((void *)children);
1976 if (parent == root || parent == 0)
1977 break;
1978
1979 win = parent;
1980 if (get_title)
1981 status = XGetWMName(x11_display, win, &text_prop);
1982 else
1983 status = XGetWMIconName(x11_display, win, &text_prop);
1984 }
1985 }
1986 if (status && text_prop.value != NULL)
1987 {
1988 retval = TRUE;
1989 if (!test_only)
1990 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01001991 if (get_title)
1992 vim_free(oldtitle);
1993 else
1994 vim_free(oldicon);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001995 if (text_prop.encoding == XA_STRING && !has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (get_title)
1998 oldtitle = vim_strsave((char_u *)text_prop.value);
1999 else
2000 oldicon = vim_strsave((char_u *)text_prop.value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 else
2003 {
2004 char **cl;
2005 Status transform_status;
2006 int n = 0;
2007
2008 transform_status = XmbTextPropertyToTextList(x11_display,
2009 &text_prop,
2010 &cl, &n);
2011 if (transform_status >= Success && n > 0 && cl[0])
2012 {
2013 if (get_title)
2014 oldtitle = vim_strsave((char_u *) cl[0]);
2015 else
2016 oldicon = vim_strsave((char_u *) cl[0]);
2017 XFreeStringList(cl);
2018 }
2019 else
2020 {
2021 if (get_title)
2022 oldtitle = vim_strsave((char_u *)text_prop.value);
2023 else
2024 oldicon = vim_strsave((char_u *)text_prop.value);
2025 }
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 XFree((void *)text_prop.value);
2029 }
2030 }
2031 return retval;
2032}
2033
Bram Moolenaar0f873732019-12-05 20:28:46 +01002034// Xutf8 functions are not available on older systems. Note that on some
2035// systems X_HAVE_UTF8_STRING may be defined in a header file but
2036// Xutf8SetWMProperties() is not in the X11 library. Configure checks for
2037// that and defines HAVE_XUTF8SETWMPROPERTIES.
Bram Moolenaara12a1612019-01-24 16:39:02 +01002038#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaarcbc246a2014-10-11 14:47:26 +02002039# if X_HAVE_UTF8_STRING && HAVE_XUTF8SETWMPROPERTIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040# define USE_UTF8_STRING
2041# endif
2042#endif
2043
2044/*
2045 * Set x11 Window Title
2046 *
2047 * get_x11_windis() must be called before this and have returned OK
2048 */
2049 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002050set_x11_title(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002052 // XmbSetWMProperties() and Xutf8SetWMProperties() should use a STRING
2053 // when possible, COMPOUND_TEXT otherwise. COMPOUND_TEXT isn't
2054 // supported everywhere and STRING doesn't work for multi-byte titles.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055#ifdef USE_UTF8_STRING
2056 if (enc_utf8)
2057 Xutf8SetWMProperties(x11_display, x11_window, (const char *)title,
2058 NULL, NULL, 0, NULL, NULL, NULL);
2059 else
2060#endif
2061 {
2062#if XtSpecificationRelease >= 4
2063# ifdef FEAT_XFONTSET
2064 XmbSetWMProperties(x11_display, x11_window, (const char *)title,
2065 NULL, NULL, 0, NULL, NULL, NULL);
2066# else
2067 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002068 char *c_title = (char *)title;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
Bram Moolenaar0f873732019-12-05 20:28:46 +01002070 // directly from example 3-18 "basicwin" of Xlib Programming Manual
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002071 (void)XStringListToTextProperty(&c_title, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 XSetWMProperties(x11_display, x11_window, &text_prop,
2073 NULL, NULL, 0, NULL, NULL, NULL);
2074# endif
2075#else
2076 XStoreName(x11_display, x11_window, (char *)title);
2077#endif
2078 }
2079 XFlush(x11_display);
2080}
2081
2082/*
2083 * Set x11 Window icon
2084 *
2085 * get_x11_windis() must be called before this and have returned OK
2086 */
2087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002088set_x11_icon(char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002090 // See above for comments about using X*SetWMProperties().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#ifdef USE_UTF8_STRING
2092 if (enc_utf8)
2093 Xutf8SetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2094 NULL, 0, NULL, NULL, NULL);
2095 else
2096#endif
2097 {
2098#if XtSpecificationRelease >= 4
2099# ifdef FEAT_XFONTSET
2100 XmbSetWMProperties(x11_display, x11_window, NULL, (const char *)icon,
2101 NULL, 0, NULL, NULL, NULL);
2102# else
2103 XTextProperty text_prop;
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002104 char *c_icon = (char *)icon;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
Bram Moolenaar9d75c832005-01-25 21:57:23 +00002106 (void)XStringListToTextProperty(&c_icon, 1, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 XSetWMProperties(x11_display, x11_window, NULL, &text_prop,
2108 NULL, 0, NULL, NULL, NULL);
2109# endif
2110#else
2111 XSetIconName(x11_display, x11_window, (char *)icon);
2112#endif
2113 }
2114 XFlush(x11_display);
2115}
2116
Bram Moolenaar0f873732019-12-05 20:28:46 +01002117#else // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002120get_x11_title(int test_only UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 return FALSE;
2123}
2124
2125 static int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002126get_x11_icon(int test_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 if (!test_only)
2129 {
2130 if (STRNCMP(T_NAME, "builtin_", 8) == 0)
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002131 oldicon = vim_strsave(T_NAME + 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 else
Bram Moolenaar20de1c22009-07-22 11:28:11 +00002133 oldicon = vim_strsave(T_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 return FALSE;
2136}
2137
Bram Moolenaar0f873732019-12-05 20:28:46 +01002138#endif // FEAT_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002141mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 return get_x11_title(TRUE);
2144}
2145
2146 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002147mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 return get_x11_icon(TRUE);
2150}
2151
2152/*
2153 * Set the window title and icon.
2154 */
2155 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002156mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157{
2158 int type = 0;
2159 static int recursive = 0;
2160
Bram Moolenaar0f873732019-12-05 20:28:46 +01002161 if (T_NAME == NULL) // no terminal name (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 return;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002163 if (title == NULL && icon == NULL) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return;
2165
Bram Moolenaar0f873732019-12-05 20:28:46 +01002166 // When one of the X11 functions causes a deadly signal, we get here again
2167 // recursively. Avoid hanging then (something is probably locked).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (recursive)
2169 return;
2170 ++recursive;
2171
2172 /*
2173 * if the window ID and the display is known, we may use X11 calls
2174 */
2175#ifdef FEAT_X11
2176 if (get_x11_windis() == OK)
2177 type = 1;
2178#else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002179# if defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2180 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (gui.in_use)
2182 type = 1;
2183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#endif
2185
2186 /*
Bram Moolenaarf82bac32010-07-25 22:30:20 +02002187 * Note: if "t_ts" is set, title is set with escape sequence rather
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 * than x11 calls, because the x11 calls don't always work
2189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if ((type || *T_TS != NUL) && title != NULL)
2191 {
Bram Moolenaard8f0cef2018-08-19 22:20:16 +02002192 if (oldtitle_outdated)
2193 {
2194 oldtitle_outdated = FALSE;
2195 VIM_CLEAR(oldtitle);
2196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (oldtitle == NULL
2198#ifdef FEAT_GUI
2199 && !gui.in_use
2200#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002201 ) // first call but not in GUI, save title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 (void)get_x11_title(FALSE);
2203
Bram Moolenaar0f873732019-12-05 20:28:46 +01002204 if (*T_TS != NUL) // it's OK if t_fs is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 term_settitle(title);
2206#ifdef FEAT_X11
2207 else
2208# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002209 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002211 set_x11_title(title); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002213#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2215 else
2216 gui_mch_settitle(title, icon);
2217#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002218 unix_did_set_title = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
2220
2221 if ((type || *T_CIS != NUL) && icon != NULL)
2222 {
2223 if (oldicon == NULL
2224#ifdef FEAT_GUI
2225 && !gui.in_use
2226#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002227 ) // first call, save icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 get_x11_icon(FALSE);
2229
2230 if (*T_CIS != NUL)
2231 {
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002232 out_str(T_CIS); // set icon start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 out_str_nf(icon);
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02002234 out_str(T_CIE); // set icon end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 out_flush();
2236 }
2237#ifdef FEAT_X11
2238 else
2239# ifdef FEAT_GUI_GTK
Bram Moolenaar0f873732019-12-05 20:28:46 +01002240 if (!gui.in_use) // don't do this if GTK+ is running
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002242 set_x11_icon(icon); // x11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#endif
2244 did_set_icon = TRUE;
2245 }
2246 --recursive;
2247}
2248
2249/*
2250 * Restore the window/icon title.
2251 * "which" is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +02002252 * SAVE_RESTORE_TITLE only restore title
2253 * SAVE_RESTORE_ICON only restore icon
2254 * SAVE_RESTORE_BOTH restore title and icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 */
2256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002257mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258{
Bram Moolenaardac13472019-09-16 21:06:21 +02002259 int do_push_pop = unix_did_set_title || did_set_icon;
Bram Moolenaare5c83282019-05-03 23:15:37 +02002260
Bram Moolenaar0f873732019-12-05 20:28:46 +01002261 // only restore the title or icon when it has been set
Bram Moolenaardac13472019-09-16 21:06:21 +02002262 mch_settitle(((which & SAVE_RESTORE_TITLE) && unix_did_set_title) ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 (oldtitle ? oldtitle : p_titleold) : NULL,
Bram Moolenaar40385db2018-08-07 22:31:44 +02002264 ((which & SAVE_RESTORE_ICON) && did_set_icon) ? oldicon : NULL);
2265
Bram Moolenaare5c83282019-05-03 23:15:37 +02002266 if (do_push_pop)
2267 {
2268 // pop and push from/to the stack
2269 term_pop_title(which);
2270 term_push_title(which);
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272}
2273
Bram Moolenaar0f873732019-12-05 20:28:46 +01002274#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
2276/*
2277 * Return TRUE if "name" looks like some xterm name.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002278 * Seiichi Sato mentioned that "mlterm" works like xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 */
2280 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002281vim_is_xterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 if (name == NULL)
2284 return FALSE;
2285 return (STRNICMP(name, "xterm", 5) == 0
2286 || STRNICMP(name, "nxterm", 6) == 0
2287 || STRNICMP(name, "kterm", 5) == 0
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002288 || STRNICMP(name, "mlterm", 6) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 || STRNICMP(name, "rxvt", 4) == 0
Bram Moolenaar995e4af2017-09-01 20:24:03 +02002290 || STRNICMP(name, "screen.xterm", 12) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 || STRCMP(name, "builtin_xterm") == 0);
2292}
2293
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002294#if defined(FEAT_MOUSE_XTERM) || defined(PROTO)
2295/*
2296 * Return TRUE if "name" appears to be that of a terminal
2297 * known to support the xterm-style mouse protocol.
2298 * Relies on term_is_xterm having been set to its correct value.
2299 */
2300 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002301use_xterm_like_mouse(char_u *name)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002302{
2303 return (name != NULL
Bram Moolenaare56132b2016-08-14 18:23:21 +02002304 && (term_is_xterm
2305 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002306 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaare56132b2016-08-14 18:23:21 +02002307 || STRICMP(name, "st") == 0
2308 || STRNICMP(name, "st-", 3) == 0
2309 || STRNICMP(name, "stterm", 6) == 0));
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00002310}
2311#endif
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313/*
2314 * Return non-zero when using an xterm mouse, according to 'ttymouse'.
2315 * Return 1 for "xterm".
2316 * Return 2 for "xterm2".
Bram Moolenaarc8427482011-10-20 21:09:35 +02002317 * Return 3 for "urxvt".
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002318 * Return 4 for "sgr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
2320 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002321use_xterm_mouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002323 if (ttym_flags == TTYM_SGR)
2324 return 4;
Bram Moolenaarc8427482011-10-20 21:09:35 +02002325 if (ttym_flags == TTYM_URXVT)
2326 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (ttym_flags == TTYM_XTERM2)
2328 return 2;
2329 if (ttym_flags == TTYM_XTERM)
2330 return 1;
2331 return 0;
2332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002335vim_is_iris(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 if (name == NULL)
2338 return FALSE;
2339 return (STRNICMP(name, "iris-ansi", 9) == 0
2340 || STRCMP(name, "builtin_iris-ansi") == 0);
2341}
2342
2343 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002344vim_is_vt300(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 if (name == NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002347 return FALSE; // actually all ANSI comp. terminals should be here
2348 // catch VT100 - VT5xx
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002349 return ((STRNICMP(name, "vt", 2) == 0
2350 && vim_strchr((char_u *)"12345", name[2]) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 || STRCMP(name, "builtin_vt320") == 0);
2352}
2353
2354/*
2355 * Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
2356 * This should include all windowed terminal emulators.
2357 */
2358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002359vim_is_fastterm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 if (name == NULL)
2362 return FALSE;
2363 if (vim_is_xterm(name) || vim_is_vt300(name) || vim_is_iris(name))
2364 return TRUE;
2365 return ( STRNICMP(name, "hpterm", 6) == 0
2366 || STRNICMP(name, "sun-cmd", 7) == 0
2367 || STRNICMP(name, "screen", 6) == 0
Bram Moolenaar0ba40702016-10-12 14:50:54 +02002368 || STRNICMP(name, "tmux", 4) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 || STRNICMP(name, "dtterm", 6) == 0);
2370}
2371
2372/*
2373 * Insert user name in s[len].
2374 * Return OK if a name found.
2375 */
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379#ifdef VMS
Bram Moolenaarffb8ab02005-09-07 21:15:32 +00002380 vim_strncpy(s, (char_u *)cuserid(NULL), len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return OK;
2382#else
2383 return mch_get_uname(getuid(), s, len);
2384#endif
2385}
2386
2387/*
2388 * Insert user name for "uid" in s[len].
2389 * Return OK if a name found.
2390 */
2391 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002392mch_get_uname(uid_t uid, char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
2395 struct passwd *pw;
2396
2397 if ((pw = getpwuid(uid)) != NULL
2398 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
2399 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002400 vim_strncpy(s, (char_u *)pw->pw_name, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return OK;
2402 }
2403#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002404 sprintf((char *)s, "%d", (int)uid); // assumes s is long enough
2405 return FAIL; // a number is not a name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406}
2407
2408/*
2409 * Insert host name is s[len].
2410 */
2411
2412#ifdef HAVE_SYS_UTSNAME_H
2413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002414mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 struct utsname vutsname;
2417
2418 if (uname(&vutsname) < 0)
2419 *s = NUL;
2420 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002421 vim_strncpy(s, (char_u *)vutsname.nodename, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002423#else // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425# ifdef HAVE_SYS_SYSTEMINFO_H
2426# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
2427# endif
2428
2429 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002430mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432# ifdef VAXC
2433 vaxc$gethostname((char *)s, len);
2434# else
2435 gethostname((char *)s, len);
2436# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002437 s[len - 1] = NUL; // make sure it's terminated
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002439#endif // HAVE_SYS_UTSNAME_H
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441/*
2442 * return process ID
2443 */
2444 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002445mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 return (long)getpid();
2448}
2449
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002450/*
2451 * return TRUE if process "pid" is still running
2452 */
2453 int
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002454mch_process_running(long pid)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002455{
2456 // EMX kill() not working correctly, it seems
2457 return kill(pid, 0) == 0;
2458}
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01002462strerror(int err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 extern int sys_nerr;
2465 extern char *sys_errlist[];
2466 static char er[20];
2467
2468 if (err > 0 && err < sys_nerr)
2469 return (sys_errlist[err]);
2470 sprintf(er, "Error %d", err);
2471 return er;
2472}
2473#endif
2474
2475/*
Bram Moolenaar964b3742019-05-24 18:54:09 +02002476 * Get name of current directory into buffer "buf" of length "len" bytes.
2477 * "len" must be at least PATH_MAX.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 * Return OK for success, FAIL for failure.
2479 */
2480 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002481mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483#if defined(USE_GETCWD)
2484 if (getcwd((char *)buf, len) == NULL)
2485 {
2486 STRCPY(buf, strerror(errno));
2487 return FAIL;
2488 }
2489 return OK;
2490#else
2491 return (getwd((char *)buf) != NULL ? OK : FAIL);
2492#endif
2493}
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495/*
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002496 * Get absolute file name into "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 *
2498 * return FAIL for failure, OK for success
2499 */
2500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002501mch_FullName(
2502 char_u *fname,
2503 char_u *buf,
2504 int len,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002505 int force) // also expand when already absolute path
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507 int l;
Bram Moolenaar38323e42007-03-06 19:22:53 +00002508#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 int fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002510 static int dont_fchdir = FALSE; // TRUE when fchdir() doesn't work
Bram Moolenaar38323e42007-03-06 19:22:53 +00002511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 char_u olddir[MAXPATHL];
2513 char_u *p;
2514 int retval = OK;
Bram Moolenaarbf820722008-06-21 11:12:49 +00002515#ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01002516 char_u posix_fname[MAXPATHL]; // Cygwin docs mention MAX_PATH, but
2517 // it's not always defined
Bram Moolenaarbf820722008-06-21 11:12:49 +00002518#endif
2519
Bram Moolenaar38323e42007-03-06 19:22:53 +00002520#ifdef VMS
2521 fname = vms_fixfilename(fname);
2522#endif
2523
Bram Moolenaara2442432007-04-26 14:26:37 +00002524#ifdef __CYGWIN__
2525 /*
2526 * This helps for when "/etc/hosts" is a symlink to "c:/something/hosts".
2527 */
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002528# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01002529 // Use CCP_RELATIVE to avoid that it sometimes returns a path that ends in
2530 // a forward slash.
Bram Moolenaar06b07342015-12-31 22:26:28 +01002531 cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
2532 fname, posix_fname, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002533# else
Bram Moolenaarbf820722008-06-21 11:12:49 +00002534 cygwin_conv_to_posix_path(fname, posix_fname);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002535# endif
Bram Moolenaarbf820722008-06-21 11:12:49 +00002536 fname = posix_fname;
Bram Moolenaara2442432007-04-26 14:26:37 +00002537#endif
2538
Bram Moolenaar0f873732019-12-05 20:28:46 +01002539 // Expand it if forced or not an absolute path.
2540 // Do not do it for "/file", the result is always "/".
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002541 if ((force || !mch_isFullName(fname))
2542 && ((p = vim_strrchr(fname, '/')) == NULL || p != fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
2544 /*
2545 * If the file name has a path, change to that directory for a moment,
Bram Moolenaar964b3742019-05-24 18:54:09 +02002546 * and then get the directory (and get back to where we were).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 * This will get the correct path name with "../" things.
2548 */
Bram Moolenaare3303cb2015-12-31 18:29:46 +01002549 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002551#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /*
2553 * Use fchdir() if possible, it's said to be faster and more
2554 * reliable. But on SunOS 4 it might not work. Check this by
2555 * doing a fchdir() right now.
2556 */
2557 if (!dont_fchdir)
2558 {
2559 fd = open(".", O_RDONLY | O_EXTRA, 0);
2560 if (fd >= 0 && fchdir(fd) < 0)
2561 {
2562 close(fd);
2563 fd = -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002564 dont_fchdir = TRUE; // don't try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566 }
Bram Moolenaar38323e42007-03-06 19:22:53 +00002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar0f873732019-12-05 20:28:46 +01002569 // Only change directory when we are sure we can return to where
2570 // we are now. After doing "su" chdir(".") might not work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (
Bram Moolenaar38323e42007-03-06 19:22:53 +00002572#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 fd < 0 &&
Bram Moolenaar38323e42007-03-06 19:22:53 +00002574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 (mch_dirname(olddir, MAXPATHL) == FAIL
2576 || mch_chdir((char *)olddir) != 0))
2577 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002578 p = NULL; // can't get current dir: don't chdir
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 retval = FAIL;
2580 }
2581 else
2582 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002583 // The directory is copied into buf[], to be able to remove
2584 // the file name without changing it (could be a string in
2585 // read-only memory)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (p - fname >= len)
2587 retval = FAIL;
2588 else
2589 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002590 vim_strncpy(buf, fname, p - fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (mch_chdir((char *)buf))
2592 retval = FAIL;
2593 else
2594 fname = p + 1;
2595 *buf = NUL;
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598 }
2599 if (mch_dirname(buf, len) == FAIL)
2600 {
2601 retval = FAIL;
2602 *buf = NUL;
2603 }
2604 if (p != NULL)
2605 {
Bram Moolenaar38323e42007-03-06 19:22:53 +00002606#ifdef HAVE_FCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (fd >= 0)
2608 {
Bram Moolenaar25724922009-07-14 15:38:41 +00002609 if (p_verbose >= 5)
2610 {
2611 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002612 msg("fchdir() to previous dir");
Bram Moolenaar25724922009-07-14 15:38:41 +00002613 verbose_leave();
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 l = fchdir(fd);
2616 close(fd);
2617 }
2618 else
Bram Moolenaar38323e42007-03-06 19:22:53 +00002619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 l = mch_chdir((char *)olddir);
2621 if (l != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002622 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624
2625 l = STRLEN(buf);
Bram Moolenaardac75692012-10-14 04:35:45 +02002626 if (l >= len - 1)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002627 retval = FAIL; // no space for trailing "/"
Bram Moolenaar38323e42007-03-06 19:22:53 +00002628#ifndef VMS
Bram Moolenaardac75692012-10-14 04:35:45 +02002629 else if (l > 0 && buf[l - 1] != '/' && *fname != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 && STRCMP(fname, ".") != 0)
Bram Moolenaardac75692012-10-14 04:35:45 +02002631 STRCAT(buf, "/");
Bram Moolenaar38323e42007-03-06 19:22:53 +00002632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaar3d20ca12006-11-28 16:43:58 +00002634
Bram Moolenaar0f873732019-12-05 20:28:46 +01002635 // Catch file names which are too long.
Bram Moolenaar78a15312009-05-15 19:33:18 +00002636 if (retval == FAIL || (int)(STRLEN(buf) + STRLEN(fname)) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return FAIL;
2638
Bram Moolenaar0f873732019-12-05 20:28:46 +01002639 // Do not append ".", "/dir/." is equal to "/dir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (STRCMP(fname, ".") != 0)
2641 STRCAT(buf, fname);
2642
2643 return OK;
2644}
2645
2646/*
2647 * Return TRUE if "fname" does not depend on the current directory.
2648 */
2649 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002650mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002652#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 return ( fname[0] == '/' || fname[0] == '.' ||
2654 strchr((char *)fname,':') || strchr((char *)fname,'"') ||
2655 (strchr((char *)fname,'[') && strchr((char *)fname,']'))||
2656 (strchr((char *)fname,'<') && strchr((char *)fname,'>')) );
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002657#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return (*fname == '/' || *fname == '~');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#endif
2660}
2661
Bram Moolenaar24552be2005-12-10 20:17:30 +00002662#if defined(USE_FNAME_CASE) || defined(PROTO)
2663/*
2664 * Set the case of the file name, if it already exists. This will cause the
2665 * file name to remain exactly the same.
Bram Moolenaarc2a27c32007-12-01 16:19:33 +00002666 * Only required for file systems where case is ignored and preserved.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002667 */
Bram Moolenaar24552be2005-12-10 20:17:30 +00002668 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002669fname_case(
2670 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002671 int len UNUSED) // buffer size, only used when name gets longer
Bram Moolenaar24552be2005-12-10 20:17:30 +00002672{
2673 struct stat st;
2674 char_u *slash, *tail;
2675 DIR *dirp;
2676 struct dirent *dp;
2677
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002678 if (mch_lstat((char *)name, &st) >= 0)
Bram Moolenaar24552be2005-12-10 20:17:30 +00002679 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002680 // Open the directory where the file is located.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002681 slash = vim_strrchr(name, '/');
2682 if (slash == NULL)
2683 {
2684 dirp = opendir(".");
2685 tail = name;
2686 }
2687 else
2688 {
2689 *slash = NUL;
2690 dirp = opendir((char *)name);
2691 *slash = '/';
2692 tail = slash + 1;
2693 }
2694
2695 if (dirp != NULL)
2696 {
2697 while ((dp = readdir(dirp)) != NULL)
2698 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002699 // Only accept names that differ in case and are the same byte
2700 // length. TODO: accept different length name.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002701 if (STRICMP(tail, dp->d_name) == 0
2702 && STRLEN(tail) == STRLEN(dp->d_name))
2703 {
2704 char_u newname[MAXPATHL + 1];
2705 struct stat st2;
2706
Bram Moolenaar0f873732019-12-05 20:28:46 +01002707 // Verify the inode is equal.
Bram Moolenaar24552be2005-12-10 20:17:30 +00002708 vim_strncpy(newname, name, MAXPATHL);
2709 vim_strncpy(newname + (tail - name), (char_u *)dp->d_name,
2710 MAXPATHL - (tail - name));
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01002711 if (mch_lstat((char *)newname, &st2) >= 0
Bram Moolenaar24552be2005-12-10 20:17:30 +00002712 && st.st_ino == st2.st_ino
2713 && st.st_dev == st2.st_dev)
2714 {
2715 STRCPY(tail, dp->d_name);
2716 break;
2717 }
2718 }
2719 }
2720
2721 closedir(dirp);
2722 }
2723 }
2724}
2725#endif
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727/*
2728 * Get file permissions for 'name'.
2729 * Returns -1 when it doesn't exist.
2730 */
2731 long
Bram Moolenaar05540972016-01-30 20:31:25 +01002732mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 struct stat statb;
2735
Bram Moolenaar0f873732019-12-05 20:28:46 +01002736 // Keep the #ifdef outside of stat(), it may be a macro.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#ifdef VMS
2738 if (stat((char *)vms_fixfilename(name), &statb))
2739#else
2740 if (stat((char *)name, &statb))
2741#endif
2742 return -1;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002743#ifdef __INTERIX
Bram Moolenaar0f873732019-12-05 20:28:46 +01002744 // The top bit makes the value negative, which means the file doesn't
2745 // exist. Remove the bit, we don't use it.
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002746 return statb.st_mode & ~S_ADDACE;
2747#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 return statb.st_mode;
Bram Moolenaar708f62c2007-08-11 20:24:10 +00002749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750}
2751
2752/*
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002753 * Set file permission for "name" to "perm".
2754 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 */
2756 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002757mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 return (chmod((char *)
2760#ifdef VMS
2761 vms_fixfilename(name),
2762#else
2763 name,
2764#endif
2765 (mode_t)perm) == 0 ? OK : FAIL);
2766}
2767
Bram Moolenaarcd142e32017-11-16 17:03:45 +01002768#if defined(HAVE_FCHMOD) || defined(PROTO)
2769/*
2770 * Set file permission for open file "fd" to "perm".
2771 * Return FAIL for failure, OK otherwise.
2772 */
2773 int
2774mch_fsetperm(int fd, long perm)
2775{
2776 return (fchmod(fd, (mode_t)perm) == 0 ? OK : FAIL);
2777}
2778#endif
2779
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#if defined(HAVE_ACL) || defined(PROTO)
2781# ifdef HAVE_SYS_ACL_H
2782# include <sys/acl.h>
2783# endif
2784# ifdef HAVE_SYS_ACCESS_H
2785# include <sys/access.h>
2786# endif
2787
2788# ifdef HAVE_SOLARIS_ACL
2789typedef struct vim_acl_solaris_T {
2790 int acl_cnt;
2791 aclent_t *acl_entry;
2792} vim_acl_solaris_T;
2793# endif
2794
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002795#if defined(HAVE_SELINUX) || defined(PROTO)
2796/*
2797 * Copy security info from "from_file" to "to_file".
2798 */
2799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002800mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002801{
2802 if (from_file == NULL)
2803 return;
2804
2805 if (selinux_enabled == -1)
2806 selinux_enabled = is_selinux_enabled();
2807
2808 if (selinux_enabled > 0)
2809 {
2810 security_context_t from_context = NULL;
2811 security_context_t to_context = NULL;
2812
2813 if (getfilecon((char *)from_file, &from_context) < 0)
2814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002815 // If the filesystem doesn't support extended attributes,
2816 // the original had no special security context and the
2817 // target cannot have one either.
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002818 if (errno == EOPNOTSUPP)
2819 return;
2820
Bram Moolenaar32526b32019-01-19 17:43:09 +01002821 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002822 msg_outtrans(from_file);
2823 msg_putchar('\n');
2824 return;
2825 }
2826 if (getfilecon((char *)to_file, &to_context) < 0)
2827 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002828 msg_puts(_("\nCould not get security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002829 msg_outtrans(to_file);
2830 msg_putchar('\n');
2831 freecon (from_context);
2832 return ;
2833 }
2834 if (strcmp(from_context, to_context) != 0)
2835 {
2836 if (setfilecon((char *)to_file, from_context) < 0)
2837 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002838 msg_puts(_("\nCould not set security context for "));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002839 msg_outtrans(to_file);
2840 msg_putchar('\n');
2841 }
2842 }
2843 freecon(to_context);
2844 freecon(from_context);
2845 }
2846}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002847#endif // HAVE_SELINUX
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00002848
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002849#if defined(HAVE_SMACK) && !defined(PROTO)
2850/*
2851 * Copy security info from "from_file" to "to_file".
2852 */
2853 void
Bram Moolenaard14e00e2016-01-31 17:30:51 +01002854mch_copy_sec(char_u *from_file, char_u *to_file)
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002855{
Bram Moolenaar62f167f2014-04-23 12:52:40 +02002856 static const char * const smack_copied_attributes[] =
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002857 {
2858 XATTR_NAME_SMACK,
2859 XATTR_NAME_SMACKEXEC,
2860 XATTR_NAME_SMACKMMAP
2861 };
2862
2863 char buffer[SMACK_LABEL_LEN];
2864 const char *name;
2865 int index;
2866 int ret;
2867 ssize_t size;
2868
2869 if (from_file == NULL)
2870 return;
2871
2872 for (index = 0 ; index < (int)(sizeof(smack_copied_attributes)
2873 / sizeof(smack_copied_attributes)[0]) ; index++)
2874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002875 // get the name of the attribute to copy
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002876 name = smack_copied_attributes[index];
2877
Bram Moolenaar0f873732019-12-05 20:28:46 +01002878 // get the value of the attribute in buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002879 size = getxattr((char*)from_file, name, buffer, sizeof(buffer));
2880 if (size >= 0)
2881 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002882 // copy the attribute value of buffer
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002883 ret = setxattr((char*)to_file, name, buffer, (size_t)size, 0);
2884 if (ret < 0)
2885 {
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002886 vim_snprintf((char *)IObuff, IOSIZE,
2887 _("Could not set security context %s for %s"),
2888 name, to_file);
2889 msg_outtrans(IObuff);
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002890 msg_putchar('\n');
2891 }
2892 }
2893 else
2894 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002895 // what reason of not having the attribute value?
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002896 switch (errno)
2897 {
2898 case ENOTSUP:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002899 // extended attributes aren't supported or enabled
2900 // should a message be echoed? not sure...
2901 return; // leave because it isn't useful to continue
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002902
2903 case ERANGE:
2904 default:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002905 // no enough size OR unexpected error
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002906 vim_snprintf((char *)IObuff, IOSIZE,
2907 _("Could not get security context %s for %s. Removing it!"),
2908 name, from_file);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002909 msg_puts((char *)IObuff);
Bram Moolenaar4a1314c2016-01-27 20:47:18 +01002910 msg_putchar('\n');
Bram Moolenaar0f873732019-12-05 20:28:46 +01002911 // FALLTHROUGH to remove the attribute
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002912
2913 case ENODATA:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002914 // no attribute of this name
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002915 ret = removexattr((char*)to_file, name);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002916 // Silently ignore errors, apparently this happens when
2917 // smack is not actually being used.
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002918 break;
2919 }
2920 }
2921 }
2922}
Bram Moolenaar0f873732019-12-05 20:28:46 +01002923#endif // HAVE_SMACK
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02002924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925/*
2926 * Return a pointer to the ACL of file "fname" in allocated memory.
2927 * Return NULL if the ACL is not available for whatever reason.
2928 */
2929 vim_acl_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002930mch_get_acl(char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 vim_acl_T ret = NULL;
2933#ifdef HAVE_POSIX_ACL
2934 ret = (vim_acl_T)acl_get_file((char *)fname, ACL_TYPE_ACCESS);
2935#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01002936#ifdef HAVE_SOLARIS_ZFS_ACL
2937 acl_t *aclent;
2938
2939 if (acl_get((char *)fname, 0, &aclent) < 0)
2940 return NULL;
2941 ret = (vim_acl_T)aclent;
2942#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943#ifdef HAVE_SOLARIS_ACL
2944 vim_acl_solaris_T *aclent;
2945
2946 aclent = malloc(sizeof(vim_acl_solaris_T));
2947 if ((aclent->acl_cnt = acl((char *)fname, GETACLCNT, 0, NULL)) < 0)
2948 {
2949 free(aclent);
2950 return NULL;
2951 }
2952 aclent->acl_entry = malloc(aclent->acl_cnt * sizeof(aclent_t));
2953 if (acl((char *)fname, GETACL, aclent->acl_cnt, aclent->acl_entry) < 0)
2954 {
2955 free(aclent->acl_entry);
2956 free(aclent);
2957 return NULL;
2958 }
2959 ret = (vim_acl_T)aclent;
2960#else
2961#if defined(HAVE_AIX_ACL)
2962 int aclsize;
2963 struct acl *aclent;
2964
2965 aclsize = sizeof(struct acl);
2966 aclent = malloc(aclsize);
2967 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2968 {
2969 if (errno == ENOSPC)
2970 {
2971 aclsize = aclent->acl_len;
2972 aclent = realloc(aclent, aclsize);
2973 if (statacl((char *)fname, STX_NORMAL, aclent, aclsize) < 0)
2974 {
2975 free(aclent);
2976 return NULL;
2977 }
2978 }
2979 else
2980 {
2981 free(aclent);
2982 return NULL;
2983 }
2984 }
2985 ret = (vim_acl_T)aclent;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002986#endif // HAVE_AIX_ACL
2987#endif // HAVE_SOLARIS_ACL
2988#endif // HAVE_SOLARIS_ZFS_ACL
2989#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 return ret;
2991}
2992
2993/*
2994 * Set the ACL of file "fname" to "acl" (unless it's NULL).
2995 */
2996 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002997mch_set_acl(char_u *fname UNUSED, vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 if (aclent == NULL)
3000 return;
3001#ifdef HAVE_POSIX_ACL
3002 acl_set_file((char *)fname, ACL_TYPE_ACCESS, (acl_t)aclent);
3003#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003004#ifdef HAVE_SOLARIS_ZFS_ACL
3005 acl_set((char *)fname, (acl_t *)aclent);
3006#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#ifdef HAVE_SOLARIS_ACL
3008 acl((char *)fname, SETACL, ((vim_acl_solaris_T *)aclent)->acl_cnt,
3009 ((vim_acl_solaris_T *)aclent)->acl_entry);
3010#else
3011#ifdef HAVE_AIX_ACL
3012 chacl((char *)fname, aclent, ((struct acl *)aclent)->acl_len);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003013#endif // HAVE_AIX_ACL
3014#endif // HAVE_SOLARIS_ACL
3015#endif // HAVE_SOLARIS_ZFS_ACL
3016#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017}
3018
3019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003020mch_free_acl(vim_acl_T aclent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if (aclent == NULL)
3023 return;
3024#ifdef HAVE_POSIX_ACL
3025 acl_free((acl_t)aclent);
3026#else
Bram Moolenaar8d462f92012-02-05 22:51:33 +01003027#ifdef HAVE_SOLARIS_ZFS_ACL
3028 acl_free((acl_t *)aclent);
3029#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef HAVE_SOLARIS_ACL
3031 free(((vim_acl_solaris_T *)aclent)->acl_entry);
3032 free(aclent);
3033#else
3034#ifdef HAVE_AIX_ACL
3035 free(aclent);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003036#endif // HAVE_AIX_ACL
3037#endif // HAVE_SOLARIS_ACL
3038#endif // HAVE_SOLARIS_ZFS_ACL
3039#endif // HAVE_POSIX_ACL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040}
3041#endif
3042
3043/*
3044 * Set hidden flag for "name".
3045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003047mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaar0f873732019-12-05 20:28:46 +01003049 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050}
3051
3052/*
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003053 * return TRUE if "name" is a directory or a symlink to a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 * return FALSE if "name" is not a directory
3055 * return FALSE for error
3056 */
3057 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003058mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 struct stat statb;
3061
Bram Moolenaar0f873732019-12-05 20:28:46 +01003062 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 return FALSE;
3064 if (stat((char *)name, &statb))
3065 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067}
3068
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003069/*
3070 * return TRUE if "name" is a directory, NOT a symlink to a directory
3071 * return FALSE if "name" is not a directory
3072 * return FALSE for error
3073 */
3074 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003075mch_isrealdir(char_u *name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003076{
3077 struct stat statb;
3078
Bram Moolenaar0f873732019-12-05 20:28:46 +01003079 if (*name == NUL) // Some stat()s don't flag "" as an error.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003080 return FALSE;
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01003081 if (mch_lstat((char *)name, &statb))
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003082 return FALSE;
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003083 return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
Bram Moolenaar43a34f92016-01-17 15:56:34 +01003084}
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086/*
3087 * Return 1 if "name" is an executable file, 0 if not or it doesn't exist.
3088 */
3089 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003090executable_file(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 struct stat st;
3093
3094 if (stat((char *)name, &st))
3095 return 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003096#ifdef VMS
Bram Moolenaar0f873732019-12-05 20:28:46 +01003097 // Like on Unix system file can have executable rights but not necessarily
3098 // be an executable, but on Unix is not a default for an ordinary file to
3099 // have an executable flag - on VMS it is in most cases.
3100 // Therefore, this check does not have any sense - let keep us to the
3101 // conventions instead:
3102 // *.COM and *.EXE files are the executables - the rest are not. This is
3103 // not ideal but better then it was.
Bram Moolenaar206f0112014-03-12 16:51:55 +01003104 int vms_executable = 0;
3105 if (S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0)
3106 {
3107 if (strstr(vms_tolower((char*)name),".exe") != NULL
3108 || strstr(vms_tolower((char*)name),".com")!= NULL)
3109 vms_executable = 1;
3110 }
3111 return vms_executable;
3112#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 return S_ISREG(st.st_mode) && mch_access((char *)name, X_OK) == 0;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115}
3116
3117/*
Bram Moolenaar2fcf6682017-03-11 20:03:42 +01003118 * Return TRUE if "name" can be found in $PATH and executed, FALSE if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +01003119 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 * Return -1 if unknown.
3121 */
3122 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003123mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 char_u *buf;
3126 char_u *p, *e;
3127 int retval;
3128
Bram Moolenaar0f873732019-12-05 20:28:46 +01003129 // When "use_path" is false and if it's an absolute or relative path don't
3130 // need to use $PATH.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003131 if (!use_path || gettail(name) != name)
Bram Moolenaar206f0112014-03-12 16:51:55 +01003132 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003133 // There must be a path separator, files in the current directory
3134 // can't be executed.
Bram Moolenaard08b8c42019-07-24 14:59:45 +02003135 if ((use_path || gettail(name) != name) && executable_file(name))
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003136 {
3137 if (path != NULL)
3138 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003139 if (name[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003140 *path = FullName_save(name, TRUE);
3141 else
3142 *path = vim_strsave(name);
3143 }
3144 return TRUE;
3145 }
3146 return FALSE;
Bram Moolenaar206f0112014-03-12 16:51:55 +01003147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 p = (char_u *)getenv("PATH");
3150 if (p == NULL || *p == NUL)
3151 return -1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003152 buf = alloc(STRLEN(name) + STRLEN(p) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (buf == NULL)
3154 return -1;
3155
3156 /*
3157 * Walk through all entries in $PATH to check if "name" exists there and
3158 * is an executable file.
3159 */
3160 for (;;)
3161 {
3162 e = (char_u *)strchr((char *)p, ':');
3163 if (e == NULL)
3164 e = p + STRLEN(p);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003165 if (e - p <= 1) // empty entry means current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 STRCPY(buf, "./");
3167 else
3168 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003169 vim_strncpy(buf, p, e - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 add_pathsep(buf);
3171 }
3172 STRCAT(buf, name);
3173 retval = executable_file(buf);
3174 if (retval == 1)
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003175 {
3176 if (path != NULL)
3177 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003178 if (buf[0] != '/')
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003179 *path = FullName_save(buf, TRUE);
3180 else
3181 *path = vim_strsave(buf);
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 break;
Bram Moolenaarc7f02552014-04-01 21:00:59 +02003184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 if (*e != ':')
3187 break;
3188 p = e + 1;
3189 }
3190
3191 vim_free(buf);
3192 return retval;
3193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195/*
3196 * Check what "name" is:
3197 * NODE_NORMAL: file or directory (or doesn't exist)
3198 * NODE_WRITABLE: writable device, socket, fifo, etc.
3199 * NODE_OTHER: non-writable things
3200 */
3201 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003202mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
3204 struct stat st;
3205
3206 if (stat((char *)name, &st))
3207 return NODE_NORMAL;
3208 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
3209 return NODE_NORMAL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003210 if (S_ISBLK(st.st_mode)) // block device isn't writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return NODE_OTHER;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003212 // Everything else is writable?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 return NODE_WRITABLE;
3214}
3215
3216 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003217mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219#ifdef HAVE_CHECK_STACK_GROWTH
3220 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 check_stack_growth((char *)&i);
3223
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003224# ifdef HAVE_STACK_LIMIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 get_stack_limit();
3226# endif
3227
3228#endif
3229
3230 /*
3231 * Setup an alternative stack for signals. Helps to catch signals when
3232 * running out of stack space.
3233 * Use of sigaltstack() is preferred, it's more portable.
3234 * Ignore any errors.
3235 */
3236#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003237 signal_stack = alloc(SIGSTKSZ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 init_signal_stack();
3239#endif
3240}
3241
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003242#if defined(EXITFREE) || defined(PROTO)
3243 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003244mch_free_mem(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003245{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3247 if (clip_star.owned)
3248 clip_lose_selection(&clip_star);
3249 if (clip_plus.owned)
3250 clip_lose_selection(&clip_plus);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003251# endif
Bram Moolenaare8208012008-06-20 09:59:25 +00003252# if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003253 if (xterm_Shell != (Widget)0)
3254 XtDestroyWidget(xterm_Shell);
Bram Moolenaare8208012008-06-20 09:59:25 +00003255# ifndef LESSTIF_VERSION
Bram Moolenaar0f873732019-12-05 20:28:46 +01003256 // Lesstif crashes here, lose some memory
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003257 if (xterm_dpy != NULL)
3258 XtCloseDisplay(xterm_dpy);
3259 if (app_context != (XtAppContext)NULL)
Bram Moolenaare8208012008-06-20 09:59:25 +00003260 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003261 XtDestroyApplicationContext(app_context);
Bram Moolenaare8208012008-06-20 09:59:25 +00003262# ifdef FEAT_X11
Bram Moolenaar0f873732019-12-05 20:28:46 +01003263 x11_display = NULL; // freed by XtDestroyApplicationContext()
Bram Moolenaare8208012008-06-20 09:59:25 +00003264# endif
3265 }
3266# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003267# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003268# if defined(FEAT_X11)
Bram Moolenaare8208012008-06-20 09:59:25 +00003269 if (x11_display != NULL
3270# ifdef FEAT_XCLIPBOARD
3271 && x11_display != xterm_dpy
3272# endif
3273 )
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 XCloseDisplay(x11_display);
3275# endif
3276# if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003277 VIM_CLEAR(signal_stack);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278# endif
3279# ifdef FEAT_TITLE
3280 vim_free(oldtitle);
3281 vim_free(oldicon);
3282# endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003283}
3284#endif
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286/*
3287 * Output a newline when exiting.
3288 * Make sure the newline goes to the same stream as the text.
3289 */
3290 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003291exit_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003293 if (silent_mode)
3294 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (newline_on_exit || msg_didout)
3296 {
3297 if (msg_use_printf())
3298 {
3299 if (info_message)
3300 mch_msg("\n");
3301 else
3302 mch_errmsg("\r\n");
3303 }
3304 else
3305 out_char('\n');
3306 }
3307 else
3308 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003309 restore_cterm_colors(); // get original colors back
3310 msg_clr_eos_force(); // clear the rest of the display
3311 windgoto((int)Rows - 1, 0); // may have moved the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313}
3314
Bram Moolenaarb4151682020-05-11 22:13:28 +02003315#ifdef USE_GCOV_FLUSH
3316extern void __gcov_flush();
3317#endif
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003320mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 exiting = TRUE;
3323
3324#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3325 x11_export_final_selection();
3326#endif
3327
3328#ifdef FEAT_GUI
3329 if (!gui.in_use)
3330#endif
3331 {
3332 settmode(TMODE_COOK);
3333#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003334 // restore xterm title and icon name
3335 mch_restore_title(SAVE_RESTORE_BOTH);
3336 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#endif
3338 /*
3339 * When t_ti is not empty but it doesn't cause swapping terminal
3340 * pages, need to output a newline when msg_didout is set. But when
3341 * t_ti does swap pages it should not go to the shell page. Do this
3342 * before stoptermcap().
3343 */
3344 if (swapping_screen() && !newline_on_exit)
3345 exit_scroll();
3346
Bram Moolenaar0f873732019-12-05 20:28:46 +01003347 // Stop termcap: May need to check for T_CRV response, which
3348 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 stoptermcap();
3350
3351 /*
3352 * A newline is only required after a message in the alternate screen.
3353 * This is set to TRUE by wait_return().
3354 */
3355 if (!swapping_screen() || newline_on_exit)
3356 exit_scroll();
3357
Bram Moolenaar0f873732019-12-05 20:28:46 +01003358 // Cursor may have been switched off without calling starttermcap()
3359 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (full_screen)
3361 cursor_on();
3362 }
3363 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003364 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaarb4151682020-05-11 22:13:28 +02003365
3366#ifdef USE_GCOV_FLUSH
3367 // Flush coverage info before possibly being killed by a deadly signal.
3368 __gcov_flush();
3369#endif
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 may_core_dump();
3372#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 gui_exit(r);
3375#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003376
Bram Moolenaar56718732006-03-15 22:53:57 +00003377#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003378 mac_conv_cleanup();
3379#endif
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003382 // A core dump won't be created if the signal handler
3383 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (deadly_signal != 0)
3385 return;
3386#endif
3387
Bram Moolenaar009b2592004-10-24 19:18:58 +00003388#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003389 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003390#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003391
3392#ifdef EXITFREE
3393 free_all_mem();
3394#endif
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 exit(r);
3397}
3398
3399 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003400may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 if (deadly_signal != 0)
3403 {
3404 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003405 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407}
3408
3409#ifndef VMS
3410
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003411/*
3412 * Get the file descriptor to use for tty operations.
3413 */
3414 static int
3415get_tty_fd(int fd)
3416{
3417 int tty_fd = fd;
3418
3419#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3420 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3421 // "fd" when it is a master device.
3422 if (mch_isatty(fd) > 1)
3423 {
3424 char *name;
3425
3426 name = ptsname(fd);
3427 if (name == NULL)
3428 return -1;
3429
3430 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3431 if (tty_fd < 0)
3432 return -1;
3433 }
3434#endif
3435 return tty_fd;
3436}
3437
3438 static int
3439mch_tcgetattr(int fd, void *term)
3440{
3441 int tty_fd;
3442 int retval = -1;
3443
3444 tty_fd = get_tty_fd(fd);
3445 if (tty_fd >= 0)
3446 {
3447#ifdef NEW_TTY_SYSTEM
3448# ifdef HAVE_TERMIOS_H
3449 retval = tcgetattr(tty_fd, (struct termios *)term);
3450# else
3451 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3452# endif
3453#else
3454 // for "old" tty systems
3455 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3456#endif
3457 if (tty_fd != fd)
3458 close(tty_fd);
3459 }
3460 return retval;
3461}
3462
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003464mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 static int first = TRUE;
3467
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003468#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469# ifdef HAVE_TERMIOS_H
3470 static struct termios told;
3471 struct termios tnew;
3472# else
3473 static struct termio told;
3474 struct termio tnew;
3475# endif
3476
3477 if (first)
3478 {
3479 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003480 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482
3483 tnew = told;
3484 if (tmode == TMODE_RAW)
3485 {
3486 /*
3487 * ~ICRNL enables typing ^V^M
3488 */
3489 tnew.c_iflag &= ~ICRNL;
3490 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3491# if defined(IEXTEN) && !defined(__MINT__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003492 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
3493 // but it breaks function keys on MINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494# endif
3495 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003496# ifdef ONLCR
3497 // Don't map NL -> CR NL, we do it ourselves.
3498 // Also disable expanding tabs if possible.
3499# ifdef XTABS
3500 tnew.c_oflag &= ~(ONLCR | XTABS);
3501# else
3502# ifdef TAB3
3503 tnew.c_oflag &= ~(ONLCR | TAB3);
3504# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003506# endif
3507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003509 tnew.c_cc[VMIN] = 1; // return after 1 char
3510 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003513 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003514 // Also reset ICANON here, otherwise on Solaris select() won't see
3515 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003516 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003517 tnew.c_cc[VMIN] = 1; // return after 1 char
3518 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521# if defined(HAVE_TERMIOS_H)
3522 {
3523 int n = 10;
3524
Bram Moolenaar0f873732019-12-05 20:28:46 +01003525 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3526 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3528 && errno == EINTR && n > 0)
3529 --n;
3530 }
3531# else
3532 ioctl(read_cmd_fd, TCSETA, &tnew);
3533# endif
3534
3535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
3537 * for "old" tty systems
3538 */
3539# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003540# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541# endif
3542 static struct sgttyb ttybold;
3543 struct sgttyb ttybnew;
3544
3545 if (first)
3546 {
3547 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003548 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550
3551 ttybnew = ttybold;
3552 if (tmode == TMODE_RAW)
3553 {
3554 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3555 ttybnew.sg_flags |= RAW;
3556 }
3557 else if (tmode == TMODE_SLEEP)
3558 ttybnew.sg_flags &= ~(ECHO);
3559 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3560#endif
3561 curr_tmode = tmode;
3562}
3563
3564/*
3565 * Try to get the code for "t_kb" from the stty setting
3566 *
3567 * Even if termcap claims a backspace key, the user's setting *should*
3568 * prevail. stty knows more about reality than termcap does, and if
3569 * somebody's usual erase key is DEL (which, for most BSD users, it will
3570 * be), they're going to get really annoyed if their erase key starts
3571 * doing forward deletes for no reason. (Eric Fischer)
3572 */
3573 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003574get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003576 ttyinfo_T info;
3577 char_u buf[2];
3578 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003580 if (get_tty_info(read_cmd_fd, &info) == OK)
3581 {
3582 intr_char = info.interrupt;
3583 buf[0] = info.backspace;
3584 buf[1] = NUL;
3585 add_termcode((char_u *)"kb", buf, FALSE);
3586
Bram Moolenaar0f873732019-12-05 20:28:46 +01003587 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003588 p = find_termcode((char_u *)"kD");
3589 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3590 do_fixdel(NULL);
3591 }
3592}
3593
3594/*
3595 * Obtain the characters that Backspace and Enter produce on "fd".
3596 * Returns OK or FAIL.
3597 */
3598 int
3599get_tty_info(int fd, ttyinfo_T *info)
3600{
3601#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602# ifdef HAVE_TERMIOS_H
3603 struct termios keys;
3604# else
3605 struct termio keys;
3606# endif
3607
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003608 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003610 info->backspace = keys.c_cc[VERASE];
3611 info->interrupt = keys.c_cc[VINTR];
3612 if (keys.c_iflag & ICRNL)
3613 info->enter = NL;
3614 else
3615 info->enter = CAR;
3616 if (keys.c_oflag & ONLCR)
3617 info->nl_does_cr = TRUE;
3618 else
3619 info->nl_does_cr = FALSE;
3620 return OK;
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003623 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 struct sgttyb keys;
3625
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003626 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003628 info->backspace = keys.sg_erase;
3629 info->interrupt = keys.sg_kill;
3630 info->enter = CAR;
3631 info->nl_does_cr = TRUE;
3632 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636}
3637
Bram Moolenaar0f873732019-12-05 20:28:46 +01003638#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003640static int mouse_ison = FALSE;
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642/*
3643 * Set mouse clicks on or off.
3644 */
3645 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003646mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003648#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003649 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 int xterm_mouse_vers;
3652
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003653#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003654 if (!on)
3655 // Make sure not tracing mouse movements. Important when a button-down
3656 // was received but no release yet.
3657 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003658#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003659
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003660 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003661#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003662 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003663#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003664 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003665 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 return;
3667
3668 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003669
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003670#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003671 if (ttym_flags == TTYM_URXVT)
3672 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003673 out_str_nf((char_u *)
3674 (on
3675 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3676 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003677 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003678 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003679#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003680
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003681 if (ttym_flags == TTYM_SGR)
3682 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003683 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003684 out_str_nf((char_u *)
3685 (on
3686 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3687 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003688 mouse_ison = on;
3689 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003691#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003692 if (bevalterm_ison != (p_bevalterm && on))
3693 {
3694 bevalterm_ison = (p_bevalterm && on);
3695 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003696 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003697 out_str_nf((char_u *)
3698 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003699 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003700#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (xterm_mouse_vers > 0)
3703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003704 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 out_str_nf((char_u *)
3706 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003707 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003708#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003709 bevalterm_ison
3710 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003711#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003712 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003714 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 out_str_nf((char_u *)
3716 (xterm_mouse_vers > 1
3717 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3718 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003719 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003722#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else if (ttym_flags == TTYM_DEC)
3724 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003725 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003727 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003729 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003733#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 else
3735 {
3736 if (on)
3737 {
3738 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003739 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 else
3742 {
3743 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003744 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003749#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003750 else
3751 {
3752 if (on)
3753 {
3754 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003755 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003756 }
3757 else
3758 {
3759 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003761 }
3762 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003763#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003764
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003765#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else
3767 {
3768 if (on)
3769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003770 // D - Enable Mouse up/down messages
3771 // L - Enable Left Button Reporting
3772 // M - Enable Middle Button Reporting
3773 // R - Enable Right Button Reporting
3774 // K - Enable SHIFT and CTRL key Reporting
3775 // + - Enable Advanced messaging of mouse moves and up/down messages
3776 // Q - Quiet No Ack
3777 // # - Numeric value of mouse pointer required
3778 // 0 = Multiview 2000 cursor, used as standard
3779 // 1 = Windows Arrow
3780 // 2 = Windows I Beam
3781 // 3 = Windows Hour Glass
3782 // 4 = Windows Cross Hair
3783 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003784# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003785 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3787 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003788# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3790 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003791# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003792 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 else
3795 {
3796 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3797 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003798 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003801#endif
3802#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 else
3804 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003805 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (on)
3807 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3808 else
3809 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003810 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813}
3814
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003815#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003816/*
3817 * Called when 'balloonevalterm' changed.
3818 */
3819 void
3820mch_bevalterm_changed(void)
3821{
3822 mch_setmouse(mouse_ison);
3823}
3824#endif
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826/*
3827 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3828 */
3829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003830check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832# ifdef FEAT_MOUSE_XTERM
3833 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003834# ifdef FEAT_MOUSE_URXVT
3835 && use_xterm_mouse() != 3
3836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837# ifdef FEAT_GUI
3838 && !gui.in_use
3839# endif
3840 )
3841 {
3842 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003843 ? IF_EB("\233M", CSI_STR "M")
3844 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (*p_mouse != NUL)
3846 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003847 // force mouse off and maybe on to send possibly new mouse
3848 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 mch_setmouse(FALSE);
3850 setmouse();
3851 }
3852 }
3853 else
3854 del_mouse_termcode(KS_MOUSE);
3855# endif
3856
3857# ifdef FEAT_MOUSE_GPM
3858 if (!use_xterm_mouse()
3859# ifdef FEAT_GUI
3860 && !gui.in_use
3861# endif
3862 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003863 set_mouse_termcode(KS_GPM_MOUSE,
3864 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3865 else
3866 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867# endif
3868
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003869# ifdef FEAT_SYSMOUSE
3870 if (!use_xterm_mouse()
3871# ifdef FEAT_GUI
3872 && !gui.in_use
3873# endif
3874 )
3875 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3876# endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003879 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (!use_xterm_mouse()
3881# ifdef FEAT_GUI
3882 && !gui.in_use
3883# endif
3884 )
3885 set_mouse_termcode(KS_JSBTERM_MOUSE,
3886 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3887 else
3888 del_mouse_termcode(KS_JSBTERM_MOUSE);
3889# endif
3890
3891# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003892 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3893 // define it in the GUI or when using an xterm.
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_NETTERM_MOUSE,
3900 (char_u *)IF_EB("\033}", ESC_STR "}"));
3901 else
3902 del_mouse_termcode(KS_NETTERM_MOUSE);
3903# endif
3904
3905# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003906 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003907 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908# ifdef FEAT_GUI
3909 && !gui.in_use
3910# endif
3911 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3913 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 else
3915 del_mouse_termcode(KS_DEC_MOUSE);
3916# endif
3917# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003918 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003919 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920# ifdef FEAT_GUI
3921 && !gui.in_use
3922# endif
3923 )
3924 set_mouse_termcode(KS_PTERM_MOUSE,
3925 (char_u *) IF_EB("\033[", ESC_STR "["));
3926 else
3927 del_mouse_termcode(KS_PTERM_MOUSE);
3928# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003929# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003930 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003931# ifdef FEAT_GUI
3932 && !gui.in_use
3933# endif
3934 )
3935 {
3936 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003937 ? IF_EB("\233*M", CSI_STR "*M")
3938 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003939
3940 if (*p_mouse != NUL)
3941 {
3942 mch_setmouse(FALSE);
3943 setmouse();
3944 }
3945 }
3946 else
3947 del_mouse_termcode(KS_URXVT_MOUSE);
3948# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003949 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003950# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003951 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003952# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003953 )
3954 {
3955 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003956 ? IF_EB("\233<*M", CSI_STR "<*M")
3957 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3958
3959 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3960 ? IF_EB("\233<*m", CSI_STR "<*m")
3961 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003962
3963 if (*p_mouse != NUL)
3964 {
3965 mch_setmouse(FALSE);
3966 setmouse();
3967 }
3968 }
3969 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003970 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003971 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003972 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#ifndef VMS
3977
3978/*
3979 * Try to get the current window size:
3980 * 1. with an ioctl(), most accurate method
3981 * 2. from the environment variables LINES and COLUMNS
3982 * 3. from the termcap
3983 * 4. keep using the old values
3984 * Return OK when size could be determined, FAIL otherwise.
3985 */
3986 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003987mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
3989 long rows = 0;
3990 long columns = 0;
3991 char_u *p;
3992
3993 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 * 1. try using an ioctl. It is the most accurate method.
3995 *
3996 * Try using TIOCGWINSZ first, some systems that have it also define
3997 * TIOCGSIZE but don't have a struct ttysize.
3998 */
3999# ifdef TIOCGWINSZ
4000 {
4001 struct winsize ws;
4002 int fd = 1;
4003
Bram Moolenaar0f873732019-12-05 20:28:46 +01004004 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (!isatty(fd) && isatty(read_cmd_fd))
4006 fd = read_cmd_fd;
4007 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
4008 {
4009 columns = ws.ws_col;
4010 rows = ws.ws_row;
4011 }
4012 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004013# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014# ifdef TIOCGSIZE
4015 {
4016 struct ttysize ts;
4017 int fd = 1;
4018
Bram Moolenaar0f873732019-12-05 20:28:46 +01004019 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (!isatty(fd) && isatty(read_cmd_fd))
4021 fd = read_cmd_fd;
4022 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4023 {
4024 columns = ts.ts_cols;
4025 rows = ts.ts_lines;
4026 }
4027 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004028# endif // TIOCGSIZE
4029# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 /*
4032 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004033 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4034 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004036 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 if ((p = (char_u *)getenv("LINES")))
4039 rows = atoi((char *)p);
4040 if ((p = (char_u *)getenv("COLUMNS")))
4041 columns = atoi((char *)p);
4042 }
4043
4044#ifdef HAVE_TGETENT
4045 /*
4046 * 3. try reading "co" and "li" entries from termcap
4047 */
4048 if (columns == 0 || rows == 0)
4049 getlinecol(&columns, &rows);
4050#endif
4051
4052 /*
4053 * 4. If everything fails, use the old values
4054 */
4055 if (columns <= 0 || rows <= 0)
4056 return FAIL;
4057
4058 Rows = rows;
4059 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004060 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return OK;
4062}
4063
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004064#if defined(FEAT_TERMINAL) || defined(PROTO)
4065/*
4066 * Report the windows size "rows" and "cols" to tty "fd".
4067 */
4068 int
4069mch_report_winsize(int fd, int rows, int cols)
4070{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004071 int tty_fd;
4072 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004073
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004074 tty_fd = get_tty_fd(fd);
4075 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004076 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004077# if defined(TIOCSWINSZ)
4078 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004079
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004080 ws.ws_col = cols;
4081 ws.ws_row = rows;
4082 ws.ws_xpixel = cols * 5;
4083 ws.ws_ypixel = rows * 10;
4084 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4085 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4086 retval == 0 ? "success" : "failed");
4087# elif defined(TIOCSSIZE)
4088 struct ttysize ts;
4089
4090 ts.ts_cols = cols;
4091 ts.ts_lines = rows;
4092 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4093 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4094 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004095# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004096 if (tty_fd != fd)
4097 close(tty_fd);
4098 }
4099 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004100}
4101#endif
4102
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103/*
4104 * Try to set the window size to Rows and Columns.
4105 */
4106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004107mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 if (*T_CWS)
4110 {
4111 /*
4112 * NOTE: if you get an error here that term_set_winsize() is
4113 * undefined, check the output of configure. It could probably not
4114 * find a ncurses, termcap or termlib library.
4115 */
4116 term_set_winsize((int)Rows, (int)Columns);
4117 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004118 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120}
4121
Bram Moolenaar0f873732019-12-05 20:28:46 +01004122#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124/*
4125 * Rows and/or Columns has changed.
4126 */
4127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004128mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004130 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131}
4132
Bram Moolenaar205b8862011-09-07 15:04:31 +02004133/*
4134 * Wait for process "child" to end.
4135 * Return "child" if it exited properly, <= 0 on error.
4136 */
4137 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004138wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004139{
4140 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004141 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004142
4143 while (wait_pid != child)
4144 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004145 // When compiled with Python threads are probably used, in which case
4146 // wait() sometimes hangs for no obvious reason. Use waitpid()
4147 // instead and loop (like the GUI). Also needed for other interfaces,
4148 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004149# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004151# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004152 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004153# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004154 if (wait_pid == 0)
4155 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004156 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004157 mch_delay(delay_msec, TRUE);
4158 if (++delay_msec > 10)
4159 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004160 continue;
4161 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004162 if (wait_pid <= 0
4163# ifdef ECHILD
4164 && errno == ECHILD
4165# endif
4166 )
4167 break;
4168 }
4169 return wait_pid;
4170}
4171
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004172#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004173/*
4174 * Set the environment for a child process.
4175 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004176 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004177set_child_environment(
4178 long rows,
4179 long columns,
4180 char *term,
4181 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004182{
4183# ifdef HAVE_SETENV
4184 char envbuf[50];
4185# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004186 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004187 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004188 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004189 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004190 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004191# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004192 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004193# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004194# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004195 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004196# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004197# endif
4198
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004199# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004200 setenv("TERM", term, 1);
4201 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004202 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004203 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004204 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004205 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004206 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004207 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004208 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004209# ifdef FEAT_TERMINAL
4210 if (is_terminal)
4211 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004212 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004213 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4214 }
4215# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004216# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004217 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004218# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004219# else
4220 /*
4221 * Putenv does not copy the string, it has to remain valid.
4222 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004223 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004224 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004225 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4226 putenv(envbuf_Term);
4227 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004228 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004229 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4230 putenv(envbuf_Lines);
4231 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4232 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004233 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004234 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004235 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004236# ifdef FEAT_TERMINAL
4237 if (is_terminal)
4238 {
4239 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004240 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004241 putenv(envbuf_Version);
4242 }
4243# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004244# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004245 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4246 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4247 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004248# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004249# endif
4250}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004251
4252 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004253set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004254{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004255 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004256}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004257#endif
4258
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004259#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004260/*
4261 * Open a PTY, with FD for the master and slave side.
4262 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004263 * When successful both file descriptors are stored and the allocated pty name
4264 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004265 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004266 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004267open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004268{
4269 char *tty_name;
4270
Bram Moolenaar59386482019-02-10 22:43:46 +01004271 if (name1 != NULL)
4272 *name1 = NULL;
4273 if (name2 != NULL)
4274 *name2 = NULL;
4275
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004276 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004277 if (*pty_master_fd >= 0)
4278 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004279 // Leaving out O_NOCTTY may lead to waitpid() always returning
4280 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4281 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004282#ifdef O_NOCTTY
4283 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4284#else
4285 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4286#endif
4287 if (*pty_slave_fd < 0)
4288 {
4289 close(*pty_master_fd);
4290 *pty_master_fd = -1;
4291 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004292 else
4293 {
4294 if (name1 != NULL)
4295 *name1 = vim_strsave((char_u *)tty_name);
4296 if (name2 != NULL)
4297 *name2 = vim_strsave((char_u *)tty_name);
4298 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004299 }
4300}
4301#endif
4302
Bram Moolenaarfae42832017-08-01 22:24:26 +02004303/*
4304 * Send SIGINT to a child process if "c" is an interrupt character.
4305 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004306 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004307may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4308{
4309# ifdef SIGINT
4310 if (c == Ctrl_C || c == intr_char)
4311 {
4312# ifdef HAVE_SETSID
4313 kill(-pid, SIGINT);
4314# else
4315 kill(0, SIGINT);
4316# endif
4317 if (wpid > 0)
4318 kill(wpid, SIGINT);
4319 }
4320# endif
4321}
4322
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004323#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004324
Bram Moolenaar0f873732019-12-05 20:28:46 +01004325/*
4326 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4327 * pointers, the last one is NULL.
4328 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4329 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004330 int
4331unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004332 char_u *cmd,
4333 char ***argvp,
4334 char_u **sh_tofree,
4335 char_u **shcf_tofree)
4336{
4337 char **argv = NULL;
4338 int argc;
4339
4340 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004341 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004342 return FAIL;
4343
4344 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4345 return FAIL;
4346 *argvp = argv;
4347
4348 if (cmd != NULL)
4349 {
4350 char_u *s;
4351 char_u *p;
4352
4353 if (extra_shell_arg != NULL)
4354 argv[argc++] = (char *)extra_shell_arg;
4355
Bram Moolenaar0f873732019-12-05 20:28:46 +01004356 // Break 'shellcmdflag' into white separated parts. This doesn't
4357 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004358 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004359 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004360 return FAIL;
4361 s = *shcf_tofree;
4362 p = p_shcf;
4363 while (*p != NUL)
4364 {
4365 argv[argc++] = (char *)s;
4366 while (*p && *p != ' ' && *p != TAB)
4367 *s++ = *p++;
4368 *s++ = NUL;
4369 p = skipwhite(p);
4370 }
4371
4372 argv[argc++] = (char *)cmd;
4373 }
4374 argv[argc] = NULL;
4375 return OK;
4376}
4377#endif
4378
4379#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4380/*
4381 * Use a terminal window to run a shell command in.
4382 */
4383 static int
4384mch_call_shell_terminal(
4385 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004386 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004387{
4388 jobopt_T opt;
4389 char **argv = NULL;
4390 char_u *tofree1 = NULL;
4391 char_u *tofree2 = NULL;
4392 int retval = -1;
4393 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004394 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004395 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004396 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004397
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004398 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004399 goto theend;
4400
4401 init_job_options(&opt);
4402 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4403 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004404 if (buf == NULL)
4405 goto theend;
4406
4407 job = term_getjob(buf->b_term);
4408 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004409
Bram Moolenaar0f873732019-12-05 20:28:46 +01004410 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004411 aucmd_prepbuf(&aco, buf);
4412
4413 clear_oparg(&oa);
4414 while (term_use_loop())
4415 {
4416 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4417 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004418 // If terminal_loop() returns OK we got a key that is handled
4419 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004420 if (terminal_loop(TRUE) == OK)
4421 normal_cmd(&oa, TRUE);
4422 }
4423 else
4424 normal_cmd(&oa, TRUE);
4425 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004426 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004427 ch_log(NULL, "system command finished");
4428
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004429 job_unref(job);
4430
Bram Moolenaar0f873732019-12-05 20:28:46 +01004431 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004432 aucmd_restbuf(&aco);
4433
4434 wait_return(TRUE);
4435 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4436
4437theend:
4438 vim_free(argv);
4439 vim_free(tofree1);
4440 vim_free(tofree2);
4441 return retval;
4442}
4443#endif
4444
4445#ifdef USE_SYSTEM
4446/*
4447 * Use system() to start the shell: simple but slow.
4448 */
4449 static int
4450mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004451 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004452 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454#ifdef VMS
4455 char *ifn = NULL;
4456 char *ofn = NULL;
4457#endif
4458 int tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004459 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004460 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 out_flush();
4463
4464 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004465 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
Bram Moolenaar62b42182010-09-21 22:09:37 +02004467# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004468 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004469 loose_clipboard();
4470# endif
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (cmd == NULL)
4473 x = system((char *)p_sh);
4474 else
4475 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004476# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (ofn = strchr((char *)cmd, '>'))
4478 *ofn++ = '\0';
4479 if (ifn = strchr((char *)cmd, '<'))
4480 {
4481 char *p;
4482
4483 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004484 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (p)
4486 *p = '\0';
4487 }
4488 if (ofn)
4489 x = vms_sys((char *)cmd, ofn, ifn);
4490 else
4491 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004492# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004493 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004495 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (newcmd == NULL)
4497 x = 0;
4498 else
4499 {
4500 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4501 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4502 (char *)p_shcf,
4503 (char *)cmd);
4504 x = system((char *)newcmd);
4505 vim_free(newcmd);
4506 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509# ifdef VMS
4510 x = vms_sys_status(x);
4511# endif
4512 if (emsg_silent)
4513 ;
4514 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004515 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 else if (x && !(options & SHELL_SILENT))
4517 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004518 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 msg_outnum((long)x);
4520 msg_putchar('\n');
4521 }
4522
4523 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004524 {
4525 // The shell may have messed with the mode, always set it.
4526 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004527 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529# ifdef FEAT_TITLE
4530 resettitle();
4531# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004532# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4533 restore_clipboard();
4534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar0f873732019-12-05 20:28:46 +01004538#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar0f873732019-12-05 20:28:46 +01004540# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4541 // 127, some shells use that already
4542# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar13568252018-03-16 20:46:58 +01004544/*
4545 * Don't use system(), use fork()/exec().
4546 */
4547 static int
4548mch_call_shell_fork(
4549 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004550 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004551{
4552 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004554 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 pid_t wait_pid = 0;
4556# ifdef HAVE_UNION_WAIT
4557 union wait status;
4558# else
4559 int status = -1;
4560# endif
4561 int retval = -1;
4562 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004563 char_u *tofree1 = NULL;
4564 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004567# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004570 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 int fd_fromshell[2];
4572 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004573 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 out_flush();
4576 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004577 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02004578 if (tmode == TMODE_RAW)
4579 // The shell may have messed with the mode, always set it later.
4580 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004582 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004583 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004586 * For the GUI, when writing the output into the buffer and when reading
4587 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4588 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590 if ((options & (SHELL_READ|SHELL_WRITE))
4591# ifdef FEAT_GUI
4592 || (gui.in_use && show_shell_mess)
4593# endif
4594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 /*
4598 * Try to open a master pty.
4599 * If this works, open the slave pty.
4600 * If the slave can't be opened, close the master pty.
4601 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004602 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004603 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If not opening a pty or it didn't work, try using pipes.
4606 */
4607 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004611 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
4613 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004614 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 close(fd_toshell[0]);
4617 close(fd_toshell[1]);
4618 }
4619 }
4620 if (pipe_error)
4621 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004622 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 out_flush();
4624 }
4625 }
4626 }
4627
Bram Moolenaar0f873732019-12-05 20:28:46 +01004628 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004630 SIGSET_DECL(curset)
4631
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004632# if defined(__BEOS__) && USE_THREAD_FOR_INPUT_WITH_TIMEOUT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 beos_cleanup_read_thread();
4634# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004635
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004636 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004637 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004638 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004640 UNBLOCK_SIGNALS(&curset);
4641
Bram Moolenaar32526b32019-01-19 17:43:09 +01004642 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004643 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004645 || (gui.in_use && show_shell_mess)
4646# endif
4647 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004650 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652 close(pty_master_fd);
4653 close(pty_slave_fd);
4654 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004655 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 close(fd_toshell[0]);
4659 close(fd_toshell[1]);
4660 close(fd_fromshell[0]);
4661 close(fd_fromshell[1]);
4662 }
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004665 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004667 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004668 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
Bram Moolenaar819524702018-02-27 19:10:00 +01004670# ifdef FEAT_JOB_CHANNEL
4671 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004672 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004673 ch_logfile((char_u *)"", (char_u *)"");
4674# endif
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (!show_shell_mess || (options & SHELL_EXPAND))
4677 {
4678 int fd;
4679
4680 /*
4681 * Don't want to show any message from the shell. Can't just
4682 * close stdout and stderr though, because some systems will
4683 * break if you try to write to them after that, so we must
4684 * use dup() to replace them with something else -- webb
4685 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4686 * waiting for input.
4687 */
4688 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4689 fclose(stdin);
4690 fclose(stdout);
4691 fclose(stderr);
4692
4693 /*
4694 * If any of these open()'s and dup()'s fail, we just continue
4695 * anyway. It's not fatal, and on most systems it will make
4696 * no difference at all. On a few it will cause the execvp()
4697 * to exit with a non-zero status even when the completion
4698 * could be done, which is nothing too serious. If the open()
4699 * or dup() failed we'd just do the same thing ourselves
4700 * anyway -- webb
4701 */
4702 if (fd >= 0)
4703 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004704 vim_ignored = dup(fd); // To replace stdin (fd 0)
4705 vim_ignored = dup(fd); // To replace stdout (fd 1)
4706 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
Bram Moolenaar0f873732019-12-05 20:28:46 +01004708 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 close(fd);
4710 }
4711 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004712 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004714 || gui.in_use
4715# endif
4716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
4718
Bram Moolenaardf177f62005-02-22 08:39:57 +00004719# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004720 // Create our own process group, so that the child and all its
4721 // children can be kill()ed. Don't do this when using pipes,
4722 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004723 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004724 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004725 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004726# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004727 // When doing "!xterm&" and 'shell' is bash: the shell
4728 // will exit and send SIGHUP to all processes in its
4729 // group, killing the just started process. Ignore SIGHUP
4730 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004731 signal(SIGHUP, SIG_IGN);
4732# endif
4733 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004734# endif
4735# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004736 if (pty_slave_fd >= 0)
4737 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004738 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004739 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004740 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004742 // Try to become controlling tty (probably doesn't work,
4743 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004744 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004746 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004747# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004748 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaara5792f52005-11-23 21:25:05 +00004750 /*
4751 * stderr is only redirected when using the GUI, so that a
4752 * program like gpg can still access the terminal to get a
4753 * passphrase using stderr.
4754 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if (pty_master_fd >= 0)
4757 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004758 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
Bram Moolenaar0f873732019-12-05 20:28:46 +01004760 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004762 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004764 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004765 if (gui.in_use)
4766 {
4767 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004768 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaar0f873732019-12-05 20:28:46 +01004771 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004776 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 close(fd_toshell[1]);
4778 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004779 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 close(fd_toshell[0]);
4781
Bram Moolenaar0f873732019-12-05 20:28:46 +01004782 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 close(fd_fromshell[0]);
4784 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004785 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 close(fd_fromshell[1]);
4787
Bram Moolenaara5792f52005-11-23 21:25:05 +00004788# ifdef FEAT_GUI
4789 if (gui.in_use)
4790 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004791 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004792 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004793 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004794 }
4795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 /*
4800 * There is no type cast for the argv, because the type may be
4801 * different on different machines. This may cause a warning
4802 * message with strict compilers, don't worry about it.
4803 * Call _exit() instead of exit() to avoid closing the connection
4804 * to the X server (esp. with GTK, which uses atexit()).
4805 */
4806 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004807 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004809 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 /*
4812 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004813 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 */
4815 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004816 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004817 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004818# ifdef FEAT_JOB_CHANNEL
4819 ++dont_check_job_ended;
4820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 /*
4822 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004823 * This is also used to pipe stdin/stdout to/from the external
4824 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826 if ((options & (SHELL_READ|SHELL_WRITE))
4827# ifdef FEAT_GUI
4828 || (gui.in_use && show_shell_mess)
4829# endif
4830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004832# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004834 int buffer_off = 0; // valid bytes in buffer[]
4835 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4836 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 int len;
4838 int p_more_save;
4839 int old_State;
4840 int c;
4841 int toshell_fd;
4842 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004843 garray_T ga;
4844 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004845# ifdef ELAPSED_FUNC
4846 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (pty_master_fd >= 0)
4851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 fromshell_fd = pty_master_fd;
4853 toshell_fd = dup(pty_master_fd);
4854 }
4855 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
4858 close(fd_toshell[0]);
4859 close(fd_fromshell[1]);
4860 toshell_fd = fd_toshell[1];
4861 fromshell_fd = fd_fromshell[0];
4862 }
4863
4864 /*
4865 * Write to the child if there are typed characters.
4866 * Read from the child if there are characters available.
4867 * Repeat the reading a few times if more characters are
4868 * available. Need to check for typed keys now and then, but
4869 * not too often (delays when no chars are available).
4870 * This loop is quit if no characters can be read from the pty
4871 * (WaitForChar detected special condition), or there are no
4872 * characters available and the child has exited.
4873 * Only check if the child has exited when there is no more
4874 * output. The child may exit before all the output has
4875 * been printed.
4876 *
4877 * Currently this busy loops!
4878 * This can probably dead-lock when the write blocks!
4879 */
4880 p_more_save = p_more;
4881 p_more = FALSE;
4882 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004883 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004885 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004886 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004887 // Fork a process that will write the lines to the
4888 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004889 if ((wpid = fork()) == -1)
4890 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004891 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004892 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004893 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894 {
4895 linenr_T lnum = curbuf->b_op_start.lnum;
4896 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004897 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004898 size_t l;
4899
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004900 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 for (;;)
4902 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004903 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004904 if (l == 0)
4905 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004906 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004907 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 len = write(toshell_fd, "", (size_t)1);
4909 else
4910 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004911 char_u *s = vim_strchr(lp + written, NL);
4912
Bram Moolenaar89d40322006-08-29 15:30:07 +00004913 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004914 s == NULL ? l
4915 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004916 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004917 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004919 // Finished a line, add a NL, unless this line
4920 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004921 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004922 || (!curbuf->b_p_bin
4923 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004924 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004925 && (lnum !=
4926 curbuf->b_ml.ml_line_count
4927 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004928 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004929 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 ++lnum;
4931 if (lnum > curbuf->b_op_end.lnum)
4932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004933 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004934 close(toshell_fd);
4935 toshell_fd = -1;
4936 break;
4937 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004938 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004939 written = 0;
4940 }
4941 else if (len > 0)
4942 written += len;
4943 }
4944 _exit(0);
4945 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004946 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004947 {
4948 close(toshell_fd);
4949 toshell_fd = -1;
4950 }
4951 }
4952
4953 if (options & SHELL_READ)
4954 ga_init2(&ga, 1, BUFLEN);
4955
4956 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004957# ifdef ELAPSED_FUNC
4958 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 for (;;)
4961 {
4962 /*
4963 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004964 * if there are any.
4965 * Don't do this if we are expanding wild cards (would eat
4966 * typeahead).
4967 * Don't do this when filtering and terminal is in cooked
4968 * mode, the shell command will handle the I/O. Avoids
4969 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004970 * Don't get characters when the child has already
4971 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004972 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004973 * while (noread_cnt > 4), avoids that ":r !ls" eats
4974 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
4976 len = 0;
4977 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004978 && ((options &
4979 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4980 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004981# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004982 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004983# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004984 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004985 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004986 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004988 if (ta_len == 0)
4989 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004990 // Get extra characters when we don't have any.
4991 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004992 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004993# ifdef ELAPSED_FUNC
4994 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004995# endif
4996 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4997 }
4998 if (ta_len > 0 || len > 0)
4999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * For pipes:
5002 * Check for CTRL-C: send interrupt signal to child.
5003 * Check for CTRL-D: EOF, close pipe to child.
5004 */
5005 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
5006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 /*
5008 * Send SIGINT to the child's group or all
5009 * processes in our group.
5010 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005011 may_send_sigint(ta_buf[ta_len], pid, wpid);
5012
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (pty_master_fd < 0 && toshell_fd >= 0
5014 && ta_buf[ta_len] == Ctrl_D)
5015 {
5016 close(toshell_fd);
5017 toshell_fd = -1;
5018 }
5019 }
5020
Bram Moolenaarf4140482020-02-15 23:06:45 +01005021 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
5023 /*
5024 * For pipes: echo the typed characters.
5025 * For a pty this does not seem to work.
5026 */
5027 if (pty_master_fd < 0)
5028 {
5029 for (i = ta_len; i < ta_len + len; ++i)
5030 {
5031 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5032 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 else if (has_mbyte)
5034 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005035 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 msg_outtrans_len(ta_buf + i, l);
5038 i += l - 1;
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 else
5041 msg_outtrans_len(ta_buf + i, 1);
5042 }
5043 windgoto(msg_row, msg_col);
5044 out_flush();
5045 }
5046
5047 ta_len += len;
5048
5049 /*
5050 * Write the characters to the child, unless EOF has
5051 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005052 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005053 * When writing buffer lines, drop the typed
5054 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005056 if (options & SHELL_WRITE)
5057 ta_len = 0;
5058 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5061 if (len > 0)
5062 {
5063 ta_len -= len;
5064 mch_memmove(ta_buf, ta_buf + len, ta_len);
5065 }
5066 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
Bram Moolenaardf177f62005-02-22 08:39:57 +00005070 if (got_int)
5071 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005072 // CTRL-C sends a signal to the child, we ignore it
5073 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005074# ifdef HAVE_SETSID
5075 kill(-pid, SIGINT);
5076# else
5077 kill(0, SIGINT);
5078# endif
5079 if (wpid > 0)
5080 kill(wpid, SIGINT);
5081 got_int = FALSE;
5082 }
5083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 /*
5085 * Check if the child has any characters to be printed.
5086 * Read them and write them to our window. Repeat this as
5087 * long as there is something to do, avoid the 10ms wait
5088 * for mch_inchar(), or sending typeahead characters to
5089 * the external process.
5090 * TODO: This should handle escape sequences, compatible
5091 * to some terminal (vt52?).
5092 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005093 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005094 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005096 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005099 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005101
5102 noread_cnt = 0;
5103 if (options & SHELL_READ)
5104 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005105 // Do NUL -> NL translation, append NL separated
5106 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005107 for (i = 0; i < len; ++i)
5108 {
5109 if (buffer[i] == NL)
5110 append_ga_line(&ga);
5111 else if (buffer[i] == NUL)
5112 ga_append(&ga, NL);
5113 else
5114 ga_append(&ga, buffer[i]);
5115 }
5116 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005117 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005120 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
Bram Moolenaardf177f62005-02-22 08:39:57 +00005122 len += buffer_off;
5123 buffer[len] = NUL;
5124
Bram Moolenaar0f873732019-12-05 20:28:46 +01005125 // Check if the last character in buffer[] is
5126 // incomplete, keep these bytes for the next
5127 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 for (p = buffer; p < buffer + len; p += l)
5129 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005130 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005132 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 else if (MB_BYTE2LEN(*p) != l)
5134 break;
5135 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005136 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005138 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (len >= 12)
5140 ++p;
5141 else
5142 {
5143 buffer_off = len;
5144 continue;
5145 }
5146 }
5147 c = *p;
5148 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005149 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (p < buffer + len)
5151 {
5152 *p = c;
5153 buffer_off = (buffer + len) - p;
5154 mch_memmove(buffer, p, buffer_off);
5155 continue;
5156 }
5157 buffer_off = 0;
5158 }
5159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005162 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164
5165 windgoto(msg_row, msg_col);
5166 cursor_on();
5167 out_flush();
5168 if (got_int)
5169 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005170
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005171# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005172 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005173 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005174 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005175
Bram Moolenaar0f873732019-12-05 20:28:46 +01005176 // Avoid that we keep looping here without
5177 // checking for a CTRL-C for a long time. Don't
5178 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005179 if (msec > 2000)
5180 {
5181 noread_cnt = 5;
5182 break;
5183 }
5184 }
5185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187
Bram Moolenaar0f873732019-12-05 20:28:46 +01005188 // If we already detected the child has finished, continue
5189 // reading output for a short while. Some text may be
5190 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005191 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005192 {
5193 if (noread_cnt < 5)
5194 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005195 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005196 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 /*
5199 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005200 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005202# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005203 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005204# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5208 || (wait_pid == pid && WIFEXITED(status)))
5209 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005210 // Don't break the loop yet, try reading more
5211 // characters from "fromshell_fd" first. When using
5212 // pipes there might still be something to read and
5213 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005216 else
5217 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005218
Bram Moolenaar95a51352013-03-21 22:53:50 +01005219# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005220 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005221 clip_update();
5222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224finished:
5225 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005226 if (options & SHELL_READ)
5227 {
5228 if (ga.ga_len > 0)
5229 {
5230 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005231 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005232 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005233 }
5234 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005235 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005236 ga_clear(&ga);
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 /*
5240 * Give all typeahead that wasn't used back to ui_inchar().
5241 */
5242 if (ta_len)
5243 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 State = old_State;
5245 if (toshell_fd >= 0)
5246 close(toshell_fd);
5247 close(fromshell_fd);
5248 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005249# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005250 else
5251 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005252 long delay_msec = 1;
5253
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005254 /*
5255 * Similar to the loop above, but only handle X events, no
5256 * I/O.
5257 */
5258 for (;;)
5259 {
5260 if (got_int)
5261 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005262 // CTRL-C sends a signal to the child, we ignore it
5263 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005264# ifdef HAVE_SETSID
5265 kill(-pid, SIGINT);
5266# else
5267 kill(0, SIGINT);
5268# endif
5269 got_int = FALSE;
5270 }
5271# ifdef __NeXT__
5272 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5273# else
5274 wait_pid = waitpid(pid, &status, WNOHANG);
5275# endif
5276 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5277 || (wait_pid == pid && WIFEXITED(status)))
5278 {
5279 wait_pid = pid;
5280 break;
5281 }
5282
Bram Moolenaar0f873732019-12-05 20:28:46 +01005283 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005284 clip_update();
5285
Bram Moolenaar0f873732019-12-05 20:28:46 +01005286 // Wait for 1 to 10 msec. 1 is faster but gives the child
5287 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005288 mch_delay(delay_msec, TRUE);
5289 if (++delay_msec > 10)
5290 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005291 }
5292 }
5293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /*
5296 * Wait until our child has exited.
5297 * Ignore wait() returning pids of other children and returning
5298 * because of some signal like SIGWINCH.
5299 * Don't wait if wait_pid was already set above, indicating the
5300 * child already exited.
5301 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005302 if (wait_pid != pid)
5303 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar624891f2010-10-13 16:22:09 +02005305# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 // Close slave side of pty. Only do this after the child has
5307 // exited, otherwise the child may hang when it tries to write on
5308 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005309 if (pty_master_fd >= 0)
5310 close(pty_slave_fd);
5311# endif
5312
Bram Moolenaar0f873732019-12-05 20:28:46 +01005313 // Make sure the child that writes to the external program is
5314 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005315 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005316 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005317 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005318 wait4pid(wpid, NULL);
5319 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005320
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005321# ifdef FEAT_JOB_CHANNEL
5322 --dont_check_job_ended;
5323# endif
5324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 /*
5326 * Set to raw mode right now, otherwise a CTRL-C after
5327 * catch_signals() will kill Vim.
5328 */
5329 if (tmode == TMODE_RAW)
5330 settmode(TMODE_RAW);
5331 did_settmode = TRUE;
5332 set_signals();
5333
5334 if (WIFEXITED(status))
5335 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005336 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005338 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
5340 if (retval == EXEC_FAILED)
5341 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005342 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 msg_outtrans(p_sh);
5344 msg_putchar('\n');
5345 }
5346 else if (!(options & SHELL_SILENT))
5347 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005348 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 msg_outnum((long)retval);
5350 msg_putchar('\n');
5351 }
5352 }
5353 }
5354 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005355 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
5359error:
5360 if (!did_settmode)
5361 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005362 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363# ifdef FEAT_TITLE
5364 resettitle();
5365# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005366 vim_free(argv);
5367 vim_free(tofree1);
5368 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005371}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005372#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005373
5374 int
5375mch_call_shell(
5376 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005377 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005378{
5379#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5380 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5381 return mch_call_shell_terminal(cmd, options);
5382#endif
5383#ifdef USE_SYSTEM
5384 return mch_call_shell_system(cmd, options);
5385#else
5386 return mch_call_shell_fork(cmd, options);
5387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388}
5389
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005390#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005391 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005392mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005393{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005394 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005395 int fd_in[2] = {-1, -1}; // for stdin
5396 int fd_out[2] = {-1, -1}; // for stdout
5397 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005398 int pty_master_fd = -1;
5399 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005400 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005401 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5402 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5403 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005404 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005405 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5406 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005407 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005408 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005409 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005410
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005411 if (use_out_for_err && use_null_for_out)
5412 use_null_for_err = TRUE;
5413
Bram Moolenaar0f873732019-12-05 20:28:46 +01005414 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005415 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005416
Bram Moolenaarb2412082017-08-20 18:09:14 +02005417 if (options->jo_pty
5418 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005419 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005420 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005421 open_pty(&pty_master_fd, &pty_slave_fd,
5422 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005423
Bram Moolenaar0f873732019-12-05 20:28:46 +01005424 // TODO: without the channel feature connect the child to /dev/null?
5425 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005426 if (use_file_for_in)
5427 {
5428 char_u *fname = options->jo_io_name[PART_IN];
5429
5430 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5431 if (fd_in[0] < 0)
5432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005433 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005434 goto failed;
5435 }
5436 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005437 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005438 // When writing buffer lines to the input don't use the pty, so that
5439 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005440 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5441 && pipe(fd_in) < 0)
5442 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005443
5444 if (use_file_for_out)
5445 {
5446 char_u *fname = options->jo_io_name[PART_OUT];
5447
5448 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5449 if (fd_out[1] < 0)
5450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005451 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005452 goto failed;
5453 }
5454 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005455 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005456 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005457
5458 if (use_file_for_err)
5459 {
5460 char_u *fname = options->jo_io_name[PART_ERR];
5461
5462 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5463 if (fd_err[1] < 0)
5464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005465 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005466 goto failed;
5467 }
5468 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005469 else if (!use_out_for_err && !use_null_for_err
5470 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005471 goto failed;
5472
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005473 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5474 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005475 if (options->jo_set & JO_CHANNEL)
5476 {
5477 channel = options->jo_channel;
5478 if (channel != NULL)
5479 ++channel->ch_refcount;
5480 }
5481 else
5482 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005483 if (channel == NULL)
5484 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005485 if (job->jv_tty_out != NULL)
5486 ch_log(channel, "using pty %s on fd %d",
5487 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005488 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005489
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005490 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005491 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005492 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005494 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005495 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005496 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005497 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005498 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005499 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005500 int null_fd = -1;
5501 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005502
Bram Moolenaar0f873732019-12-05 20:28:46 +01005503 // child
5504 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005505 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506
Bram Moolenaar819524702018-02-27 19:10:00 +01005507# ifdef FEAT_JOB_CHANNEL
5508 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005509 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005510 ch_logfile((char_u *)"", (char_u *)"");
5511# endif
5512
Bram Moolenaar835dc632016-02-07 14:27:38 +01005513# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005514 // Create our own process group, so that the child and all its
5515 // children can be kill()ed. Don't do this when using pipes,
5516 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517 (void)setsid();
5518# endif
5519
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005520# ifdef FEAT_TERMINAL
5521 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005522 {
5523 char *term = (char *)T_NAME;
5524
5525#ifdef FEAT_GUI
5526 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005527 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005528 term = getenv("TERM");
5529#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005530 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005531 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005532 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005533 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005534 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005535 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005536 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005537 else if (t_colors > 16)
5538 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005539 else
5540 term = "xterm";
5541 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005542 set_child_environment(
5543 (long)options->jo_term_rows,
5544 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005545 term,
5546 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005547 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005548 else
5549# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005550 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005551
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005552 if (options->jo_env != NULL)
5553 {
5554 dict_T *dict = options->jo_env;
5555 hashitem_T *hi;
5556 int todo = (int)dict->dv_hashtab.ht_used;
5557
5558 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5559 if (!HASHITEM_EMPTY(hi))
5560 {
5561 typval_T *item = &dict_lookup(hi)->di_tv;
5562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005563 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005564 --todo;
5565 }
5566 }
5567
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005568 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005569 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005570 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005571 if (null_fd < 0)
5572 {
5573 perror("opening /dev/null failed");
5574 _exit(OPEN_NULL_FAILED);
5575 }
5576 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005577
Bram Moolenaar223896d2017-08-02 22:33:28 +02005578 if (pty_slave_fd >= 0)
5579 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005580 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005581 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005582# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005583 // Try to become controlling tty (probably doesn't work,
5584 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005585 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5586# endif
5587 }
5588
Bram Moolenaar0f873732019-12-05 20:28:46 +01005589 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005590 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005591 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005592 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005593 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005594 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005595 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005596 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005597
Bram Moolenaar0f873732019-12-05 20:28:46 +01005598 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005599 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005600 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005601 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005602 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005603 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005604 }
5605 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005606 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005607 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005608 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005609 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005610 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005611
Bram Moolenaar0f873732019-12-05 20:28:46 +01005612 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005613 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005614 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005615 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005616 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005617 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005618 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005619 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005620
5621 if (fd_in[0] >= 0)
5622 close(fd_in[0]);
5623 if (fd_in[1] >= 0)
5624 close(fd_in[1]);
5625 if (fd_out[0] >= 0)
5626 close(fd_out[0]);
5627 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005628 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005629 if (fd_err[0] >= 0)
5630 close(fd_err[0]);
5631 if (fd_err[1] >= 0)
5632 close(fd_err[1]);
5633 if (pty_master_fd >= 0)
5634 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005635 close(pty_master_fd); // not used in the child
5636 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005637 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005638
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005639 if (null_fd >= 0)
5640 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005641
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005642 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5643 _exit(EXEC_FAILED);
5644
Bram Moolenaar0f873732019-12-05 20:28:46 +01005645 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005646 execvp(argv[0], argv);
5647
Bram Moolenaar4694a172016-04-21 14:05:23 +02005648 if (stderr_works)
5649 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005650# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005651 // calling free_all_mem() here causes problems. Ignore valgrind
5652 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005653# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005654 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005655 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005656
Bram Moolenaar0f873732019-12-05 20:28:46 +01005657 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005658 UNBLOCK_SIGNALS(&curset);
5659
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005660 job->jv_pid = pid;
5661 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005662 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005663
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005664 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005665 close(pty_slave_fd); // not used in the parent
5666 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005667 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005668 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005669 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005670 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005671 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005672 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005673 if (channel != NULL)
5674 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005675 int in_fd = INVALID_FD;
5676 int out_fd = INVALID_FD;
5677 int err_fd = INVALID_FD;
5678
5679 if (!(use_file_for_in || use_null_for_in))
5680 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5681
5682 if (!(use_file_for_out || use_null_for_out))
5683 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5684
5685 // When using pty_master_fd only set it for stdout, do not duplicate
5686 // it for stderr, it only needs to be read once.
5687 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5688 {
5689 if (fd_err[0] >= 0)
5690 err_fd = fd_err[0];
5691 else if (out_fd != pty_master_fd)
5692 err_fd = pty_master_fd;
5693 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005694
5695 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005696 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005697 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005698 else
5699 {
5700 if (fd_in[1] >= 0)
5701 close(fd_in[1]);
5702 if (fd_out[0] >= 0)
5703 close(fd_out[0]);
5704 if (fd_err[0] >= 0)
5705 close(fd_err[0]);
5706 if (pty_master_fd >= 0)
5707 close(pty_master_fd);
5708 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005709
Bram Moolenaar0f873732019-12-05 20:28:46 +01005710 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005711 return;
5712
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005713failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005714 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005716 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005717 if (fd_in[1] >= 0)
5718 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005720 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005721 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005722 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005723 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005724 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005725 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005726 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005727 if (pty_master_fd >= 0)
5728 close(pty_master_fd);
5729 if (pty_slave_fd >= 0)
5730 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005731}
5732
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005733 static char_u *
5734get_signal_name(int sig)
5735{
5736 int i;
5737 char_u numbuf[NUMBUFLEN];
5738
5739 if (sig == SIGKILL)
5740 return vim_strsave((char_u *)"kill");
5741
5742 for (i = 0; signal_info[i].sig != -1; i++)
5743 if (sig == signal_info[i].sig)
5744 return strlow_save((char_u *)signal_info[i].name);
5745
5746 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5747 return vim_strsave(numbuf);
5748}
5749
Bram Moolenaar835dc632016-02-07 14:27:38 +01005750 char *
5751mch_job_status(job_T *job)
5752{
5753# ifdef HAVE_UNION_WAIT
5754 union wait status;
5755# else
5756 int status = -1;
5757# endif
5758 pid_t wait_pid = 0;
5759
5760# ifdef __NeXT__
5761 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5762# else
5763 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5764# endif
5765 if (wait_pid == -1)
5766 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005767 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005768 if (job->jv_status < JOB_ENDED)
5769 ch_log(job->jv_channel, "Job no longer exists: %s",
5770 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005771 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005772 }
5773 if (wait_pid == 0)
5774 return "run";
5775 if (WIFEXITED(status))
5776 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005777 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005778 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005779 if (job->jv_status < JOB_ENDED)
5780 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005781 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005782 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005783 if (WIFSIGNALED(status))
5784 {
5785 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005786 job->jv_termsig = get_signal_name(WTERMSIG(status));
5787 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5788 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5789 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005790 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005791 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005792 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005793
5794return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005795 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005796 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005797 return "dead";
5798}
5799
5800 job_T *
5801mch_detect_ended_job(job_T *job_list)
5802{
5803# ifdef HAVE_UNION_WAIT
5804 union wait status;
5805# else
5806 int status = -1;
5807# endif
5808 pid_t wait_pid = 0;
5809 job_T *job;
5810
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005811# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005812 // Do not do this when waiting for a shell command to finish, we would get
5813 // the exit value here (and discard it), the exit value obtained there
5814 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005815 if (dont_check_job_ended > 0)
5816 return NULL;
5817# endif
5818
Bram Moolenaar97792de2016-10-15 18:36:49 +02005819# ifdef __NeXT__
5820 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5821# else
5822 wait_pid = waitpid(-1, &status, WNOHANG);
5823# endif
5824 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005825 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005826 return NULL;
5827 for (job = job_list; job != NULL; job = job->jv_next)
5828 {
5829 if (job->jv_pid == wait_pid)
5830 {
5831 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005832 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005833 job->jv_exitval = WEXITSTATUS(status);
5834 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005835 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005836 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005837 job->jv_termsig = get_signal_name(WTERMSIG(status));
5838 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005839 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005840 {
5841 ch_log(job->jv_channel, "Job ended");
5842 job->jv_status = JOB_ENDED;
5843 }
5844 return job;
5845 }
5846 }
5847 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005848}
5849
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005850/*
5851 * Send a (deadly) signal to "job".
5852 * Return FAIL if "how" is not a valid name.
5853 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005854 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005855mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005857 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005858
Bram Moolenaar923d9262016-02-25 20:56:01 +01005859 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005860 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005861 else if (STRCMP(how, "hup") == 0)
5862 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005863 else if (STRCMP(how, "quit") == 0)
5864 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005865 else if (STRCMP(how, "int") == 0)
5866 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867 else if (STRCMP(how, "kill") == 0)
5868 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005869#ifdef SIGWINCH
5870 else if (STRCMP(how, "winch") == 0)
5871 sig = SIGWINCH;
5872#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005873 else if (isdigit(*how))
5874 sig = atoi((char *)how);
5875 else
5876 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005877
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005878 // Never kill ourselves!
5879 if (job->jv_pid != 0)
5880 {
5881 // TODO: have an option to only kill the process, not the group?
5882 kill(-job->jv_pid, sig);
5883 kill(job->jv_pid, sig);
5884 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005885
Bram Moolenaar835dc632016-02-07 14:27:38 +01005886 return OK;
5887}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005888
5889/*
5890 * Clear the data related to "job".
5891 */
5892 void
5893mch_clear_job(job_T *job)
5894{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005895 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005896# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005897 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005898# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005899 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005900# endif
5901}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005902#endif
5903
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005904#if defined(FEAT_TERMINAL) || defined(PROTO)
5905 int
5906mch_create_pty_channel(job_T *job, jobopt_T *options)
5907{
5908 int pty_master_fd = -1;
5909 int pty_slave_fd = -1;
5910 channel_T *channel;
5911
Bram Moolenaar59386482019-02-10 22:43:46 +01005912 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005913 close(pty_slave_fd);
5914
5915 channel = add_channel();
5916 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005917 {
5918 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005919 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005920 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005921 if (job->jv_tty_out != NULL)
5922 ch_log(channel, "using pty %s on fd %d",
5923 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005924 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005925 channel->ch_keep_open = TRUE;
5926
Bram Moolenaar0f873732019-12-05 20:28:46 +01005927 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5928 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005929 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005930 channel_set_job(channel, job, options);
5931 return OK;
5932}
5933#endif
5934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935/*
5936 * Check for CTRL-C typed by reading all available characters.
5937 * In cooked mode we should get SIGINT, no need to check.
5938 */
5939 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005940mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005942 if ((curr_tmode == TMODE_RAW || force)
5943 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 fill_input_buf(FALSE);
5945}
5946
5947/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005948 * Wait "msec" msec until a character is available from the mouse, keyboard,
5949 * from inbuf[].
5950 * "msec" == -1 will block forever.
5951 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005952 * When "ignore_input" is TRUE even check for pending input when input is
5953 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005954 * "interrupted" (if not NULL) is set to TRUE when no character is available
5955 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005956 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005957 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 */
5959 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005960WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005962#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005963 return ui_wait_for_chars_or_timer(
5964 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005965#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005966 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005967#endif
5968}
5969
5970/*
5971 * Wait "msec" msec until a character is available from the mouse or keyboard
5972 * or from inbuf[].
5973 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005974 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005975 * "interrupted" (if not NULL) is set to TRUE when no character is available
5976 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005977 * When a GUI is being used, this will never get called -- webb
5978 */
5979 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005980WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#ifdef FEAT_MOUSE_GPM
5983 int gpm_process_wanted;
5984#endif
5985#ifdef FEAT_XCLIPBOARD
5986 int rest;
5987#endif
5988 int avail;
5989
Bram Moolenaar0f873732019-12-05 20:28:46 +01005990 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 return 1;
5992
5993#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005994 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 if (WantQueryMouse)
5996 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005997 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02005998 if (!no_query_mouse_for_testing)
5999 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001#endif
6002
6003 /*
6004 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
6005 * events. This is a bit complicated, because they might both be defined.
6006 */
6007#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6008# ifdef FEAT_XCLIPBOARD
6009 rest = 0;
6010 if (do_xterm_trace())
6011 rest = msec;
6012# endif
6013 do
6014 {
6015# ifdef FEAT_XCLIPBOARD
6016 if (rest != 0)
6017 {
6018 msec = XT_TRACE_DELAY;
6019 if (rest >= 0 && rest < XT_TRACE_DELAY)
6020 msec = rest;
6021 if (rest >= 0)
6022 rest -= msec;
6023 }
6024# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006025# ifdef FEAT_SOUND_CANBERRA
6026 // Invoke any pending sound callbacks.
6027 if (has_sound_callback_in_queue())
6028 invoke_sound_callback();
6029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030# ifdef FEAT_MOUSE_GPM
6031 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006032 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006033 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006034 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006036 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006040 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 return 1;
6042# ifdef FEAT_XCLIPBOARD
6043 if (rest == 0 || !do_xterm_trace())
6044# endif
6045 break;
6046 }
6047 }
6048 while (FALSE
6049# ifdef FEAT_MOUSE_GPM
6050 || (gpm_process_wanted && mch_gpm_process() == 0)
6051# endif
6052# ifdef FEAT_XCLIPBOARD
6053 || (!avail && rest != 0)
6054# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006055 )
6056 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
6058#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006059 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#endif
6061 return avail;
6062}
6063
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006064#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065/*
6066 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006067 * "msec" == 0 will check for characters once.
6068 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006071 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006072 * "interrupted" (if not NULL) is set to TRUE when no character is available
6073 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#if defined(__BEOS__)
6076 int
6077#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006078 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006080RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006083 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006084#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 static int busy = FALSE;
6086
Bram Moolenaar0f873732019-12-05 20:28:46 +01006087 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088# define MAY_LOOP
6089
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006090# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006091 // Remember at what time we started, so that we know how much longer we
6092 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006093 long start_msec = msec;
6094 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006096 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006097 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098# endif
6099
Bram Moolenaar0f873732019-12-05 20:28:46 +01006100 // Handle being called recursively. This may happen for the session
6101 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 if (busy)
6103 return 0;
6104#endif
6105
6106#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006107 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108#endif
6109 {
6110#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006111 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006112# ifdef FEAT_MZSCHEME
6113 int mzquantum_used = FALSE;
6114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#endif
6116#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006117 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006118 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 int nfd;
6120# ifdef FEAT_XCLIPBOARD
6121 int xterm_idx = -1;
6122# endif
6123# ifdef FEAT_MOUSE_GPM
6124 int gpm_idx = -1;
6125# endif
6126# ifdef USE_XSMP
6127 int xsmp_idx = -1;
6128# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006129 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006131# ifdef FEAT_MZSCHEME
6132 mzvim_check_threads();
6133 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6134 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006135 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006136 mzquantum_used = TRUE;
6137 }
6138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 fds[0].fd = fd;
6140 fds[0].events = POLLIN;
6141 nfd = 1;
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006144 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (xterm_Shell != (Widget)0)
6146 {
6147 xterm_idx = nfd;
6148 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6149 fds[nfd].events = POLLIN;
6150 nfd++;
6151 }
6152# endif
6153# ifdef FEAT_MOUSE_GPM
6154 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6155 {
6156 gpm_idx = nfd;
6157 fds[nfd].fd = gpm_fd;
6158 fds[nfd].events = POLLIN;
6159 nfd++;
6160 }
6161# endif
6162# ifdef USE_XSMP
6163 if (xsmp_icefd != -1)
6164 {
6165 xsmp_idx = nfd;
6166 fds[nfd].fd = xsmp_icefd;
6167 fds[nfd].events = POLLIN;
6168 nfd++;
6169 }
6170# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006172 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006173#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006174 if (interrupted != NULL)
6175 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006177 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006178
6179 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006180 if (result == 0 && interrupted != NULL && ret > 0)
6181 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006182
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006183# ifdef FEAT_MZSCHEME
6184 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006185 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006186 finished = FALSE;
6187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189# ifdef FEAT_XCLIPBOARD
6190 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6191 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006192 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006194 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 finished = FALSE;
6196 }
6197# endif
6198# ifdef FEAT_MOUSE_GPM
6199 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201# endif
6202# ifdef USE_XSMP
6203 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6204 {
6205 if (fds[xsmp_idx].revents & POLLIN)
6206 {
6207 busy = TRUE;
6208 xsmp_handle_requests();
6209 busy = FALSE;
6210 }
6211 else if (fds[xsmp_idx].revents & POLLHUP)
6212 {
6213 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006214 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 xsmp_close();
6216 }
6217 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006218 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006221#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006222 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006223 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006224 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
Bram Moolenaar0f873732019-12-05 20:28:46 +01006227#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
6229 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006230 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006231 // These are static because they can take 8 Kbyte each and cause the
6232 // signal stack to run out with -O3.
6233 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006235 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006237# ifdef FEAT_MZSCHEME
6238 mzvim_check_threads();
6239 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6240 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006241 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006242 mzquantum_used = TRUE;
6243 }
6244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006246 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006248 tv.tv_sec = towait / 1000;
6249 tv.tv_usec = (towait % 1000) * (1000000/1000);
6250 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006252 else
6253 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 /*
6256 * Select on ready for reading and exceptional condition (end of file).
6257 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006258select_eintr:
6259 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006260 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 FD_ZERO(&efds);
6262 FD_SET(fd, &rfds);
6263# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006264 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 FD_SET(fd, &efds);
6266# endif
6267 maxfd = fd;
6268
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006270 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (xterm_Shell != (Widget)0)
6272 {
6273 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6274 if (maxfd < ConnectionNumber(xterm_dpy))
6275 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006276
Bram Moolenaar0f873732019-12-05 20:28:46 +01006277 // An event may have already been read but not handled. In
6278 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006279 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281# endif
6282# ifdef FEAT_MOUSE_GPM
6283 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6284 {
6285 FD_SET(gpm_fd, &rfds);
6286 FD_SET(gpm_fd, &efds);
6287 if (maxfd < gpm_fd)
6288 maxfd = gpm_fd;
6289 }
6290# endif
6291# ifdef USE_XSMP
6292 if (xsmp_icefd != -1)
6293 {
6294 FD_SET(xsmp_icefd, &rfds);
6295 FD_SET(xsmp_icefd, &efds);
6296 if (maxfd < xsmp_icefd)
6297 maxfd = xsmp_icefd;
6298 }
6299# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006300# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006301 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006302# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006303 if (interrupted != NULL)
6304 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
Bram Moolenaar643b6142018-09-12 20:29:09 +02006306 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6307 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006308 result = ret > 0 && FD_ISSET(fd, &rfds);
6309 if (result)
6310 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006311 else if (interrupted != NULL && ret > 0)
6312 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006313
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006314# ifdef EINTR
6315 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006316 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006317 // Check whether window has been resized, EINTR may be caused by
6318 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006319 if (do_resize)
6320 handle_resize();
6321
Bram Moolenaar0f873732019-12-05 20:28:46 +01006322 // Interrupted by a signal, need to try again. We ignore msec
6323 // here, because we do want to check even after a timeout if
6324 // characters are available. Needed for reading output of an
6325 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006326 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006327 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006328# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006329# ifdef __TANDEM
6330 if (ret == -1 && errno == ENOTSUP)
6331 {
6332 FD_ZERO(&rfds);
6333 FD_ZERO(&efds);
6334 ret = 0;
6335 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006336# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006337# ifdef FEAT_MZSCHEME
6338 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006339 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006340 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341# endif
6342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343# ifdef FEAT_XCLIPBOARD
6344 if (ret > 0 && xterm_Shell != (Widget)0
6345 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6346 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006347 xterm_update(); // Maybe we should hand out clipboard
6348 // continue looping when we only got the X event and the input
6349 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 if (--ret == 0 && !input_available())
6351 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006352 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 finished = FALSE;
6354 }
6355 }
6356# endif
6357# ifdef FEAT_MOUSE_GPM
6358 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6359 {
6360 if (FD_ISSET(gpm_fd, &efds))
6361 gpm_close();
6362 else if (FD_ISSET(gpm_fd, &rfds))
6363 *check_for_gpm = 1;
6364 }
6365# endif
6366# ifdef USE_XSMP
6367 if (ret > 0 && xsmp_icefd != -1)
6368 {
6369 if (FD_ISSET(xsmp_icefd, &efds))
6370 {
6371 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006372 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 xsmp_close();
6374 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006375 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377 else if (FD_ISSET(xsmp_icefd, &rfds))
6378 {
6379 busy = TRUE;
6380 xsmp_handle_requests();
6381 busy = FALSE;
6382 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006383 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385 }
6386# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006387#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006388 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006389 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006390 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
Bram Moolenaar0f873732019-12-05 20:28:46 +01006393#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395#ifdef MAY_LOOP
6396 if (finished || msec == 0)
6397 break;
6398
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006399# ifdef FEAT_CLIENTSERVER
6400 if (server_waiting())
6401 break;
6402# endif
6403
Bram Moolenaar0f873732019-12-05 20:28:46 +01006404 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (msec > 0)
6406 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006407# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006408 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006409 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006411 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 msec = msec / 2;
6413# endif
6414 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006415 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417#endif
6418 }
6419
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006420 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421}
6422
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006424 * Expand a path into all matching files and/or directories. Handles "*",
6425 * "?", "[a-z]", "**", etc.
6426 * "path" has backslashes before chars that are not to be expanded.
6427 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 */
6429 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006430mch_expandpath(
6431 garray_T *gap,
6432 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006433 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
Bram Moolenaar02743632005-07-25 20:42:36 +00006435 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438/*
6439 * mch_expand_wildcards() - this code does wild-card pattern matching using
6440 * the shell
6441 *
6442 * return OK for success, FAIL for error (you may lose some memory) and put
6443 * an error message in *file.
6444 *
6445 * num_pat is number of input patterns
6446 * pat is array of pointers to input patterns
6447 * num_file is pointer to number of matched file names
6448 * file is pointer to array of pointers to matched file names
6449 */
6450
6451#ifndef SEEK_SET
6452# define SEEK_SET 0
6453#endif
6454#ifndef SEEK_END
6455# define SEEK_END 2
6456#endif
6457
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006458#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006461mch_expand_wildcards(
6462 int num_pat,
6463 char_u **pat,
6464 int *num_file,
6465 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006466 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 int i;
6469 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006470 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 char_u *p;
6472 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaarc7247912008-01-13 12:54:11 +00006474 /*
6475 * This is the non-OS/2 implementation (really Unix).
6476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 int j;
6478 char_u *tempname;
6479 char_u *command;
6480 FILE *fd;
6481 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006482#define STYLE_ECHO 0 // use "echo", the default
6483#define STYLE_GLOB 1 // use "glob", for csh
6484#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6485#define STYLE_PRINT 3 // use "print -N", for zsh
6486#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6487 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 int shell_style = STYLE_ECHO;
6489 int check_spaces;
6490 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006491 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006492 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006493 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
Bram Moolenaar0f873732019-12-05 20:28:46 +01006495 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 *file = NULL;
6497
6498 /*
6499 * If there are no wildcards, just copy the names to allocated memory.
6500 * Saves a lot of time, because we don't have to start a new shell.
6501 */
6502 if (!have_wildcard(num_pat, pat))
6503 return save_patterns(num_pat, pat, num_file, file);
6504
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006505# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006506 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006507 if (sandbox != 0 && check_secure())
6508 return FAIL;
6509# endif
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 /*
6512 * Don't allow the use of backticks in secure and restricted mode.
6513 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006514 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 for (i = 0; i < num_pat; ++i)
6516 if (vim_strchr(pat[i], '`') != NULL
6517 && (check_restricted() || check_secure()))
6518 return FAIL;
6519
6520 /*
6521 * get a name for the temp file
6522 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006523 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006525 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 return FAIL;
6527 }
6528
6529 /*
6530 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006531 * file.
6532 * STYLE_BT: NL separated
6533 * If expanding `cmd` execute it directly.
6534 * STYLE_GLOB: NUL separated
6535 * If we use *csh, "glob" will work better than "echo".
6536 * STYLE_PRINT: NL or NUL separated
6537 * If we use *zsh, "print -N" will work better than "glob".
6538 * STYLE_VIMGLOB: NL separated
6539 * If we use *sh*, we define "vimglob()".
6540 * STYLE_ECHO: space separated.
6541 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 */
6543 if (num_pat == 1 && *pat[0] == '`'
6544 && (len = STRLEN(pat[0])) > 2
6545 && *(pat[0] + len - 1) == '`')
6546 shell_style = STYLE_BT;
6547 else if ((len = STRLEN(p_sh)) >= 3)
6548 {
6549 if (STRCMP(p_sh + len - 3, "csh") == 0)
6550 shell_style = STYLE_GLOB;
6551 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6552 shell_style = STYLE_PRINT;
6553 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006554 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6555 "sh") != NULL)
6556 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
Bram Moolenaar0f873732019-12-05 20:28:46 +01006558 // Compute the length of the command. We need 2 extra bytes: for the
6559 // optional '&' and for the NUL.
6560 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006562 if (shell_style == STYLE_VIMGLOB)
6563 len += STRLEN(sh_vimglob_func);
6564
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006565 for (i = 0; i < num_pat; ++i)
6566 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 // Count the length of the patterns in the same way as they are put in
6568 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006569#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006570 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006571#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006572 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006573 for (j = 0; pat[i][j] != NUL; ++j)
6574 {
6575 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006576 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006577 ++len;
6578 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006579#endif
6580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 command = alloc(len);
6582 if (command == NULL)
6583 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006584 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 vim_free(tempname);
6586 return FAIL;
6587 }
6588
6589 /*
6590 * Build the shell command:
6591 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6592 * recognizes this).
6593 * - Add the shell command to print the expanded names.
6594 * - Add the temp file name.
6595 * - Add the file name patterns.
6596 */
6597 if (shell_style == STYLE_BT)
6598 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006599 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006600 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006601 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006603 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006604 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006606 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006608 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 *p = ' ';
6610 }
6611 STRCAT(command, ">");
6612 }
6613 else
6614 {
6615 if (flags & EW_NOTFOUND)
6616 STRCPY(command, "set nonomatch; ");
6617 else
6618 STRCPY(command, "unset nonomatch; ");
6619 if (shell_style == STYLE_GLOB)
6620 STRCAT(command, "glob >");
6621 else if (shell_style == STYLE_PRINT)
6622 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006623 else if (shell_style == STYLE_VIMGLOB)
6624 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 else
6626 STRCAT(command, "echo >");
6627 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006628
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (shell_style != STYLE_BT)
6632 for (i = 0; i < num_pat; ++i)
6633 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006634 // When using system() always add extra quotes, because the shell
6635 // is started twice. Otherwise put a backslash before special
6636 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#ifdef USE_SYSTEM
6638 STRCAT(command, " \"");
6639 STRCAT(command, pat[i]);
6640 STRCAT(command, "\"");
6641#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006642 int intick = FALSE;
6643
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 p = command + STRLEN(command);
6645 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006646 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006647 {
6648 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006649 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006650 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6651 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006652 // Remove a backslash, take char literally. But keep
6653 // backslash inside backticks, before a special character
6654 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006655 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006656 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6657 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006658 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006659 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006660 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006661 else if (!intick
6662 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6663 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006664 // Put a backslash before a special character, but not
6665 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6666 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006667 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006668
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006670 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 *p = NUL;
6673#endif
6674 }
6675 if (flags & EW_SILENT)
6676 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006677 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006678 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 /*
6681 * Using zsh -G: If a pattern has no matches, it is just deleted from
6682 * the argument list, otherwise zsh gives an error message and doesn't
6683 * expand any other pattern.
6684 */
6685 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006686 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688 /*
6689 * If we use -f then shell variables set in .cshrc won't get expanded.
6690 * vi can do it, so we will too, but it is only necessary if there is a "$"
6691 * in one of the patterns, otherwise we can still use the fast option.
6692 */
6693 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006694 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696 /*
6697 * execute the shell command
6698 */
6699 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6700
Bram Moolenaar0f873732019-12-05 20:28:46 +01006701 // When running in the background, give it some time to create the temp
6702 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006703 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 mch_delay(10L, TRUE);
6705
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 show_shell_mess = TRUE;
6708 vim_free(command);
6709
Bram Moolenaar0f873732019-12-05 20:28:46 +01006710 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
6712 mch_remove(tempname);
6713 vim_free(tempname);
6714 /*
6715 * With interactive completion, the error message is not printed.
6716 * However with USE_SYSTEM, I don't know how to turn off error messages
6717 * from the shell, so screen may still get messed up -- webb.
6718 */
6719#ifndef USE_SYSTEM
6720 if (!(flags & EW_SILENT))
6721#endif
6722 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006723 redraw_later_clear(); // probably messed up screen
6724 msg_putchar('\n'); // clear bottom line quickly
6725 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726#ifdef USE_SYSTEM
6727 if (!(flags & EW_SILENT))
6728#endif
6729 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006730 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006731 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
6733 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6735 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (shell_style == STYLE_BT)
6737 return FAIL;
6738 goto notfound;
6739 }
6740
6741 /*
6742 * read the names from the file into memory
6743 */
6744 fd = fopen((char *)tempname, READBIN);
6745 if (fd == NULL)
6746 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006747 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (!(flags & EW_SILENT))
6749 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006750 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006751 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753 vim_free(tempname);
6754 goto notfound;
6755 }
6756 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006757 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006759 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006760 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006761 buffer = NULL;
6762 else
6763 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (buffer == NULL)
6765 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006766 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 mch_remove(tempname);
6768 vim_free(tempname);
6769 fclose(fd);
6770 return FAIL;
6771 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006772 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 i = fread((char *)buffer, 1, len, fd);
6774 fclose(fd);
6775 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006776 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006778 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006779 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 vim_free(tempname);
6781 vim_free(buffer);
6782 return FAIL;
6783 }
6784 vim_free(tempname);
6785
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006786# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006787 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006789 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6791 *p++ = buffer[i];
6792 len = p - buffer;
6793# endif
6794
6795
Bram Moolenaar0f873732019-12-05 20:28:46 +01006796 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (shell_style == STYLE_ECHO)
6798 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006799 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006801 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
6803 while (*p != ' ' && *p != '\n')
6804 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
6807 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006808 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006809 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006811 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006813 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
6815 while (*p != '\n' && *p != NUL)
6816 ++p;
6817 if (*p != NUL)
6818 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006822 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 else
6824 {
6825 /*
6826 * Some versions of zsh use spaces instead of NULs to separate
6827 * results. Only do this when there is no NUL before the end of the
6828 * buffer, otherwise we would never be able to use file names with
6829 * embedded spaces when zsh does use NULs.
6830 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6831 * don't check for spaces again.
6832 */
6833 check_spaces = FALSE;
6834 if (shell_style == STYLE_PRINT && !did_find_nul)
6835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006836 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006837 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006838 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 did_find_nul = TRUE;
6840 else
6841 check_spaces = TRUE;
6842 }
6843
6844 /*
6845 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6846 * already is one, for STYLE_GLOB it needs to be added.
6847 */
6848 if (len && buffer[len - 1] == NUL)
6849 --len;
6850 else
6851 buffer[len] = NUL;
6852 i = 0;
6853 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006854 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
6856 ++i;
6857 *p = NUL;
6858 }
6859 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006860 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862 if (i == 0)
6863 {
6864 /*
6865 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6866 * /bin/sh will happily expand it to nothing rather than returning an
6867 * error; and hey, it's good to check anyway -- webb.
6868 */
6869 vim_free(buffer);
6870 goto notfound;
6871 }
6872 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006873 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 if (*file == NULL)
6875 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006876 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 vim_free(buffer);
6878 return FAIL;
6879 }
6880
6881 /*
6882 * Isolate the individual file names.
6883 */
6884 p = buffer;
6885 for (i = 0; i < *num_file; ++i)
6886 {
6887 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006888 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006889 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6890 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006892 while (!(shell_style == STYLE_ECHO && *p == ' ')
6893 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006895 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 *p = NUL;
6897 else
6898 {
6899 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006905 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006907 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
6909 }
6910
6911 /*
6912 * Move the file names to allocated memory.
6913 */
6914 for (j = 0, i = 0; i < *num_file; ++i)
6915 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006916 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6918 continue;
6919
Bram Moolenaar0f873732019-12-05 20:28:46 +01006920 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 dir = (mch_isdir((*file)[i]));
6922 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6923 continue;
6924
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006926 if (!dir && (flags & EW_EXEC)
6927 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006928 continue;
6929
Bram Moolenaar964b3742019-05-24 18:54:09 +02006930 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (p)
6932 {
6933 STRCPY(p, (*file)[i]);
6934 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 (*file)[j++] = p;
6937 }
6938 }
6939 vim_free(buffer);
6940 *num_file = j;
6941
Bram Moolenaar0f873732019-12-05 20:28:46 +01006942 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006944 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 goto notfound;
6946 }
6947
6948 return OK;
6949
6950notfound:
6951 if (flags & EW_NOTFOUND)
6952 return save_patterns(num_pat, pat, num_file, file);
6953 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954}
6955
Bram Moolenaar0f873732019-12-05 20:28:46 +01006956#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006959save_patterns(
6960 int num_pat,
6961 char_u **pat,
6962 int *num_file,
6963 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006966 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006968 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 if (*file == NULL)
6970 return FAIL;
6971 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006972 {
6973 s = vim_strsave(pat[i]);
6974 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006975 // Be compatible with expand_filename(): halve the number of
6976 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006977 backslash_halve(s);
6978 (*file)[i] = s;
6979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 *num_file = num_pat;
6981 return OK;
6982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984/*
6985 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6986 * expand.
6987 */
6988 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006989mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006991 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (*p == '\\' && p[1] != NUL)
6994 ++p;
6995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (vim_strchr((char_u *)
6997#ifdef VMS
6998 "*?%"
6999#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#endif
7002 , *p) != NULL)
7003 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
7005 return FALSE;
7006}
7007
7008/*
7009 * Return TRUE if the string "p" contains a wildcard.
7010 * Don't recognize '~' at the end as a wildcard.
7011 */
7012 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007013mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007015 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (*p == '\\' && p[1] != NUL)
7018 ++p;
7019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (vim_strchr((char_u *)
7021#ifdef VMS
7022 "*?%$"
7023#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025#endif
7026 , *p) != NULL
7027 || (*p == '~' && p[1] != NUL))
7028 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030 return FALSE;
7031}
7032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007034have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 int i;
7037
7038 for (i = 0; i < num; i++)
7039 if (mch_has_wildcard(file[i]))
7040 return 1;
7041 return 0;
7042}
7043
7044 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007045have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 int i;
7048
7049 for (i = 0; i < num; i++)
7050 if (vim_strchr(file[i], '$') != NULL)
7051 return TRUE;
7052 return FALSE;
7053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007055#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056/*
7057 * Scaled-down version of rename(), which is missing in Xenix.
7058 * This version can only move regular files and will fail if the
7059 * destination exists.
7060 */
7061 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007062mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 struct stat st;
7065
Bram Moolenaar0f873732019-12-05 20:28:46 +01007066 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007068 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007070 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 return 0;
7072 return -1;
7073}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007074#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007076#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077/*
7078 * Initializes connection with gpm (if it isn't already opened)
7079 * Return 1 if succeeded (or connection already opened), 0 if failed
7080 */
7081 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007082gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007084 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
7086 if (!gpm_flag)
7087 {
7088 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7089 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007090 // Default handling for mouse move
7091 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 gpm_connect.maxMod = 0xffff;
7093 if (Gpm_Open(&gpm_connect, 0) > 0)
7094 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007095 // gpm library tries to handling TSTP causes
7096 // problems. Anyways, we close connection to Gpm whenever
7097 // we are going to suspend or starting an external process
7098 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007099# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007101# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007102 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 }
7104 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007105 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 return 0;
7107 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007108 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109}
7110
7111/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007112 * Returns TRUE if the GPM mouse is enabled.
7113 */
7114 int
7115gpm_enabled(void)
7116{
7117 return gpm_flag && gpm_fd >= 0;
7118}
7119
7120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 */
7123 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007124gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007126 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 Gpm_Close();
7128}
7129
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007130/*
7131 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007133 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 */
7135 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007136mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
7138 int button;
7139 static Gpm_Event gpm_event;
7140 char_u string[6];
7141 int_u vim_modifiers;
7142 int row,col;
7143 unsigned char buttons_mask;
7144 unsigned char gpm_modifiers;
7145 static unsigned char old_buttons = 0;
7146
7147 Gpm_GetEvent(&gpm_event);
7148
7149#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007150 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (hold_gui_events)
7152 return 0;
7153#endif
7154
7155 row = gpm_event.y - 1;
7156 col = gpm_event.x - 1;
7157
Bram Moolenaar0f873732019-12-05 20:28:46 +01007158 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 string[1] = 'M';
7160 string[2] = 'G';
7161 switch (GPM_BARE_EVENTS(gpm_event.type))
7162 {
7163 case GPM_DRAG:
7164 string[3] = MOUSE_DRAG;
7165 break;
7166 case GPM_DOWN:
7167 buttons_mask = gpm_event.buttons & ~old_buttons;
7168 old_buttons = gpm_event.buttons;
7169 switch (buttons_mask)
7170 {
7171 case GPM_B_LEFT:
7172 button = MOUSE_LEFT;
7173 break;
7174 case GPM_B_MIDDLE:
7175 button = MOUSE_MIDDLE;
7176 break;
7177 case GPM_B_RIGHT:
7178 button = MOUSE_RIGHT;
7179 break;
7180 default:
7181 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007182 // Don't know what to do. Can more than one button be
7183 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
7185 string[3] = (char_u)(button | 0x20);
7186 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7187 break;
7188 case GPM_UP:
7189 string[3] = MOUSE_RELEASE;
7190 old_buttons &= ~gpm_event.buttons;
7191 break;
7192 default:
7193 return 0;
7194 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007195 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 gpm_modifiers = gpm_event.modifiers;
7197 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007198 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7199 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7200 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7202 vim_modifiers |= MOUSE_SHIFT;
7203
7204 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7205 vim_modifiers |= MOUSE_CTRL;
7206 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7207 vim_modifiers |= MOUSE_ALT;
7208 string[3] |= vim_modifiers;
7209 string[4] = (char_u)(col + ' ' + 1);
7210 string[5] = (char_u)(row + ' ' + 1);
7211 add_to_input_buf(string, 6);
7212 return 6;
7213}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007214#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007216#ifdef FEAT_SYSMOUSE
7217/*
7218 * Initialize connection with sysmouse.
7219 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7220 * output, any sysmouse output than will be processed via sig_sysmouse().
7221 * Return OK if succeeded, FAIL if failed.
7222 */
7223 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007224sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007225{
7226 struct mouse_info mouse;
7227
7228 mouse.operation = MOUSE_MODE;
7229 mouse.u.mode.mode = 0;
7230 mouse.u.mode.signal = SIGUSR2;
7231 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7232 {
7233 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7234 mouse.operation = MOUSE_SHOW;
7235 ioctl(1, CONS_MOUSECTL, &mouse);
7236 return OK;
7237 }
7238 return FAIL;
7239}
7240
7241/*
7242 * Stop processing SIGUSR2 signals, and also make sure that
7243 * virtual console do not send us any sysmouse related signal.
7244 */
7245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007246sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007247{
7248 struct mouse_info mouse;
7249
7250 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7251 mouse.operation = MOUSE_MODE;
7252 mouse.u.mode.mode = 0;
7253 mouse.u.mode.signal = 0;
7254 ioctl(1, CONS_MOUSECTL, &mouse);
7255}
7256
7257/*
7258 * Gets info from sysmouse and adds special keys to input buf.
7259 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007260 static RETSIGTYPE
7261sig_sysmouse SIGDEFARG(sigarg)
7262{
7263 struct mouse_info mouse;
7264 struct video_info video;
7265 char_u string[6];
7266 int row, col;
7267 int button;
7268 int buttons;
7269 static int oldbuttons = 0;
7270
7271#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007272 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007273 if (hold_gui_events)
7274 return;
7275#endif
7276
7277 mouse.operation = MOUSE_GETINFO;
7278 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7279 && ioctl(1, FBIO_MODEINFO, &video) != -1
7280 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7281 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7282 {
7283 row = mouse.u.data.y / video.vi_cheight;
7284 col = mouse.u.data.x / video.vi_cwidth;
7285 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007286 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007287 string[1] = 'M';
7288 string[2] = 'S';
7289 if (oldbuttons == buttons && buttons != 0)
7290 {
7291 button = MOUSE_DRAG;
7292 }
7293 else
7294 {
7295 switch (buttons)
7296 {
7297 case 0:
7298 button = MOUSE_RELEASE;
7299 break;
7300 case 1:
7301 button = MOUSE_LEFT;
7302 break;
7303 case 2:
7304 button = MOUSE_MIDDLE;
7305 break;
7306 case 4:
7307 button = MOUSE_RIGHT;
7308 break;
7309 default:
7310 return;
7311 }
7312 oldbuttons = buttons;
7313 }
7314 string[3] = (char_u)(button);
7315 string[4] = (char_u)(col + ' ' + 1);
7316 string[5] = (char_u)(row + ' ' + 1);
7317 add_to_input_buf(string, 6);
7318 }
7319 return;
7320}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007321#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007322
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007324typedef char_u * (*STRPROCSTR)(char_u *);
7325typedef char_u * (*INTPROCSTR)(int);
7326typedef int (*STRPROCINT)(char_u *);
7327typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329/*
7330 * Call a DLL routine which takes either a string or int param
7331 * and returns an allocated string.
7332 */
7333 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007334mch_libcall(
7335 char_u *libname,
7336 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007337 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007338 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007339 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007340 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342# if defined(USE_DLOPEN)
7343 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007344 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345# else
7346 shl_t hinstLib;
7347# endif
7348 STRPROCSTR ProcAdd;
7349 INTPROCSTR ProcAddI;
7350 char_u *retval_str = NULL;
7351 int retval_int = 0;
7352 int success = FALSE;
7353
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007354 /*
7355 * Get a handle to the DLL module.
7356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007358 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007359 (void)dlerror();
7360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 hinstLib = dlopen((char *)libname, RTLD_LAZY
7362# ifdef RTLD_LOCAL
7363 | RTLD_LOCAL
7364# endif
7365 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007366 if (hinstLib == NULL)
7367 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007368 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007369 dlerr = (char *)dlerror();
7370 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007371 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373# else
7374 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7375# endif
7376
Bram Moolenaar0f873732019-12-05 20:28:46 +01007377 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (hinstLib != NULL)
7379 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007380# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /*
7382 * Catch a crash when calling the library function. For example when
7383 * using a number where a string pointer is expected.
7384 */
7385 mch_startjmp();
7386 if (SETJMP(lc_jump_env) != 0)
7387 {
7388 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007389# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007390 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 mch_didjmp();
7393 }
7394 else
7395# endif
7396 {
7397 retval_str = NULL;
7398 retval_int = 0;
7399
7400 if (argstring != NULL)
7401 {
7402# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007403 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007404 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405# else
7406 if (shl_findsym(&hinstLib, (const char *)funcname,
7407 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7408 ProcAdd = NULL;
7409# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007410 if ((success = (ProcAdd != NULL
7411# if defined(USE_DLOPEN)
7412 && dlerr == NULL
7413# endif
7414 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
7416 if (string_result == NULL)
7417 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7418 else
7419 retval_str = (ProcAdd)(argstring);
7420 }
7421 }
7422 else
7423 {
7424# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007425 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007426 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427# else
7428 if (shl_findsym(&hinstLib, (const char *)funcname,
7429 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7430 ProcAddI = NULL;
7431# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007432 if ((success = (ProcAddI != NULL
7433# if defined(USE_DLOPEN)
7434 && dlerr == NULL
7435# endif
7436 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
7438 if (string_result == NULL)
7439 retval_int = ((INTPROCINT)ProcAddI)(argint);
7440 else
7441 retval_str = (ProcAddI)(argint);
7442 }
7443 }
7444
Bram Moolenaar0f873732019-12-05 20:28:46 +01007445 // Save the string before we free the library.
7446 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (string_result == NULL)
7448 *number_result = retval_int;
7449 else if (retval_str != NULL
7450 && retval_str != (char_u *)1
7451 && retval_str != (char_u *)-1)
7452 *string_result = vim_strsave(retval_str);
7453 }
7454
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007455# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 mch_endjmp();
7457# ifdef SIGHASARG
7458 if (lc_signal != 0)
7459 {
7460 int i;
7461
Bram Moolenaar0f873732019-12-05 20:28:46 +01007462 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 for (i = 0; signal_info[i].sig != -1; i++)
7464 if (lc_signal == signal_info[i].sig)
7465 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007466 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 }
7468# endif
7469# endif
7470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007472 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007473 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007474 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007475
Bram Moolenaar0f873732019-12-05 20:28:46 +01007476 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 (void)dlclose(hinstLib);
7478# else
7479 (void)shl_unload(hinstLib);
7480# endif
7481 }
7482
7483 if (!success)
7484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007485 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 return FAIL;
7487 }
7488
7489 return OK;
7490}
7491#endif
7492
7493#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007494static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495static int xterm_button;
7496
7497/*
7498 * Setup a dummy window for X selections in a terminal.
7499 */
7500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007501setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
7503 int z = 0;
7504 char *strp = "";
7505 Widget AppShell;
7506
7507 if (!x_connect_to_server())
7508 return;
7509
7510 open_app_context();
7511 if (app_context != NULL && xterm_Shell == (Widget)0)
7512 {
7513 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007514# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007516# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007517# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007518 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519
7520 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007521 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522# endif
7523
Bram Moolenaar0f873732019-12-05 20:28:46 +01007524 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 oldhandler = XSetErrorHandler(x_error_check);
7526
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007527# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007528 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7530 mch_startjmp();
7531 if (SETJMP(lc_jump_env) != 0)
7532 {
7533 mch_didjmp();
7534 xterm_dpy = NULL;
7535 }
7536 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 {
7539 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7540 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007541 if (xterm_dpy != NULL)
7542 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007543# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007548# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007549 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007551# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007552 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 (void)XSetErrorHandler(oldhandler);
7554
7555 if (xterm_dpy == NULL)
7556 {
7557 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007558 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 return;
7560 }
7561
Bram Moolenaar0f873732019-12-05 20:28:46 +01007562 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 (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
Bram Moolenaar0f873732019-12-05 20:28:46 +01007574 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 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);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007603 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 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 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007655 // Get the hints just before tracking starts. The font size might
7656 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007657 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 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007662 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 return FALSE;
7664 }
7665
Bram Moolenaar0f873732019-12-05 20:28:46 +01007666 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 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
Bram Moolenaar0f873732019-12-05 20:28:46 +01007672 // 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.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 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
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007713# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714/*
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 Moolenaar0f873732019-12-05 20:28:46 +01007728 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 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 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007738 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 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 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007781 // 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 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007796 // 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 Moolenaar0554fa42019-06-14 21:36:54 +02007803clip_xterm_own_selection(Clipboard_T *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 Moolenaar0554fa42019-06-14 21:36:54 +02007811clip_xterm_lose_selection(Clipboard_T *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 Moolenaar0554fa42019-06-14 21:36:54 +02007818clip_xterm_request_selection(Clipboard_T *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 Moolenaar0554fa42019-06-14 21:36:54 +02007825clip_xterm_set_selection(Clipboard_T *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 Moolenaar071d4272004-06-13 20:20:40 +00007836
7837# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838/*
7839 * This is our chance to ask the user if they want to save,
7840 * or abort the logout
7841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007843xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 cmdmod_T save_cmdmod;
7846 int cancel_shutdown = False;
7847
7848 save_cmdmod = cmdmod;
7849 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007850 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007851 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 cancel_shutdown = True;
7853 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007854 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 out_flush();
7856
Bram Moolenaar0f873732019-12-05 20:28:46 +01007857 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 SmcInteractDone(smc_conn, cancel_shutdown);
7859
Bram Moolenaar0f873732019-12-05 20:28:46 +01007860 // Finish off
7861 // Only end save-yourself here if we're not cancelling shutdown;
7862 // we'll get a cancelled callback later in which we'll end it.
7863 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (!cancel_shutdown)
7865 {
7866 xsmp.save_yourself = False;
7867 SmcSaveYourselfDone(smc_conn, True);
7868 }
7869}
7870# endif
7871
7872/*
7873 * Callback that starts save-yourself.
7874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007876xsmp_handle_save_yourself(
7877 SmcConn smc_conn,
7878 SmPointer client_data UNUSED,
7879 int save_type UNUSED,
7880 Bool shutdown,
7881 int interact_style UNUSED,
7882 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007884 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (xsmp.save_yourself)
7886 SmcSaveYourselfDone(smc_conn, True);
7887 xsmp.save_yourself = True;
7888 xsmp.shutdown = shutdown;
7889
Bram Moolenaar0f873732019-12-05 20:28:46 +01007890 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007892 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893
7894 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007895 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896
7897# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007898 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007900 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 SmcInteractRequest(smc_conn, SmDialogError,
7902 xsmp_handle_interaction, client_data);
7903 else
7904# endif
7905 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007906 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 SmcSaveYourselfDone(smc_conn, True);
7908 xsmp.save_yourself = False;
7909 }
7910}
7911
7912
7913/*
7914 * Callback to warn us of imminent death.
7915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007917xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 xsmp_close();
7920
Bram Moolenaar0f873732019-12-05 20:28:46 +01007921 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 getout_preserve_modified(0);
7923}
7924
7925
7926/*
7927 * Callback to tell us that save-yourself has completed.
7928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007930xsmp_save_complete(
7931 SmcConn smc_conn UNUSED,
7932 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 xsmp.save_yourself = False;
7935}
7936
7937
7938/*
7939 * Callback to tell us that an instigated shutdown was cancelled
7940 * (maybe even by us)
7941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007943xsmp_shutdown_cancelled(
7944 SmcConn smc_conn,
7945 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947 if (xsmp.save_yourself)
7948 SmcSaveYourselfDone(smc_conn, True);
7949 xsmp.save_yourself = False;
7950 xsmp.shutdown = False;
7951}
7952
7953
7954/*
7955 * Callback to tell us that a new ICE connection has been established.
7956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007958xsmp_ice_connection(
7959 IceConn iceConn,
7960 IcePointer clientData UNUSED,
7961 Bool opening,
7962 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007964 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (opening)
7966 {
7967 xsmp_icefd = IceConnectionNumber(iceConn);
7968 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7969 }
7970}
7971
7972
Bram Moolenaar0f873732019-12-05 20:28:46 +01007973// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007975xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 Bool rep;
7978
7979 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7980 == IceProcessMessagesIOError)
7981 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007982 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007984 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 xsmp_close();
7986 return FAIL;
7987 }
7988 else
7989 return OK;
7990}
7991
7992static int dummy;
7993
Bram Moolenaar0f873732019-12-05 20:28:46 +01007994// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 void
7996xsmp_init(void)
7997{
7998 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 SmcCallbacks smcallbacks;
8000#if 0
8001 SmPropValue smname;
8002 SmProp smnameprop;
8003 SmProp *smprops[1];
8004#endif
8005
8006 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008007 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008
8009 xsmp.save_yourself = xsmp.shutdown = False;
8010
Bram Moolenaar0f873732019-12-05 20:28:46 +01008011 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8013 smcallbacks.save_yourself.client_data = NULL;
8014 smcallbacks.die.callback = xsmp_die;
8015 smcallbacks.die.client_data = NULL;
8016 smcallbacks.save_complete.callback = xsmp_save_complete;
8017 smcallbacks.save_complete.client_data = NULL;
8018 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8019 smcallbacks.shutdown_cancelled.client_data = NULL;
8020
Bram Moolenaar0f873732019-12-05 20:28:46 +01008021 // Set up a watch on ICE connection creations. The "dummy" argument is
8022 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8024 {
8025 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008026 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 return;
8028 }
8029
Bram Moolenaar0f873732019-12-05 20:28:46 +01008030 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 xsmp.smcconn = SmcOpenConnection(
8032 NULL,
8033 NULL,
8034 SmProtoMajor,
8035 SmProtoMinor,
8036 SmcSaveYourselfProcMask | SmcDieProcMask
8037 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8038 &smcallbacks,
8039 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008040 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008041 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 errorstring);
8043 if (xsmp.smcconn == NULL)
8044 {
8045 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008048 {
8049 vim_snprintf(errorreport, sizeof(errorreport),
8050 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008051 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 return;
8054 }
8055 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8056
8057#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008058 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 smname.value = "vim";
8060 smname.length = 3;
8061 smnameprop.name = "SmProgram";
8062 smnameprop.type = "SmARRAY8";
8063 smnameprop.num_vals = 1;
8064 smnameprop.vals = &smname;
8065
8066 smprops[0] = &smnameprop;
8067 SmcSetProperties(xsmp.smcconn, 1, smprops);
8068#endif
8069}
8070
8071
Bram Moolenaar0f873732019-12-05 20:28:46 +01008072// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008074xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 if (xsmp_icefd != -1)
8077 {
8078 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008079 if (xsmp.clientid != NULL)
8080 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008081 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 xsmp_icefd = -1;
8083 }
8084}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008085#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086
8087
8088#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008089// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090char CtrlTable[] =
8091{
8092/* 00 - 5E */
8093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8094 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8097 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8099/* ^ */ 0x1E,
8100/* - */ 0x1F,
8101/* 61 - 6C */
8102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8103/* _ */ 0x1F,
8104/* 6E - 80 */
8105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8106/* a */ 0x01,
8107/* b */ 0x02,
8108/* c */ 0x03,
8109/* d */ 0x37,
8110/* e */ 0x2D,
8111/* f */ 0x2E,
8112/* g */ 0x2F,
8113/* h */ 0x16,
8114/* i */ 0x05,
8115/* 8A - 90 */
8116 0, 0, 0, 0, 0, 0, 0,
8117/* j */ 0x15,
8118/* k */ 0x0B,
8119/* l */ 0x0C,
8120/* m */ 0x0D,
8121/* n */ 0x0E,
8122/* o */ 0x0F,
8123/* p */ 0x10,
8124/* q */ 0x11,
8125/* r */ 0x12,
8126/* 9A - A1 */
8127 0, 0, 0, 0, 0, 0, 0, 0,
8128/* s */ 0x13,
8129/* t */ 0x3C,
8130/* u */ 0x3D,
8131/* v */ 0x32,
8132/* w */ 0x26,
8133/* x */ 0x18,
8134/* y */ 0x19,
8135/* z */ 0x3F,
8136/* AA - AC */
8137 0, 0, 0,
8138/* [ */ 0x27,
8139/* AE - BC */
8140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8141/* ] */ 0x1D,
8142/* BE - C0 */ 0, 0, 0,
8143/* A */ 0x01,
8144/* B */ 0x02,
8145/* C */ 0x03,
8146/* D */ 0x37,
8147/* E */ 0x2D,
8148/* F */ 0x2E,
8149/* G */ 0x2F,
8150/* H */ 0x16,
8151/* I */ 0x05,
8152/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8153/* J */ 0x15,
8154/* K */ 0x0B,
8155/* L */ 0x0C,
8156/* M */ 0x0D,
8157/* N */ 0x0E,
8158/* O */ 0x0F,
8159/* P */ 0x10,
8160/* Q */ 0x11,
8161/* R */ 0x12,
8162/* DA - DF */ 0, 0, 0, 0, 0, 0,
8163/* \ */ 0x1C,
8164/* E1 */ 0,
8165/* S */ 0x13,
8166/* T */ 0x3C,
8167/* U */ 0x3D,
8168/* V */ 0x32,
8169/* W */ 0x26,
8170/* X */ 0x18,
8171/* Y */ 0x19,
8172/* Z */ 0x3F,
8173/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8175};
8176
8177char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008178{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8180 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8181 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8182 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8183};
8184
8185
Bram Moolenaar0f873732019-12-05 20:28:46 +01008186// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008188{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8190 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8191 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8192 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8193};
8194
8195
8196#endif