blob: b5ae9f868716bd718a63d9678902f4ddec073e44 [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
3315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003316mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 exiting = TRUE;
3319
3320#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
3321 x11_export_final_selection();
3322#endif
3323
3324#ifdef FEAT_GUI
3325 if (!gui.in_use)
3326#endif
3327 {
3328 settmode(TMODE_COOK);
3329#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02003330 // restore xterm title and icon name
3331 mch_restore_title(SAVE_RESTORE_BOTH);
3332 term_pop_title(SAVE_RESTORE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
3334 /*
3335 * When t_ti is not empty but it doesn't cause swapping terminal
3336 * pages, need to output a newline when msg_didout is set. But when
3337 * t_ti does swap pages it should not go to the shell page. Do this
3338 * before stoptermcap().
3339 */
3340 if (swapping_screen() && !newline_on_exit)
3341 exit_scroll();
3342
Bram Moolenaar0f873732019-12-05 20:28:46 +01003343 // Stop termcap: May need to check for T_CRV response, which
3344 // requires RAW mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 stoptermcap();
3346
3347 /*
3348 * A newline is only required after a message in the alternate screen.
3349 * This is set to TRUE by wait_return().
3350 */
3351 if (!swapping_screen() || newline_on_exit)
3352 exit_scroll();
3353
Bram Moolenaar0f873732019-12-05 20:28:46 +01003354 // Cursor may have been switched off without calling starttermcap()
3355 // when doing "vim -u vimrc" and vimrc contains ":q".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (full_screen)
3357 cursor_on();
3358 }
3359 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01003360 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 may_core_dump();
3362#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 gui_exit(r);
3365#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00003366
Bram Moolenaar56718732006-03-15 22:53:57 +00003367#ifdef MACOS_CONVERT
Bram Moolenaardf177f62005-02-22 08:39:57 +00003368 mac_conv_cleanup();
3369#endif
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef __QNX__
Bram Moolenaar0f873732019-12-05 20:28:46 +01003372 // A core dump won't be created if the signal handler
3373 // doesn't return, so we can't call exit()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (deadly_signal != 0)
3375 return;
3376#endif
3377
Bram Moolenaar009b2592004-10-24 19:18:58 +00003378#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003379 netbeans_send_disconnect();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003380#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003381
3382#ifdef EXITFREE
3383 free_all_mem();
3384#endif
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 exit(r);
3387}
3388
3389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003390may_core_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 if (deadly_signal != 0)
3393 {
3394 signal(deadly_signal, SIG_DFL);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003395 kill(getpid(), deadly_signal); // Die using the signal we caught
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397}
3398
3399#ifndef VMS
3400
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003401/*
3402 * Get the file descriptor to use for tty operations.
3403 */
3404 static int
3405get_tty_fd(int fd)
3406{
3407 int tty_fd = fd;
3408
3409#if defined(HAVE_SVR4_PTYS) && defined(SUN_SYSTEM)
3410 // On SunOS: Get the terminal parameters from "fd", or the slave device of
3411 // "fd" when it is a master device.
3412 if (mch_isatty(fd) > 1)
3413 {
3414 char *name;
3415
3416 name = ptsname(fd);
3417 if (name == NULL)
3418 return -1;
3419
3420 tty_fd = open(name, O_RDONLY | O_NOCTTY | O_EXTRA, 0);
3421 if (tty_fd < 0)
3422 return -1;
3423 }
3424#endif
3425 return tty_fd;
3426}
3427
3428 static int
3429mch_tcgetattr(int fd, void *term)
3430{
3431 int tty_fd;
3432 int retval = -1;
3433
3434 tty_fd = get_tty_fd(fd);
3435 if (tty_fd >= 0)
3436 {
3437#ifdef NEW_TTY_SYSTEM
3438# ifdef HAVE_TERMIOS_H
3439 retval = tcgetattr(tty_fd, (struct termios *)term);
3440# else
3441 retval = ioctl(tty_fd, TCGETA, (struct termio *)term);
3442# endif
3443#else
3444 // for "old" tty systems
3445 retval = ioctl(tty_fd, TIOCGETP, (struct sgttyb *)term);
3446#endif
3447 if (tty_fd != fd)
3448 close(tty_fd);
3449 }
3450 return retval;
3451}
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003454mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 static int first = TRUE;
3457
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003458#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459# ifdef HAVE_TERMIOS_H
3460 static struct termios told;
3461 struct termios tnew;
3462# else
3463 static struct termio told;
3464 struct termio tnew;
3465# endif
3466
3467 if (first)
3468 {
3469 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003470 mch_tcgetattr(read_cmd_fd, &told);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472
3473 tnew = told;
3474 if (tmode == TMODE_RAW)
3475 {
3476 /*
3477 * ~ICRNL enables typing ^V^M
3478 */
3479 tnew.c_iflag &= ~ICRNL;
3480 tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
3481# if defined(IEXTEN) && !defined(__MINT__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003482 | IEXTEN // IEXTEN enables typing ^V on SOLARIS
3483 // but it breaks function keys on MINT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484# endif
3485 );
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003486# ifdef ONLCR
3487 // Don't map NL -> CR NL, we do it ourselves.
3488 // Also disable expanding tabs if possible.
3489# ifdef XTABS
3490 tnew.c_oflag &= ~(ONLCR | XTABS);
3491# else
3492# ifdef TAB3
3493 tnew.c_oflag &= ~(ONLCR | TAB3);
3494# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 tnew.c_oflag &= ~ONLCR;
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003496# endif
3497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498# endif
Bram Moolenaarfaf626e2019-10-24 17:43:25 +02003499 tnew.c_cc[VMIN] = 1; // return after 1 char
3500 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 else if (tmode == TMODE_SLEEP)
Bram Moolenaar40de4562016-07-01 15:03:46 +02003503 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003504 // Also reset ICANON here, otherwise on Solaris select() won't see
3505 // typeahead characters.
Bram Moolenaar40de4562016-07-01 15:03:46 +02003506 tnew.c_lflag &= ~(ICANON | ECHO);
Bram Moolenaar0f873732019-12-05 20:28:46 +01003507 tnew.c_cc[VMIN] = 1; // return after 1 char
3508 tnew.c_cc[VTIME] = 0; // don't wait
Bram Moolenaar40de4562016-07-01 15:03:46 +02003509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511# if defined(HAVE_TERMIOS_H)
3512 {
3513 int n = 10;
3514
Bram Moolenaar0f873732019-12-05 20:28:46 +01003515 // A signal may cause tcsetattr() to fail (e.g., SIGCONT). Retry a
3516 // few times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 while (tcsetattr(read_cmd_fd, TCSANOW, &tnew) == -1
3518 && errno == EINTR && n > 0)
3519 --n;
3520 }
3521# else
3522 ioctl(read_cmd_fd, TCSETA, &tnew);
3523# endif
3524
3525#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /*
3527 * for "old" tty systems
3528 */
3529# ifndef TIOCSETN
Bram Moolenaar0f873732019-12-05 20:28:46 +01003530# define TIOCSETN TIOCSETP // for hpux 9.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531# endif
3532 static struct sgttyb ttybold;
3533 struct sgttyb ttybnew;
3534
3535 if (first)
3536 {
3537 first = FALSE;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003538 mch_tcgetattr(read_cmd_fd, &ttybold);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
3541 ttybnew = ttybold;
3542 if (tmode == TMODE_RAW)
3543 {
3544 ttybnew.sg_flags &= ~(CRMOD | ECHO);
3545 ttybnew.sg_flags |= RAW;
3546 }
3547 else if (tmode == TMODE_SLEEP)
3548 ttybnew.sg_flags &= ~(ECHO);
3549 ioctl(read_cmd_fd, TIOCSETN, &ttybnew);
3550#endif
3551 curr_tmode = tmode;
3552}
3553
3554/*
3555 * Try to get the code for "t_kb" from the stty setting
3556 *
3557 * Even if termcap claims a backspace key, the user's setting *should*
3558 * prevail. stty knows more about reality than termcap does, and if
3559 * somebody's usual erase key is DEL (which, for most BSD users, it will
3560 * be), they're going to get really annoyed if their erase key starts
3561 * doing forward deletes for no reason. (Eric Fischer)
3562 */
3563 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003564get_stty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003566 ttyinfo_T info;
3567 char_u buf[2];
3568 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003570 if (get_tty_info(read_cmd_fd, &info) == OK)
3571 {
3572 intr_char = info.interrupt;
3573 buf[0] = info.backspace;
3574 buf[1] = NUL;
3575 add_termcode((char_u *)"kb", buf, FALSE);
3576
Bram Moolenaar0f873732019-12-05 20:28:46 +01003577 // If <BS> and <DEL> are now the same, redefine <DEL>.
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003578 p = find_termcode((char_u *)"kD");
3579 if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
3580 do_fixdel(NULL);
3581 }
3582}
3583
3584/*
3585 * Obtain the characters that Backspace and Enter produce on "fd".
3586 * Returns OK or FAIL.
3587 */
3588 int
3589get_tty_info(int fd, ttyinfo_T *info)
3590{
3591#ifdef NEW_TTY_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592# ifdef HAVE_TERMIOS_H
3593 struct termios keys;
3594# else
3595 struct termio keys;
3596# endif
3597
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003598 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003600 info->backspace = keys.c_cc[VERASE];
3601 info->interrupt = keys.c_cc[VINTR];
3602 if (keys.c_iflag & ICRNL)
3603 info->enter = NL;
3604 else
3605 info->enter = CAR;
3606 if (keys.c_oflag & ONLCR)
3607 info->nl_does_cr = TRUE;
3608 else
3609 info->nl_does_cr = FALSE;
3610 return OK;
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01003613 // for "old" tty systems
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 struct sgttyb keys;
3615
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01003616 if (mch_tcgetattr(fd, &keys) != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003618 info->backspace = keys.sg_erase;
3619 info->interrupt = keys.sg_kill;
3620 info->enter = CAR;
3621 info->nl_does_cr = TRUE;
3622 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624#endif
Bram Moolenaar4f44b882017-08-13 20:06:18 +02003625 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
3627
Bram Moolenaar0f873732019-12-05 20:28:46 +01003628#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003630static int mouse_ison = FALSE;
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632/*
3633 * Set mouse clicks on or off.
3634 */
3635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003636mch_setmouse(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003638#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003639 static int bevalterm_ison = FALSE;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 int xterm_mouse_vers;
3642
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003643#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaara06afc72018-08-27 23:24:16 +02003644 if (!on)
3645 // Make sure not tracing mouse movements. Important when a button-down
3646 // was received but no release yet.
3647 stop_xterm_trace();
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003648#endif
Bram Moolenaara06afc72018-08-27 23:24:16 +02003649
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003650 if (on == mouse_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003651#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003652 && p_bevalterm == bevalterm_ison
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003653#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003654 )
Bram Moolenaar0f873732019-12-05 20:28:46 +01003655 // return quickly if nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return;
3657
3658 xterm_mouse_vers = use_xterm_mouse();
Bram Moolenaarc8427482011-10-20 21:09:35 +02003659
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003660#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003661 if (ttym_flags == TTYM_URXVT)
3662 {
Bram Moolenaarc8427482011-10-20 21:09:35 +02003663 out_str_nf((char_u *)
3664 (on
3665 ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
3666 : IF_EB("\033[?1015l", ESC_STR "[?1015l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003667 mouse_ison = on;
Bram Moolenaarc8427482011-10-20 21:09:35 +02003668 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003669#endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003670
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003671 if (ttym_flags == TTYM_SGR)
3672 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003673 // SGR mode supports columns above 223
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003674 out_str_nf((char_u *)
3675 (on
3676 ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
3677 : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003678 mouse_ison = on;
3679 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003680
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003681#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003682 if (bevalterm_ison != (p_bevalterm && on))
3683 {
3684 bevalterm_ison = (p_bevalterm && on);
3685 if (xterm_mouse_vers > 1 && !bevalterm_ison)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003686 // disable mouse movement events, enabling is below
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003687 out_str_nf((char_u *)
3688 (IF_EB("\033[?1003l", ESC_STR "[?1003l")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003689 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003690#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (xterm_mouse_vers > 0)
3693 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003694 if (on) // enable mouse events, use mouse tracking if available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 out_str_nf((char_u *)
3696 (xterm_mouse_vers > 1
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003697 ? (
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003698#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003699 bevalterm_ison
3700 ? IF_EB("\033[?1003h", ESC_STR "[?1003h") :
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003701#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003702 IF_EB("\033[?1002h", ESC_STR "[?1002h"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 : IF_EB("\033[?1000h", ESC_STR "[?1000h")));
Bram Moolenaar0f873732019-12-05 20:28:46 +01003704 else // disable mouse events, could probably always send the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 out_str_nf((char_u *)
3706 (xterm_mouse_vers > 1
3707 ? IF_EB("\033[?1002l", ESC_STR "[?1002l")
3708 : IF_EB("\033[?1000l", ESC_STR "[?1000l")));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003709 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003712#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 else if (ttym_flags == TTYM_DEC)
3714 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003715 if (on) // enable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 out_str_nf((char_u *)"\033[1;2'z\033[1;3'{");
Bram Moolenaar0f873732019-12-05 20:28:46 +01003717 else // disable mouse events
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 out_str_nf((char_u *)"\033['z");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003719 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003723#ifdef FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
3725 {
3726 if (on)
3727 {
3728 if (gpm_open())
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003729 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 else
3732 {
3733 gpm_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003734 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003739#ifdef FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003740 else
3741 {
3742 if (on)
3743 {
3744 if (sysmouse_open() == OK)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003745 mouse_ison = TRUE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003746 }
3747 else
3748 {
3749 sysmouse_close();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003750 mouse_ison = FALSE;
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003751 }
3752 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003753#endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003754
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003755#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else
3757 {
3758 if (on)
3759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003760 // D - Enable Mouse up/down messages
3761 // L - Enable Left Button Reporting
3762 // M - Enable Middle Button Reporting
3763 // R - Enable Right Button Reporting
3764 // K - Enable SHIFT and CTRL key Reporting
3765 // + - Enable Advanced messaging of mouse moves and up/down messages
3766 // Q - Quiet No Ack
3767 // # - Numeric value of mouse pointer required
3768 // 0 = Multiview 2000 cursor, used as standard
3769 // 1 = Windows Arrow
3770 // 2 = Windows I Beam
3771 // 3 = Windows Hour Glass
3772 // 4 = Windows Cross Hair
3773 // 5 = Windows UP Arrow
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003774# ifdef JSBTERM_MOUSE_NONADVANCED
Bram Moolenaar0f873732019-12-05 20:28:46 +01003775 // Disables full feedback of pointer movements
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK1Q\033\\",
3777 ESC_STR "[0~ZwLMRK1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003778# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 out_str_nf((char_u *)IF_EB("\033[0~ZwLMRK+1Q\033\\",
3780 ESC_STR "[0~ZwLMRK+1Q" ESC_STR "\\"));
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003781# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003782 mouse_ison = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 else
3785 {
3786 out_str_nf((char_u *)IF_EB("\033[0~ZwQ\033\\",
3787 ESC_STR "[0~ZwQ" ESC_STR "\\"));
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003788 mouse_ison = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003791#endif
3792#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 else
3794 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003795 // 1 = button press, 6 = release, 7 = drag, 1h...9l = right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (on)
3797 out_str_nf("\033[>1h\033[>6h\033[>7h\033[>1h\033[>9l");
3798 else
3799 out_str_nf("\033[>1l\033[>6l\033[>7l\033[>1l\033[>9h");
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003800 mouse_ison = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803}
3804
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003805#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003806/*
3807 * Called when 'balloonevalterm' changed.
3808 */
3809 void
3810mch_bevalterm_changed(void)
3811{
3812 mch_setmouse(mouse_ison);
3813}
3814#endif
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816/*
3817 * Set the mouse termcode, depending on the 'term' and 'ttymouse' options.
3818 */
3819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003820check_mouse_termcode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822# ifdef FEAT_MOUSE_XTERM
3823 if (use_xterm_mouse()
Bram Moolenaarc8427482011-10-20 21:09:35 +02003824# ifdef FEAT_MOUSE_URXVT
3825 && use_xterm_mouse() != 3
3826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827# ifdef FEAT_GUI
3828 && !gui.in_use
3829# endif
3830 )
3831 {
3832 set_mouse_termcode(KS_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003833 ? IF_EB("\233M", CSI_STR "M")
3834 : IF_EB("\033[M", ESC_STR "[M")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (*p_mouse != NUL)
3836 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01003837 // force mouse off and maybe on to send possibly new mouse
3838 // activation sequence to the xterm, with(out) drag tracing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 mch_setmouse(FALSE);
3840 setmouse();
3841 }
3842 }
3843 else
3844 del_mouse_termcode(KS_MOUSE);
3845# endif
3846
3847# ifdef FEAT_MOUSE_GPM
3848 if (!use_xterm_mouse()
3849# ifdef FEAT_GUI
3850 && !gui.in_use
3851# endif
3852 )
Bram Moolenaarbedf0912019-05-04 16:58:45 +02003853 set_mouse_termcode(KS_GPM_MOUSE,
3854 (char_u *)IF_EB("\033MG", ESC_STR "MG"));
3855 else
3856 del_mouse_termcode(KS_GPM_MOUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857# endif
3858
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003859# ifdef FEAT_SYSMOUSE
3860 if (!use_xterm_mouse()
3861# ifdef FEAT_GUI
3862 && !gui.in_use
3863# endif
3864 )
3865 set_mouse_termcode(KS_MOUSE, (char_u *)IF_EB("\033MS", ESC_STR "MS"));
3866# endif
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868# ifdef FEAT_MOUSE_JSB
Bram Moolenaar0f873732019-12-05 20:28:46 +01003869 // Conflicts with xterm mouse: "\033[" and "\033[M" ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (!use_xterm_mouse()
3871# ifdef FEAT_GUI
3872 && !gui.in_use
3873# endif
3874 )
3875 set_mouse_termcode(KS_JSBTERM_MOUSE,
3876 (char_u *)IF_EB("\033[0~zw", ESC_STR "[0~zw"));
3877 else
3878 del_mouse_termcode(KS_JSBTERM_MOUSE);
3879# endif
3880
3881# ifdef FEAT_MOUSE_NET
Bram Moolenaar0f873732019-12-05 20:28:46 +01003882 // There is no conflict, but one may type "ESC }" from Insert mode. Don't
3883 // define it in the GUI or when using an xterm.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (!use_xterm_mouse()
3885# ifdef FEAT_GUI
3886 && !gui.in_use
3887# endif
3888 )
3889 set_mouse_termcode(KS_NETTERM_MOUSE,
3890 (char_u *)IF_EB("\033}", ESC_STR "}"));
3891 else
3892 del_mouse_termcode(KS_NETTERM_MOUSE);
3893# endif
3894
3895# ifdef FEAT_MOUSE_DEC
Bram Moolenaar0f873732019-12-05 20:28:46 +01003896 // Conflicts with xterm mouse: "\033[" and "\033[M"
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003897 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898# ifdef FEAT_GUI
3899 && !gui.in_use
3900# endif
3901 )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003902 set_mouse_termcode(KS_DEC_MOUSE, (char_u *)(term_is_8bit(T_NAME)
3903 ? IF_EB("\233", CSI_STR) : IF_EB("\033[", ESC_STR "[")));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else
3905 del_mouse_termcode(KS_DEC_MOUSE);
3906# endif
3907# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar0f873732019-12-05 20:28:46 +01003908 // same conflict as the dec mouse
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003909 if (!use_xterm_mouse()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910# ifdef FEAT_GUI
3911 && !gui.in_use
3912# endif
3913 )
3914 set_mouse_termcode(KS_PTERM_MOUSE,
3915 (char_u *) IF_EB("\033[", ESC_STR "["));
3916 else
3917 del_mouse_termcode(KS_PTERM_MOUSE);
3918# endif
Bram Moolenaarc8427482011-10-20 21:09:35 +02003919# ifdef FEAT_MOUSE_URXVT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003920 if (use_xterm_mouse() == 3
Bram Moolenaarc8427482011-10-20 21:09:35 +02003921# ifdef FEAT_GUI
3922 && !gui.in_use
3923# endif
3924 )
3925 {
3926 set_mouse_termcode(KS_URXVT_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003927 ? IF_EB("\233*M", CSI_STR "*M")
3928 : IF_EB("\033[*M", ESC_STR "[*M")));
Bram Moolenaarc8427482011-10-20 21:09:35 +02003929
3930 if (*p_mouse != NUL)
3931 {
3932 mch_setmouse(FALSE);
3933 setmouse();
3934 }
3935 }
3936 else
3937 del_mouse_termcode(KS_URXVT_MOUSE);
3938# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003939 if (use_xterm_mouse() == 4
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003940# ifdef FEAT_GUI
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003941 && !gui.in_use
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01003942# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003943 )
3944 {
3945 set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
Bram Moolenaara529ce02017-06-22 22:37:57 +02003946 ? IF_EB("\233<*M", CSI_STR "<*M")
3947 : IF_EB("\033[<*M", ESC_STR "[<*M")));
3948
3949 set_mouse_termcode(KS_SGR_MOUSE_RELEASE, (char_u *)(term_is_8bit(T_NAME)
3950 ? IF_EB("\233<*m", CSI_STR "<*m")
3951 : IF_EB("\033[<*m", ESC_STR "[<*m")));
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003952
3953 if (*p_mouse != NUL)
3954 {
3955 mch_setmouse(FALSE);
3956 setmouse();
3957 }
3958 }
3959 else
Bram Moolenaara529ce02017-06-22 22:37:57 +02003960 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02003961 del_mouse_termcode(KS_SGR_MOUSE);
Bram Moolenaara529ce02017-06-22 22:37:57 +02003962 del_mouse_termcode(KS_SGR_MOUSE_RELEASE);
3963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#ifndef VMS
3967
3968/*
3969 * Try to get the current window size:
3970 * 1. with an ioctl(), most accurate method
3971 * 2. from the environment variables LINES and COLUMNS
3972 * 3. from the termcap
3973 * 4. keep using the old values
3974 * Return OK when size could be determined, FAIL otherwise.
3975 */
3976 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003977mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 long rows = 0;
3980 long columns = 0;
3981 char_u *p;
3982
3983 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 * 1. try using an ioctl. It is the most accurate method.
3985 *
3986 * Try using TIOCGWINSZ first, some systems that have it also define
3987 * TIOCGSIZE but don't have a struct ttysize.
3988 */
3989# ifdef TIOCGWINSZ
3990 {
3991 struct winsize ws;
3992 int fd = 1;
3993
Bram Moolenaar0f873732019-12-05 20:28:46 +01003994 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (!isatty(fd) && isatty(read_cmd_fd))
3996 fd = read_cmd_fd;
3997 if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
3998 {
3999 columns = ws.ws_col;
4000 rows = ws.ws_row;
4001 }
4002 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004003# else // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004# ifdef TIOCGSIZE
4005 {
4006 struct ttysize ts;
4007 int fd = 1;
4008
Bram Moolenaar0f873732019-12-05 20:28:46 +01004009 // When stdout is not a tty, use stdin for the ioctl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (!isatty(fd) && isatty(read_cmd_fd))
4011 fd = read_cmd_fd;
4012 if (ioctl(fd, TIOCGSIZE, &ts) == 0)
4013 {
4014 columns = ts.ts_cols;
4015 rows = ts.ts_lines;
4016 }
4017 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004018# endif // TIOCGSIZE
4019# endif // TIOCGWINSZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
4021 /*
4022 * 2. get size from environment
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004023 * When being POSIX compliant ('|' flag in 'cpoptions') this overrules
4024 * the ioctl() values!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004026 if (columns == 0 || rows == 0 || vim_strchr(p_cpo, CPO_TSIZE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
4028 if ((p = (char_u *)getenv("LINES")))
4029 rows = atoi((char *)p);
4030 if ((p = (char_u *)getenv("COLUMNS")))
4031 columns = atoi((char *)p);
4032 }
4033
4034#ifdef HAVE_TGETENT
4035 /*
4036 * 3. try reading "co" and "li" entries from termcap
4037 */
4038 if (columns == 0 || rows == 0)
4039 getlinecol(&columns, &rows);
4040#endif
4041
4042 /*
4043 * 4. If everything fails, use the old values
4044 */
4045 if (columns <= 0 || rows <= 0)
4046 return FAIL;
4047
4048 Rows = rows;
4049 Columns = columns;
Bram Moolenaare057d402013-06-30 17:51:51 +02004050 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return OK;
4052}
4053
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004054#if defined(FEAT_TERMINAL) || defined(PROTO)
4055/*
4056 * Report the windows size "rows" and "cols" to tty "fd".
4057 */
4058 int
4059mch_report_winsize(int fd, int rows, int cols)
4060{
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004061 int tty_fd;
4062 int retval = -1;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004063
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004064 tty_fd = get_tty_fd(fd);
4065 if (tty_fd >= 0)
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004066 {
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004067# if defined(TIOCSWINSZ)
4068 struct winsize ws;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004069
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004070 ws.ws_col = cols;
4071 ws.ws_row = rows;
4072 ws.ws_xpixel = cols * 5;
4073 ws.ws_ypixel = rows * 10;
4074 retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
4075 ch_log(NULL, "ioctl(TIOCSWINSZ) %s",
4076 retval == 0 ? "success" : "failed");
4077# elif defined(TIOCSSIZE)
4078 struct ttysize ts;
4079
4080 ts.ts_cols = cols;
4081 ts.ts_lines = rows;
4082 retval = ioctl(tty_fd, TIOCSSIZE, &ts);
4083 ch_log(NULL, "ioctl(TIOCSSIZE) %s",
4084 retval == 0 ? "success" : "failed");
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004085# endif
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004086 if (tty_fd != fd)
4087 close(tty_fd);
4088 }
4089 return retval == 0 ? OK : FAIL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02004090}
4091#endif
4092
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093/*
4094 * Try to set the window size to Rows and Columns.
4095 */
4096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004097mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 if (*T_CWS)
4100 {
4101 /*
4102 * NOTE: if you get an error here that term_set_winsize() is
4103 * undefined, check the output of configure. It could probably not
4104 * find a ncurses, termcap or termlib library.
4105 */
4106 term_set_winsize((int)Rows, (int)Columns);
4107 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01004108 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110}
4111
Bram Moolenaar0f873732019-12-05 20:28:46 +01004112#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114/*
4115 * Rows and/or Columns has changed.
4116 */
4117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004118mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
Bram Moolenaar0f873732019-12-05 20:28:46 +01004120 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122
Bram Moolenaar205b8862011-09-07 15:04:31 +02004123/*
4124 * Wait for process "child" to end.
4125 * Return "child" if it exited properly, <= 0 on error.
4126 */
4127 static pid_t
Bram Moolenaar05540972016-01-30 20:31:25 +01004128wait4pid(pid_t child, waitstatus *status)
Bram Moolenaar205b8862011-09-07 15:04:31 +02004129{
4130 pid_t wait_pid = 0;
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004131 long delay_msec = 1;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004132
4133 while (wait_pid != child)
4134 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004135 // When compiled with Python threads are probably used, in which case
4136 // wait() sometimes hangs for no obvious reason. Use waitpid()
4137 // instead and loop (like the GUI). Also needed for other interfaces,
4138 // they might call system().
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004139# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02004140 wait_pid = wait4(child, status, WNOHANG, (struct rusage *)0);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004141# else
Bram Moolenaar205b8862011-09-07 15:04:31 +02004142 wait_pid = waitpid(child, status, WNOHANG);
Bram Moolenaar6be120e2012-04-20 15:55:16 +02004143# endif
Bram Moolenaar205b8862011-09-07 15:04:31 +02004144 if (wait_pid == 0)
4145 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004146 // Wait for 1 to 10 msec before trying again.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02004147 mch_delay(delay_msec, TRUE);
4148 if (++delay_msec > 10)
4149 delay_msec = 10;
Bram Moolenaar205b8862011-09-07 15:04:31 +02004150 continue;
4151 }
Bram Moolenaar205b8862011-09-07 15:04:31 +02004152 if (wait_pid <= 0
4153# ifdef ECHILD
4154 && errno == ECHILD
4155# endif
4156 )
4157 break;
4158 }
4159 return wait_pid;
4160}
4161
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004162#if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar7da34602017-08-01 17:14:21 +02004163/*
4164 * Set the environment for a child process.
4165 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004166 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004167set_child_environment(
4168 long rows,
4169 long columns,
4170 char *term,
4171 int is_terminal UNUSED)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004172{
4173# ifdef HAVE_SETENV
4174 char envbuf[50];
4175# else
Bram Moolenaar5f7e7bd2017-07-22 14:08:43 +02004176 static char envbuf_Term[30];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004177 static char envbuf_Rows[20];
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004178 static char envbuf_Lines[20];
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004179 static char envbuf_Columns[20];
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004180 static char envbuf_Colors[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004181# ifdef FEAT_TERMINAL
Bram Moolenaard7a137f2018-06-12 18:05:24 +02004182 static char envbuf_Version[20];
Bram Moolenaar493359e2018-06-12 20:25:52 +02004183# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004184# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004185 static char envbuf_Servername[60];
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004186# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004187# endif
4188
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004189# ifdef HAVE_SETENV
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004190 setenv("TERM", term, 1);
4191 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004192 setenv("ROWS", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004193 sprintf((char *)envbuf, "%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004194 setenv("LINES", (char *)envbuf, 1);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004195 sprintf((char *)envbuf, "%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004196 setenv("COLUMNS", (char *)envbuf, 1);
Bram Moolenaar759d8152020-04-26 16:52:49 +02004197 sprintf((char *)envbuf, "%d", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004198 setenv("COLORS", (char *)envbuf, 1);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004199# ifdef FEAT_TERMINAL
4200 if (is_terminal)
4201 {
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004202 sprintf((char *)envbuf, "%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004203 setenv("VIM_TERMINAL", (char *)envbuf, 1);
4204 }
4205# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004206# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004207 setenv("VIM_SERVERNAME", serverName == NULL ? "" : (char *)serverName, 1);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004208# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004209# else
4210 /*
4211 * Putenv does not copy the string, it has to remain valid.
4212 * Use a static array to avoid losing allocated memory.
Bram Moolenaar7da34602017-08-01 17:14:21 +02004213 * This won't work well when running multiple children...
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004214 */
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004215 vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term);
4216 putenv(envbuf_Term);
4217 vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004218 putenv(envbuf_Rows);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004219 vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows);
4220 putenv(envbuf_Lines);
4221 vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns),
4222 "COLUMNS=%ld", columns);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004223 putenv(envbuf_Columns);
Bram Moolenaaraffc8fd2020-04-28 21:58:29 +02004224 vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004225 putenv(envbuf_Colors);
Bram Moolenaar493359e2018-06-12 20:25:52 +02004226# ifdef FEAT_TERMINAL
4227 if (is_terminal)
4228 {
4229 vim_snprintf(envbuf_Version, sizeof(envbuf_Version),
Bram Moolenaar5ecdf962018-06-13 20:49:50 +02004230 "VIM_TERMINAL=%ld", (long)get_vim_var_nr(VV_VERSION));
Bram Moolenaar493359e2018-06-12 20:25:52 +02004231 putenv(envbuf_Version);
4232 }
4233# endif
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004234# ifdef FEAT_CLIENTSERVER
Bram Moolenaar7da34602017-08-01 17:14:21 +02004235 vim_snprintf(envbuf_Servername, sizeof(envbuf_Servername),
4236 "VIM_SERVERNAME=%s", serverName == NULL ? "" : (char *)serverName);
4237 putenv(envbuf_Servername);
Bram Moolenaar2a4f06f2017-08-01 18:44:29 +02004238# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004239# endif
4240}
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004241
4242 static void
Bram Moolenaar493359e2018-06-12 20:25:52 +02004243set_default_child_environment(int is_terminal)
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004244{
Bram Moolenaar493359e2018-06-12 20:25:52 +02004245 set_child_environment(Rows, Columns, "dumb", is_terminal);
Bram Moolenaar58556cd2017-07-20 23:04:46 +02004246}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01004247#endif
4248
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004249#if defined(FEAT_GUI) || defined(FEAT_JOB_CHANNEL)
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004250/*
4251 * Open a PTY, with FD for the master and slave side.
4252 * When failing "pty_master_fd" and "pty_slave_fd" are -1.
Bram Moolenaar59386482019-02-10 22:43:46 +01004253 * When successful both file descriptors are stored and the allocated pty name
4254 * is stored in both "*name1" and "*name2".
Bram Moolenaar979e8c52017-08-01 15:08:07 +02004255 */
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004256 static void
Bram Moolenaar59386482019-02-10 22:43:46 +01004257open_pty(int *pty_master_fd, int *pty_slave_fd, char_u **name1, char_u **name2)
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004258{
4259 char *tty_name;
4260
Bram Moolenaar59386482019-02-10 22:43:46 +01004261 if (name1 != NULL)
4262 *name1 = NULL;
4263 if (name2 != NULL)
4264 *name2 = NULL;
4265
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004266 *pty_master_fd = mch_openpty(&tty_name); // open pty
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004267 if (*pty_master_fd >= 0)
4268 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004269 // Leaving out O_NOCTTY may lead to waitpid() always returning
4270 // 0 on Mac OS X 10.7 thereby causing freezes. Let's assume
4271 // adding O_NOCTTY always works when defined.
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004272#ifdef O_NOCTTY
4273 *pty_slave_fd = open(tty_name, O_RDWR | O_NOCTTY | O_EXTRA, 0);
4274#else
4275 *pty_slave_fd = open(tty_name, O_RDWR | O_EXTRA, 0);
4276#endif
4277 if (*pty_slave_fd < 0)
4278 {
4279 close(*pty_master_fd);
4280 *pty_master_fd = -1;
4281 }
Bram Moolenaar59386482019-02-10 22:43:46 +01004282 else
4283 {
4284 if (name1 != NULL)
4285 *name1 = vim_strsave((char_u *)tty_name);
4286 if (name2 != NULL)
4287 *name2 = vim_strsave((char_u *)tty_name);
4288 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004289 }
4290}
4291#endif
4292
Bram Moolenaarfae42832017-08-01 22:24:26 +02004293/*
4294 * Send SIGINT to a child process if "c" is an interrupt character.
4295 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02004296 static void
Bram Moolenaarfae42832017-08-01 22:24:26 +02004297may_send_sigint(int c UNUSED, pid_t pid UNUSED, pid_t wpid UNUSED)
4298{
4299# ifdef SIGINT
4300 if (c == Ctrl_C || c == intr_char)
4301 {
4302# ifdef HAVE_SETSID
4303 kill(-pid, SIGINT);
4304# else
4305 kill(0, SIGINT);
4306# endif
4307 if (wpid > 0)
4308 kill(wpid, SIGINT);
4309 }
4310# endif
4311}
4312
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004313#if !defined(USE_SYSTEM) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar13568252018-03-16 20:46:58 +01004314
Bram Moolenaar0f873732019-12-05 20:28:46 +01004315/*
4316 * Parse "cmd" and return the result in "argvp" which is an allocated array of
4317 * pointers, the last one is NULL.
4318 * The "sh_tofree" and "shcf_tofree" must be later freed by the caller.
4319 */
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004320 int
4321unix_build_argv(
Bram Moolenaar13568252018-03-16 20:46:58 +01004322 char_u *cmd,
4323 char ***argvp,
4324 char_u **sh_tofree,
4325 char_u **shcf_tofree)
4326{
4327 char **argv = NULL;
4328 int argc;
4329
4330 *sh_tofree = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004331 if (*sh_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004332 return FAIL;
4333
4334 if (mch_parse_cmd(*sh_tofree, TRUE, &argv, &argc) == FAIL)
4335 return FAIL;
4336 *argvp = argv;
4337
4338 if (cmd != NULL)
4339 {
4340 char_u *s;
4341 char_u *p;
4342
4343 if (extra_shell_arg != NULL)
4344 argv[argc++] = (char *)extra_shell_arg;
4345
Bram Moolenaar0f873732019-12-05 20:28:46 +01004346 // Break 'shellcmdflag' into white separated parts. This doesn't
4347 // handle quoted strings, they are very unlikely to appear.
Bram Moolenaar964b3742019-05-24 18:54:09 +02004348 *shcf_tofree = alloc(STRLEN(p_shcf) + 1);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004349 if (*shcf_tofree == NULL) // out of memory
Bram Moolenaar13568252018-03-16 20:46:58 +01004350 return FAIL;
4351 s = *shcf_tofree;
4352 p = p_shcf;
4353 while (*p != NUL)
4354 {
4355 argv[argc++] = (char *)s;
4356 while (*p && *p != ' ' && *p != TAB)
4357 *s++ = *p++;
4358 *s++ = NUL;
4359 p = skipwhite(p);
4360 }
4361
4362 argv[argc++] = (char *)cmd;
4363 }
4364 argv[argc] = NULL;
4365 return OK;
4366}
4367#endif
4368
4369#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
4370/*
4371 * Use a terminal window to run a shell command in.
4372 */
4373 static int
4374mch_call_shell_terminal(
4375 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004376 int options UNUSED) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004377{
4378 jobopt_T opt;
4379 char **argv = NULL;
4380 char_u *tofree1 = NULL;
4381 char_u *tofree2 = NULL;
4382 int retval = -1;
4383 buf_T *buf;
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004384 job_T *job;
Bram Moolenaar13568252018-03-16 20:46:58 +01004385 aco_save_T aco;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004386 oparg_T oa; // operator arguments
Bram Moolenaar13568252018-03-16 20:46:58 +01004387
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004388 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar13568252018-03-16 20:46:58 +01004389 goto theend;
4390
4391 init_job_options(&opt);
4392 ch_log(NULL, "starting terminal for system command '%s'", cmd);
4393 buf = term_start(NULL, argv, &opt, TERM_START_SYSTEM);
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004394 if (buf == NULL)
4395 goto theend;
4396
4397 job = term_getjob(buf->b_term);
4398 ++job->jv_refcount;
Bram Moolenaar13568252018-03-16 20:46:58 +01004399
Bram Moolenaar0f873732019-12-05 20:28:46 +01004400 // Find a window to make "buf" curbuf.
Bram Moolenaar13568252018-03-16 20:46:58 +01004401 aucmd_prepbuf(&aco, buf);
4402
4403 clear_oparg(&oa);
4404 while (term_use_loop())
4405 {
4406 if (oa.op_type == OP_NOP && oa.regname == NUL && !VIsual_active)
4407 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004408 // If terminal_loop() returns OK we got a key that is handled
4409 // in Normal model. We don't do redrawing anyway.
Bram Moolenaar13568252018-03-16 20:46:58 +01004410 if (terminal_loop(TRUE) == OK)
4411 normal_cmd(&oa, TRUE);
4412 }
4413 else
4414 normal_cmd(&oa, TRUE);
4415 }
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004416 retval = job->jv_exitval;
Bram Moolenaar13568252018-03-16 20:46:58 +01004417 ch_log(NULL, "system command finished");
4418
Bram Moolenaarf9c38832018-06-19 19:59:20 +02004419 job_unref(job);
4420
Bram Moolenaar0f873732019-12-05 20:28:46 +01004421 // restore curwin/curbuf and a few other things
Bram Moolenaar13568252018-03-16 20:46:58 +01004422 aucmd_restbuf(&aco);
4423
4424 wait_return(TRUE);
4425 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
4426
4427theend:
4428 vim_free(argv);
4429 vim_free(tofree1);
4430 vim_free(tofree2);
4431 return retval;
4432}
4433#endif
4434
4435#ifdef USE_SYSTEM
4436/*
4437 * Use system() to start the shell: simple but slow.
4438 */
4439 static int
4440mch_call_shell_system(
Bram Moolenaar05540972016-01-30 20:31:25 +01004441 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004442 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444#ifdef VMS
4445 char *ifn = NULL;
4446 char *ofn = NULL;
4447#endif
4448 int tmode = cur_tmode;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004449 char_u *newcmd; // only needed for unix
Bram Moolenaarde5e2c22016-11-04 20:35:31 +01004450 int x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 out_flush();
4453
4454 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004455 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaar62b42182010-09-21 22:09:37 +02004457# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004458 save_clipboard();
Bram Moolenaar62b42182010-09-21 22:09:37 +02004459 loose_clipboard();
4460# endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (cmd == NULL)
4463 x = system((char *)p_sh);
4464 else
4465 {
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004466# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (ofn = strchr((char *)cmd, '>'))
4468 *ofn++ = '\0';
4469 if (ifn = strchr((char *)cmd, '<'))
4470 {
4471 char *p;
4472
4473 *ifn++ = '\0';
Bram Moolenaar0f873732019-12-05 20:28:46 +01004474 p = strchr(ifn,' '); // chop off any trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (p)
4476 *p = '\0';
4477 }
4478 if (ofn)
4479 x = vms_sys((char *)cmd, ofn, ifn);
4480 else
4481 x = system((char *)cmd);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004482# else
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004483 newcmd = alloc(STRLEN(p_sh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 + (extra_shell_arg == NULL ? 0 : STRLEN(extra_shell_arg))
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004485 + STRLEN(p_shcf) + STRLEN(cmd) + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (newcmd == NULL)
4487 x = 0;
4488 else
4489 {
4490 sprintf((char *)newcmd, "%s %s %s %s", p_sh,
4491 extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
4492 (char *)p_shcf,
4493 (char *)cmd);
4494 x = system((char *)newcmd);
4495 vim_free(newcmd);
4496 }
Bram Moolenaara06ecab2016-07-16 14:47:36 +02004497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499# ifdef VMS
4500 x = vms_sys_status(x);
4501# endif
4502 if (emsg_silent)
4503 ;
4504 else if (x == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004505 msg_puts(_("\nCannot execute shell sh\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 else if (x && !(options & SHELL_SILENT))
4507 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004508 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 msg_outnum((long)x);
4510 msg_putchar('\n');
4511 }
4512
4513 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004514 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515# ifdef FEAT_TITLE
4516 resettitle();
4517# endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01004518# if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4519 restore_clipboard();
4520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 return x;
Bram Moolenaar13568252018-03-16 20:46:58 +01004522}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
Bram Moolenaar0f873732019-12-05 20:28:46 +01004524#else // USE_SYSTEM
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaar0f873732019-12-05 20:28:46 +01004526# define EXEC_FAILED 122 // Exit code when shell didn't execute. Don't use
4527 // 127, some shells use that already
4528# define OPEN_NULL_FAILED 123 // Exit code if /dev/null can't be opened
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaar13568252018-03-16 20:46:58 +01004530/*
4531 * Don't use system(), use fork()/exec().
4532 */
4533 static int
4534mch_call_shell_fork(
4535 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01004536 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01004537{
4538 int tmode = cur_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 pid_t pid;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004540 pid_t wpid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 pid_t wait_pid = 0;
4542# ifdef HAVE_UNION_WAIT
4543 union wait status;
4544# else
4545 int status = -1;
4546# endif
4547 int retval = -1;
4548 char **argv = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01004549 char_u *tofree1 = NULL;
4550 char_u *tofree2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 int i;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004552 int pty_master_fd = -1; // for pty's
Bram Moolenaardf177f62005-02-22 08:39:57 +00004553# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 int pty_slave_fd = -1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004555# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01004556 int fd_toshell[2]; // for pipes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 int fd_fromshell[2];
4558 int pipe_error = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004559 int did_settmode = FALSE; // settmode(TMODE_RAW) called
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 out_flush();
4562 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004563 settmode(TMODE_COOK); // set to normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaar197c6b72019-11-03 23:37:12 +01004565 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01004566 goto error;
Bram Moolenaarea35ef62011-08-04 22:59:28 +02004567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 /*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004569 * For the GUI, when writing the output into the buffer and when reading
4570 * input from the buffer: Try using a pseudo-tty to get the stdin/stdout
4571 * of the executed command into the Vim window. Or use a pipe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004573 if ((options & (SHELL_READ|SHELL_WRITE))
4574# ifdef FEAT_GUI
4575 || (gui.in_use && show_shell_mess)
4576# endif
4577 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 /*
4581 * Try to open a master pty.
4582 * If this works, open the slave pty.
4583 * If the slave can't be opened, close the master pty.
4584 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004585 if (p_guipty && !(options & (SHELL_READ|SHELL_WRITE)))
Bram Moolenaar59386482019-02-10 22:43:46 +01004586 open_pty(&pty_master_fd, &pty_slave_fd, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 /*
4588 * If not opening a pty or it didn't work, try using pipes.
4589 */
4590 if (pty_master_fd < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
4593 pipe_error = (pipe(fd_toshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004594 if (!pipe_error) // pipe create OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 pipe_error = (pipe(fd_fromshell) < 0);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004597 if (pipe_error) // pipe create failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 close(fd_toshell[0]);
4600 close(fd_toshell[1]);
4601 }
4602 }
4603 if (pipe_error)
4604 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004605 msg_puts(_("\nCannot create pipes\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 out_flush();
4607 }
4608 }
4609 }
4610
Bram Moolenaar0f873732019-12-05 20:28:46 +01004611 if (!pipe_error) // pty or pipe opened or not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004613 SIGSET_DECL(curset)
4614
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004615# if defined(__BEOS__) && USE_THREAD_FOR_INPUT_WITH_TIMEOUT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 beos_cleanup_read_thread();
4617# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004618
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004619 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004620 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004621 if (pid == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004623 UNBLOCK_SIGNALS(&curset);
4624
Bram Moolenaar32526b32019-01-19 17:43:09 +01004625 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004626 if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004628 || (gui.in_use && show_shell_mess)
4629# endif
4630 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00004632# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01004633 if (pty_master_fd >= 0) // close the pseudo tty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 close(pty_master_fd);
4636 close(pty_slave_fd);
4637 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004638 else // close the pipes
Bram Moolenaardf177f62005-02-22 08:39:57 +00004639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 close(fd_toshell[0]);
4642 close(fd_toshell[1]);
4643 close(fd_fromshell[0]);
4644 close(fd_fromshell[1]);
4645 }
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004648 else if (pid == 0) // child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004650 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004651 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
Bram Moolenaar819524702018-02-27 19:10:00 +01004653# ifdef FEAT_JOB_CHANNEL
4654 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01004655 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01004656 ch_logfile((char_u *)"", (char_u *)"");
4657# endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (!show_shell_mess || (options & SHELL_EXPAND))
4660 {
4661 int fd;
4662
4663 /*
4664 * Don't want to show any message from the shell. Can't just
4665 * close stdout and stderr though, because some systems will
4666 * break if you try to write to them after that, so we must
4667 * use dup() to replace them with something else -- webb
4668 * Connect stdin to /dev/null too, so ":n `cat`" doesn't hang,
4669 * waiting for input.
4670 */
4671 fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
4672 fclose(stdin);
4673 fclose(stdout);
4674 fclose(stderr);
4675
4676 /*
4677 * If any of these open()'s and dup()'s fail, we just continue
4678 * anyway. It's not fatal, and on most systems it will make
4679 * no difference at all. On a few it will cause the execvp()
4680 * to exit with a non-zero status even when the completion
4681 * could be done, which is nothing too serious. If the open()
4682 * or dup() failed we'd just do the same thing ourselves
4683 * anyway -- webb
4684 */
4685 if (fd >= 0)
4686 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004687 vim_ignored = dup(fd); // To replace stdin (fd 0)
4688 vim_ignored = dup(fd); // To replace stdout (fd 1)
4689 vim_ignored = dup(fd); // To replace stderr (fd 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar0f873732019-12-05 20:28:46 +01004691 // Don't need this now that we've duplicated it
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 close(fd);
4693 }
4694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004695 else if ((options & (SHELL_READ|SHELL_WRITE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696# ifdef FEAT_GUI
Bram Moolenaardf177f62005-02-22 08:39:57 +00004697 || gui.in_use
4698# endif
4699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701
Bram Moolenaardf177f62005-02-22 08:39:57 +00004702# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01004703 // Create our own process group, so that the child and all its
4704 // children can be kill()ed. Don't do this when using pipes,
4705 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004706 if (p_stmp)
Bram Moolenaar07256082009-02-04 13:19:42 +00004707 {
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004708 (void)setsid();
Bram Moolenaar07256082009-02-04 13:19:42 +00004709# if defined(SIGHUP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004710 // When doing "!xterm&" and 'shell' is bash: the shell
4711 // will exit and send SIGHUP to all processes in its
4712 // group, killing the just started process. Ignore SIGHUP
4713 // to avoid that. (suggested by Simon Schubert)
Bram Moolenaar07256082009-02-04 13:19:42 +00004714 signal(SIGHUP, SIG_IGN);
4715# endif
4716 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004717# endif
4718# ifdef FEAT_GUI
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004719 if (pty_slave_fd >= 0)
4720 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004721 // push stream discipline modules
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004722 if (options & SHELL_COOKED)
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01004723 setup_slavepty(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01004725 // Try to become controlling tty (probably doesn't work,
4726 // unless run by root)
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004727 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728# endif
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004729 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004730# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02004731 set_default_child_environment(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
Bram Moolenaara5792f52005-11-23 21:25:05 +00004733 /*
4734 * stderr is only redirected when using the GUI, so that a
4735 * program like gpg can still access the terminal to get a
4736 * passphrase using stderr.
4737 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004738# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if (pty_master_fd >= 0)
4740 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004741 close(pty_master_fd); // close master side of pty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar0f873732019-12-05 20:28:46 +01004743 // set up stdin/stdout/stderr for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004745 vim_ignored = dup(pty_slave_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004747 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004748 if (gui.in_use)
4749 {
4750 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004751 vim_ignored = dup(pty_slave_fd);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
Bram Moolenaar0f873732019-12-05 20:28:46 +01004754 close(pty_slave_fd); // has been dupped, close it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004759 // set up stdin for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 close(fd_toshell[1]);
4761 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004762 vim_ignored = dup(fd_toshell[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 close(fd_toshell[0]);
4764
Bram Moolenaar0f873732019-12-05 20:28:46 +01004765 // set up stdout for the child
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 close(fd_fromshell[0]);
4767 close(1);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004768 vim_ignored = dup(fd_fromshell[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 close(fd_fromshell[1]);
4770
Bram Moolenaara5792f52005-11-23 21:25:05 +00004771# ifdef FEAT_GUI
4772 if (gui.in_use)
4773 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004774 // set up stderr for the child
Bram Moolenaara5792f52005-11-23 21:25:05 +00004775 close(2);
Bram Moolenaar42335f52018-09-13 15:33:43 +02004776 vim_ignored = dup(1);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004777 }
4778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 /*
4783 * There is no type cast for the argv, because the type may be
4784 * different on different machines. This may cause a warning
4785 * message with strict compilers, don't worry about it.
4786 * Call _exit() instead of exit() to avoid closing the connection
4787 * to the X server (esp. with GTK, which uses atexit()).
4788 */
4789 execvp(argv[0], argv);
Bram Moolenaar0f873732019-12-05 20:28:46 +01004790 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004792 else // parent
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 /*
4795 * While child is running, ignore terminating signals.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004796 * Do catch CTRL-C, so that "got_int" is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
4798 catch_signals(SIG_IGN, SIG_ERR);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004799 catch_int_signal();
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02004800 UNBLOCK_SIGNALS(&curset);
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01004801# ifdef FEAT_JOB_CHANNEL
4802 ++dont_check_job_ended;
4803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 /*
4805 * For the GUI we redirect stdin, stdout and stderr to our window.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004806 * This is also used to pipe stdin/stdout to/from the external
4807 * command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004809 if ((options & (SHELL_READ|SHELL_WRITE))
4810# ifdef FEAT_GUI
4811 || (gui.in_use && show_shell_mess)
4812# endif
4813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004815# define BUFLEN 100 // length for buffer, pseudo tty limit is 128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 char_u buffer[BUFLEN + 1];
Bram Moolenaar0f873732019-12-05 20:28:46 +01004817 int buffer_off = 0; // valid bytes in buffer[]
4818 char_u ta_buf[BUFLEN + 1]; // TypeAHead
4819 int ta_len = 0; // valid bytes in ta_buf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 int len;
4821 int p_more_save;
4822 int old_State;
4823 int c;
4824 int toshell_fd;
4825 int fromshell_fd;
Bram Moolenaardf177f62005-02-22 08:39:57 +00004826 garray_T ga;
4827 int noread_cnt;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004828# ifdef ELAPSED_FUNC
4829 elapsed_T start_tv;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
Bram Moolenaardf177f62005-02-22 08:39:57 +00004832# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (pty_master_fd >= 0)
4834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 fromshell_fd = pty_master_fd;
4836 toshell_fd = dup(pty_master_fd);
4837 }
4838 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00004839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 close(fd_toshell[0]);
4842 close(fd_fromshell[1]);
4843 toshell_fd = fd_toshell[1];
4844 fromshell_fd = fd_fromshell[0];
4845 }
4846
4847 /*
4848 * Write to the child if there are typed characters.
4849 * Read from the child if there are characters available.
4850 * Repeat the reading a few times if more characters are
4851 * available. Need to check for typed keys now and then, but
4852 * not too often (delays when no chars are available).
4853 * This loop is quit if no characters can be read from the pty
4854 * (WaitForChar detected special condition), or there are no
4855 * characters available and the child has exited.
4856 * Only check if the child has exited when there is no more
4857 * output. The child may exit before all the output has
4858 * been printed.
4859 *
4860 * Currently this busy loops!
4861 * This can probably dead-lock when the write blocks!
4862 */
4863 p_more_save = p_more;
4864 p_more = FALSE;
4865 old_State = State;
Bram Moolenaar0f873732019-12-05 20:28:46 +01004866 State = EXTERNCMD; // don't redraw at window resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004868 if ((options & SHELL_WRITE) && toshell_fd >= 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004869 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004870 // Fork a process that will write the lines to the
4871 // external program.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004872 if ((wpid = fork()) == -1)
4873 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004874 msg_puts(_("\nCannot fork\n"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004875 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004876 else if (wpid == 0) // child
Bram Moolenaardf177f62005-02-22 08:39:57 +00004877 {
4878 linenr_T lnum = curbuf->b_op_start.lnum;
4879 int written = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004880 char_u *lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004881 size_t l;
4882
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004883 close(fromshell_fd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004884 for (;;)
4885 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004886 l = STRLEN(lp + written);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004887 if (l == 0)
4888 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004889 else if (lp[written] == NL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01004890 // NL -> NUL translation
Bram Moolenaardf177f62005-02-22 08:39:57 +00004891 len = write(toshell_fd, "", (size_t)1);
4892 else
4893 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004894 char_u *s = vim_strchr(lp + written, NL);
4895
Bram Moolenaar89d40322006-08-29 15:30:07 +00004896 len = write(toshell_fd, (char *)lp + written,
Bram Moolenaar78a15312009-05-15 19:33:18 +00004897 s == NULL ? l
4898 : (size_t)(s - (lp + written)));
Bram Moolenaardf177f62005-02-22 08:39:57 +00004899 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00004900 if (len == (int)l)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004901 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004902 // Finished a line, add a NL, unless this line
4903 // should not have one.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004904 if (lnum != curbuf->b_op_end.lnum
Bram Moolenaar34d72d42015-07-17 14:18:08 +02004905 || (!curbuf->b_p_bin
4906 && curbuf->b_p_fixeol)
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004907 || (lnum != curbuf->b_no_eol_lnum
Bram Moolenaardf177f62005-02-22 08:39:57 +00004908 && (lnum !=
4909 curbuf->b_ml.ml_line_count
4910 || curbuf->b_p_eol)))
Bram Moolenaar42335f52018-09-13 15:33:43 +02004911 vim_ignored = write(toshell_fd, "\n",
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004912 (size_t)1);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004913 ++lnum;
4914 if (lnum > curbuf->b_op_end.lnum)
4915 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004916 // finished all the lines, close pipe
Bram Moolenaardf177f62005-02-22 08:39:57 +00004917 close(toshell_fd);
4918 toshell_fd = -1;
4919 break;
4920 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00004921 lp = ml_get(lnum);
Bram Moolenaardf177f62005-02-22 08:39:57 +00004922 written = 0;
4923 }
4924 else if (len > 0)
4925 written += len;
4926 }
4927 _exit(0);
4928 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01004929 else // parent
Bram Moolenaardf177f62005-02-22 08:39:57 +00004930 {
4931 close(toshell_fd);
4932 toshell_fd = -1;
4933 }
4934 }
4935
4936 if (options & SHELL_READ)
4937 ga_init2(&ga, 1, BUFLEN);
4938
4939 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004940# ifdef ELAPSED_FUNC
4941 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 for (;;)
4944 {
4945 /*
4946 * Check if keys have been typed, write them to the child
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004947 * if there are any.
4948 * Don't do this if we are expanding wild cards (would eat
4949 * typeahead).
4950 * Don't do this when filtering and terminal is in cooked
4951 * mode, the shell command will handle the I/O. Avoids
4952 * that a typed password is echoed for ssh or gpg command.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004953 * Don't get characters when the child has already
4954 * finished (wait_pid == 0).
Bram Moolenaardf177f62005-02-22 08:39:57 +00004955 * Don't read characters unless we didn't get output for a
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004956 * while (noread_cnt > 4), avoids that ":r !ls" eats
4957 * typeahead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
4959 len = 0;
4960 if (!(options & SHELL_EXPAND)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004961 && ((options &
4962 (SHELL_READ|SHELL_WRITE|SHELL_COOKED))
4963 != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004964# ifdef FEAT_GUI
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004965 || gui.in_use
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004966# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00004967 )
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004968 && wait_pid == 0
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004969 && (ta_len > 0 || noread_cnt > 4))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004971 if (ta_len == 0)
4972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01004973 // Get extra characters when we don't have any.
4974 // Reset the counter and timer.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004975 noread_cnt = 0;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004976# ifdef ELAPSED_FUNC
4977 ELAPSED_INIT(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00004978# endif
4979 len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
4980 }
4981 if (ta_len > 0 || len > 0)
4982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 /*
4984 * For pipes:
4985 * Check for CTRL-C: send interrupt signal to child.
4986 * Check for CTRL-D: EOF, close pipe to child.
4987 */
4988 if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
4989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 /*
4991 * Send SIGINT to the child's group or all
4992 * processes in our group.
4993 */
Bram Moolenaarfae42832017-08-01 22:24:26 +02004994 may_send_sigint(ta_buf[ta_len], pid, wpid);
4995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 if (pty_master_fd < 0 && toshell_fd >= 0
4997 && ta_buf[ta_len] == Ctrl_D)
4998 {
4999 close(toshell_fd);
5000 toshell_fd = -1;
5001 }
5002 }
5003
Bram Moolenaarf4140482020-02-15 23:06:45 +01005004 term_replace_bs_del_keycode(ta_buf, ta_len, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /*
5007 * For pipes: echo the typed characters.
5008 * For a pty this does not seem to work.
5009 */
5010 if (pty_master_fd < 0)
5011 {
5012 for (i = ta_len; i < ta_len + len; ++i)
5013 {
5014 if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
5015 msg_putchar(ta_buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 else if (has_mbyte)
5017 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005018 int l = (*mb_ptr2len)(ta_buf + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020 msg_outtrans_len(ta_buf + i, l);
5021 i += l - 1;
5022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 else
5024 msg_outtrans_len(ta_buf + i, 1);
5025 }
5026 windgoto(msg_row, msg_col);
5027 out_flush();
5028 }
5029
5030 ta_len += len;
5031
5032 /*
5033 * Write the characters to the child, unless EOF has
5034 * been typed for pipes. Write one character at a
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005035 * time, to avoid losing too much typeahead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005036 * When writing buffer lines, drop the typed
5037 * characters (only check for CTRL-C).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005039 if (options & SHELL_WRITE)
5040 ta_len = 0;
5041 else if (toshell_fd >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 len = write(toshell_fd, (char *)ta_buf, (size_t)1);
5044 if (len > 0)
5045 {
5046 ta_len -= len;
5047 mch_memmove(ta_buf, ta_buf + len, ta_len);
5048 }
5049 }
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052
Bram Moolenaardf177f62005-02-22 08:39:57 +00005053 if (got_int)
5054 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005055 // CTRL-C sends a signal to the child, we ignore it
5056 // ourselves
Bram Moolenaardf177f62005-02-22 08:39:57 +00005057# ifdef HAVE_SETSID
5058 kill(-pid, SIGINT);
5059# else
5060 kill(0, SIGINT);
5061# endif
5062 if (wpid > 0)
5063 kill(wpid, SIGINT);
5064 got_int = FALSE;
5065 }
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 /*
5068 * Check if the child has any characters to be printed.
5069 * Read them and write them to our window. Repeat this as
5070 * long as there is something to do, avoid the 10ms wait
5071 * for mch_inchar(), or sending typeahead characters to
5072 * the external process.
5073 * TODO: This should handle escape sequences, compatible
5074 * to some terminal (vt52?).
5075 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076 ++noread_cnt;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01005077 while (RealWaitForChar(fromshell_fd, 10L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005079 len = read_eintr(fromshell_fd, buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 + buffer_off, (size_t)(BUFLEN - buffer_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 );
Bram Moolenaar0f873732019-12-05 20:28:46 +01005082 if (len <= 0) // end of file or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 goto finished;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005084
5085 noread_cnt = 0;
5086 if (options & SHELL_READ)
5087 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005088 // Do NUL -> NL translation, append NL separated
5089 // lines to the current buffer.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005090 for (i = 0; i < len; ++i)
5091 {
5092 if (buffer[i] == NL)
5093 append_ga_line(&ga);
5094 else if (buffer[i] == NUL)
5095 ga_append(&ga, NL);
5096 else
5097 ga_append(&ga, buffer[i]);
5098 }
5099 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005100 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 int l;
Bram Moolenaara2150ac2018-03-17 13:15:17 +01005103 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
Bram Moolenaardf177f62005-02-22 08:39:57 +00005105 len += buffer_off;
5106 buffer[len] = NUL;
5107
Bram Moolenaar0f873732019-12-05 20:28:46 +01005108 // Check if the last character in buffer[] is
5109 // incomplete, keep these bytes for the next
5110 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 for (p = buffer; p < buffer + len; p += l)
5112 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005113 l = MB_CPTR2LEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (l == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005115 l = 1; // NUL byte?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 else if (MB_BYTE2LEN(*p) != l)
5117 break;
5118 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01005119 if (p == buffer) // no complete character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005121 // avoid getting stuck at an illegal byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (len >= 12)
5123 ++p;
5124 else
5125 {
5126 buffer_off = len;
5127 continue;
5128 }
5129 }
5130 c = *p;
5131 *p = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005132 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (p < buffer + len)
5134 {
5135 *p = c;
5136 buffer_off = (buffer + len) - p;
5137 mch_memmove(buffer, p, buffer_off);
5138 continue;
5139 }
5140 buffer_off = 0;
5141 }
5142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
5144 buffer[len] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005145 msg_puts((char *)buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147
5148 windgoto(msg_row, msg_col);
5149 cursor_on();
5150 out_flush();
5151 if (got_int)
5152 break;
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005153
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005154# ifdef ELAPSED_FUNC
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005155 if (wait_pid == 0)
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005156 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01005157 long msec = ELAPSED_FUNC(start_tv);
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005158
Bram Moolenaar0f873732019-12-05 20:28:46 +01005159 // Avoid that we keep looping here without
5160 // checking for a CTRL-C for a long time. Don't
5161 // break out too often to avoid losing typeahead.
Bram Moolenaarb3dc8fd2009-02-22 01:52:59 +00005162 if (msec > 2000)
5163 {
5164 noread_cnt = 5;
5165 break;
5166 }
5167 }
5168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
Bram Moolenaar0f873732019-12-05 20:28:46 +01005171 // If we already detected the child has finished, continue
5172 // reading output for a short while. Some text may be
5173 // buffered.
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005174 if (wait_pid == pid)
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005175 {
5176 if (noread_cnt < 5)
5177 continue;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005178 break;
Bram Moolenaar17fe5e12016-04-04 22:03:08 +02005179 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 /*
5182 * Check if the child still exists, before checking for
Bram Moolenaare37d50a2008-08-06 17:06:04 +00005183 * typed characters (otherwise we would lose typeahead).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005185# ifdef __NeXT__
Bram Moolenaar205b8862011-09-07 15:04:31 +02005186 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005187# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 wait_pid = waitpid(pid, &status, WNOHANG);
Bram Moolenaardf177f62005-02-22 08:39:57 +00005189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5191 || (wait_pid == pid && WIFEXITED(status)))
5192 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005193 // Don't break the loop yet, try reading more
5194 // characters from "fromshell_fd" first. When using
5195 // pipes there might still be something to read and
5196 // then we'll break the loop at the "break" above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 wait_pid = pid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005199 else
5200 wait_pid = 0;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005201
Bram Moolenaar95a51352013-03-21 22:53:50 +01005202# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005203 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005204 clip_update();
5205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
5207finished:
5208 p_more = p_more_save;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005209 if (options & SHELL_READ)
5210 {
5211 if (ga.ga_len > 0)
5212 {
5213 append_ga_line(&ga);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005214 // remember that the NL was missing
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005215 curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005216 }
5217 else
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01005218 curbuf->b_no_eol_lnum = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00005219 ga_clear(&ga);
5220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 /*
5223 * Give all typeahead that wasn't used back to ui_inchar().
5224 */
5225 if (ta_len)
5226 ui_inchar_undo(ta_buf, ta_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 State = old_State;
5228 if (toshell_fd >= 0)
5229 close(toshell_fd);
5230 close(fromshell_fd);
5231 }
Bram Moolenaar95a51352013-03-21 22:53:50 +01005232# if defined(FEAT_XCLIPBOARD) && defined(FEAT_X11)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005233 else
5234 {
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005235 long delay_msec = 1;
5236
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005237 /*
5238 * Similar to the loop above, but only handle X events, no
5239 * I/O.
5240 */
5241 for (;;)
5242 {
5243 if (got_int)
5244 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005245 // CTRL-C sends a signal to the child, we ignore it
5246 // ourselves
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005247# ifdef HAVE_SETSID
5248 kill(-pid, SIGINT);
5249# else
5250 kill(0, SIGINT);
5251# endif
5252 got_int = FALSE;
5253 }
5254# ifdef __NeXT__
5255 wait_pid = wait4(pid, &status, WNOHANG, (struct rusage *)0);
5256# else
5257 wait_pid = waitpid(pid, &status, WNOHANG);
5258# endif
5259 if ((wait_pid == (pid_t)-1 && errno == ECHILD)
5260 || (wait_pid == pid && WIFEXITED(status)))
5261 {
5262 wait_pid = pid;
5263 break;
5264 }
5265
Bram Moolenaar0f873732019-12-05 20:28:46 +01005266 // Handle any X events, e.g. serving the clipboard.
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005267 clip_update();
5268
Bram Moolenaar0f873732019-12-05 20:28:46 +01005269 // Wait for 1 to 10 msec. 1 is faster but gives the child
5270 // less time.
Bram Moolenaar0abe0522016-08-28 16:53:12 +02005271 mch_delay(delay_msec, TRUE);
5272 if (++delay_msec > 10)
5273 delay_msec = 10;
Bram Moolenaar090cfc12013-03-19 12:35:42 +01005274 }
5275 }
5276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * Wait until our child has exited.
5280 * Ignore wait() returning pids of other children and returning
5281 * because of some signal like SIGWINCH.
5282 * Don't wait if wait_pid was already set above, indicating the
5283 * child already exited.
5284 */
Bram Moolenaar205b8862011-09-07 15:04:31 +02005285 if (wait_pid != pid)
5286 wait_pid = wait4pid(pid, &status);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
Bram Moolenaar624891f2010-10-13 16:22:09 +02005288# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01005289 // Close slave side of pty. Only do this after the child has
5290 // exited, otherwise the child may hang when it tries to write on
5291 // the pty.
Bram Moolenaar624891f2010-10-13 16:22:09 +02005292 if (pty_master_fd >= 0)
5293 close(pty_slave_fd);
5294# endif
5295
Bram Moolenaar0f873732019-12-05 20:28:46 +01005296 // Make sure the child that writes to the external program is
5297 // dead.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005298 if (wpid > 0)
Bram Moolenaar205b8862011-09-07 15:04:31 +02005299 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005300 kill(wpid, SIGKILL);
Bram Moolenaar205b8862011-09-07 15:04:31 +02005301 wait4pid(wpid, NULL);
5302 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00005303
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005304# ifdef FEAT_JOB_CHANNEL
5305 --dont_check_job_ended;
5306# endif
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 /*
5309 * Set to raw mode right now, otherwise a CTRL-C after
5310 * catch_signals() will kill Vim.
5311 */
5312 if (tmode == TMODE_RAW)
5313 settmode(TMODE_RAW);
5314 did_settmode = TRUE;
5315 set_signals();
5316
5317 if (WIFEXITED(status))
5318 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005319 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 retval = WEXITSTATUS(status);
Bram Moolenaar75676462013-01-30 14:55:42 +01005321 if (retval != 0 && !emsg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 if (retval == EXEC_FAILED)
5324 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005325 msg_puts(_("\nCannot execute shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 msg_outtrans(p_sh);
5327 msg_putchar('\n');
5328 }
5329 else if (!(options & SHELL_SILENT))
5330 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005331 msg_puts(_("\nshell returned "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 msg_outnum((long)retval);
5333 msg_putchar('\n');
5334 }
5335 }
5336 }
5337 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005338 msg_puts(_("\nCommand terminated\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342error:
5343 if (!did_settmode)
5344 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005345 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346# ifdef FEAT_TITLE
5347 resettitle();
5348# endif
Bram Moolenaar13568252018-03-16 20:46:58 +01005349 vim_free(argv);
5350 vim_free(tofree1);
5351 vim_free(tofree2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
5353 return retval;
Bram Moolenaar13568252018-03-16 20:46:58 +01005354}
Bram Moolenaar0f873732019-12-05 20:28:46 +01005355#endif // USE_SYSTEM
Bram Moolenaar13568252018-03-16 20:46:58 +01005356
5357 int
5358mch_call_shell(
5359 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01005360 int options) // SHELL_*, see vim.h
Bram Moolenaar13568252018-03-16 20:46:58 +01005361{
5362#if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
5363 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL)
5364 return mch_call_shell_terminal(cmd, options);
5365#endif
5366#ifdef USE_SYSTEM
5367 return mch_call_shell_system(cmd, options);
5368#else
5369 return mch_call_shell_fork(cmd, options);
5370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371}
5372
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005373#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005374 void
Bram Moolenaar493359e2018-06-12 20:25:52 +02005375mch_job_start(char **argv, job_T *job, jobopt_T *options, int is_terminal)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005376{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005377 pid_t pid;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005378 int fd_in[2] = {-1, -1}; // for stdin
5379 int fd_out[2] = {-1, -1}; // for stdout
5380 int fd_err[2] = {-1, -1}; // for stderr
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005381 int pty_master_fd = -1;
5382 int pty_slave_fd = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01005383 channel_T *channel = NULL;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005384 int use_null_for_in = options->jo_io[PART_IN] == JIO_NULL;
5385 int use_null_for_out = options->jo_io[PART_OUT] == JIO_NULL;
5386 int use_null_for_err = options->jo_io[PART_ERR] == JIO_NULL;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005387 int use_file_for_in = options->jo_io[PART_IN] == JIO_FILE;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005388 int use_file_for_out = options->jo_io[PART_OUT] == JIO_FILE;
5389 int use_file_for_err = options->jo_io[PART_ERR] == JIO_FILE;
Bram Moolenaarb2412082017-08-20 18:09:14 +02005390 int use_buffer_for_in = options->jo_io[PART_IN] == JIO_BUFFER;
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005391 int use_out_for_err = options->jo_io[PART_ERR] == JIO_OUT;
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005392 SIGSET_DECL(curset)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005393
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005394 if (use_out_for_err && use_null_for_out)
5395 use_null_for_err = TRUE;
5396
Bram Moolenaar0f873732019-12-05 20:28:46 +01005397 // default is to fail
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005398 job->jv_status = JOB_FAILED;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005399
Bram Moolenaarb2412082017-08-20 18:09:14 +02005400 if (options->jo_pty
5401 && (!(use_file_for_in || use_null_for_in)
Bram Moolenaar59386482019-02-10 22:43:46 +01005402 || !(use_file_for_out || use_null_for_out)
Bram Moolenaarb2412082017-08-20 18:09:14 +02005403 || !(use_out_for_err || use_file_for_err || use_null_for_err)))
Bram Moolenaar59386482019-02-10 22:43:46 +01005404 open_pty(&pty_master_fd, &pty_slave_fd,
5405 &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005406
Bram Moolenaar0f873732019-12-05 20:28:46 +01005407 // TODO: without the channel feature connect the child to /dev/null?
5408 // Open pipes for stdin, stdout, stderr.
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005409 if (use_file_for_in)
5410 {
5411 char_u *fname = options->jo_io_name[PART_IN];
5412
5413 fd_in[0] = mch_open((char *)fname, O_RDONLY, 0);
5414 if (fd_in[0] < 0)
5415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005416 semsg(_(e_notopen), fname);
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005417 goto failed;
5418 }
5419 }
Bram Moolenaarb2412082017-08-20 18:09:14 +02005420 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01005421 // When writing buffer lines to the input don't use the pty, so that
5422 // the pipe can be closed when all lines were written.
Bram Moolenaarb2412082017-08-20 18:09:14 +02005423 if (!use_null_for_in && (pty_master_fd < 0 || use_buffer_for_in)
5424 && pipe(fd_in) < 0)
5425 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005426
5427 if (use_file_for_out)
5428 {
5429 char_u *fname = options->jo_io_name[PART_OUT];
5430
5431 fd_out[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
5432 if (fd_out[1] < 0)
5433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005434 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005435 goto failed;
5436 }
5437 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005438 else if (!use_null_for_out && pty_master_fd < 0 && pipe(fd_out) < 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005439 goto failed;
Bram Moolenaare98d1212016-03-08 15:37:41 +01005440
5441 if (use_file_for_err)
5442 {
5443 char_u *fname = options->jo_io_name[PART_ERR];
5444
5445 fd_err[1] = mch_open((char *)fname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
5446 if (fd_err[1] < 0)
5447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005448 semsg(_(e_notopen), fname);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005449 goto failed;
5450 }
5451 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005452 else if (!use_out_for_err && !use_null_for_err
5453 && pty_master_fd < 0 && pipe(fd_err) < 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005454 goto failed;
5455
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005456 if (!use_null_for_in || !use_null_for_out || !use_null_for_err)
5457 {
Bram Moolenaarde279892016-03-11 22:19:44 +01005458 if (options->jo_set & JO_CHANNEL)
5459 {
5460 channel = options->jo_channel;
5461 if (channel != NULL)
5462 ++channel->ch_refcount;
5463 }
5464 else
5465 channel = add_channel();
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005466 if (channel == NULL)
5467 goto failed;
Bram Moolenaarf3360612017-10-01 16:21:31 +02005468 if (job->jv_tty_out != NULL)
5469 ch_log(channel, "using pty %s on fd %d",
5470 job->jv_tty_out, pty_master_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005471 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005472
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005473 BLOCK_SIGNALS(&curset);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005474 pid = fork(); // maybe we should use vfork()
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005475 if (pid == -1)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005476 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005477 // failed to fork
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005478 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005479 goto failed;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005481 if (pid == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 {
Bram Moolenaar4694a172016-04-21 14:05:23 +02005483 int null_fd = -1;
5484 int stderr_works = TRUE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005485
Bram Moolenaar0f873732019-12-05 20:28:46 +01005486 // child
5487 reset_signals(); // handle signals normally
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005488 UNBLOCK_SIGNALS(&curset);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005489
Bram Moolenaar819524702018-02-27 19:10:00 +01005490# ifdef FEAT_JOB_CHANNEL
5491 if (ch_log_active())
Bram Moolenaar0f873732019-12-05 20:28:46 +01005492 // close the log file in the child
Bram Moolenaar819524702018-02-27 19:10:00 +01005493 ch_logfile((char_u *)"", (char_u *)"");
5494# endif
5495
Bram Moolenaar835dc632016-02-07 14:27:38 +01005496# ifdef HAVE_SETSID
Bram Moolenaar0f873732019-12-05 20:28:46 +01005497 // Create our own process group, so that the child and all its
5498 // children can be kill()ed. Don't do this when using pipes,
5499 // because stdin is not a tty, we would lose /dev/tty.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005500 (void)setsid();
5501# endif
5502
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005503# ifdef FEAT_TERMINAL
5504 if (options->jo_term_rows > 0)
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005505 {
5506 char *term = (char *)T_NAME;
5507
5508#ifdef FEAT_GUI
5509 if (term_is_gui(T_NAME))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005510 // In the GUI 'term' is not what we want, use $TERM.
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005511 term = getenv("TERM");
5512#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005513 // Use 'term' or $TERM if it starts with "xterm", otherwise fall
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005514 // back to "xterm" or "xterm-color".
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005515 if (term == NULL || *term == NUL || STRNCMP(term, "xterm", 5) != 0)
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005516 {
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005517 if (t_colors >= 256)
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005518 // TODO: should we check this name is supported?
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005519 term = "xterm-256color";
Bram Moolenaar4d5d0df2020-04-14 20:56:31 +02005520 else if (t_colors > 16)
5521 term = "xterm-color";
Bram Moolenaar5ba8d352020-04-05 21:42:12 +02005522 else
5523 term = "xterm";
5524 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005525 set_child_environment(
5526 (long)options->jo_term_rows,
5527 (long)options->jo_term_cols,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005528 term,
5529 is_terminal);
Bram Moolenaar9a993e32018-04-05 22:15:22 +02005530 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02005531 else
5532# endif
Bram Moolenaar493359e2018-06-12 20:25:52 +02005533 set_default_child_environment(is_terminal);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005534
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005535 if (options->jo_env != NULL)
5536 {
5537 dict_T *dict = options->jo_env;
5538 hashitem_T *hi;
5539 int todo = (int)dict->dv_hashtab.ht_used;
5540
5541 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
5542 if (!HASHITEM_EMPTY(hi))
5543 {
5544 typval_T *item = &dict_lookup(hi)->di_tv;
5545
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005546 vim_setenv((char_u*)hi->hi_key, tv_get_string(item));
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005547 --todo;
5548 }
5549 }
5550
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005551 if (use_null_for_in || use_null_for_out || use_null_for_err)
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005552 {
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005553 null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0);
Bram Moolenaarb109bb42017-08-21 21:07:29 +02005554 if (null_fd < 0)
5555 {
5556 perror("opening /dev/null failed");
5557 _exit(OPEN_NULL_FAILED);
5558 }
5559 }
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005560
Bram Moolenaar223896d2017-08-02 22:33:28 +02005561 if (pty_slave_fd >= 0)
5562 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005563 // push stream discipline modules
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01005564 setup_slavepty(pty_slave_fd);
Bram Moolenaar223896d2017-08-02 22:33:28 +02005565# ifdef TIOCSCTTY
Bram Moolenaar0f873732019-12-05 20:28:46 +01005566 // Try to become controlling tty (probably doesn't work,
5567 // unless run by root)
Bram Moolenaar223896d2017-08-02 22:33:28 +02005568 ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
5569# endif
5570 }
5571
Bram Moolenaar0f873732019-12-05 20:28:46 +01005572 // set up stdin for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005573 close(0);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005574 if (use_null_for_in && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005575 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005576 else if (fd_in[0] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005577 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005578 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005579 vim_ignored = dup(fd_in[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005580
Bram Moolenaar0f873732019-12-05 20:28:46 +01005581 // set up stderr for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005582 close(2);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005583 if (use_null_for_err && null_fd >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005584 {
Bram Moolenaar42335f52018-09-13 15:33:43 +02005585 vim_ignored = dup(null_fd);
Bram Moolenaar4694a172016-04-21 14:05:23 +02005586 stderr_works = FALSE;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005587 }
5588 else if (use_out_for_err)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005589 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005590 else if (fd_err[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005591 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005592 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005593 vim_ignored = dup(fd_err[1]);
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005594
Bram Moolenaar0f873732019-12-05 20:28:46 +01005595 // set up stdout for the child
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005596 close(1);
Bram Moolenaarc0a1d7f2016-03-19 14:12:50 +01005597 if (use_null_for_out && null_fd >= 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005598 vim_ignored = dup(null_fd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005599 else if (fd_out[1] < 0)
Bram Moolenaar42335f52018-09-13 15:33:43 +02005600 vim_ignored = dup(pty_slave_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005601 else
Bram Moolenaar42335f52018-09-13 15:33:43 +02005602 vim_ignored = dup(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005603
5604 if (fd_in[0] >= 0)
5605 close(fd_in[0]);
5606 if (fd_in[1] >= 0)
5607 close(fd_in[1]);
5608 if (fd_out[0] >= 0)
5609 close(fd_out[0]);
5610 if (fd_out[1] >= 0)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005611 close(fd_out[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005612 if (fd_err[0] >= 0)
5613 close(fd_err[0]);
5614 if (fd_err[1] >= 0)
5615 close(fd_err[1]);
5616 if (pty_master_fd >= 0)
5617 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005618 close(pty_master_fd); // not used in the child
5619 close(pty_slave_fd); // was duped above
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005620 }
Bram Moolenaarea83bf02016-05-08 09:40:51 +02005621
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005622 if (null_fd >= 0)
5623 close(null_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005624
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005625 if (options->jo_cwd != NULL && mch_chdir((char *)options->jo_cwd) != 0)
5626 _exit(EXEC_FAILED);
5627
Bram Moolenaar0f873732019-12-05 20:28:46 +01005628 // See above for type of argv.
Bram Moolenaar835dc632016-02-07 14:27:38 +01005629 execvp(argv[0], argv);
5630
Bram Moolenaar4694a172016-04-21 14:05:23 +02005631 if (stderr_works)
5632 perror("executing job failed");
Bram Moolenaarfae42832017-08-01 22:24:26 +02005633# ifdef EXITFREE
Bram Moolenaar0f873732019-12-05 20:28:46 +01005634 // calling free_all_mem() here causes problems. Ignore valgrind
5635 // reporting possibly leaked memory.
Bram Moolenaarfae42832017-08-01 22:24:26 +02005636# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01005637 _exit(EXEC_FAILED); // exec failed, return failure code
Bram Moolenaar835dc632016-02-07 14:27:38 +01005638 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005639
Bram Moolenaar0f873732019-12-05 20:28:46 +01005640 // parent
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02005641 UNBLOCK_SIGNALS(&curset);
5642
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005643 job->jv_pid = pid;
5644 job->jv_status = JOB_STARTED;
Bram Moolenaar0f873732019-12-05 20:28:46 +01005645 job->jv_channel = channel; // ch_refcount was set above
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005646
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005647 if (pty_master_fd >= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005648 close(pty_slave_fd); // not used in the parent
5649 // close child stdin, stdout and stderr
Bram Moolenaar819524702018-02-27 19:10:00 +01005650 if (fd_in[0] >= 0)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +01005651 close(fd_in[0]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005652 if (fd_out[1] >= 0)
Bram Moolenaare98d1212016-03-08 15:37:41 +01005653 close(fd_out[1]);
Bram Moolenaar819524702018-02-27 19:10:00 +01005654 if (fd_err[1] >= 0)
Bram Moolenaarc25558b2016-03-03 21:02:23 +01005655 close(fd_err[1]);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005656 if (channel != NULL)
5657 {
Bram Moolenaar652de232019-04-04 20:13:09 +02005658 int in_fd = INVALID_FD;
5659 int out_fd = INVALID_FD;
5660 int err_fd = INVALID_FD;
5661
5662 if (!(use_file_for_in || use_null_for_in))
5663 in_fd = fd_in[1] >= 0 ? fd_in[1] : pty_master_fd;
5664
5665 if (!(use_file_for_out || use_null_for_out))
5666 out_fd = fd_out[0] >= 0 ? fd_out[0] : pty_master_fd;
5667
5668 // When using pty_master_fd only set it for stdout, do not duplicate
5669 // it for stderr, it only needs to be read once.
5670 if (!(use_out_for_err || use_file_for_err || use_null_for_err))
5671 {
5672 if (fd_err[0] >= 0)
5673 err_fd = fd_err[0];
5674 else if (out_fd != pty_master_fd)
5675 err_fd = pty_master_fd;
5676 }
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005677
5678 channel_set_pipes(channel, in_fd, out_fd, err_fd);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005679 channel_set_job(channel, job, options);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01005680 }
Bram Moolenaar979e8c52017-08-01 15:08:07 +02005681 else
5682 {
5683 if (fd_in[1] >= 0)
5684 close(fd_in[1]);
5685 if (fd_out[0] >= 0)
5686 close(fd_out[0]);
5687 if (fd_err[0] >= 0)
5688 close(fd_err[0]);
5689 if (pty_master_fd >= 0)
5690 close(pty_master_fd);
5691 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005692
Bram Moolenaar0f873732019-12-05 20:28:46 +01005693 // success!
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005694 return;
5695
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005696failed:
Bram Moolenaarde279892016-03-11 22:19:44 +01005697 channel_unref(channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005698 if (fd_in[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005699 close(fd_in[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005700 if (fd_in[1] >= 0)
5701 close(fd_in[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005702 if (fd_out[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005703 close(fd_out[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005704 if (fd_out[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005705 close(fd_out[1]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005706 if (fd_err[0] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005707 close(fd_err[0]);
Bram Moolenaare98d1212016-03-08 15:37:41 +01005708 if (fd_err[1] >= 0)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005709 close(fd_err[1]);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005710 if (pty_master_fd >= 0)
5711 close(pty_master_fd);
5712 if (pty_slave_fd >= 0)
5713 close(pty_slave_fd);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005714}
5715
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005716 static char_u *
5717get_signal_name(int sig)
5718{
5719 int i;
5720 char_u numbuf[NUMBUFLEN];
5721
5722 if (sig == SIGKILL)
5723 return vim_strsave((char_u *)"kill");
5724
5725 for (i = 0; signal_info[i].sig != -1; i++)
5726 if (sig == signal_info[i].sig)
5727 return strlow_save((char_u *)signal_info[i].name);
5728
5729 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d", sig);
5730 return vim_strsave(numbuf);
5731}
5732
Bram Moolenaar835dc632016-02-07 14:27:38 +01005733 char *
5734mch_job_status(job_T *job)
5735{
5736# ifdef HAVE_UNION_WAIT
5737 union wait status;
5738# else
5739 int status = -1;
5740# endif
5741 pid_t wait_pid = 0;
5742
5743# ifdef __NeXT__
5744 wait_pid = wait4(job->jv_pid, &status, WNOHANG, (struct rusage *)0);
5745# else
5746 wait_pid = waitpid(job->jv_pid, &status, WNOHANG);
5747# endif
5748 if (wait_pid == -1)
5749 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005750 // process must have exited
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005751 if (job->jv_status < JOB_ENDED)
5752 ch_log(job->jv_channel, "Job no longer exists: %s",
5753 strerror(errno));
Bram Moolenaar97792de2016-10-15 18:36:49 +02005754 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005755 }
5756 if (wait_pid == 0)
5757 return "run";
5758 if (WIFEXITED(status))
5759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01005760 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005761 job->jv_exitval = WEXITSTATUS(status);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005762 if (job->jv_status < JOB_ENDED)
5763 ch_log(job->jv_channel, "Job exited with %d", job->jv_exitval);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005764 goto return_dead;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005765 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005766 if (WIFSIGNALED(status))
5767 {
5768 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005769 job->jv_termsig = get_signal_name(WTERMSIG(status));
5770 if (job->jv_status < JOB_ENDED && job->jv_termsig != NULL)
5771 ch_log(job->jv_channel, "Job terminated by signal \"%s\"",
5772 job->jv_termsig);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005773 goto return_dead;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005774 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01005775 return "run";
Bram Moolenaar97792de2016-10-15 18:36:49 +02005776
5777return_dead:
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005778 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005779 job->jv_status = JOB_ENDED;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005780 return "dead";
5781}
5782
5783 job_T *
5784mch_detect_ended_job(job_T *job_list)
5785{
5786# ifdef HAVE_UNION_WAIT
5787 union wait status;
5788# else
5789 int status = -1;
5790# endif
5791 pid_t wait_pid = 0;
5792 job_T *job;
5793
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005794# ifndef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01005795 // Do not do this when waiting for a shell command to finish, we would get
5796 // the exit value here (and discard it), the exit value obtained there
5797 // would then be wrong.
Bram Moolenaarc4d4ac22016-11-07 22:42:57 +01005798 if (dont_check_job_ended > 0)
5799 return NULL;
5800# endif
5801
Bram Moolenaar97792de2016-10-15 18:36:49 +02005802# ifdef __NeXT__
5803 wait_pid = wait4(-1, &status, WNOHANG, (struct rusage *)0);
5804# else
5805 wait_pid = waitpid(-1, &status, WNOHANG);
5806# endif
5807 if (wait_pid <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005808 // no process ended
Bram Moolenaar97792de2016-10-15 18:36:49 +02005809 return NULL;
5810 for (job = job_list; job != NULL; job = job->jv_next)
5811 {
5812 if (job->jv_pid == wait_pid)
5813 {
5814 if (WIFEXITED(status))
Bram Moolenaar0f873732019-12-05 20:28:46 +01005815 // LINTED avoid "bitwise operation on signed value"
Bram Moolenaar97792de2016-10-15 18:36:49 +02005816 job->jv_exitval = WEXITSTATUS(status);
5817 else if (WIFSIGNALED(status))
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005818 {
Bram Moolenaar97792de2016-10-15 18:36:49 +02005819 job->jv_exitval = -1;
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005820 job->jv_termsig = get_signal_name(WTERMSIG(status));
5821 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005822 if (job->jv_status < JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005823 {
5824 ch_log(job->jv_channel, "Job ended");
5825 job->jv_status = JOB_ENDED;
5826 }
5827 return job;
5828 }
5829 }
5830 return NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005831}
5832
Bram Moolenaar3a117e12016-10-30 21:57:52 +01005833/*
5834 * Send a (deadly) signal to "job".
5835 * Return FAIL if "how" is not a valid name.
5836 */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005837 int
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005838mch_signal_job(job_T *job, char_u *how)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005839{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01005840 int sig = -1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841
Bram Moolenaar923d9262016-02-25 20:56:01 +01005842 if (*how == NUL || STRCMP(how, "term") == 0)
Bram Moolenaar835dc632016-02-07 14:27:38 +01005843 sig = SIGTERM;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005844 else if (STRCMP(how, "hup") == 0)
5845 sig = SIGHUP;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846 else if (STRCMP(how, "quit") == 0)
5847 sig = SIGQUIT;
Bram Moolenaar923d9262016-02-25 20:56:01 +01005848 else if (STRCMP(how, "int") == 0)
5849 sig = SIGINT;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850 else if (STRCMP(how, "kill") == 0)
5851 sig = SIGKILL;
Bram Moolenaarb13501f2017-07-22 22:32:56 +02005852#ifdef SIGWINCH
5853 else if (STRCMP(how, "winch") == 0)
5854 sig = SIGWINCH;
5855#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 else if (isdigit(*how))
5857 sig = atoi((char *)how);
5858 else
5859 return FAIL;
Bram Moolenaar76467df2016-02-12 19:30:26 +01005860
Bram Moolenaar76ab4fd2018-12-08 14:39:05 +01005861 // Never kill ourselves!
5862 if (job->jv_pid != 0)
5863 {
5864 // TODO: have an option to only kill the process, not the group?
5865 kill(-job->jv_pid, sig);
5866 kill(job->jv_pid, sig);
5867 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01005868
Bram Moolenaar835dc632016-02-07 14:27:38 +01005869 return OK;
5870}
Bram Moolenaar76467df2016-02-12 19:30:26 +01005871
5872/*
5873 * Clear the data related to "job".
5874 */
5875 void
5876mch_clear_job(job_T *job)
5877{
Bram Moolenaar0f873732019-12-05 20:28:46 +01005878 // call waitpid because child process may become zombie
Bram Moolenaar76467df2016-02-12 19:30:26 +01005879# ifdef __NeXT__
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005880 (void)wait4(job->jv_pid, NULL, WNOHANG, (struct rusage *)0);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005881# else
Bram Moolenaar4ca812b2016-03-02 21:51:16 +01005882 (void)waitpid(job->jv_pid, NULL, WNOHANG);
Bram Moolenaar76467df2016-02-12 19:30:26 +01005883# endif
5884}
Bram Moolenaar835dc632016-02-07 14:27:38 +01005885#endif
5886
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005887#if defined(FEAT_TERMINAL) || defined(PROTO)
5888 int
5889mch_create_pty_channel(job_T *job, jobopt_T *options)
5890{
5891 int pty_master_fd = -1;
5892 int pty_slave_fd = -1;
5893 channel_T *channel;
5894
Bram Moolenaar59386482019-02-10 22:43:46 +01005895 open_pty(&pty_master_fd, &pty_slave_fd, &job->jv_tty_out, &job->jv_tty_in);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005896 close(pty_slave_fd);
5897
5898 channel = add_channel();
5899 if (channel == NULL)
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005900 {
5901 close(pty_master_fd);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005902 return FAIL;
Bram Moolenaar1b9f9d32017-09-05 23:32:38 +02005903 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02005904 if (job->jv_tty_out != NULL)
5905 ch_log(channel, "using pty %s on fd %d",
5906 job->jv_tty_out, pty_master_fd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01005907 job->jv_channel = channel; // ch_refcount was set by add_channel()
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005908 channel->ch_keep_open = TRUE;
5909
Bram Moolenaar0f873732019-12-05 20:28:46 +01005910 // Only set the pty_master_fd for stdout, do not duplicate it for stderr,
5911 // it only needs to be read once.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005912 channel_set_pipes(channel, pty_master_fd, pty_master_fd, INVALID_FD);
Bram Moolenaar13ebb032017-08-26 22:02:51 +02005913 channel_set_job(channel, job, options);
5914 return OK;
5915}
5916#endif
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918/*
5919 * Check for CTRL-C typed by reading all available characters.
5920 * In cooked mode we should get SIGINT, no need to check.
5921 */
5922 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005923mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02005925 if ((curr_tmode == TMODE_RAW || force)
5926 && RealWaitForChar(read_cmd_fd, 0L, NULL, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 fill_input_buf(FALSE);
5928}
5929
5930/*
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005931 * Wait "msec" msec until a character is available from the mouse, keyboard,
5932 * from inbuf[].
5933 * "msec" == -1 will block forever.
5934 * Invokes timer callbacks when needed.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005935 * When "ignore_input" is TRUE even check for pending input when input is
5936 * already available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005937 * "interrupted" (if not NULL) is set to TRUE when no character is available
5938 * but something else needs to be done.
Bram Moolenaar40b1b542016-04-20 20:18:23 +02005939 * Returns TRUE when a character is available.
Bram Moolenaarcda77642016-06-04 13:32:35 +02005940 * When a GUI is being used, this will never get called -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 */
5942 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005943WaitForChar(long msec, int *interrupted, int ignore_input)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005945#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01005946 return ui_wait_for_chars_or_timer(
5947 msec, WaitForCharOrMouse, interrupted, ignore_input) == OK;
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005948#else
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005949 return WaitForCharOrMouse(msec, interrupted, ignore_input);
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005950#endif
5951}
5952
5953/*
5954 * Wait "msec" msec until a character is available from the mouse or keyboard
5955 * or from inbuf[].
5956 * "msec" == -1 will block forever.
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005957 * for "ignore_input" see WaitForCharOr().
Bram Moolenaarcda77642016-06-04 13:32:35 +02005958 * "interrupted" (if not NULL) is set to TRUE when no character is available
5959 * but something else needs to be done.
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005960 * When a GUI is being used, this will never get called -- webb
5961 */
5962 static int
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02005963WaitForCharOrMouse(long msec, int *interrupted, int ignore_input)
Bram Moolenaar943bb2b2016-03-19 14:11:18 +01005964{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965#ifdef FEAT_MOUSE_GPM
5966 int gpm_process_wanted;
5967#endif
5968#ifdef FEAT_XCLIPBOARD
5969 int rest;
5970#endif
5971 int avail;
5972
Bram Moolenaar0f873732019-12-05 20:28:46 +01005973 if (!ignore_input && input_available()) // something in inbuf[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 return 1;
5975
5976#if defined(FEAT_MOUSE_DEC)
Bram Moolenaar0f873732019-12-05 20:28:46 +01005977 // May need to query the mouse position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 if (WantQueryMouse)
5979 {
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005980 WantQueryMouse = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +02005981 if (!no_query_mouse_for_testing)
5982 mch_write((char_u *)IF_EB("\033[1'|", ESC_STR "[1'|"), 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
5984#endif
5985
5986 /*
5987 * For FEAT_MOUSE_GPM and FEAT_XCLIPBOARD we loop here to process mouse
5988 * events. This is a bit complicated, because they might both be defined.
5989 */
5990#if defined(FEAT_MOUSE_GPM) || defined(FEAT_XCLIPBOARD)
5991# ifdef FEAT_XCLIPBOARD
5992 rest = 0;
5993 if (do_xterm_trace())
5994 rest = msec;
5995# endif
5996 do
5997 {
5998# ifdef FEAT_XCLIPBOARD
5999 if (rest != 0)
6000 {
6001 msec = XT_TRACE_DELAY;
6002 if (rest >= 0 && rest < XT_TRACE_DELAY)
6003 msec = rest;
6004 if (rest >= 0)
6005 rest -= msec;
6006 }
6007# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006008# ifdef FEAT_SOUND_CANBERRA
6009 // Invoke any pending sound callbacks.
6010 if (has_sound_callback_in_queue())
6011 invoke_sound_callback();
6012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013# ifdef FEAT_MOUSE_GPM
6014 gpm_process_wanted = 0;
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006015 avail = RealWaitForChar(read_cmd_fd, msec,
Bram Moolenaarcda77642016-06-04 13:32:35 +02006016 &gpm_process_wanted, interrupted);
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006017 if (!avail && !gpm_process_wanted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018# else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006019 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (!avail)
Bram Moolenaarb5432d82019-08-30 19:28:25 +02006021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02006023 if (!ignore_input && input_available())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 return 1;
6025# ifdef FEAT_XCLIPBOARD
6026 if (rest == 0 || !do_xterm_trace())
6027# endif
6028 break;
6029 }
6030 }
6031 while (FALSE
6032# ifdef FEAT_MOUSE_GPM
6033 || (gpm_process_wanted && mch_gpm_process() == 0)
6034# endif
6035# ifdef FEAT_XCLIPBOARD
6036 || (!avail && rest != 0)
6037# endif
Bram Moolenaar8fdd7212016-03-26 19:41:48 +01006038 )
6039 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040
6041#else
Bram Moolenaarcda77642016-06-04 13:32:35 +02006042 avail = RealWaitForChar(read_cmd_fd, msec, NULL, interrupted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#endif
6044 return avail;
6045}
6046
Bram Moolenaar4ffa0702013-12-11 17:12:37 +01006047#ifndef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048/*
6049 * Wait "msec" msec until a character is available from file descriptor "fd".
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006050 * "msec" == 0 will check for characters once.
6051 * "msec" == -1 will block until a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 * When a GUI is being used, this will not be used for input -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * Or when a Linux GPM mouse event is waiting.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006054 * Or when a clientserver message is on the queue.
Bram Moolenaarcda77642016-06-04 13:32:35 +02006055 * "interrupted" (if not NULL) is set to TRUE when no character is available
6056 * but something else needs to be done.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#if defined(__BEOS__)
6059 int
6060#else
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006061 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006063RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 int ret;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006066 int result;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006067#if defined(FEAT_XCLIPBOARD) || defined(USE_XSMP) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 static int busy = FALSE;
6069
Bram Moolenaar0f873732019-12-05 20:28:46 +01006070 // May retry getting characters after an event was handled.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071# define MAY_LOOP
6072
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006073# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006074 // Remember at what time we started, so that we know how much longer we
6075 // should wait after being interrupted.
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01006076 long start_msec = msec;
6077 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078
Bram Moolenaar76b6dfe2016-06-04 14:37:22 +02006079 if (msec > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006080 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# endif
6082
Bram Moolenaar0f873732019-12-05 20:28:46 +01006083 // Handle being called recursively. This may happen for the session
6084 // manager stuff, it may save the file, which does a breakcheck.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 if (busy)
6086 return 0;
6087#endif
6088
6089#ifdef MAY_LOOP
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00006090 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091#endif
6092 {
6093#ifdef MAY_LOOP
Bram Moolenaar0f873732019-12-05 20:28:46 +01006094 int finished = TRUE; // default is to 'loop' just once
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006095# ifdef FEAT_MZSCHEME
6096 int mzquantum_used = FALSE;
6097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#endif
6099#ifndef HAVE_SELECT
Bram Moolenaar0f873732019-12-05 20:28:46 +01006100 // each channel may use in, out and err
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006101 struct pollfd fds[6 + 3 * MAX_OPEN_CHANNELS];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 int nfd;
6103# ifdef FEAT_XCLIPBOARD
6104 int xterm_idx = -1;
6105# endif
6106# ifdef FEAT_MOUSE_GPM
6107 int gpm_idx = -1;
6108# endif
6109# ifdef USE_XSMP
6110 int xsmp_idx = -1;
6111# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006112 int towait = (int)msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006114# ifdef FEAT_MZSCHEME
6115 mzvim_check_threads();
6116 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6117 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006118 towait = (int)p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006119 mzquantum_used = TRUE;
6120 }
6121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 fds[0].fd = fd;
6123 fds[0].events = POLLIN;
6124 nfd = 1;
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006127 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (xterm_Shell != (Widget)0)
6129 {
6130 xterm_idx = nfd;
6131 fds[nfd].fd = ConnectionNumber(xterm_dpy);
6132 fds[nfd].events = POLLIN;
6133 nfd++;
6134 }
6135# endif
6136# ifdef FEAT_MOUSE_GPM
6137 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6138 {
6139 gpm_idx = nfd;
6140 fds[nfd].fd = gpm_fd;
6141 fds[nfd].events = POLLIN;
6142 nfd++;
6143 }
6144# endif
6145# ifdef USE_XSMP
6146 if (xsmp_icefd != -1)
6147 {
6148 xsmp_idx = nfd;
6149 fds[nfd].fd = xsmp_icefd;
6150 fds[nfd].events = POLLIN;
6151 nfd++;
6152 }
6153# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006154#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006155 nfd = channel_poll_setup(nfd, &fds, &towait);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006156#endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006157 if (interrupted != NULL)
6158 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006160 ret = poll(fds, nfd, towait);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006161
6162 result = ret > 0 && (fds[0].revents & POLLIN);
Bram Moolenaarcda77642016-06-04 13:32:35 +02006163 if (result == 0 && interrupted != NULL && ret > 0)
6164 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006165
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006166# ifdef FEAT_MZSCHEME
6167 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006168 // MzThreads scheduling is required and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006169 finished = FALSE;
6170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172# ifdef FEAT_XCLIPBOARD
6173 if (xterm_Shell != (Widget)0 && (fds[xterm_idx].revents & POLLIN))
6174 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006175 xterm_update(); // Maybe we should hand out clipboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (--ret == 0 && !input_available())
Bram Moolenaar0f873732019-12-05 20:28:46 +01006177 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 finished = FALSE;
6179 }
6180# endif
6181# ifdef FEAT_MOUSE_GPM
6182 if (gpm_idx >= 0 && (fds[gpm_idx].revents & POLLIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 *check_for_gpm = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184# endif
6185# ifdef USE_XSMP
6186 if (xsmp_idx >= 0 && (fds[xsmp_idx].revents & (POLLIN | POLLHUP)))
6187 {
6188 if (fds[xsmp_idx].revents & POLLIN)
6189 {
6190 busy = TRUE;
6191 xsmp_handle_requests();
6192 busy = FALSE;
6193 }
6194 else if (fds[xsmp_idx].revents & POLLHUP)
6195 {
6196 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006197 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 xsmp_close();
6199 }
6200 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006201 finished = FALSE; // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006204#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006205 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006206 if (ret >= 0)
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006207 channel_poll_check(ret, &fds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
Bram Moolenaar0f873732019-12-05 20:28:46 +01006210#else // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 struct timeval tv;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006213 struct timeval *tvp;
Bram Moolenaar61fb8d82018-11-12 21:45:08 +01006214 // These are static because they can take 8 Kbyte each and cause the
6215 // signal stack to run out with -O3.
6216 static fd_set rfds, wfds, efds;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 int maxfd;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006218 long towait = msec;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006220# ifdef FEAT_MZSCHEME
6221 mzvim_check_threads();
6222 if (mzthreads_allowed() && p_mzq > 0 && (msec < 0 || msec > p_mzq))
6223 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006224 towait = p_mzq; // don't wait longer than 'mzquantum'
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006225 mzquantum_used = TRUE;
6226 }
6227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006229 if (towait >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006231 tv.tv_sec = towait / 1000;
6232 tv.tv_usec = (towait % 1000) * (1000000/1000);
6233 tvp = &tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006235 else
6236 tvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238 /*
6239 * Select on ready for reading and exceptional condition (end of file).
6240 */
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006241select_eintr:
6242 FD_ZERO(&rfds);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006243 FD_ZERO(&wfds);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 FD_ZERO(&efds);
6245 FD_SET(fd, &rfds);
6246# if !defined(__QNX__) && !defined(__CYGWIN32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006247 // For QNX select() always returns 1 if this is set. Why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 FD_SET(fd, &efds);
6249# endif
6250 maxfd = fd;
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252# ifdef FEAT_XCLIPBOARD
Bram Moolenaarb1e26502014-11-19 18:48:46 +01006253 may_restore_clipboard();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (xterm_Shell != (Widget)0)
6255 {
6256 FD_SET(ConnectionNumber(xterm_dpy), &rfds);
6257 if (maxfd < ConnectionNumber(xterm_dpy))
6258 maxfd = ConnectionNumber(xterm_dpy);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006259
Bram Moolenaar0f873732019-12-05 20:28:46 +01006260 // An event may have already been read but not handled. In
6261 // particularly, XFlush may cause this.
Bram Moolenaardd82d692012-08-15 17:26:57 +02006262 xterm_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
6264# endif
6265# ifdef FEAT_MOUSE_GPM
6266 if (check_for_gpm != NULL && gpm_flag && gpm_fd >= 0)
6267 {
6268 FD_SET(gpm_fd, &rfds);
6269 FD_SET(gpm_fd, &efds);
6270 if (maxfd < gpm_fd)
6271 maxfd = gpm_fd;
6272 }
6273# endif
6274# ifdef USE_XSMP
6275 if (xsmp_icefd != -1)
6276 {
6277 FD_SET(xsmp_icefd, &rfds);
6278 FD_SET(xsmp_icefd, &efds);
6279 if (maxfd < xsmp_icefd)
6280 maxfd = xsmp_icefd;
6281 }
6282# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006283# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf3360612017-10-01 16:21:31 +02006284 maxfd = channel_select_setup(maxfd, &rfds, &wfds, &tv, &tvp);
Bram Moolenaardd82d692012-08-15 17:26:57 +02006285# endif
Bram Moolenaarcda77642016-06-04 13:32:35 +02006286 if (interrupted != NULL)
6287 *interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288
Bram Moolenaar643b6142018-09-12 20:29:09 +02006289 ret = select(maxfd + 1, SELECT_TYPE_ARG234 &rfds,
6290 SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 &efds, tvp);
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006291 result = ret > 0 && FD_ISSET(fd, &rfds);
6292 if (result)
6293 --ret;
Bram Moolenaarcda77642016-06-04 13:32:35 +02006294 else if (interrupted != NULL && ret > 0)
6295 *interrupted = TRUE;
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006296
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006297# ifdef EINTR
6298 if (ret == -1 && errno == EINTR)
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006299 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006300 // Check whether window has been resized, EINTR may be caused by
6301 // SIGWINCH.
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006302 if (do_resize)
6303 handle_resize();
6304
Bram Moolenaar0f873732019-12-05 20:28:46 +01006305 // Interrupted by a signal, need to try again. We ignore msec
6306 // here, because we do want to check even after a timeout if
6307 // characters are available. Needed for reading output of an
6308 // external command after the process has finished.
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006309 goto select_eintr;
Bram Moolenaar2e7b1df2011-10-12 21:04:20 +02006310 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006311# endif
Bram Moolenaar311d9822007-02-27 15:48:28 +00006312# ifdef __TANDEM
6313 if (ret == -1 && errno == ENOTSUP)
6314 {
6315 FD_ZERO(&rfds);
6316 FD_ZERO(&efds);
6317 ret = 0;
6318 }
Bram Moolenaar493c7a82011-09-07 14:06:47 +02006319# endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006320# ifdef FEAT_MZSCHEME
6321 if (ret == 0 && mzquantum_used)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006322 // loop if MzThreads must be scheduled and timeout occurred
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006323 finished = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324# endif
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326# ifdef FEAT_XCLIPBOARD
6327 if (ret > 0 && xterm_Shell != (Widget)0
6328 && FD_ISSET(ConnectionNumber(xterm_dpy), &rfds))
6329 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006330 xterm_update(); // Maybe we should hand out clipboard
6331 // continue looping when we only got the X event and the input
6332 // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (--ret == 0 && !input_available())
6334 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006335 // Try again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 finished = FALSE;
6337 }
6338 }
6339# endif
6340# ifdef FEAT_MOUSE_GPM
6341 if (ret > 0 && gpm_flag && check_for_gpm != NULL && gpm_fd >= 0)
6342 {
6343 if (FD_ISSET(gpm_fd, &efds))
6344 gpm_close();
6345 else if (FD_ISSET(gpm_fd, &rfds))
6346 *check_for_gpm = 1;
6347 }
6348# endif
6349# ifdef USE_XSMP
6350 if (ret > 0 && xsmp_icefd != -1)
6351 {
6352 if (FD_ISSET(xsmp_icefd, &efds))
6353 {
6354 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006355 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 xsmp_close();
6357 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006358 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360 else if (FD_ISSET(xsmp_icefd, &rfds))
6361 {
6362 busy = TRUE;
6363 xsmp_handle_requests();
6364 busy = FALSE;
6365 if (--ret == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006366 finished = FALSE; // keep going if event was only one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 }
6368 }
6369# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006370#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +01006371 // also call when ret == 0, we may be polling a keep-open channel
Bram Moolenaarf3360612017-10-01 16:21:31 +02006372 if (ret >= 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006373 ret = channel_select_check(ret, &rfds, &wfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +02006374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
Bram Moolenaar0f873732019-12-05 20:28:46 +01006376#endif // HAVE_SELECT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378#ifdef MAY_LOOP
6379 if (finished || msec == 0)
6380 break;
6381
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006382# ifdef FEAT_CLIENTSERVER
6383 if (server_waiting())
6384 break;
6385# endif
6386
Bram Moolenaar0f873732019-12-05 20:28:46 +01006387 // We're going to loop around again, find out for how long
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (msec > 0)
6389 {
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006390# ifdef ELAPSED_FUNC
Bram Moolenaar0f873732019-12-05 20:28:46 +01006391 // Compute remaining wait time.
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006392 msec = start_msec - ELAPSED_FUNC(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006394 // Guess we got interrupted halfway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 msec = msec / 2;
6396# endif
6397 if (msec <= 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006398 break; // waited long enough
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400#endif
6401 }
6402
Bram Moolenaarec70bdd2016-02-18 22:17:42 +01006403 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404}
6405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406/*
Bram Moolenaar02743632005-07-25 20:42:36 +00006407 * Expand a path into all matching files and/or directories. Handles "*",
6408 * "?", "[a-z]", "**", etc.
6409 * "path" has backslashes before chars that are not to be expanded.
6410 * Returns the number of matches found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 */
6412 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006413mch_expandpath(
6414 garray_T *gap,
6415 char_u *path,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006416 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
Bram Moolenaar02743632005-07-25 20:42:36 +00006418 return unix_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421/*
6422 * mch_expand_wildcards() - this code does wild-card pattern matching using
6423 * the shell
6424 *
6425 * return OK for success, FAIL for error (you may lose some memory) and put
6426 * an error message in *file.
6427 *
6428 * num_pat is number of input patterns
6429 * pat is array of pointers to input patterns
6430 * num_file is pointer to number of matched file names
6431 * file is pointer to array of pointers to matched file names
6432 */
6433
6434#ifndef SEEK_SET
6435# define SEEK_SET 0
6436#endif
6437#ifndef SEEK_END
6438# define SEEK_END 2
6439#endif
6440
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006441#define SHELL_SPECIAL (char_u *)"\t \"&'$;<>()\\|"
Bram Moolenaar316059c2006-01-14 21:18:42 +00006442
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006444mch_expand_wildcards(
6445 int num_pat,
6446 char_u **pat,
6447 int *num_file,
6448 char_u ***file,
Bram Moolenaar0f873732019-12-05 20:28:46 +01006449 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 int i;
6452 size_t len;
Bram Moolenaar85325f82017-03-30 21:18:45 +02006453 long llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 char_u *p;
6455 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
Bram Moolenaarc7247912008-01-13 12:54:11 +00006457 /*
6458 * This is the non-OS/2 implementation (really Unix).
6459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 int j;
6461 char_u *tempname;
6462 char_u *command;
6463 FILE *fd;
6464 char_u *buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006465#define STYLE_ECHO 0 // use "echo", the default
6466#define STYLE_GLOB 1 // use "glob", for csh
6467#define STYLE_VIMGLOB 2 // use "vimglob", for Posix sh
6468#define STYLE_PRINT 3 // use "print -N", for zsh
6469#define STYLE_BT 4 // `cmd` expansion, execute the pattern
6470 // directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int shell_style = STYLE_ECHO;
6472 int check_spaces;
6473 static int did_find_nul = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006474 int ampersand = FALSE;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006475 // vimglob() function to define for Posix shell
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006476 static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo \"$1\"; shift; done }; vimglob >";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
Bram Moolenaar0f873732019-12-05 20:28:46 +01006478 *num_file = 0; // default: no files found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 *file = NULL;
6480
6481 /*
6482 * If there are no wildcards, just copy the names to allocated memory.
6483 * Saves a lot of time, because we don't have to start a new shell.
6484 */
6485 if (!have_wildcard(num_pat, pat))
6486 return save_patterns(num_pat, pat, num_file, file);
6487
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006488# ifdef HAVE_SANDBOX
Bram Moolenaar0f873732019-12-05 20:28:46 +01006489 // Don't allow any shell command in the sandbox.
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006490 if (sandbox != 0 && check_secure())
6491 return FAIL;
6492# endif
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 /*
6495 * Don't allow the use of backticks in secure and restricted mode.
6496 */
Bram Moolenaar0e634da2005-07-20 21:57:28 +00006497 if (secure || restricted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 for (i = 0; i < num_pat; ++i)
6499 if (vim_strchr(pat[i], '`') != NULL
6500 && (check_restricted() || check_secure()))
6501 return FAIL;
6502
6503 /*
6504 * get a name for the temp file
6505 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006506 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006508 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 return FAIL;
6510 }
6511
6512 /*
6513 * Let the shell expand the patterns and write the result into the temp
Bram Moolenaarc7247912008-01-13 12:54:11 +00006514 * file.
6515 * STYLE_BT: NL separated
6516 * If expanding `cmd` execute it directly.
6517 * STYLE_GLOB: NUL separated
6518 * If we use *csh, "glob" will work better than "echo".
6519 * STYLE_PRINT: NL or NUL separated
6520 * If we use *zsh, "print -N" will work better than "glob".
6521 * STYLE_VIMGLOB: NL separated
6522 * If we use *sh*, we define "vimglob()".
6523 * STYLE_ECHO: space separated.
6524 * A shell we don't know, stay safe and use "echo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 */
6526 if (num_pat == 1 && *pat[0] == '`'
6527 && (len = STRLEN(pat[0])) > 2
6528 && *(pat[0] + len - 1) == '`')
6529 shell_style = STYLE_BT;
6530 else if ((len = STRLEN(p_sh)) >= 3)
6531 {
6532 if (STRCMP(p_sh + len - 3, "csh") == 0)
6533 shell_style = STYLE_GLOB;
6534 else if (STRCMP(p_sh + len - 3, "zsh") == 0)
6535 shell_style = STYLE_PRINT;
6536 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006537 if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
6538 "sh") != NULL)
6539 shell_style = STYLE_VIMGLOB;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
Bram Moolenaar0f873732019-12-05 20:28:46 +01006541 // Compute the length of the command. We need 2 extra bytes: for the
6542 // optional '&' and for the NUL.
6543 // Worst case: "unset nonomatch; print -N >" plus two is 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 len = STRLEN(tempname) + 29;
Bram Moolenaarc7247912008-01-13 12:54:11 +00006545 if (shell_style == STYLE_VIMGLOB)
6546 len += STRLEN(sh_vimglob_func);
6547
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006548 for (i = 0; i < num_pat; ++i)
6549 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006550 // Count the length of the patterns in the same way as they are put in
6551 // "command" below.
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006552#ifdef USE_SYSTEM
Bram Moolenaar0f873732019-12-05 20:28:46 +01006553 len += STRLEN(pat[i]) + 3; // add space and two quotes
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006554#else
Bram Moolenaar0f873732019-12-05 20:28:46 +01006555 ++len; // add space
Bram Moolenaar316059c2006-01-14 21:18:42 +00006556 for (j = 0; pat[i][j] != NUL; ++j)
6557 {
6558 if (vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006559 ++len; // may add a backslash
Bram Moolenaar316059c2006-01-14 21:18:42 +00006560 ++len;
6561 }
Bram Moolenaarb23c3382005-01-31 19:09:12 +00006562#endif
6563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 command = alloc(len);
6565 if (command == NULL)
6566 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006567 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 vim_free(tempname);
6569 return FAIL;
6570 }
6571
6572 /*
6573 * Build the shell command:
6574 * - Set $nonomatch depending on EW_NOTFOUND (hopefully the shell
6575 * recognizes this).
6576 * - Add the shell command to print the expanded names.
6577 * - Add the temp file name.
6578 * - Add the file name patterns.
6579 */
6580 if (shell_style == STYLE_BT)
6581 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006582 // change `command; command& ` to (command; command )
Bram Moolenaar316059c2006-01-14 21:18:42 +00006583 STRCPY(command, "(");
Bram Moolenaar0f873732019-12-05 20:28:46 +01006584 STRCAT(command, pat[0] + 1); // exclude first backtick
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 p = command + STRLEN(command) - 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006586 *p-- = ')'; // remove last backtick
Bram Moolenaar1c465442017-03-12 20:10:05 +01006587 while (p > command && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 --p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006589 if (*p == '&') // remove trailing '&'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
Bram Moolenaarbdace832019-03-02 10:13:42 +01006591 ampersand = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 *p = ' ';
6593 }
6594 STRCAT(command, ">");
6595 }
6596 else
6597 {
6598 if (flags & EW_NOTFOUND)
6599 STRCPY(command, "set nonomatch; ");
6600 else
6601 STRCPY(command, "unset nonomatch; ");
6602 if (shell_style == STYLE_GLOB)
6603 STRCAT(command, "glob >");
6604 else if (shell_style == STYLE_PRINT)
6605 STRCAT(command, "print -N >");
Bram Moolenaarc7247912008-01-13 12:54:11 +00006606 else if (shell_style == STYLE_VIMGLOB)
6607 STRCAT(command, sh_vimglob_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 else
6609 STRCAT(command, "echo >");
6610 }
Bram Moolenaarc7247912008-01-13 12:54:11 +00006611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 STRCAT(command, tempname);
Bram Moolenaarc7247912008-01-13 12:54:11 +00006613
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if (shell_style != STYLE_BT)
6615 for (i = 0; i < num_pat; ++i)
6616 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006617 // When using system() always add extra quotes, because the shell
6618 // is started twice. Otherwise put a backslash before special
6619 // characters, except inside ``.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620#ifdef USE_SYSTEM
6621 STRCAT(command, " \"");
6622 STRCAT(command, pat[i]);
6623 STRCAT(command, "\"");
6624#else
Bram Moolenaar582fd852005-03-28 20:58:01 +00006625 int intick = FALSE;
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 p = command + STRLEN(command);
6628 *p++ = ' ';
Bram Moolenaar316059c2006-01-14 21:18:42 +00006629 for (j = 0; pat[i][j] != NUL; ++j)
Bram Moolenaar582fd852005-03-28 20:58:01 +00006630 {
6631 if (pat[i][j] == '`')
Bram Moolenaar582fd852005-03-28 20:58:01 +00006632 intick = !intick;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006633 else if (pat[i][j] == '\\' && pat[i][j + 1] != NUL)
6634 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006635 // Remove a backslash, take char literally. But keep
6636 // backslash inside backticks, before a special character
6637 // and before a backtick.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006638 if (intick
Bram Moolenaar49315f62006-02-04 00:54:59 +00006639 || vim_strchr(SHELL_SPECIAL, pat[i][j + 1]) != NULL
6640 || pat[i][j + 1] == '`')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006641 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006642 ++j;
Bram Moolenaar316059c2006-01-14 21:18:42 +00006643 }
Bram Moolenaare4df1642014-08-29 12:58:44 +02006644 else if (!intick
6645 && ((flags & EW_KEEPDOLLAR) == 0 || pat[i][j] != '$')
6646 && vim_strchr(SHELL_SPECIAL, pat[i][j]) != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006647 // Put a backslash before a special character, but not
6648 // when inside ``. And not for $var when EW_KEEPDOLLAR is
6649 // set.
Bram Moolenaar316059c2006-01-14 21:18:42 +00006650 *p++ = '\\';
Bram Moolenaar280f1262006-01-30 00:14:18 +00006651
Bram Moolenaar0f873732019-12-05 20:28:46 +01006652 // Copy one character.
Bram Moolenaar280f1262006-01-30 00:14:18 +00006653 *p++ = pat[i][j];
Bram Moolenaar582fd852005-03-28 20:58:01 +00006654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 *p = NUL;
6656#endif
6657 }
6658 if (flags & EW_SILENT)
6659 show_shell_mess = FALSE;
Bram Moolenaarbdace832019-03-02 10:13:42 +01006660 if (ampersand)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006661 STRCAT(command, "&"); // put the '&' after the redirection
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 /*
6664 * Using zsh -G: If a pattern has no matches, it is just deleted from
6665 * the argument list, otherwise zsh gives an error message and doesn't
6666 * expand any other pattern.
6667 */
6668 if (shell_style == STYLE_PRINT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006669 extra_shell_arg = (char_u *)"-G"; // Use zsh NULL_GLOB option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671 /*
6672 * If we use -f then shell variables set in .cshrc won't get expanded.
6673 * vi can do it, so we will too, but it is only necessary if there is a "$"
6674 * in one of the patterns, otherwise we can still use the fast option.
6675 */
6676 else if (shell_style == STYLE_GLOB && !have_dollars(num_pat, pat))
Bram Moolenaar0f873732019-12-05 20:28:46 +01006677 extra_shell_arg = (char_u *)"-f"; // Use csh fast option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 /*
6680 * execute the shell command
6681 */
6682 i = call_shell(command, SHELL_EXPAND | SHELL_SILENT);
6683
Bram Moolenaar0f873732019-12-05 20:28:46 +01006684 // When running in the background, give it some time to create the temp
6685 // file, but don't wait for it to finish.
Bram Moolenaarbdace832019-03-02 10:13:42 +01006686 if (ampersand)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 mch_delay(10L, TRUE);
6688
Bram Moolenaar0f873732019-12-05 20:28:46 +01006689 extra_shell_arg = NULL; // cleanup
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 show_shell_mess = TRUE;
6691 vim_free(command);
6692
Bram Moolenaar0f873732019-12-05 20:28:46 +01006693 if (i != 0) // mch_call_shell() failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
6695 mch_remove(tempname);
6696 vim_free(tempname);
6697 /*
6698 * With interactive completion, the error message is not printed.
6699 * However with USE_SYSTEM, I don't know how to turn off error messages
6700 * from the shell, so screen may still get messed up -- webb.
6701 */
6702#ifndef USE_SYSTEM
6703 if (!(flags & EW_SILENT))
6704#endif
6705 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006706 redraw_later_clear(); // probably messed up screen
6707 msg_putchar('\n'); // clear bottom line quickly
6708 cmdline_row = Rows - 1; // continue on last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709#ifdef USE_SYSTEM
6710 if (!(flags & EW_SILENT))
6711#endif
6712 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006713 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006714 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006717 // If a `cmd` expansion failed, don't list `cmd` as a match, even when
6718 // EW_NOTFOUND is given
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (shell_style == STYLE_BT)
6720 return FAIL;
6721 goto notfound;
6722 }
6723
6724 /*
6725 * read the names from the file into memory
6726 */
6727 fd = fopen((char *)tempname, READBIN);
6728 if (fd == NULL)
6729 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006730 // Something went wrong, perhaps a file name with a special char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (!(flags & EW_SILENT))
6732 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006733 msg(_(e_wildexpand));
Bram Moolenaar0f873732019-12-05 20:28:46 +01006734 msg_start(); // don't overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
6736 vim_free(tempname);
6737 goto notfound;
6738 }
6739 fseek(fd, 0L, SEEK_END);
Bram Moolenaar0f873732019-12-05 20:28:46 +01006740 llen = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 fseek(fd, 0L, SEEK_SET);
Bram Moolenaar85325f82017-03-30 21:18:45 +02006742 if (llen < 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006743 // just in case ftell() would fail
Bram Moolenaar85325f82017-03-30 21:18:45 +02006744 buffer = NULL;
6745 else
6746 buffer = alloc(llen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (buffer == NULL)
6748 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006749 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 mch_remove(tempname);
6751 vim_free(tempname);
6752 fclose(fd);
6753 return FAIL;
6754 }
Bram Moolenaar85325f82017-03-30 21:18:45 +02006755 len = llen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 i = fread((char *)buffer, 1, len, fd);
6757 fclose(fd);
6758 mch_remove(tempname);
Bram Moolenaar78a15312009-05-15 19:33:18 +00006759 if (i != (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006761 // unexpected read error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006762 semsg(_(e_notread), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 vim_free(tempname);
6764 vim_free(buffer);
6765 return FAIL;
6766 }
6767 vim_free(tempname);
6768
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006769# ifdef __CYGWIN__
Bram Moolenaar0f873732019-12-05 20:28:46 +01006770 // Translate <CR><NL> into <NL>. Caution, buffer may contain NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 p = buffer;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02006772 for (i = 0; i < (int)len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (!(buffer[i] == CAR && buffer[i + 1] == NL))
6774 *p++ = buffer[i];
6775 len = p - buffer;
6776# endif
6777
6778
Bram Moolenaar0f873732019-12-05 20:28:46 +01006779 // file names are separated with Space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (shell_style == STYLE_ECHO)
6781 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006782 buffer[len] = '\n'; // make sure the buffer ends in NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006784 for (i = 0; *p != '\n'; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {
6786 while (*p != ' ' && *p != '\n')
6787 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006788 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006791 // file names are separated with NL
Bram Moolenaarc7247912008-01-13 12:54:11 +00006792 else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006794 buffer[len] = NUL; // make sure the buffer ends in NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 p = buffer;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006796 for (i = 0; *p != NUL; ++i) // count number of entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
6798 while (*p != '\n' && *p != NUL)
6799 ++p;
6800 if (*p != NUL)
6801 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006802 p = skipwhite(p); // skip leading white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006805 // file names are separated with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 else
6807 {
6808 /*
6809 * Some versions of zsh use spaces instead of NULs to separate
6810 * results. Only do this when there is no NUL before the end of the
6811 * buffer, otherwise we would never be able to use file names with
6812 * embedded spaces when zsh does use NULs.
6813 * When we found a NUL once, we know zsh is OK, set did_find_nul and
6814 * don't check for spaces again.
6815 */
6816 check_spaces = FALSE;
6817 if (shell_style == STYLE_PRINT && !did_find_nul)
6818 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006819 // If there is a NUL, set did_find_nul, else set check_spaces
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006820 buffer[len] = NUL;
Bram Moolenaarb011af92013-12-11 13:21:51 +01006821 if (len && (int)STRLEN(buffer) < (int)len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 did_find_nul = TRUE;
6823 else
6824 check_spaces = TRUE;
6825 }
6826
6827 /*
6828 * Make sure the buffer ends with a NUL. For STYLE_PRINT there
6829 * already is one, for STYLE_GLOB it needs to be added.
6830 */
6831 if (len && buffer[len - 1] == NUL)
6832 --len;
6833 else
6834 buffer[len] = NUL;
6835 i = 0;
6836 for (p = buffer; p < buffer + len; ++p)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006837 if (*p == NUL || (*p == ' ' && check_spaces)) // count entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {
6839 ++i;
6840 *p = NUL;
6841 }
6842 if (len)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006843 ++i; // count last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 }
6845 if (i == 0)
6846 {
6847 /*
6848 * Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
6849 * /bin/sh will happily expand it to nothing rather than returning an
6850 * error; and hey, it's good to check anyway -- webb.
6851 */
6852 vim_free(buffer);
6853 goto notfound;
6854 }
6855 *num_file = i;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006856 *file = ALLOC_MULT(char_u *, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (*file == NULL)
6858 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006859 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 vim_free(buffer);
6861 return FAIL;
6862 }
6863
6864 /*
6865 * Isolate the individual file names.
6866 */
6867 p = buffer;
6868 for (i = 0; i < *num_file; ++i)
6869 {
6870 (*file)[i] = p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006871 // Space or NL separates
Bram Moolenaarc7247912008-01-13 12:54:11 +00006872 if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
6873 || shell_style == STYLE_VIMGLOB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {
Bram Moolenaar49315f62006-02-04 00:54:59 +00006875 while (!(shell_style == STYLE_ECHO && *p == ' ')
6876 && *p != '\n' && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006878 if (p == buffer + len) // last entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 *p = NUL;
6880 else
6881 {
6882 *p++ = NUL;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006883 p = skipwhite(p); // skip to next entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01006886 else // NUL separates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006888 while (*p && p < buffer + len) // skip entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 ++p;
Bram Moolenaar0f873732019-12-05 20:28:46 +01006890 ++p; // skip NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 }
6893
6894 /*
6895 * Move the file names to allocated memory.
6896 */
6897 for (j = 0, i = 0; i < *num_file; ++i)
6898 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01006899 // Require the files to exist. Helps when using /bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
6901 continue;
6902
Bram Moolenaar0f873732019-12-05 20:28:46 +01006903 // check if this entry should be included
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 dir = (mch_isdir((*file)[i]));
6905 if ((dir && !(flags & EW_DIR)) || (!dir && !(flags & EW_FILE)))
6906 continue;
6907
Bram Moolenaar0f873732019-12-05 20:28:46 +01006908 // Skip files that are not executable if we check for that.
Bram Moolenaarb5971142015-03-21 17:32:19 +01006909 if (!dir && (flags & EW_EXEC)
6910 && !mch_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaara2031822006-03-07 22:29:51 +00006911 continue;
6912
Bram Moolenaar964b3742019-05-24 18:54:09 +02006913 p = alloc(STRLEN((*file)[i]) + 1 + dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (p)
6915 {
6916 STRCPY(p, (*file)[i]);
6917 if (dir)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006918 add_pathsep(p); // add '/' to a directory name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 (*file)[j++] = p;
6920 }
6921 }
6922 vim_free(buffer);
6923 *num_file = j;
6924
Bram Moolenaar0f873732019-12-05 20:28:46 +01006925 if (*num_file == 0) // rejected all entries
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006927 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 goto notfound;
6929 }
6930
6931 return OK;
6932
6933notfound:
6934 if (flags & EW_NOTFOUND)
6935 return save_patterns(num_pat, pat, num_file, file);
6936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937}
6938
Bram Moolenaar0f873732019-12-05 20:28:46 +01006939#endif // VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006942save_patterns(
6943 int num_pat,
6944 char_u **pat,
6945 int *num_file,
6946 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 int i;
Bram Moolenaard8b02732005-01-14 21:48:43 +00006949 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006951 *file = ALLOC_MULT(char_u *, num_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (*file == NULL)
6953 return FAIL;
6954 for (i = 0; i < num_pat; i++)
Bram Moolenaard8b02732005-01-14 21:48:43 +00006955 {
6956 s = vim_strsave(pat[i]);
6957 if (s != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01006958 // Be compatible with expand_filename(): halve the number of
6959 // backslashes.
Bram Moolenaard8b02732005-01-14 21:48:43 +00006960 backslash_halve(s);
6961 (*file)[i] = s;
6962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 *num_file = num_pat;
6964 return OK;
6965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967/*
6968 * Return TRUE if the string "p" contains a wildcard that mch_expandpath() can
6969 * expand.
6970 */
6971 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006972mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006974 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (*p == '\\' && p[1] != NUL)
6977 ++p;
6978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (vim_strchr((char_u *)
6980#ifdef VMS
6981 "*?%"
6982#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 "*?[{'"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#endif
6985 , *p) != NULL)
6986 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988 return FALSE;
6989}
6990
6991/*
6992 * Return TRUE if the string "p" contains a wildcard.
6993 * Don't recognize '~' at the end as a wildcard.
6994 */
6995 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006996mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006998 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (*p == '\\' && p[1] != NUL)
7001 ++p;
7002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (vim_strchr((char_u *)
7004#ifdef VMS
7005 "*?%$"
7006#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 "*?[{`'$"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#endif
7009 , *p) != NULL
7010 || (*p == '~' && p[1] != NUL))
7011 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
7013 return FALSE;
7014}
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007017have_wildcard(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 int i;
7020
7021 for (i = 0; i < num; i++)
7022 if (mch_has_wildcard(file[i]))
7023 return 1;
7024 return 0;
7025}
7026
7027 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007028have_dollars(int num, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 int i;
7031
7032 for (i = 0; i < num; i++)
7033 if (vim_strchr(file[i], '$') != NULL)
7034 return TRUE;
7035 return FALSE;
7036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007038#if !defined(HAVE_RENAME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039/*
7040 * Scaled-down version of rename(), which is missing in Xenix.
7041 * This version can only move regular files and will fail if the
7042 * destination exists.
7043 */
7044 int
Bram Moolenaarfdcc9af2016-02-29 12:52:39 +01007045mch_rename(const char *src, const char *dest)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 struct stat st;
7048
Bram Moolenaar0f873732019-12-05 20:28:46 +01007049 if (stat(dest, &st) >= 0) // fail if destination exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007051 if (link(src, dest) != 0) // link file to new name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 return -1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007053 if (mch_remove(src) == 0) // delete link to old name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 return 0;
7055 return -1;
7056}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007057#endif // !HAVE_RENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007059#if defined(FEAT_MOUSE_GPM) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060/*
7061 * Initializes connection with gpm (if it isn't already opened)
7062 * Return 1 if succeeded (or connection already opened), 0 if failed
7063 */
7064 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007065gpm_open(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007067 static Gpm_Connect gpm_connect; // Must it be kept till closing ?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
7069 if (!gpm_flag)
7070 {
7071 gpm_connect.eventMask = (GPM_UP | GPM_DRAG | GPM_DOWN);
7072 gpm_connect.defaultMask = ~GPM_HARD;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007073 // Default handling for mouse move
7074 gpm_connect.minMod = 0; // Handle any modifier keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 gpm_connect.maxMod = 0xffff;
7076 if (Gpm_Open(&gpm_connect, 0) > 0)
7077 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007078 // gpm library tries to handling TSTP causes
7079 // problems. Anyways, we close connection to Gpm whenever
7080 // we are going to suspend or starting an external process
7081 // so we shouldn't have problem with this
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007082# ifdef SIGTSTP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 signal(SIGTSTP, restricted ? SIG_IGN : SIG_DFL);
Bram Moolenaar76243bd2009-03-02 01:47:02 +00007084# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007085 return 1; // succeed
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 if (gpm_fd == -2)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007088 Gpm_Close(); // We don't want to talk to xterm via gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 return 0;
7090 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007091 return 1; // already open
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092}
7093
7094/*
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007095 * Returns TRUE if the GPM mouse is enabled.
7096 */
7097 int
7098gpm_enabled(void)
7099{
7100 return gpm_flag && gpm_fd >= 0;
7101}
7102
7103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 * Closes connection to gpm
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 */
7106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007107gpm_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007109 if (gpm_enabled())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 Gpm_Close();
7111}
7112
Bram Moolenaarbedf0912019-05-04 16:58:45 +02007113/*
7114 * Reads gpm event and adds special keys to input buf. Returns length of
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 * generated key sequence.
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007116 * This function is styled after gui_send_mouse_event().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
7118 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007119mch_gpm_process(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 int button;
7122 static Gpm_Event gpm_event;
7123 char_u string[6];
7124 int_u vim_modifiers;
7125 int row,col;
7126 unsigned char buttons_mask;
7127 unsigned char gpm_modifiers;
7128 static unsigned char old_buttons = 0;
7129
7130 Gpm_GetEvent(&gpm_event);
7131
7132#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007133 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (hold_gui_events)
7135 return 0;
7136#endif
7137
7138 row = gpm_event.y - 1;
7139 col = gpm_event.x - 1;
7140
Bram Moolenaar0f873732019-12-05 20:28:46 +01007141 string[0] = ESC; // Our termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 string[1] = 'M';
7143 string[2] = 'G';
7144 switch (GPM_BARE_EVENTS(gpm_event.type))
7145 {
7146 case GPM_DRAG:
7147 string[3] = MOUSE_DRAG;
7148 break;
7149 case GPM_DOWN:
7150 buttons_mask = gpm_event.buttons & ~old_buttons;
7151 old_buttons = gpm_event.buttons;
7152 switch (buttons_mask)
7153 {
7154 case GPM_B_LEFT:
7155 button = MOUSE_LEFT;
7156 break;
7157 case GPM_B_MIDDLE:
7158 button = MOUSE_MIDDLE;
7159 break;
7160 case GPM_B_RIGHT:
7161 button = MOUSE_RIGHT;
7162 break;
7163 default:
7164 return 0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007165 // Don't know what to do. Can more than one button be
7166 // reported in one event?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168 string[3] = (char_u)(button | 0x20);
7169 SET_NUM_MOUSE_CLICKS(string[3], gpm_event.clicks + 1);
7170 break;
7171 case GPM_UP:
7172 string[3] = MOUSE_RELEASE;
7173 old_buttons &= ~gpm_event.buttons;
7174 break;
7175 default:
7176 return 0;
7177 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01007178 // This code is based on gui_x11_mouse_cb in gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 gpm_modifiers = gpm_event.modifiers;
7180 vim_modifiers = 0x0;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007181 // I ignore capslock stats. Aren't we all just hate capslock mixing with
7182 // Vim commands ? Besides, gpm_event.modifiers is unsigned char, and
7183 // K_CAPSSHIFT is defined 8, so it probably isn't even reported
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (gpm_modifiers & ((1 << KG_SHIFT) | (1 << KG_SHIFTR) | (1 << KG_SHIFTL)))
7185 vim_modifiers |= MOUSE_SHIFT;
7186
7187 if (gpm_modifiers & ((1 << KG_CTRL) | (1 << KG_CTRLR) | (1 << KG_CTRLL)))
7188 vim_modifiers |= MOUSE_CTRL;
7189 if (gpm_modifiers & ((1 << KG_ALT) | (1 << KG_ALTGR)))
7190 vim_modifiers |= MOUSE_ALT;
7191 string[3] |= vim_modifiers;
7192 string[4] = (char_u)(col + ' ' + 1);
7193 string[5] = (char_u)(row + ' ' + 1);
7194 add_to_input_buf(string, 6);
7195 return 6;
7196}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007197#endif // FEAT_MOUSE_GPM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007199#ifdef FEAT_SYSMOUSE
7200/*
7201 * Initialize connection with sysmouse.
7202 * Let virtual console inform us with SIGUSR2 for pending sysmouse
7203 * output, any sysmouse output than will be processed via sig_sysmouse().
7204 * Return OK if succeeded, FAIL if failed.
7205 */
7206 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007207sysmouse_open(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007208{
7209 struct mouse_info mouse;
7210
7211 mouse.operation = MOUSE_MODE;
7212 mouse.u.mode.mode = 0;
7213 mouse.u.mode.signal = SIGUSR2;
7214 if (ioctl(1, CONS_MOUSECTL, &mouse) != -1)
7215 {
7216 signal(SIGUSR2, (RETSIGTYPE (*)())sig_sysmouse);
7217 mouse.operation = MOUSE_SHOW;
7218 ioctl(1, CONS_MOUSECTL, &mouse);
7219 return OK;
7220 }
7221 return FAIL;
7222}
7223
7224/*
7225 * Stop processing SIGUSR2 signals, and also make sure that
7226 * virtual console do not send us any sysmouse related signal.
7227 */
7228 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007229sysmouse_close(void)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007230{
7231 struct mouse_info mouse;
7232
7233 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7234 mouse.operation = MOUSE_MODE;
7235 mouse.u.mode.mode = 0;
7236 mouse.u.mode.signal = 0;
7237 ioctl(1, CONS_MOUSECTL, &mouse);
7238}
7239
7240/*
7241 * Gets info from sysmouse and adds special keys to input buf.
7242 */
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007243 static RETSIGTYPE
7244sig_sysmouse SIGDEFARG(sigarg)
7245{
7246 struct mouse_info mouse;
7247 struct video_info video;
7248 char_u string[6];
7249 int row, col;
7250 int button;
7251 int buttons;
7252 static int oldbuttons = 0;
7253
7254#ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01007255 // Don't put events in the input queue now.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007256 if (hold_gui_events)
7257 return;
7258#endif
7259
7260 mouse.operation = MOUSE_GETINFO;
7261 if (ioctl(1, FBIO_GETMODE, &video.vi_mode) != -1
7262 && ioctl(1, FBIO_MODEINFO, &video) != -1
7263 && ioctl(1, CONS_MOUSECTL, &mouse) != -1
7264 && video.vi_cheight > 0 && video.vi_cwidth > 0)
7265 {
7266 row = mouse.u.data.y / video.vi_cheight;
7267 col = mouse.u.data.x / video.vi_cwidth;
7268 buttons = mouse.u.data.buttons;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007269 string[0] = ESC; // Our termcode
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007270 string[1] = 'M';
7271 string[2] = 'S';
7272 if (oldbuttons == buttons && buttons != 0)
7273 {
7274 button = MOUSE_DRAG;
7275 }
7276 else
7277 {
7278 switch (buttons)
7279 {
7280 case 0:
7281 button = MOUSE_RELEASE;
7282 break;
7283 case 1:
7284 button = MOUSE_LEFT;
7285 break;
7286 case 2:
7287 button = MOUSE_MIDDLE;
7288 break;
7289 case 4:
7290 button = MOUSE_RIGHT;
7291 break;
7292 default:
7293 return;
7294 }
7295 oldbuttons = buttons;
7296 }
7297 string[3] = (char_u)(button);
7298 string[4] = (char_u)(col + ' ' + 1);
7299 string[5] = (char_u)(row + ' ' + 1);
7300 add_to_input_buf(string, 6);
7301 }
7302 return;
7303}
Bram Moolenaar0f873732019-12-05 20:28:46 +01007304#endif // FEAT_SYSMOUSE
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306#if defined(FEAT_LIBCALL) || defined(PROTO)
Bram Moolenaard99df422016-01-29 23:20:40 +01007307typedef char_u * (*STRPROCSTR)(char_u *);
7308typedef char_u * (*INTPROCSTR)(int);
7309typedef int (*STRPROCINT)(char_u *);
7310typedef int (*INTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312/*
7313 * Call a DLL routine which takes either a string or int param
7314 * and returns an allocated string.
7315 */
7316 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007317mch_libcall(
7318 char_u *libname,
7319 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007320 char_u *argstring, // NULL when using a argint
Bram Moolenaar05540972016-01-30 20:31:25 +01007321 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +01007322 char_u **string_result, // NULL when using number_result
Bram Moolenaar05540972016-01-30 20:31:25 +01007323 int *number_result)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325# if defined(USE_DLOPEN)
7326 void *hinstLib;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007327 char *dlerr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328# else
7329 shl_t hinstLib;
7330# endif
7331 STRPROCSTR ProcAdd;
7332 INTPROCSTR ProcAddI;
7333 char_u *retval_str = NULL;
7334 int retval_int = 0;
7335 int success = FALSE;
7336
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007337 /*
7338 * Get a handle to the DLL module.
7339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007341 // First clear any error, it's not cleared by the dlopen() call.
Bram Moolenaarb39ef122006-06-22 16:19:31 +00007342 (void)dlerror();
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 hinstLib = dlopen((char *)libname, RTLD_LAZY
7345# ifdef RTLD_LOCAL
7346 | RTLD_LOCAL
7347# endif
7348 );
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007349 if (hinstLib == NULL)
7350 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007351 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007352 dlerr = (char *)dlerror();
7353 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007354 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356# else
7357 hinstLib = shl_load((const char*)libname, BIND_IMMEDIATE|BIND_VERBOSE, 0L);
7358# endif
7359
Bram Moolenaar0f873732019-12-05 20:28:46 +01007360 // If the handle is valid, try to get the function address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (hinstLib != NULL)
7362 {
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007363# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 /*
7365 * Catch a crash when calling the library function. For example when
7366 * using a number where a string pointer is expected.
7367 */
7368 mch_startjmp();
7369 if (SETJMP(lc_jump_env) != 0)
7370 {
7371 success = FALSE;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007372# if defined(USE_DLOPEN)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007373 dlerr = NULL;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 mch_didjmp();
7376 }
7377 else
7378# endif
7379 {
7380 retval_str = NULL;
7381 retval_int = 0;
7382
7383 if (argstring != NULL)
7384 {
7385# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007386 *(void **)(&ProcAdd) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007387 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388# else
7389 if (shl_findsym(&hinstLib, (const char *)funcname,
7390 TYPE_PROCEDURE, (void *)&ProcAdd) < 0)
7391 ProcAdd = NULL;
7392# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007393 if ((success = (ProcAdd != NULL
7394# if defined(USE_DLOPEN)
7395 && dlerr == NULL
7396# endif
7397 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 if (string_result == NULL)
7400 retval_int = ((STRPROCINT)ProcAdd)(argstring);
7401 else
7402 retval_str = (ProcAdd)(argstring);
7403 }
7404 }
7405 else
7406 {
7407# if defined(USE_DLOPEN)
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007408 *(void **)(&ProcAddI) = dlsym(hinstLib, (const char *)funcname);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007409 dlerr = (char *)dlerror();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410# else
7411 if (shl_findsym(&hinstLib, (const char *)funcname,
7412 TYPE_PROCEDURE, (void *)&ProcAddI) < 0)
7413 ProcAddI = NULL;
7414# endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007415 if ((success = (ProcAddI != NULL
7416# if defined(USE_DLOPEN)
7417 && dlerr == NULL
7418# endif
7419 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
7421 if (string_result == NULL)
7422 retval_int = ((INTPROCINT)ProcAddI)(argint);
7423 else
7424 retval_str = (ProcAddI)(argint);
7425 }
7426 }
7427
Bram Moolenaar0f873732019-12-05 20:28:46 +01007428 // Save the string before we free the library.
7429 // Assume that a "1" or "-1" result is an illegal pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (string_result == NULL)
7431 *number_result = retval_int;
7432 else if (retval_str != NULL
7433 && retval_str != (char_u *)1
7434 && retval_str != (char_u *)-1)
7435 *string_result = vim_strsave(retval_str);
7436 }
7437
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007438# ifdef USING_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 mch_endjmp();
7440# ifdef SIGHASARG
7441 if (lc_signal != 0)
7442 {
7443 int i;
7444
Bram Moolenaar0f873732019-12-05 20:28:46 +01007445 // try to find the name of this signal
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 for (i = 0; signal_info[i].sig != -1; i++)
7447 if (lc_signal == signal_info[i].sig)
7448 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007449 semsg("E368: got SIG%s in libcall()", signal_info[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451# endif
7452# endif
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454# if defined(USE_DLOPEN)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007455 // "dlerr" must be used before dlclose()
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007456 if (dlerr != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007457 semsg(_("dlerror = \"%s\""), dlerr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007458
Bram Moolenaar0f873732019-12-05 20:28:46 +01007459 // Free the DLL module.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 (void)dlclose(hinstLib);
7461# else
7462 (void)shl_unload(hinstLib);
7463# endif
7464 }
7465
7466 if (!success)
7467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007468 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 return FAIL;
7470 }
7471
7472 return OK;
7473}
7474#endif
7475
7476#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007477static int xterm_trace = -1; // default: disabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478static int xterm_button;
7479
7480/*
7481 * Setup a dummy window for X selections in a terminal.
7482 */
7483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007484setup_term_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 int z = 0;
7487 char *strp = "";
7488 Widget AppShell;
7489
7490 if (!x_connect_to_server())
7491 return;
7492
7493 open_app_context();
7494 if (app_context != NULL && xterm_Shell == (Widget)0)
7495 {
7496 int (*oldhandler)();
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007497# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int (*oldIOhandler)();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007499# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007500# ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01007501 elapsed_T start_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
7503 if (p_verbose > 0)
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007504 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505# endif
7506
Bram Moolenaar0f873732019-12-05 20:28:46 +01007507 // Ignore X errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 oldhandler = XSetErrorHandler(x_error_check);
7509
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007510# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007511 // Ignore X IO errors while opening the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 oldIOhandler = XSetIOErrorHandler(x_IOerror_check);
7513 mch_startjmp();
7514 if (SETJMP(lc_jump_env) != 0)
7515 {
7516 mch_didjmp();
7517 xterm_dpy = NULL;
7518 }
7519 else
Bram Moolenaaredce7422019-01-20 18:39:30 +01007520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {
7522 xterm_dpy = XtOpenDisplay(app_context, xterm_display,
7523 "vim_xterm", "Vim_xterm", NULL, 0, &z, &strp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007524 if (xterm_dpy != NULL)
7525 xterm_dpy_retry_count = 0;
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007526# if defined(USING_SETJMP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 mch_endjmp();
Bram Moolenaaredce7422019-01-20 18:39:30 +01007528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530
Bram Moolenaarb2148f52019-01-20 23:43:57 +01007531# if defined(USING_SETJMP)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007532 // Now handle X IO errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 (void)XSetIOErrorHandler(oldIOhandler);
Bram Moolenaaredce7422019-01-20 18:39:30 +01007534# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01007535 // Now handle X errors normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 (void)XSetErrorHandler(oldhandler);
7537
7538 if (xterm_dpy == NULL)
7539 {
7540 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007541 verb_msg(_("Opening the X display failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 return;
7543 }
7544
Bram Moolenaar0f873732019-12-05 20:28:46 +01007545 // Catch terminating error of the X server connection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 (void)XSetIOErrorHandler(x_IOerror_handler);
7547
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007548# ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007550 {
7551 verbose_enter();
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01007552 xopen_message(ELAPSED_FUNC(start_tv));
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007553 verbose_leave();
7554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555# endif
7556
Bram Moolenaar0f873732019-12-05 20:28:46 +01007557 // Create a Shell to make converters work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 AppShell = XtVaAppCreateShell("vim_xterm", "Vim_xterm",
7559 applicationShellWidgetClass, xterm_dpy,
7560 NULL);
7561 if (AppShell == (Widget)0)
7562 return;
7563 xterm_Shell = XtVaCreatePopupShell("VIM",
7564 topLevelShellWidgetClass, AppShell,
7565 XtNmappedWhenManaged, 0,
7566 XtNwidth, 1,
7567 XtNheight, 1,
7568 NULL);
7569 if (xterm_Shell == (Widget)0)
7570 return;
7571
7572 x11_setup_atoms(xterm_dpy);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02007573 x11_setup_selection(xterm_Shell);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (x11_display == NULL)
7575 x11_display = xterm_dpy;
7576
7577 XtRealizeWidget(xterm_Shell);
7578 XSync(xterm_dpy, False);
7579 xterm_update();
7580 }
7581 if (xterm_Shell != (Widget)0)
7582 {
7583 clip_init(TRUE);
7584 if (x11_window == 0 && (strp = getenv("WINDOWID")) != NULL)
7585 x11_window = (Window)atol(strp);
Bram Moolenaar0f873732019-12-05 20:28:46 +01007586 // Check if $WINDOWID is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (test_x11_window(xterm_dpy) == FAIL)
7588 x11_window = 0;
7589 if (x11_window != 0)
7590 xterm_trace = 0;
7591 }
7592}
7593
7594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007595start_xterm_trace(int button)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 if (x11_window == 0 || xterm_trace < 0 || xterm_Shell == (Widget)0)
7598 return;
7599 xterm_trace = 1;
7600 xterm_button = button;
7601 do_xterm_trace();
7602}
7603
7604
7605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007606stop_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 if (xterm_trace < 0)
7609 return;
7610 xterm_trace = 0;
7611}
7612
7613/*
7614 * Query the xterm pointer and generate mouse termcodes if necessary
7615 * return TRUE if dragging is active, else FALSE
7616 */
7617 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007618do_xterm_trace(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 Window root, child;
7621 int root_x, root_y;
7622 int win_x, win_y;
7623 int row, col;
7624 int_u mask_return;
7625 char_u buf[50];
7626 char_u *strp;
7627 long got_hints;
7628 static char_u *mouse_code;
7629 static char_u mouse_name[2] = {KS_MOUSE, KE_FILLER};
7630 static int prev_row = 0, prev_col = 0;
7631 static XSizeHints xterm_hints;
7632
7633 if (xterm_trace <= 0)
7634 return FALSE;
7635
7636 if (xterm_trace == 1)
7637 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007638 // Get the hints just before tracking starts. The font size might
7639 // have changed recently.
Bram Moolenaara6c2c912008-01-13 15:31:00 +00007640 if (!XGetWMNormalHints(xterm_dpy, x11_window, &xterm_hints, &got_hints)
7641 || !(got_hints & PResizeInc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 || xterm_hints.width_inc <= 1
7643 || xterm_hints.height_inc <= 1)
7644 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007645 xterm_trace = -1; // Not enough data -- disable tracing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 return FALSE;
7647 }
7648
Bram Moolenaar0f873732019-12-05 20:28:46 +01007649 // Rely on the same mouse code for the duration of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 mouse_code = find_termcode(mouse_name);
7651 prev_row = mouse_row;
Bram Moolenaarcde88542015-08-11 19:14:00 +02007652 prev_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 xterm_trace = 2;
7654
Bram Moolenaar0f873732019-12-05 20:28:46 +01007655 // Find the offset of the chars, there might be a scrollbar on the
7656 // left of the window and/or a menu on the top (eterm etc.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7658 &win_x, &win_y, &mask_return);
7659 xterm_hints.y = win_y - (xterm_hints.height_inc * mouse_row)
7660 - (xterm_hints.height_inc / 2);
7661 if (xterm_hints.y <= xterm_hints.height_inc / 2)
7662 xterm_hints.y = 2;
7663 xterm_hints.x = win_x - (xterm_hints.width_inc * mouse_col)
7664 - (xterm_hints.width_inc / 2);
7665 if (xterm_hints.x <= xterm_hints.width_inc / 2)
7666 xterm_hints.x = 2;
7667 return TRUE;
7668 }
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007669 if (mouse_code == NULL || STRLEN(mouse_code) > 45)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
7671 xterm_trace = 0;
7672 return FALSE;
7673 }
7674
7675 XQueryPointer(xterm_dpy, x11_window, &root, &child, &root_x, &root_y,
7676 &win_x, &win_y, &mask_return);
7677
7678 row = check_row((win_y - xterm_hints.y) / xterm_hints.height_inc);
7679 col = check_col((win_x - xterm_hints.x) / xterm_hints.width_inc);
7680 if (row == prev_row && col == prev_col)
7681 return TRUE;
7682
7683 STRCPY(buf, mouse_code);
7684 strp = buf + STRLEN(buf);
7685 *strp++ = (xterm_button | MOUSE_DRAG) & ~0x20;
7686 *strp++ = (char_u)(col + ' ' + 1);
7687 *strp++ = (char_u)(row + ' ' + 1);
7688 *strp = 0;
7689 add_to_input_buf(buf, STRLEN(buf));
7690
7691 prev_row = row;
7692 prev_col = col;
7693 return TRUE;
7694}
7695
Bram Moolenaard4aa83a2019-05-09 18:59:31 +02007696# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697/*
7698 * Destroy the display, window and app_context. Required for GTK.
7699 */
7700 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007701clear_xterm_clip(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703 if (xterm_Shell != (Widget)0)
7704 {
7705 XtDestroyWidget(xterm_Shell);
7706 xterm_Shell = (Widget)0;
7707 }
7708 if (xterm_dpy != NULL)
7709 {
Bram Moolenaare8208012008-06-20 09:59:25 +00007710# if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01007711 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 XtCloseDisplay(xterm_dpy);
Bram Moolenaare8208012008-06-20 09:59:25 +00007713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 if (x11_display == xterm_dpy)
7715 x11_display = NULL;
7716 xterm_dpy = NULL;
7717 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007718# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (app_context != (XtAppContext)NULL)
7720 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007721 // Lesstif and Solaris crash here, lose some memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 XtDestroyApplicationContext(app_context);
7723 app_context = (XtAppContext)NULL;
7724 }
Bram Moolenaare8208012008-06-20 09:59:25 +00007725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726}
7727# endif
7728
7729/*
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007730 * Catch up with GUI or X events.
7731 */
7732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007733clip_update(void)
Bram Moolenaar090cfc12013-03-19 12:35:42 +01007734{
7735# ifdef FEAT_GUI
7736 if (gui.in_use)
7737 gui_mch_update();
7738 else
7739# endif
7740 if (xterm_Shell != (Widget)0)
7741 xterm_update();
7742}
7743
7744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 * Catch up with any queued X events. This may put keyboard input into the
7746 * input buffer, call resize call-backs, trigger timers etc. If there is
7747 * nothing in the X event queue (& no timers pending), then we return
7748 * immediately.
7749 */
7750 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007751xterm_update(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 XEvent event;
7754
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007755 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007757 XtInputMask mask = XtAppPending(app_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007759 if (mask == 0 || vim_is_input_buf_full())
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007760 break;
7761
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007762 if (mask & XtIMXEvent)
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007763 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007764 // There is an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007765 XtAppNextEvent(app_context, &event);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007766#ifdef FEAT_CLIENTSERVER
7767 {
7768 XPropertyEvent *e = (XPropertyEvent *)&event;
7769
7770 if (e->type == PropertyNotify && e->window == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 && e->atom == commProperty && e->state == PropertyNewValue)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007772 serverEventProc(xterm_dpy, &event, 0);
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007775 XtDispatchEvent(&event);
7776 }
Bram Moolenaarb1fc2bf2015-03-20 16:26:54 +01007777 else
7778 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007779 // There is something else than an event to process.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007780 XtAppProcessEvent(app_context, mask);
7781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783}
7784
7785 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007786clip_xterm_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 if (xterm_Shell != (Widget)0)
7789 return clip_x11_own_selection(xterm_Shell, cbd);
7790 return FAIL;
7791}
7792
7793 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007794clip_xterm_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 if (xterm_Shell != (Widget)0)
7797 clip_x11_lose_selection(xterm_Shell, cbd);
7798}
7799
7800 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007801clip_xterm_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
7803 if (xterm_Shell != (Widget)0)
7804 clip_x11_request_selection(xterm_Shell, xterm_dpy, cbd);
7805}
7806
7807 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007808clip_xterm_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 clip_x11_set_selection(cbd);
7811}
7812#endif
7813
7814
7815#if defined(USE_XSMP) || defined(PROTO)
7816/*
7817 * Code for X Session Management Protocol.
7818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
7820# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821/*
7822 * This is our chance to ask the user if they want to save,
7823 * or abort the logout
7824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007826xsmp_handle_interaction(SmcConn smc_conn, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 cmdmod_T save_cmdmod;
7829 int cancel_shutdown = False;
7830
7831 save_cmdmod = cmdmod;
7832 cmdmod.confirm = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007833 if (check_changed_any(FALSE, FALSE))
Bram Moolenaar0f873732019-12-05 20:28:46 +01007834 // Mustn't logout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 cancel_shutdown = True;
7836 cmdmod = save_cmdmod;
Bram Moolenaar0f873732019-12-05 20:28:46 +01007837 setcursor(); // position cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 out_flush();
7839
Bram Moolenaar0f873732019-12-05 20:28:46 +01007840 // Done interaction
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 SmcInteractDone(smc_conn, cancel_shutdown);
7842
Bram Moolenaar0f873732019-12-05 20:28:46 +01007843 // Finish off
7844 // Only end save-yourself here if we're not cancelling shutdown;
7845 // we'll get a cancelled callback later in which we'll end it.
7846 // Hopefully get around glitchy SMs (like GNOME-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 if (!cancel_shutdown)
7848 {
7849 xsmp.save_yourself = False;
7850 SmcSaveYourselfDone(smc_conn, True);
7851 }
7852}
7853# endif
7854
7855/*
7856 * Callback that starts save-yourself.
7857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007859xsmp_handle_save_yourself(
7860 SmcConn smc_conn,
7861 SmPointer client_data UNUSED,
7862 int save_type UNUSED,
7863 Bool shutdown,
7864 int interact_style UNUSED,
7865 Bool fast UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007867 // Handle already being in saveyourself
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 if (xsmp.save_yourself)
7869 SmcSaveYourselfDone(smc_conn, True);
7870 xsmp.save_yourself = True;
7871 xsmp.shutdown = shutdown;
7872
Bram Moolenaar0f873732019-12-05 20:28:46 +01007873 // First up, preserve all files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 out_flush();
Bram Moolenaar0f873732019-12-05 20:28:46 +01007875 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
7877 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007878 verb_msg(_("XSMP handling save-yourself request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879
7880# if defined(FEAT_GUI) && defined(USE_XSMP_INTERACT)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007881 // Now see if we can ask about unsaved files
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (shutdown && !fast && gui.in_use)
Bram Moolenaar0f873732019-12-05 20:28:46 +01007883 // Need to interact with user, but need SM's permission
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 SmcInteractRequest(smc_conn, SmDialogError,
7885 xsmp_handle_interaction, client_data);
7886 else
7887# endif
7888 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007889 // Can stop the cycle here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 SmcSaveYourselfDone(smc_conn, True);
7891 xsmp.save_yourself = False;
7892 }
7893}
7894
7895
7896/*
7897 * Callback to warn us of imminent death.
7898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007900xsmp_die(SmcConn smc_conn UNUSED, SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 xsmp_close();
7903
Bram Moolenaar0f873732019-12-05 20:28:46 +01007904 // quit quickly leaving swapfiles for modified buffers behind
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 getout_preserve_modified(0);
7906}
7907
7908
7909/*
7910 * Callback to tell us that save-yourself has completed.
7911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007913xsmp_save_complete(
7914 SmcConn smc_conn UNUSED,
7915 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 xsmp.save_yourself = False;
7918}
7919
7920
7921/*
7922 * Callback to tell us that an instigated shutdown was cancelled
7923 * (maybe even by us)
7924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007926xsmp_shutdown_cancelled(
7927 SmcConn smc_conn,
7928 SmPointer client_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 if (xsmp.save_yourself)
7931 SmcSaveYourselfDone(smc_conn, True);
7932 xsmp.save_yourself = False;
7933 xsmp.shutdown = False;
7934}
7935
7936
7937/*
7938 * Callback to tell us that a new ICE connection has been established.
7939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007941xsmp_ice_connection(
7942 IceConn iceConn,
7943 IcePointer clientData UNUSED,
7944 Bool opening,
7945 IcePointer *watchData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
Bram Moolenaar0f873732019-12-05 20:28:46 +01007947 // Intercept creation of ICE connection fd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 if (opening)
7949 {
7950 xsmp_icefd = IceConnectionNumber(iceConn);
7951 IceRemoveConnectionWatch(xsmp_ice_connection, NULL);
7952 }
7953}
7954
7955
Bram Moolenaar0f873732019-12-05 20:28:46 +01007956// Handle any ICE processing that's required; return FAIL if SM lost
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007958xsmp_handle_requests(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 Bool rep;
7961
7962 if (IceProcessMessages(xsmp.iceconn, NULL, &rep)
7963 == IceProcessMessagesIOError)
7964 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01007965 // Lost ICE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007967 verb_msg(_("XSMP lost ICE connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 xsmp_close();
7969 return FAIL;
7970 }
7971 else
7972 return OK;
7973}
7974
7975static int dummy;
7976
Bram Moolenaar0f873732019-12-05 20:28:46 +01007977// Set up X Session Management Protocol
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 void
7979xsmp_init(void)
7980{
7981 char errorstring[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 SmcCallbacks smcallbacks;
7983#if 0
7984 SmPropValue smname;
7985 SmProp smnameprop;
7986 SmProp *smprops[1];
7987#endif
7988
7989 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007990 verb_msg(_("XSMP opening connection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
7992 xsmp.save_yourself = xsmp.shutdown = False;
7993
Bram Moolenaar0f873732019-12-05 20:28:46 +01007994 // Set up SM callbacks - must have all, even if they're not used
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 smcallbacks.save_yourself.callback = xsmp_handle_save_yourself;
7996 smcallbacks.save_yourself.client_data = NULL;
7997 smcallbacks.die.callback = xsmp_die;
7998 smcallbacks.die.client_data = NULL;
7999 smcallbacks.save_complete.callback = xsmp_save_complete;
8000 smcallbacks.save_complete.client_data = NULL;
8001 smcallbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
8002 smcallbacks.shutdown_cancelled.client_data = NULL;
8003
Bram Moolenaar0f873732019-12-05 20:28:46 +01008004 // Set up a watch on ICE connection creations. The "dummy" argument is
8005 // apparently required for FreeBSD (we get a BUS error when using NULL).
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (IceAddConnectionWatch(xsmp_ice_connection, &dummy) == 0)
8007 {
8008 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008009 verb_msg(_("XSMP ICE connection watch failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 return;
8011 }
8012
Bram Moolenaar0f873732019-12-05 20:28:46 +01008013 // Create an SM connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 xsmp.smcconn = SmcOpenConnection(
8015 NULL,
8016 NULL,
8017 SmProtoMajor,
8018 SmProtoMinor,
8019 SmcSaveYourselfProcMask | SmcDieProcMask
8020 | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
8021 &smcallbacks,
8022 NULL,
Bram Moolenaare8208012008-06-20 09:59:25 +00008023 &xsmp.clientid,
Bram Moolenaar4841a7c2018-09-22 14:08:49 +02008024 sizeof(errorstring) - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 errorstring);
8026 if (xsmp.smcconn == NULL)
8027 {
8028 char errorreport[132];
Bram Moolenaar051b7822005-05-19 21:00:46 +00008029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (p_verbose > 0)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008031 {
8032 vim_snprintf(errorreport, sizeof(errorreport),
8033 _("XSMP SmcOpenConnection failed: %s"), errorstring);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008034 verb_msg(errorreport);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00008035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 return;
8037 }
8038 xsmp.iceconn = SmcGetIceConnection(xsmp.smcconn);
8039
8040#if 0
Bram Moolenaar0f873732019-12-05 20:28:46 +01008041 // ID ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 smname.value = "vim";
8043 smname.length = 3;
8044 smnameprop.name = "SmProgram";
8045 smnameprop.type = "SmARRAY8";
8046 smnameprop.num_vals = 1;
8047 smnameprop.vals = &smname;
8048
8049 smprops[0] = &smnameprop;
8050 SmcSetProperties(xsmp.smcconn, 1, smprops);
8051#endif
8052}
8053
8054
Bram Moolenaar0f873732019-12-05 20:28:46 +01008055// Shut down XSMP comms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008057xsmp_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
8059 if (xsmp_icefd != -1)
8060 {
8061 SmcCloseConnection(xsmp.smcconn, 0, NULL);
Bram Moolenaar5a221812008-11-12 12:08:45 +00008062 if (xsmp.clientid != NULL)
8063 free(xsmp.clientid);
Bram Moolenaare8208012008-06-20 09:59:25 +00008064 xsmp.clientid = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 xsmp_icefd = -1;
8066 }
8067}
Bram Moolenaar0f873732019-12-05 20:28:46 +01008068#endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
8070
8071#ifdef EBCDIC
Bram Moolenaar0f873732019-12-05 20:28:46 +01008072// Translate character to its CTRL- value
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073char CtrlTable[] =
8074{
8075/* 00 - 5E */
8076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8079 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8080 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8081 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8082/* ^ */ 0x1E,
8083/* - */ 0x1F,
8084/* 61 - 6C */
8085 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8086/* _ */ 0x1F,
8087/* 6E - 80 */
8088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8089/* a */ 0x01,
8090/* b */ 0x02,
8091/* c */ 0x03,
8092/* d */ 0x37,
8093/* e */ 0x2D,
8094/* f */ 0x2E,
8095/* g */ 0x2F,
8096/* h */ 0x16,
8097/* i */ 0x05,
8098/* 8A - 90 */
8099 0, 0, 0, 0, 0, 0, 0,
8100/* j */ 0x15,
8101/* k */ 0x0B,
8102/* l */ 0x0C,
8103/* m */ 0x0D,
8104/* n */ 0x0E,
8105/* o */ 0x0F,
8106/* p */ 0x10,
8107/* q */ 0x11,
8108/* r */ 0x12,
8109/* 9A - A1 */
8110 0, 0, 0, 0, 0, 0, 0, 0,
8111/* s */ 0x13,
8112/* t */ 0x3C,
8113/* u */ 0x3D,
8114/* v */ 0x32,
8115/* w */ 0x26,
8116/* x */ 0x18,
8117/* y */ 0x19,
8118/* z */ 0x3F,
8119/* AA - AC */
8120 0, 0, 0,
8121/* [ */ 0x27,
8122/* AE - BC */
8123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8124/* ] */ 0x1D,
8125/* BE - C0 */ 0, 0, 0,
8126/* A */ 0x01,
8127/* B */ 0x02,
8128/* C */ 0x03,
8129/* D */ 0x37,
8130/* E */ 0x2D,
8131/* F */ 0x2E,
8132/* G */ 0x2F,
8133/* H */ 0x16,
8134/* I */ 0x05,
8135/* CA - D0 */ 0, 0, 0, 0, 0, 0, 0,
8136/* J */ 0x15,
8137/* K */ 0x0B,
8138/* L */ 0x0C,
8139/* M */ 0x0D,
8140/* N */ 0x0E,
8141/* O */ 0x0F,
8142/* P */ 0x10,
8143/* Q */ 0x11,
8144/* R */ 0x12,
8145/* DA - DF */ 0, 0, 0, 0, 0, 0,
8146/* \ */ 0x1C,
8147/* E1 */ 0,
8148/* S */ 0x13,
8149/* T */ 0x3C,
8150/* U */ 0x3D,
8151/* V */ 0x32,
8152/* W */ 0x26,
8153/* X */ 0x18,
8154/* Y */ 0x19,
8155/* Z */ 0x3F,
8156/* EA - FF*/ 0, 0, 0, 0, 0, 0,
8157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8158};
8159
8160char MetaCharTable[]=
Bram Moolenaar0f873732019-12-05 20:28:46 +01008161{// 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 0, 0, 0, 0,'\\', 0,'F', 0,'W','M','N', 0, 0, 0, 0, 0,
8163 0, 0, 0, 0,']', 0, 0,'G', 0, 0,'R','O', 0, 0, 0, 0,
8164 '@','A','B','C','D','E', 0, 0,'H','I','J','K','L', 0, 0, 0,
8165 'P','Q', 0,'S','T','U','V', 0,'X','Y','Z','[', 0, 0,'^', 0
8166};
8167
8168
Bram Moolenaar0f873732019-12-05 20:28:46 +01008169// TODO: Use characters NOT numbers!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170char CtrlCharTable[]=
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 124,193,194,195, 0,201, 0, 0, 0, 0, 0,210,211,212,213,214,
8173 215,216,217,226, 0,209,200, 0,231,232, 0, 0,224,189, 95,109,
8174 0, 0, 0, 0, 0, 0,230,173, 0, 0, 0, 0, 0,197,198,199,
8175 0, 0,229, 0, 0, 0, 0,196, 0, 0, 0, 0,227,228, 0,233,
8176};
8177
8178
8179#endif