blob: aea3d5a9e2df38780d07cd732d14393f793de7e3 [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 Moolenaar0f873732019-12-05 20:28:46 +01004524 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525# ifdef FEAT_TITLE
4526 resettitle();
4527# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004528# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4529 restore_clipboard();
4530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
Bram Moolenaar0f873732019-12-05 20:28:46 +01004534#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4537 // 127, some shells use that already
4538# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar13568252018-03-16 20:46:58 +01004540/*
4541 * Don't use system(), use fork()/exec().
4542 */
4543 static int
4544mch_call_shell_fork(
4545 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004546 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004547{
4548 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004550 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 pid_t wait_pid = 0;
4552# ifdef HAVE_UNION_WAIT
4553 union wait status;
4554# else
4555 int status = -1;
4556# endif
4557 int retval = -1;
4558 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004559 char_u *tofree1 = NULL;
4560 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004562 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004563# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004566 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 int fd_fromshell[2];
4568 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004569 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 out_flush();
4572 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004573 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004575 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004576 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579 * For the GUI, when writing the output into the buffer and when reading
4580 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4581 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004583 if ((options & (SHELL_READ|SHELL_WRITE))
4584# ifdef FEAT_GUI
4585 || (gui.in_use && show_shell_mess)
4586# endif
4587 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004589# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 /*
4591 * Try to open a master pty.
4592 * If this works, open the slave pty.
4593 * If the slave can't be opened, close the master pty.
4594 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004595 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004596 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 /*
4598 * If not opening a pty or it didn't work, try using pipes.
4599 */
4600 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
4603 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004604 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004607 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 close(fd_toshell[0]);
4610 close(fd_toshell[1]);
4611 }
4612 }
4613 if (pipe_error)
4614 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004615 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 out_flush();
4617 }
4618 }
4619 }
4620
Bram Moolenaar0f873732019-12-05 20:28:46 +01004621 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004623 SIGSET_DECL(curset)
4624
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004625# if defined(__BEOS__) && USE_THREAD_FOR_INPUT_WITH_TIMEOUT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 beos_cleanup_read_thread();
4627# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004628
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004629 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004630 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004631 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004633 UNBLOCK_SIGNALS(&curset);
4634
Bram Moolenaar32526b32019-01-19 17:43:09 +01004635 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004636 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004638 || (gui.in_use && show_shell_mess)
4639# endif
4640 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004642# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004643 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 close(pty_master_fd);
4646 close(pty_slave_fd);
4647 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 close(fd_toshell[0]);
4652 close(fd_toshell[1]);
4653 close(fd_fromshell[0]);
4654 close(fd_fromshell[1]);
4655 }
4656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004658 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004660 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004661 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
Bram Moolenaar819524702018-02-27 19:10:00 +01004663# ifdef FEAT_JOB_CHANNEL
4664 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004665 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004666 ch_logfile((char_u *)"", (char_u *)"");
4667# endif
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (!show_shell_mess || (options & SHELL_EXPAND))
4670 {
4671 int fd;
4672
4673 /*
4674 * Don't want to show any message from the shell. Can't just
4675 * close stdout and stderr though, because some systems will
4676 * break if you try to write to them after that, so we must
4677 * use dup() to replace them with something else -- webb
4678 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4679 * waiting for input.
4680 */
4681 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4682 fclose(stdin);
4683 fclose(stdout);
4684 fclose(stderr);
4685
4686 /*
4687 * If any of these open()'s and dup()'s fail, we just continue
4688 * anyway. It's not fatal, and on most systems it will make
4689 * no difference at all. On a few it will cause the execvp()
4690 * to exit with a non-zero status even when the completion
4691 * could be done, which is nothing too serious. If the open()
4692 * or dup() failed we'd just do the same thing ourselves
4693 * anyway -- webb
4694 */
4695 if (fd >= 0)
4696 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004697 vim_ignored = dup(fd); // To replace stdin (fd 0)
4698 vim_ignored = dup(fd); // To replace stdout (fd 1)
4699 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaar0f873732019-12-05 20:28:46 +01004701 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 close(fd);
4703 }
4704 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004705 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004707 || gui.in_use
4708# endif
4709 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711
Bram Moolenaardf177f62005-02-22 08:39:57 +00004712# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004713 // Create our own process group, so that the child and all its
4714 // children can be kill()ed. Don't do this when using pipes,
4715 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004716 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004717 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004718 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004719# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004720 // When doing "!xterm&" and 'shell' is bash: the shell
4721 // will exit and send SIGHUP to all processes in its
4722 // group, killing the just started process. Ignore SIGHUP
4723 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004724 signal(SIGHUP, SIG_IGN);
4725# endif
4726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727# endif
4728# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004729 if (pty_slave_fd >= 0)
4730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004731 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004732 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004733 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004735 // Try to become controlling tty (probably doesn't work,
4736 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004737 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004739 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004740# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004741 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaara5792f52005-11-23 21:25:05 +00004743 /*
4744 * stderr is only redirected when using the GUI, so that a
4745 * program like gpg can still access the terminal to get a
4746 * passphrase using stderr.
4747 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004748# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if (pty_master_fd >= 0)
4750 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004751 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaar0f873732019-12-05 20:28:46 +01004753 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004755 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004757 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004758 if (gui.in_use)
4759 {
4760 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004761 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar0f873732019-12-05 20:28:46 +01004764 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004769 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 close(fd_toshell[1]);
4771 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004772 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 close(fd_toshell[0]);
4774
Bram Moolenaar0f873732019-12-05 20:28:46 +01004775 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 close(fd_fromshell[0]);
4777 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004778 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 close(fd_fromshell[1]);
4780
Bram Moolenaara5792f52005-11-23 21:25:05 +00004781# ifdef FEAT_GUI
4782 if (gui.in_use)
4783 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004784 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004785 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004786 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004787 }
4788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 /*
4793 * There is no type cast for the argv, because the type may be
4794 * different on different machines. This may cause a warning
4795 * message with strict compilers, don't worry about it.
4796 * Call _exit() instead of exit() to avoid closing the connection
4797 * to the X server (esp. with GTK, which uses atexit()).
4798 */
4799 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004800 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004802 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804 /*
4805 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
4808 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004810 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004811# ifdef FEAT_JOB_CHANNEL
4812 ++dont_check_job_ended;
4813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /*
4815 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004816 * This is also used to pipe stdin/stdout to/from the external
4817 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004819 if ((options & (SHELL_READ|SHELL_WRITE))
4820# ifdef FEAT_GUI
4821 || (gui.in_use && show_shell_mess)
4822# endif
4823 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004825# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004827 int buffer_off = 0; // valid bytes in buffer[]
4828 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4829 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 int len;
4831 int p_more_save;
4832 int old_State;
4833 int c;
4834 int toshell_fd;
4835 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004836 garray_T ga;
4837 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004838# ifdef ELAPSED_FUNC
4839 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
Bram Moolenaardf177f62005-02-22 08:39:57 +00004842# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (pty_master_fd >= 0)
4844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 fromshell_fd = pty_master_fd;
4846 toshell_fd = dup(pty_master_fd);
4847 }
4848 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 close(fd_toshell[0]);
4852 close(fd_fromshell[1]);
4853 toshell_fd = fd_toshell[1];
4854 fromshell_fd = fd_fromshell[0];
4855 }
4856
4857 /*
4858 * Write to the child if there are typed characters.
4859 * Read from the child if there are characters available.
4860 * Repeat the reading a few times if more characters are
4861 * available. Need to check for typed keys now and then, but
4862 * not too often (delays when no chars are available).
4863 * This loop is quit if no characters can be read from the pty
4864 * (WaitForChar detected special condition), or there are no
4865 * characters available and the child has exited.
4866 * Only check if the child has exited when there is no more
4867 * output. The child may exit before all the output has
4868 * been printed.
4869 *
4870 * Currently this busy loops!
4871 * This can probably dead-lock when the write blocks!
4872 */
4873 p_more_save = p_more;
4874 p_more = FALSE;
4875 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004876 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004878 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004879 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004880 // Fork a process that will write the lines to the
4881 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004882 if ((wpid = fork()) == -1)
4883 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004884 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004885 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004886 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 {
4888 linenr_T lnum = curbuf->b_op_start.lnum;
4889 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004890 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 size_t l;
4892
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004893 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004894 for (;;)
4895 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004896 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004897 if (l == 0)
4898 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004899 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004900 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 len = write(toshell_fd, "", (size_t)1);
4902 else
4903 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004904 char_u *s = vim_strchr(lp + written, NL);
4905
Bram Moolenaar89d40322006-08-29 15:30:07 +00004906 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004907 s == NULL ? l
4908 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004909 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004910 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004912 // Finished a line, add a NL, unless this line
4913 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004914 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004915 || (!curbuf->b_p_bin
4916 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004917 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004918 && (lnum !=
4919 curbuf->b_ml.ml_line_count
4920 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004921 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004922 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004923 ++lnum;
4924 if (lnum > curbuf->b_op_end.lnum)
4925 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004926 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004927 close(toshell_fd);
4928 toshell_fd = -1;
4929 break;
4930 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004931 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004932 written = 0;
4933 }
4934 else if (len > 0)
4935 written += len;
4936 }
4937 _exit(0);
4938 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004939 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004940 {
4941 close(toshell_fd);
4942 toshell_fd = -1;
4943 }
4944 }
4945
4946 if (options & SHELL_READ)
4947 ga_init2(&ga, 1, BUFLEN);
4948
4949 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004950# ifdef ELAPSED_FUNC
4951 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 for (;;)
4954 {
4955 /*
4956 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004957 * if there are any.
4958 * Don't do this if we are expanding wild cards (would eat
4959 * typeahead).
4960 * Don't do this when filtering and terminal is in cooked
4961 * mode, the shell command will handle the I/O. Avoids
4962 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004963 * Don't get characters when the child has already
4964 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004965 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004966 * while (noread_cnt > 4), avoids that ":r !ls" eats
4967 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 */
4969 len = 0;
4970 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004971 && ((options &
4972 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4973 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004974# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004975 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004976# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004977 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004978 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004979 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004981 if (ta_len == 0)
4982 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004983 // Get extra characters when we don't have any.
4984 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004985 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004986# ifdef ELAPSED_FUNC
4987 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004988# endif
4989 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4990 }
4991 if (ta_len > 0 || len > 0)
4992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /*
4994 * For pipes:
4995 * Check for CTRL-C: send interrupt signal to child.
4996 * Check for CTRL-D: EOF, close pipe to child.
4997 */
4998 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * Send SIGINT to the child's group or all
5002 * processes in our group.
5003 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02005004 may_send_sigint(ta_buf[ta_len], pid, wpid);
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (pty_master_fd < 0 && toshell_fd >= 0
5007 && ta_buf[ta_len] == Ctrl_D)
5008 {
5009 close(toshell_fd);
5010 toshell_fd = -1;
5011 }
5012 }
5013
Bram Moolenaarf4140482020-02-15 23:06:45 +01005014 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 /*
5017 * For pipes: echo the typed characters.
5018 * For a pty this does not seem to work.
5019 */
5020 if (pty_master_fd < 0)
5021 {
5022 for (i = ta_len; i < ta_len + len; ++i)
5023 {
5024 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5025 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else if (has_mbyte)
5027 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005028 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 msg_outtrans_len(ta_buf + i, l);
5031 i += l - 1;
5032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 else
5034 msg_outtrans_len(ta_buf + i, 1);
5035 }
5036 windgoto(msg_row, msg_col);
5037 out_flush();
5038 }
5039
5040 ta_len += len;
5041
5042 /*
5043 * Write the characters to the child, unless EOF has
5044 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005045 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005046 * When writing buffer lines, drop the typed
5047 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005049 if (options & SHELL_WRITE)
5050 ta_len = 0;
5051 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5054 if (len > 0)
5055 {
5056 ta_len -= len;
5057 mch_memmove(ta_buf, ta_buf + len, ta_len);
5058 }
5059 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062
Bram Moolenaardf177f62005-02-22 08:39:57 +00005063 if (got_int)
5064 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005065 // CTRL-C sends a signal to the child, we ignore it
5066 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005067# ifdef HAVE_SETSID
5068 kill(-pid, SIGINT);
5069# else
5070 kill(0, SIGINT);
5071# endif
5072 if (wpid > 0)
5073 kill(wpid, SIGINT);
5074 got_int = FALSE;
5075 }
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 /*
5078 * Check if the child has any characters to be printed.
5079 * Read them and write them to our window. Repeat this as
5080 * long as there is something to do, avoid the 10ms wait
5081 * for mch_inchar(), or sending typeahead characters to
5082 * the external process.
5083 * TODO: This should handle escape sequences, compatible
5084 * to some terminal (vt52?).
5085 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005086 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005087 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005089 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005092 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005094
5095 noread_cnt = 0;
5096 if (options & SHELL_READ)
5097 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005098 // Do NUL -> NL translation, append NL separated
5099 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005100 for (i = 0; i < len; ++i)
5101 {
5102 if (buffer[i] == NL)
5103 append_ga_line(&ga);
5104 else if (buffer[i] == NUL)
5105 ga_append(&ga, NL);
5106 else
5107 ga_append(&ga, buffer[i]);
5108 }
5109 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005110 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
Bram Moolenaardf177f62005-02-22 08:39:57 +00005115 len += buffer_off;
5116 buffer[len] = NUL;
5117
Bram Moolenaar0f873732019-12-05 20:28:46 +01005118 // Check if the last character in buffer[] is
5119 // incomplete, keep these bytes for the next
5120 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 for (p = buffer; p < buffer + len; p += l)
5122 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005123 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005125 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 else if (MB_BYTE2LEN(*p) != l)
5127 break;
5128 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005129 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005131 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (len >= 12)
5133 ++p;
5134 else
5135 {
5136 buffer_off = len;
5137 continue;
5138 }
5139 }
5140 c = *p;
5141 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005142 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (p < buffer + len)
5144 {
5145 *p = c;
5146 buffer_off = (buffer + len) - p;
5147 mch_memmove(buffer, p, buffer_off);
5148 continue;
5149 }
5150 buffer_off = 0;
5151 }
5152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005155 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157
5158 windgoto(msg_row, msg_col);
5159 cursor_on();
5160 out_flush();
5161 if (got_int)
5162 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005163
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005164# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005165 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005166 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005167 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005168
Bram Moolenaar0f873732019-12-05 20:28:46 +01005169 // Avoid that we keep looping here without
5170 // checking for a CTRL-C for a long time. Don't
5171 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005172 if (msec > 2000)
5173 {
5174 noread_cnt = 5;
5175 break;
5176 }
5177 }
5178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
Bram Moolenaar0f873732019-12-05 20:28:46 +01005181 // If we already detected the child has finished, continue
5182 // reading output for a short while. Some text may be
5183 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005184 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005185 {
5186 if (noread_cnt < 5)
5187 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005188 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005189 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 /*
5192 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005193 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005195# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005196 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5201 || (wait_pid == pid && WIFEXITED(status)))
5202 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005203 // Don't break the loop yet, try reading more
5204 // characters from "fromshell_fd" first. When using
5205 // pipes there might still be something to read and
5206 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005209 else
5210 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005211
Bram Moolenaar95a51352013-03-21 22:53:50 +01005212# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005213 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005214 clip_update();
5215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217finished:
5218 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005219 if (options & SHELL_READ)
5220 {
5221 if (ga.ga_len > 0)
5222 {
5223 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005224 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005225 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005226 }
5227 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005228 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005229 ga_clear(&ga);
5230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 /*
5233 * Give all typeahead that wasn't used back to ui_inchar().
5234 */
5235 if (ta_len)
5236 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 State = old_State;
5238 if (toshell_fd >= 0)
5239 close(toshell_fd);
5240 close(fromshell_fd);
5241 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005242# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005243 else
5244 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005245 long delay_msec = 1;
5246
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005247 /*
5248 * Similar to the loop above, but only handle X events, no
5249 * I/O.
5250 */
5251 for (;;)
5252 {
5253 if (got_int)
5254 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005255 // CTRL-C sends a signal to the child, we ignore it
5256 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005257# ifdef HAVE_SETSID
5258 kill(-pid, SIGINT);
5259# else
5260 kill(0, SIGINT);
5261# endif
5262 got_int = FALSE;
5263 }
5264# ifdef __NeXT__
5265 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5266# else
5267 wait_pid = waitpid(pid, &status, WNOHANG);
5268# endif
5269 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5270 || (wait_pid == pid && WIFEXITED(status)))
5271 {
5272 wait_pid = pid;
5273 break;
5274 }
5275
Bram Moolenaar0f873732019-12-05 20:28:46 +01005276 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005277 clip_update();
5278
Bram Moolenaar0f873732019-12-05 20:28:46 +01005279 // Wait for 1 to 10 msec. 1 is faster but gives the child
5280 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005281 mch_delay(delay_msec, TRUE);
5282 if (++delay_msec > 10)
5283 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005284 }
5285 }
5286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /*
5289 * Wait until our child has exited.
5290 * Ignore wait() returning pids of other children and returning
5291 * because of some signal like SIGWINCH.
5292 * Don't wait if wait_pid was already set above, indicating the
5293 * child already exited.
5294 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005295 if (wait_pid != pid)
5296 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
Bram Moolenaar624891f2010-10-13 16:22:09 +02005298# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005299 // Close slave side of pty. Only do this after the child has
5300 // exited, otherwise the child may hang when it tries to write on
5301 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005302 if (pty_master_fd >= 0)
5303 close(pty_slave_fd);
5304# endif
5305
Bram Moolenaar0f873732019-12-05 20:28:46 +01005306 // Make sure the child that writes to the external program is
5307 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005308 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005309 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005310 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005311 wait4pid(wpid, NULL);
5312 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005313
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005314# ifdef FEAT_JOB_CHANNEL
5315 --dont_check_job_ended;
5316# endif
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 /*
5319 * Set to raw mode right now, otherwise a CTRL-C after
5320 * catch_signals() will kill Vim.
5321 */
5322 if (tmode == TMODE_RAW)
5323 settmode(TMODE_RAW);
5324 did_settmode = TRUE;
5325 set_signals();
5326
5327 if (WIFEXITED(status))
5328 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005329 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005331 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 if (retval == EXEC_FAILED)
5334 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005335 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 msg_outtrans(p_sh);
5337 msg_putchar('\n');
5338 }
5339 else if (!(options & SHELL_SILENT))
5340 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005341 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 msg_outnum((long)retval);
5343 msg_putchar('\n');
5344 }
5345 }
5346 }
5347 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005348 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352error:
5353 if (!did_settmode)
5354 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005355 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356# ifdef FEAT_TITLE
5357 resettitle();
5358# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005359 vim_free(argv);
5360 vim_free(tofree1);
5361 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005364}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005365#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005366
5367 int
5368mch_call_shell(
5369 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005370 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005371{
5372#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5373 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5374 return mch_call_shell_terminal(cmd, options);
5375#endif
5376#ifdef USE_SYSTEM
5377 return mch_call_shell_system(cmd, options);
5378#else
5379 return mch_call_shell_fork(cmd, options);
5380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381}
5382
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005383#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005384 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005385mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005386{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005387 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005388 int fd_in[2] = {-1, -1}; // for stdin
5389 int fd_out[2] = {-1, -1}; // for stdout
5390 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005391 int pty_master_fd = -1;
5392 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005393 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005394 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5395 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5396 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005397 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005398 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5399 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005400 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005401 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005402 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005403
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005404 if (use_out_for_err && use_null_for_out)
5405 use_null_for_err = TRUE;
5406
Bram Moolenaar0f873732019-12-05 20:28:46 +01005407 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005408 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005409
Bram Moolenaarb2412082017-08-20 18:09:14 +02005410 if (options->jo_pty
5411 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005412 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005413 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005414 open_pty(&pty_master_fd, &pty_slave_fd,
5415 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005416
Bram Moolenaar0f873732019-12-05 20:28:46 +01005417 // TODO: without the channel feature connect the child to /dev/null?
5418 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005419 if (use_file_for_in)
5420 {
5421 char_u *fname = options->jo_io_name[PART_IN];
5422
5423 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5424 if (fd_in[0] < 0)
5425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005426 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005427 goto failed;
5428 }
5429 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005430 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005431 // When writing buffer lines to the input don't use the pty, so that
5432 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005433 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5434 && pipe(fd_in) < 0)
5435 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005436
5437 if (use_file_for_out)
5438 {
5439 char_u *fname = options->jo_io_name[PART_OUT];
5440
5441 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5442 if (fd_out[1] < 0)
5443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005444 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005445 goto failed;
5446 }
5447 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005448 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005449 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005450
5451 if (use_file_for_err)
5452 {
5453 char_u *fname = options->jo_io_name[PART_ERR];
5454
5455 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5456 if (fd_err[1] < 0)
5457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005458 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005459 goto failed;
5460 }
5461 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005462 else if (!use_out_for_err && !use_null_for_err
5463 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005464 goto failed;
5465
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005466 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5467 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005468 if (options->jo_set & JO_CHANNEL)
5469 {
5470 channel = options->jo_channel;
5471 if (channel != NULL)
5472 ++channel->ch_refcount;
5473 }
5474 else
5475 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005476 if (channel == NULL)
5477 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005478 if (job->jv_tty_out != NULL)
5479 ch_log(channel, "using pty %s on fd %d",
5480 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005481 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005482
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005483 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005484 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005485 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005487 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005488 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005489 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005490 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005491 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005492 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005493 int null_fd = -1;
5494 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005495
Bram Moolenaar0f873732019-12-05 20:28:46 +01005496 // child
5497 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005498 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005499
Bram Moolenaar819524702018-02-27 19:10:00 +01005500# ifdef FEAT_JOB_CHANNEL
5501 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005502 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005503 ch_logfile((char_u *)"", (char_u *)"");
5504# endif
5505
Bram Moolenaar835dc632016-02-07 14:27:38 +01005506# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005507 // Create our own process group, so that the child and all its
5508 // children can be kill()ed. Don't do this when using pipes,
5509 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 (void)setsid();
5511# endif
5512
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005513# ifdef FEAT_TERMINAL
5514 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005515 {
5516 char *term = (char *)T_NAME;
5517
5518#ifdef FEAT_GUI
5519 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005520 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005521 term = getenv("TERM");
5522#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005523 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005524 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005525 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005526 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005527 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005528 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005529 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005530 else if (t_colors > 16)
5531 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005532 else
5533 term = "xterm";
5534 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005535 set_child_environment(
5536 (long)options->jo_term_rows,
5537 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005538 term,
5539 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005540 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005541 else
5542# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005543 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005544
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005545 if (options->jo_env != NULL)
5546 {
5547 dict_T *dict = options->jo_env;
5548 hashitem_T *hi;
5549 int todo = (int)dict->dv_hashtab.ht_used;
5550
5551 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5552 if (!HASHITEM_EMPTY(hi))
5553 {
5554 typval_T *item = &dict_lookup(hi)->di_tv;
5555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005557 --todo;
5558 }
5559 }
5560
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005561 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005562 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005563 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005564 if (null_fd < 0)
5565 {
5566 perror("opening /dev/null failed");
5567 _exit(OPEN_NULL_FAILED);
5568 }
5569 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005570
Bram Moolenaar223896d2017-08-02 22:33:28 +02005571 if (pty_slave_fd >= 0)
5572 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005573 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005574 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005575# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005576 // Try to become controlling tty (probably doesn't work,
5577 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005578 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5579# endif
5580 }
5581
Bram Moolenaar0f873732019-12-05 20:28:46 +01005582 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005583 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005584 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005585 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005586 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005587 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005588 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005589 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005590
Bram Moolenaar0f873732019-12-05 20:28:46 +01005591 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005592 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005593 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005594 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005595 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005596 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005597 }
5598 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005599 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005600 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005601 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005602 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005603 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005604
Bram Moolenaar0f873732019-12-05 20:28:46 +01005605 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005606 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005607 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005608 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005609 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005610 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005611 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005612 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005613
5614 if (fd_in[0] >= 0)
5615 close(fd_in[0]);
5616 if (fd_in[1] >= 0)
5617 close(fd_in[1]);
5618 if (fd_out[0] >= 0)
5619 close(fd_out[0]);
5620 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005621 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005622 if (fd_err[0] >= 0)
5623 close(fd_err[0]);
5624 if (fd_err[1] >= 0)
5625 close(fd_err[1]);
5626 if (pty_master_fd >= 0)
5627 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005628 close(pty_master_fd); // not used in the child
5629 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005630 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005631
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005632 if (null_fd >= 0)
5633 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005634
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005635 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5636 _exit(EXEC_FAILED);
5637
Bram Moolenaar0f873732019-12-05 20:28:46 +01005638 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005639 execvp(argv[0], argv);
5640
Bram Moolenaar4694a172016-04-21 14:05:23 +02005641 if (stderr_works)
5642 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005643# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005644 // calling free_all_mem() here causes problems. Ignore valgrind
5645 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005646# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005647 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005648 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005649
Bram Moolenaar0f873732019-12-05 20:28:46 +01005650 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005651 UNBLOCK_SIGNALS(&curset);
5652
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005653 job->jv_pid = pid;
5654 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005655 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005656
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005657 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005658 close(pty_slave_fd); // not used in the parent
5659 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005660 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005661 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005662 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005663 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005664 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005665 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005666 if (channel != NULL)
5667 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005668 int in_fd = INVALID_FD;
5669 int out_fd = INVALID_FD;
5670 int err_fd = INVALID_FD;
5671
5672 if (!(use_file_for_in || use_null_for_in))
5673 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5674
5675 if (!(use_file_for_out || use_null_for_out))
5676 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5677
5678 // When using pty_master_fd only set it for stdout, do not duplicate
5679 // it for stderr, it only needs to be read once.
5680 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5681 {
5682 if (fd_err[0] >= 0)
5683 err_fd = fd_err[0];
5684 else if (out_fd != pty_master_fd)
5685 err_fd = pty_master_fd;
5686 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005687
5688 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005689 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005690 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005691 else
5692 {
5693 if (fd_in[1] >= 0)
5694 close(fd_in[1]);
5695 if (fd_out[0] >= 0)
5696 close(fd_out[0]);
5697 if (fd_err[0] >= 0)
5698 close(fd_err[0]);
5699 if (pty_master_fd >= 0)
5700 close(pty_master_fd);
5701 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702
Bram Moolenaar0f873732019-12-05 20:28:46 +01005703 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005704 return;
5705
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005706failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005707 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005708 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005709 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005710 if (fd_in[1] >= 0)
5711 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005712 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005713 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005714 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005715 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005716 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005717 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005718 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005719 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005720 if (pty_master_fd >= 0)
5721 close(pty_master_fd);
5722 if (pty_slave_fd >= 0)
5723 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005724}
5725
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005726 static char_u *
5727get_signal_name(int sig)
5728{
5729 int i;
5730 char_u numbuf[NUMBUFLEN];
5731
5732 if (sig == SIGKILL)
5733 return vim_strsave((char_u *)"kill");
5734
5735 for (i = 0; signal_info[i].sig != -1; i++)
5736 if (sig == signal_info[i].sig)
5737 return strlow_save((char_u *)signal_info[i].name);
5738
5739 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5740 return vim_strsave(numbuf);
5741}
5742
Bram Moolenaar835dc632016-02-07 14:27:38 +01005743 char *
5744mch_job_status(job_T *job)
5745{
5746# ifdef HAVE_UNION_WAIT
5747 union wait status;
5748# else
5749 int status = -1;
5750# endif
5751 pid_t wait_pid = 0;
5752
5753# ifdef __NeXT__
5754 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5755# else
5756 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5757# endif
5758 if (wait_pid == -1)
5759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005760 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005761 if (job->jv_status < JOB_ENDED)
5762 ch_log(job->jv_channel, "Job no longer exists: %s",
5763 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005764 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005765 }
5766 if (wait_pid == 0)
5767 return "run";
5768 if (WIFEXITED(status))
5769 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005770 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005771 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005772 if (job->jv_status < JOB_ENDED)
5773 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005774 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005775 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005776 if (WIFSIGNALED(status))
5777 {
5778 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005779 job->jv_termsig = get_signal_name(WTERMSIG(status));
5780 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5781 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5782 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005783 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005784 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005785 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005786
5787return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005788 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005789 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005790 return "dead";
5791}
5792
5793 job_T *
5794mch_detect_ended_job(job_T *job_list)
5795{
5796# ifdef HAVE_UNION_WAIT
5797 union wait status;
5798# else
5799 int status = -1;
5800# endif
5801 pid_t wait_pid = 0;
5802 job_T *job;
5803
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005804# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005805 // Do not do this when waiting for a shell command to finish, we would get
5806 // the exit value here (and discard it), the exit value obtained there
5807 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005808 if (dont_check_job_ended > 0)
5809 return NULL;
5810# endif
5811
Bram Moolenaar97792de2016-10-15 18:36:49 +02005812# ifdef __NeXT__
5813 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5814# else
5815 wait_pid = waitpid(-1, &status, WNOHANG);
5816# endif
5817 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005818 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005819 return NULL;
5820 for (job = job_list; job != NULL; job = job->jv_next)
5821 {
5822 if (job->jv_pid == wait_pid)
5823 {
5824 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005825 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005826 job->jv_exitval = WEXITSTATUS(status);
5827 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005828 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005829 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005830 job->jv_termsig = get_signal_name(WTERMSIG(status));
5831 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005832 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005833 {
5834 ch_log(job->jv_channel, "Job ended");
5835 job->jv_status = JOB_ENDED;
5836 }
5837 return job;
5838 }
5839 }
5840 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841}
5842
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005843/*
5844 * Send a (deadly) signal to "job".
5845 * Return FAIL if "how" is not a valid name.
5846 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005848mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005850 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851
Bram Moolenaar923d9262016-02-25 20:56:01 +01005852 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005854 else if (STRCMP(how, "hup") == 0)
5855 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 else if (STRCMP(how, "quit") == 0)
5857 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005858 else if (STRCMP(how, "int") == 0)
5859 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005860 else if (STRCMP(how, "kill") == 0)
5861 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005862#ifdef SIGWINCH
5863 else if (STRCMP(how, "winch") == 0)
5864 sig = SIGWINCH;
5865#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866 else if (isdigit(*how))
5867 sig = atoi((char *)how);
5868 else
5869 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005870
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005871 // Never kill ourselves!
5872 if (job->jv_pid != 0)
5873 {
5874 // TODO: have an option to only kill the process, not the group?
5875 kill(-job->jv_pid, sig);
5876 kill(job->jv_pid, sig);
5877 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005878
Bram Moolenaar835dc632016-02-07 14:27:38 +01005879 return OK;
5880}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881
5882/*
5883 * Clear the data related to "job".
5884 */
5885 void
5886mch_clear_job(job_T *job)
5887{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005888 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005889# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005890 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005891# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005892 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005893# endif
5894}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895#endif
5896
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005897#if defined(FEAT_TERMINAL) || defined(PROTO)
5898 int
5899mch_create_pty_channel(job_T *job, jobopt_T *options)
5900{
5901 int pty_master_fd = -1;
5902 int pty_slave_fd = -1;
5903 channel_T *channel;
5904
Bram Moolenaar59386482019-02-10 22:43:46 +01005905 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005906 close(pty_slave_fd);
5907
5908 channel = add_channel();
5909 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005910 {
5911 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005912 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005913 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005914 if (job->jv_tty_out != NULL)
5915 ch_log(channel, "using pty %s on fd %d",
5916 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005917 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005918 channel->ch_keep_open = TRUE;
5919
Bram Moolenaar0f873732019-12-05 20:28:46 +01005920 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5921 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005922 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005923 channel_set_job(channel, job, options);
5924 return OK;
5925}
5926#endif
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * Check for CTRL-C typed by reading all available characters.
5930 * In cooked mode we should get SIGINT, no need to check.
5931 */
5932 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005933mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005935 if ((curr_tmode == TMODE_RAW || force)
5936 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 fill_input_buf(FALSE);
5938}
5939
5940/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005941 * Wait "msec" msec until a character is available from the mouse, keyboard,
5942 * from inbuf[].
5943 * "msec" == -1 will block forever.
5944 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005945 * When "ignore_input" is TRUE even check for pending input when input is
5946 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005947 * "interrupted" (if not NULL) is set to TRUE when no character is available
5948 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005949 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005950 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005953WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005955#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005956 return ui_wait_for_chars_or_timer(
5957 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005958#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005959 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005960#endif
5961}
5962
5963/*
5964 * Wait "msec" msec until a character is available from the mouse or keyboard
5965 * or from inbuf[].
5966 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005967 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005968 * "interrupted" (if not NULL) is set to TRUE when no character is available
5969 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005970 * When a GUI is being used, this will never get called -- webb
5971 */
5972 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005973WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975#ifdef FEAT_MOUSE_GPM
5976 int gpm_process_wanted;
5977#endif
5978#ifdef FEAT_XCLIPBOARD
5979 int rest;
5980#endif
5981 int avail;
5982
Bram Moolenaar0f873732019-12-05 20:28:46 +01005983 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 return 1;
5985
5986#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005987 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 if (WantQueryMouse)
5989 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005990 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02005991 if (!no_query_mouse_for_testing)
5992 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 }
5994#endif
5995
5996 /*
5997 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5998 * events. This is a bit complicated, because they might both be defined.
5999 */
6000#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
6001# ifdef FEAT_XCLIPBOARD
6002 rest = 0;
6003 if (do_xterm_trace())
6004 rest = msec;
6005# endif
6006 do
6007 {
6008# ifdef FEAT_XCLIPBOARD
6009 if (rest != 0)
6010 {
6011 msec = XT_TRACE_DELAY;
6012 if (rest >= 0 && rest < XT_TRACE_DELAY)
6013 msec = rest;
6014 if (rest >= 0)
6015 rest -= msec;
6016 }
6017# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006018# ifdef FEAT_SOUND_CANBERRA
6019 // Invoke any pending sound callbacks.
6020 if (has_sound_callback_in_queue())
6021 invoke_sound_callback();
6022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023# ifdef FEAT_MOUSE_GPM
6024 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006025 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006026 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006027 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006029 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006033 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return 1;
6035# ifdef FEAT_XCLIPBOARD
6036 if (rest == 0 || !do_xterm_trace())
6037# endif
6038 break;
6039 }
6040 }
6041 while (FALSE
6042# ifdef FEAT_MOUSE_GPM
6043 || (gpm_process_wanted && mch_gpm_process() == 0)
6044# endif
6045# ifdef FEAT_XCLIPBOARD
6046 || (!avail && rest != 0)
6047# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006048 )
6049 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006052 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#endif
6054 return avail;
6055}
6056
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006057#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058/*
6059 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006060 * "msec" == 0 will check for characters once.
6061 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006064 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006065 * "interrupted" (if not NULL) is set to TRUE when no character is available
6066 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068#if defined(__BEOS__)
6069 int
6070#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006071 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006073RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
6075 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006076 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006077#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 static int busy = FALSE;
6079
Bram Moolenaar0f873732019-12-05 20:28:46 +01006080 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# define MAY_LOOP
6082
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006083# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006084 // Remember at what time we started, so that we know how much longer we
6085 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006086 long start_msec = msec;
6087 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006089 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006090 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091# endif
6092
Bram Moolenaar0f873732019-12-05 20:28:46 +01006093 // Handle being called recursively. This may happen for the session
6094 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (busy)
6096 return 0;
6097#endif
6098
6099#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006100 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#endif
6102 {
6103#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006104 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006105# ifdef FEAT_MZSCHEME
6106 int mzquantum_used = FALSE;
6107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108#endif
6109#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006110 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006111 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int nfd;
6113# ifdef FEAT_XCLIPBOARD
6114 int xterm_idx = -1;
6115# endif
6116# ifdef FEAT_MOUSE_GPM
6117 int gpm_idx = -1;
6118# endif
6119# ifdef USE_XSMP
6120 int xsmp_idx = -1;
6121# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006122 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006124# ifdef FEAT_MZSCHEME
6125 mzvim_check_threads();
6126 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6127 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006128 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006129 mzquantum_used = TRUE;
6130 }
6131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 fds[0].fd = fd;
6133 fds[0].events = POLLIN;
6134 nfd = 1;
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006137 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (xterm_Shell != (Widget)0)
6139 {
6140 xterm_idx = nfd;
6141 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6142 fds[nfd].events = POLLIN;
6143 nfd++;
6144 }
6145# endif
6146# ifdef FEAT_MOUSE_GPM
6147 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6148 {
6149 gpm_idx = nfd;
6150 fds[nfd].fd = gpm_fd;
6151 fds[nfd].events = POLLIN;
6152 nfd++;
6153 }
6154# endif
6155# ifdef USE_XSMP
6156 if (xsmp_icefd != -1)
6157 {
6158 xsmp_idx = nfd;
6159 fds[nfd].fd = xsmp_icefd;
6160 fds[nfd].events = POLLIN;
6161 nfd++;
6162 }
6163# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006164#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006165 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006166#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006167 if (interrupted != NULL)
6168 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006170 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006171
6172 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006173 if (result == 0 && interrupted != NULL && ret > 0)
6174 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006175
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006176# ifdef FEAT_MZSCHEME
6177 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006178 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006179 finished = FALSE;
6180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182# ifdef FEAT_XCLIPBOARD
6183 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6184 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006185 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006187 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 finished = FALSE;
6189 }
6190# endif
6191# ifdef FEAT_MOUSE_GPM
6192 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194# endif
6195# ifdef USE_XSMP
6196 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6197 {
6198 if (fds[xsmp_idx].revents & POLLIN)
6199 {
6200 busy = TRUE;
6201 xsmp_handle_requests();
6202 busy = FALSE;
6203 }
6204 else if (fds[xsmp_idx].revents & POLLHUP)
6205 {
6206 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006207 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 xsmp_close();
6209 }
6210 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006211 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
6213# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006214#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006215 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006216 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006217 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
Bram Moolenaar0f873732019-12-05 20:28:46 +01006220#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
6222 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006223 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006224 // These are static because they can take 8 Kbyte each and cause the
6225 // signal stack to run out with -O3.
6226 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006228 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006230# ifdef FEAT_MZSCHEME
6231 mzvim_check_threads();
6232 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6233 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006234 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006235 mzquantum_used = TRUE;
6236 }
6237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006239 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006241 tv.tv_sec = towait / 1000;
6242 tv.tv_usec = (towait % 1000) * (1000000/1000);
6243 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006245 else
6246 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
6248 /*
6249 * Select on ready for reading and exceptional condition (end of file).
6250 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006251select_eintr:
6252 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006253 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 FD_ZERO(&efds);
6255 FD_SET(fd, &rfds);
6256# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006257 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 FD_SET(fd, &efds);
6259# endif
6260 maxfd = fd;
6261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006263 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (xterm_Shell != (Widget)0)
6265 {
6266 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6267 if (maxfd < ConnectionNumber(xterm_dpy))
6268 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006269
Bram Moolenaar0f873732019-12-05 20:28:46 +01006270 // An event may have already been read but not handled. In
6271 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006272 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274# endif
6275# ifdef FEAT_MOUSE_GPM
6276 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6277 {
6278 FD_SET(gpm_fd, &rfds);
6279 FD_SET(gpm_fd, &efds);
6280 if (maxfd < gpm_fd)
6281 maxfd = gpm_fd;
6282 }
6283# endif
6284# ifdef USE_XSMP
6285 if (xsmp_icefd != -1)
6286 {
6287 FD_SET(xsmp_icefd, &rfds);
6288 FD_SET(xsmp_icefd, &efds);
6289 if (maxfd < xsmp_icefd)
6290 maxfd = xsmp_icefd;
6291 }
6292# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006293# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006294 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006295# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006296 if (interrupted != NULL)
6297 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
Bram Moolenaar643b6142018-09-12 20:29:09 +02006299 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6300 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006301 result = ret > 0 && FD_ISSET(fd, &rfds);
6302 if (result)
6303 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006304 else if (interrupted != NULL && ret > 0)
6305 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006306
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006307# ifdef EINTR
6308 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006309 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006310 // Check whether window has been resized, EINTR may be caused by
6311 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006312 if (do_resize)
6313 handle_resize();
6314
Bram Moolenaar0f873732019-12-05 20:28:46 +01006315 // Interrupted by a signal, need to try again. We ignore msec
6316 // here, because we do want to check even after a timeout if
6317 // characters are available. Needed for reading output of an
6318 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006319 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006320 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006321# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006322# ifdef __TANDEM
6323 if (ret == -1 && errno == ENOTSUP)
6324 {
6325 FD_ZERO(&rfds);
6326 FD_ZERO(&efds);
6327 ret = 0;
6328 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006329# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006330# ifdef FEAT_MZSCHEME
6331 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006332 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006333 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334# endif
6335
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336# ifdef FEAT_XCLIPBOARD
6337 if (ret > 0 && xterm_Shell != (Widget)0
6338 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6339 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006340 xterm_update(); // Maybe we should hand out clipboard
6341 // continue looping when we only got the X event and the input
6342 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (--ret == 0 && !input_available())
6344 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006345 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 finished = FALSE;
6347 }
6348 }
6349# endif
6350# ifdef FEAT_MOUSE_GPM
6351 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6352 {
6353 if (FD_ISSET(gpm_fd, &efds))
6354 gpm_close();
6355 else if (FD_ISSET(gpm_fd, &rfds))
6356 *check_for_gpm = 1;
6357 }
6358# endif
6359# ifdef USE_XSMP
6360 if (ret > 0 && xsmp_icefd != -1)
6361 {
6362 if (FD_ISSET(xsmp_icefd, &efds))
6363 {
6364 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006365 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 xsmp_close();
6367 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006368 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370 else if (FD_ISSET(xsmp_icefd, &rfds))
6371 {
6372 busy = TRUE;
6373 xsmp_handle_requests();
6374 busy = FALSE;
6375 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006376 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378 }
6379# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006380#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006381 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006382 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006383 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
Bram Moolenaar0f873732019-12-05 20:28:46 +01006386#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388#ifdef MAY_LOOP
6389 if (finished || msec == 0)
6390 break;
6391
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006392# ifdef FEAT_CLIENTSERVER
6393 if (server_waiting())
6394 break;
6395# endif
6396
Bram Moolenaar0f873732019-12-05 20:28:46 +01006397 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (msec > 0)
6399 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006400# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006401 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006402 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006404 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 msec = msec / 2;
6406# endif
6407 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006408 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410#endif
6411 }
6412
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006413 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006417 * Expand a path into all matching files and/or directories. Handles "*",
6418 * "?", "[a-z]", "**", etc.
6419 * "path" has backslashes before chars that are not to be expanded.
6420 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 */
6422 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006423mch_expandpath(
6424 garray_T *gap,
6425 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006426 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
Bram Moolenaar02743632005-07-25 20:42:36 +00006428 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430
6431/*
6432 * mch_expand_wildcards() - this code does wild-card pattern matching using
6433 * the shell
6434 *
6435 * return OK for success, FAIL for error (you may lose some memory) and put
6436 * an error message in *file.
6437 *
6438 * num_pat is number of input patterns
6439 * pat is array of pointers to input patterns
6440 * num_file is pointer to number of matched file names
6441 * file is pointer to array of pointers to matched file names
6442 */
6443
6444#ifndef SEEK_SET
6445# define SEEK_SET 0
6446#endif
6447#ifndef SEEK_END
6448# define SEEK_END 2
6449#endif
6450
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006451#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006452
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006454mch_expand_wildcards(
6455 int num_pat,
6456 char_u **pat,
6457 int *num_file,
6458 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006459 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 int i;
6462 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006463 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 char_u *p;
6465 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
Bram Moolenaarc7247912008-01-13 12:54:11 +00006467 /*
6468 * This is the non-OS/2 implementation (really Unix).
6469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 int j;
6471 char_u *tempname;
6472 char_u *command;
6473 FILE *fd;
6474 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006475#define STYLE_ECHO 0 // use "echo", the default
6476#define STYLE_GLOB 1 // use "glob", for csh
6477#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6478#define STYLE_PRINT 3 // use "print -N", for zsh
6479#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6480 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 int shell_style = STYLE_ECHO;
6482 int check_spaces;
6483 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006484 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006485 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006486 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar0f873732019-12-05 20:28:46 +01006488 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 *file = NULL;
6490
6491 /*
6492 * If there are no wildcards, just copy the names to allocated memory.
6493 * Saves a lot of time, because we don't have to start a new shell.
6494 */
6495 if (!have_wildcard(num_pat, pat))
6496 return save_patterns(num_pat, pat, num_file, file);
6497
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006498# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006499 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006500 if (sandbox != 0 && check_secure())
6501 return FAIL;
6502# endif
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 /*
6505 * Don't allow the use of backticks in secure and restricted mode.
6506 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006507 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 for (i = 0; i < num_pat; ++i)
6509 if (vim_strchr(pat[i], '`') != NULL
6510 && (check_restricted() || check_secure()))
6511 return FAIL;
6512
6513 /*
6514 * get a name for the temp file
6515 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006516 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006518 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 return FAIL;
6520 }
6521
6522 /*
6523 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006524 * file.
6525 * STYLE_BT: NL separated
6526 * If expanding `cmd` execute it directly.
6527 * STYLE_GLOB: NUL separated
6528 * If we use *csh, "glob" will work better than "echo".
6529 * STYLE_PRINT: NL or NUL separated
6530 * If we use *zsh, "print -N" will work better than "glob".
6531 * STYLE_VIMGLOB: NL separated
6532 * If we use *sh*, we define "vimglob()".
6533 * STYLE_ECHO: space separated.
6534 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 */
6536 if (num_pat == 1 && *pat[0] == '`'
6537 && (len = STRLEN(pat[0])) > 2
6538 && *(pat[0] + len - 1) == '`')
6539 shell_style = STYLE_BT;
6540 else if ((len = STRLEN(p_sh)) >= 3)
6541 {
6542 if (STRCMP(p_sh + len - 3, "csh") == 0)
6543 shell_style = STYLE_GLOB;
6544 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6545 shell_style = STYLE_PRINT;
6546 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006547 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6548 "sh") != NULL)
6549 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
Bram Moolenaar0f873732019-12-05 20:28:46 +01006551 // Compute the length of the command. We need 2 extra bytes: for the
6552 // optional '&' and for the NUL.
6553 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006555 if (shell_style == STYLE_VIMGLOB)
6556 len += STRLEN(sh_vimglob_func);
6557
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006558 for (i = 0; i < num_pat; ++i)
6559 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006560 // Count the length of the patterns in the same way as they are put in
6561 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006562#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006563 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006564#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006565 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006566 for (j = 0; pat[i][j] != NUL; ++j)
6567 {
6568 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006569 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006570 ++len;
6571 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006572#endif
6573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 command = alloc(len);
6575 if (command == NULL)
6576 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006577 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 vim_free(tempname);
6579 return FAIL;
6580 }
6581
6582 /*
6583 * Build the shell command:
6584 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6585 * recognizes this).
6586 * - Add the shell command to print the expanded names.
6587 * - Add the temp file name.
6588 * - Add the file name patterns.
6589 */
6590 if (shell_style == STYLE_BT)
6591 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006592 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006593 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006594 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006596 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006597 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006599 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006601 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 *p = ' ';
6603 }
6604 STRCAT(command, ">");
6605 }
6606 else
6607 {
6608 if (flags & EW_NOTFOUND)
6609 STRCPY(command, "set nonomatch; ");
6610 else
6611 STRCPY(command, "unset nonomatch; ");
6612 if (shell_style == STYLE_GLOB)
6613 STRCAT(command, "glob >");
6614 else if (shell_style == STYLE_PRINT)
6615 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006616 else if (shell_style == STYLE_VIMGLOB)
6617 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 else
6619 STRCAT(command, "echo >");
6620 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (shell_style != STYLE_BT)
6625 for (i = 0; i < num_pat; ++i)
6626 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006627 // When using system() always add extra quotes, because the shell
6628 // is started twice. Otherwise put a backslash before special
6629 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630#ifdef USE_SYSTEM
6631 STRCAT(command, " \"");
6632 STRCAT(command, pat[i]);
6633 STRCAT(command, "\"");
6634#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006635 int intick = FALSE;
6636
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 p = command + STRLEN(command);
6638 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006639 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006640 {
6641 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006642 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006643 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6644 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006645 // Remove a backslash, take char literally. But keep
6646 // backslash inside backticks, before a special character
6647 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006648 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006649 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6650 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006651 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006652 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006653 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006654 else if (!intick
6655 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6656 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006657 // Put a backslash before a special character, but not
6658 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6659 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006660 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006661
Bram Moolenaar0f873732019-12-05 20:28:46 +01006662 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006663 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 *p = NUL;
6666#endif
6667 }
6668 if (flags & EW_SILENT)
6669 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006670 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006671 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 /*
6674 * Using zsh -G: If a pattern has no matches, it is just deleted from
6675 * the argument list, otherwise zsh gives an error message and doesn't
6676 * expand any other pattern.
6677 */
6678 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006679 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681 /*
6682 * If we use -f then shell variables set in .cshrc won't get expanded.
6683 * vi can do it, so we will too, but it is only necessary if there is a "$"
6684 * in one of the patterns, otherwise we can still use the fast option.
6685 */
6686 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006687 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 /*
6690 * execute the shell command
6691 */
6692 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6693
Bram Moolenaar0f873732019-12-05 20:28:46 +01006694 // When running in the background, give it some time to create the temp
6695 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006696 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 mch_delay(10L, TRUE);
6698
Bram Moolenaar0f873732019-12-05 20:28:46 +01006699 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 show_shell_mess = TRUE;
6701 vim_free(command);
6702
Bram Moolenaar0f873732019-12-05 20:28:46 +01006703 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
6705 mch_remove(tempname);
6706 vim_free(tempname);
6707 /*
6708 * With interactive completion, the error message is not printed.
6709 * However with USE_SYSTEM, I don't know how to turn off error messages
6710 * from the shell, so screen may still get messed up -- webb.
6711 */
6712#ifndef USE_SYSTEM
6713 if (!(flags & EW_SILENT))
6714#endif
6715 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006716 redraw_later_clear(); // probably messed up screen
6717 msg_putchar('\n'); // clear bottom line quickly
6718 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719#ifdef USE_SYSTEM
6720 if (!(flags & EW_SILENT))
6721#endif
6722 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006723 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006724 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
6726 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006727 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6728 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (shell_style == STYLE_BT)
6730 return FAIL;
6731 goto notfound;
6732 }
6733
6734 /*
6735 * read the names from the file into memory
6736 */
6737 fd = fopen((char *)tempname, READBIN);
6738 if (fd == NULL)
6739 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (!(flags & EW_SILENT))
6742 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006743 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006744 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 }
6746 vim_free(tempname);
6747 goto notfound;
6748 }
6749 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006750 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006752 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006753 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006754 buffer = NULL;
6755 else
6756 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (buffer == NULL)
6758 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006759 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 mch_remove(tempname);
6761 vim_free(tempname);
6762 fclose(fd);
6763 return FAIL;
6764 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006765 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 i = fread((char *)buffer, 1, len, fd);
6767 fclose(fd);
6768 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006769 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006771 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006772 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 vim_free(tempname);
6774 vim_free(buffer);
6775 return FAIL;
6776 }
6777 vim_free(tempname);
6778
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006779# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006780 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006782 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6784 *p++ = buffer[i];
6785 len = p - buffer;
6786# endif
6787
6788
Bram Moolenaar0f873732019-12-05 20:28:46 +01006789 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (shell_style == STYLE_ECHO)
6791 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006792 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006794 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
6796 while (*p != ' ' && *p != '\n')
6797 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006798 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
6800 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006801 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006802 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006804 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006806 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 while (*p != '\n' && *p != NUL)
6809 ++p;
6810 if (*p != NUL)
6811 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006812 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006815 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 else
6817 {
6818 /*
6819 * Some versions of zsh use spaces instead of NULs to separate
6820 * results. Only do this when there is no NUL before the end of the
6821 * buffer, otherwise we would never be able to use file names with
6822 * embedded spaces when zsh does use NULs.
6823 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6824 * don't check for spaces again.
6825 */
6826 check_spaces = FALSE;
6827 if (shell_style == STYLE_PRINT && !did_find_nul)
6828 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006829 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006830 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006831 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 did_find_nul = TRUE;
6833 else
6834 check_spaces = TRUE;
6835 }
6836
6837 /*
6838 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6839 * already is one, for STYLE_GLOB it needs to be added.
6840 */
6841 if (len && buffer[len - 1] == NUL)
6842 --len;
6843 else
6844 buffer[len] = NUL;
6845 i = 0;
6846 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006847 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
6849 ++i;
6850 *p = NUL;
6851 }
6852 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006853 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855 if (i == 0)
6856 {
6857 /*
6858 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6859 * /bin/sh will happily expand it to nothing rather than returning an
6860 * error; and hey, it's good to check anyway -- webb.
6861 */
6862 vim_free(buffer);
6863 goto notfound;
6864 }
6865 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006866 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (*file == NULL)
6868 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006869 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 vim_free(buffer);
6871 return FAIL;
6872 }
6873
6874 /*
6875 * Isolate the individual file names.
6876 */
6877 p = buffer;
6878 for (i = 0; i < *num_file; ++i)
6879 {
6880 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006881 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006882 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6883 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006885 while (!(shell_style == STYLE_ECHO && *p == ' ')
6886 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006888 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 *p = NUL;
6890 else
6891 {
6892 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006893 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
6895 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006896 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006898 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006900 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902 }
6903
6904 /*
6905 * Move the file names to allocated memory.
6906 */
6907 for (j = 0, i = 0; i < *num_file; ++i)
6908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006909 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6911 continue;
6912
Bram Moolenaar0f873732019-12-05 20:28:46 +01006913 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 dir = (mch_isdir((*file)[i]));
6915 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6916 continue;
6917
Bram Moolenaar0f873732019-12-05 20:28:46 +01006918 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006919 if (!dir && (flags & EW_EXEC)
6920 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006921 continue;
6922
Bram Moolenaar964b3742019-05-24 18:54:09 +02006923 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (p)
6925 {
6926 STRCPY(p, (*file)[i]);
6927 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006928 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 (*file)[j++] = p;
6930 }
6931 }
6932 vim_free(buffer);
6933 *num_file = j;
6934
Bram Moolenaar0f873732019-12-05 20:28:46 +01006935 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006937 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 goto notfound;
6939 }
6940
6941 return OK;
6942
6943notfound:
6944 if (flags & EW_NOTFOUND)
6945 return save_patterns(num_pat, pat, num_file, file);
6946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947}
6948
Bram Moolenaar0f873732019-12-05 20:28:46 +01006949#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006952save_patterns(
6953 int num_pat,
6954 char_u **pat,
6955 int *num_file,
6956 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006959 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006961 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (*file == NULL)
6963 return FAIL;
6964 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006965 {
6966 s = vim_strsave(pat[i]);
6967 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006968 // Be compatible with expand_filename(): halve the number of
6969 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006970 backslash_halve(s);
6971 (*file)[i] = s;
6972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 *num_file = num_pat;
6974 return OK;
6975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977/*
6978 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6979 * expand.
6980 */
6981 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006982mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006984 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (*p == '\\' && p[1] != NUL)
6987 ++p;
6988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (vim_strchr((char_u *)
6990#ifdef VMS
6991 "*?%"
6992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#endif
6995 , *p) != NULL)
6996 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998 return FALSE;
6999}
7000
7001/*
7002 * Return TRUE if the string "p" contains a wildcard.
7003 * Don't recognize '~' at the end as a wildcard.
7004 */
7005 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007006mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007008 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (*p == '\\' && p[1] != NUL)
7011 ++p;
7012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (vim_strchr((char_u *)
7014#ifdef VMS
7015 "*?%$"
7016#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#endif
7019 , *p) != NULL
7020 || (*p == '~' && p[1] != NUL))
7021 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 }
7023 return FALSE;
7024}
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007027have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 int i;
7030
7031 for (i = 0; i < num; i++)
7032 if (mch_has_wildcard(file[i]))
7033 return 1;
7034 return 0;
7035}
7036
7037 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007038have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 int i;
7041
7042 for (i = 0; i < num; i++)
7043 if (vim_strchr(file[i], '$') != NULL)
7044 return TRUE;
7045 return FALSE;
7046}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007048#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049/*
7050 * Scaled-down version of rename(), which is missing in Xenix.
7051 * This version can only move regular files and will fail if the
7052 * destination exists.
7053 */
7054 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007055mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 struct stat st;
7058
Bram Moolenaar0f873732019-12-05 20:28:46 +01007059 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007061 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007063 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 return 0;
7065 return -1;
7066}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007067#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007069#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070/*
7071 * Initializes connection with gpm (if it isn't already opened)
7072 * Return 1 if succeeded (or connection already opened), 0 if failed
7073 */
7074 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007075gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007077 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 if (!gpm_flag)
7080 {
7081 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7082 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007083 // Default handling for mouse move
7084 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 gpm_connect.maxMod = 0xffff;
7086 if (Gpm_Open(&gpm_connect, 0) > 0)
7087 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007088 // gpm library tries to handling TSTP causes
7089 // problems. Anyways, we close connection to Gpm whenever
7090 // we are going to suspend or starting an external process
7091 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007092# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007094# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007095 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007098 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 return 0;
7100 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007101 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
7104/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007105 * Returns TRUE if the GPM mouse is enabled.
7106 */
7107 int
7108gpm_enabled(void)
7109{
7110 return gpm_flag && gpm_fd >= 0;
7111}
7112
7113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
7116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007117gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007119 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 Gpm_Close();
7121}
7122
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007123/*
7124 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007126 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 */
7128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007129mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 int button;
7132 static Gpm_Event gpm_event;
7133 char_u string[6];
7134 int_u vim_modifiers;
7135 int row,col;
7136 unsigned char buttons_mask;
7137 unsigned char gpm_modifiers;
7138 static unsigned char old_buttons = 0;
7139
7140 Gpm_GetEvent(&gpm_event);
7141
7142#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007143 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (hold_gui_events)
7145 return 0;
7146#endif
7147
7148 row = gpm_event.y - 1;
7149 col = gpm_event.x - 1;
7150
Bram Moolenaar0f873732019-12-05 20:28:46 +01007151 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 string[1] = 'M';
7153 string[2] = 'G';
7154 switch (GPM_BARE_EVENTS(gpm_event.type))
7155 {
7156 case GPM_DRAG:
7157 string[3] = MOUSE_DRAG;
7158 break;
7159 case GPM_DOWN:
7160 buttons_mask = gpm_event.buttons & ~old_buttons;
7161 old_buttons = gpm_event.buttons;
7162 switch (buttons_mask)
7163 {
7164 case GPM_B_LEFT:
7165 button = MOUSE_LEFT;
7166 break;
7167 case GPM_B_MIDDLE:
7168 button = MOUSE_MIDDLE;
7169 break;
7170 case GPM_B_RIGHT:
7171 button = MOUSE_RIGHT;
7172 break;
7173 default:
7174 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007175 // Don't know what to do. Can more than one button be
7176 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
7178 string[3] = (char_u)(button | 0x20);
7179 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7180 break;
7181 case GPM_UP:
7182 string[3] = MOUSE_RELEASE;
7183 old_buttons &= ~gpm_event.buttons;
7184 break;
7185 default:
7186 return 0;
7187 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007188 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 gpm_modifiers = gpm_event.modifiers;
7190 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007191 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7192 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7193 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7195 vim_modifiers |= MOUSE_SHIFT;
7196
7197 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7198 vim_modifiers |= MOUSE_CTRL;
7199 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7200 vim_modifiers |= MOUSE_ALT;
7201 string[3] |= vim_modifiers;
7202 string[4] = (char_u)(col + ' ' + 1);
7203 string[5] = (char_u)(row + ' ' + 1);
7204 add_to_input_buf(string, 6);
7205 return 6;
7206}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007207#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007209#ifdef FEAT_SYSMOUSE
7210/*
7211 * Initialize connection with sysmouse.
7212 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7213 * output, any sysmouse output than will be processed via sig_sysmouse().
7214 * Return OK if succeeded, FAIL if failed.
7215 */
7216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007217sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007218{
7219 struct mouse_info mouse;
7220
7221 mouse.operation = MOUSE_MODE;
7222 mouse.u.mode.mode = 0;
7223 mouse.u.mode.signal = SIGUSR2;
7224 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7225 {
7226 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7227 mouse.operation = MOUSE_SHOW;
7228 ioctl(1, CONS_MOUSECTL, &mouse);
7229 return OK;
7230 }
7231 return FAIL;
7232}
7233
7234/*
7235 * Stop processing SIGUSR2 signals, and also make sure that
7236 * virtual console do not send us any sysmouse related signal.
7237 */
7238 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007239sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007240{
7241 struct mouse_info mouse;
7242
7243 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7244 mouse.operation = MOUSE_MODE;
7245 mouse.u.mode.mode = 0;
7246 mouse.u.mode.signal = 0;
7247 ioctl(1, CONS_MOUSECTL, &mouse);
7248}
7249
7250/*
7251 * Gets info from sysmouse and adds special keys to input buf.
7252 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007253 static RETSIGTYPE
7254sig_sysmouse SIGDEFARG(sigarg)
7255{
7256 struct mouse_info mouse;
7257 struct video_info video;
7258 char_u string[6];
7259 int row, col;
7260 int button;
7261 int buttons;
7262 static int oldbuttons = 0;
7263
7264#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007265 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007266 if (hold_gui_events)
7267 return;
7268#endif
7269
7270 mouse.operation = MOUSE_GETINFO;
7271 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7272 && ioctl(1, FBIO_MODEINFO, &video) != -1
7273 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7274 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7275 {
7276 row = mouse.u.data.y / video.vi_cheight;
7277 col = mouse.u.data.x / video.vi_cwidth;
7278 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007279 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007280 string[1] = 'M';
7281 string[2] = 'S';
7282 if (oldbuttons == buttons && buttons != 0)
7283 {
7284 button = MOUSE_DRAG;
7285 }
7286 else
7287 {
7288 switch (buttons)
7289 {
7290 case 0:
7291 button = MOUSE_RELEASE;
7292 break;
7293 case 1:
7294 button = MOUSE_LEFT;
7295 break;
7296 case 2:
7297 button = MOUSE_MIDDLE;
7298 break;
7299 case 4:
7300 button = MOUSE_RIGHT;
7301 break;
7302 default:
7303 return;
7304 }
7305 oldbuttons = buttons;
7306 }
7307 string[3] = (char_u)(button);
7308 string[4] = (char_u)(col + ' ' + 1);
7309 string[5] = (char_u)(row + ' ' + 1);
7310 add_to_input_buf(string, 6);
7311 }
7312 return;
7313}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007314#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007317typedef char_u * (*STRPROCSTR)(char_u *);
7318typedef char_u * (*INTPROCSTR)(int);
7319typedef int (*STRPROCINT)(char_u *);
7320typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
7322/*
7323 * Call a DLL routine which takes either a string or int param
7324 * and returns an allocated string.
7325 */
7326 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007327mch_libcall(
7328 char_u *libname,
7329 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007330 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007331 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007332 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007333 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335# if defined(USE_DLOPEN)
7336 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007337 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338# else
7339 shl_t hinstLib;
7340# endif
7341 STRPROCSTR ProcAdd;
7342 INTPROCSTR ProcAddI;
7343 char_u *retval_str = NULL;
7344 int retval_int = 0;
7345 int success = FALSE;
7346
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007347 /*
7348 * Get a handle to the DLL module.
7349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007351 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007352 (void)dlerror();
7353
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 hinstLib = dlopen((char *)libname, RTLD_LAZY
7355# ifdef RTLD_LOCAL
7356 | RTLD_LOCAL
7357# endif
7358 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007359 if (hinstLib == NULL)
7360 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007361 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007362 dlerr = (char *)dlerror();
7363 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007364 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366# else
7367 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7368# endif
7369
Bram Moolenaar0f873732019-12-05 20:28:46 +01007370 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (hinstLib != NULL)
7372 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007373# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 /*
7375 * Catch a crash when calling the library function. For example when
7376 * using a number where a string pointer is expected.
7377 */
7378 mch_startjmp();
7379 if (SETJMP(lc_jump_env) != 0)
7380 {
7381 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007382# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007383 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 mch_didjmp();
7386 }
7387 else
7388# endif
7389 {
7390 retval_str = NULL;
7391 retval_int = 0;
7392
7393 if (argstring != NULL)
7394 {
7395# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007396 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007397 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398# else
7399 if (shl_findsym(&hinstLib, (const char *)funcname,
7400 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7401 ProcAdd = NULL;
7402# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007403 if ((success = (ProcAdd != NULL
7404# if defined(USE_DLOPEN)
7405 && dlerr == NULL
7406# endif
7407 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {
7409 if (string_result == NULL)
7410 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7411 else
7412 retval_str = (ProcAdd)(argstring);
7413 }
7414 }
7415 else
7416 {
7417# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007418 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007419 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420# else
7421 if (shl_findsym(&hinstLib, (const char *)funcname,
7422 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7423 ProcAddI = NULL;
7424# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007425 if ((success = (ProcAddI != NULL
7426# if defined(USE_DLOPEN)
7427 && dlerr == NULL
7428# endif
7429 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {
7431 if (string_result == NULL)
7432 retval_int = ((INTPROCINT)ProcAddI)(argint);
7433 else
7434 retval_str = (ProcAddI)(argint);
7435 }
7436 }
7437
Bram Moolenaar0f873732019-12-05 20:28:46 +01007438 // Save the string before we free the library.
7439 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 if (string_result == NULL)
7441 *number_result = retval_int;
7442 else if (retval_str != NULL
7443 && retval_str != (char_u *)1
7444 && retval_str != (char_u *)-1)
7445 *string_result = vim_strsave(retval_str);
7446 }
7447
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007448# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 mch_endjmp();
7450# ifdef SIGHASARG
7451 if (lc_signal != 0)
7452 {
7453 int i;
7454
Bram Moolenaar0f873732019-12-05 20:28:46 +01007455 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 for (i = 0; signal_info[i].sig != -1; i++)
7457 if (lc_signal == signal_info[i].sig)
7458 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007459 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461# endif
7462# endif
7463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007465 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007466 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007467 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007468
Bram Moolenaar0f873732019-12-05 20:28:46 +01007469 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 (void)dlclose(hinstLib);
7471# else
7472 (void)shl_unload(hinstLib);
7473# endif
7474 }
7475
7476 if (!success)
7477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007478 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 return FAIL;
7480 }
7481
7482 return OK;
7483}
7484#endif
7485
7486#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007487static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488static int xterm_button;
7489
7490/*
7491 * Setup a dummy window for X selections in a terminal.
7492 */
7493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007494setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 int z = 0;
7497 char *strp = "";
7498 Widget AppShell;
7499
7500 if (!x_connect_to_server())
7501 return;
7502
7503 open_app_context();
7504 if (app_context != NULL && xterm_Shell == (Widget)0)
7505 {
7506 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007507# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007509# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007510# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007511 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
7513 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007514 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515# endif
7516
Bram Moolenaar0f873732019-12-05 20:28:46 +01007517 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 oldhandler = XSetErrorHandler(x_error_check);
7519
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007520# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007521 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7523 mch_startjmp();
7524 if (SETJMP(lc_jump_env) != 0)
7525 {
7526 mch_didjmp();
7527 xterm_dpy = NULL;
7528 }
7529 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {
7532 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7533 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007534 if (xterm_dpy != NULL)
7535 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007536# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
7540
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007541# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007542 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007544# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007545 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 (void)XSetErrorHandler(oldhandler);
7547
7548 if (xterm_dpy == NULL)
7549 {
7550 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007551 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 return;
7553 }
7554
Bram Moolenaar0f873732019-12-05 20:28:46 +01007555 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 (void)XSetIOErrorHandler(x_IOerror_handler);
7557
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007558# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007560 {
7561 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007562 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007563 verbose_leave();
7564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565# endif
7566
Bram Moolenaar0f873732019-12-05 20:28:46 +01007567 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7569 applicationShellWidgetClass, xterm_dpy,
7570 NULL);
7571 if (AppShell == (Widget)0)
7572 return;
7573 xterm_Shell = XtVaCreatePopupShell("VIM",
7574 topLevelShellWidgetClass, AppShell,
7575 XtNmappedWhenManaged, 0,
7576 XtNwidth, 1,
7577 XtNheight, 1,
7578 NULL);
7579 if (xterm_Shell == (Widget)0)
7580 return;
7581
7582 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007583 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (x11_display == NULL)
7585 x11_display = xterm_dpy;
7586
7587 XtRealizeWidget(xterm_Shell);
7588 XSync(xterm_dpy, False);
7589 xterm_update();
7590 }
7591 if (xterm_Shell != (Widget)0)
7592 {
7593 clip_init(TRUE);
7594 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7595 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007596 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 if (test_x11_window(xterm_dpy) == FAIL)
7598 x11_window = 0;
7599 if (x11_window != 0)
7600 xterm_trace = 0;
7601 }
7602}
7603
7604 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007605start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7608 return;
7609 xterm_trace = 1;
7610 xterm_button = button;
7611 do_xterm_trace();
7612}
7613
7614
7615 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007616stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 if (xterm_trace < 0)
7619 return;
7620 xterm_trace = 0;
7621}
7622
7623/*
7624 * Query the xterm pointer and generate mouse termcodes if necessary
7625 * return TRUE if dragging is active, else FALSE
7626 */
7627 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007628do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 Window root, child;
7631 int root_x, root_y;
7632 int win_x, win_y;
7633 int row, col;
7634 int_u mask_return;
7635 char_u buf[50];
7636 char_u *strp;
7637 long got_hints;
7638 static char_u *mouse_code;
7639 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7640 static int prev_row = 0, prev_col = 0;
7641 static XSizeHints xterm_hints;
7642
7643 if (xterm_trace <= 0)
7644 return FALSE;
7645
7646 if (xterm_trace == 1)
7647 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007648 // Get the hints just before tracking starts. The font size might
7649 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007650 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7651 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 || xterm_hints.width_inc <= 1
7653 || xterm_hints.height_inc <= 1)
7654 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007655 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 return FALSE;
7657 }
7658
Bram Moolenaar0f873732019-12-05 20:28:46 +01007659 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 mouse_code = find_termcode(mouse_name);
7661 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007662 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 xterm_trace = 2;
7664
Bram Moolenaar0f873732019-12-05 20:28:46 +01007665 // Find the offset of the chars, there might be a scrollbar on the
7666 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7668 &win_x, &win_y, &mask_return);
7669 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7670 - (xterm_hints.height_inc / 2);
7671 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7672 xterm_hints.y = 2;
7673 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7674 - (xterm_hints.width_inc / 2);
7675 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7676 xterm_hints.x = 2;
7677 return TRUE;
7678 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007679 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
7681 xterm_trace = 0;
7682 return FALSE;
7683 }
7684
7685 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7686 &win_x, &win_y, &mask_return);
7687
7688 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7689 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7690 if (row == prev_row && col == prev_col)
7691 return TRUE;
7692
7693 STRCPY(buf, mouse_code);
7694 strp = buf + STRLEN(buf);
7695 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7696 *strp++ = (char_u)(col + ' ' + 1);
7697 *strp++ = (char_u)(row + ' ' + 1);
7698 *strp = 0;
7699 add_to_input_buf(buf, STRLEN(buf));
7700
7701 prev_row = row;
7702 prev_col = col;
7703 return TRUE;
7704}
7705
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007706# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*
7708 * Destroy the display, window and app_context. Required for GTK.
7709 */
7710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007711clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 if (xterm_Shell != (Widget)0)
7714 {
7715 XtDestroyWidget(xterm_Shell);
7716 xterm_Shell = (Widget)0;
7717 }
7718 if (xterm_dpy != NULL)
7719 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007720# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007721 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 if (x11_display == xterm_dpy)
7725 x11_display = NULL;
7726 xterm_dpy = NULL;
7727 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007728# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 if (app_context != (XtAppContext)NULL)
7730 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007731 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 XtDestroyApplicationContext(app_context);
7733 app_context = (XtAppContext)NULL;
7734 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737# endif
7738
7739/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007740 * Catch up with GUI or X events.
7741 */
7742 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007743clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007744{
7745# ifdef FEAT_GUI
7746 if (gui.in_use)
7747 gui_mch_update();
7748 else
7749# endif
7750 if (xterm_Shell != (Widget)0)
7751 xterm_update();
7752}
7753
7754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 * Catch up with any queued X events. This may put keyboard input into the
7756 * input buffer, call resize call-backs, trigger timers etc. If there is
7757 * nothing in the X event queue (& no timers pending), then we return
7758 * immediately.
7759 */
7760 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007761xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 XEvent event;
7764
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007765 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007767 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007769 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007770 break;
7771
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007772 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007773 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007774 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007775 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007776#ifdef FEAT_CLIENTSERVER
7777 {
7778 XPropertyEvent *e = (XPropertyEvent *)&event;
7779
7780 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007782 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007785 XtDispatchEvent(&event);
7786 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007787 else
7788 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007789 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007790 XtAppProcessEvent(app_context, mask);
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793}
7794
7795 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007796clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 if (xterm_Shell != (Widget)0)
7799 return clip_x11_own_selection(xterm_Shell, cbd);
7800 return FAIL;
7801}
7802
7803 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007804clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 if (xterm_Shell != (Widget)0)
7807 clip_x11_lose_selection(xterm_Shell, cbd);
7808}
7809
7810 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007811clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 if (xterm_Shell != (Widget)0)
7814 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7815}
7816
7817 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007818clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 clip_x11_set_selection(cbd);
7821}
7822#endif
7823
7824
7825#if defined(USE_XSMP) || defined(PROTO)
7826/*
7827 * Code for X Session Management Protocol.
7828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831/*
7832 * This is our chance to ask the user if they want to save,
7833 * or abort the logout
7834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007836xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 cmdmod_T save_cmdmod;
7839 int cancel_shutdown = False;
7840
7841 save_cmdmod = cmdmod;
7842 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007843 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007844 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 cancel_shutdown = True;
7846 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007847 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 out_flush();
7849
Bram Moolenaar0f873732019-12-05 20:28:46 +01007850 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 SmcInteractDone(smc_conn, cancel_shutdown);
7852
Bram Moolenaar0f873732019-12-05 20:28:46 +01007853 // Finish off
7854 // Only end save-yourself here if we're not cancelling shutdown;
7855 // we'll get a cancelled callback later in which we'll end it.
7856 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (!cancel_shutdown)
7858 {
7859 xsmp.save_yourself = False;
7860 SmcSaveYourselfDone(smc_conn, True);
7861 }
7862}
7863# endif
7864
7865/*
7866 * Callback that starts save-yourself.
7867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007869xsmp_handle_save_yourself(
7870 SmcConn smc_conn,
7871 SmPointer client_data UNUSED,
7872 int save_type UNUSED,
7873 Bool shutdown,
7874 int interact_style UNUSED,
7875 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007877 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (xsmp.save_yourself)
7879 SmcSaveYourselfDone(smc_conn, True);
7880 xsmp.save_yourself = True;
7881 xsmp.shutdown = shutdown;
7882
Bram Moolenaar0f873732019-12-05 20:28:46 +01007883 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007885 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886
7887 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007888 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
7890# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007891 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007893 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 SmcInteractRequest(smc_conn, SmDialogError,
7895 xsmp_handle_interaction, client_data);
7896 else
7897# endif
7898 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007899 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 SmcSaveYourselfDone(smc_conn, True);
7901 xsmp.save_yourself = False;
7902 }
7903}
7904
7905
7906/*
7907 * Callback to warn us of imminent death.
7908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007910xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 xsmp_close();
7913
Bram Moolenaar0f873732019-12-05 20:28:46 +01007914 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 getout_preserve_modified(0);
7916}
7917
7918
7919/*
7920 * Callback to tell us that save-yourself has completed.
7921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007923xsmp_save_complete(
7924 SmcConn smc_conn UNUSED,
7925 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 xsmp.save_yourself = False;
7928}
7929
7930
7931/*
7932 * Callback to tell us that an instigated shutdown was cancelled
7933 * (maybe even by us)
7934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007936xsmp_shutdown_cancelled(
7937 SmcConn smc_conn,
7938 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 if (xsmp.save_yourself)
7941 SmcSaveYourselfDone(smc_conn, True);
7942 xsmp.save_yourself = False;
7943 xsmp.shutdown = False;
7944}
7945
7946
7947/*
7948 * Callback to tell us that a new ICE connection has been established.
7949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007951xsmp_ice_connection(
7952 IceConn iceConn,
7953 IcePointer clientData UNUSED,
7954 Bool opening,
7955 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007957 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (opening)
7959 {
7960 xsmp_icefd = IceConnectionNumber(iceConn);
7961 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7962 }
7963}
7964
7965
Bram Moolenaar0f873732019-12-05 20:28:46 +01007966// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007968xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 Bool rep;
7971
7972 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7973 == IceProcessMessagesIOError)
7974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007975 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007977 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 xsmp_close();
7979 return FAIL;
7980 }
7981 else
7982 return OK;
7983}
7984
7985static int dummy;
7986
Bram Moolenaar0f873732019-12-05 20:28:46 +01007987// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 void
7989xsmp_init(void)
7990{
7991 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 SmcCallbacks smcallbacks;
7993#if 0
7994 SmPropValue smname;
7995 SmProp smnameprop;
7996 SmProp *smprops[1];
7997#endif
7998
7999 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008000 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001
8002 xsmp.save_yourself = xsmp.shutdown = False;
8003
Bram Moolenaar0f873732019-12-05 20:28:46 +01008004 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
8006 smcallbacks.save_yourself.client_data = NULL;
8007 smcallbacks.die.callback = xsmp_die;
8008 smcallbacks.die.client_data = NULL;
8009 smcallbacks.save_complete.callback = xsmp_save_complete;
8010 smcallbacks.save_complete.client_data = NULL;
8011 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8012 smcallbacks.shutdown_cancelled.client_data = NULL;
8013
Bram Moolenaar0f873732019-12-05 20:28:46 +01008014 // Set up a watch on ICE connection creations. The "dummy" argument is
8015 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8017 {
8018 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008019 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 return;
8021 }
8022
Bram Moolenaar0f873732019-12-05 20:28:46 +01008023 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 xsmp.smcconn = SmcOpenConnection(
8025 NULL,
8026 NULL,
8027 SmProtoMajor,
8028 SmProtoMinor,
8029 SmcSaveYourselfProcMask | SmcDieProcMask
8030 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8031 &smcallbacks,
8032 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008033 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008034 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 errorstring);
8036 if (xsmp.smcconn == NULL)
8037 {
8038 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008039
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008041 {
8042 vim_snprintf(errorreport, sizeof(errorreport),
8043 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008044 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 return;
8047 }
8048 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8049
8050#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008051 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 smname.value = "vim";
8053 smname.length = 3;
8054 smnameprop.name = "SmProgram";
8055 smnameprop.type = "SmARRAY8";
8056 smnameprop.num_vals = 1;
8057 smnameprop.vals = &smname;
8058
8059 smprops[0] = &smnameprop;
8060 SmcSetProperties(xsmp.smcconn, 1, smprops);
8061#endif
8062}
8063
8064
Bram Moolenaar0f873732019-12-05 20:28:46 +01008065// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008067xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 if (xsmp_icefd != -1)
8070 {
8071 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008072 if (xsmp.clientid != NULL)
8073 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008074 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 xsmp_icefd = -1;
8076 }
8077}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008078#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080
8081#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008082// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083char CtrlTable[] =
8084{
8085/* 00 - 5E */
8086 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8087 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8092/* ^ */ 0x1E,
8093/* - */ 0x1F,
8094/* 61 - 6C */
8095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8096/* _ */ 0x1F,
8097/* 6E - 80 */
8098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8099/* a */ 0x01,
8100/* b */ 0x02,
8101/* c */ 0x03,
8102/* d */ 0x37,
8103/* e */ 0x2D,
8104/* f */ 0x2E,
8105/* g */ 0x2F,
8106/* h */ 0x16,
8107/* i */ 0x05,
8108/* 8A - 90 */
8109 0, 0, 0, 0, 0, 0, 0,
8110/* j */ 0x15,
8111/* k */ 0x0B,
8112/* l */ 0x0C,
8113/* m */ 0x0D,
8114/* n */ 0x0E,
8115/* o */ 0x0F,
8116/* p */ 0x10,
8117/* q */ 0x11,
8118/* r */ 0x12,
8119/* 9A - A1 */
8120 0, 0, 0, 0, 0, 0, 0, 0,
8121/* s */ 0x13,
8122/* t */ 0x3C,
8123/* u */ 0x3D,
8124/* v */ 0x32,
8125/* w */ 0x26,
8126/* x */ 0x18,
8127/* y */ 0x19,
8128/* z */ 0x3F,
8129/* AA - AC */
8130 0, 0, 0,
8131/* [ */ 0x27,
8132/* AE - BC */
8133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8134/* ] */ 0x1D,
8135/* BE - C0 */ 0, 0, 0,
8136/* A */ 0x01,
8137/* B */ 0x02,
8138/* C */ 0x03,
8139/* D */ 0x37,
8140/* E */ 0x2D,
8141/* F */ 0x2E,
8142/* G */ 0x2F,
8143/* H */ 0x16,
8144/* I */ 0x05,
8145/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8146/* J */ 0x15,
8147/* K */ 0x0B,
8148/* L */ 0x0C,
8149/* M */ 0x0D,
8150/* N */ 0x0E,
8151/* O */ 0x0F,
8152/* P */ 0x10,
8153/* Q */ 0x11,
8154/* R */ 0x12,
8155/* DA - DF */ 0, 0, 0, 0, 0, 0,
8156/* \ */ 0x1C,
8157/* E1 */ 0,
8158/* S */ 0x13,
8159/* T */ 0x3C,
8160/* U */ 0x3D,
8161/* V */ 0x32,
8162/* W */ 0x26,
8163/* X */ 0x18,
8164/* Y */ 0x19,
8165/* Z */ 0x3F,
8166/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8168};
8169
8170char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008171{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8173 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8174 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8175 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8176};
8177
8178
Bram Moolenaar0f873732019-12-05 20:28:46 +01008179// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180char CtrlCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008181{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8183 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8184 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8185 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8186};
8187
8188
8189#endif